PageRenderTime 27ms CodeModel.GetById 7ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://mobicents.googlecode.com/
Java | 306 lines | 215 code | 52 blank | 39 comment | 62 complexity | 885c483eb344ad0a94b5bb87c323984e 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.util.ArrayList;
 27import java.util.Arrays;
 28
 29import org.mobicents.protocols.asn.AsnException;
 30import org.mobicents.protocols.asn.AsnInputStream;
 31import org.mobicents.protocols.asn.AsnOutputStream;
 32import org.mobicents.protocols.asn.Tag;
 33import org.mobicents.protocols.ss7.map.api.MAPException;
 34import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
 35import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
 36import org.mobicents.protocols.ss7.map.api.primitives.MAPExtensionContainer;
 37import org.mobicents.protocols.ss7.map.api.primitives.MAPPrivateExtension;
 38
 39/**
 40 * @author sergey vetyutnev
 41 * 
 42 */
 43public class MAPExtensionContainerImpl implements MAPExtensionContainer, MAPAsnPrimitive {
 44
 45	protected static final int PRIVATEEXTENSIONLIST_REF_TAG = 0x00;
 46	protected static final int PSCEXTENSIONS_REF_TAG = 0x01;
 47
 48	private ArrayList<MAPPrivateExtension> privateExtensionList;
 49	private byte[] pcsExtensions;
 50
 51	public MAPExtensionContainerImpl() {
 52	}
 53
 54	public MAPExtensionContainerImpl(ArrayList<MAPPrivateExtension> privateExtensionList, byte[] pcsExtensions) {
 55		this.privateExtensionList = privateExtensionList;
 56		this.pcsExtensions = pcsExtensions;
 57	}
 58
 59	@Override
 60	public ArrayList<MAPPrivateExtension> getPrivateExtensionList() {
 61		return this.privateExtensionList;
 62	}
 63
 64	@Override
 65	public void setPrivateExtensionList(ArrayList<MAPPrivateExtension> privateExtensionList) {
 66		this.privateExtensionList = privateExtensionList;
 67	}
 68
 69	@Override
 70	public byte[] getPcsExtensions() {
 71		return this.pcsExtensions;
 72	}
 73
 74	@Override
 75	public void setPcsExtensions(byte[] pcsExtensions) {
 76		this.pcsExtensions = pcsExtensions;
 77	}
 78
 79
 80	@Override
 81	public int getTag() throws MAPException {
 82		return Tag.SEQUENCE;
 83	}
 84
 85	@Override
 86	public int getTagClass() {
 87		return Tag.CLASS_UNIVERSAL;
 88	}
 89
 90	@Override
 91	public boolean getIsPrimitive() {
 92		return false;
 93	}
 94
 95
 96	@Override
 97	public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
 98		// Definition from GSM 09.02 version 5.15.1 Page 690
 99		// extensionContainer SEQUENCE {
100		// privateExtensionList [0] IMPLICIT SEQUENCE ( SIZE( 1 .. 10 ) ) OF
101		// SEQUENCE {
102		// extId MAP-EXTENSION .&extensionId ( {
103		// ,
104		// ...} ) ,
105		// extType MAP-EXTENSION .&ExtensionType ( {
106		// ,
107		// ...} { @extId } ) OPTIONAL} OPTIONAL,
108		// pcs-Extensions [1] IMPLICIT SEQUENCE {
109		// ... } OPTIONAL,
110		// ... } OPTIONAL,
111		// ... }
112		
113		try {
114			AsnInputStream ais = ansIS.readSequenceStream();
115			this._decode(ais);
116		} catch (IOException e) {
117			throw new MAPParsingComponentException("IOException when decoding ExtensionContainer: " + e.getMessage(), e,
118					MAPParsingComponentExceptionReason.MistypedParameter);
119		} catch (AsnException e) {
120			throw new MAPParsingComponentException("AsnException when decoding ExtensionContainer: " + e.getMessage(), e,
121					MAPParsingComponentExceptionReason.MistypedParameter);
122		}
123	}
124
125	@Override
126	public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
127
128		try {
129			AsnInputStream ais = ansIS.readSequenceStreamData(length);
130			this._decode(ais);
131		} catch (IOException e) {
132			throw new MAPParsingComponentException("IOException when decoding ExtensionContainer: " + e.getMessage(), e,
133					MAPParsingComponentExceptionReason.MistypedParameter);
134		} catch (AsnException e) {
135			throw new MAPParsingComponentException("AsnException when decoding ExtensionContainer: " + e.getMessage(), e,
136					MAPParsingComponentExceptionReason.MistypedParameter);
137		}
138	}
139	
140	private void _decode(AsnInputStream ansIS) throws MAPParsingComponentException, IOException, AsnException {
141
142		this.privateExtensionList = null;
143		this.pcsExtensions = null;
144
145		while (true) {
146			if (ansIS.available() == 0)
147				break;
148
149			int tag = ansIS.readTag();
150			if (tag == MAPExtensionContainerImpl.PRIVATEEXTENSIONLIST_REF_TAG && ansIS.getTagClass() == Tag.CLASS_CONTEXT_SPECIFIC) {
151				
152				if(ansIS.isTagPrimitive())
153					throw new MAPParsingComponentException("Error while ExtensionContainer decoding: privateExtensionList is primitive",
154							MAPParsingComponentExceptionReason.MistypedParameter);
155				if (this.privateExtensionList != null)
156					throw new MAPParsingComponentException("Error while ExtensionContainer decoding: More than one PrivateExtensionList has found",
157							MAPParsingComponentExceptionReason.MistypedParameter);
158
159				AsnInputStream localAis2 = ansIS.readSequenceStream();
160				this.privateExtensionList = new ArrayList<MAPPrivateExtension>();
161				while (localAis2.available() > 0) {
162					tag = localAis2.readTag();
163					if (tag != Tag.SEQUENCE || localAis2.getTagClass() != Tag.CLASS_UNIVERSAL || localAis2.isTagPrimitive())
164						throw new MAPParsingComponentException("Error while ExtensionContainer decoding: Bad tag, tagClass or primitiveFactor of PrivateExtension",
165								MAPParsingComponentExceptionReason.MistypedParameter);
166					if (this.privateExtensionList.size() >= 10)
167						throw new MAPParsingComponentException("More then 10 PrivateExtension found when PrivateExtensionList decoding",
168								MAPParsingComponentExceptionReason.MistypedParameter);
169
170					MAPPrivateExtensionImpl privateExtension = new MAPPrivateExtensionImpl();
171					privateExtension.decodeAll(localAis2);
172					this.privateExtensionList.add(privateExtension);
173				}
174			} else if (tag == MAPExtensionContainerImpl.PSCEXTENSIONS_REF_TAG && ansIS.getTagClass() == Tag.CLASS_CONTEXT_SPECIFIC) {
175				
176				if(ansIS.isTagPrimitive())
177					throw new MAPParsingComponentException("Error while PCS-Extensions decoding: PCS-Extensions is primitive",
178							MAPParsingComponentExceptionReason.MistypedParameter);
179				if (this.pcsExtensions != null)
180					throw new MAPParsingComponentException("Error while PCS-Extensions decoding: More than one PCS-Extensions has found",
181							MAPParsingComponentExceptionReason.MistypedParameter);
182
183				this.pcsExtensions = ansIS.readSequence();
184			}
185		}
186	}
187	
188	@Override
189	public void encodeAll(AsnOutputStream asnOs) throws MAPException {
190		
191		this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.SEQUENCE);
192	}
193	
194	@Override
195	public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
196		
197		try {
198			asnOs.writeTag(tagClass, false, tag);
199			int pos = asnOs.StartContentDefiniteLength();
200			this.encodeData(asnOs);
201			asnOs.FinalizeContent(pos);
202		} catch (AsnException e) {
203			throw new MAPException("AsnException when encoding ExtensionContainer: " + e.getMessage(), e);
204		}
205	}
206
207	@Override
208	public void encodeData(AsnOutputStream asnOs) throws MAPException {
209		
210		if (this.privateExtensionList == null && this.pcsExtensions == null)
211			throw new MAPException(
212					"Error when encoding ExtensionContainer: Both PrivateExtensionList and PcsExtensions are empty when ExtensionContainer encoding");
213		if (this.privateExtensionList != null && (this.privateExtensionList.size() == 0 || this.privateExtensionList.size() > 10))
214			throw new MAPException(
215					"Error when encoding ExtensionContainer: PrivateExtensionList must contains from 1 to 10 elements when ExtensionContainer encoding");
216
217		try {
218			
219			if (this.privateExtensionList != null) {
220				asnOs.writeTag(Tag.CLASS_CONTEXT_SPECIFIC, false, PRIVATEEXTENSIONLIST_REF_TAG);
221				int pos2 = asnOs.StartContentDefiniteLength();
222				
223				for (MAPPrivateExtension pe : this.privateExtensionList) {
224					((MAPPrivateExtensionImpl)pe).encodeAll(asnOs);
225				}				
226
227				asnOs.FinalizeContent(pos2);
228			}
229			
230			if (this.pcsExtensions != null) {
231				asnOs.writeTag(Tag.CLASS_CONTEXT_SPECIFIC, false, PSCEXTENSIONS_REF_TAG);
232				int pos2 = asnOs.StartContentDefiniteLength();
233
234				asnOs.write(this.pcsExtensions);
235
236				asnOs.FinalizeContent(pos2);
237			}			
238		} catch (AsnException e) {
239			throw new MAPException("AsnException when encoding ExtensionContainer: " + e.getMessage(), e);
240		}
241	}
242	
243	
244	@Override
245	public String toString() {
246		StringBuilder sb = new StringBuilder();
247		sb.append("ExtensionContainer [");
248		
249		if (this.privateExtensionList != null && this.privateExtensionList.size() > 0) {
250			for(MAPPrivateExtension pe : this.privateExtensionList) {
251				sb.append("\n");
252				sb.append(pe.toString());
253			}
254		}
255		
256		if (this.pcsExtensions != null) {
257			sb.append("\nPcsExtensions=");
258			sb.append(this.ArrayToString(this.pcsExtensions));
259		}
260
261		sb.append("]");
262
263		return sb.toString();
264	}
265	
266	@Override
267	public int hashCode() {
268		final int prime = 31;
269		int result = 1;
270		result = prime * result + Arrays.hashCode(pcsExtensions);
271		result = prime * result + ((privateExtensionList == null) ? 0 : privateExtensionList.hashCode());
272		return result;
273	}
274
275	@Override
276	public boolean equals(Object obj) {
277		if (this == obj)
278			return true;
279		if (obj == null)
280			return false;
281		if (getClass() != obj.getClass())
282			return false;
283		MAPExtensionContainerImpl other = (MAPExtensionContainerImpl) obj;
284		if (!Arrays.equals(pcsExtensions, other.pcsExtensions))
285			return false;
286		if (privateExtensionList == null) {
287			if (other.privateExtensionList != null)
288				return false;
289		} else if (!privateExtensionList.equals(other.privateExtensionList))
290			return false;
291		return true;
292	}
293
294	private String ArrayToString(byte[] array) {
295		StringBuilder sb = new StringBuilder();
296		int i1 = 0;
297		for (byte b : array) {
298			if (i1 == 0)
299				i1 = 1;
300			else
301				sb.append(", ");
302			sb.append(b);
303		}
304		return sb.toString();
305	}
306}