PageRenderTime 49ms CodeModel.GetById 1ms app.highlight 42ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 265 lines | 178 code | 55 blank | 32 comment | 31 complexity | 6dbb2055509390881bef221a87cdb41e 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 javolution.xml.XMLFormat;
 28import javolution.xml.stream.XMLStreamException;
 29
 30import org.mobicents.protocols.asn.AsnException;
 31import org.mobicents.protocols.asn.AsnInputStream;
 32import org.mobicents.protocols.asn.AsnOutputStream;
 33import org.mobicents.protocols.asn.Tag;
 34import org.mobicents.protocols.ss7.map.api.MAPException;
 35import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
 36import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
 37import org.mobicents.protocols.ss7.map.api.primitives.AddressNature;
 38import org.mobicents.protocols.ss7.map.api.primitives.AddressString;
 39import org.mobicents.protocols.ss7.map.api.primitives.NumberingPlan;
 40
 41/**
 42 * 
 43 * @author amit bhayani
 44 * @author sergey vetyutnev
 45 * 
 46 */
 47public class AddressStringImpl extends TbcdString implements AddressString {
 48	
 49	private static final String NAI = "nai";
 50	private static final String NPI = "npi";
 51	private static final String NUMBER = "number";
 52
 53	protected int NO_EXTENSION_MASK = 0x80;
 54	protected int NATURE_OF_ADD_IND_MASK = 0x70;
 55	protected int NUMBERING_PLAN_IND_MASK = 0x0F;
 56
 57	protected AddressNature addressNature;
 58	protected NumberingPlan numberingPlan;
 59	protected String address;
 60
 61	private boolean isExtension;
 62
 63	public AddressStringImpl() {
 64	}
 65
 66	public AddressStringImpl(AddressNature addressNature, NumberingPlan numberingPlan, String address) {
 67		super();
 68		this.addressNature = addressNature;
 69		this.numberingPlan = numberingPlan;
 70		this.address = address;
 71	}
 72
 73	public String getAddress() {
 74		return this.address;
 75	}
 76
 77	public AddressNature getAddressNature() {
 78		return this.addressNature;
 79	}
 80
 81	public NumberingPlan getNumberingPlan() {
 82		return this.numberingPlan;
 83	}
 84
 85	public boolean isExtension() {
 86		return isExtension;
 87	}
 88
 89	@Override
 90	public int getTag() throws MAPException {
 91		return Tag.STRING_OCTET;
 92	}
 93
 94	@Override
 95	public int getTagClass() {
 96		return Tag.CLASS_UNIVERSAL;
 97	}
 98
 99	@Override
100	public boolean getIsPrimitive() {
101		return true;
102	}
103
104	@Override
105	public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
106
107		try {
108			int length = ansIS.readLength();
109			this._decode(ansIS, length);
110		} catch (IOException e) {
111			throw new MAPParsingComponentException("IOException when decoding AddressString: " + e.getMessage(), e,
112					MAPParsingComponentExceptionReason.MistypedParameter);
113		}
114	}
115
116	@Override
117	public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
118
119		try {
120			this._decode(ansIS, length);
121		} catch (IOException e) {
122			throw new MAPParsingComponentException("IOException when decoding AddressString: " + e.getMessage(), e,
123					MAPParsingComponentExceptionReason.MistypedParameter);
124		}
125	}
126
127	protected void _testLengthDecode(int length) throws MAPParsingComponentException {
128		if (length > 20)
129			throw new MAPParsingComponentException("Error when decoding AddressString: mesage length must not exceed 20",
130					MAPParsingComponentExceptionReason.MistypedParameter);
131	}
132
133	private void _decode(AsnInputStream ansIS, int length) throws MAPParsingComponentException, IOException {
134
135		this._testLengthDecode(length);
136
137		// The first byte has extension, nature of address indicator and
138		// numbering plan indicator
139		int nature = ansIS.read();
140
141		if ((nature & NO_EXTENSION_MASK) == 0x80) {
142			this.isExtension = false;
143		} else {
144			this.isExtension = true;
145		}
146
147		int natureOfAddInd = ((nature & NATURE_OF_ADD_IND_MASK) >> 4);
148
149		this.addressNature = AddressNature.getInstance(natureOfAddInd);
150
151		int numbPlanInd = (nature & NUMBERING_PLAN_IND_MASK);
152
153		this.numberingPlan = NumberingPlan.getInstance(numbPlanInd);
154
155		this.address = this.decodeString(ansIS, length - 1);
156	}
157
158	@Override
159	public void encodeAll(AsnOutputStream asnOs) throws MAPException {
160
161		this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.STRING_OCTET);
162	}
163
164	@Override
165	public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
166
167		try {
168			asnOs.writeTag(tagClass, true, tag);
169			int pos = asnOs.StartContentDefiniteLength();
170			this.encodeData(asnOs);
171			asnOs.FinalizeContent(pos);
172		} catch (AsnException e) {
173			throw new MAPException("AsnException when encoding AddressString: " + e.getMessage(), e);
174		}
175	}
176
177	protected void _testLengthEncode() throws MAPException {
178
179		if (this.address.length() > 38)
180			throw new MAPException("Error when encoding AddressString: address length must not exceed 38 digits");
181	}
182
183	@Override
184	public void encodeData(AsnOutputStream asnOs) throws MAPException {
185
186		if (this.addressNature == null || this.numberingPlan == null || this.address == null)
187			throw new MAPException("Error when encoding AddressString: addressNature, numberingPlan or address is empty");
188
189		this._testLengthEncode();
190
191		int nature = 1;
192
193		if (this.isExtension) {
194			nature = 0;
195		}
196
197		nature = nature << 7;
198
199		nature = nature | (this.addressNature.getIndicator() << 4);
200
201		nature = nature | (this.numberingPlan.getIndicator());
202
203		asnOs.write(nature);
204
205		this.encodeString(asnOs, this.address);
206	}
207
208	@Override
209	public String toString() {
210		return "AddressString[AddressNature=" + this.addressNature.toString() + ", NumberingPlan=" + this.numberingPlan.toString() + ", Address="
211				+ this.address + "]";
212	}
213
214	@Override
215	public int hashCode() {
216		final int prime = 31;
217		int result = 1;
218		result = prime * result + ((address == null) ? 0 : address.hashCode());
219		result = prime * result + ((addressNature == null) ? 0 : addressNature.hashCode());
220		result = prime * result + ((numberingPlan == null) ? 0 : numberingPlan.hashCode());
221		return result;
222	}
223
224	@Override
225	public boolean equals(Object obj) {
226		if (this == obj)
227			return true;
228		if (obj == null)
229			return false;
230		if (getClass() != obj.getClass())
231			return false;
232		AddressStringImpl other = (AddressStringImpl) obj;
233		if (address == null) {
234			if (other.address != null)
235				return false;
236		} else if (!address.equals(other.address))
237			return false;
238		if (addressNature != other.addressNature)
239			return false;
240		if (numberingPlan != other.numberingPlan)
241			return false;
242		return true;
243	}
244
245	/**
246	 * XML Serialization/Deserialization
247	 */
248	protected static final XMLFormat<AddressStringImpl> ADDRESS_STRING_XML = new XMLFormat<AddressStringImpl>(AddressStringImpl.class) {
249
250		@Override
251		public void read(javolution.xml.XMLFormat.InputElement xml, AddressStringImpl addressStringImpl) throws XMLStreamException {
252			addressStringImpl.addressNature = AddressNature.getInstance(xml.getAttribute(NAI, 0));
253			addressStringImpl.numberingPlan = NumberingPlan.getInstance(xml.getAttribute(NPI, 0));
254			addressStringImpl.address = xml.getAttribute(NUMBER, "");
255		}
256
257		@Override
258		public void write(AddressStringImpl addressStringImpl, javolution.xml.XMLFormat.OutputElement xml) throws XMLStreamException {
259
260			xml.setAttribute(NAI, addressStringImpl.addressNature.getIndicator());
261			xml.setAttribute(NPI, addressStringImpl.numberingPlan.getIndicator());
262			xml.setAttribute(NUMBER, addressStringImpl.address);
263		}
264	};
265}