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