/protocols/ss7/inap/inap-impl/src/main/java/org/mobicents/protocols/ss7/inap/primitives/LegIDImpl.java

http://mobicents.googlecode.com/ · Java · 205 lines · 147 code · 32 blank · 26 comment · 30 complexity · 443c55defd02f2fa68e228613b342ab2 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.inap.primitives;
  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.primitives.LegID;
  32. import org.mobicents.protocols.ss7.inap.api.primitives.LegType;
  33. /**
  34. *
  35. * @author sergey vetyutnev
  36. *
  37. */
  38. public class LegIDImpl implements LegID, INAPAsnPrimitive {
  39. public static final int _ID_sendingSideID = 0;
  40. public static final int _ID_receivingSideID = 1;
  41. public static final String _PrimitiveName = "LegID";
  42. private LegType sendingSideID;
  43. private LegType receivingSideID;
  44. public LegIDImpl() {
  45. }
  46. public LegIDImpl(boolean isSendingSideID, LegType legID) {
  47. if (isSendingSideID)
  48. this.sendingSideID = legID;
  49. else
  50. this.receivingSideID = legID;
  51. }
  52. @Override
  53. public LegType getSendingSideID() {
  54. return sendingSideID;
  55. }
  56. @Override
  57. public LegType getReceivingSideID() {
  58. return receivingSideID;
  59. }
  60. @Override
  61. public int getTag() throws INAPException {
  62. if (this.sendingSideID != null) {
  63. return _ID_sendingSideID;
  64. } else {
  65. return _ID_receivingSideID;
  66. }
  67. }
  68. @Override
  69. public int getTagClass() {
  70. return Tag.CLASS_CONTEXT_SPECIFIC;
  71. }
  72. @Override
  73. public boolean getIsPrimitive() {
  74. return true;
  75. }
  76. @Override
  77. public void decodeAll(AsnInputStream ansIS) throws INAPParsingComponentException {
  78. try {
  79. int length = ansIS.readLength();
  80. this._decode(ansIS, length);
  81. } catch (IOException e) {
  82. throw new INAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
  83. INAPParsingComponentExceptionReason.MistypedParameter);
  84. } catch (AsnException e) {
  85. throw new INAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
  86. INAPParsingComponentExceptionReason.MistypedParameter);
  87. }
  88. }
  89. @Override
  90. public void decodeData(AsnInputStream ansIS, int length) throws INAPParsingComponentException {
  91. try {
  92. this._decode(ansIS, length);
  93. } catch (IOException e) {
  94. throw new INAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
  95. INAPParsingComponentExceptionReason.MistypedParameter);
  96. } catch (AsnException e) {
  97. throw new INAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
  98. INAPParsingComponentExceptionReason.MistypedParameter);
  99. }
  100. }
  101. private void _decode(AsnInputStream asnIS, int length) throws INAPParsingComponentException, IOException, AsnException {
  102. if (asnIS.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC || !asnIS.isTagPrimitive())
  103. throw new INAPParsingComponentException("Error while decoding " + _PrimitiveName + ": bad tag class or is not primitive: TagClass=" + asnIS.getTagClass(),
  104. INAPParsingComponentExceptionReason.MistypedParameter);
  105. byte[] buf;
  106. switch (asnIS.getTag()) {
  107. case _ID_sendingSideID:
  108. buf = asnIS.readOctetStringData(length);
  109. if (buf.length != 1)
  110. throw new INAPParsingComponentException("Error while decoding " + _PrimitiveName + ": sendingSideID length must be 1 but it equals "
  111. + buf.length, INAPParsingComponentExceptionReason.MistypedParameter);
  112. this.sendingSideID = LegType.getInstance(buf[0]);
  113. if (this.sendingSideID == null)
  114. throw new INAPParsingComponentException("Error while decoding " + _PrimitiveName + ": sendingSideID value must be 1 or 2 it equals "
  115. + buf[0], INAPParsingComponentExceptionReason.MistypedParameter);
  116. break;
  117. case _ID_receivingSideID:
  118. buf = asnIS.readOctetStringData(length);
  119. if (buf.length != 1)
  120. throw new INAPParsingComponentException("Error while decoding " + _PrimitiveName + ": sendingSideID length must be 1 but it equals "
  121. + buf.length, INAPParsingComponentExceptionReason.MistypedParameter);
  122. this.receivingSideID = LegType.getInstance(buf[0]);
  123. if (this.receivingSideID == null)
  124. throw new INAPParsingComponentException("Error while decoding " + _PrimitiveName + ": sendingSideID value must be 1 or 2 it equals "
  125. + buf[0], INAPParsingComponentExceptionReason.MistypedParameter);
  126. break;
  127. default:
  128. throw new INAPParsingComponentException("Error while decoding " + _PrimitiveName + ": bad tag : tag=" + asnIS.getTag(),
  129. INAPParsingComponentExceptionReason.MistypedParameter);
  130. }
  131. }
  132. @Override
  133. public void encodeAll(AsnOutputStream asnOs) throws INAPException {
  134. this.encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, this.getTag());
  135. }
  136. @Override
  137. public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws INAPException {
  138. try {
  139. asnOs.writeTag(tagClass, true, tag);
  140. int pos = asnOs.StartContentDefiniteLength();
  141. this.encodeData(asnOs);
  142. asnOs.FinalizeContent(pos);
  143. } catch (AsnException e) {
  144. throw new INAPException("AsnException when encoding " + _PrimitiveName + ": " + e.getMessage(), e);
  145. }
  146. }
  147. @Override
  148. public void encodeData(AsnOutputStream asnOs) throws INAPException {
  149. if (this.sendingSideID == null && this.receivingSideID == null || this.sendingSideID != null && this.receivingSideID != null)
  150. throw new INAPException("Error while encoding the " + _PrimitiveName + ": one of sendingSideID or receivingSideID (not both) must not be empty");
  151. byte[] buf = new byte[1];
  152. if (this.sendingSideID != null)
  153. buf[0] = (byte) sendingSideID.getCode();
  154. else
  155. buf[0] = (byte) receivingSideID.getCode();
  156. asnOs.writeOctetStringData(buf);
  157. }
  158. @Override
  159. public String toString() {
  160. StringBuilder sb = new StringBuilder();
  161. sb.append("LegID [");
  162. if (this.sendingSideID != null) {
  163. sb.append("sendingSideID=");
  164. sb.append(sendingSideID);
  165. }
  166. if (this.receivingSideID != null) {
  167. sb.append("receivingSideID=");
  168. sb.append(receivingSideID);
  169. }
  170. sb.append("]");
  171. return sb.toString();
  172. }
  173. }