PageRenderTime 37ms CodeModel.GetById 13ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/smpp/src/main/java/org/mobicents/protocols/smpp/message/tlv/Tag.java

http://mobicents.googlecode.com/
Java | 462 lines | 218 code | 87 blank | 157 comment | 15 complexity | 930ab3c6b68ed2f3439e09cdbfb55c42 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.message.tlv;
 24
 25import java.util.HashMap;
 26import java.util.Map;
 27
 28import org.mobicents.protocols.smpp.message.param.ParamDescriptor;
 29
 30/**
 31 * Enumeration class for optional parameter tag values.
 32 * 
 33 * @version $Id: Tag.java 457 2009-01-15 17:37:42Z orank $
 34 */
 35public final class Tag implements java.io.Serializable {
 36
 37    /**
 38     * Look-up table of statically defined tags. This <b>must</b> be defined
 39     * before all the tags as the Tag constructor expects this object to exist.
 40     */
 41    private static Map<Integer, Tag> tagTable = new HashMap<Integer, Tag>();
 42
 43    static final long serialVersionUID = 2L;
 44    
 45    public static final Tag DEST_ADDR_SUBUNIT =
 46        new Tag(0x05, BasicDescriptors.INTEGER1, 1);
 47
 48    public static final Tag DEST_NETWORK_TYPE =
 49        new Tag(0x06, BasicDescriptors.INTEGER1, 1);
 50
 51    public static final Tag DEST_BEARER_TYPE =
 52        new Tag(0x07, BasicDescriptors.INTEGER1, 1);
 53
 54    public static final Tag DEST_TELEMATICS_ID =
 55        new Tag(0x08, BasicDescriptors.INTEGER2, 2);
 56
 57    public static final Tag SOURCE_ADDR_SUBUNIT =
 58        new Tag(0x0d, BasicDescriptors.INTEGER1, 1);
 59
 60    public static final Tag SOURCE_NETWORK_TYPE =
 61        new Tag(0x0e, BasicDescriptors.INTEGER1, 1);
 62
 63    public static final Tag SOURCE_BEARER_TYPE =
 64        new Tag(0x0f, BasicDescriptors.INTEGER1, 1);
 65
 66    public static final Tag SOURCE_TELEMATICS_ID =
 67        new Tag(0x10, BasicDescriptors.INTEGER1, 1);
 68
 69    public static final Tag QOS_TIME_TO_LIVE =
 70        new Tag(0x17, BasicDescriptors.INTEGER4, 4);
 71
 72    public static final Tag PAYLOAD_TYPE =
 73        new Tag(0x19, BasicDescriptors.INTEGER1, 1);
 74
 75    public static final Tag ADDITIONAL_STATUS_INFO_TEXT =
 76        new Tag(0x1d, BasicDescriptors.CSTRING, 1, 256);
 77
 78    public static final Tag RECEIPTED_MESSAGE_ID =
 79        new Tag(0x1e, BasicDescriptors.CSTRING, 1, 65);
 80
 81    public static final Tag MS_MSG_WAIT_FACILITIES =
 82        new Tag(0x30, BasicDescriptors.BITMASK, 1);
 83
 84    public static final Tag PRIVACY_INDICATOR =
 85        new Tag(0x201, BasicDescriptors.INTEGER1, 1);
 86
 87    public static final Tag SOURCE_SUBADDRESS =
 88        new Tag(0x202, BasicDescriptors.BYTES, 2, 23);
 89
 90    public static final Tag DEST_SUBADDRESS =
 91        new Tag(0x203, BasicDescriptors.BYTES, 2, 23);
 92
 93    public static final Tag USER_MESSAGE_REFERENCE =
 94        new Tag(0x204, BasicDescriptors.INTEGER2, 2);
 95
 96    public static final Tag USER_RESPONSE_CODE =
 97        new Tag(0x205, BasicDescriptors.INTEGER1, 1);
 98
 99    public static final Tag SOURCE_PORT =
100        new Tag(0x20a, BasicDescriptors.INTEGER2, 2);
101
102    public static final Tag DESTINATION_PORT =
103        new Tag(0x20b, BasicDescriptors.INTEGER2, 2);
104
105    public static final Tag SAR_MSG_REF_NUM =
106        new Tag(0x20c, BasicDescriptors.INTEGER2, 2);
107
108    public static final Tag LANGUAGE_INDICATOR =
109        new Tag(0x20d, BasicDescriptors.INTEGER1, 1);
110
111    public static final Tag SAR_TOTAL_SEGMENTS =
112        new Tag(0x20e, BasicDescriptors.INTEGER1, 1);
113
114    public static final Tag SAR_SEGMENT_SEQNUM =
115        new Tag(0x20f, BasicDescriptors.INTEGER1, 1);
116
117    public static final Tag SC_INTERFACE_VERSION =
118        new Tag(0x210, BasicDescriptors.INTEGER1, 1);
119
120    public static final Tag CALLBACK_NUM_PRES_IND =
121        new Tag(0x302, BasicDescriptors.BITMASK, 1);
122
123    public static final Tag CALLBACK_NUM_ATAG =
124        new Tag(0x303, BasicDescriptors.BYTES, 0, 65);
125
126    public static final Tag NUMBER_OF_MESSAGES =
127        new Tag(0x304, BasicDescriptors.INTEGER1, 1);
128
129    public static final Tag CALLBACK_NUM =
130        new Tag(0x381, BasicDescriptors.BYTES, 4, 19);
131
132    public static final Tag DPF_RESULT =
133        new Tag(0x420, BasicDescriptors.INTEGER1, 1);
134
135    public static final Tag SET_DPF =
136        new Tag(0x421, BasicDescriptors.INTEGER1, 1);
137
138    public static final Tag MS_AVAILABILITY_STATUS =
139        new Tag(0x422, BasicDescriptors.INTEGER1, 1);
140
141    public static final Tag NETWORK_ERROR_CODE =
142        new Tag(0x423, BasicDescriptors.BYTES, 3);
143
144    public static final Tag MESSAGE_PAYLOAD =
145        new Tag(0x424, BasicDescriptors.BYTES, -1);
146
147    public static final Tag DELIVERY_FAILURE_REASON =
148        new Tag(0x425, BasicDescriptors.INTEGER1, 1);
149
150    public static final Tag MORE_MESSAGES_TO_SEND =
151        new Tag(0x426, BasicDescriptors.INTEGER1, 1);
152
153    public static final Tag MESSAGE_STATE =
154        new Tag(0x427, BasicDescriptors.INTEGER1, 1);
155
156    public static final Tag USSD_SERVICE_OP =
157        new Tag(0x501, BasicDescriptors.BYTES, 1);
158
159    public static final Tag DISPLAY_TIME =
160        new Tag(0x1201, BasicDescriptors.INTEGER1, 1);
161
162    public static final Tag SMS_SIGNAL =
163        new Tag(0x1203, BasicDescriptors.INTEGER2, 2);
164
165    public static final Tag MS_VALIDITY =
166        new Tag(0x1204, BasicDescriptors.INTEGER1, 1);
167
168    public static final Tag ALERT_ON_MESSAGE_DELIVERY =
169        new Tag(0x130c, null, 0);
170
171    public static final Tag ITS_REPLY_TYPE =
172        new Tag(0x1380, BasicDescriptors.INTEGER1, 1);
173
174    public static final Tag ITS_SESSION_INFO =
175        new Tag(0x1383, BasicDescriptors.BYTES, 2);
176
177    public static final Tag BROADCAST_AREA_IDENTIFIER =
178        new Tag(0x0606, BasicDescriptors.BYTES, -1);
179    
180    public static final Tag BROADCAST_AREA_SUCCESS =
181        new Tag(0x0608, BasicDescriptors.INTEGER1, 1);
182    
183    public static final Tag BROADCAST_CONTENT_TYPE_INFO =
184        new Tag(0x0602, BasicDescriptors.BYTES, 1, 255);
185    
186    public static final Tag BROADCAST_CHANNEL_INDICATOR =
187        new Tag(0x0600, BasicDescriptors.INTEGER1, 1);
188    
189    public static final Tag BROADCAST_CONTENT_TYPE =
190        new Tag(0x0601, BasicDescriptors.BYTES, 1, 255);
191
192    public static final Tag BROADCAST_END_TIME = 
193        new Tag(0x0609, BasicDescriptors.DATE, 16, 16);
194    
195    public static final Tag BROADCAST_ERROR_STATUS =
196        new Tag(0x607, BasicDescriptors.INTEGER4, 4);
197
198    public static final Tag BROADCAST_FREQUENCY_INTERVAL =
199        new Tag(0x0605, BasicDescriptors.BYTES, 3);
200
201    public static final Tag BROADCAST_MESSAGE_CLASS =
202        new Tag(0x0603, BasicDescriptors.INTEGER1, 1);
203    
204    public static final Tag BROADCAST_REP_NUM = 
205        new Tag(0x604, BasicDescriptors.INTEGER2, 2);
206    
207    public static final Tag BROADCAST_SERVICE_GROUP =
208        new Tag(0x060A, BasicDescriptors.BYTES, 1, 255);
209    
210    /**
211     * Integer value of this tag.
212     */
213    private Integer tag;
214
215    /**
216     * The minimum length a value of this tag type can be.
217     */
218    private int minLength = -1;
219
220    /**
221     * The maximum length a value of this tag type can be.
222     */
223    private int maxLength = -1;
224
225    /**
226     * The class used for encoding and decoding values of this tag type.
227     * @see org.mobicents.protocols.smpp.message.param.ParamDescriptor
228     */
229    private ParamDescriptor paramDescriptor;
230
231    private Tag(int tag,
232            ParamDescriptor paramDescriptor,
233            int fixedLength) throws TagDefinedException {
234        this(tag, paramDescriptor, fixedLength, fixedLength);
235    }
236    
237    /**
238     * Create a new Tag.
239     * 
240     * @param tag
241     *            The integer value of the tag.
242     * @param enc
243     *            The encoding class to use to encode and decode values.
244     * @param minLength
245     *            The minimum length allowed for the value.
246     * @param maxLength
247     *            The maximum length allowed for the value.
248     * @throws org.mobicents.protocols.smpp.message.tlv.TagDefinedException
249     *             If a tag with integer value <code>tag</code> has already
250     *             been defined.
251     */
252    private Tag(int tag,
253            ParamDescriptor paramDescriptor,
254            int minLength,
255            int maxLength) throws TagDefinedException {
256        this.tag = new Integer(tag);
257        this.paramDescriptor = paramDescriptor;
258        this.minLength = minLength;
259        this.maxLength = maxLength;
260        synchronized (tagTable) {
261            if (tagTable.containsKey(this.tag)) {
262                throw new TagDefinedException(tag, "Tag 0x"
263                        + Integer.toHexString(tag) + " is already defined.");
264            }
265            tagTable.put(this.tag, this);
266        }
267    }
268
269    /**
270     * Get the integer value of this tag.
271     * 
272     * @return the integer value of this tag.
273     */
274    public int intValue() {
275        return tag.intValue();
276    }
277
278    /**
279     * Get the allowed length of a value of this tag type.
280     * 
281     * @return The allowed length, or the maximum length if a range is set.
282     */
283    public int getLength() {
284        return maxLength < 0 ? minLength : maxLength;
285    }
286
287    /**
288     * Get the minimum length of a value of this tag type.
289     * 
290     * @return the minimum length of a value of this tag type.
291     */
292    public int getMinLength() {
293        return minLength;
294    }
295
296    /**
297     * Get the maximum length of a value of this tag type.
298     * 
299     * @return the maximum length of a value of this tag type.
300     */
301    public int getMaxLength() {
302        return maxLength;
303    }
304
305    public ParamDescriptor getParamDescriptor() {
306        return paramDescriptor;
307    }
308    
309    /**
310     * Test for equality. Two tags are equal if their integer values are
311     * equivalent.
312     * 
313     * @return true if <code>obj</code> is Tag and has the same tag value.
314     */
315    @Override
316    public boolean equals(Object obj) {
317        if (obj == null) {
318            return false;
319        }
320
321        if (!(obj instanceof Tag)) {
322            return false;
323        }
324
325        if (this.tag == null) {
326            return false;
327        }
328
329        Tag other = (Tag) obj;
330
331        if (other.tag == null) {
332            return false;
333        }
334
335        return this.tag.intValue() == other.tag.intValue();
336    }
337
338    /**
339     * Test for equality against an integer.
340     * 
341     * @return true if this Tag's integer value is equal to <code>tag</code>.
342     */
343    public boolean equals(int tag) {
344        return tag == this.tag.intValue();
345    }
346
347    /**
348     * Get the hashCode for this Tag. The hashCode for a Tag is the same as:
349     * <br>
350     * <code>new Integer(tag.tagValue()).hashCode()</code>
351     * 
352     * @return A hash code for this tag.
353     */
354    @Override
355    public int hashCode() {
356        return tag.hashCode();
357    }
358
359    /**
360     * Convert this tag to a String. This returns a decimal representation of
361     * the tag's integer value in a String.
362     * 
363     * @return This tag's string value.
364     */
365    public String toString() {
366        return tag.toString();
367    }
368
369    /**
370     * Convert this tag to a String. This returns a hex representation of the
371     * tag's integer value in a String.
372     * 
373     * @return This tag's hexadecimal representation.
374     */
375    public String toHexString() {
376        return Integer.toHexString(tag.intValue());
377    }
378
379    /**
380     * Get the Tag object that represents tag <code>tagValue</code>. If the
381     * tag is known then the static Tag object representing the tag is returned.
382     * If the tag is not known, a fresh instance of Tag will be returned which
383     * uses an octet-string type.
384     * 
385     * <p><b>WARNING</b> The behaviour of this method may change to returning
386     * <code>null</code> for an undefined tag. It needs to be determined
387     * which behaviour is the best.</p>
388     * 
389     * @return The Tag object representing the tag <code>tagValue</code>.
390     *         Will never return <code>null</code>.
391     */
392    public static Tag getTag(int tagValue) {
393        Tag t = (Tag) tagTable.get(new Integer(tagValue));
394        if (t == null) {
395            return Tag.defineTag(tagValue, BasicDescriptors.BYTES, -1);
396        } else {
397            return t;
398        }
399    }
400
401    /**
402     * Define a new tag type which has a fixed length.
403     * @param tagValue The integer value of the tag.
404     * @param paramDescriptor The parameter type descriptor.
405     * @param fixedSize The defined size of the parameter.
406     * @throws org.mobicents.protocols.smpp.message.tlv.TagDefinedException If an attempt is
407     * made to define a tag with a integer value equivalent to an already
408     * defined tag.
409     * @see ParamDescriptor
410     */
411    public static Tag defineTag(int tagValue,
412            ParamDescriptor paramDescriptor,
413            int fixedSize) throws TagDefinedException {
414        return new Tag(tagValue, paramDescriptor, fixedSize, fixedSize);
415    }
416
417    /**
418     * Define a new tag type with minimum and maximum sizes.
419     * @param tagValue The integer value of the tag.
420     * @param paramDescriptor The parameter type descriptor.
421     * @param minSize The minimum size of the parameter.
422     * @param maxSize The maximum size of the parameter.
423     * @throws org.mobicents.protocols.smpp.message.tlv.TagDefinedException If an attempt is
424     * made to define a tag with a integer value equivalent to an already
425     * defined tag.
426     * @see ParamDescriptor
427     */
428    public static Tag defineTag(int tagValue,
429            ParamDescriptor paramDescriptor,
430            int minSize,
431            int maxSize) throws TagDefinedException {
432        return new Tag(tagValue, paramDescriptor, minSize, maxSize);
433    }
434
435    /**
436     * Determine if a tag is defined for a particular tag integer value. 
437     * @param tagValue The integer value of the tag.
438     * @return <code>true</code> if the tag is defined, <code>false</code>
439     * otherwise.
440     */
441    public static boolean isTagDefined(int tagValue) {
442        return tagTable.containsKey(new Integer(tagValue));
443    }
444    
445    /**
446     * Undefine a tag. This removes all knoweledge of this tag type from the
447     * internal tables. If there is no such tag defined already, this method
448     * will do nothing.
449     * 
450     * @param tag
451     *            The tag to undefine. null if there was no tag defined already.
452     * @return The Tag object that has been undefined.
453     */
454    public static Tag undefineTag(Tag tag) {
455        if (tag == null) {
456            return null;
457        }
458        synchronized (tagTable) {
459            return (Tag) tagTable.remove(tag.tag);
460        }
461    }
462}