PageRenderTime 110ms CodeModel.GetById 99ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/service/subscriberManagement/ExtBasicServiceCodeImpl.java

http://mobicents.googlecode.com/
Java | 202 lines | 139 code | 37 blank | 26 comment | 21 complexity | bf7ed33767b9f275f13c5693a290992b 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.subscriberManagement;
 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.subscriberManagement.ExtBasicServiceCode;
 35import org.mobicents.protocols.ss7.map.api.service.subscriberManagement.ExtBearerServiceCode;
 36import org.mobicents.protocols.ss7.map.api.service.subscriberManagement.ExtTeleserviceCode;
 37import org.mobicents.protocols.ss7.map.primitives.MAPAsnPrimitive;
 38
 39/**
 40 * 
 41 * @author sergey vetyutnev
 42 * 
 43 */
 44public class ExtBasicServiceCodeImpl implements ExtBasicServiceCode, MAPAsnPrimitive  {
 45
 46	public static final int _ID_ext_BearerService = 2;
 47	public static final int _ID_ext_Teleservice = 3;
 48
 49	public static final String _PrimitiveName = "ExtBasicServiceCode";
 50
 51	private ExtBearerServiceCode extBearerService;
 52	private ExtTeleserviceCode extTeleservice;	
 53
 54	public ExtBasicServiceCodeImpl() {
 55	}
 56
 57	public ExtBasicServiceCodeImpl(ExtBearerServiceCode extBearerService) {
 58		this.extBearerService = extBearerService;
 59	}
 60
 61	public ExtBasicServiceCodeImpl(ExtTeleserviceCode extTeleservice) {
 62		this.extTeleservice = extTeleservice;
 63	}
 64	
 65	@Override
 66	public ExtBearerServiceCode getExtBearerService() {
 67		return extBearerService;
 68	}
 69
 70	@Override
 71	public ExtTeleserviceCode getExtTeleservice() {
 72		return extTeleservice;
 73	}
 74
 75	
 76	
 77	@Override
 78	public int getTag() throws MAPException {
 79		if (extBearerService != null)
 80			return _ID_ext_BearerService;
 81		else
 82			return _ID_ext_Teleservice;
 83	}
 84
 85	@Override
 86	public int getTagClass() {
 87		return Tag.CLASS_CONTEXT_SPECIFIC;
 88	}
 89
 90	@Override
 91	public boolean getIsPrimitive() {
 92		return true;
 93	}
 94
 95	@Override
 96	public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
 97
 98		try {
 99			int length = ansIS.readLength();
100			this._decode(ansIS, length);
101		} catch (IOException e) {
102			throw new MAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
103					MAPParsingComponentExceptionReason.MistypedParameter);
104		} catch (AsnException e) {
105			throw new MAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
106					MAPParsingComponentExceptionReason.MistypedParameter);
107		}
108	}
109
110	@Override
111	public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
112
113		try {
114			this._decode(ansIS, length);
115		} catch (IOException e) {
116			throw new MAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
117					MAPParsingComponentExceptionReason.MistypedParameter);
118		} catch (AsnException e) {
119			throw new MAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + e.getMessage(), e,
120					MAPParsingComponentExceptionReason.MistypedParameter);
121		}
122	}
123
124	private void _decode(AsnInputStream ais, int length) throws MAPParsingComponentException, IOException, AsnException {
125
126		this.extBearerService = null;
127		this.extTeleservice = null;	
128
129		int tag = ais.getTag();
130
131		if (ais.getTagClass() == Tag.CLASS_CONTEXT_SPECIFIC) {
132			switch (tag) {
133			case _ID_ext_BearerService:
134				this.extBearerService = new ExtBearerServiceCodeImpl();
135				((ExtBearerServiceCodeImpl) this.extBearerService).decodeData(ais, length);
136				break;
137			case _ID_ext_Teleservice:
138				this.extTeleservice = new ExtTeleserviceCodeImpl();
139				((ExtTeleserviceCodeImpl) this.extTeleservice).decodeData(ais, length);
140				break;
141
142			default:
143				throw new MAPParsingComponentException("Error while decoding " + _PrimitiveName + ": bad choice tag",
144						MAPParsingComponentExceptionReason.MistypedParameter);
145			}
146		} else {
147			throw new MAPParsingComponentException("Error while decoding " + _PrimitiveName + ": bad choice tagClass",
148					MAPParsingComponentExceptionReason.MistypedParameter);
149		}
150	}
151
152	@Override
153	public void encodeAll(AsnOutputStream asnOs) throws MAPException {
154
155		this.encodeAll(asnOs, this.getTagClass(), this.getTag());
156	}
157
158	@Override
159	public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
160		
161		try {
162			asnOs.writeTag(tagClass, true, tag);
163			int pos = asnOs.StartContentDefiniteLength();
164			this.encodeData(asnOs);
165			asnOs.FinalizeContent(pos);
166		} catch (AsnException e) {
167			throw new MAPException("AsnException when encoding " + _PrimitiveName + ": " + e.getMessage(), e);
168		}
169	}
170
171	@Override
172	public void encodeData(AsnOutputStream asnOs) throws MAPException {
173
174		if (this.extBearerService == null && this.extTeleservice == null)
175			throw new MAPException("Both extBearerService and extTeleservice must not be null");
176		if (this.extBearerService != null && this.extTeleservice != null)
177			throw new MAPException("Both extBearerService and extTeleservice must not be not null");
178
179		if (this.extBearerService != null) {
180			((ExtBearerServiceCodeImpl) this.extBearerService).encodeData(asnOs);
181		} else {
182			((ExtTeleserviceCodeImpl) this.extTeleservice).encodeData(asnOs);
183		}
184	}
185
186	@Override
187	public String toString() {
188		StringBuilder sb = new StringBuilder();
189		sb.append("ExtBasicServiceCode [");
190
191		if (this.extBearerService != null) {
192			sb.append(this.extBearerService.toString());
193		}
194		if (this.extTeleservice != null) {
195			sb.append(this.extTeleservice.toString());
196		}
197
198		sb.append("]");
199
200		return sb.toString();
201	}
202}