/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/service/callhandling/CallReferenceNumberImpl.java

http://mobicents.googlecode.com/ · Java · 161 lines · 102 code · 33 blank · 26 comment · 8 complexity · 5cffdcbb4a05432601d41438792d9629 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.callhandling;
  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.callhandling.CallReferenceNumber;
  32. import org.mobicents.protocols.ss7.map.primitives.MAPAsnPrimitive;
  33. /**
  34. *
  35. * @author sergey vetyutnev
  36. *
  37. */
  38. public class CallReferenceNumberImpl implements CallReferenceNumber, MAPAsnPrimitive {
  39. public static final String _PrimitiveName = "CallReferenceNumber";
  40. private byte[] data;
  41. public CallReferenceNumberImpl(){
  42. }
  43. public CallReferenceNumberImpl(byte[] data) {
  44. this.data = data;
  45. }
  46. @Override
  47. public byte[] getData() {
  48. return this.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 false;
  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 CallReferenceNumber: " + e.getMessage(), e,
  69. MAPParsingComponentExceptionReason.MistypedParameter);
  70. } catch (AsnException e) {
  71. throw new MAPParsingComponentException("AsnException when decoding CallReferenceNumber: " + 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 CallReferenceNumber: " + e.getMessage(), e,
  81. MAPParsingComponentExceptionReason.MistypedParameter);
  82. } catch (AsnException e) {
  83. throw new MAPParsingComponentException("AsnException when decoding CallReferenceNumber: " + e.getMessage(), e,
  84. MAPParsingComponentExceptionReason.MistypedParameter);
  85. }
  86. }
  87. private void _decode(AsnInputStream ansIS, int length) throws MAPParsingComponentException, IOException, AsnException {
  88. this.data = ansIS.readOctetStringData(length);
  89. if (this.data.length < 1 || this.data.length > 8)
  90. throw new MAPParsingComponentException("Error decoding CallReferenceNumber: length must be from 1 to 8, real length = " + length,
  91. MAPParsingComponentExceptionReason.MistypedParameter);
  92. }
  93. @Override
  94. public void encodeAll(AsnOutputStream asnOs) throws MAPException {
  95. this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, 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 || this.data.length == 0)
  111. throw new MAPException("Error when encoding " + _PrimitiveName + ": data is empty");
  112. asnOs.writeOctetStringData(data);
  113. }
  114. @Override
  115. public String toString() {
  116. return "CallReferenceNumber [Data= " + this.printDataArr() + "]";
  117. }
  118. private String printDataArr() {
  119. StringBuilder sb = new StringBuilder();
  120. if( this.data!=null ) {
  121. for( int b : this.data ) {
  122. sb.append(b);
  123. sb.append(" ");
  124. }
  125. }
  126. return sb.toString();
  127. }
  128. }