PageRenderTime 28ms CodeModel.GetById 19ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/service/callhandling/CallReferenceNumberImpl.java

http://mobicents.googlecode.com/
Java | 161 lines | 102 code | 33 blank | 26 comment | 8 complexity | 5cffdcbb4a05432601d41438792d9629 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.service.callhandling;
 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.map.api.MAPException;
 32import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
 33import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
 34import org.mobicents.protocols.ss7.map.api.service.callhandling.CallReferenceNumber;
 35import org.mobicents.protocols.ss7.map.primitives.MAPAsnPrimitive;
 36
 37/**
 38 * 
 39 * @author sergey vetyutnev
 40 * 
 41 */
 42public class CallReferenceNumberImpl implements CallReferenceNumber, MAPAsnPrimitive {
 43
 44	public static final String _PrimitiveName = "CallReferenceNumber";
 45
 46	private byte[] data;
 47
 48	
 49	public CallReferenceNumberImpl(){
 50	}
 51	
 52	public CallReferenceNumberImpl(byte[] data) {
 53		this.data = data;
 54	}
 55
 56
 57	@Override
 58	public byte[] getData() {
 59		return this.data;
 60	}
 61
 62	
 63	@Override
 64	public int getTag() throws MAPException {
 65		return Tag.STRING_OCTET;
 66	}
 67
 68	@Override
 69	public int getTagClass() {
 70		return Tag.CLASS_UNIVERSAL;
 71	}
 72
 73	@Override
 74	public boolean getIsPrimitive() {
 75		return false;
 76	}
 77
 78	@Override
 79	public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
 80
 81		try {
 82			int length = ansIS.readLength();
 83			this._decode(ansIS, length);
 84		} catch (IOException e) {
 85			throw new MAPParsingComponentException("IOException when decoding CallReferenceNumber: " + e.getMessage(), e,
 86					MAPParsingComponentExceptionReason.MistypedParameter);
 87		} catch (AsnException e) {
 88			throw new MAPParsingComponentException("AsnException when decoding CallReferenceNumber: " + e.getMessage(), e,
 89					MAPParsingComponentExceptionReason.MistypedParameter);
 90		}
 91	}
 92
 93	@Override
 94	public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
 95
 96		try {
 97			this._decode(ansIS, length);
 98		} catch (IOException e) {
 99			throw new MAPParsingComponentException("IOException when decoding CallReferenceNumber: " + e.getMessage(), e,
100					MAPParsingComponentExceptionReason.MistypedParameter);
101		} catch (AsnException e) {
102			throw new MAPParsingComponentException("AsnException when decoding CallReferenceNumber: " + e.getMessage(), e,
103					MAPParsingComponentExceptionReason.MistypedParameter);
104		}
105	}
106
107	private void _decode(AsnInputStream ansIS, int length) throws MAPParsingComponentException, IOException, AsnException {
108		
109		this.data = ansIS.readOctetStringData(length);
110		
111		if (this.data.length < 1 || this.data.length > 8)
112			throw new MAPParsingComponentException("Error decoding CallReferenceNumber: length must be from 1 to 8, real length = " + length,
113					MAPParsingComponentExceptionReason.MistypedParameter);
114	}
115
116	@Override
117	public void encodeAll(AsnOutputStream asnOs) throws MAPException {
118
119		this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, this.getTag());
120	}
121
122	@Override
123	public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
124		
125		try {
126			asnOs.writeTag(tagClass, true, tag);
127			int pos = asnOs.StartContentDefiniteLength();
128			this.encodeData(asnOs);
129			asnOs.FinalizeContent(pos);
130		} catch (AsnException e) {
131			throw new MAPException("AsnException when encoding " + _PrimitiveName + ": " + e.getMessage(), e);
132		}
133	}
134
135	@Override
136	public void encodeData(AsnOutputStream asnOs) throws MAPException {
137
138		if (this.data == null || this.data.length == 0)
139			throw new MAPException("Error when encoding " + _PrimitiveName + ": data is empty");
140
141		asnOs.writeOctetStringData(data);
142	}
143
144	@Override
145	public String toString() {
146		return "CallReferenceNumber [Data= " + this.printDataArr() + "]";
147	}
148	
149	private String printDataArr() {
150		StringBuilder sb = new StringBuilder();
151		if( this.data!=null ) {
152			for( int b : this.data ) {
153				sb.append(b);
154				sb.append(" ");
155			}
156		}
157		
158		return sb.toString();
159	}
160}
161