PageRenderTime 370ms CodeModel.GetById 351ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/inap/inap-impl/src/main/java/org/mobicents/protocols/ss7/inap/isup/HighLayerCompatibilityInapImpl.java

http://mobicents.googlecode.com/
Java | 203 lines | 142 code | 34 blank | 27 comment | 13 complexity | 9f763427f4016a0e78e9dcedd1c972f9 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.inap.isup;
  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.inap.api.INAPException;
  29. import org.mobicents.protocols.ss7.inap.api.INAPParsingComponentException;
  30. import org.mobicents.protocols.ss7.inap.api.INAPParsingComponentExceptionReason;
  31. import org.mobicents.protocols.ss7.inap.api.isup.HighLayerCompatibilityInap;
  32. import org.mobicents.protocols.ss7.inap.primitives.INAPAsnPrimitive;
  33. import org.mobicents.protocols.ss7.isup.ParameterException;
  34. import org.mobicents.protocols.ss7.isup.impl.message.parameter.UserTeleserviceInformationImpl;
  35. import org.mobicents.protocols.ss7.isup.message.parameter.UserTeleserviceInformation;
  36. /**
  37. *
  38. *
  39. * @author sergey vetyutnev
  40. *
  41. */
  42. public class HighLayerCompatibilityInapImpl implements HighLayerCompatibilityInap, INAPAsnPrimitive {
  43. public static final String _PrimitiveName = "HighLayerCompatibilityInap";
  44. private byte[] data;
  45. public HighLayerCompatibilityInapImpl() {
  46. }
  47. public HighLayerCompatibilityInapImpl(byte[] data) {
  48. this.data = data;
  49. }
  50. public HighLayerCompatibilityInapImpl(UserTeleserviceInformation highLayerCompatibility) throws INAPException {
  51. if (highLayerCompatibility == null)
  52. throw new INAPException("The callingPartyCategory parameter must not be null");
  53. try {
  54. this.data = ((UserTeleserviceInformationImpl) highLayerCompatibility).encode();
  55. } catch (ParameterException e) {
  56. throw new INAPException("ParameterException when encoding highLayerCompatibility: " + e.getMessage(), e);
  57. }
  58. }
  59. @Override
  60. public byte[] getData() {
  61. return data;
  62. }
  63. @Override
  64. public UserTeleserviceInformation getHighLayerCompatibility() throws INAPException {
  65. if (this.data == null)
  66. throw new INAPException("The data has not been filled");
  67. try {
  68. UserTeleserviceInformationImpl cpc = new UserTeleserviceInformationImpl();
  69. cpc.decode(this.data);
  70. return cpc;
  71. } catch (ParameterException e) {
  72. throw new INAPException("ParameterException when decoding HighLayerCompatibility: " + e.getMessage(), e);
  73. }
  74. }
  75. @Override
  76. public int getTag() throws INAPException {
  77. return Tag.STRING_OCTET;
  78. }
  79. @Override
  80. public int getTagClass() {
  81. return Tag.CLASS_UNIVERSAL;
  82. }
  83. @Override
  84. public boolean getIsPrimitive() {
  85. return true;
  86. }
  87. @Override
  88. public void decodeAll(AsnInputStream ansIS) throws INAPParsingComponentException {
  89. try {
  90. int length = ansIS.readLength();
  91. this._decode(ansIS, length);
  92. } catch (IOException e) {
  93. throw new INAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
  94. INAPParsingComponentExceptionReason.MistypedParameter);
  95. } catch (AsnException e) {
  96. throw new INAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
  97. INAPParsingComponentExceptionReason.MistypedParameter);
  98. }
  99. }
  100. @Override
  101. public void decodeData(AsnInputStream ansIS, int length) throws INAPParsingComponentException {
  102. try {
  103. this._decode(ansIS, length);
  104. } catch (IOException e) {
  105. throw new INAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
  106. INAPParsingComponentExceptionReason.MistypedParameter);
  107. } catch (AsnException e) {
  108. throw new INAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
  109. INAPParsingComponentExceptionReason.MistypedParameter);
  110. }
  111. }
  112. private void _decode(AsnInputStream ansIS, int length) throws INAPParsingComponentException, IOException, AsnException {
  113. this.data = ansIS.readOctetStringData(length);
  114. if (this.data.length < 2 || this.data.length > 2)
  115. throw new INAPParsingComponentException(
  116. "Error while decoding " + _PrimitiveName + ": data must be from 2 to 2 bytes length, found: " + this.data.length,
  117. INAPParsingComponentExceptionReason.MistypedParameter);
  118. }
  119. @Override
  120. public void encodeAll(AsnOutputStream asnOs) throws INAPException {
  121. this.encodeAll(asnOs, this.getTagClass(), this.getTag());
  122. }
  123. @Override
  124. public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws INAPException {
  125. try {
  126. asnOs.writeTag(tagClass, true, tag);
  127. int pos = asnOs.StartContentDefiniteLength();
  128. this.encodeData(asnOs);
  129. asnOs.FinalizeContent(pos);
  130. } catch (AsnException e) {
  131. throw new INAPException("AsnException when encoding " + _PrimitiveName + ": " + e.getMessage(), e);
  132. }
  133. }
  134. @Override
  135. public void encodeData(AsnOutputStream asnOs) throws INAPException {
  136. if (this.data == null)
  137. throw new INAPException("data field must not be null");
  138. if (this.data.length < 2 && this.data.length > 2)
  139. throw new INAPException("data field length must be from 2 to 2");
  140. asnOs.writeOctetStringData(data);
  141. }
  142. @Override
  143. public String toString() {
  144. StringBuilder sb = new StringBuilder();
  145. sb.append(_PrimitiveName);
  146. sb.append(" [");
  147. if (this.data != null) {
  148. sb.append("data=[");
  149. sb.append(printDataArr(this.data));
  150. sb.append("]");
  151. try {
  152. UserTeleserviceInformation cpc = this.getHighLayerCompatibility();
  153. sb.append(", ");
  154. sb.append(cpc.toString());
  155. } catch (INAPException e) {
  156. }
  157. }
  158. sb.append("]");
  159. return sb.toString();
  160. }
  161. private String printDataArr(byte[] arr) {
  162. StringBuilder sb = new StringBuilder();
  163. for (int b : arr) {
  164. sb.append(b);
  165. sb.append(", ");
  166. }
  167. return sb.toString();
  168. }
  169. }