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
534 lines (436 loc) · 16.6 KB
/
Copy pathPubNub.java
File metadata and controls
534 lines (436 loc) · 16.6 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
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.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.members.GetMembers;
import com.pubnub.api.endpoints.objects_api.members.ManageMembers;
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.spaces.CreateSpace;
import com.pubnub.api.endpoints.objects_api.spaces.DeleteSpace;
import com.pubnub.api.endpoints.objects_api.spaces.GetSpace;
import com.pubnub.api.endpoints.objects_api.spaces.GetSpaces;
import com.pubnub.api.endpoints.objects_api.spaces.UpdateSpace;
import com.pubnub.api.endpoints.objects_api.users.CreateUser;
import com.pubnub.api.endpoints.objects_api.users.DeleteUser;
import com.pubnub.api.endpoints.objects_api.users.GetUser;
import com.pubnub.api.endpoints.objects_api.users.GetUsers;
import com.pubnub.api.endpoints.objects_api.users.UpdateUser;
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.MapperManager;
import com.pubnub.api.managers.PublishSequenceManager;
import com.pubnub.api.managers.RetrofitManager;
import com.pubnub.api.managers.SubscriptionManager;
import com.pubnub.api.managers.TelemetryManager;
import com.pubnub.api.managers.token_manager.PNResourceType;
import com.pubnub.api.managers.token_manager.TokenManager;
import com.pubnub.api.managers.token_manager.TokenManagerProperties;
import com.pubnub.api.vendor.Crypto;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import lombok.Getter;
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 TokenManager tokenManager;
private static final int TIMESTAMP_DIVIDER = 1000;
private static final int MAX_SEQUENCE = 65535;
private static final String SDK_VERSION = "4.31.1";
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.tokenManager = new TokenManager();
this.subscriptionManager = new SubscriptionManager(this, retrofitManager, this.telemetryManager);
this.publishSequenceManager = new PublishSequenceManager(MAX_SEQUENCE);
instanceId = UUID.randomUUID().toString();
}
@NotNull
public String getBaseUrl() {
return this.basePathManager.getBasePath();
}
public void addListener(@NotNull SubscribeCallback listener) {
subscriptionManager.addListener(listener);
}
public void removeListener(@NotNull SubscribeCallback listener) {
subscriptionManager.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);
}
@NotNull
public RemoveChannelsFromPush removePushNotificationsFromChannels() {
return new RemoveChannelsFromPush(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public RemoveAllPushChannelsForDevice removeAllPushNotificationsFromDeviceWithPushToken() {
return new RemoveAllPushChannelsForDevice(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public ListPushProvisions auditPushChannelProvisions() {
return new ListPushProvisions(this, this.telemetryManager, this.retrofitManager);
}
// end push
@NotNull
public WhereNow whereNow() {
return new WhereNow(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public HereNow hereNow() {
return new HereNow(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public Time time() {
return new Time(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public History history() {
return new History(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public FetchMessages fetchMessages() {
return new FetchMessages(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public DeleteMessages deleteMessages() {
return new DeleteMessages(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public MessageCounts messageCounts() {
return new MessageCounts(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public Grant grant() {
return new Grant(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public GrantToken grantToken() {
return new GrantToken(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public GetState getPresenceState() {
return new GetState(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public SetState setPresenceState() {
return new SetState(this, subscriptionManager, this.telemetryManager, this.retrofitManager);
}
@NotNull
public Publish publish() {
return new Publish(this, publishSequenceManager, this.telemetryManager, this.retrofitManager);
}
@NotNull
public Signal signal() {
return new Signal(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public ListAllChannelGroup listAllChannelGroups() {
return new ListAllChannelGroup(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public AllChannelsChannelGroup listChannelsForChannelGroup() {
return new AllChannelsChannelGroup(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public AddChannelChannelGroup addChannelsToChannelGroup() {
return new AddChannelChannelGroup(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public RemoveChannelChannelGroup removeChannelsFromChannelGroup() {
return new RemoveChannelChannelGroup(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public DeleteChannelGroup deleteChannelGroup() {
return new DeleteChannelGroup(this, this.telemetryManager, this.retrofitManager);
}
// Start Objects API
@NotNull
public GetUsers getUsers() {
return new GetUsers(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public GetUser getUser() {
return new GetUser(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public CreateUser createUser() {
return new CreateUser(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public UpdateUser updateUser() {
return new UpdateUser(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public DeleteUser deleteUser() {
return new DeleteUser(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public GetSpaces getSpaces() {
return new GetSpaces(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public GetSpace getSpace() {
return new GetSpace(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public CreateSpace createSpace() {
return new CreateSpace(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public UpdateSpace updateSpace() {
return new UpdateSpace(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public DeleteSpace deleteSpace() {
return new DeleteSpace(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public GetMemberships getMemberships() {
return new GetMemberships(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public GetMembers getMembers() {
return new GetMembers(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public ManageMemberships manageMemberships() {
return new ManageMemberships(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public ManageMembers manageMembers() {
return new ManageMembers(this, this.telemetryManager, this.retrofitManager);
}
// End Objects API
// Start Message Actions API
@NotNull
public AddMessageAction addMessageAction() {
return new AddMessageAction(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public GetMessageActions getMessageActions() {
return new GetMessageActions(this, this.telemetryManager, this.retrofitManager);
}
@NotNull
public RemoveMessageAction removeMessageAction() {
return new RemoveMessageAction(this, this.telemetryManager, this.retrofitManager);
}
// End Message Actions API
// 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();
}
return new Crypto(cipherKey).decrypt(inputString);
}
/**
* 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();
}
return new Crypto(cipherKey).encrypt(inputString);
}
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 subscriptionManager.getSubscribedChannels();
}
@NotNull
public List<String> getSubscribedChannelGroups() {
return subscriptionManager.getSubscribedChannelGroups();
}
public void unsubscribeAll() {
subscriptionManager.unsubscribeAll();
}
public void setToken(String token) throws PubNubException {
tokenManager.setToken(token);
}
public void setTokens(@NotNull List<String> tokens) throws PubNubException {
tokenManager.setTokens(tokens);
}
@Nullable
public String getToken(@NotNull String resourceId, @NotNull PNResourceType resourceType) {
return tokenManager.getToken(TokenManagerProperties.builder()
.resourceId(resourceId)
.pnResourceType(resourceType)
.build());
}
@Nullable
public String getToken(@NotNull TokenManagerProperties tokenManagerProperties) {
return tokenManager.getToken(tokenManagerProperties);
}
@NotNull
public HashMap<String, HashMap<String, HashMap<String, String>>> getTokens() {
return tokenManager.getTokens();
}
@NotNull
public HashMap<String, HashMap<String, String>> getTokensByResource(@NotNull PNResourceType resourceType) {
return tokenManager.getTokensByResource(resourceType);
}
}