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

http://mobicents.googlecode.com/ · Java · 202 lines · 139 code · 37 blank · 26 comment · 21 complexity · bf7ed33767b9f275f13c5693a290992b 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.ExtBasicServiceCode;
  32. import org.mobicents.protocols.ss7.map.api.service.subscriberManagement.ExtBearerServiceCode;
  33. import org.mobicents.protocols.ss7.map.api.service.subscriberManagement.ExtTeleserviceCode;
  34. import org.mobicents.protocols.ss7.map.primitives.MAPAsnPrimitive;
  35. /**
  36. *
  37. * @author sergey vetyutnev
  38. *
  39. */
  40. public class ExtBasicServiceCodeImpl implements ExtBasicServiceCode, MAPAsnPrimitive {
  41. public static final int _ID_ext_BearerService = 2;
  42. public static final int _ID_ext_Teleservice = 3;
  43. public static final String _PrimitiveName = "ExtBasicServiceCode";
  44. private ExtBearerServiceCode extBearerService;
  45. private ExtTeleserviceCode extTeleservice;
  46. public ExtBasicServiceCodeImpl() {
  47. }
  48. public ExtBasicServiceCodeImpl(ExtBearerServiceCode extBearerService) {
  49. this.extBearerService = extBearerService;
  50. }
  51. public ExtBasicServiceCodeImpl(ExtTeleserviceCode extTeleservice) {
  52. this.extTeleservice = extTeleservice;
  53. }
  54. @Override
  55. public ExtBearerServiceCode getExtBearerService() {
  56. return extBearerService;
  57. }
  58. @Override
  59. public ExtTeleserviceCode getExtTeleservice() {
  60. return extTeleservice;
  61. }
  62. @Override
  63. public int getTag() throws MAPException {
  64. if (extBearerService != null)
  65. return _ID_ext_BearerService;
  66. else
  67. return _ID_ext_Teleservice;
  68. }
  69. @Override
  70. public int getTagClass() {
  71. return Tag.CLASS_CONTEXT_SPECIFIC;
  72. }
  73. @Override
  74. public boolean getIsPrimitive() {
  75. return true;
  76. }
  77. @Override
  78. public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
  79. try {
  80. int length = ansIS.readLength();
  81. this._decode(ansIS, length);
  82. } catch (IOException e) {
  83. throw new MAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
  84. MAPParsingComponentExceptionReason.MistypedParameter);
  85. } catch (AsnException e) {
  86. throw new MAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
  87. MAPParsingComponentExceptionReason.MistypedParameter);
  88. }
  89. }
  90. @Override
  91. public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
  92. try {
  93. this._decode(ansIS, length);
  94. } catch (IOException e) {
  95. throw new MAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
  96. MAPParsingComponentExceptionReason.MistypedParameter);
  97. } catch (AsnException e) {
  98. throw new MAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
  99. MAPParsingComponentExceptionReason.MistypedParameter);
  100. }
  101. }
  102. private void _decode(AsnInputStream ais, int length) throws MAPParsingComponentException, IOException, AsnException {
  103. this.extBearerService = null;
  104. this.extTeleservice = null;
  105. int tag = ais.getTag();
  106. if (ais.getTagClass() == Tag.CLASS_CONTEXT_SPECIFIC) {
  107. switch (tag) {
  108. case _ID_ext_BearerService:
  109. this.extBearerService = new ExtBearerServiceCodeImpl();
  110. ((ExtBearerServiceCodeImpl) this.extBearerService).decodeData(ais, length);
  111. break;
  112. case _ID_ext_Teleservice:
  113. this.extTeleservice = new ExtTeleserviceCodeImpl();
  114. ((ExtTeleserviceCodeImpl) this.extTeleservice).decodeData(ais, length);
  115. break;
  116. default:
  117. throw new MAPParsingComponentException("Error while decoding " + _PrimitiveName + ": bad choice tag",
  118. MAPParsingComponentExceptionReason.MistypedParameter);
  119. }
  120. } else {
  121. throw new MAPParsingComponentException("Error while decoding " + _PrimitiveName + ": bad choice tagClass",
  122. MAPParsingComponentExceptionReason.MistypedParameter);
  123. }
  124. }
  125. @Override
  126. public void encodeAll(AsnOutputStream asnOs) throws MAPException {
  127. this.encodeAll(asnOs, this.getTagClass(), this.getTag());
  128. }
  129. @Override
  130. public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
  131. try {
  132. asnOs.writeTag(tagClass, true, tag);
  133. int pos = asnOs.StartContentDefiniteLength();
  134. this.encodeData(asnOs);
  135. asnOs.FinalizeContent(pos);
  136. } catch (AsnException e) {
  137. throw new MAPException("AsnException when encoding " + _PrimitiveName + ": " + e.getMessage(), e);
  138. }
  139. }
  140. @Override
  141. public void encodeData(AsnOutputStream asnOs) throws MAPException {
  142. if (this.extBearerService == null && this.extTeleservice == null)
  143. throw new MAPException("Both extBearerService and extTeleservice must not be null");
  144. if (this.extBearerService != null && this.extTeleservice != null)
  145. throw new MAPException("Both extBearerService and extTeleservice must not be not null");
  146. if (this.extBearerService != null) {
  147. ((ExtBearerServiceCodeImpl) this.extBearerService).encodeData(asnOs);
  148. } else {
  149. ((ExtTeleserviceCodeImpl) this.extTeleservice).encodeData(asnOs);
  150. }
  151. }
  152. @Override
  153. public String toString() {
  154. StringBuilder sb = new StringBuilder();
  155. sb.append("ExtBasicServiceCode [");
  156. if (this.extBearerService != null) {
  157. sb.append(this.extBearerService.toString());
  158. }
  159. if (this.extTeleservice != null) {
  160. sb.append(this.extTeleservice.toString());
  161. }
  162. sb.append("]");
  163. return sb.toString();
  164. }
  165. }