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