PageRenderTime 34ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

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