/protocols/ss7/inap/inap-impl/src/main/java/org/mobicents/protocols/ss7/inap/isup/HighLayerCompatibilityInapImpl.java
Java | 203 lines | 142 code | 34 blank | 27 comment | 13 complexity | 9f763427f4016a0e78e9dcedd1c972f9 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.isup; 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.isup.HighLayerCompatibilityInap; 35import org.mobicents.protocols.ss7.inap.primitives.INAPAsnPrimitive; 36import org.mobicents.protocols.ss7.isup.ParameterException; 37import org.mobicents.protocols.ss7.isup.impl.message.parameter.UserTeleserviceInformationImpl; 38import org.mobicents.protocols.ss7.isup.message.parameter.UserTeleserviceInformation; 39 40 41/** 42* 43* 44* @author sergey vetyutnev 45* 46*/ 47public class HighLayerCompatibilityInapImpl implements HighLayerCompatibilityInap, INAPAsnPrimitive { 48 49 public static final String _PrimitiveName = "HighLayerCompatibilityInap"; 50 51 private byte[] data; 52 53 public HighLayerCompatibilityInapImpl() { 54 } 55 56 public HighLayerCompatibilityInapImpl(byte[] data) { 57 this.data = data; 58 } 59 60 public HighLayerCompatibilityInapImpl(UserTeleserviceInformation highLayerCompatibility) throws INAPException { 61 if (highLayerCompatibility == null) 62 throw new INAPException("The callingPartyCategory parameter must not be null"); 63 try { 64 this.data = ((UserTeleserviceInformationImpl) highLayerCompatibility).encode(); 65 } catch (ParameterException e) { 66 throw new INAPException("ParameterException when encoding highLayerCompatibility: " + e.getMessage(), e); 67 } 68 } 69 70 @Override 71 public byte[] getData() { 72 return data; 73 } 74 75 @Override 76 public UserTeleserviceInformation getHighLayerCompatibility() throws INAPException { 77 if (this.data == null) 78 throw new INAPException("The data has not been filled"); 79 80 try { 81 UserTeleserviceInformationImpl cpc = new UserTeleserviceInformationImpl(); 82 cpc.decode(this.data); 83 return cpc; 84 } catch (ParameterException e) { 85 throw new INAPException("ParameterException when decoding HighLayerCompatibility: " + e.getMessage(), e); 86 } 87 } 88 89 @Override 90 public int getTag() throws INAPException { 91 return Tag.STRING_OCTET; 92 } 93 94 @Override 95 public int getTagClass() { 96 return Tag.CLASS_UNIVERSAL; 97 } 98 99 @Override 100 public boolean getIsPrimitive() { 101 return true; 102 } 103 104 @Override 105 public void decodeAll(AsnInputStream ansIS) throws INAPParsingComponentException { 106 107 try { 108 int length = ansIS.readLength(); 109 this._decode(ansIS, length); 110 } catch (IOException e) { 111 throw new INAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + e.getMessage(), e, 112 INAPParsingComponentExceptionReason.MistypedParameter); 113 } catch (AsnException e) { 114 throw new INAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + e.getMessage(), e, 115 INAPParsingComponentExceptionReason.MistypedParameter); 116 } 117 } 118 119 @Override 120 public void decodeData(AsnInputStream ansIS, int length) throws INAPParsingComponentException { 121 122 try { 123 this._decode(ansIS, length); 124 } catch (IOException e) { 125 throw new INAPParsingComponentException("IOException when decoding " + _PrimitiveName + ": " + e.getMessage(), e, 126 INAPParsingComponentExceptionReason.MistypedParameter); 127 } catch (AsnException e) { 128 throw new INAPParsingComponentException("AsnException when decoding " + _PrimitiveName + ": " + e.getMessage(), e, 129 INAPParsingComponentExceptionReason.MistypedParameter); 130 } 131 } 132 133 private void _decode(AsnInputStream ansIS, int length) throws INAPParsingComponentException, IOException, AsnException { 134 135 this.data = ansIS.readOctetStringData(length); 136 if (this.data.length < 2 || this.data.length > 2) 137 throw new INAPParsingComponentException( 138 "Error while decoding " + _PrimitiveName + ": data must be from 2 to 2 bytes length, found: " + this.data.length, 139 INAPParsingComponentExceptionReason.MistypedParameter); 140 } 141 142 @Override 143 public void encodeAll(AsnOutputStream asnOs) throws INAPException { 144 this.encodeAll(asnOs, this.getTagClass(), this.getTag()); 145 } 146 147 @Override 148 public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws INAPException { 149 150 try { 151 asnOs.writeTag(tagClass, true, tag); 152 int pos = asnOs.StartContentDefiniteLength(); 153 this.encodeData(asnOs); 154 asnOs.FinalizeContent(pos); 155 } catch (AsnException e) { 156 throw new INAPException("AsnException when encoding " + _PrimitiveName + ": " + e.getMessage(), e); 157 } 158 } 159 160 @Override 161 public void encodeData(AsnOutputStream asnOs) throws INAPException { 162 163 if (this.data == null) 164 throw new INAPException("data field must not be null"); 165 if (this.data.length < 2 && this.data.length > 2) 166 throw new INAPException("data field length must be from 2 to 2"); 167 168 asnOs.writeOctetStringData(data); 169 } 170 171 @Override 172 public String toString() { 173 StringBuilder sb = new StringBuilder(); 174 sb.append(_PrimitiveName); 175 sb.append(" ["); 176 177 if (this.data != null) { 178 sb.append("data=["); 179 sb.append(printDataArr(this.data)); 180 sb.append("]"); 181 try { 182 UserTeleserviceInformation cpc = this.getHighLayerCompatibility(); 183 sb.append(", "); 184 sb.append(cpc.toString()); 185 } catch (INAPException e) { 186 } 187 } 188 189 sb.append("]"); 190 191 return sb.toString(); 192 } 193 194 private String printDataArr(byte[] arr) { 195 StringBuilder sb = new StringBuilder(); 196 for (int b : arr) { 197 sb.append(b); 198 sb.append(", "); 199 } 200 201 return sb.toString(); 202 } 203}