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

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