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

http://mobicents.googlecode.com/ · Java · 311 lines · 179 code · 43 blank · 89 comment · 30 complexity · 2d60c89151790a2909025828bce40aed 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. /*
  23. JBoss, Home of Professional Open Source
  24. * Copyright 2011, Red Hat, Inc. and individual contributors
  25. * by the @authors tag. See the copyright.txt in the distribution for a
  26. * full listing of individual contributors.
  27. *
  28. * This is free software; you can redistribute it and/or modify it
  29. * under the terms of the GNU Lesser General Public License as
  30. * published by the Free Software Foundation; either version 2.1 of
  31. * the License, or (at your option) any later version.
  32. *
  33. * This software is distributed in the hope that it will be useful,
  34. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  36. * Lesser General Public License for more details.
  37. *
  38. * You should have received a copy of the GNU Lesser General Public
  39. * License along with this software; if not, write to the Free
  40. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  41. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  42. */
  43. package org.mobicents.protocols.ss7.map.primitives;
  44. import java.io.IOException;
  45. import java.util.Arrays;
  46. import org.mobicents.protocols.asn.AsnException;
  47. import org.mobicents.protocols.asn.AsnInputStream;
  48. import org.mobicents.protocols.asn.AsnOutputStream;
  49. import org.mobicents.protocols.asn.Tag;
  50. import org.mobicents.protocols.ss7.map.api.MAPException;
  51. import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
  52. import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
  53. import org.mobicents.protocols.ss7.map.api.primitives.MAPPrivateExtension;
  54. /**
  55. * @author sergey vetyutnev
  56. *
  57. */
  58. public class MAPPrivateExtensionImpl implements MAPPrivateExtension, MAPAsnPrimitive {
  59. private long[] oId;
  60. private byte[] data;
  61. public MAPPrivateExtensionImpl() {
  62. }
  63. public MAPPrivateExtensionImpl(long[] oId, byte[] data) {
  64. this.oId = oId;
  65. this.data = data;
  66. }
  67. /*
  68. * (non-Javadoc)
  69. *
  70. * @see
  71. * org.mobicents.protocols.ss7.map.api.dialog.MAPPrivateExtension#getOId()
  72. */
  73. @Override
  74. public long[] getOId() {
  75. return this.oId;
  76. }
  77. /*
  78. * (non-Javadoc)
  79. *
  80. * @see
  81. * org.mobicents.protocols.ss7.map.api.dialog.MAPPrivateExtension#setOId
  82. * (long[])
  83. */
  84. @Override
  85. public void setOId(long[] oId) {
  86. this.oId = oId;
  87. }
  88. /*
  89. * (non-Javadoc)
  90. *
  91. * @see
  92. * org.mobicents.protocols.ss7.map.api.dialog.MAPPrivateExtension#getData()
  93. */
  94. @Override
  95. public byte[] getData() {
  96. return this.data;
  97. }
  98. /*
  99. * (non-Javadoc)
  100. *
  101. * @see
  102. * org.mobicents.protocols.ss7.map.api.dialog.MAPPrivateExtension#setData
  103. * (byte[])
  104. */
  105. @Override
  106. public void setData(byte[] data) {
  107. this.data = data;
  108. }
  109. @Override
  110. public int getTag() throws MAPException {
  111. return Tag.SEQUENCE;
  112. }
  113. @Override
  114. public int getTagClass() {
  115. return Tag.CLASS_UNIVERSAL;
  116. }
  117. @Override
  118. public boolean getIsPrimitive() {
  119. return false;
  120. }
  121. @Override
  122. public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
  123. // Definition from GSM 09.02 version 5.15.1 Page 690
  124. // extensionContainer SEQUENCE {
  125. // privateExtensionList [0] IMPLICIT SEQUENCE ( SIZE( 1 .. 10 ) ) OF
  126. // SEQUENCE {
  127. // extId MAP-EXTENSION .&extensionId ( {
  128. // ,
  129. // ...} ) ,
  130. // extType MAP-EXTENSION .&ExtensionType ( {
  131. // ,
  132. // ...} { @extId } ) OPTIONAL} OPTIONAL,
  133. // pcs-Extensions [1] IMPLICIT SEQUENCE {
  134. // ... } OPTIONAL,
  135. // ... } OPTIONAL,
  136. // ... }
  137. try {
  138. AsnInputStream ais = ansIS.readSequenceStream();
  139. this._decode(ais);
  140. } catch (IOException e) {
  141. throw new MAPParsingComponentException("IOException when decoding PrivateExtension: " + e.getMessage(), e,
  142. MAPParsingComponentExceptionReason.MistypedParameter);
  143. } catch (AsnException e) {
  144. throw new MAPParsingComponentException("AsnException when decoding PrivateExtension: " + e.getMessage(), e,
  145. MAPParsingComponentExceptionReason.MistypedParameter);
  146. }
  147. }
  148. @Override
  149. public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
  150. try {
  151. AsnInputStream ais = ansIS.readSequenceStreamData(length);
  152. this._decode(ais);
  153. } catch (IOException e) {
  154. throw new MAPParsingComponentException("IOException when decoding PrivateExtension: " + e.getMessage(), e,
  155. MAPParsingComponentExceptionReason.MistypedParameter);
  156. } catch (AsnException e) {
  157. throw new MAPParsingComponentException("AsnException when decoding PrivateExtension: " + e.getMessage(), e,
  158. MAPParsingComponentExceptionReason.MistypedParameter);
  159. }
  160. }
  161. private void _decode(AsnInputStream ansIS) throws MAPParsingComponentException, IOException, AsnException {
  162. // extId
  163. int tag = ansIS.readTag();
  164. if (tag != Tag.OBJECT_IDENTIFIER || ansIS.getTagClass() != Tag.CLASS_UNIVERSAL || !ansIS.isTagPrimitive())
  165. throw new MAPParsingComponentException("Error decoding PrivateExtension: bad tag, tagClass or primitiveFactor of ExtentionId",
  166. MAPParsingComponentExceptionReason.MistypedParameter);
  167. this.oId = ansIS.readObjectIdentifier();
  168. // extType
  169. if (ansIS.available() > 0) {
  170. this.data = new byte[ansIS.available()];
  171. ansIS.read(this.data);
  172. }
  173. else
  174. this.data = null;
  175. }
  176. @Override
  177. public void encodeAll(AsnOutputStream asnOs) throws MAPException {
  178. this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.SEQUENCE);
  179. }
  180. @Override
  181. public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
  182. try {
  183. asnOs.writeTag(tagClass, false, tag);
  184. int pos = asnOs.StartContentDefiniteLength();
  185. this.encodeData(asnOs);
  186. asnOs.FinalizeContent(pos);
  187. } catch (AsnException e) {
  188. throw new MAPException("AsnException when encoding PrivateExtension: " + e.getMessage(), e);
  189. }
  190. }
  191. @Override
  192. public void encodeData(AsnOutputStream asnOs) throws MAPException {
  193. if (this.oId == null || this.oId.length < 2)
  194. throw new MAPException("Error when encoding PrivateExtension: OId value must not be empty when coding PrivateExtension");
  195. try {
  196. asnOs.writeObjectIdentifier(this.oId);
  197. if (this.data != null)
  198. asnOs.write(this.data);
  199. } catch (IOException e) {
  200. throw new MAPException("IOException when encoding PrivateExtension: " + e.getMessage(), e);
  201. } catch (AsnException e) {
  202. throw new MAPException("AsnException when encoding PrivateExtension: " + e.getMessage(), e);
  203. }
  204. }
  205. @Override
  206. public String toString() {
  207. StringBuilder sb = new StringBuilder();
  208. sb.append("PrivateExtension [");
  209. if (this.oId != null || this.oId.length > 0) {
  210. sb.append("Oid=");
  211. sb.append(this.ArrayToString(this.oId));
  212. }
  213. if (this.data != null ) {
  214. sb.append(", data=");
  215. sb.append(this.ArrayToString(this.data));
  216. }
  217. sb.append("]");
  218. return sb.toString();
  219. }
  220. private String ArrayToString(byte[] array) {
  221. StringBuilder sb = new StringBuilder();
  222. int i1 = 0;
  223. for (byte b : array) {
  224. if (i1 == 0)
  225. i1 = 1;
  226. else
  227. sb.append(", ");
  228. sb.append(b);
  229. }
  230. return sb.toString();
  231. }
  232. private String ArrayToString(long[] array) {
  233. StringBuilder sb = new StringBuilder();
  234. int i1 = 0;
  235. for (long b : array) {
  236. if (i1 == 0)
  237. i1 = 1;
  238. else
  239. sb.append(", ");
  240. sb.append(b);
  241. }
  242. return sb.toString();
  243. }
  244. @Override
  245. public int hashCode() {
  246. final int prime = 31;
  247. int result = 1;
  248. result = prime * result + Arrays.hashCode(data);
  249. result = prime * result + Arrays.hashCode(oId);
  250. return result;
  251. }
  252. @Override
  253. public boolean equals(Object obj) {
  254. if (this == obj)
  255. return true;
  256. if (obj == null)
  257. return false;
  258. if (getClass() != obj.getClass())
  259. return false;
  260. MAPPrivateExtensionImpl other = (MAPPrivateExtensionImpl) obj;
  261. if (!Arrays.equals(data, other.data))
  262. return false;
  263. if (!Arrays.equals(oId, other.oId))
  264. return false;
  265. return true;
  266. }
  267. // ...............................
  268. }