forked from fizzed/cloudhopper-smpp
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathBaseSm.java
More file actions
246 lines (209 loc) · 8.68 KB
/
BaseSm.java
File metadata and controls
246 lines (209 loc) · 8.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package com.cloudhopper.smpp.pdu;
/*
* #%L
* ch-smpp
* %%
* Copyright (C) 2009 - 2015 Cloudhopper by Twitter
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import com.cloudhopper.commons.util.HexUtil;
import com.cloudhopper.smpp.type.Address;
import com.cloudhopper.smpp.type.UnrecoverablePduException;
import com.cloudhopper.smpp.type.RecoverablePduException;
import com.cloudhopper.commons.util.StringUtil;
import com.cloudhopper.smpp.type.SmppInvalidArgumentException;
import com.cloudhopper.smpp.util.ChannelBufferUtil;
import com.cloudhopper.smpp.util.PduUtil;
import org.jboss.netty.buffer.ChannelBuffer;
/**
* Base "short message" PDU as a super class for submit_sm, deliver_sm, and
* data_sm. Having a common base class they all inherit from makes it easier
* to work with requests in a standard way, even though data_sm does NOT actually
* support all of the same parameters.
*
* @author joelauer (twitter: @jjlauer or <a href="http://twitter.com/jjlauer" target=window>http://twitter.com/jjlauer</a>)
*/
public abstract class BaseSm<R extends PduResponse> extends PduRequest<R> {
protected String serviceType;
protected Address sourceAddress;
protected Address destAddress;
protected byte esmClass;
private byte protocolId; // not present in data_sm
private byte priority; // not present in data_sm
private String scheduleDeliveryTime; // not present in data_sm
private String validityPeriod; // not present in data_sm
protected byte registeredDelivery;
private byte replaceIfPresent; // not present in data_sm
protected byte dataCoding;
private byte defaultMsgId; // not present in data_sm, not used in deliver_sm
private byte[] shortMessage; // not present in data_sm
public BaseSm(int commandId, String name) {
super(commandId, name);
}
public int getShortMessageLength() {
return this.shortMessage == null ? 0 : this.shortMessage.length;
}
public byte[] getShortMessage() {
return this.shortMessage;
}
public void setShortMessage(byte[] value) throws SmppInvalidArgumentException {
if (value != null && value.length > 255) {
throw new SmppInvalidArgumentException("A short message in a PDU can only be a max of 255 bytes [actual=" + value.length + "]; use optional parameter message_payload as an alternative");
}
this.shortMessage = value;
}
public byte getReplaceIfPresent() {
return this.replaceIfPresent;
}
public void setReplaceIfPresent(byte value) {
this.replaceIfPresent = value;
}
public byte getDataCoding() {
return this.dataCoding;
}
public void setDataCoding(byte value) {
this.dataCoding = value;
}
public byte getDefaultMsgId() {
return this.defaultMsgId;
}
public void setDefaultMsgId(byte value) {
this.defaultMsgId = value;
}
public byte getRegisteredDelivery() {
return this.registeredDelivery;
}
public void setRegisteredDelivery(byte value) {
this.registeredDelivery = value;
}
public String getValidityPeriod() {
return this.validityPeriod;
}
public void setValidityPeriod(String value) {
this.validityPeriod = value;
}
public String getScheduleDeliveryTime() {
return this.scheduleDeliveryTime;
}
public void setScheduleDeliveryTime(String value) {
this.scheduleDeliveryTime = value;
}
public byte getPriority() {
return this.priority;
}
public void setPriority(byte value) {
this.priority = value;
}
public byte getEsmClass() {
return this.esmClass;
}
public void setEsmClass(byte value) {
this.esmClass = value;
}
public byte getProtocolId() {
return this.protocolId;
}
public void setProtocolId(byte value) {
this.protocolId = value;
}
public String getServiceType() {
return this.serviceType;
}
public void setServiceType(String value) {
this.serviceType = value;
}
public Address getSourceAddress() {
return this.sourceAddress;
}
public void setSourceAddress(Address value) {
this.sourceAddress = value;
}
public Address getDestAddress() {
return this.destAddress;
}
public void setDestAddress(Address value) {
this.destAddress = value;
}
@Override
public void readBody(ChannelBuffer buffer) throws UnrecoverablePduException, RecoverablePduException {
this.serviceType = ChannelBufferUtil.readNullTerminatedString(buffer);
this.sourceAddress = ChannelBufferUtil.readAddress(buffer);
this.destAddress = ChannelBufferUtil.readAddress(buffer);
this.esmClass = buffer.readByte();
this.protocolId = buffer.readByte();
this.priority = buffer.readByte();
this.scheduleDeliveryTime = ChannelBufferUtil.readNullTerminatedString(buffer);
this.validityPeriod = ChannelBufferUtil.readNullTerminatedString(buffer);
this.registeredDelivery = buffer.readByte();
this.replaceIfPresent = buffer.readByte();
this.dataCoding = buffer.readByte();
this.defaultMsgId = buffer.readByte();
// this is always an unsigned version of the short message length
short shortMessageLength = buffer.readUnsignedByte();
this.shortMessage = new byte[shortMessageLength];
buffer.readBytes(this.shortMessage);
}
@Override
public int calculateByteSizeOfBody() {
int bodyLength = 0;
bodyLength += PduUtil.calculateByteSizeOfNullTerminatedString(this.serviceType);
bodyLength += PduUtil.calculateByteSizeOfAddress(this.sourceAddress);
bodyLength += PduUtil.calculateByteSizeOfAddress(this.destAddress);
bodyLength += 3; // esmClass, priority, protocolId
bodyLength += PduUtil.calculateByteSizeOfNullTerminatedString(this.scheduleDeliveryTime);
bodyLength += PduUtil.calculateByteSizeOfNullTerminatedString(this.validityPeriod);
bodyLength += 5; // regDelivery, replace, dataCoding, defaultMsgId, messageLength bytes
bodyLength += getShortMessageLength();
return bodyLength;
}
@Override
public void writeBody(ChannelBuffer buffer) throws UnrecoverablePduException, RecoverablePduException {
ChannelBufferUtil.writeNullTerminatedString(buffer, this.serviceType);
ChannelBufferUtil.writeAddress(buffer, this.sourceAddress);
ChannelBufferUtil.writeAddress(buffer, this.destAddress);
buffer.writeByte(this.esmClass);
buffer.writeByte(this.protocolId);
buffer.writeByte(this.priority);
ChannelBufferUtil.writeNullTerminatedString(buffer, this.scheduleDeliveryTime);
ChannelBufferUtil.writeNullTerminatedString(buffer, this.validityPeriod);
buffer.writeByte(this.registeredDelivery);
buffer.writeByte(this.replaceIfPresent);
buffer.writeByte(this.dataCoding);
buffer.writeByte(this.defaultMsgId);
buffer.writeByte((byte)getShortMessageLength());
if (this.shortMessage != null) {
buffer.writeBytes(this.shortMessage);
}
}
@Override
public void appendBodyToString(StringBuilder buffer) {
buffer.append("(serviceType [");
buffer.append(StringUtil.toStringWithNullAsEmpty(this.serviceType));
buffer.append("] sourceAddr [");
buffer.append(StringUtil.toStringWithNullAsEmpty(this.sourceAddress));
buffer.append("] destAddr [");
buffer.append(StringUtil.toStringWithNullAsEmpty(this.destAddress));
buffer.append("] esmCls [0x");
buffer.append(HexUtil.toHexString(this.esmClass));
buffer.append("] regDlvry [0x");
buffer.append(HexUtil.toHexString(this.registeredDelivery));
// NOTE: skipped protocolId, priority, scheduledDlvryTime, validityPeriod,replace and defaultMsgId
buffer.append("] dcs [0x");
buffer.append(HexUtil.toHexString(this.dataCoding));
buffer.append("] message [");
HexUtil.appendHexString(buffer, this.shortMessage);
buffer.append("])");
}
}