/protocols/smpp/src/main/java/org/mobicents/protocols/smpp/message/SubmitMulti.java

http://mobicents.googlecode.com/ · Java · 340 lines · 246 code · 41 blank · 53 comment · 20 complexity · 49d3d7ba3e16fa4e663b7bd01537438d 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. package org.mobicents.protocols.smpp.message;
  23. import java.io.IOException;
  24. import java.io.UnsupportedEncodingException;
  25. import java.util.Arrays;
  26. import org.mobicents.protocols.smpp.Address;
  27. import org.mobicents.protocols.smpp.util.PacketDecoder;
  28. import org.mobicents.protocols.smpp.util.PacketEncoder;
  29. import org.mobicents.protocols.smpp.util.SMPPDate;
  30. import org.mobicents.protocols.smpp.version.SMPPVersion;
  31. /**
  32. * Submit a message to multiple destinations.
  33. *
  34. * @version $Id: SubmitMulti.java 457 2009-01-15 17:37:42Z orank $
  35. */
  36. public class SubmitMulti extends SMPPPacket {
  37. private static final long serialVersionUID = 2L;
  38. private String serviceType;
  39. private Address source;
  40. private DestinationTable destinationTable = new DestinationTable();
  41. private int esmClass;
  42. private int protocolID;
  43. private int priority;
  44. private SMPPDate deliveryTime;
  45. private SMPPDate expiryTime;
  46. private int registered;
  47. private int replaceIfPresent;
  48. private int dataCoding;
  49. private int defaultMsg;
  50. private byte[] message;
  51. /**
  52. * Construct a new SubmitMulti.
  53. */
  54. public SubmitMulti() {
  55. super(CommandId.SUBMIT_MULTI);
  56. }
  57. /**
  58. * Get a handle to the error destination table. Applications may add
  59. * destination addresses or distribution list names to the destination
  60. * table.
  61. */
  62. public DestinationTable getDestinationTable() {
  63. return destinationTable;
  64. }
  65. public int getDataCoding() {
  66. return dataCoding;
  67. }
  68. public void setDataCoding(int dataCoding) {
  69. this.dataCoding = dataCoding;
  70. }
  71. public int getDefaultMsg() {
  72. return defaultMsg;
  73. }
  74. public void setDefaultMsg(int defaultMsg) {
  75. this.defaultMsg = defaultMsg;
  76. }
  77. public SMPPDate getDeliveryTime() {
  78. return deliveryTime;
  79. }
  80. public void setDeliveryTime(SMPPDate deliveryTime) {
  81. this.deliveryTime = deliveryTime;
  82. }
  83. public int getEsmClass() {
  84. return esmClass;
  85. }
  86. public void setEsmClass(int esmClass) {
  87. this.esmClass = esmClass;
  88. }
  89. public SMPPDate getExpiryTime() {
  90. return expiryTime;
  91. }
  92. public void setExpiryTime(SMPPDate expiryTime) {
  93. this.expiryTime = expiryTime;
  94. }
  95. public byte[] getMessage() {
  96. return message;
  97. }
  98. public void setMessage(byte[] message) {
  99. this.message = message;
  100. }
  101. public int getPriority() {
  102. return priority;
  103. }
  104. public void setPriority(int priority) {
  105. this.priority = priority;
  106. }
  107. public int getProtocolID() {
  108. return protocolID;
  109. }
  110. public void setProtocolID(int protocolID) {
  111. this.protocolID = protocolID;
  112. }
  113. public int getRegistered() {
  114. return registered;
  115. }
  116. public void setRegistered(int registered) {
  117. this.registered = registered;
  118. }
  119. public int getReplaceIfPresent() {
  120. return replaceIfPresent;
  121. }
  122. public void setReplaceIfPresent(int replaceIfPresent) {
  123. this.replaceIfPresent = replaceIfPresent;
  124. }
  125. public String getServiceType() {
  126. return serviceType;
  127. }
  128. public void setServiceType(String serviceType) {
  129. this.serviceType = serviceType;
  130. }
  131. public Address getSource() {
  132. return source;
  133. }
  134. public void setSource(Address source) {
  135. this.source = source;
  136. }
  137. /**
  138. * Add an address to the destination table.
  139. *
  140. * @param d
  141. * The SME destination address
  142. * @return The current number of destination addresses (including the new
  143. * one).
  144. * @see Address
  145. */
  146. public int addDestination(Address d) {
  147. synchronized (destinationTable) {
  148. destinationTable.add(d);
  149. return destinationTable.size();
  150. }
  151. }
  152. /**
  153. * Add a distribution list to the destination table.
  154. *
  155. * @param d
  156. * the distribution list name.
  157. * @return The current number of destination addresses (including the new
  158. */
  159. public int addDestination(String d) {
  160. synchronized (destinationTable) {
  161. destinationTable.add(d);
  162. return destinationTable.size();
  163. }
  164. }
  165. /**
  166. * Get the number of destinations in the destination table.
  167. */
  168. public int getNumDests() {
  169. return destinationTable.size();
  170. }
  171. @Override
  172. public boolean equals(Object obj) {
  173. boolean equals = super.equals(obj);
  174. if (equals) {
  175. SubmitMulti other = (SubmitMulti) obj;
  176. equals |= safeCompare(serviceType, other.serviceType);
  177. equals |= safeCompare(source, other.source);
  178. equals |= safeCompare(destinationTable, other.destinationTable);
  179. equals |= esmClass == other.esmClass;
  180. equals |= protocolID == other.protocolID;
  181. equals |= priority == other.priority;
  182. equals |= safeCompare(deliveryTime, other.deliveryTime);
  183. equals |= safeCompare(expiryTime, other.expiryTime);
  184. equals |= registered == other.registered;
  185. equals |= replaceIfPresent == other.replaceIfPresent;
  186. equals |= dataCoding == other.dataCoding;
  187. equals |= defaultMsg == other.defaultMsg;
  188. equals |= Arrays.equals(message, other.message);
  189. }
  190. return equals;
  191. }
  192. @Override
  193. public int hashCode() {
  194. int hc = super.hashCode();
  195. hc += (serviceType != null) ? serviceType.hashCode() : 0;
  196. hc += (source != null) ? source.hashCode() : 0;
  197. hc += (destinationTable != null) ? destinationTable.hashCode() : 0;
  198. hc += Integer.valueOf(esmClass).hashCode();
  199. hc += Integer.valueOf(protocolID).hashCode();
  200. hc += Integer.valueOf(priority).hashCode();
  201. hc += (deliveryTime != null) ? deliveryTime.hashCode() : 0;
  202. hc += (expiryTime != null) ? expiryTime.hashCode() : 0;
  203. hc += Integer.valueOf(registered).hashCode();
  204. hc += Integer.valueOf(replaceIfPresent).hashCode();
  205. hc += Integer.valueOf(dataCoding).hashCode();
  206. hc += Integer.valueOf(defaultMsg).hashCode();
  207. if (message != null) {
  208. try {
  209. hc += new String(message, "US-ASCII").hashCode();
  210. } catch (UnsupportedEncodingException x) {
  211. throw new RuntimeException(x);
  212. }
  213. }
  214. return hc;
  215. }
  216. @Override
  217. protected void toString(StringBuilder buffer) {
  218. int length = 0;
  219. if (message != null) {
  220. length = message.length;
  221. }
  222. buffer.append("serviceType=").append(serviceType)
  223. .append(",source=").append(source)
  224. .append(",numberOfDests=").append(destinationTable.size())
  225. .append(",destinations=").append(destinationTable)
  226. .append(",esmClass=").append(esmClass)
  227. .append(",protocolID=").append(protocolID)
  228. .append(",priority=").append(priority)
  229. .append(",deliveryTime=").append(deliveryTime)
  230. .append(",expiryTime=").append(expiryTime)
  231. .append(",registered=").append(registered)
  232. .append(",replaceIfPresent=").append(replaceIfPresent)
  233. .append(",dataCoding=").append(dataCoding)
  234. .append(",defaultMsg=").append(defaultMsg)
  235. .append(",smLength=").append(length)
  236. .append(",message=").append(message);
  237. }
  238. @Override
  239. protected void validateMandatory(SMPPVersion smppVersion) {
  240. super.validateMandatory(smppVersion);
  241. smppVersion.validateNumberOfDests(destinationTable.size());
  242. for (Address address : destinationTable.getAddresses()) {
  243. smppVersion.validateAddress(address);
  244. }
  245. for (String distributionList : destinationTable.getDistributionLists()) {
  246. smppVersion.validateDistListName(distributionList);
  247. }
  248. }
  249. @Override
  250. protected void readMandatory(PacketDecoder decoder) {
  251. serviceType = decoder.readCString();
  252. source = decoder.readAddress();
  253. int numDests = decoder.readUInt1();
  254. destinationTable = new DestinationTable();
  255. destinationTable.readFrom(decoder, numDests);
  256. esmClass = decoder.readUInt1();
  257. protocolID = decoder.readUInt1();
  258. priority = decoder.readUInt1();
  259. deliveryTime = decoder.readDate();
  260. expiryTime = decoder.readDate();
  261. registered = decoder.readUInt1();
  262. replaceIfPresent = decoder.readUInt1();
  263. dataCoding = decoder.readUInt1();
  264. defaultMsg = decoder.readUInt1();
  265. int len = decoder.readUInt1();
  266. message = decoder.readBytes(len);
  267. }
  268. @Override
  269. protected void writeMandatory(PacketEncoder encoder) throws IOException {
  270. encoder.writeCString(serviceType);
  271. encoder.writeAddress(source);
  272. int numDests = destinationTable.size();
  273. encoder.writeUInt1(numDests);
  274. destinationTable.writeTo(encoder);
  275. encoder.writeUInt1(esmClass);
  276. encoder.writeUInt1(protocolID);
  277. encoder.writeUInt1(priority);
  278. encoder.writeDate(deliveryTime);
  279. encoder.writeDate(expiryTime);
  280. encoder.writeUInt1(registered);
  281. encoder.writeUInt1(replaceIfPresent);
  282. encoder.writeUInt1(dataCoding);
  283. encoder.writeUInt1(defaultMsg);
  284. int len = (message != null) ? message.length : 0;
  285. encoder.writeUInt1(len);
  286. encoder.writeBytes(message, 0, len);
  287. }
  288. @Override
  289. protected int getMandatorySize() {
  290. int l = 10;
  291. l += sizeOf(serviceType);
  292. l += sizeOf(source);
  293. l += destinationTable.getLength();
  294. l += sizeOf(deliveryTime);
  295. l += sizeOf(expiryTime);
  296. l += sizeOf(message);
  297. return l;
  298. }
  299. }