PageRenderTime 14ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/service/lsm/LCSCodewordImpl.java

http://mobicents.googlecode.com/
Java | 290 lines | 154 code | 34 blank | 102 comment | 31 complexity | 1a57ba65c638c7630f683d41b7e037ca MD5 | raw file
  1/*
  2 * JBoss, Home of Professional Open Source
  3 * Copyright 2011, Red Hat, Inc. and individual contributors
  4 * by the @authors tag. See the copyright.txt in the distribution for a
  5 * full listing of individual contributors.
  6 *
  7 * This is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU Lesser General Public License as
  9 * published by the Free Software Foundation; either version 2.1 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This software is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * Lesser General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this software; if not, write to the Free
 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21 */
 22
 23package org.mobicents.protocols.ss7.map.service.lsm;
 24
 25import java.io.IOException;
 26
 27import org.mobicents.protocols.asn.AsnException;
 28import org.mobicents.protocols.asn.AsnInputStream;
 29import org.mobicents.protocols.asn.AsnOutputStream;
 30import org.mobicents.protocols.asn.Tag;
 31import org.mobicents.protocols.ss7.map.api.MAPException;
 32import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
 33import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
 34import org.mobicents.protocols.ss7.map.api.primitives.USSDString;
 35import org.mobicents.protocols.ss7.map.api.service.lsm.LCSCodeword;
 36import org.mobicents.protocols.ss7.map.primitives.MAPAsnPrimitive;
 37import org.mobicents.protocols.ss7.map.primitives.USSDStringImpl;
 38
 39/**
 40 * @author amit bhayani
 41 * 
 42 */
 43public class LCSCodewordImpl implements LCSCodeword, MAPAsnPrimitive {
 44
 45	private static final int _TAG_DATA_CODING_SCHEME = 0;
 46	private static final int _TAG_LCS_CODE_WORD_STRING = 1;
 47
 48	private byte dataCodingScheme;
 49	private USSDString lcsCodewordString = null;
 50
 51	/**
 52	 * 
 53	 */
 54	public LCSCodewordImpl() {
 55		super();
 56		// TODO Auto-generated constructor stub
 57	}
 58
 59	/**
 60	 * @param dataCodingScheme
 61	 * @param lcsCodewordString
 62	 */
 63	public LCSCodewordImpl(byte dataCodingScheme, USSDString lcsCodewordString) {
 64		super();
 65		this.dataCodingScheme = dataCodingScheme;
 66		this.lcsCodewordString = lcsCodewordString;
 67	}
 68
 69	/*
 70	 * (non-Javadoc)
 71	 * 
 72	 * @see org.mobicents.protocols.ss7.map.api.service.lsm.LCSCodeword#
 73	 * getDataCodingScheme()
 74	 */
 75	@Override
 76	public byte getDataCodingScheme() {
 77		return this.dataCodingScheme;
 78	}
 79
 80	/*
 81	 * (non-Javadoc)
 82	 * 
 83	 * @see org.mobicents.protocols.ss7.map.api.service.lsm.LCSCodeword#
 84	 * getLCSCodewordString()
 85	 */
 86	@Override
 87	public USSDString getLCSCodewordString() {
 88		return this.lcsCodewordString;
 89	}
 90
 91	/*
 92	 * (non-Javadoc)
 93	 * 
 94	 * @see
 95	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTag()
 96	 */
 97	@Override
 98	public int getTag() throws MAPException {
 99		return Tag.SEQUENCE;
100	}
101
102	/*
103	 * (non-Javadoc)
104	 * 
105	 * @see
106	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTagClass
107	 * ()
108	 */
109	@Override
110	public int getTagClass() {
111		return Tag.CLASS_UNIVERSAL;
112	}
113
114	/*
115	 * (non-Javadoc)
116	 * 
117	 * @see
118	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getIsPrimitive
119	 * ()
120	 */
121	@Override
122	public boolean getIsPrimitive() {
123		return false;
124	}
125
126	/*
127	 * (non-Javadoc)
128	 * 
129	 * @see
130	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeAll
131	 * (org.mobicents.protocols.asn.AsnInputStream)
132	 */
133	@Override
134	public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
135		try {
136			int length = ansIS.readLength();
137			this._decode(ansIS, length);
138		} catch (IOException e) {
139			throw new MAPParsingComponentException("IOException when decoding LCSClientName: " + e.getMessage(), e,
140					MAPParsingComponentExceptionReason.MistypedParameter);
141		} catch (AsnException e) {
142			throw new MAPParsingComponentException("AsnException when decoding LCSClientName: " + e.getMessage(), e,
143					MAPParsingComponentExceptionReason.MistypedParameter);
144		}
145	}
146
147	/*
148	 * (non-Javadoc)
149	 * 
150	 * @see
151	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeData
152	 * (org.mobicents.protocols.asn.AsnInputStream, int)
153	 */
154	@Override
155	public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
156		try {
157			this._decode(ansIS, length);
158		} catch (IOException e) {
159			throw new MAPParsingComponentException("IOException when decoding LCSCodeword: " + e.getMessage(), e,
160					MAPParsingComponentExceptionReason.MistypedParameter);
161		} catch (AsnException e) {
162			throw new MAPParsingComponentException("AsnException when decoding LCSCodeword: " + e.getMessage(), e,
163					MAPParsingComponentExceptionReason.MistypedParameter);
164		}
165	}
166
167	private void _decode(AsnInputStream asnIS, int length) throws MAPParsingComponentException, IOException, AsnException {
168
169		AsnInputStream ais = asnIS.readSequenceStreamData(length);
170
171		int tag = ais.readTag();
172
173		// Decode mandatory dataCodingScheme [0] USSD-DataCodingScheme,
174		if (ais.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC || !ais.isTagPrimitive() || tag != _TAG_DATA_CODING_SCHEME) {
175			throw new MAPParsingComponentException(
176					"Error while decoding LCSCodeword: Parameter 0[dataCodingScheme [0] USSD-DataCodingScheme] bad tag class, tag or not primitive",
177					MAPParsingComponentExceptionReason.MistypedParameter);
178		}
179
180		int length1 = ais.readLength();
181		this.dataCodingScheme = ais.readOctetStringData(length1)[0];
182
183		tag = ais.readTag();
184
185		// Decode mandatory lcsCodewordString [1] LCSCodewordString,
186		if (ais.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC || !ais.isTagPrimitive() || tag != _TAG_LCS_CODE_WORD_STRING) {
187			throw new MAPParsingComponentException(
188					"Error while decoding LCSCodeword: Parameter 1[lcsCodewordString [1] LCSCodewordString] bad tag class, tag or not primitive",
189					MAPParsingComponentExceptionReason.MistypedParameter);
190		}
191
192		this.lcsCodewordString = new USSDStringImpl();
193		((USSDStringImpl)this.lcsCodewordString).decodeAll(ais);
194		
195
196		while (true) {
197			if (ais.available() == 0)
198				break;
199			switch (ais.readTag()) {
200			default:
201				ais.advanceElement();
202				break;
203			}
204		}
205	}
206
207	/*
208	 * (non-Javadoc)
209	 * 
210	 * @see
211	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll
212	 * (org.mobicents.protocols.asn.AsnOutputStream)
213	 */
214	@Override
215	public void encodeAll(AsnOutputStream asnOs) throws MAPException {
216		this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.SEQUENCE);
217	}
218
219	/*
220	 * (non-Javadoc)
221	 * 
222	 * @see
223	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll
224	 * (org.mobicents.protocols.asn.AsnOutputStream, int, int)
225	 */
226	@Override
227	public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
228		try {
229			asnOs.writeTag(tagClass, false, tag);
230			int pos = asnOs.StartContentDefiniteLength();
231			this.encodeData(asnOs);
232			asnOs.FinalizeContent(pos);
233		} catch (AsnException e) {
234			throw new MAPException("AsnException when encoding LCSClientName", e);
235		}
236	}
237
238	/*
239	 * (non-Javadoc)
240	 * 
241	 * @see
242	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeData
243	 * (org.mobicents.protocols.asn.AsnOutputStream)
244	 */
245	@Override
246	public void encodeData(AsnOutputStream asnOs) throws MAPException {
247
248		if (this.lcsCodewordString == null)
249			throw new MAPException("lcsCodewordString must not be null");
250
251		try {
252			asnOs.writeOctetString(Tag.CLASS_CONTEXT_SPECIFIC, _TAG_DATA_CODING_SCHEME, new byte[] { this.dataCodingScheme });
253
254			((USSDStringImpl)this.lcsCodewordString).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _TAG_LCS_CODE_WORD_STRING);
255		} catch (IOException e) {
256			throw new MAPException("IOException when encoding LCSClientName", e);
257		} catch (AsnException e) {
258			throw new MAPException("AsnException when encoding LCSClientName", e);
259		}
260	}
261
262	@Override
263	public int hashCode() {
264		final int prime = 31;
265		int result = 1;
266		result = prime * result + dataCodingScheme;
267		result = prime * result + ((lcsCodewordString == null) ? 0 : lcsCodewordString.hashCode());
268		return result;
269	}
270
271	@Override
272	public boolean equals(Object obj) {
273		if (this == obj)
274			return true;
275		if (obj == null)
276			return false;
277		if (getClass() != obj.getClass())
278			return false;
279		LCSCodewordImpl other = (LCSCodewordImpl) obj;
280		if (dataCodingScheme != other.dataCodingScheme)
281			return false;
282		if (lcsCodewordString == null) {
283			if (other.lcsCodewordString != null)
284				return false;
285		} else if (!lcsCodewordString.equals(other.lcsCodewordString))
286			return false;
287		return true;
288	}
289
290}