/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/service/lsm/LCSClientNameImpl.java
Java | 328 lines | 177 code | 37 blank | 114 comment | 39 complexity | 231a3b26ba3436fdf065edcc7432459f 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.lsm; 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.primitives.USSDString; 35import org.mobicents.protocols.ss7.map.api.service.lsm.LCSClientName; 36import org.mobicents.protocols.ss7.map.api.service.lsm.LCSFormatIndicator; 37import org.mobicents.protocols.ss7.map.primitives.MAPAsnPrimitive; 38import org.mobicents.protocols.ss7.map.primitives.USSDStringImpl; 39 40/** 41 * @author amit bhayani 42 * 43 */ 44public class LCSClientNameImpl implements LCSClientName, MAPAsnPrimitive { 45 46 private static final int _TAG_DATA_CODING_SCHEME = 0; 47 private static final int _TAG_NAME_STRING = 2; 48 private static final int _TAG_LCS_FORMAT_INDICATOR = 3; 49 50 private byte dataCodingScheme = 0; 51 private USSDString nameString; 52 private LCSFormatIndicator lcsFormatIndicator; 53 54 /** 55 * 56 */ 57 public LCSClientNameImpl() { 58 super(); 59 } 60 61 /** 62 * @param dataCodingScheme 63 * @param nameString 64 * @param lcsFormatIndicator 65 */ 66 public LCSClientNameImpl(byte dataCodingScheme, USSDString nameString, LCSFormatIndicator lcsFormatIndicator) { 67 super(); 68 this.dataCodingScheme = dataCodingScheme; 69 this.nameString = nameString; 70 this.lcsFormatIndicator = lcsFormatIndicator; 71 } 72 73 /* 74 * (non-Javadoc) 75 * 76 * @see org.mobicents.protocols.ss7.map.api.service.lsm.LCSClientName# 77 * getDataCodingScheme() 78 */ 79 @Override 80 public byte getDataCodingScheme() { 81 return this.dataCodingScheme; 82 } 83 84 /* 85 * (non-Javadoc) 86 * 87 * @see 88 * org.mobicents.protocols.ss7.map.api.service.lsm.LCSClientName#getNameString 89 * () 90 */ 91 @Override 92 public USSDString getNameString() { 93 return this.nameString; 94 } 95 96 /* 97 * (non-Javadoc) 98 * 99 * @see org.mobicents.protocols.ss7.map.api.service.lsm.LCSClientName# 100 * getLCSFormatIndicator() 101 */ 102 @Override 103 public LCSFormatIndicator getLCSFormatIndicator() { 104 return this.lcsFormatIndicator; 105 } 106 107 /* 108 * (non-Javadoc) 109 * 110 * @see 111 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTag() 112 */ 113 @Override 114 public int getTag() throws MAPException { 115 return Tag.SEQUENCE; 116 } 117 118 /* 119 * (non-Javadoc) 120 * 121 * @see 122 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTagClass 123 * () 124 */ 125 @Override 126 public int getTagClass() { 127 return Tag.CLASS_UNIVERSAL; 128 } 129 130 /* 131 * (non-Javadoc) 132 * 133 * @see 134 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getIsPrimitive 135 * () 136 */ 137 @Override 138 public boolean getIsPrimitive() { 139 return false; 140 } 141 142 /* 143 * (non-Javadoc) 144 * 145 * @see 146 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeAll 147 * (org.mobicents.protocols.asn.AsnInputStream) 148 */ 149 @Override 150 public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException { 151 try { 152 int length = ansIS.readLength(); 153 this._decode(ansIS, length); 154 } catch (IOException e) { 155 throw new MAPParsingComponentException("IOException when decoding LCSClientName: " + e.getMessage(), e, 156 MAPParsingComponentExceptionReason.MistypedParameter); 157 } catch (AsnException e) { 158 throw new MAPParsingComponentException("AsnException when decoding LCSClientName: " + e.getMessage(), e, 159 MAPParsingComponentExceptionReason.MistypedParameter); 160 } 161 } 162 163 /* 164 * (non-Javadoc) 165 * 166 * @see 167 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeData 168 * (org.mobicents.protocols.asn.AsnInputStream, int) 169 */ 170 @Override 171 public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException { 172 try { 173 this._decode(ansIS, length); 174 } catch (IOException e) { 175 throw new MAPParsingComponentException("IOException when decoding LCSClientName: " + e.getMessage(), e, 176 MAPParsingComponentExceptionReason.MistypedParameter); 177 } catch (AsnException e) { 178 throw new MAPParsingComponentException("AsnException when decoding LCSClientName: " + e.getMessage(), e, 179 MAPParsingComponentExceptionReason.MistypedParameter); 180 } 181 } 182 183 private void _decode(AsnInputStream asnIS, int length) throws MAPParsingComponentException, IOException, AsnException { 184 185 AsnInputStream ais = asnIS.readSequenceStreamData(length); 186 187 int tag = ais.readTag(); 188 189 // Decode mandatory dataCodingScheme [0] USSD-DataCodingScheme, 190 if (ais.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC || !ais.isTagPrimitive() || tag != _TAG_DATA_CODING_SCHEME) { 191 throw new MAPParsingComponentException( 192 "Error while decoding LCSClientName: Parameter 0[dataCodingScheme [0] USSD-DataCodingScheme] bad tag class, tag or not primitive", 193 MAPParsingComponentExceptionReason.MistypedParameter); 194 } 195 196 int length1 = ais.readLength(); 197 this.dataCodingScheme = ais.readOctetStringData(length1)[0]; 198 199 tag = ais.readTag(); 200 201 // Decode mandatory nameString [2] NameString, 202 if (ais.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC || !ais.isTagPrimitive() || tag != _TAG_NAME_STRING) { 203 throw new MAPParsingComponentException( 204 "Error while decoding LCSClientName: Parameter 1[nameString [2] NameString,] bad tag class, tag or not primitive", 205 MAPParsingComponentExceptionReason.MistypedParameter); 206 } 207 208 this.nameString = new USSDStringImpl(); 209 ((USSDStringImpl)this.nameString).decodeAll(ais); 210 211 while (true) { 212 if (ais.available() == 0) 213 break; 214 215 tag = ais.readTag(); 216 switch (tag) { 217 case _TAG_LCS_FORMAT_INDICATOR: 218 // Decode lcs-FormatIndicator [3] LCS-FormatIndicator OPTIONAL 219 if (ais.getTagClass() != Tag.CLASS_CONTEXT_SPECIFIC || !ais.isTagPrimitive()) { 220 throw new MAPParsingComponentException( 221 "Error while decoding LCSClientName: Parameter 2[lcs-FormatIndicator [3] LCS-FormatIndicator OPTIONAL] bad tag class, tag or not primitive", 222 MAPParsingComponentExceptionReason.MistypedParameter); 223 } 224 length1 = ais.readLength(); 225 this.lcsFormatIndicator = LCSFormatIndicator.getLCSFormatIndicator((int) ais.readIntegerData(length1)); 226 break; 227 228 default: 229 // throw new 230 // MAPParsingComponentException("Decoding LCSClientExternalID failed. Expected externalAddress [0] or extensionContainer [1] but found " 231 // + p.getTag(), 232 // MAPParsingComponentExceptionReason.MistypedParameter); 233 ais.advanceElement(); 234 } 235 } 236 } 237 238 /* 239 * (non-Javadoc) 240 * 241 * @see 242 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll 243 * (org.mobicents.protocols.asn.AsnOutputStream) 244 */ 245 @Override 246 public void encodeAll(AsnOutputStream asnOs) throws MAPException { 247 this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.SEQUENCE); 248 } 249 250 /* 251 * (non-Javadoc) 252 * 253 * @see 254 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll 255 * (org.mobicents.protocols.asn.AsnOutputStream, int, int) 256 */ 257 @Override 258 public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException { 259 try { 260 asnOs.writeTag(tagClass, false, tag); 261 int pos = asnOs.StartContentDefiniteLength(); 262 this.encodeData(asnOs); 263 asnOs.FinalizeContent(pos); 264 } catch (AsnException e) { 265 throw new MAPException("AsnException when encoding LCSClientName", e); 266 } 267 } 268 269 /* 270 * (non-Javadoc) 271 * 272 * @see 273 * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeData 274 * (org.mobicents.protocols.asn.AsnOutputStream) 275 */ 276 @Override 277 public void encodeData(AsnOutputStream asnOs) throws MAPException { 278 279 if (this.nameString == null) 280 throw new MAPException("nameString must not be null"); 281 282 try { 283 asnOs.writeOctetString(Tag.CLASS_CONTEXT_SPECIFIC, _TAG_DATA_CODING_SCHEME, new byte[] { this.dataCodingScheme }); 284 285 ((USSDStringImpl)this.nameString).encodeAll(asnOs, Tag.CLASS_CONTEXT_SPECIFIC, _TAG_NAME_STRING); 286 287 if (this.lcsFormatIndicator != null) { 288 asnOs.writeInteger(Tag.CLASS_CONTEXT_SPECIFIC, _TAG_LCS_FORMAT_INDICATOR, this.lcsFormatIndicator.getIndicator()); 289 } 290 } catch (IOException e) { 291 throw new MAPException("IOException when encoding LCSClientName", e); 292 } catch (AsnException e) { 293 throw new MAPException("AsnException when encoding LCSClientName", e); 294 } 295 } 296 297 @Override 298 public int hashCode() { 299 final int prime = 31; 300 int result = 1; 301 result = prime * result + dataCodingScheme; 302 result = prime * result + ((lcsFormatIndicator == null) ? 0 : lcsFormatIndicator.hashCode()); 303 result = prime * result + ((nameString == null) ? 0 : nameString.hashCode()); 304 return result; 305 } 306 307 @Override 308 public boolean equals(Object obj) { 309 if (this == obj) 310 return true; 311 if (obj == null) 312 return false; 313 if (getClass() != obj.getClass()) 314 return false; 315 LCSClientNameImpl other = (LCSClientNameImpl) obj; 316 if (dataCodingScheme != other.dataCodingScheme) 317 return false; 318 if (lcsFormatIndicator != other.lcsFormatIndicator) 319 return false; 320 if (nameString == null) { 321 if (other.nameString != null) 322 return false; 323 } else if (!nameString.equals(other.nameString)) 324 return false; 325 return true; 326 } 327 328}