/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/service/lsm/AreaListImpl.java

http://mobicents.googlecode.com/ · Java · 248 lines · 128 code · 29 blank · 91 comment · 20 complexity · 358de4dac30bd4de37c1a8ea8d994489 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.lsm;
  23. import java.io.IOException;
  24. import java.util.ArrayList;
  25. import java.util.Arrays;
  26. import java.util.List;
  27. import org.mobicents.protocols.asn.AsnException;
  28. import org.mobicents.protocols.asn.AsnInputStream;
  29. import org.mobicents.protocols.asn.AsnOutputStream;
  30. import org.mobicents.protocols.asn.Tag;
  31. import org.mobicents.protocols.ss7.map.api.MAPException;
  32. import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
  33. import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
  34. import org.mobicents.protocols.ss7.map.api.service.lsm.Area;
  35. import org.mobicents.protocols.ss7.map.api.service.lsm.AreaList;
  36. import org.mobicents.protocols.ss7.map.primitives.MAPAsnPrimitive;
  37. /**
  38. * @author amit bhayani
  39. *
  40. */
  41. public class AreaListImpl implements AreaList, MAPAsnPrimitive {
  42. private Area[] areas = null;
  43. /**
  44. *
  45. */
  46. public AreaListImpl() {
  47. super();
  48. }
  49. /**
  50. * @param areas
  51. */
  52. public AreaListImpl(Area[] areas) {
  53. super();
  54. this.areas = areas;
  55. }
  56. /*
  57. * (non-Javadoc)
  58. *
  59. * @see org.mobicents.protocols.ss7.map.api.service.lsm.AreaList#getAreas()
  60. */
  61. @Override
  62. public Area[] getAreas() {
  63. return this.areas;
  64. }
  65. /*
  66. * (non-Javadoc)
  67. *
  68. * @see
  69. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTag()
  70. */
  71. @Override
  72. public int getTag() throws MAPException {
  73. return Tag.SEQUENCE;
  74. }
  75. /*
  76. * (non-Javadoc)
  77. *
  78. * @see
  79. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTagClass
  80. * ()
  81. */
  82. @Override
  83. public int getTagClass() {
  84. return Tag.CLASS_UNIVERSAL;
  85. }
  86. /*
  87. * (non-Javadoc)
  88. *
  89. * @see
  90. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getIsPrimitive
  91. * ()
  92. */
  93. @Override
  94. public boolean getIsPrimitive() {
  95. return false;
  96. }
  97. /*
  98. * (non-Javadoc)
  99. *
  100. * @see
  101. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeAll
  102. * (org.mobicents.protocols.asn.AsnInputStream)
  103. */
  104. @Override
  105. public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
  106. try {
  107. int length = ansIS.readLength();
  108. this._decode(ansIS, length);
  109. } catch (IOException e) {
  110. throw new MAPParsingComponentException("IOException when decoding ReportSMDeliveryStatusRequest: " + e.getMessage(), e,
  111. MAPParsingComponentExceptionReason.MistypedParameter);
  112. } catch (AsnException e) {
  113. throw new MAPParsingComponentException("AsnException when decoding ReportSMDeliveryStatusRequest: " + e.getMessage(), e,
  114. MAPParsingComponentExceptionReason.MistypedParameter);
  115. }
  116. }
  117. /*
  118. * (non-Javadoc)
  119. *
  120. * @see
  121. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeData
  122. * (org.mobicents.protocols.asn.AsnInputStream, int)
  123. */
  124. @Override
  125. public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
  126. try {
  127. this._decode(ansIS, length);
  128. } catch (IOException e) {
  129. throw new MAPParsingComponentException("IOException when decoding ReportSMDeliveryStatusRequest: " + e.getMessage(), e,
  130. MAPParsingComponentExceptionReason.MistypedParameter);
  131. } catch (AsnException e) {
  132. throw new MAPParsingComponentException("AsnException when decoding ReportSMDeliveryStatusRequest: " + e.getMessage(), e,
  133. MAPParsingComponentExceptionReason.MistypedParameter);
  134. }
  135. }
  136. private void _decode(AsnInputStream asnIS, int length) throws MAPParsingComponentException, IOException, AsnException {
  137. AsnInputStream ais = asnIS.readSequenceStreamData(length);
  138. List<Area> arealList = new ArrayList<Area>();
  139. while (true) {
  140. if (ais.available() == 0)
  141. break;
  142. int tag = ais.readTag();
  143. if (ais.getTagClass() != Tag.CLASS_UNIVERSAL || ais.isTagPrimitive() || tag != Tag.SEQUENCE) {
  144. throw new MAPParsingComponentException("Error while decoding AreaList: bad tag class, tag or not primitive",
  145. MAPParsingComponentExceptionReason.MistypedParameter);
  146. }
  147. Area a = new AreaImpl();
  148. ((AreaImpl)a).decodeAll(ais);
  149. arealList.add(a);
  150. }//end of while
  151. this.areas = new Area[arealList.size()];
  152. this.areas = arealList.toArray(this.areas);
  153. }
  154. /*
  155. * (non-Javadoc)
  156. *
  157. * @see
  158. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll
  159. * (org.mobicents.protocols.asn.AsnOutputStream)
  160. */
  161. @Override
  162. public void encodeAll(AsnOutputStream asnOs) throws MAPException {
  163. this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.SEQUENCE);
  164. }
  165. /*
  166. * (non-Javadoc)
  167. *
  168. * @see
  169. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll
  170. * (org.mobicents.protocols.asn.AsnOutputStream, int, int)
  171. */
  172. @Override
  173. public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
  174. try {
  175. asnOs.writeTag(tagClass, false, tag);
  176. int pos = asnOs.StartContentDefiniteLength();
  177. this.encodeData(asnOs);
  178. asnOs.FinalizeContent(pos);
  179. } catch (AsnException e) {
  180. throw new MAPException("AsnException when encoding reportSMDeliveryStatusRequest: " + e.getMessage(), e);
  181. }
  182. }
  183. /*
  184. * (non-Javadoc)
  185. *
  186. * @see
  187. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeData
  188. * (org.mobicents.protocols.asn.AsnOutputStream)
  189. */
  190. @Override
  191. public void encodeData(AsnOutputStream asnOs) throws MAPException {
  192. if (this.areas == null || this.areas.length == 0) {
  193. throw new MAPException("Error while encoding AreaList the mandatory parameter[Area[]] is not defined");
  194. }
  195. for (int count = 0; count < areas.length; count++) {
  196. ((AreaImpl)areas[count]).encodeAll(asnOs);
  197. }
  198. }
  199. @Override
  200. public int hashCode() {
  201. final int prime = 31;
  202. int result = 1;
  203. result = prime * result + Arrays.hashCode(areas);
  204. return result;
  205. }
  206. @Override
  207. public boolean equals(Object obj) {
  208. if (this == obj)
  209. return true;
  210. if (obj == null)
  211. return false;
  212. if (getClass() != obj.getClass())
  213. return false;
  214. AreaListImpl other = (AreaListImpl) obj;
  215. if (!Arrays.equals(areas, other.areas))
  216. return false;
  217. return true;
  218. }
  219. }