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

http://mobicents.googlecode.com/ · Java · 307 lines · 214 code · 55 blank · 38 comment · 32 complexity · ccbfd36d7e3e9655d67294b5541204b6 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. /**
  31. * Submit a message to the SMSC for delivery to a single destination.
  32. *
  33. * @version $Id: SubmitSM.java 452 2009-01-15 16:56:36Z orank $
  34. * @author amit bhayani
  35. * @author orank
  36. */
  37. public class SubmitSM extends SMPPPacket {
  38. private static final long serialVersionUID = 2L;
  39. private String serviceType;
  40. private Address source;
  41. private Address destination;
  42. private int esmClass;
  43. private int protocolID;
  44. private int priority;
  45. private SMPPDate deliveryTime;
  46. private SMPPDate expiryTime;
  47. private int registered;
  48. private int replaceIfPresent;
  49. private int dataCoding;
  50. private int defaultMsg;
  51. private byte[] message;
  52. public SubmitSM() {
  53. super(CommandId.SUBMIT_SM);
  54. }
  55. SubmitSM(int commandId) {
  56. // Convenience constructor provided for deliver_sm.
  57. super(commandId);
  58. }
  59. public int getDataCoding() {
  60. return dataCoding;
  61. }
  62. public void setDataCoding(int dataCoding) {
  63. this.dataCoding = dataCoding;
  64. }
  65. public int getDefaultMsg() {
  66. return defaultMsg;
  67. }
  68. public void setDefaultMsg(int defaultMsg) {
  69. this.defaultMsg = defaultMsg;
  70. }
  71. public SMPPDate getDeliveryTime() {
  72. return deliveryTime;
  73. }
  74. public void setDeliveryTime(SMPPDate deliveryTime) {
  75. this.deliveryTime = deliveryTime;
  76. }
  77. public Address getDestination() {
  78. return destination;
  79. }
  80. public void setDestination(Address destination) {
  81. this.destination = destination;
  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. @Override
  138. public boolean equals(Object obj) {
  139. boolean equals = super.equals(obj);
  140. if (equals) {
  141. SubmitSM other = (SubmitSM) obj;
  142. equals |= safeCompare(serviceType, other.serviceType);
  143. equals |= safeCompare(source, other.source);
  144. equals |= safeCompare(destination, other.destination);
  145. equals |= esmClass == other.esmClass;
  146. equals |= protocolID == other.protocolID;
  147. equals |= priority == other.priority;
  148. equals |= safeCompare(deliveryTime, other.deliveryTime);
  149. equals |= safeCompare(expiryTime, other.expiryTime);
  150. equals |= registered == other.registered;
  151. equals |= replaceIfPresent == other.replaceIfPresent;
  152. equals |= dataCoding == other.dataCoding;
  153. equals |= defaultMsg == other.defaultMsg;
  154. equals |= Arrays.equals(message, other.message);
  155. }
  156. return equals;
  157. }
  158. @Override
  159. public int hashCode() {
  160. int hc = super.hashCode();
  161. hc += (serviceType != null) ? serviceType.hashCode() : 0;
  162. hc += (source != null) ? source.hashCode() : 0;
  163. hc += (destination != null) ? destination.hashCode() : 0;
  164. hc += Integer.valueOf(esmClass).hashCode();
  165. hc += Integer.valueOf(protocolID).hashCode();
  166. hc += Integer.valueOf(priority).hashCode();
  167. hc += (deliveryTime != null) ? deliveryTime.hashCode() : 0;
  168. hc += (expiryTime != null) ? expiryTime.hashCode() : 0;
  169. hc += Integer.valueOf(registered).hashCode();
  170. hc += Integer.valueOf(replaceIfPresent).hashCode();
  171. hc += Integer.valueOf(dataCoding).hashCode();
  172. hc += Integer.valueOf(defaultMsg).hashCode();
  173. if (message != null) {
  174. try {
  175. hc += new String(message, "US-ASCII").hashCode();
  176. } catch (UnsupportedEncodingException x) {
  177. throw new RuntimeException(x);
  178. }
  179. }
  180. return hc;
  181. }
  182. /**
  183. * Return the number of bytes this packet would be encoded as to an
  184. * OutputStream.
  185. *
  186. * @return the number of bytes this packet would encode as.
  187. */
  188. public int getMandatorySize() {
  189. int len = ((serviceType != null) ? serviceType.length() : 0)
  190. + ((source != null) ? source.getLength() : 3)
  191. + ((destination != null) ? destination.getLength() : 3)
  192. + ((deliveryTime != null) ? deliveryTime.getLength()
  193. : 1)
  194. + ((expiryTime != null) ? expiryTime.getLength() : 1)
  195. + ((message != null) ? (message.length + 1) : 1);
  196. // 8 1-byte integers, 3 c-strings
  197. return len + 8;
  198. }
  199. protected void writeMandatory(PacketEncoder encoder) throws IOException {
  200. encoder.writeCString(serviceType);
  201. if (source != null) {
  202. source.writeTo(encoder);
  203. } else {
  204. // Write ton=0(null), npi=0(null), address=\0(nul)
  205. new Address().writeTo(encoder);
  206. }
  207. if (destination != null) {
  208. destination.writeTo(encoder);
  209. } else {
  210. // Write ton=0(null), npi=0(null), address=\0(nul)
  211. new Address().writeTo(encoder);
  212. }
  213. encoder.writeUInt1(esmClass);
  214. encoder.writeUInt1(protocolID);
  215. encoder.writeUInt1(priority);
  216. encoder.writeDate(deliveryTime);
  217. encoder.writeDate(expiryTime);
  218. encoder.writeUInt1(registered);
  219. encoder.writeUInt1(replaceIfPresent);
  220. encoder.writeUInt1(dataCoding);
  221. encoder.writeUInt1(defaultMsg);
  222. int smLength = 0;
  223. if (message != null) {
  224. smLength = message.length;
  225. }
  226. encoder.writeUInt1(smLength);
  227. if (message != null) {
  228. encoder.writeBytes(message);
  229. }
  230. }
  231. @Override
  232. protected void readMandatory(PacketDecoder decoder) {
  233. this.serviceType = decoder.readCString();
  234. this.source = new Address();
  235. this.source.readFrom(decoder);
  236. this.destination = new Address();
  237. this.destination.readFrom(decoder);
  238. this.esmClass = decoder.readUInt1();
  239. this.protocolID = decoder.readUInt1();
  240. this.priority = decoder.readUInt1();
  241. this.deliveryTime = decoder.readDate();
  242. this.expiryTime = decoder.readDate();
  243. this.registered = decoder.readUInt1();
  244. this.replaceIfPresent = decoder.readUInt1();
  245. this.dataCoding = decoder.readUInt1();
  246. this.defaultMsg = decoder.readUInt1();
  247. int smLength = decoder.readUInt1();
  248. if(smLength > 0){
  249. this.message =decoder.readBytes(smLength);
  250. }
  251. }
  252. }