PageRenderTime 18ms CodeModel.GetById 5ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 198 lines | 113 code | 33 blank | 52 comment | 17 complexity | 7e84adf3a03a0554dd0a7bd5031b4c12 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;
 26
 27import org.mobicents.protocols.asn.AsnException;
 28import org.mobicents.protocols.asn.AsnInputStream;
 29import org.mobicents.protocols.asn.AsnOutputStream;
 30import org.mobicents.protocols.asn.Tag;
 31import org.mobicents.protocols.ss7.map.api.MAPException;
 32import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
 33import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
 34import org.mobicents.protocols.ss7.map.api.primitives.IMSI;
 35
 36/**
 37 * 
 38 * @author sergey vetyutnev
 39 * 
 40 */
 41public class IMSIImpl extends TbcdString implements IMSI {
 42
 43	private String data;
 44
 45	
 46	public IMSIImpl() {
 47	}
 48	
 49	public IMSIImpl(String data) {
 50		this.data = data;
 51	}
 52
 53	@Override
 54	public String getData() {
 55		return this.data;
 56	}
 57
 58	
 59	@Override
 60	public int getTag() throws MAPException {
 61		return Tag.STRING_OCTET;
 62	}
 63
 64	@Override
 65	public int getTagClass() {
 66		return Tag.CLASS_UNIVERSAL;
 67	}
 68
 69	@Override
 70	public boolean getIsPrimitive() {
 71		return true;
 72	}
 73	
 74
 75	@Override
 76	public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
 77		try {
 78			int length = ansIS.readLength();
 79			this._decode(ansIS, length);
 80		} catch (IOException e) {
 81			throw new MAPParsingComponentException("IOException when decoding IMSI: " + e.getMessage(), e,
 82					MAPParsingComponentExceptionReason.MistypedParameter);
 83		}
 84	}
 85
 86	@Override
 87	public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
 88		try {
 89			this._decode(ansIS, length);
 90		} catch (IOException e) {
 91			throw new MAPParsingComponentException("IOException when decoding IMSI: " + e.getMessage(), e,
 92					MAPParsingComponentExceptionReason.MistypedParameter);
 93		}
 94	}
 95
 96	private void _decode(AsnInputStream ansIS, int length) throws MAPParsingComponentException, IOException {
 97
 98		if (length < 3 || length > 8)
 99			throw new MAPParsingComponentException("Error decoding IMSI: the IMSI field must contain from 3 to 8 octets. Contains: " + length,
100					MAPParsingComponentExceptionReason.MistypedParameter);
101
102		try {
103			String res = this.decodeString(ansIS, length);
104			this.data = res;
105
106//			String sMcc = res.substring(0, 3);
107//			String sMnc = res.substring(3, 5);
108//			this.MSIN = res.substring(5);
109//
110//			this.MCC = (long) Integer.parseInt(sMcc);
111//			this.MNC = (long) Integer.parseInt(sMnc);
112		} catch (IOException e) {
113			throw new MAPParsingComponentException("IOException when decoding IMSI: " + e.getMessage(), e,
114					MAPParsingComponentExceptionReason.MistypedParameter);
115		}
116	}	
117	
118	@Override
119	public void encodeAll(AsnOutputStream asnOs) throws MAPException {
120		
121		this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.STRING_OCTET);
122	}
123
124	@Override
125	public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
126		
127		try {
128			asnOs.writeTag(tagClass, true, tag);
129			int pos = asnOs.StartContentDefiniteLength();
130			this.encodeData(asnOs);
131			asnOs.FinalizeContent(pos);
132		} catch (AsnException e) {
133			throw new MAPException("AsnException when encoding IMSI: " + e.getMessage(), e);
134		}
135	}
136
137	@Override
138	public void encodeData(AsnOutputStream asnOs) throws MAPException {
139
140		if (this.data == null)
141			throw new MAPException("Error while encoding the IMSI: data is not defined");
142
143
144//		if (this.MCC == null || this.MNC == null || this.MSIN == null)
145//			throw new MAPException("Error while encoding the IMSI: MMC, MNC or MSIN is not defined");
146//
147//		if (this.MCC < 0 || this.MCC > 999)
148//			throw new MAPException("Error while encoding the IMSI: Bad MCC value");
149//		if (this.MNC < 0 || this.MNC > 99)
150//			throw new MAPException("Error while encoding the IMSI: Bad MNC value");
151//		if (this.MSIN.length() < 1 || this.MSIN.length() > 11)
152//			throw new MAPException("Error while encoding the IMSI: Bad MSIN value");
153//
154//		StringBuilder sb = new StringBuilder();
155//		if (this.MCC < 100)
156//			sb.append("0");
157//		if (this.MCC < 10)
158//			sb.append("0");
159//		sb.append(this.MCC);
160//		if (this.MNC < 10)
161//			sb.append("0");
162//		sb.append(this.MNC);
163//		sb.append(this.MSIN);
164
165		this.encodeString(asnOs, this.data);
166	}
167
168	
169	@Override
170	public String toString() {
171		return "IMSI [" + this.data + "]";
172	}
173
174	@Override
175	public int hashCode() {
176		final int prime = 31;
177		int result = 1;
178		result = prime * result + ((data == null) ? 0 : data.hashCode());
179		return result;
180	}
181
182	@Override
183	public boolean equals(Object obj) {
184		if (this == obj)
185			return true;
186		if (obj == null)
187			return false;
188		if (getClass() != obj.getClass())
189			return false;
190		IMSIImpl other = (IMSIImpl) obj;
191		if (data == null) {
192			if (other.data != null)
193				return false;
194		} else if (!data.equals(other.data))
195			return false;
196		return true;
197	}
198}