/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/errors/MAPErrorMessageExtensionContainerImpl.java

http://mobicents.googlecode.com/ · Java · 203 lines · 134 code · 41 blank · 28 comment · 14 complexity · 4aa4d35871f5669344d02288d8b3fe07 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.errors;
  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.errors.MAPErrorMessageExtensionContainer;
  32. import org.mobicents.protocols.ss7.map.api.primitives.MAPExtensionContainer;
  33. import org.mobicents.protocols.ss7.map.primitives.MAPExtensionContainerImpl;
  34. /**
  35. * The MAP ReturnError message with the single parameter - ExtensionContainer
  36. *
  37. *
  38. * @author sergey vetyutnev
  39. *
  40. */
  41. public class MAPErrorMessageExtensionContainerImpl extends MAPErrorMessageImpl implements MAPErrorMessageExtensionContainer {
  42. private MAPExtensionContainer extensionContainer;
  43. protected MAPErrorMessageExtensionContainerImpl(Long errorCode) {
  44. super(errorCode);
  45. }
  46. public MAPErrorMessageExtensionContainerImpl(Long errorCode, MAPExtensionContainer extensionContainer) {
  47. super(errorCode);
  48. this.extensionContainer = extensionContainer;
  49. }
  50. @Override
  51. public MAPExtensionContainer getExtensionContainer() {
  52. return this.extensionContainer;
  53. }
  54. @Override
  55. public void setExtensionContainer(MAPExtensionContainer extensionContainer) {
  56. this.extensionContainer = extensionContainer;
  57. }
  58. @Override
  59. public boolean isEmExtensionContainer() {
  60. return true;
  61. }
  62. @Override
  63. public MAPErrorMessageExtensionContainer getEmExtensionContainer() {
  64. return this;
  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. try {
  81. int length = ansIS.readLength();
  82. this._decode(ansIS, length);
  83. } catch (IOException e) {
  84. throw new MAPParsingComponentException("IOException when decoding MAPErrorMessageExtensionContainer: " + e.getMessage(), e,
  85. MAPParsingComponentExceptionReason.MistypedParameter);
  86. } catch (AsnException e) {
  87. throw new MAPParsingComponentException("AsnException when decoding MAPErrorMessageExtensionContainer: " + e.getMessage(), e,
  88. MAPParsingComponentExceptionReason.MistypedParameter);
  89. }
  90. }
  91. @Override
  92. public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
  93. try {
  94. this._decode(ansIS, length);
  95. } catch (IOException e) {
  96. throw new MAPParsingComponentException("IOException when decoding MAPErrorMessageExtensionContainer: " + e.getMessage(), e,
  97. MAPParsingComponentExceptionReason.MistypedParameter);
  98. } catch (AsnException e) {
  99. throw new MAPParsingComponentException("AsnException when decoding MAPErrorMessageExtensionContainer: " + e.getMessage(), e,
  100. MAPParsingComponentExceptionReason.MistypedParameter);
  101. }
  102. }
  103. private void _decode(AsnInputStream ansIS, int length) throws MAPParsingComponentException, IOException, AsnException {
  104. this.extensionContainer = null;
  105. if (ansIS.getTagClass() != Tag.CLASS_UNIVERSAL || ansIS.getTag() != Tag.SEQUENCE || ansIS.isTagPrimitive())
  106. throw new MAPParsingComponentException("Error decoding MAPErrorMessageExtensionContainer: bad tag class or tag or parameter is primitive",
  107. MAPParsingComponentExceptionReason.MistypedParameter);
  108. AsnInputStream ais = ansIS.readSequenceStreamData(length);
  109. while (true) {
  110. if (ais.available() == 0)
  111. break;
  112. int tag = ais.readTag();
  113. switch (ais.getTagClass()) {
  114. case Tag.CLASS_UNIVERSAL:
  115. switch (tag) {
  116. case Tag.SEQUENCE:
  117. this.extensionContainer = new MAPExtensionContainerImpl();
  118. ((MAPExtensionContainerImpl)this.extensionContainer).decodeAll(ais);
  119. break;
  120. default:
  121. ais.advanceElement();
  122. break;
  123. }
  124. break;
  125. default:
  126. ais.advanceElement();
  127. break;
  128. }
  129. }
  130. }
  131. @Override
  132. public void encodeAll(AsnOutputStream asnOs) throws MAPException {
  133. this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.SEQUENCE);
  134. }
  135. @Override
  136. public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
  137. try {
  138. asnOs.writeTag(tagClass, false, tag);
  139. int pos = asnOs.StartContentDefiniteLength();
  140. this.encodeData(asnOs);
  141. asnOs.FinalizeContent(pos);
  142. } catch (AsnException e) {
  143. throw new MAPException("AsnException when encoding MAPErrorMessageExtensionContainer: " + e.getMessage(), e);
  144. }
  145. }
  146. @Override
  147. public void encodeData(AsnOutputStream aos) throws MAPException {
  148. if (this.extensionContainer == null)
  149. return;
  150. ((MAPExtensionContainerImpl)this.extensionContainer).encodeAll(aos);
  151. }
  152. @Override
  153. public String toString() {
  154. StringBuilder sb = new StringBuilder();
  155. sb.append("MAPErrorMessageExtensionContainer [");
  156. sb.append("ErrorCode=" + this.errorCode);
  157. if (this.extensionContainer != null)
  158. sb.append(", extensionContainer=" + this.extensionContainer.toString());
  159. sb.append("]");
  160. return sb.toString();
  161. }
  162. }