PageRenderTime 39ms CodeModel.GetById 1ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/errors/MAPErrorMessageExtensionContainerImpl.java

http://mobicents.googlecode.com/
Java | 203 lines | 134 code | 41 blank | 28 comment | 14 complexity | 4aa4d35871f5669344d02288d8b3fe07 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.errors;
 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.errors.MAPErrorMessageExtensionContainer;
 35import org.mobicents.protocols.ss7.map.api.primitives.MAPExtensionContainer;
 36import org.mobicents.protocols.ss7.map.primitives.MAPExtensionContainerImpl;
 37
 38/**
 39 * The MAP ReturnError message with the single parameter - ExtensionContainer
 40 *
 41 * 
 42 * @author sergey vetyutnev
 43 * 
 44 */
 45public class MAPErrorMessageExtensionContainerImpl extends MAPErrorMessageImpl implements MAPErrorMessageExtensionContainer {
 46
 47	private MAPExtensionContainer extensionContainer; 
 48
 49	
 50	protected MAPErrorMessageExtensionContainerImpl(Long errorCode) {
 51		super(errorCode);
 52	}
 53	
 54	public MAPErrorMessageExtensionContainerImpl(Long errorCode, MAPExtensionContainer extensionContainer) {
 55		super(errorCode);
 56		
 57		this.extensionContainer = extensionContainer;
 58	}
 59
 60	
 61	@Override
 62	public MAPExtensionContainer getExtensionContainer() {
 63		return this.extensionContainer;
 64	}
 65
 66	@Override
 67	public void setExtensionContainer(MAPExtensionContainer extensionContainer) {
 68		this.extensionContainer = extensionContainer;
 69	}
 70	
 71	@Override
 72	public boolean isEmExtensionContainer() {
 73		return true;
 74	}
 75
 76	@Override
 77	public MAPErrorMessageExtensionContainer getEmExtensionContainer() {
 78		return this;
 79	}
 80
 81
 82	@Override
 83	public int getTag() throws MAPException {
 84		return Tag.SEQUENCE;
 85	}
 86
 87	@Override
 88	public int getTagClass() {
 89		return Tag.CLASS_UNIVERSAL;
 90	}
 91
 92	@Override
 93	public boolean getIsPrimitive() {
 94		return false;
 95	}
 96
 97	@Override
 98	public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
 99
100		try {
101			int length = ansIS.readLength();
102			this._decode(ansIS, length);
103		} catch (IOException e) {
104			throw new MAPParsingComponentException("IOException when decoding MAPErrorMessageExtensionContainer: " + e.getMessage(), e,
105					MAPParsingComponentExceptionReason.MistypedParameter);
106		} catch (AsnException e) {
107			throw new MAPParsingComponentException("AsnException when decoding MAPErrorMessageExtensionContainer: " + e.getMessage(), e,
108					MAPParsingComponentExceptionReason.MistypedParameter);
109		}
110	}
111
112	@Override
113	public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
114
115		try {
116			this._decode(ansIS, length);
117		} catch (IOException e) {
118			throw new MAPParsingComponentException("IOException when decoding MAPErrorMessageExtensionContainer: " + e.getMessage(), e,
119					MAPParsingComponentExceptionReason.MistypedParameter);
120		} catch (AsnException e) {
121			throw new MAPParsingComponentException("AsnException when decoding MAPErrorMessageExtensionContainer: " + e.getMessage(), e,
122					MAPParsingComponentExceptionReason.MistypedParameter);
123		}
124	}
125
126	private void _decode(AsnInputStream ansIS, int length) throws MAPParsingComponentException, IOException, AsnException {
127		
128		this.extensionContainer = null;
129		
130		if (ansIS.getTagClass() != Tag.CLASS_UNIVERSAL || ansIS.getTag() != Tag.SEQUENCE || ansIS.isTagPrimitive())
131			throw new MAPParsingComponentException("Error decoding MAPErrorMessageExtensionContainer: bad tag class or tag or parameter is primitive",
132					MAPParsingComponentExceptionReason.MistypedParameter);
133		
134		AsnInputStream ais = ansIS.readSequenceStreamData(length);
135		
136		while (true) {
137			if (ais.available() == 0)
138				break;
139
140			int tag = ais.readTag();
141
142			switch (ais.getTagClass()) {
143			case Tag.CLASS_UNIVERSAL:
144				switch (tag) {
145				case Tag.SEQUENCE:
146					this.extensionContainer = new MAPExtensionContainerImpl();
147					((MAPExtensionContainerImpl)this.extensionContainer).decodeAll(ais);
148					break;
149
150				default:
151					ais.advanceElement();
152					break;
153				}
154				break;
155
156			default:
157				ais.advanceElement();
158				break;
159			}
160		}
161	}
162
163	@Override
164	public void encodeAll(AsnOutputStream asnOs) throws MAPException {
165
166		this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.SEQUENCE);
167	}
168
169	@Override
170	public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
171		
172		try {
173			asnOs.writeTag(tagClass, false, tag);
174			int pos = asnOs.StartContentDefiniteLength();
175			this.encodeData(asnOs);
176			asnOs.FinalizeContent(pos);
177		} catch (AsnException e) {
178			throw new MAPException("AsnException when encoding MAPErrorMessageExtensionContainer: " + e.getMessage(), e);
179		}
180	}
181
182	@Override
183	public void encodeData(AsnOutputStream aos) throws MAPException {
184		
185		if (this.extensionContainer == null)
186			return;
187
188		((MAPExtensionContainerImpl)this.extensionContainer).encodeAll(aos);
189	}
190
191	@Override
192	public String toString() {
193		StringBuilder sb = new StringBuilder();
194		
195		sb.append("MAPErrorMessageExtensionContainer [");
196		sb.append("ErrorCode=" + this.errorCode);
197		if (this.extensionContainer != null)
198			sb.append(", extensionContainer=" + this.extensionContainer.toString());
199		sb.append("]");
200		
201		return sb.toString();
202	}
203}