/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/service/subscriberManagement/ExtBearerServiceCodeImpl.java

http://mobicents.googlecode.com/ · Java · 167 lines · 109 code · 32 blank · 26 comment · 9 complexity · 0fa6d5574d40bfdf8c446250079be949 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.service.subscriberManagement;
  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.service.subscriberManagement.ExtBearerServiceCode;
  32. import org.mobicents.protocols.ss7.map.primitives.MAPAsnPrimitive;
  33. /**
  34. *
  35. * @author sergey vetyutnev
  36. *
  37. */
  38. public class ExtBearerServiceCodeImpl implements ExtBearerServiceCode, MAPAsnPrimitive {
  39. public static final String _PrimitiveName = "ExtBearerServiceCode";
  40. private byte[] data;
  41. public ExtBearerServiceCodeImpl() {
  42. }
  43. public ExtBearerServiceCodeImpl(byte[] data) {
  44. this.data = data;
  45. }
  46. @Override
  47. public byte[] getData() {
  48. return data;
  49. }
  50. @Override
  51. public int getTag() throws MAPException {
  52. return Tag.STRING_OCTET;
  53. }
  54. @Override
  55. public int getTagClass() {
  56. return Tag.CLASS_UNIVERSAL;
  57. }
  58. @Override
  59. public boolean getIsPrimitive() {
  60. return true;
  61. }
  62. @Override
  63. public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
  64. try {
  65. int length = ansIS.readLength();
  66. this._decode(ansIS, length);
  67. } catch (IOException e) {
  68. throw new MAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
  69. MAPParsingComponentExceptionReason.MistypedParameter);
  70. } catch (AsnException e) {
  71. throw new MAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
  72. MAPParsingComponentExceptionReason.MistypedParameter);
  73. }
  74. }
  75. @Override
  76. public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
  77. try {
  78. this._decode(ansIS, length);
  79. } catch (IOException e) {
  80. throw new MAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
  81. MAPParsingComponentExceptionReason.MistypedParameter);
  82. } catch (AsnException e) {
  83. throw new MAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
  84. MAPParsingComponentExceptionReason.MistypedParameter);
  85. }
  86. }
  87. private void _decode(AsnInputStream ais, int length) throws MAPParsingComponentException, IOException, AsnException {
  88. this.data = ais.readOctetStringData(length);
  89. if (this.data.length < 1 || this.data.length > 5)
  90. throw new MAPParsingComponentException("Error while decoding " + _PrimitiveName + ": octetString must be from 1 to 5 bytes length, found: "
  91. + this.data.length, MAPParsingComponentExceptionReason.MistypedParameter);
  92. }
  93. @Override
  94. public void encodeAll(AsnOutputStream asnOs) throws MAPException {
  95. this.encodeAll(asnOs, this.getTagClass(), this.getTag());
  96. }
  97. @Override
  98. public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
  99. try {
  100. asnOs.writeTag(tagClass, true, tag);
  101. int pos = asnOs.StartContentDefiniteLength();
  102. this.encodeData(asnOs);
  103. asnOs.FinalizeContent(pos);
  104. } catch (AsnException e) {
  105. throw new MAPException("AsnException when encoding " + _PrimitiveName + ": " + e.getMessage(), e);
  106. }
  107. }
  108. @Override
  109. public void encodeData(AsnOutputStream asnOs) throws MAPException {
  110. if (this.data == null)
  111. throw new MAPException("data must not be null");
  112. if (this.data.length < 1 && this.data.length > 5)
  113. throw new MAPException("data length must be from 1 to 5");
  114. asnOs.writeOctetStringData(data);
  115. }
  116. @Override
  117. public String toString() {
  118. StringBuilder sb = new StringBuilder();
  119. sb.append("ExtBearerServiceCode [");
  120. if (this.data != null) {
  121. sb.append("data=");
  122. sb.append(printDataArr(this.data));
  123. }
  124. sb.append("]");
  125. return sb.toString();
  126. }
  127. private String printDataArr(byte[] arr) {
  128. StringBuilder sb = new StringBuilder();
  129. for (int b : arr) {
  130. sb.append(b);
  131. sb.append(" ");
  132. }
  133. return sb.toString();
  134. }
  135. }