forked from ringcentral/pubnub-jtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPubNub.java
More file actions
606 lines (502 loc) · 21 KB
/
Copy pathPubNub.java
File metadata and controls
606 lines (502 loc) · 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
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
package com.pubnub.api;
import com.pubnub.api.builder.PresenceBuilder;
import com.pubnub.api.builder.PubNubErrorBuilder;
import com.pubnub.api.builder.SubscribeBuilder;
import com.pubnub.api.builder.UnsubscribeBuilder;
import com.pubnub.api.callbacks.SubscribeCallback;
import com.pubnub.api.endpoints.DeleteMessages;
import com.pubnub.api.endpoints.FetchMessages;
import com.pubnub.api.endpoints.History;
import com.pubnub.api.endpoints.MessageCounts;
import com.pubnub.api.endpoints.Time;
import com.pubnub.api.endpoints.access.Grant;
import com.pubnub.api.endpoints.access.GrantToken;
import com.pubnub.api.endpoints.channel_groups.AddChannelChannelGroup;
import com.pubnub.api.endpoints.channel_groups.AllChannelsChannelGroup;
import com.pubnub.api.endpoints.channel_groups.DeleteChannelGroup;
import com.pubnub.api.endpoints.channel_groups.ListAllChannelGroup;
import com.pubnub.api.endpoints.channel_groups.RemoveChannelChannelGroup;
import com.pubnub.api.endpoints.files.DeleteFile;
import com.pubnub.api.endpoints.files.DownloadFile;
import com.pubnub.api.endpoints.files.GetFileUrl;
import com.pubnub.api.endpoints.files.ListFiles;
import com.pubnub.api.endpoints.files.PublishFileMessage;
import com.pubnub.api.endpoints.files.SendFile;
import com.pubnub.api.endpoints.message_actions.AddMessageAction;
import com.pubnub.api.endpoints.message_actions.GetMessageActions;
import com.pubnub.api.endpoints.message_actions.RemoveMessageAction;
import com.pubnub.api.endpoints.objects_api.channel.GetAllChannelsMetadata;
import com.pubnub.api.endpoints.objects_api.channel.GetChannelMetadata;
import com.pubnub.api.endpoints.objects_api.channel.RemoveChannelMetadata;
import com.pubnub.api.endpoints.objects_api.channel.SetChannelMetadata;
import com.pubnub.api.endpoints.objects_api.members.GetChannelMembers;
import com.pubnub.api.endpoints.objects_api.members.ManageChannelMembers;
import com.pubnub.api.endpoints.objects_api.members.RemoveChannelMembers;
import com.pubnub.api.endpoints.objects_api.members.SetChannelMembers;
import com.pubnub.api.endpoints.objects_api.memberships.GetMemberships;
import com.pubnub.api.endpoints.objects_api.memberships.ManageMemberships;
import com.pubnub.api.endpoints.objects_api.memberships.RemoveMemberships;
import com.pubnub.api.endpoints.objects_api.memberships.SetMemberships;
import com.pubnub.api.endpoints.objects_api.uuid.GetAllUUIDMetadata;
import com.pubnub.api.endpoints.objects_api.uuid.GetUUIDMetadata;
import com.pubnub.api.endpoints.objects_api.uuid.RemoveUUIDMetadata;
import com.pubnub.api.endpoints.objects_api.uuid.SetUUIDMetadata;
import com.pubnub.api.endpoints.presence.GetState;
import com.pubnub.api.endpoints.presence.HereNow;
import com.pubnub.api.endpoints.presence.SetState;
import com.pubnub.api.endpoints.presence.WhereNow;
import com.pubnub.api.endpoints.pubsub.Publish;
import com.pubnub.api.endpoints.pubsub.Signal;
import com.pubnub.api.endpoints.push.AddChannelsToPush;
import com.pubnub.api.endpoints.push.ListPushProvisions;
import com.pubnub.api.endpoints.push.RemoveAllPushChannelsForDevice;
import com.pubnub.api.endpoints.push.RemoveChannelsFromPush;
import com.pubnub.api.managers.BasePathManager;
import com.pubnub.api.managers.DelayedReconnectionManager;
import com.pubnub.api.managers.DuplicationManager;
import com.pubnub.api.managers.ListenerManager;
import com.pubnub.api.managers.MapperManager;
import com.pubnub.api.managers.PublishSequenceManager;
import com.pubnub.api.managers.ReconnectionManager;
import com.pubnub.api.managers.RetrofitManager;
import com.pubnub.api.managers.StateManager;
import com.pubnub.api.managers.SubscriptionManager;
import com.pubnub.api.managers.TelemetryManager;
import com.pubnub.api.managers.token_manager.TokenManager;
import com.pubnub.api.managers.token_manager.TokenParser;
import com.pubnub.api.models.consumer.access_manager.v3.PNToken;
import com.pubnub.api.vendor.Crypto;
import com.pubnub.api.vendor.FileEncryptionUtil;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;
public class PubNub {
@Getter
private @NotNull PNConfiguration configuration;
@Getter
private @NotNull MapperManager mapper;
private String instanceId;
private SubscriptionManager subscriptionManager;
private BasePathManager basePathManager;
private PublishSequenceManager publishSequenceManager;
private TelemetryManager telemetryManager;
private RetrofitManager retrofitManager;
private final TokenParser tokenParser;
private static final int TIMESTAMP_DIVIDER = 1000;
private static final int MAX_SEQUENCE = 65535;
private static final String SDK_VERSION = "5.2.0";
private final ListenerManager listenerManager;
private final StateManager stateManager;
private final TokenManager tokenManager;
public PubNub(@NotNull PNConfiguration initialConfig) {
this.configuration = initialConfig;
this.mapper = new MapperManager();
this.telemetryManager = new TelemetryManager();
this.basePathManager = new BasePathManager(initialConfig);
this.retrofitManager = new RetrofitManager(this);
this.listenerManager = new ListenerManager(this);
this.stateManager = new StateManager(this.configuration);
this.tokenManager = new TokenManager();
final ReconnectionManager reconnectionManager = new ReconnectionManager(this);
final DelayedReconnectionManager delayedReconnectionManager = new DelayedReconnectionManager(this);
final DuplicationManager duplicationManager = new DuplicationManager(this.configuration);
this.subscriptionManager = new SubscriptionManager(this,
retrofitManager,
this.telemetryManager,
stateManager,
listenerManager,
reconnectionManager,
delayedReconnectionManager,
duplicationManager,
tokenManager);
this.publishSequenceManager = new PublishSequenceManager(MAX_SEQUENCE);
this.tokenParser = new TokenParser();
instanceId = UUID.randomUUID().toString();
}
@NotNull
public String getBaseUrl() {
return this.basePathManager.getBasePath();
}
public void addListener(@NotNull SubscribeCallback listener) {
listenerManager.addListener(listener);
}
public void removeListener(@NotNull SubscribeCallback listener) {
listenerManager.removeListener(listener);
}
@NotNull
public SubscribeBuilder subscribe() {
return new SubscribeBuilder(this.subscriptionManager);
}
@NotNull
public UnsubscribeBuilder unsubscribe() {
return new UnsubscribeBuilder(this.subscriptionManager);
}
@NotNull
public PresenceBuilder presence() {
return new PresenceBuilder(this.subscriptionManager);
}
// start push
@NotNull
public AddChannelsToPush addPushNotificationsOnChannels() {
return new AddChannelsToPush(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public RemoveChannelsFromPush removePushNotificationsFromChannels() {
return new RemoveChannelsFromPush(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public RemoveAllPushChannelsForDevice removeAllPushNotificationsFromDeviceWithPushToken() {
return new RemoveAllPushChannelsForDevice(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public ListPushProvisions auditPushChannelProvisions() {
return new ListPushProvisions(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
// end push
@NotNull
public WhereNow whereNow() {
return new WhereNow(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public HereNow hereNow() {
return new HereNow(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public Time time() {
return new Time(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public History history() {
return new History(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public FetchMessages fetchMessages() {
return new FetchMessages(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public DeleteMessages deleteMessages() {
return new DeleteMessages(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public MessageCounts messageCounts() {
return new MessageCounts(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public Grant grant() {
return new Grant(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public GrantToken grantToken() {
return new GrantToken(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public GetState getPresenceState() {
return new GetState(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public SetState setPresenceState() {
return new SetState(this, subscriptionManager, this.telemetryManager, this.retrofitManager, tokenManager);
}
@NotNull
public Publish publish() {
return new Publish(this, publishSequenceManager, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public Signal signal() {
return new Signal(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public ListAllChannelGroup listAllChannelGroups() {
return new ListAllChannelGroup(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public AllChannelsChannelGroup listChannelsForChannelGroup() {
return new AllChannelsChannelGroup(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public AddChannelChannelGroup addChannelsToChannelGroup() {
return new AddChannelChannelGroup(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public RemoveChannelChannelGroup removeChannelsFromChannelGroup() {
return new RemoveChannelChannelGroup(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public DeleteChannelGroup deleteChannelGroup() {
return new DeleteChannelGroup(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
// Start Objects API
public SetUUIDMetadata setUUIDMetadata() {
return SetUUIDMetadata.create(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public GetAllUUIDMetadata getAllUUIDMetadata() {
return GetAllUUIDMetadata.create(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public GetUUIDMetadata getUUIDMetadata() {
return GetUUIDMetadata.create(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public RemoveUUIDMetadata removeUUIDMetadata() {
return new RemoveUUIDMetadata(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
public SetChannelMetadata.Builder setChannelMetadata() {
return SetChannelMetadata.builder(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public GetAllChannelsMetadata getAllChannelsMetadata() {
return GetAllChannelsMetadata.create(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public GetChannelMetadata.Builder getChannelMetadata() {
return GetChannelMetadata.builder(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
public RemoveChannelMetadata.Builder removeChannelMetadata() {
return RemoveChannelMetadata.builder(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public GetMemberships getMemberships() {
return GetMemberships.create(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public SetMemberships.Builder setMemberships() {
return SetMemberships.builder(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public RemoveMemberships.Builder removeMemberships() {
return RemoveMemberships.builder(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public ManageMemberships.Builder manageMemberships() {
return ManageMemberships.builder(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public GetChannelMembers.Builder getChannelMembers() {
return GetChannelMembers.builder(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public SetChannelMembers.Builder setChannelMembers() {
return SetChannelMembers.builder(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public RemoveChannelMembers.Builder removeChannelMembers() {
return RemoveChannelMembers.builder(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public ManageChannelMembers.Builder manageChannelMembers() {
return ManageChannelMembers.builder(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
// End Objects API
// Start Message Actions API
@NotNull
public AddMessageAction addMessageAction() {
return new AddMessageAction(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public GetMessageActions getMessageActions() {
return new GetMessageActions(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
@NotNull
public RemoveMessageAction removeMessageAction() {
return new RemoveMessageAction(this, this.telemetryManager, this.retrofitManager, this.tokenManager);
}
// End Message Actions API
@NotNull
public SendFile.Builder sendFile() {
return SendFile.builder(this, telemetryManager, retrofitManager, tokenManager);
}
public ListFiles.Builder listFiles() {
return new ListFiles.Builder(this, telemetryManager, retrofitManager, tokenManager);
}
public GetFileUrl.Builder getFileUrl() {
return GetFileUrl.builder(
this,
telemetryManager,
retrofitManager,
tokenManager);
}
public DownloadFile.Builder downloadFile() {
return DownloadFile.builder(
this,
telemetryManager,
retrofitManager,
tokenManager);
}
public DeleteFile.Builder deleteFile() {
return DeleteFile.builder(
this,
telemetryManager,
retrofitManager,
tokenManager);
}
public PublishFileMessage.Builder publishFileMessage() {
return PublishFileMessage.builder(
this,
telemetryManager,
retrofitManager,
tokenManager);
}
// public methods
/**
* Perform Cryptographic decryption of an input string using cipher key provided by PNConfiguration
*
* @param inputString String to be encrypted
* @return String containing the encryption of inputString using cipherKey
*/
@Nullable
public String decrypt(String inputString) throws PubNubException {
if (inputString == null) {
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_INVALID_ARGUMENTS).build();
}
return decrypt(inputString, this.getConfiguration().getCipherKey());
}
/**
* Perform Cryptographic decryption of an input string using the cipher key
*
* @param inputString String to be encrypted
* @param cipherKey cipher key to be used for encryption
* @return String containing the encryption of inputString using cipherKey
* @throws PubNubException throws exception in case of failed encryption
*/
@Nullable
public String decrypt(String inputString, String cipherKey) throws PubNubException {
if (inputString == null) {
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_INVALID_ARGUMENTS).build();
}
boolean dynamicIV = this.getConfiguration().isUseRandomInitializationVector();
return new Crypto(cipherKey, dynamicIV).decrypt(inputString);
}
public InputStream decryptInputStream(InputStream inputStream) throws PubNubException {
return decryptInputStream(inputStream, this.getConfiguration().getCipherKey());
}
public InputStream decryptInputStream(InputStream inputStream, String cipherKey) throws PubNubException {
return FileEncryptionUtil.decrypt(cipherKey, inputStream);
}
/**
* Perform Cryptographic encryption of an input string and the cipher key provided by PNConfiguration
*
* @param inputString String to be encrypted
* @return String containing the encryption of inputString using cipherKey
*/
@Nullable
public String encrypt(String inputString) throws PubNubException {
if (inputString == null) {
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_INVALID_ARGUMENTS).build();
}
return encrypt(inputString, this.getConfiguration().getCipherKey());
}
/**
* Perform Cryptographic encryption of an input string and the cipher key.
*
* @param inputString String to be encrypted
* @param cipherKey cipher key to be used for encryption
* @return String containing the encryption of inputString using cipherKey
* @throws PubNubException throws exception in case of failed encryption
*/
@Nullable
public String encrypt(String inputString, String cipherKey) throws PubNubException {
if (inputString == null) {
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_INVALID_ARGUMENTS).build();
}
boolean dynamicIV = this.getConfiguration().isUseRandomInitializationVector();
return new Crypto(cipherKey, dynamicIV).encrypt(inputString);
}
public InputStream encryptInputStream(InputStream inputStream) throws PubNubException {
return encryptInputStream(inputStream, this.getConfiguration().getCipherKey());
}
public InputStream encryptInputStream(InputStream inputStream, String cipherKey) throws PubNubException {
return FileEncryptionUtil.encrypt(cipherKey, inputStream);
}
public int getTimestamp() {
return (int) ((new Date().getTime()) / TIMESTAMP_DIVIDER);
}
/**
* @return instance uuid.
*/
@NotNull
public String getInstanceId() {
return instanceId;
}
/**
* @return request uuid.
*/
@NotNull
public String getRequestId() {
return UUID.randomUUID().toString();
}
/**
* @return version of the SDK.
*/
@NotNull
public String getVersion() {
return SDK_VERSION;
}
/**
* Stop the SDK and terminate all listeners.
*/
@Deprecated
public void stop() {
subscriptionManager.stop();
}
/**
* Destroy the SDK to cancel all ongoing requests and stop heartbeat timer.
*/
public void destroy() {
try {
subscriptionManager.destroy(false);
retrofitManager.destroy(false);
} catch (Exception error) {
//
}
}
/**
* Force destroy the SDK to evict the connection pools and close executors.
*/
public void forceDestroy() {
try {
subscriptionManager.destroy(true);
retrofitManager.destroy(true);
telemetryManager.stopCleanUpTimer();
} catch (Exception error) {
//
}
}
/**
* Perform a Reconnect to the network
*/
public void reconnect() {
subscriptionManager.reconnect();
}
/**
* Perform a disconnect from the listeners
*/
public void disconnect() {
subscriptionManager.disconnect();
}
@NotNull
public Publish fire() {
return publish().shouldStore(false).replicate(false);
}
@NotNull
public List<String> getSubscribedChannels() {
return stateManager.subscriptionStateData(false).getChannels();
}
@NotNull
public List<String> getSubscribedChannelGroups() {
return this.stateManager.subscriptionStateData(false).getChannelGroups();
}
public void unsubscribeAll() {
subscriptionManager.unsubscribeAll();
}
public PNToken parseToken(String token) throws PubNubException {
return tokenParser.unwrapToken(token);
}
public void setToken(String token) {
tokenManager.setToken(token);
}
}