PageRenderTime 26ms CodeModel.GetById 11ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/protocols/ss7/inap/inap-impl/src/main/java/org/mobicents/protocols/ss7/inap/isup/CallingPartysCategoryInapImpl.java

http://mobicents.googlecode.com/
Java | 201 lines | 142 code | 33 blank | 26 comment | 13 complexity | 00258f30bbbe75e28394d9b849bea111 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.inap.isup;
 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.inap.api.INAPException;
 32import org.mobicents.protocols.ss7.inap.api.INAPParsingComponentException;
 33import org.mobicents.protocols.ss7.inap.api.INAPParsingComponentExceptionReason;
 34import org.mobicents.protocols.ss7.inap.api.isup.CallingPartysCategoryInap;
 35import org.mobicents.protocols.ss7.inap.primitives.INAPAsnPrimitive;
 36import org.mobicents.protocols.ss7.isup.ParameterException;
 37import org.mobicents.protocols.ss7.isup.impl.message.parameter.CallingPartyCategoryImpl;
 38import org.mobicents.protocols.ss7.isup.message.parameter.CallingPartyCategory;
 39
 40/**
 41* 
 42* @author sergey vetyutnev
 43* 
 44*/
 45public class CallingPartysCategoryInapImpl implements CallingPartysCategoryInap, INAPAsnPrimitive {
 46
 47	public static final String _PrimitiveName = "CallingPartysCategoryInap";
 48
 49	private byte[] data;
 50
 51	public CallingPartysCategoryInapImpl() {
 52	}
 53
 54	public CallingPartysCategoryInapImpl(byte[] data) {
 55		this.data = data;
 56	}
 57
 58	public CallingPartysCategoryInapImpl(CallingPartyCategory callingPartyCategory) throws INAPException {
 59		if (callingPartyCategory == null)
 60			throw new INAPException("The callingPartyCategory parameter must not be null");
 61		try {
 62			this.data = ((CallingPartyCategoryImpl) callingPartyCategory).encode();
 63		} catch (ParameterException e) {
 64			throw new INAPException("ParameterException when encoding callingPartyCategory: " + e.getMessage(), e);
 65		}
 66	}
 67
 68	@Override
 69	public byte[] getData() {
 70		return data;
 71	}
 72
 73	@Override
 74	public CallingPartyCategory getCallingPartyCategory() throws INAPException {
 75		if (this.data == null)
 76			throw new INAPException("The data has not been filled");
 77
 78		try {
 79			CallingPartyCategoryImpl cpc = new CallingPartyCategoryImpl();
 80			cpc.decode(this.data);
 81			return cpc;
 82		} catch (ParameterException e) {
 83			throw new INAPException("ParameterException when decoding CallingPartyCategory: " + e.getMessage(), e);
 84		}
 85	}
 86
 87	@Override
 88	public int getTag() throws INAPException {
 89		return Tag.STRING_OCTET;
 90	}
 91
 92	@Override
 93	public int getTagClass() {
 94		return Tag.CLASS_UNIVERSAL;
 95	}
 96
 97	@Override
 98	public boolean getIsPrimitive() {
 99		return true;
100	}
101
102	@Override
103	public void decodeAll(AsnInputStream ansIS) throws INAPParsingComponentException {
104
105		try {
106			int length = ansIS.readLength();
107			this._decode(ansIS, length);
108		} catch (IOException e) {
109			throw new INAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
110					INAPParsingComponentExceptionReason.MistypedParameter);
111		} catch (AsnException e) {
112			throw new INAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
113					INAPParsingComponentExceptionReason.MistypedParameter);
114		}
115	}
116
117	@Override
118	public void decodeData(AsnInputStream ansIS, int length) throws INAPParsingComponentException {
119
120		try {
121			this._decode(ansIS, length);
122		} catch (IOException e) {
123			throw new INAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
124					INAPParsingComponentExceptionReason.MistypedParameter);
125		} catch (AsnException e) {
126			throw new INAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
127					INAPParsingComponentExceptionReason.MistypedParameter);
128		}
129	}
130
131	private void _decode(AsnInputStream ansIS, int length) throws INAPParsingComponentException, IOException, AsnException {
132
133		this.data = ansIS.readOctetStringData(length);
134		if (this.data.length < 1 || this.data.length > 1)
135			throw new INAPParsingComponentException(
136					"Error while decoding " + _PrimitiveName + ": data must be from 1 to 1 bytes length, found: " + this.data.length,
137					INAPParsingComponentExceptionReason.MistypedParameter);
138	}
139
140	@Override
141	public void encodeAll(AsnOutputStream asnOs) throws INAPException {
142		this.encodeAll(asnOs, this.getTagClass(), this.getTag());
143	}
144
145	@Override
146	public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws INAPException {
147
148		try {
149			asnOs.writeTag(tagClass, true, tag);
150			int pos = asnOs.StartContentDefiniteLength();
151			this.encodeData(asnOs);
152			asnOs.FinalizeContent(pos);
153		} catch (AsnException e) {
154			throw new INAPException("AsnException when encoding " + _PrimitiveName + ": " + e.getMessage(), e);
155		}
156	}
157
158	@Override
159	public void encodeData(AsnOutputStream asnOs) throws INAPException {
160
161		if (this.data == null)
162			throw new INAPException("data field must not be null");
163		if (this.data.length < 1 && this.data.length > 1)
164			throw new INAPException("data field length must be from 1 to 1");
165
166		asnOs.writeOctetStringData(data);
167	}
168
169	@Override
170	public String toString() {
171		StringBuilder sb = new StringBuilder();
172		sb.append(_PrimitiveName);
173		sb.append(" [");
174
175		if (this.data != null) {
176			sb.append("data=[");
177			sb.append(printDataArr(this.data));
178			sb.append("]");
179			try {
180				CallingPartyCategory cpc = this.getCallingPartyCategory();
181				sb.append(", ");
182				sb.append(cpc.toString());
183			} catch (INAPException e) {
184			}
185		}
186
187		sb.append("]");
188
189		return sb.toString();
190	}
191
192	private String printDataArr(byte[] arr) {
193		StringBuilder sb = new StringBuilder();
194		for (int b : arr) {
195			sb.append(b);
196			sb.append(", ");
197		}
198
199		return sb.toString();
200	}
201}