PageRenderTime 67ms CodeModel.GetById 45ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://mobicents.googlecode.com/
Java | 279 lines | 152 code | 24 blank | 103 comment | 30 complexity | 85ab9cda0a02d89d2e9a1085a80fbedf 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.IMSI;
 35import org.mobicents.protocols.ss7.map.api.primitives.ISDNAddressString;
 36import org.mobicents.protocols.ss7.map.api.service.lsm.SubscriberIdentity;
 37import org.mobicents.protocols.ss7.map.primitives.IMSIImpl;
 38import org.mobicents.protocols.ss7.map.primitives.ISDNAddressStringImpl;
 39import org.mobicents.protocols.ss7.map.primitives.MAPAsnPrimitive;
 40
 41/**
 42 * @author amit bhayani
 43 * 
 44 */
 45public class SubscriberIdentityImpl implements SubscriberIdentity, MAPAsnPrimitive {
 46	private static final int _TAG_IMSI = 0;
 47	private static final int _TAG_MSISDN = 1;
 48
 49	private IMSI imsi = null;
 50	private ISDNAddressString msisdn = null;
 51
 52	/**
 53	 * 
 54	 */
 55	public SubscriberIdentityImpl() {
 56		super();
 57	}
 58
 59	/**
 60	 * @param imsi
 61	 */
 62	public SubscriberIdentityImpl(IMSI imsi) {
 63		super();
 64		this.imsi = imsi;
 65		this.msisdn = null;
 66	}
 67
 68	/**
 69	 * @param msisdn
 70	 */
 71	public SubscriberIdentityImpl(ISDNAddressString msisdn) {
 72		super();
 73		this.msisdn = msisdn;
 74		this.imsi = null;
 75	}
 76
 77	/*
 78	 * (non-Javadoc)
 79	 * 
 80	 * @see
 81	 * org.mobicents.protocols.ss7.map.api.service.lsm.SubscriberIdentity#getIMSI
 82	 * ()
 83	 */
 84	@Override
 85	public IMSI getIMSI() {
 86		return this.imsi;
 87	}
 88
 89	/*
 90	 * (non-Javadoc)
 91	 * 
 92	 * @see
 93	 * org.mobicents.protocols.ss7.map.api.service.lsm.SubscriberIdentity#getMSISDN
 94	 * ()
 95	 */
 96	@Override
 97	public ISDNAddressString getMSISDN() {
 98		return this.msisdn;
 99	}
100
101	/*
102	 * (non-Javadoc)
103	 * 
104	 * @see
105	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTag()
106	 */
107	@Override
108	public int getTag() throws MAPException {
109		if (this.imsi != null) {
110			return _TAG_IMSI;
111		} else {
112			return _TAG_MSISDN;
113		}
114	}
115
116	/*
117	 * (non-Javadoc)
118	 * 
119	 * @see
120	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTagClass
121	 * ()
122	 */
123	@Override
124	public int getTagClass() {
125		return Tag.CLASS_CONTEXT_SPECIFIC;
126	}
127
128	/*
129	 * (non-Javadoc)
130	 * 
131	 * @see
132	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getIsPrimitive
133	 * ()
134	 */
135	@Override
136	public boolean getIsPrimitive() {
137		return true;
138	}
139
140	/*
141	 * (non-Javadoc)
142	 * 
143	 * @see
144	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeAll
145	 * (org.mobicents.protocols.asn.AsnInputStream)
146	 */
147	@Override
148	public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
149		try {
150			int length = ansIS.readLength();
151			this._decode(ansIS, length);
152		} catch (IOException e) {
153			throw new MAPParsingComponentException("IOException when decoding SubscriberIdentity: ", e, MAPParsingComponentExceptionReason.MistypedParameter);
154		} catch (AsnException e) {
155			throw new MAPParsingComponentException("AsnException when decoding SubscriberIdentity: ", e, MAPParsingComponentExceptionReason.MistypedParameter);
156		}
157	}
158
159	/*
160	 * (non-Javadoc)
161	 * 
162	 * @see
163	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeData
164	 * (org.mobicents.protocols.asn.AsnInputStream, int)
165	 */
166	@Override
167	public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
168		try {
169			this._decode(ansIS, length);
170		} catch (IOException e) {
171			throw new MAPParsingComponentException("IOException when decoding SM_RP_DA: " + e.getMessage(), e,
172					MAPParsingComponentExceptionReason.MistypedParameter);
173		} catch (AsnException e) {
174			throw new MAPParsingComponentException("AsnException when decoding SM_RP_DA: " + e.getMessage(), e,
175					MAPParsingComponentExceptionReason.MistypedParameter);
176		}
177	}
178
179	private void _decode(AsnInputStream asnIS, int length) throws MAPParsingComponentException, IOException, AsnException {
180
181		if (asnIS.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC || !asnIS.isTagPrimitive())
182			throw new MAPParsingComponentException("Error while decoding SubscriberIdentity: bad tag class or is not primitive: TagClass="
183					+ asnIS.getTagClass(), MAPParsingComponentExceptionReason.MistypedParameter);
184
185		switch (asnIS.getTag()) {
186		case _TAG_IMSI:
187			this.imsi = new IMSIImpl();
188			((IMSIImpl)this.imsi).decodeData(asnIS, length);
189			break;
190		case _TAG_MSISDN:
191			this.msisdn = new ISDNAddressStringImpl();
192			((ISDNAddressStringImpl)this.msisdn).decodeData(asnIS, length);
193			break;
194		default:
195			throw new MAPParsingComponentException(
196					"Error while decoding SubscriberIdentity: Expexted imsi [0] IMSI or msisdn [1] ISDN-AddressString, but found " + asnIS.getTag(),
197					MAPParsingComponentExceptionReason.MistypedParameter);
198		}
199	}
200
201	/*
202	 * (non-Javadoc)
203	 * 
204	 * @see
205	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll
206	 * (org.mobicents.protocols.asn.AsnOutputStream)
207	 */
208	@Override
209	public void encodeAll(AsnOutputStream asnOs) throws MAPException {
210		this.encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, this.getTag());
211	}
212
213	/*
214	 * (non-Javadoc)
215	 * 
216	 * @see
217	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll
218	 * (org.mobicents.protocols.asn.AsnOutputStream, int, int)
219	 */
220	@Override
221	public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
222		try {
223			asnOs.writeTag(tagClass, true, tag);
224			int pos = asnOs.StartContentDefiniteLength();
225			this.encodeData(asnOs);
226			asnOs.FinalizeContent(pos);
227		} catch (AsnException e) {
228			throw new MAPException("AsnException when encoding AdditionalNumber: " + e.getMessage(), e);
229		}
230	}
231
232	/*
233	 * (non-Javadoc)
234	 * 
235	 * @see
236	 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeData
237	 * (org.mobicents.protocols.asn.AsnOutputStream)
238	 */
239	@Override
240	public void encodeData(AsnOutputStream asnOs) throws MAPException {
241		if (this.imsi != null) {
242			((IMSIImpl)this.imsi).encodeData(asnOs);
243		} else {
244			((ISDNAddressStringImpl)this.msisdn).encodeData(asnOs);
245		}
246	}
247
248	@Override
249	public int hashCode() {
250		final int prime = 31;
251		int result = 1;
252		result = prime * result + ((imsi == null) ? 0 : imsi.hashCode());
253		result = prime * result + ((msisdn == null) ? 0 : msisdn.hashCode());
254		return result;
255	}
256
257	@Override
258	public boolean equals(Object obj) {
259		if (this == obj)
260			return true;
261		if (obj == null)
262			return false;
263		if (getClass() != obj.getClass())
264			return false;
265		SubscriberIdentityImpl other = (SubscriberIdentityImpl) obj;
266		if (imsi == null) {
267			if (other.imsi != null)
268				return false;
269		} else if (!imsi.equals(other.imsi))
270			return false;
271		if (msisdn == null) {
272			if (other.msisdn != null)
273				return false;
274		} else if (!msisdn.equals(other.msisdn))
275			return false;
276		return true;
277	}
278
279}