-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathMessage.java
More file actions
336 lines (288 loc) · 7.21 KB
/
Copy pathMessage.java
File metadata and controls
336 lines (288 loc) · 7.21 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package pl.smsapi.message;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import pl.smsapi.SmsapiException;
import pl.smsapi.sender.Sender;
public abstract class Message implements MessageInterface{
private String username;
private String password;
private final ArrayList<String> to = new ArrayList<String>();
private final ArrayList<String> idx = new ArrayList<String>();
private String group;
private String date;
protected final HashMap<String, String> params = new HashMap<String, String>();
private Sender sender;
private String path;
public String getPath() {
return path;
}
protected void setPath(String path) {
this.path = path;
}
//---end--abstract method---------
@Override
public final HashMap getParams() {
return params;
}
@Override
public final String getUsername() {
return username;
}
/**
* Ustawia login potrzebny do autoryzacji w smsapi.pl
*
* @param username
*/
@Override
public final void setUsername(String username) {
this.username = username;
}
@Override
public final String getPassword() {
return password;
}
/**
* Ustawia hasło potrzebne do autoryzacji w smsapi.pl -wartość podać jako string zakodowany przez MD5
*
* @param password String
*/
public final void setPassword(String password) {
this.password = password;
}
/**
* Ustawia hasło potrzebne do autoryzacji w smsapi.pl -hasło można podać jako otwarte wtedy trzeba ustawić codeMD5
* na true
*
* @param password String
* @param codeMD5 bool
*/
public final void setPassword(String password, boolean encodeMD5) {
this.password = (encodeMD5 == true) ? MD5Digest(password) : password;
}
/**
* Zwraca listę numerów telefonów
*
* @return ArrayList
*/
public final ArrayList getTo() {
return to;
}
/**
* Zwraca listę indetyfikatorów
*
* @return ArrayList
*/
public final ArrayList getIdx() {
return idx;
}
/**
* Numer odbiorcy wiadomości w formacie 48xxxxxxxxx lub xxxxxxxxx
*
* @param to String
*/
public final void addTo(String to) {
this.to.add(to);
}
/**
* Numer odbiorcy wiadomości w formacie 48xxxxxxxxx lub xxxxxxxxx
*
* @param to long
*/
public final void addTo(long to) {
String tmp = Long.toString(to);
addTo(tmp);
}
/**
* Numery odbiorców wiadomości w formacie tablicy
*
* @param to String[]
*/
public final void addTo(String[] to) {
for (String item : to) {
addTo(item);
}
}
/**
* Opcjonalny parametr użytkownika wysyłany z wiadomością a następnie zwracany przy wywołaniu zwrotnym CALLBACK.
* Parametr idx może mieć maksymalnie 36 znaków dopuszczalne są cyfry 0 - 9 oraz litery a – z -wielkość liter nie
* jest rozróżniana
*
* @param to
* @param idx
*/
public final void addTo(String to, String idx) {
this.to.add(to);
this.idx.add(idx);
}
/**
* Zwraca parametr group
*
* @return String
*/
public final String getGroup() {
return group;
}
/**
* Nazwa grupy kontaktów z książki telefonicznej, do których ma zostać wysłana wiadomość -ustawienie tego parametru
* powoduje ze parametr "to" nie będzie brany pod uwagę
*
* @param group String
*/
public final void setGroup(String group) {
this.group = group;
}
/**
* Zwraca paramert date
*
* @return String
*/
public final String getDate() {
return date;
}
/**
* Określa kiedy wiadomość ma być wysłana. W przypadku wstawienia daty przeszłej wiadomość zostanie wysłana od razu.
* Wiadomość można zaplanować na maksymalnie 3 miesiące do przodu Data w formacie unixtime przekazana jako string
*
* @param date String
*/
public final void setDate(String date) {
this.date = date;
}
/**
* Data w formacie unixtime przekazana jako long
*
* @param date long
*/
public final void setDate(long date) {
Long time = date;
setDate(time.toString());
}
/**
* Data w formacie unixtime przekazana jako objekt typu Calendar
*
* @param cal GregorianCalendar
*/
public final void setDate(Calendar cal) {
long time = cal.getTimeInMillis() / 1000;
setDate(time);
}
/**
* Zwraca parametr test
*
* @return String
*/
public final String getTest() {
return params.get("test");
}
/**
* Wiadomość nie jest wysyłana, wyświetlana jest jedynie odpowiedź
*
* @param test
*/
public final void setTest(boolean test) {
if (test == true) {
params.put("test", "1");
} else if (test == false) {
params.remove("test");
}
}
@Override
public final Sender getSender() {
return sender;
}
@Override
public final void setSender(Sender sender) {
this.sender = sender;
}
public static String MD5Digest(String str) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes());
byte byteData[] = md.digest();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < byteData.length; i++) {
sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(Message.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
/**
* Uruchamia wysyłkę wiadomości w smsapi.pl
*
* @return bool
*/
@Override
public boolean send() {
if (sender == null) {
throw new SmsapiException("No exists sender");
}
sender.setMessage(this);
return sender.send();
}
/**
* Zwraca liste odpowiedzi po wywolaniu send();
*
* @return ArrayList Result
* @see Result
*/
public ArrayList<Result> getResults() {
if (sender == null) {
throw new SmsapiException("No exists sender");
}
return sender.getResults();
}
/**
* Zwraca parametr check_idx
*
* @return String
*/
public final String getCheckIdx() {
return params.get("check_idx");
}
/**
* Pozwala zabezpieczyć przed wysłanie dwóch wiadomości z identyczną wartością parametru idx. W przypadku ustawienia
* parametru (&check_idx=1) system sprawdza czy wiadomość z takim idx już została przyjęta jeśli tak zwracany jest
* błąd 53.
*
* @param checkIdx bool
*/
public final void setCheckIdx(boolean checkIdx) {
if (checkIdx == true) {
params.put("check_idx", "1");
} else if (checkIdx == false) {
params.remove("check_idx");
}
}
public final void setHttps(boolean https) {
if (sender == null) {
throw new SmsapiException("No exists sender");
}
if (https == true) {
sender.setProtocol("https");
} else {
sender.setProtocol("http");
}
}
private void cleanAllParams() {
group = null;
date = null;
to.clear();
idx.clear();
params.clear();
}
public final boolean deleteScheduled(String idMessage) {
if (idMessage.isEmpty()) {
throw new IllegalArgumentException("ID message not have to empty");
}
params.put("sch_del", idMessage);
return send();
}
}