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

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

http://mobicents.googlecode.com/
Java | 311 lines | 179 code | 43 blank | 89 comment | 30 complexity | 2d60c89151790a2909025828bce40aed 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
 23/*
 24 JBoss, Home of Professional Open Source
 25 * Copyright 2011, Red Hat, Inc. and individual contributors
 26 * by the @authors tag. See the copyright.txt in the distribution for a
 27 * full listing of individual contributors.
 28 *
 29 * This is free software; you can redistribute it and/or modify it
 30 * under the terms of the GNU Lesser General Public License as
 31 * published by the Free Software Foundation; either version 2.1 of
 32 * the License, or (at your option) any later version.
 33 *
 34 * This software is distributed in the hope that it will be useful,
 35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 37 * Lesser General Public License for more details.
 38 *
 39 * You should have received a copy of the GNU Lesser General Public
 40 * License along with this software; if not, write to the Free
 41 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 42 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 43 */
 44
 45package org.mobicents.protocols.ss7.map.primitives;
 46
 47import java.io.IOException;
 48import java.util.Arrays;
 49
 50import org.mobicents.protocols.asn.AsnException;
 51import org.mobicents.protocols.asn.AsnInputStream;
 52import org.mobicents.protocols.asn.AsnOutputStream;
 53import org.mobicents.protocols.asn.Tag;
 54import org.mobicents.protocols.ss7.map.api.MAPException;
 55import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
 56import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
 57import org.mobicents.protocols.ss7.map.api.primitives.MAPPrivateExtension;
 58
 59/**
 60 * @author sergey vetyutnev
 61 * 
 62 */
 63public class MAPPrivateExtensionImpl implements MAPPrivateExtension, MAPAsnPrimitive {
 64
 65	private long[] oId;
 66	private byte[] data;
 67
 68	public MAPPrivateExtensionImpl() {
 69	}
 70
 71	public MAPPrivateExtensionImpl(long[] oId, byte[] data) {
 72		this.oId = oId;
 73		this.data = data;
 74	}
 75
 76	/*
 77	 * (non-Javadoc)
 78	 * 
 79	 * @see
 80	 * org.mobicents.protocols.ss7.map.api.dialog.MAPPrivateExtension#getOId()
 81	 */
 82	@Override
 83	public long[] getOId() {
 84		return this.oId;
 85	}
 86
 87	/*
 88	 * (non-Javadoc)
 89	 * 
 90	 * @see
 91	 * org.mobicents.protocols.ss7.map.api.dialog.MAPPrivateExtension#setOId
 92	 * (long[])
 93	 */
 94	@Override
 95	public void setOId(long[] oId) {
 96		this.oId = oId;
 97	}
 98
 99	/*
100	 * (non-Javadoc)
101	 * 
102	 * @see
103	 * org.mobicents.protocols.ss7.map.api.dialog.MAPPrivateExtension#getData()
104	 */
105	@Override
106	public byte[] getData() {
107		return this.data;
108	}
109
110	/*
111	 * (non-Javadoc)
112	 * 
113	 * @see
114	 * org.mobicents.protocols.ss7.map.api.dialog.MAPPrivateExtension#setData
115	 * (byte[])
116	 */
117	@Override
118	public void setData(byte[] data) {
119		this.data = data;
120	}
121
122
123	@Override
124	public int getTag() throws MAPException {
125		return Tag.SEQUENCE;
126	}
127
128	@Override
129	public int getTagClass() {
130		return Tag.CLASS_UNIVERSAL;
131	}
132
133	@Override
134	public boolean getIsPrimitive() {
135		return false;
136	}
137
138	@Override
139	public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
140		// Definition from GSM 09.02 version 5.15.1 Page 690
141		// extensionContainer SEQUENCE {
142		// privateExtensionList [0] IMPLICIT SEQUENCE ( SIZE( 1 .. 10 ) ) OF
143		// SEQUENCE {
144		// extId MAP-EXTENSION .&extensionId ( {
145		// ,
146		// ...} ) ,
147		// extType MAP-EXTENSION .&ExtensionType ( {
148		// ,
149		// ...} { @extId } ) OPTIONAL} OPTIONAL,
150		// pcs-Extensions [1] IMPLICIT SEQUENCE {
151		// ... } OPTIONAL,
152		// ... } OPTIONAL,
153		// ... }
154
155		try {
156			AsnInputStream ais = ansIS.readSequenceStream();
157			this._decode(ais);
158		} catch (IOException e) {
159			throw new MAPParsingComponentException("IOException when decoding PrivateExtension: " + e.getMessage(), e,
160					MAPParsingComponentExceptionReason.MistypedParameter);
161		} catch (AsnException e) {
162			throw new MAPParsingComponentException("AsnException when decoding PrivateExtension: " + e.getMessage(), e,
163					MAPParsingComponentExceptionReason.MistypedParameter);
164		}
165	}
166
167	@Override
168	public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
169		
170		try {
171			AsnInputStream ais = ansIS.readSequenceStreamData(length);
172			this._decode(ais);
173		} catch (IOException e) {
174			throw new MAPParsingComponentException("IOException when decoding PrivateExtension: " + e.getMessage(), e,
175					MAPParsingComponentExceptionReason.MistypedParameter);
176		} catch (AsnException e) {
177			throw new MAPParsingComponentException("AsnException when decoding PrivateExtension: " + e.getMessage(), e,
178					MAPParsingComponentExceptionReason.MistypedParameter);
179		}
180	}
181	
182	private void _decode(AsnInputStream ansIS) throws MAPParsingComponentException, IOException, AsnException {
183
184		// extId
185		int tag = ansIS.readTag();
186		if (tag != Tag.OBJECT_IDENTIFIER || ansIS.getTagClass() != Tag.CLASS_UNIVERSAL || !ansIS.isTagPrimitive())
187			throw new MAPParsingComponentException("Error decoding PrivateExtension: bad tag, tagClass or primitiveFactor of ExtentionId",
188					MAPParsingComponentExceptionReason.MistypedParameter);
189		this.oId = ansIS.readObjectIdentifier();
190
191		// extType
192		if (ansIS.available() > 0) {
193			this.data = new byte[ansIS.available()];
194			ansIS.read(this.data);
195		}
196		else
197			this.data = null;
198	}
199
200	@Override
201	public void encodeAll(AsnOutputStream asnOs) throws MAPException {
202		this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.SEQUENCE);
203	}
204
205	@Override
206	public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
207		
208		try {
209			asnOs.writeTag(tagClass, false, tag);
210			int pos = asnOs.StartContentDefiniteLength();
211			this.encodeData(asnOs);
212			asnOs.FinalizeContent(pos);
213		} catch (AsnException e) {
214			throw new MAPException("AsnException when encoding PrivateExtension: " + e.getMessage(), e);
215		}
216	}
217
218	@Override
219	public void encodeData(AsnOutputStream asnOs) throws MAPException {
220		
221		if (this.oId == null || this.oId.length < 2)
222			throw new MAPException("Error when encoding PrivateExtension: OId value must not be empty when coding PrivateExtension");
223		
224		try {
225			asnOs.writeObjectIdentifier(this.oId);
226			if (this.data != null)
227				asnOs.write(this.data);
228		} catch (IOException e) {
229			throw new MAPException("IOException when encoding PrivateExtension: " + e.getMessage(), e);
230		} catch (AsnException e) {
231			throw new MAPException("AsnException when encoding PrivateExtension: " + e.getMessage(), e);
232		}
233	}
234	
235	@Override
236	public String toString() {
237		StringBuilder sb = new StringBuilder();
238		sb.append("PrivateExtension [");
239		
240		if (this.oId != null || this.oId.length > 0) {
241			sb.append("Oid=");
242			sb.append(this.ArrayToString(this.oId));
243		}
244
245		if (this.data != null ) {
246			sb.append(", data=");
247			sb.append(this.ArrayToString(this.data));
248		}
249
250		sb.append("]");
251
252		return sb.toString();
253	}
254	
255	private String ArrayToString(byte[] array) {
256		StringBuilder sb = new StringBuilder();
257		int i1 = 0;
258		for (byte b : array) {
259			if (i1 == 0)
260				i1 = 1;
261			else
262				sb.append(", ");
263			sb.append(b);
264		}
265		return sb.toString();
266	}
267	
268	private String ArrayToString(long[] array) {
269		StringBuilder sb = new StringBuilder();
270		int i1 = 0;
271		for (long b : array) {
272			if (i1 == 0)
273				i1 = 1;
274			else
275				sb.append(", ");
276			sb.append(b);
277		}
278		return sb.toString();
279	}
280
281	@Override
282	public int hashCode() {
283		final int prime = 31;
284		int result = 1;
285		result = prime * result + Arrays.hashCode(data);
286		result = prime * result + Arrays.hashCode(oId);
287		return result;
288	}
289
290	@Override
291	public boolean equals(Object obj) {
292		if (this == obj)
293			return true;
294		if (obj == null)
295			return false;
296		if (getClass() != obj.getClass())
297			return false;
298		MAPPrivateExtensionImpl other = (MAPPrivateExtensionImpl) obj;
299		if (!Arrays.equals(data, other.data))
300			return false;
301		if (!Arrays.equals(oId, other.oId))
302			return false;
303		return true;
304	}
305
306	
307	// ...............................
308	
309
310
311}