PageRenderTime 49ms CodeModel.GetById 10ms app.highlight 34ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 215 lines | 152 code | 28 blank | 35 comment | 10 complexity | a7b8d0e4d926fe7707c9cca81b250da7 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;
 24
 25import java.io.IOException;
 26import java.io.UnsupportedEncodingException;
 27import java.util.Arrays;
 28
 29import org.mobicents.protocols.smpp.Address;
 30import org.mobicents.protocols.smpp.util.PacketDecoder;
 31import org.mobicents.protocols.smpp.util.PacketEncoder;
 32import org.mobicents.protocols.smpp.util.SMPPDate;
 33import org.mobicents.protocols.smpp.version.SMPPVersion;
 34
 35/**
 36 * Replace a message. This message submits a short message to the SMSC replacing
 37 * a previously submitted message.
 38 * 
 39 * @version $Id: ReplaceSM.java 457 2009-01-15 17:37:42Z orank $
 40 */
 41public class ReplaceSM extends SMPPPacket {
 42    private static final long serialVersionUID = 2L;
 43    
 44    private String messageId;
 45    private Address source;
 46    private SMPPDate deliveryTime;
 47    private SMPPDate expiryTime;
 48    private int registered;
 49    private int defaultMsg;
 50    private byte[] message;
 51
 52    /**
 53     * Construct a new ReplaceSM.
 54     */
 55    public ReplaceSM() {
 56        super(CommandId.REPLACE_SM);
 57    }
 58    
 59    public int getDefaultMsg() {
 60        return defaultMsg;
 61    }
 62
 63    public void setDefaultMsg(int defaultMsg) {
 64        this.defaultMsg = defaultMsg;
 65    }
 66
 67    public SMPPDate getDeliveryTime() {
 68        return deliveryTime;
 69    }
 70
 71    public void setDeliveryTime(SMPPDate deliveryTime) {
 72        this.deliveryTime = deliveryTime;
 73    }
 74
 75    public SMPPDate getExpiryTime() {
 76        return expiryTime;
 77    }
 78
 79    public void setExpiryTime(SMPPDate expiryTime) {
 80        this.expiryTime = expiryTime;
 81    }
 82
 83    public byte[] getMessage() {
 84        return message;
 85    }
 86
 87    public void setMessage(byte[] message) {
 88        this.message = message;
 89    }
 90
 91    public String getMessageId() {
 92        return messageId;
 93    }
 94
 95    public void setMessageId(String messageId) {
 96        this.messageId = messageId;
 97    }
 98
 99    public int getRegistered() {
100        return registered;
101    }
102
103    public void setRegistered(int registered) {
104        this.registered = registered;
105    }
106
107    public Address getSource() {
108        return source;
109    }
110
111    public void setSource(Address source) {
112        this.source = source;
113    }
114
115    /**
116     * Get the number of octets in the message payload.
117     * 
118     * @return The number of octets (bytes) in the message payload.
119     */
120    public int getMessageLen() {
121        return sizeOf(message);
122    }
123
124    @Override
125    public boolean equals(Object obj) {
126        boolean equals = super.equals(obj);
127        if (equals) {
128            ReplaceSM other = (ReplaceSM) obj;
129            equals |= safeCompare(messageId, other.messageId);
130            equals |= safeCompare(source, other.source);
131            equals |= safeCompare(deliveryTime, other.deliveryTime);
132            equals |= safeCompare(expiryTime, other.expiryTime);
133            equals |= registered == other.registered;
134            equals |= defaultMsg == other.defaultMsg;
135            equals |= Arrays.equals(message, other.message);
136        }
137        return equals;
138    }
139    
140    @Override
141    public int hashCode() {
142        int hc = super.hashCode();
143        hc += (messageId != null) ? messageId.hashCode() : 0;
144        hc += (source != null) ? source.hashCode() : 0;
145        hc += (deliveryTime != null) ? deliveryTime.hashCode() : 0;
146        hc += (expiryTime != null) ? expiryTime.hashCode() : 0;
147        hc += Integer.valueOf(registered).hashCode();
148        hc += Integer.valueOf(defaultMsg).hashCode();
149        if (message != null) {
150            try {
151                hc += new String(message, "US-ASCII").hashCode();
152            } catch (UnsupportedEncodingException x) {
153                throw new RuntimeException(x);
154            }
155        }
156        return hc;
157    }
158
159    @Override
160    protected void toString(StringBuilder buffer) {
161        buffer.append("messageId=").append(messageId)
162        .append(",source=").append(source)
163        .append(",deliveryTime=").append(deliveryTime)
164        .append(",expiryTime=").append(expiryTime)
165        .append(",registered=").append(registered)
166        .append(",defaultMsg=").append(defaultMsg)
167        .append(",length=").append(getMessageLen())
168        .append(",message=").append(message);
169    }
170    
171    @Override
172    protected void validateMandatory(SMPPVersion smppVersion) {
173        smppVersion.validateMessageId(messageId);
174        smppVersion.validateAddress(source);
175        smppVersion.validateRegisteredDelivery(registered);
176        smppVersion.validateDefaultMsg(defaultMsg);
177        smppVersion.validateMessage(message, 0, getMessageLen());
178    }
179    
180    @Override
181    protected void readMandatory(PacketDecoder decoder) {
182        messageId = decoder.readCString();
183        source = decoder.readAddress();
184        deliveryTime = decoder.readDate();
185        expiryTime = decoder.readDate();
186        registered = decoder.readUInt1();
187        defaultMsg = decoder.readUInt1();
188        int len = decoder.readUInt1();
189        message = decoder.readBytes(len);
190    }
191    
192    @Override
193    protected void writeMandatory(PacketEncoder encoder) throws IOException {
194        encoder.writeCString(messageId);
195        encoder.writeAddress(source);
196        encoder.writeDate(deliveryTime);
197        encoder.writeDate(expiryTime);
198        encoder.writeUInt1(registered);
199        encoder.writeUInt1(defaultMsg);
200        int len = (message != null) ? message.length : 0;
201        encoder.writeUInt1(len);
202        encoder.writeBytes(message, 0, len);
203    }
204    
205    @Override
206    protected int getMandatorySize() {
207        int length = 4;
208        length += sizeOf(messageId);
209        length += sizeOf(source);
210        length += sizeOf(deliveryTime);
211        length += sizeOf(expiryTime);
212        length += sizeOf(message);
213        return length;
214    }
215}