Repository navigation
Expand file tree
/
Copy pathPubnub.java
More file actions
701 lines (622 loc) · 21.5 KB
/
Copy pathPubnub.java
File metadata and controls
701 lines (622 loc) · 21.5 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
package pubnub;
import com.ning.http.client.*;
import com.ning.http.client.AsyncHttpClientConfig.Builder;
import org.json.JSONArray;
import org.json.JSONObject;
import pubnub.crypto.PubnubCrypto;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.Future;
import java.util.zip.GZIPInputStream;
/**
* PubNub 3.1 Real-time Push Cloud API
*
* @author Stephen Blum
* @package pubnub
*/
public class Pubnub {
private String ORIGIN = "pubsub.pubnub.com";
private String PUBLISH_KEY = "";
private String SUBSCRIBE_KEY = "";
private String SECRET_KEY = "";
private String CIPHER_KEY = "";
private boolean SSL = false;
private class ChannelStatus {
String channel;
boolean connected, first;
}
private List<ChannelStatus> subscriptions;
/**
* PubNub 3.1 with Cipher Key
*
* Prepare PubNub State.
*
* @param String Publish Key.
* @param String Subscribe Key.
* @param String Secret Key.
* @param String Cipher Key.
* @param boolean SSL Enabled.
*/
public Pubnub(
String publish_key,
String subscribe_key,
String secret_key,
String cipher_key,
boolean ssl_on
) {
this.init( publish_key, subscribe_key, secret_key, cipher_key, ssl_on );
}
/**
* PubNub 3.0
*
* Prepare PubNub Class State.
*
* @param String Publish Key.
* @param String Subscribe Key.
* @param String Secret Key.
* @param boolean SSL Enabled.
*/
public Pubnub(
String publish_key,
String subscribe_key,
String secret_key,
boolean ssl_on
) {
this.init( publish_key, subscribe_key, secret_key, "", ssl_on );
}
/**
* PubNub 2.0 Compatibility
*
* Prepare PubNub Class State.
*
* @param String Publish Key.
* @param String Subscribe Key.
*/
public Pubnub(
String publish_key,
String subscribe_key
) {
this.init( publish_key, subscribe_key, "", "", false );
}
/**
* PubNub 3.0 without SSL
*
* Prepare PubNub Class State.
*
* @param String Publish Key.
* @param String Subscribe Key.
* @param String Secret Key.
*/
public Pubnub(
String publish_key,
String subscribe_key,
String secret_key
) {
this.init( publish_key, subscribe_key, secret_key, "", false );
}
/**
* Init
*
* Prepare PubNub Class State.
*
* @param String Publish Key.
* @param String Subscribe Key.
* @param String Secret Key.
* @param String Cipher Key.
* @param boolean SSL Enabled.
*/
public void init(
String publish_key,
String subscribe_key,
String secret_key,
String cipher_key,
boolean ssl_on
) {
this.PUBLISH_KEY = publish_key;
this.SUBSCRIBE_KEY = subscribe_key;
this.SECRET_KEY = secret_key;
this.CIPHER_KEY = cipher_key;
this.SSL = ssl_on;
// SSL On?
if (this.SSL) {
this.ORIGIN = "https://" + this.ORIGIN;
}
else {
this.ORIGIN = "http://" + this.ORIGIN;
}
}
/**
* Publish
*
* Send a message to a channel.
*
* @param String channel name.
* @param JSONObject message.
* @return JSONArray.
*/
public JSONArray publish( String channel, JSONObject message ) {
HashMap<String, Object> args = new HashMap<String, Object>(2);
args.put("channel", channel);
args.put("message", message);
return publish( args );
}
/**
* Publish
*
* Send a message to a channel.
*
* @param HashMap<String, Object> containing channel name, message.
* @return JSONArray.
*/
public JSONArray publish( HashMap<String, Object> args ) {
String channel = (String) args.get("channel");
Object message= args.get("message");
if(message instanceof JSONObject) {
JSONObject obj=(JSONObject)message;
if(this.CIPHER_KEY.length() > 0) {
// Encrypt Message
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
message = pc.encrypt(obj);
} else {
message=obj;
}
//System.out.println();
} else if(message instanceof String) {
String obj=(String)message;
if(this.CIPHER_KEY.length() > 0) {
// Encrypt Message
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
try {
message = pc.encrypt(obj);
} catch (Exception e) {
e.printStackTrace();
}
} else {
message=obj;
}
message="\""+message+"\"";
}else if(message instanceof JSONArray) {
JSONArray obj=(JSONArray)message;
if(this.CIPHER_KEY.length() > 0) {
// Encrypt Message
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
message = pc.encryptJSONArray(obj);
} else {
message=obj;
}
System.out.println();
}
// Generate String to Sign
String signature = "0";
if (this.SECRET_KEY.length() > 0) {
StringBuilder string_to_sign = new StringBuilder();
string_to_sign
.append(this.PUBLISH_KEY)
.append('/')
.append(this.SUBSCRIBE_KEY)
.append('/')
.append(this.SECRET_KEY)
.append('/')
.append(channel)
.append('/')
.append(message.toString());
// Sign Message
signature = PubnubCrypto.getHMacSHA256(this.SECRET_KEY, string_to_sign.toString());
}
// Build URL
List<String> url = new ArrayList<String>();
url.add("publish");
url.add(this.PUBLISH_KEY);
url.add(this.SUBSCRIBE_KEY);
url.add(signature);
url.add(channel);
url.add("0");
url.add(message.toString());
return _request(url);
}
/**
* Subscribe
*
* Listen for a message on a channel.
*
* @param String channel name.
* @param Callback function callback.
*/
public void subscribe( String channel, Callback callback ) {
HashMap<String, Object> args = new HashMap<String, Object>(2);
args.put("channel", channel);
args.put("callback", callback);
subscribe( args );
}
/**
* Subscribe
*
* Listen for a message on a channel.
*
* @param HashMap<String, Object> containing channel name, function callback.
*/
public void subscribe( HashMap<String, Object> args ) {
args.put("timetoken", "0");
this._subscribe( args );
}
/**
* Subscribe - Private Interface
*
* Patch provided by petereddy on GitHub
*
* @param HashMap<String, Object> containing channel name, function callback, timetoken.
*/
private void _subscribe( HashMap<String, Object> args ) {
String channel = (String) args.get("channel");
String timetoken = (String) args.get("timetoken");
Callback callback;
// Validate Arguments
if (args.get("callback") != null) {
callback = (Callback) args.get("callback");
} else {
System.out.println("Invalid Callback.");
return;
}
if (channel == null || channel.equals("")) {
callback.errorCallback(channel,"Invalid Channel.");
return;
}
// Ensure Single Connection
if (subscriptions != null && subscriptions.size() > 0) {
boolean channel_exist = false;
for (ChannelStatus it : subscriptions) {
if(it.channel.equals(channel)) {
channel_exist = true;
break;
}
}
if (!channel_exist) {
ChannelStatus cs = new ChannelStatus();
cs.channel = channel;
cs.connected = true;
subscriptions.add(cs);
} else {
callback.errorCallback(channel,"Already Connected");
return;
}
} else {
// New Channel
ChannelStatus cs = new ChannelStatus();
cs.channel = channel;
cs.connected = true;
subscriptions = new ArrayList<Pubnub.ChannelStatus>();
subscriptions.add(cs);
}
while (true) {
try {
// Build URL
List<String> url = java.util.Arrays.asList(
"subscribe", this.SUBSCRIBE_KEY, channel, "0", timetoken
);
// Stop Connection?
boolean is_disconnect = false;
for (ChannelStatus it : subscriptions) {
if (it.channel.equals(channel)) {
if(!it.connected) {
subscriptions.remove(it);
callback.disconnectCallback(channel);
is_disconnect = true;
break;
}
}
}
if (is_disconnect)
return;
// Wait for Message
JSONArray response = _request(url);
// Stop Connection?
for (ChannelStatus it : subscriptions) {
if (it.channel.equals(channel)) {
if (!it.connected) {
subscriptions.remove(it);
callback.disconnectCallback(channel);
is_disconnect = true;
break;
}
}
}
if (is_disconnect)
return;
// Problem?
if (response == null || response.optInt(1) == 0) {
for (ChannelStatus it : subscriptions) {
if (it.channel.equals(channel)) {
if(it.connected && it.first){
subscriptions.remove(it);
callback.disconnectCallback(channel);
}else{
subscriptions.remove(it);
callback.errorCallback(channel,"Lost Network Connection");
}
}
}
// Ensure Connected (Call Time Function)
boolean is_reconnected = false;
while(true) {
double time_token = this.time();
if (time_token == 0.0) {
Thread.sleep(5000);
} else {
// Reconnect Callback
callback.reconnectCallback(channel);
//this._subscribe(args);
is_reconnected = true;
break;
}
}
if(is_reconnected) {
continue;
}
} else {
for (ChannelStatus it : subscriptions) {
if (it.channel.equals(channel)) {
// Connect Callback
if (!it.first) {
it.first = true;
callback.connectCallback(channel);
break;
}
}
}
}
JSONArray messages = response.optJSONArray(0);
// Update TimeToken
if (response.optString(1).length() > 0)
timetoken = response.optString(1);
for ( int i = 0; messages.length() > i; i++ ) {
JSONObject message = messages.optJSONObject(i);
if(message != null) {
if(this.CIPHER_KEY.length() > 0){
// Decrypt Message
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
message = pc.decrypt(message);
}
if(callback !=null)
callback.subscribeCallback(channel, message);
} else {
JSONArray arr = messages.optJSONArray(i);
if(arr != null) {
if(this.CIPHER_KEY.length() > 0) {
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
arr=pc.decryptJSONArray(arr); ;
}
if(callback !=null)
callback.subscribeCallback(channel,arr);
} else {
String msgs=messages.getString(0);
if(this.CIPHER_KEY.length() > 0) {
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
msgs=pc.decrypt(msgs);
}
if(callback !=null)
callback.subscribeCallback(channel,msgs);
}
}
}
} catch (Exception e) {
try { Thread.sleep(1000); }
catch(InterruptedException ie) {}
}
}
}
/**
* History
*
* Load history from a channel.
*
* @param String channel name.
* @param int limit history count response.
* @return JSONArray of history.
*/
public JSONArray history( String channel, int limit ) {
HashMap<String, Object> args = new HashMap<String, Object>(2);
args.put("channel", channel);
args.put("limit", limit);
return history( args );
}
/**
* History
*
* Load history from a channel.
*
* @param HashMap<String, Object> containing channel name, limit history count response.
* @return JSONArray of history.
*/
public JSONArray history( HashMap<String, Object> args ) {
String channel = (String) args.get("channel");
int limit = Integer.parseInt(args.get("limit").toString());
List<String> url = new ArrayList<String>();
url.add("history");
url.add(this.SUBSCRIBE_KEY);
url.add(channel);
url.add("0");
url.add(Integer.toString(limit));
JSONArray response = _request(url);
if (this.CIPHER_KEY.length() > 0) {
// Decrpyt Messages
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
return pc.decryptJSONArray(response);
} else {
return response;
}
}
/**
* Time
*
* Timestamp from PubNub Cloud.
*
* @return double timestamp.
*/
public double time() {
List<String> url = new ArrayList<String>();
url.add("time");
url.add("0");
JSONArray response = _request(url);
return response.optDouble(0);
}
/**
* UUID
*
* 32 digit UUID generation at client side.
*
* @return String uuid.
*/
public static String uuid() {
UUID uuid = UUID.randomUUID();
return uuid.toString();
}
/**
* Unsubscribe
*
* Unsubscribe/Disconnect to channel.
*
* @param HashMap<String, Object> containing channel name.
*/
public void unsubscribe( HashMap<String, Object> args ) {
String channel = (String) args.get("channel");
for (ChannelStatus it : subscriptions) {
if(it.channel.equals(channel) && it.connected) {
it.connected = false;
it.first = false;
break;
}
}
}
/**
* Request URL
*
* @param List<String> request of url directories.
* @return JSONArray from JSON response.
*/
private JSONArray _request(List<String> url_components) {
String json = "";
StringBuilder url = new StringBuilder();
Iterator<String> url_iterator = url_components.iterator();
String request_for = url_components.get(0);
url.append(this.ORIGIN);
// Generate URL with UTF-8 Encoding
while (url_iterator.hasNext()) {
try {
String url_bit = (String) url_iterator.next();
url.append("/").append(_encodeURIcomponent(url_bit));
}
catch(Exception e) {
//e.printStackTrace();
JSONArray jsono = new JSONArray();
try { jsono.put("Failed UTF-8 Encoding URL."); }
catch (Exception jsone) {}
return jsono;
}
}
AsyncHttpClient ahc = null;
try {
// Prepare Asynchronous HTTP Request
Builder cb = new AsyncHttpClientConfig.Builder();
cb.setRequestTimeoutInMs(310000);
AsyncHttpClientConfig config = cb.build();
ahc = new AsyncHttpClient(config);
RequestBuilder rb = new RequestBuilder("GET");
rb.setUrl(url.toString());
rb.addHeader("V", "3.1");
rb.addHeader("User-Agent", "Java");
rb.addHeader("Accept-Encoding", "gzip");
Request request = rb.build();
//Execute Request
Future<String> f = ahc.executeRequest(request, new AsyncCompletionHandler<String>() {
@Override
public String onCompleted(Response r) throws Exception {
String ce = r.getHeader("Content-Encoding");
InputStream resulting_is = null;
InputStream is = r.getResponseBodyAsStream();
if (ce != null && ce.equalsIgnoreCase("gzip")) {
// Decoding using 'gzip'
try {
resulting_is = new GZIPInputStream(is);
}catch (IOException e) {
resulting_is = is;
}
catch (Exception e) {
resulting_is = is;
}
}
else {
// Default (encoding is null OR 'identity')
resulting_is = is;
}
String line = "", json = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(resulting_is));
// Read JSON Message
while ((line = reader.readLine()) != null) { json += line; }
reader.close();
return json;
}
});
json = f.get();
ahc.close();
} catch (Exception e) {
// Response If Failed JSONP HTTP Request.
JSONArray jsono = new JSONArray();
try {
if(request_for != null) {
if(request_for.equals("time")) {
jsono.put("0");
} else if(request_for.equals("history")) {
jsono.put("Error: Failed JSONP HTTP Request.");
} else if(request_for.equals("publish")) {
jsono.put("0");
jsono.put("Error: Failed JSONP HTTP Request.");
} else if(request_for.equals("subscribe")) {
jsono.put("0");
jsono.put("0");
}
}
}
catch (Exception jsone) {}
if(ahc != null) {
ahc.close();
}
return jsono;
}
// Parse JSON String
try { return new JSONArray(json); }
catch (Exception e) {
JSONArray jsono = new JSONArray();
try { jsono.put("Error: Failed JSON Parsing."); }
catch (Exception jsone) {}
// Return Failure to Parse
return jsono;
}
}
private String _encodeURIcomponent(String s) {
StringBuilder o = new StringBuilder();
for (Character ch : s.toCharArray()) {
if (isUnsafe(ch)) {
o.append('%');
o.append(toHex(ch / 16));
o.append(toHex(ch % 16));
}
else o.append(encodeToUTF8(ch.toString()));
}
return o.toString();
}
private char toHex(int ch) {
return (char)(ch < 10 ? '0' + ch : 'A' + ch - 10);
}
private boolean isUnsafe(char ch) {
return " ~`!@#$%^&*()+=[]\\{}|;':\",./<>?ɂ顶".indexOf(ch) >= 0;
}
private String encodeToUTF8(String s) {
try {
String enc = URLEncoder.encode(s, "UTF-8").replace("+", "%20");
return enc;
} catch (UnsupportedEncodingException e) {
}
return s;
}
}