PageRenderTime 72ms CodeModel.GetById 31ms app.highlight 36ms RepoModel.GetById 1ms app.codeStats 0ms

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