PageRenderTime 42ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 328 lines | 177 code | 37 blank | 114 comment | 39 complexity | 231a3b26ba3436fdf065edcc7432459f 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.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.USSDString;
  32. import org.mobicents.protocols.ss7.map.api.service.lsm.LCSClientName;
  33. import org.mobicents.protocols.ss7.map.api.service.lsm.LCSFormatIndicator;
  34. import org.mobicents.protocols.ss7.map.primitives.MAPAsnPrimitive;
  35. import org.mobicents.protocols.ss7.map.primitives.USSDStringImpl;
  36. /**
  37. * @author amit bhayani
  38. *
  39. */
  40. public class LCSClientNameImpl implements LCSClientName, MAPAsnPrimitive {
  41. private static final int _TAG_DATA_CODING_SCHEME = 0;
  42. private static final int _TAG_NAME_STRING = 2;
  43. private static final int _TAG_LCS_FORMAT_INDICATOR = 3;
  44. private byte dataCodingScheme = 0;
  45. private USSDString nameString;
  46. private LCSFormatIndicator lcsFormatIndicator;
  47. /**
  48. *
  49. */
  50. public LCSClientNameImpl() {
  51. super();
  52. }
  53. /**
  54. * @param dataCodingScheme
  55. * @param nameString
  56. * @param lcsFormatIndicator
  57. */
  58. public LCSClientNameImpl(byte dataCodingScheme, USSDString nameString, LCSFormatIndicator lcsFormatIndicator) {
  59. super();
  60. this.dataCodingScheme = dataCodingScheme;
  61. this.nameString = nameString;
  62. this.lcsFormatIndicator = lcsFormatIndicator;
  63. }
  64. /*
  65. * (non-Javadoc)
  66. *
  67. * @see org.mobicents.protocols.ss7.map.api.service.lsm.LCSClientName#
  68. * getDataCodingScheme()
  69. */
  70. @Override
  71. public byte getDataCodingScheme() {
  72. return this.dataCodingScheme;
  73. }
  74. /*
  75. * (non-Javadoc)
  76. *
  77. * @see
  78. * org.mobicents.protocols.ss7.map.api.service.lsm.LCSClientName#getNameString
  79. * ()
  80. */
  81. @Override
  82. public USSDString getNameString() {
  83. return this.nameString;
  84. }
  85. /*
  86. * (non-Javadoc)
  87. *
  88. * @see org.mobicents.protocols.ss7.map.api.service.lsm.LCSClientName#
  89. * getLCSFormatIndicator()
  90. */
  91. @Override
  92. public LCSFormatIndicator getLCSFormatIndicator() {
  93. return this.lcsFormatIndicator;
  94. }
  95. /*
  96. * (non-Javadoc)
  97. *
  98. * @see
  99. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTag()
  100. */
  101. @Override
  102. public int getTag() throws MAPException {
  103. return Tag.SEQUENCE;
  104. }
  105. /*
  106. * (non-Javadoc)
  107. *
  108. * @see
  109. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTagClass
  110. * ()
  111. */
  112. @Override
  113. public int getTagClass() {
  114. return Tag.CLASS_UNIVERSAL;
  115. }
  116. /*
  117. * (non-Javadoc)
  118. *
  119. * @see
  120. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getIsPrimitive
  121. * ()
  122. */
  123. @Override
  124. public boolean getIsPrimitive() {
  125. return false;
  126. }
  127. /*
  128. * (non-Javadoc)
  129. *
  130. * @see
  131. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeAll
  132. * (org.mobicents.protocols.asn.AsnInputStream)
  133. */
  134. @Override
  135. public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
  136. try {
  137. int length = ansIS.readLength();
  138. this._decode(ansIS, length);
  139. } catch (IOException e) {
  140. throw new MAPParsingComponentException("IOException when decoding LCSClientName: " + e.getMessage(), e,
  141. MAPParsingComponentExceptionReason.MistypedParameter);
  142. } catch (AsnException e) {
  143. throw new MAPParsingComponentException("AsnException when decoding LCSClientName: " + e.getMessage(), e,
  144. MAPParsingComponentExceptionReason.MistypedParameter);
  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 LCSClientName: " + e.getMessage(), e,
  160. MAPParsingComponentExceptionReason.MistypedParameter);
  161. } catch (AsnException e) {
  162. throw new MAPParsingComponentException("AsnException when decoding LCSClientName: " + e.getMessage(), e,
  163. MAPParsingComponentExceptionReason.MistypedParameter);
  164. }
  165. }
  166. private void _decode(AsnInputStream asnIS, int length) throws MAPParsingComponentException, IOException, AsnException {
  167. AsnInputStream ais = asnIS.readSequenceStreamData(length);
  168. int tag = ais.readTag();
  169. // Decode mandatory dataCodingScheme [0] USSD-DataCodingScheme,
  170. if (ais.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC || !ais.isTagPrimitive() || tag != _TAG_DATA_CODING_SCHEME) {
  171. throw new MAPParsingComponentException(
  172. "Error while decoding LCSClientName: Parameter 0[dataCodingScheme [0] USSD-DataCodingScheme] bad tag class, tag or not primitive",
  173. MAPParsingComponentExceptionReason.MistypedParameter);
  174. }
  175. int length1 = ais.readLength();
  176. this.dataCodingScheme = ais.readOctetStringData(length1)[0];
  177. tag = ais.readTag();
  178. // Decode mandatory nameString [2] NameString,
  179. if (ais.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC || !ais.isTagPrimitive() || tag != _TAG_NAME_STRING) {
  180. throw new MAPParsingComponentException(
  181. "Error while decoding LCSClientName: Parameter 1[nameString [2] NameString,] bad tag class, tag or not primitive",
  182. MAPParsingComponentExceptionReason.MistypedParameter);
  183. }
  184. this.nameString = new USSDStringImpl();
  185. ((USSDStringImpl)this.nameString).decodeAll(ais);
  186. while (true) {
  187. if (ais.available() == 0)
  188. break;
  189. tag = ais.readTag();
  190. switch (tag) {
  191. case _TAG_LCS_FORMAT_INDICATOR:
  192. // Decode lcs-FormatIndicator [3] LCS-FormatIndicator OPTIONAL
  193. if (ais.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC || !ais.isTagPrimitive()) {
  194. throw new MAPParsingComponentException(
  195. "Error while decoding LCSClientName: Parameter 2[lcs-FormatIndicator [3] LCS-FormatIndicator OPTIONAL] bad tag class, tag or not primitive",
  196. MAPParsingComponentExceptionReason.MistypedParameter);
  197. }
  198. length1 = ais.readLength();
  199. this.lcsFormatIndicator = LCSFormatIndicator.getLCSFormatIndicator((int) ais.readIntegerData(length1));
  200. break;
  201. default:
  202. // throw new
  203. // MAPParsingComponentException("Decoding LCSClientExternalID failed. Expected externalAddress [0] or extensionContainer [1] but found "
  204. // + p.getTag(),
  205. // MAPParsingComponentExceptionReason.MistypedParameter);
  206. ais.advanceElement();
  207. }
  208. }
  209. }
  210. /*
  211. * (non-Javadoc)
  212. *
  213. * @see
  214. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll
  215. * (org.mobicents.protocols.asn.AsnOutputStream)
  216. */
  217. @Override
  218. public void encodeAll(AsnOutputStream asnOs) throws MAPException {
  219. this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.SEQUENCE);
  220. }
  221. /*
  222. * (non-Javadoc)
  223. *
  224. * @see
  225. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll
  226. * (org.mobicents.protocols.asn.AsnOutputStream, int, int)
  227. */
  228. @Override
  229. public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
  230. try {
  231. asnOs.writeTag(tagClass, false, tag);
  232. int pos = asnOs.StartContentDefiniteLength();
  233. this.encodeData(asnOs);
  234. asnOs.FinalizeContent(pos);
  235. } catch (AsnException e) {
  236. throw new MAPException("AsnException when encoding LCSClientName", e);
  237. }
  238. }
  239. /*
  240. * (non-Javadoc)
  241. *
  242. * @see
  243. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeData
  244. * (org.mobicents.protocols.asn.AsnOutputStream)
  245. */
  246. @Override
  247. public void encodeData(AsnOutputStream asnOs) throws MAPException {
  248. if (this.nameString == null)
  249. throw new MAPException("nameString must not be null");
  250. try {
  251. asnOs.writeOctetString(Tag.CLASS_CONTEXT_SPECIFIC, _TAG_DATA_CODING_SCHEME, new byte[] { this.dataCodingScheme });
  252. ((USSDStringImpl)this.nameString).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _TAG_NAME_STRING);
  253. if (this.lcsFormatIndicator != null) {
  254. asnOs.writeInteger(Tag.CLASS_CONTEXT_SPECIFIC, _TAG_LCS_FORMAT_INDICATOR, this.lcsFormatIndicator.getIndicator());
  255. }
  256. } catch (IOException e) {
  257. throw new MAPException("IOException when encoding LCSClientName", e);
  258. } catch (AsnException e) {
  259. throw new MAPException("AsnException when encoding LCSClientName", e);
  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 + ((lcsFormatIndicator == null) ? 0 : lcsFormatIndicator.hashCode());
  268. result = prime * result + ((nameString == null) ? 0 : nameString.hashCode());
  269. return result;
  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. LCSClientNameImpl other = (LCSClientNameImpl) obj;
  280. if (dataCodingScheme != other.dataCodingScheme)
  281. return false;
  282. if (lcsFormatIndicator != other.lcsFormatIndicator)
  283. return false;
  284. if (nameString == null) {
  285. if (other.nameString != null)
  286. return false;
  287. } else if (!nameString.equals(other.nameString))
  288. return false;
  289. return true;
  290. }
  291. }