/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/service/sms/AlertServiceCentreRequestIndicationImpl.java

http://mobicents.googlecode.com/ · Java · 220 lines · 157 code · 37 blank · 26 comment · 26 complexity · 0830b1b48912975f85bdbba0dbd5bfcb 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.sms;
  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.MAPMessageType;
  30. import org.mobicents.protocols.ss7.map.api.MAPOperationCode;
  31. import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
  32. import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
  33. import org.mobicents.protocols.ss7.map.api.primitives.AddressString;
  34. import org.mobicents.protocols.ss7.map.api.primitives.ISDNAddressString;
  35. import org.mobicents.protocols.ss7.map.api.service.sms.AlertServiceCentreRequestIndication;
  36. import org.mobicents.protocols.ss7.map.primitives.AddressStringImpl;
  37. import org.mobicents.protocols.ss7.map.primitives.ISDNAddressStringImpl;
  38. /**
  39. *
  40. * @author sergey vetyutnev
  41. *
  42. */
  43. public class AlertServiceCentreRequestIndicationImpl extends SmsMessageImpl implements AlertServiceCentreRequestIndication {
  44. private ISDNAddressString msisdn;
  45. private AddressString serviceCentreAddress;
  46. private int operationCode;
  47. public AlertServiceCentreRequestIndicationImpl(int operationCode) {
  48. this.operationCode = operationCode;
  49. }
  50. public AlertServiceCentreRequestIndicationImpl(ISDNAddressString msisdn, AddressString serviceCentreAddress) {
  51. this.msisdn = msisdn;
  52. this.serviceCentreAddress = serviceCentreAddress;
  53. }
  54. @Override
  55. public MAPMessageType getMessageType() {
  56. if (this.operationCode == MAPOperationCode.alertServiceCentre)
  57. return MAPMessageType.alertServiceCentre_Request;
  58. else
  59. return MAPMessageType.alertServiceCentreWithoutResult_Request;
  60. }
  61. @Override
  62. public int getOperationCode() {
  63. return this.operationCode;
  64. }
  65. @Override
  66. public ISDNAddressString getMsisdn() {
  67. return this.msisdn;
  68. }
  69. @Override
  70. public AddressString getServiceCentreAddress() {
  71. return this.serviceCentreAddress;
  72. }
  73. @Override
  74. public int getTag() throws MAPException {
  75. return Tag.SEQUENCE;
  76. }
  77. @Override
  78. public int getTagClass() {
  79. return Tag.CLASS_UNIVERSAL;
  80. }
  81. @Override
  82. public boolean getIsPrimitive() {
  83. return false;
  84. }
  85. @Override
  86. public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
  87. try {
  88. int length = ansIS.readLength();
  89. this._decode(ansIS, length);
  90. } catch (IOException e) {
  91. throw new MAPParsingComponentException("IOException when decoding AlertServiceCentreRequest: " + e.getMessage(), e,
  92. MAPParsingComponentExceptionReason.MistypedParameter);
  93. } catch (AsnException e) {
  94. throw new MAPParsingComponentException("AsnException when decoding AlertServiceCentreRequest: " + e.getMessage(), e,
  95. MAPParsingComponentExceptionReason.MistypedParameter);
  96. }
  97. }
  98. @Override
  99. public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
  100. try {
  101. this._decode(ansIS, length);
  102. } catch (IOException e) {
  103. throw new MAPParsingComponentException("IOException when decoding AlertServiceCentreRequest: " + e.getMessage(), e,
  104. MAPParsingComponentExceptionReason.MistypedParameter);
  105. } catch (AsnException e) {
  106. throw new MAPParsingComponentException("AsnException when decoding AlertServiceCentreRequest: " + e.getMessage(), e,
  107. MAPParsingComponentExceptionReason.MistypedParameter);
  108. }
  109. }
  110. private void _decode(AsnInputStream ansIS, int length) throws MAPParsingComponentException, IOException, AsnException {
  111. this.msisdn = null;
  112. this.serviceCentreAddress = null;
  113. AsnInputStream ais = ansIS.readSequenceStreamData(length);
  114. int num = 0;
  115. while( true ) {
  116. if (ais.available() == 0)
  117. break;
  118. int tag = ais.readTag();
  119. switch(num) {
  120. case 0:
  121. if (ais.getTagClass() != Tag.CLASS_UNIVERSAL || !ais.isTagPrimitive() || tag != Tag.STRING_OCTET)
  122. throw new MAPParsingComponentException(
  123. "Error while decoding AlertServiceCentreRequest.msisdn: bad tag or tag class or is not primitive: TagClass=" + ais.getTagClass()
  124. + ", tag=" + tag, MAPParsingComponentExceptionReason.MistypedParameter);
  125. this.msisdn = new ISDNAddressStringImpl();
  126. ((ISDNAddressStringImpl)this.msisdn).decodeAll(ais);
  127. break;
  128. case 1:
  129. if (ais.getTagClass() != Tag.CLASS_UNIVERSAL || !ais.isTagPrimitive() || tag != Tag.STRING_OCTET)
  130. throw new MAPParsingComponentException(
  131. "Error while decoding AlertServiceCentreRequest.serviceCentreAddress: bad tag or tag class or is not primitive: TagClass=" + ais.getTagClass()
  132. + ", tag=" + tag, MAPParsingComponentExceptionReason.MistypedParameter);
  133. this.serviceCentreAddress = new ISDNAddressStringImpl();
  134. ((AddressStringImpl)this.serviceCentreAddress).decodeAll(ais);
  135. break;
  136. default:
  137. ais.advanceElement();
  138. break;
  139. }
  140. num++;
  141. }
  142. if (this.msisdn == null || this.serviceCentreAddress == null)
  143. throw new MAPParsingComponentException("Error while decoding AlertServiceCentreRequest: 2 parameters are mandatory, found " + num,
  144. MAPParsingComponentExceptionReason.MistypedParameter);
  145. }
  146. @Override
  147. public void encodeAll(AsnOutputStream asnOs) throws MAPException {
  148. this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.SEQUENCE);
  149. }
  150. @Override
  151. public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
  152. try {
  153. asnOs.writeTag(tagClass, false, tag);
  154. int pos = asnOs.StartContentDefiniteLength();
  155. this.encodeData(asnOs);
  156. asnOs.FinalizeContent(pos);
  157. } catch (AsnException e) {
  158. throw new MAPException("AsnException when encoding AlertServiceCentreRequest: " + e.getMessage(), e);
  159. }
  160. }
  161. @Override
  162. public void encodeData(AsnOutputStream asnOs) throws MAPException {
  163. if (this.msisdn == null || this.serviceCentreAddress == null)
  164. throw new MAPException("Error when encoding AlertServiceCentreRequest: msisdn or serviceCentreAddress must not be empty");
  165. ((ISDNAddressStringImpl)this.msisdn).encodeAll(asnOs);
  166. ((AddressStringImpl)this.serviceCentreAddress).encodeAll(asnOs);
  167. }
  168. @Override
  169. public String toString() {
  170. StringBuilder sb = new StringBuilder();
  171. sb.append("AlertServiceCentreRequest [");
  172. if (this.msisdn != null) {
  173. sb.append("msisdn=");
  174. sb.append(this.msisdn.toString());
  175. }
  176. if (this.serviceCentreAddress != null) {
  177. sb.append(", serviceCentreAddress=");
  178. sb.append(this.serviceCentreAddress.toString());
  179. }
  180. sb.append("]");
  181. return sb.toString();
  182. }
  183. }