PageRenderTime 41ms CodeModel.GetById 30ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/smpp/src/main/java/org/mobicents/protocols/smpp/version/AbstractSMPPVersion.java

http://mobicents.googlecode.com/
Java | 216 lines | 141 code | 32 blank | 43 comment | 51 complexity | 1c86619708593a56b263042891d2dc23 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.smpp.version;
 24
 25import org.mobicents.protocols.smpp.Address;
 26
 27
 28/**
 29 * Class representing an SMPP protocol version. Instances of this object are
 30 * used by the rest of the API to determine is an SMPP message is supported by a
 31 * certain version of the protocol.
 32 * @version $Id: AbstractSMPPVersion.java 452 2009-01-15 16:56:36Z orank $
 33 */
 34public abstract class AbstractSMPPVersion implements SMPPVersion {
 35    private static final long serialVersionUID = 2L;
 36    /**
 37     * Integer representing this version number. The SMPP specification states
 38     * integer values that represent protocol revisions. These values are used
 39     * mainly in the bind_* and bind response messages. Integer value 0x33
 40     * represents version 3.3 of the protocol, integer value 0x34 represents
 41     * version 3.4...it's assumed further major and minor revisions of the SMPP
 42     * specification will continue this numbering scheme.
 43     */
 44    private int versionID;
 45
 46    /**
 47     * Descriptive text for this protocol version. This value is used only to
 48     * return a representative string from toString.
 49     */
 50    private String versionString;
 51
 52    /**
 53     * Create a new SMPPVersion object.
 54     */
 55    protected AbstractSMPPVersion(int versionID, String versionString) {
 56        this.versionID = versionID;
 57        this.versionString = versionString;
 58    }
 59
 60    public int getVersionID() {
 61        return versionID;
 62    }
 63
 64    public boolean isOlderThan(SMPPVersion otherVersion) {
 65        return versionID < otherVersion.getVersionID();
 66    }
 67
 68    public boolean isNewerThan(SMPPVersion otherVersion) {
 69        return versionID > otherVersion.getVersionID();
 70    }
 71
 72    public boolean equals(Object obj) {
 73        if (this == obj) {
 74            return true;
 75        }
 76        if (obj == null || !(obj instanceof SMPPVersion)) {
 77            return false;
 78        } else {
 79            return ((SMPPVersion) obj).getVersionID() == versionID;
 80        }
 81    }
 82
 83    public int hashCode() {
 84        return new Integer(versionID).hashCode();
 85    }
 86    
 87    public boolean equals(int versionID) {
 88        return versionID == this.versionID;
 89    }
 90
 91    public String toString() {
 92        return versionString;
 93    }
 94
 95    public void validateTon(int ton) {
 96        if (ton < 0 || ton > 0xff) {
 97            throw new VersionException("Invalid TON: " + ton);
 98        }
 99    }
100    
101    public void validateNpi(int npi) {
102        if (npi < 0 || npi > 0xff) {
103            throw new VersionException("Invalid NPI: " + npi);
104        }
105    }
106    
107    public void validateAddress(Address address) {
108        if (address != null) {
109            validateTon(address.getTON());
110            validateNpi(address.getNPI());
111            String addr = address.getAddress();
112            if (addr != null && addr.length() > 20) {
113                throw new VersionException("Address is too long, 20 max: " + addr);
114            }
115        }
116    }
117
118    public void validateEsmClass(int esmClass) {
119        if (esmClass < 0 || esmClass > 0xff) {
120            throw new VersionException("Invalid ESM class: " + esmClass);
121        }
122    }
123
124    public void validateProtocolID(int protocolId) {
125        if (protocolId < 0 || protocolId > 0xff) {
126            throw new VersionException("Invalid protocol ID: " + protocolId);
127        }
128    }
129
130    public void validateDataCoding(int dataCoding) {
131        if (dataCoding < 0 || dataCoding > 0xff) {
132            throw new VersionException("Invalid data coding: " + dataCoding);
133        }
134    }
135
136    public void validateDefaultMsg(int defaultMsgId) {
137        if (defaultMsgId < 0 || defaultMsgId > 0xff) {
138            throw new VersionException(
139                    "Invalid default message ID: " + defaultMsgId);
140        }
141    }
142
143    public void validateServiceType(String serviceType) {
144        if (serviceType != null && serviceType.length() > 5) {
145            throw new VersionException("Invalid service type: " + serviceType);
146        }
147    }
148
149    public void validateMessageState(int messageState) {
150        if (messageState < 0 || messageState > 0xff) {
151            throw new VersionException(
152                    "Invalid message state: " + messageState);
153        }
154    }
155
156    public void validateErrorCode(int code) {
157        if (code < 0 || code > 0xff) {
158            throw new VersionException("Invalid error code: " + code);
159        }
160    }
161
162    public void validateReplaceIfPresent(int flag) {
163        if (flag < 0 || flag > 1) {
164            throw new VersionException(
165                    "Replace-if-present flag must be 0 or 1: " + flag);
166        }
167    }
168
169    public void validateNumUnsuccessful(int num) {
170        if (num < 0 || num > 0xff) {
171            throw new VersionException(
172                    "Invalid number of unsuccessful destinations: " + num);
173        }
174    }
175
176    public void validateDistListName(String name) {
177        if (name != null && name.length() > 20) {
178            throw new VersionException(
179                    "Distribution list name too long: " + name);
180        }
181    }
182
183    public void validateSystemId(String sysId) {
184        if (sysId != null && sysId.length() > 15) {
185            throw new VersionException("System ID too long: " + sysId);
186        }
187    }
188
189    public void validatePassword(String password) {
190        if (password != null && password.length() > 8) {
191            throw new VersionException("Password too long");
192        }
193    }
194
195    public void validateSystemType(String sysType) {
196        if (sysType != null && sysType.length() > 12) {
197            throw new VersionException("System type too long: " + sysType);
198        }
199    }
200
201    public void validateAddressRange(String addressRange) {
202        // Possibly add some checks for allowed characters??
203        if (addressRange != null && addressRange.length() > 40) {
204            throw new VersionException(
205                    "Address range too long: " + addressRange);
206        }
207    }
208
209    public void validateParamName(String paramName) {
210        throw new VersionException("Parameter retrieval is not supported.");
211    }
212
213    public void validateParamValue(String paramValue) {
214        throw new VersionException("Parameter retrieval is not supported.");
215    }
216}