PageRenderTime 17ms CodeModel.GetById 2ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/inap/inap-impl/src/main/java/org/mobicents/protocols/ss7/inap/primitives/LegIDImpl.java

http://mobicents.googlecode.com/
Java | 205 lines | 147 code | 32 blank | 26 comment | 30 complexity | 443c55defd02f2fa68e228613b342ab2 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.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.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.primitives.LegID;
 35import org.mobicents.protocols.ss7.inap.api.primitives.LegType;
 36
 37/**
 38* 
 39* @author sergey vetyutnev
 40* 
 41*/
 42public class LegIDImpl implements LegID, INAPAsnPrimitive {
 43
 44	public static final int _ID_sendingSideID = 0;
 45	public static final int _ID_receivingSideID = 1;
 46
 47	public static final String _PrimitiveName = "LegID";
 48
 49	private LegType sendingSideID;
 50	private LegType receivingSideID;	
 51	
 52	
 53	public LegIDImpl() {
 54	}
 55	
 56	public LegIDImpl(boolean isSendingSideID, LegType legID) {
 57		if (isSendingSideID)
 58			this.sendingSideID = legID;
 59		else
 60			this.receivingSideID = legID;
 61	}
 62	
 63	
 64	@Override
 65	public LegType getSendingSideID() {
 66		return sendingSideID;
 67	}
 68
 69	@Override
 70	public LegType getReceivingSideID() {
 71		return receivingSideID;
 72	}
 73
 74	
 75	@Override
 76	public int getTag() throws INAPException {
 77		if (this.sendingSideID != null) {
 78			return _ID_sendingSideID;
 79		} else {
 80			return _ID_receivingSideID;
 81		}
 82	}
 83
 84	@Override
 85	public int getTagClass() {
 86		return Tag.CLASS_CONTEXT_SPECIFIC;
 87	}
 88
 89	@Override
 90	public boolean getIsPrimitive() {
 91		return true;
 92	}
 93
 94	@Override
 95	public void decodeAll(AsnInputStream ansIS) throws INAPParsingComponentException {
 96		try {
 97			int length = ansIS.readLength();
 98			this._decode(ansIS, length);
 99		} catch (IOException e) {
100			throw new INAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
101					INAPParsingComponentExceptionReason.MistypedParameter);
102		} catch (AsnException e) {
103			throw new INAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
104					INAPParsingComponentExceptionReason.MistypedParameter);
105		}
106	}
107
108	@Override
109	public void decodeData(AsnInputStream ansIS, int length) throws INAPParsingComponentException {
110		try {
111			this._decode(ansIS, length);
112		} catch (IOException e) {
113			throw new INAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
114					INAPParsingComponentExceptionReason.MistypedParameter);
115		} catch (AsnException e) {
116			throw new INAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
117					INAPParsingComponentExceptionReason.MistypedParameter);
118		}		
119	}
120
121	private void _decode(AsnInputStream asnIS, int length) throws INAPParsingComponentException, IOException, AsnException {
122		
123		if (asnIS.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC || !asnIS.isTagPrimitive())
124			throw new INAPParsingComponentException("Error while decoding " + _PrimitiveName + ": bad tag class or is not primitive: TagClass=" + asnIS.getTagClass(),
125					INAPParsingComponentExceptionReason.MistypedParameter);
126
127		byte[] buf;
128		switch (asnIS.getTag()) {
129		case _ID_sendingSideID:
130			buf = asnIS.readOctetStringData(length);
131			if (buf.length != 1)
132				throw new INAPParsingComponentException("Error while decoding " + _PrimitiveName + ": sendingSideID length must be 1 but it equals "
133						+ buf.length, INAPParsingComponentExceptionReason.MistypedParameter);
134			this.sendingSideID = LegType.getInstance(buf[0]);
135			if (this.sendingSideID == null)
136				throw new INAPParsingComponentException("Error while decoding " + _PrimitiveName + ": sendingSideID value must be 1 or 2 it equals "
137						+ buf[0], INAPParsingComponentExceptionReason.MistypedParameter);
138			break;
139		case _ID_receivingSideID:
140			buf = asnIS.readOctetStringData(length);
141			if (buf.length != 1)
142				throw new INAPParsingComponentException("Error while decoding " + _PrimitiveName + ": sendingSideID length must be 1 but it equals "
143						+ buf.length, INAPParsingComponentExceptionReason.MistypedParameter);
144			this.receivingSideID = LegType.getInstance(buf[0]);
145			if (this.receivingSideID == null)
146				throw new INAPParsingComponentException("Error while decoding " + _PrimitiveName + ": sendingSideID value must be 1 or 2 it equals "
147						+ buf[0], INAPParsingComponentExceptionReason.MistypedParameter);
148			break;
149		default:
150			throw new INAPParsingComponentException("Error while decoding " + _PrimitiveName + ": bad tag : tag=" + asnIS.getTag(),
151					INAPParsingComponentExceptionReason.MistypedParameter);
152		}
153	}
154
155	@Override
156	public void encodeAll(AsnOutputStream asnOs) throws INAPException {
157		
158		this.encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, this.getTag());
159	}
160
161	@Override
162	public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws INAPException {
163		
164		try {
165			asnOs.writeTag(tagClass, true, tag);
166			int pos = asnOs.StartContentDefiniteLength();
167			this.encodeData(asnOs);
168			asnOs.FinalizeContent(pos);
169		} catch (AsnException e) {
170			throw new INAPException("AsnException when encoding " + _PrimitiveName + ": " + e.getMessage(), e);
171		}
172	}
173
174	@Override
175	public void encodeData(AsnOutputStream asnOs) throws INAPException {
176
177		if (this.sendingSideID == null && this.receivingSideID == null || this.sendingSideID != null && this.receivingSideID != null)
178			throw new INAPException("Error while encoding the " + _PrimitiveName + ": one of sendingSideID or receivingSideID (not both) must not be empty");
179
180		byte[] buf = new byte[1];
181		if (this.sendingSideID != null)
182			buf[0] = (byte) sendingSideID.getCode();
183		else
184			buf[0] = (byte) receivingSideID.getCode();
185		asnOs.writeOctetStringData(buf);
186	}
187
188	@Override
189	public String toString() {
190
191		StringBuilder sb = new StringBuilder();
192		sb.append("LegID [");
193		if (this.sendingSideID != null) {
194			sb.append("sendingSideID=");
195			sb.append(sendingSideID);
196		}
197		if (this.receivingSideID != null) {
198			sb.append("receivingSideID=");
199			sb.append(receivingSideID);
200		}
201		sb.append("]");
202		
203		return sb.toString();
204	}
205}