PageRenderTime 36ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 265 lines | 178 code | 55 blank | 32 comment | 31 complexity | 6dbb2055509390881bef221a87cdb41e MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  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 javolution.xml.XMLFormat;
  25. import javolution.xml.stream.XMLStreamException;
  26. import org.mobicents.protocols.asn.AsnException;
  27. import org.mobicents.protocols.asn.AsnInputStream;
  28. import org.mobicents.protocols.asn.AsnOutputStream;
  29. import org.mobicents.protocols.asn.Tag;
  30. import org.mobicents.protocols.ss7.map.api.MAPException;
  31. import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
  32. import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
  33. import org.mobicents.protocols.ss7.map.api.primitives.AddressNature;
  34. import org.mobicents.protocols.ss7.map.api.primitives.AddressString;
  35. import org.mobicents.protocols.ss7.map.api.primitives.NumberingPlan;
  36. /**
  37. *
  38. * @author amit bhayani
  39. * @author sergey vetyutnev
  40. *
  41. */
  42. public class AddressStringImpl extends TbcdString implements AddressString {
  43. private static final String NAI = "nai";
  44. private static final String NPI = "npi";
  45. private static final String NUMBER = "number";
  46. protected int NO_EXTENSION_MASK = 0x80;
  47. protected int NATURE_OF_ADD_IND_MASK = 0x70;
  48. protected int NUMBERING_PLAN_IND_MASK = 0x0F;
  49. protected AddressNature addressNature;
  50. protected NumberingPlan numberingPlan;
  51. protected String address;
  52. private boolean isExtension;
  53. public AddressStringImpl() {
  54. }
  55. public AddressStringImpl(AddressNature addressNature, NumberingPlan numberingPlan, String address) {
  56. super();
  57. this.addressNature = addressNature;
  58. this.numberingPlan = numberingPlan;
  59. this.address = address;
  60. }
  61. public String getAddress() {
  62. return this.address;
  63. }
  64. public AddressNature getAddressNature() {
  65. return this.addressNature;
  66. }
  67. public NumberingPlan getNumberingPlan() {
  68. return this.numberingPlan;
  69. }
  70. public boolean isExtension() {
  71. return isExtension;
  72. }
  73. @Override
  74. public int getTag() throws MAPException {
  75. return Tag.STRING_OCTET;
  76. }
  77. @Override
  78. public int getTagClass() {
  79. return Tag.CLASS_UNIVERSAL;
  80. }
  81. @Override
  82. public boolean getIsPrimitive() {
  83. return true;
  84. }
  85. @Override
  86. public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
  87. try {
  88. int length = ansIS.readLength();
  89. this._decode(ansIS, length);
  90. } catch (IOException e) {
  91. throw new MAPParsingComponentException("IOException when decoding AddressString: " + e.getMessage(), e,
  92. MAPParsingComponentExceptionReason.MistypedParameter);
  93. }
  94. }
  95. @Override
  96. public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
  97. try {
  98. this._decode(ansIS, length);
  99. } catch (IOException e) {
  100. throw new MAPParsingComponentException("IOException when decoding AddressString: " + e.getMessage(), e,
  101. MAPParsingComponentExceptionReason.MistypedParameter);
  102. }
  103. }
  104. protected void _testLengthDecode(int length) throws MAPParsingComponentException {
  105. if (length > 20)
  106. throw new MAPParsingComponentException("Error when decoding AddressString: mesage length must not exceed 20",
  107. MAPParsingComponentExceptionReason.MistypedParameter);
  108. }
  109. private void _decode(AsnInputStream ansIS, int length) throws MAPParsingComponentException, IOException {
  110. this._testLengthDecode(length);
  111. // The first byte has extension, nature of address indicator and
  112. // numbering plan indicator
  113. int nature = ansIS.read();
  114. if ((nature & NO_EXTENSION_MASK) == 0x80) {
  115. this.isExtension = false;
  116. } else {
  117. this.isExtension = true;
  118. }
  119. int natureOfAddInd = ((nature & NATURE_OF_ADD_IND_MASK) >> 4);
  120. this.addressNature = AddressNature.getInstance(natureOfAddInd);
  121. int numbPlanInd = (nature & NUMBERING_PLAN_IND_MASK);
  122. this.numberingPlan = NumberingPlan.getInstance(numbPlanInd);
  123. this.address = this.decodeString(ansIS, length - 1);
  124. }
  125. @Override
  126. public void encodeAll(AsnOutputStream asnOs) throws MAPException {
  127. this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.STRING_OCTET);
  128. }
  129. @Override
  130. public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
  131. try {
  132. asnOs.writeTag(tagClass, true, tag);
  133. int pos = asnOs.StartContentDefiniteLength();
  134. this.encodeData(asnOs);
  135. asnOs.FinalizeContent(pos);
  136. } catch (AsnException e) {
  137. throw new MAPException("AsnException when encoding AddressString: " + e.getMessage(), e);
  138. }
  139. }
  140. protected void _testLengthEncode() throws MAPException {
  141. if (this.address.length() > 38)
  142. throw new MAPException("Error when encoding AddressString: address length must not exceed 38 digits");
  143. }
  144. @Override
  145. public void encodeData(AsnOutputStream asnOs) throws MAPException {
  146. if (this.addressNature == null || this.numberingPlan == null || this.address == null)
  147. throw new MAPException("Error when encoding AddressString: addressNature, numberingPlan or address is empty");
  148. this._testLengthEncode();
  149. int nature = 1;
  150. if (this.isExtension) {
  151. nature = 0;
  152. }
  153. nature = nature << 7;
  154. nature = nature | (this.addressNature.getIndicator() << 4);
  155. nature = nature | (this.numberingPlan.getIndicator());
  156. asnOs.write(nature);
  157. this.encodeString(asnOs, this.address);
  158. }
  159. @Override
  160. public String toString() {
  161. return "AddressString[AddressNature=" + this.addressNature.toString() + ", NumberingPlan=" + this.numberingPlan.toString() + ", Address="
  162. + this.address + "]";
  163. }
  164. @Override
  165. public int hashCode() {
  166. final int prime = 31;
  167. int result = 1;
  168. result = prime * result + ((address == null) ? 0 : address.hashCode());
  169. result = prime * result + ((addressNature == null) ? 0 : addressNature.hashCode());
  170. result = prime * result + ((numberingPlan == null) ? 0 : numberingPlan.hashCode());
  171. return result;
  172. }
  173. @Override
  174. public boolean equals(Object obj) {
  175. if (this == obj)
  176. return true;
  177. if (obj == null)
  178. return false;
  179. if (getClass() != obj.getClass())
  180. return false;
  181. AddressStringImpl other = (AddressStringImpl) obj;
  182. if (address == null) {
  183. if (other.address != null)
  184. return false;
  185. } else if (!address.equals(other.address))
  186. return false;
  187. if (addressNature != other.addressNature)
  188. return false;
  189. if (numberingPlan != other.numberingPlan)
  190. return false;
  191. return true;
  192. }
  193. /**
  194. * XML Serialization/Deserialization
  195. */
  196. protected static final XMLFormat<AddressStringImpl> ADDRESS_STRING_XML = new XMLFormat<AddressStringImpl>(AddressStringImpl.class) {
  197. @Override
  198. public void read(javolution.xml.XMLFormat.InputElement xml, AddressStringImpl addressStringImpl) throws XMLStreamException {
  199. addressStringImpl.addressNature = AddressNature.getInstance(xml.getAttribute(NAI, 0));
  200. addressStringImpl.numberingPlan = NumberingPlan.getInstance(xml.getAttribute(NPI, 0));
  201. addressStringImpl.address = xml.getAttribute(NUMBER, "");
  202. }
  203. @Override
  204. public void write(AddressStringImpl addressStringImpl, javolution.xml.XMLFormat.OutputElement xml) throws XMLStreamException {
  205. xml.setAttribute(NAI, addressStringImpl.addressNature.getIndicator());
  206. xml.setAttribute(NPI, addressStringImpl.numberingPlan.getIndicator());
  207. xml.setAttribute(NUMBER, addressStringImpl.address);
  208. }
  209. };
  210. }