PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 306 lines | 215 code | 52 blank | 39 comment | 62 complexity | 885c483eb344ad0a94b5bb87c323984e 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 java.util.ArrayList;
  25. import java.util.Arrays;
  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.MAPExtensionContainer;
  34. import org.mobicents.protocols.ss7.map.api.primitives.MAPPrivateExtension;
  35. /**
  36. * @author sergey vetyutnev
  37. *
  38. */
  39. public class MAPExtensionContainerImpl implements MAPExtensionContainer, MAPAsnPrimitive {
  40. protected static final int PRIVATEEXTENSIONLIST_REF_TAG = 0x00;
  41. protected static final int PSCEXTENSIONS_REF_TAG = 0x01;
  42. private ArrayList<MAPPrivateExtension> privateExtensionList;
  43. private byte[] pcsExtensions;
  44. public MAPExtensionContainerImpl() {
  45. }
  46. public MAPExtensionContainerImpl(ArrayList<MAPPrivateExtension> privateExtensionList, byte[] pcsExtensions) {
  47. this.privateExtensionList = privateExtensionList;
  48. this.pcsExtensions = pcsExtensions;
  49. }
  50. @Override
  51. public ArrayList<MAPPrivateExtension> getPrivateExtensionList() {
  52. return this.privateExtensionList;
  53. }
  54. @Override
  55. public void setPrivateExtensionList(ArrayList<MAPPrivateExtension> privateExtensionList) {
  56. this.privateExtensionList = privateExtensionList;
  57. }
  58. @Override
  59. public byte[] getPcsExtensions() {
  60. return this.pcsExtensions;
  61. }
  62. @Override
  63. public void setPcsExtensions(byte[] pcsExtensions) {
  64. this.pcsExtensions = pcsExtensions;
  65. }
  66. @Override
  67. public int getTag() throws MAPException {
  68. return Tag.SEQUENCE;
  69. }
  70. @Override
  71. public int getTagClass() {
  72. return Tag.CLASS_UNIVERSAL;
  73. }
  74. @Override
  75. public boolean getIsPrimitive() {
  76. return false;
  77. }
  78. @Override
  79. public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
  80. // Definition from GSM 09.02 version 5.15.1 Page 690
  81. // extensionContainer SEQUENCE {
  82. // privateExtensionList [0] IMPLICIT SEQUENCE ( SIZE( 1 .. 10 ) ) OF
  83. // SEQUENCE {
  84. // extId MAP-EXTENSION .&extensionId ( {
  85. // ,
  86. // ...} ) ,
  87. // extType MAP-EXTENSION .&ExtensionType ( {
  88. // ,
  89. // ...} { @extId } ) OPTIONAL} OPTIONAL,
  90. // pcs-Extensions [1] IMPLICIT SEQUENCE {
  91. // ... } OPTIONAL,
  92. // ... } OPTIONAL,
  93. // ... }
  94. try {
  95. AsnInputStream ais = ansIS.readSequenceStream();
  96. this._decode(ais);
  97. } catch (IOException e) {
  98. throw new MAPParsingComponentException("IOException when decoding ExtensionContainer: " + e.getMessage(), e,
  99. MAPParsingComponentExceptionReason.MistypedParameter);
  100. } catch (AsnException e) {
  101. throw new MAPParsingComponentException("AsnException when decoding ExtensionContainer: " + e.getMessage(), e,
  102. MAPParsingComponentExceptionReason.MistypedParameter);
  103. }
  104. }
  105. @Override
  106. public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
  107. try {
  108. AsnInputStream ais = ansIS.readSequenceStreamData(length);
  109. this._decode(ais);
  110. } catch (IOException e) {
  111. throw new MAPParsingComponentException("IOException when decoding ExtensionContainer: " + e.getMessage(), e,
  112. MAPParsingComponentExceptionReason.MistypedParameter);
  113. } catch (AsnException e) {
  114. throw new MAPParsingComponentException("AsnException when decoding ExtensionContainer: " + e.getMessage(), e,
  115. MAPParsingComponentExceptionReason.MistypedParameter);
  116. }
  117. }
  118. private void _decode(AsnInputStream ansIS) throws MAPParsingComponentException, IOException, AsnException {
  119. this.privateExtensionList = null;
  120. this.pcsExtensions = null;
  121. while (true) {
  122. if (ansIS.available() == 0)
  123. break;
  124. int tag = ansIS.readTag();
  125. if (tag == MAPExtensionContainerImpl.PRIVATEEXTENSIONLIST_REF_TAG && ansIS.getTagClass() == Tag.CLASS_CONTEXT_SPECIFIC) {
  126. if(ansIS.isTagPrimitive())
  127. throw new MAPParsingComponentException("Error while ExtensionContainer decoding: privateExtensionList is primitive",
  128. MAPParsingComponentExceptionReason.MistypedParameter);
  129. if (this.privateExtensionList != null)
  130. throw new MAPParsingComponentException("Error while ExtensionContainer decoding: More than one PrivateExtensionList has found",
  131. MAPParsingComponentExceptionReason.MistypedParameter);
  132. AsnInputStream localAis2 = ansIS.readSequenceStream();
  133. this.privateExtensionList = new ArrayList<MAPPrivateExtension>();
  134. while (localAis2.available() > 0) {
  135. tag = localAis2.readTag();
  136. if (tag != Tag.SEQUENCE || localAis2.getTagClass() != Tag.CLASS_UNIVERSAL || localAis2.isTagPrimitive())
  137. throw new MAPParsingComponentException("Error while ExtensionContainer decoding: Bad tag, tagClass or primitiveFactor of PrivateExtension",
  138. MAPParsingComponentExceptionReason.MistypedParameter);
  139. if (this.privateExtensionList.size() >= 10)
  140. throw new MAPParsingComponentException("More then 10 PrivateExtension found when PrivateExtensionList decoding",
  141. MAPParsingComponentExceptionReason.MistypedParameter);
  142. MAPPrivateExtensionImpl privateExtension = new MAPPrivateExtensionImpl();
  143. privateExtension.decodeAll(localAis2);
  144. this.privateExtensionList.add(privateExtension);
  145. }
  146. } else if (tag == MAPExtensionContainerImpl.PSCEXTENSIONS_REF_TAG && ansIS.getTagClass() == Tag.CLASS_CONTEXT_SPECIFIC) {
  147. if(ansIS.isTagPrimitive())
  148. throw new MAPParsingComponentException("Error while PCS-Extensions decoding: PCS-Extensions is primitive",
  149. MAPParsingComponentExceptionReason.MistypedParameter);
  150. if (this.pcsExtensions != null)
  151. throw new MAPParsingComponentException("Error while PCS-Extensions decoding: More than one PCS-Extensions has found",
  152. MAPParsingComponentExceptionReason.MistypedParameter);
  153. this.pcsExtensions = ansIS.readSequence();
  154. }
  155. }
  156. }
  157. @Override
  158. public void encodeAll(AsnOutputStream asnOs) throws MAPException {
  159. this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.SEQUENCE);
  160. }
  161. @Override
  162. public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
  163. try {
  164. asnOs.writeTag(tagClass, false, tag);
  165. int pos = asnOs.StartContentDefiniteLength();
  166. this.encodeData(asnOs);
  167. asnOs.FinalizeContent(pos);
  168. } catch (AsnException e) {
  169. throw new MAPException("AsnException when encoding ExtensionContainer: " + e.getMessage(), e);
  170. }
  171. }
  172. @Override
  173. public void encodeData(AsnOutputStream asnOs) throws MAPException {
  174. if (this.privateExtensionList == null && this.pcsExtensions == null)
  175. throw new MAPException(
  176. "Error when encoding ExtensionContainer: Both PrivateExtensionList and PcsExtensions are empty when ExtensionContainer encoding");
  177. if (this.privateExtensionList != null && (this.privateExtensionList.size() == 0 || this.privateExtensionList.size() > 10))
  178. throw new MAPException(
  179. "Error when encoding ExtensionContainer: PrivateExtensionList must contains from 1 to 10 elements when ExtensionContainer encoding");
  180. try {
  181. if (this.privateExtensionList != null) {
  182. asnOs.writeTag(Tag.CLASS_CONTEXT_SPECIFIC, false, PRIVATEEXTENSIONLIST_REF_TAG);
  183. int pos2 = asnOs.StartContentDefiniteLength();
  184. for (MAPPrivateExtension pe : this.privateExtensionList) {
  185. ((MAPPrivateExtensionImpl)pe).encodeAll(asnOs);
  186. }
  187. asnOs.FinalizeContent(pos2);
  188. }
  189. if (this.pcsExtensions != null) {
  190. asnOs.writeTag(Tag.CLASS_CONTEXT_SPECIFIC, false, PSCEXTENSIONS_REF_TAG);
  191. int pos2 = asnOs.StartContentDefiniteLength();
  192. asnOs.write(this.pcsExtensions);
  193. asnOs.FinalizeContent(pos2);
  194. }
  195. } catch (AsnException e) {
  196. throw new MAPException("AsnException when encoding ExtensionContainer: " + e.getMessage(), e);
  197. }
  198. }
  199. @Override
  200. public String toString() {
  201. StringBuilder sb = new StringBuilder();
  202. sb.append("ExtensionContainer [");
  203. if (this.privateExtensionList != null && this.privateExtensionList.size() > 0) {
  204. for(MAPPrivateExtension pe : this.privateExtensionList) {
  205. sb.append("\n");
  206. sb.append(pe.toString());
  207. }
  208. }
  209. if (this.pcsExtensions != null) {
  210. sb.append("\nPcsExtensions=");
  211. sb.append(this.ArrayToString(this.pcsExtensions));
  212. }
  213. sb.append("]");
  214. return sb.toString();
  215. }
  216. @Override
  217. public int hashCode() {
  218. final int prime = 31;
  219. int result = 1;
  220. result = prime * result + Arrays.hashCode(pcsExtensions);
  221. result = prime * result + ((privateExtensionList == null) ? 0 : privateExtensionList.hashCode());
  222. return result;
  223. }
  224. @Override
  225. public boolean equals(Object obj) {
  226. if (this == obj)
  227. return true;
  228. if (obj == null)
  229. return false;
  230. if (getClass() != obj.getClass())
  231. return false;
  232. MAPExtensionContainerImpl other = (MAPExtensionContainerImpl) obj;
  233. if (!Arrays.equals(pcsExtensions, other.pcsExtensions))
  234. return false;
  235. if (privateExtensionList == null) {
  236. if (other.privateExtensionList != null)
  237. return false;
  238. } else if (!privateExtensionList.equals(other.privateExtensionList))
  239. return false;
  240. return true;
  241. }
  242. private String ArrayToString(byte[] array) {
  243. StringBuilder sb = new StringBuilder();
  244. int i1 = 0;
  245. for (byte b : array) {
  246. if (i1 == 0)
  247. i1 = 1;
  248. else
  249. sb.append(", ");
  250. sb.append(b);
  251. }
  252. return sb.toString();
  253. }
  254. }