/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/primitives/IMSIImpl.java

http://mobicents.googlecode.com/ · Java · 198 lines · 113 code · 33 blank · 52 comment · 17 complexity · 7e84adf3a03a0554dd0a7bd5031b4c12 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.primitives;
  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. /**
  33. *
  34. * @author sergey vetyutnev
  35. *
  36. */
  37. public class IMSIImpl extends TbcdString implements IMSI {
  38. private String data;
  39. public IMSIImpl() {
  40. }
  41. public IMSIImpl(String data) {
  42. this.data = data;
  43. }
  44. @Override
  45. public String getData() {
  46. return this.data;
  47. }
  48. @Override
  49. public int getTag() throws MAPException {
  50. return Tag.STRING_OCTET;
  51. }
  52. @Override
  53. public int getTagClass() {
  54. return Tag.CLASS_UNIVERSAL;
  55. }
  56. @Override
  57. public boolean getIsPrimitive() {
  58. return true;
  59. }
  60. @Override
  61. public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
  62. try {
  63. int length = ansIS.readLength();
  64. this._decode(ansIS, length);
  65. } catch (IOException e) {
  66. throw new MAPParsingComponentException("IOException when decoding IMSI: " + e.getMessage(), e,
  67. MAPParsingComponentExceptionReason.MistypedParameter);
  68. }
  69. }
  70. @Override
  71. public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
  72. try {
  73. this._decode(ansIS, length);
  74. } catch (IOException e) {
  75. throw new MAPParsingComponentException("IOException when decoding IMSI: " + e.getMessage(), e,
  76. MAPParsingComponentExceptionReason.MistypedParameter);
  77. }
  78. }
  79. private void _decode(AsnInputStream ansIS, int length) throws MAPParsingComponentException, IOException {
  80. if (length < 3 || length > 8)
  81. throw new MAPParsingComponentException("Error decoding IMSI: the IMSI field must contain from 3 to 8 octets. Contains: " + length,
  82. MAPParsingComponentExceptionReason.MistypedParameter);
  83. try {
  84. String res = this.decodeString(ansIS, length);
  85. this.data = res;
  86. // String sMcc = res.substring(0, 3);
  87. // String sMnc = res.substring(3, 5);
  88. // this.MSIN = res.substring(5);
  89. //
  90. // this.MCC = (long) Integer.parseInt(sMcc);
  91. // this.MNC = (long) Integer.parseInt(sMnc);
  92. } catch (IOException e) {
  93. throw new MAPParsingComponentException("IOException when decoding IMSI: " + e.getMessage(), e,
  94. MAPParsingComponentExceptionReason.MistypedParameter);
  95. }
  96. }
  97. @Override
  98. public void encodeAll(AsnOutputStream asnOs) throws MAPException {
  99. this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.STRING_OCTET);
  100. }
  101. @Override
  102. public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
  103. try {
  104. asnOs.writeTag(tagClass, true, tag);
  105. int pos = asnOs.StartContentDefiniteLength();
  106. this.encodeData(asnOs);
  107. asnOs.FinalizeContent(pos);
  108. } catch (AsnException e) {
  109. throw new MAPException("AsnException when encoding IMSI: " + e.getMessage(), e);
  110. }
  111. }
  112. @Override
  113. public void encodeData(AsnOutputStream asnOs) throws MAPException {
  114. if (this.data == null)
  115. throw new MAPException("Error while encoding the IMSI: data is not defined");
  116. // if (this.MCC == null || this.MNC == null || this.MSIN == null)
  117. // throw new MAPException("Error while encoding the IMSI: MMC, MNC or MSIN is not defined");
  118. //
  119. // if (this.MCC < 0 || this.MCC > 999)
  120. // throw new MAPException("Error while encoding the IMSI: Bad MCC value");
  121. // if (this.MNC < 0 || this.MNC > 99)
  122. // throw new MAPException("Error while encoding the IMSI: Bad MNC value");
  123. // if (this.MSIN.length() < 1 || this.MSIN.length() > 11)
  124. // throw new MAPException("Error while encoding the IMSI: Bad MSIN value");
  125. //
  126. // StringBuilder sb = new StringBuilder();
  127. // if (this.MCC < 100)
  128. // sb.append("0");
  129. // if (this.MCC < 10)
  130. // sb.append("0");
  131. // sb.append(this.MCC);
  132. // if (this.MNC < 10)
  133. // sb.append("0");
  134. // sb.append(this.MNC);
  135. // sb.append(this.MSIN);
  136. this.encodeString(asnOs, this.data);
  137. }
  138. @Override
  139. public String toString() {
  140. return "IMSI [" + this.data + "]";
  141. }
  142. @Override
  143. public int hashCode() {
  144. final int prime = 31;
  145. int result = 1;
  146. result = prime * result + ((data == null) ? 0 : data.hashCode());
  147. return result;
  148. }
  149. @Override
  150. public boolean equals(Object obj) {
  151. if (this == obj)
  152. return true;
  153. if (obj == null)
  154. return false;
  155. if (getClass() != obj.getClass())
  156. return false;
  157. IMSIImpl other = (IMSIImpl) obj;
  158. if (data == null) {
  159. if (other.data != null)
  160. return false;
  161. } else if (!data.equals(other.data))
  162. return false;
  163. return true;
  164. }
  165. }