PageRenderTime 33ms CodeModel.GetById 14ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/service/sms/AlertServiceCentreRequestIndicationImpl.java

http://mobicents.googlecode.com/
Java | 220 lines | 157 code | 37 blank | 26 comment | 26 complexity | 0830b1b48912975f85bdbba0dbd5bfcb 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.sms;
 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.MAPMessageType;
 33import org.mobicents.protocols.ss7.map.api.MAPOperationCode;
 34import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
 35import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
 36import org.mobicents.protocols.ss7.map.api.primitives.AddressString;
 37import org.mobicents.protocols.ss7.map.api.primitives.ISDNAddressString;
 38import org.mobicents.protocols.ss7.map.api.service.sms.AlertServiceCentreRequestIndication;
 39import org.mobicents.protocols.ss7.map.primitives.AddressStringImpl;
 40import org.mobicents.protocols.ss7.map.primitives.ISDNAddressStringImpl;
 41
 42/**
 43 * 
 44 * @author sergey vetyutnev
 45 * 
 46 */
 47public class AlertServiceCentreRequestIndicationImpl extends SmsMessageImpl implements AlertServiceCentreRequestIndication {
 48
 49	private ISDNAddressString msisdn;
 50	private AddressString serviceCentreAddress;
 51	private int operationCode;
 52	
 53	public AlertServiceCentreRequestIndicationImpl(int operationCode) {
 54		this.operationCode = operationCode;
 55	}
 56	
 57	public AlertServiceCentreRequestIndicationImpl(ISDNAddressString msisdn, AddressString serviceCentreAddress) {
 58		this.msisdn = msisdn;
 59		this.serviceCentreAddress = serviceCentreAddress;
 60	}
 61
 62	@Override
 63	public MAPMessageType getMessageType() {
 64		if (this.operationCode == MAPOperationCode.alertServiceCentre)
 65			return MAPMessageType.alertServiceCentre_Request;
 66		else
 67			return MAPMessageType.alertServiceCentreWithoutResult_Request;
 68	}
 69
 70	@Override
 71	public int getOperationCode() {
 72		return this.operationCode;
 73	}
 74
 75	@Override
 76	public ISDNAddressString getMsisdn() {
 77		return this.msisdn;
 78	}
 79
 80	@Override
 81	public AddressString getServiceCentreAddress() {
 82		return this.serviceCentreAddress;
 83	}
 84
 85	
 86	@Override
 87	public int getTag() throws MAPException {
 88		return Tag.SEQUENCE;
 89	}
 90
 91	@Override
 92	public int getTagClass() {
 93		return Tag.CLASS_UNIVERSAL;
 94	}
 95
 96	@Override
 97	public boolean getIsPrimitive() {
 98		return false;
 99	}
100
101	
102	@Override
103	public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
104
105		try {
106			int length = ansIS.readLength();
107			this._decode(ansIS, length);
108		} catch (IOException e) {
109			throw new MAPParsingComponentException("IOException when decoding AlertServiceCentreRequest: " + e.getMessage(), e,
110					MAPParsingComponentExceptionReason.MistypedParameter);
111		} catch (AsnException e) {
112			throw new MAPParsingComponentException("AsnException when decoding AlertServiceCentreRequest: " + e.getMessage(), e,
113					MAPParsingComponentExceptionReason.MistypedParameter);
114		}
115	}
116
117	@Override
118	public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
119
120		try {
121			this._decode(ansIS, length);
122		} catch (IOException e) {
123			throw new MAPParsingComponentException("IOException when decoding AlertServiceCentreRequest: " + e.getMessage(), e,
124					MAPParsingComponentExceptionReason.MistypedParameter);
125		} catch (AsnException e) {
126			throw new MAPParsingComponentException("AsnException when decoding AlertServiceCentreRequest: " + e.getMessage(), e,
127					MAPParsingComponentExceptionReason.MistypedParameter);
128		}
129	}
130
131	private void _decode(AsnInputStream ansIS, int length) throws MAPParsingComponentException, IOException, AsnException {
132		this.msisdn = null;
133		this.serviceCentreAddress = null;
134		
135		AsnInputStream ais = ansIS.readSequenceStreamData(length);
136		int num = 0;
137		while( true ) {
138			if (ais.available() == 0)
139				break;
140			
141			int tag = ais.readTag();
142			switch(num) {
143			case 0:
144				if (ais.getTagClass() != Tag.CLASS_UNIVERSAL || !ais.isTagPrimitive() || tag != Tag.STRING_OCTET)
145					throw new MAPParsingComponentException(
146							"Error while decoding AlertServiceCentreRequest.msisdn: bad tag or tag class or is not primitive: TagClass=" + ais.getTagClass()
147									+ ", tag=" + tag, MAPParsingComponentExceptionReason.MistypedParameter);
148				this.msisdn = new ISDNAddressStringImpl();
149				((ISDNAddressStringImpl)this.msisdn).decodeAll(ais);
150				break;
151			case 1:
152				if (ais.getTagClass() != Tag.CLASS_UNIVERSAL || !ais.isTagPrimitive() || tag != Tag.STRING_OCTET)
153					throw new MAPParsingComponentException(
154							"Error while decoding AlertServiceCentreRequest.serviceCentreAddress: bad tag or tag class or is not primitive: TagClass=" + ais.getTagClass()
155									+ ", tag=" + tag, MAPParsingComponentExceptionReason.MistypedParameter);
156				this.serviceCentreAddress = new ISDNAddressStringImpl();
157				((AddressStringImpl)this.serviceCentreAddress).decodeAll(ais);
158				break;
159			default:
160				ais.advanceElement();
161				break;
162			}
163			
164			num++;
165		}
166		
167		if (this.msisdn == null || this.serviceCentreAddress == null)
168			throw new MAPParsingComponentException("Error while decoding AlertServiceCentreRequest: 2 parameters are mandatory, found " + num,
169					MAPParsingComponentExceptionReason.MistypedParameter);
170	}
171	
172	@Override
173	public void encodeAll(AsnOutputStream asnOs) throws MAPException {
174
175		this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.SEQUENCE);
176	}
177
178	@Override
179	public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
180		
181		try {
182			asnOs.writeTag(tagClass, false, tag);
183			int pos = asnOs.StartContentDefiniteLength();
184			this.encodeData(asnOs);
185			asnOs.FinalizeContent(pos);
186		} catch (AsnException e) {
187			throw new MAPException("AsnException when encoding AlertServiceCentreRequest: " + e.getMessage(), e);
188		}
189	}
190
191	@Override
192	public void encodeData(AsnOutputStream asnOs) throws MAPException {
193		
194		if (this.msisdn == null || this.serviceCentreAddress == null)
195			throw new MAPException("Error when encoding AlertServiceCentreRequest: msisdn or serviceCentreAddress must not be empty");
196		
197		((ISDNAddressStringImpl)this.msisdn).encodeAll(asnOs);
198		((AddressStringImpl)this.serviceCentreAddress).encodeAll(asnOs);
199	}
200	
201	@Override
202	public String toString() {
203		StringBuilder sb = new StringBuilder();
204		sb.append("AlertServiceCentreRequest [");
205
206		if (this.msisdn != null) {
207			sb.append("msisdn=");
208			sb.append(this.msisdn.toString());
209		}
210		if (this.serviceCentreAddress != null) {
211			sb.append(", serviceCentreAddress=");
212			sb.append(this.serviceCentreAddress.toString());
213		}
214
215		sb.append("]");
216
217		return sb.toString();
218	}
219}
220