/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/primitives/USSDStringImpl.java

http://mobicents.googlecode.com/ · Java · 261 lines · 161 code · 31 blank · 69 comment · 25 complexity · 2143541ee0db4935cbbab9b5d972186a 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.primitives;
  23. import java.io.IOException;
  24. import java.nio.ByteBuffer;
  25. import java.nio.CharBuffer;
  26. import java.nio.charset.CharacterCodingException;
  27. import java.nio.charset.Charset;
  28. import org.mobicents.protocols.asn.AsnException;
  29. import org.mobicents.protocols.asn.AsnInputStream;
  30. import org.mobicents.protocols.asn.AsnOutputStream;
  31. import org.mobicents.protocols.asn.Tag;
  32. import org.mobicents.protocols.ss7.map.GSMCharset;
  33. import org.mobicents.protocols.ss7.map.GSMCharsetDecoder;
  34. import org.mobicents.protocols.ss7.map.GSMCharsetDecodingData;
  35. import org.mobicents.protocols.ss7.map.GSMCharsetEncoder;
  36. import org.mobicents.protocols.ss7.map.GSMCharsetEncodingData;
  37. import org.mobicents.protocols.ss7.map.api.MAPException;
  38. import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
  39. import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
  40. import org.mobicents.protocols.ss7.map.api.primitives.USSDString;
  41. /**
  42. *
  43. * @author amit bhayani
  44. *
  45. */
  46. public class USSDStringImpl implements USSDString, MAPAsnPrimitive {
  47. private String ussdString;
  48. private byte[] encodedString;
  49. //TODO : Should Charset be serializable?
  50. private transient Charset charset;
  51. /**
  52. *
  53. */
  54. public USSDStringImpl() {
  55. super();
  56. }
  57. public USSDStringImpl(String ussdString, Charset charset) {
  58. this.ussdString = ussdString;
  59. this.charset = charset;
  60. // set to default if not set by user
  61. if (this.charset == null) {
  62. this.charset = new GSMCharset("GSM", new String[] {});
  63. }
  64. }
  65. public USSDStringImpl(byte[] encodedString, Charset charset) {
  66. this.encodedString = encodedString;
  67. this.charset = charset;
  68. // set to default if not set by user
  69. if (this.charset == null) {
  70. this.charset = new GSMCharset("GSM", new String[] {});
  71. }
  72. }
  73. public byte[] getEncodedString() {
  74. return this.encodedString;
  75. }
  76. public String getString() {
  77. return this.ussdString;
  78. }
  79. public Charset getCharset() {
  80. return this.charset;
  81. }
  82. /* (non-Javadoc)
  83. * @see org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTag()
  84. */
  85. @Override
  86. public int getTag() throws MAPException {
  87. return Tag.STRING_OCTET;
  88. }
  89. /* (non-Javadoc)
  90. * @see org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTagClass()
  91. */
  92. @Override
  93. public int getTagClass() {
  94. return Tag.CLASS_UNIVERSAL;
  95. }
  96. /* (non-Javadoc)
  97. * @see org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getIsPrimitive()
  98. */
  99. @Override
  100. public boolean getIsPrimitive() {
  101. return true;
  102. }
  103. /* (non-Javadoc)
  104. * @see org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeAll(org.mobicents.protocols.asn.AsnInputStream)
  105. */
  106. @Override
  107. public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
  108. try {
  109. int length = ansIS.readLength();
  110. this._decode(ansIS, length);
  111. } catch (IOException e) {
  112. throw new MAPParsingComponentException("IOException when decoding ReportSMDeliveryStatusRequest: " + e.getMessage(), e,
  113. MAPParsingComponentExceptionReason.MistypedParameter);
  114. } catch (AsnException e) {
  115. throw new MAPParsingComponentException("AsnException when decoding ReportSMDeliveryStatusRequest: " + e.getMessage(), e,
  116. MAPParsingComponentExceptionReason.MistypedParameter);
  117. }
  118. }
  119. /* (non-Javadoc)
  120. * @see org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeData(org.mobicents.protocols.asn.AsnInputStream, int)
  121. */
  122. @Override
  123. public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
  124. try {
  125. this._decode(ansIS, length);
  126. } catch (IOException e) {
  127. throw new MAPParsingComponentException("IOException when decoding ReportSMDeliveryStatusRequest: " + e.getMessage(), e,
  128. MAPParsingComponentExceptionReason.MistypedParameter);
  129. } catch (AsnException e) {
  130. throw new MAPParsingComponentException("AsnException when decoding ReportSMDeliveryStatusRequest: " + e.getMessage(), e,
  131. MAPParsingComponentExceptionReason.MistypedParameter);
  132. }
  133. }
  134. private void _decode(AsnInputStream asnIS, int length) throws MAPParsingComponentException, IOException, AsnException {
  135. this.encodedString = asnIS.readOctetStringData(length);
  136. ByteBuffer bb = ByteBuffer.wrap(this.encodedString);
  137. // set to default if not set by user
  138. if (charset == null) {
  139. charset = new GSMCharset("GSM", new String[] {});
  140. }
  141. // CharBuffer bf = this.charset.decode(bb);
  142. // this.ussdString = bf.toString();
  143. GSMCharsetDecoder decoder = (GSMCharsetDecoder) this.charset.newDecoder();
  144. decoder.setGSMCharsetDecodingData(new GSMCharsetDecodingData());
  145. CharBuffer bf = null;
  146. try {
  147. bf = decoder.decode(bb);
  148. } catch (CharacterCodingException e) {
  149. // This can not occur
  150. }
  151. if (bf != null)
  152. this.ussdString = bf.toString();
  153. else
  154. this.ussdString = "";
  155. }
  156. /* (non-Javadoc)
  157. * @see org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll(org.mobicents.protocols.asn.AsnOutputStream)
  158. */
  159. @Override
  160. public void encodeAll(AsnOutputStream asnOs) throws MAPException {
  161. this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.STRING_OCTET);
  162. }
  163. /* (non-Javadoc)
  164. * @see org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll(org.mobicents.protocols.asn.AsnOutputStream, int, int)
  165. */
  166. @Override
  167. public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
  168. try {
  169. asnOs.writeTag(tagClass, true, tag);
  170. int pos = asnOs.StartContentDefiniteLength();
  171. this.encodeData(asnOs);
  172. asnOs.FinalizeContent(pos);
  173. } catch (AsnException e) {
  174. throw new MAPException("AsnException when encoding reportSMDeliveryStatusRequest: " + e.getMessage(), e);
  175. }
  176. }
  177. /* (non-Javadoc)
  178. * @see org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeData(org.mobicents.protocols.asn.AsnOutputStream)
  179. */
  180. @Override
  181. public void encodeData(AsnOutputStream asnOs) throws MAPException {
  182. if (this.ussdString == null) {
  183. throw new MAPException("Error while encoding USSDString the mandatory USSDString is not defined");
  184. }
  185. // ByteBuffer bb = this.charset.encode(ussdString);
  186. // // Not using bb.array() as it also includes the bytes beyond limit till
  187. // // capacity
  188. // encodedString = new byte[bb.limit()];
  189. // int count = 0;
  190. // while (bb.hasRemaining()) {
  191. // encodedString[count++] = bb.get();
  192. // }
  193. GSMCharsetEncoder encoder = (GSMCharsetEncoder) charset.newEncoder();
  194. encoder.setGSMCharsetEncodingData(new GSMCharsetEncodingData());
  195. ByteBuffer bb = null;
  196. try {
  197. bb = encoder.encode(CharBuffer.wrap(this.ussdString));
  198. } catch (Exception e) {
  199. // This can not occur
  200. }
  201. if (bb != null) {
  202. this.encodedString = new byte[bb.limit()];
  203. bb.get(this.encodedString);
  204. asnOs.writeOctetStringData(this.encodedString);
  205. }
  206. }
  207. @Override
  208. public int hashCode() {
  209. final int prime = 31;
  210. int result = 1;
  211. result = prime * result + ((ussdString == null) ? 0 : ussdString.hashCode());
  212. return result;
  213. }
  214. @Override
  215. public boolean equals(Object obj) {
  216. if (this == obj)
  217. return true;
  218. if (obj == null)
  219. return false;
  220. if (getClass() != obj.getClass())
  221. return false;
  222. USSDStringImpl other = (USSDStringImpl) obj;
  223. if (ussdString == null) {
  224. if (other.ussdString != null)
  225. return false;
  226. } else if (!ussdString.equals(other.ussdString))
  227. return false;
  228. return true;
  229. }
  230. }