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
306 lines (244 loc) · 9.87 KB
/
PubNub.java
File metadata and controls
306 lines (244 loc) · 9.87 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
package com.pubnub.api;
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.FetchMessages;
import com.pubnub.api.endpoints.History;
import com.pubnub.api.endpoints.Time;
import com.pubnub.api.endpoints.access.Audit;
import com.pubnub.api.endpoints.access.Grant;
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.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.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.vendor.Crypto;
import lombok.Getter;
import java.util.Date;
import java.util.List;
import java.util.UUID;
public class PubNub {
@Getter
private PNConfiguration configuration;
@Getter
private MapperManager mapper;
private String instanceId;
private SubscriptionManager subscriptionManager;
private BasePathManager basePathManager;
private PublishSequenceManager publishSequenceManager;
private RetrofitManager retrofitManager;
private static final int TIMESTAMP_DIVIDER = 1000;
private static final int MAX_SEQUENCE = 65535;
private static final String SDK_VERSION = "4.4.4";
public PubNub(PNConfiguration initialConfig) {
this.configuration = initialConfig;
this.mapper = new MapperManager();
this.basePathManager = new BasePathManager(initialConfig);
this.retrofitManager = new RetrofitManager(this);
this.subscriptionManager = new SubscriptionManager(this, retrofitManager);
this.publishSequenceManager = new PublishSequenceManager(MAX_SEQUENCE);
instanceId = UUID.randomUUID().toString();
}
public String getBaseUrl() {
return this.basePathManager.getBasePath();
}
//
public void addListener(SubscribeCallback listener) {
subscriptionManager.addListener(listener);
}
public void removeListener(SubscribeCallback listener) {
subscriptionManager.removeListener(listener);
}
public SubscribeBuilder subscribe() {
return new SubscribeBuilder(this.subscriptionManager);
}
public UnsubscribeBuilder unsubscribe() {
return new UnsubscribeBuilder(this.subscriptionManager);
}
// start push
public AddChannelsToPush addPushNotificationsOnChannels() {
return new AddChannelsToPush(this, this.retrofitManager.getTransactionInstance());
}
public RemoveChannelsFromPush removePushNotificationsFromChannels() {
return new RemoveChannelsFromPush(this, this.retrofitManager.getTransactionInstance());
}
public RemoveAllPushChannelsForDevice removeAllPushNotificationsFromDeviceWithPushToken() {
return new RemoveAllPushChannelsForDevice(this, this.retrofitManager.getTransactionInstance());
}
public ListPushProvisions auditPushChannelProvisions() {
return new ListPushProvisions(this, this.retrofitManager.getTransactionInstance());
}
// end push
public WhereNow whereNow() {
return new WhereNow(this, this.retrofitManager.getTransactionInstance());
}
public HereNow hereNow() {
return new HereNow(this, this.retrofitManager.getTransactionInstance());
}
public Time time() {
return new Time(this, this.retrofitManager.getTransactionInstance());
}
public History history() {
return new History(this, this.retrofitManager.getTransactionInstance());
}
public FetchMessages fetchMessages() {
return new FetchMessages(this, this.retrofitManager.getTransactionInstance());
}
public Audit audit() {
return new Audit(this, this.retrofitManager.getTransactionInstance());
}
public Grant grant() {
return new Grant(this, this.retrofitManager.getTransactionInstance());
}
public GetState getPresenceState() {
return new GetState(this, this.retrofitManager.getTransactionInstance());
}
public SetState setPresenceState() {
return new SetState(this, subscriptionManager, this.retrofitManager.getTransactionInstance());
}
public Publish publish() {
return new Publish(this, publishSequenceManager, this.retrofitManager.getTransactionInstance());
}
public ListAllChannelGroup listAllChannelGroups() {
return new ListAllChannelGroup(this, this.retrofitManager.getTransactionInstance());
}
public AllChannelsChannelGroup listChannelsForChannelGroup() {
return new AllChannelsChannelGroup(this, this.retrofitManager.getTransactionInstance());
}
public AddChannelChannelGroup addChannelsToChannelGroup() {
return new AddChannelChannelGroup(this, this.retrofitManager.getTransactionInstance());
}
public RemoveChannelChannelGroup removeChannelsFromChannelGroup() {
return new RemoveChannelChannelGroup(this, this.retrofitManager.getTransactionInstance());
}
public DeleteChannelGroup deleteChannelGroup() {
return new DeleteChannelGroup(this, this.retrofitManager.getTransactionInstance());
}
// 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
*/
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
*/
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
*/
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
*/
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.
*/
public String getInstanceId() {
return instanceId;
}
/**
* @return request uuid.
*/
public String getRequestId() {
return UUID.randomUUID().toString();
}
/**
* @return version of the SDK.
*/
public String getVersion() {
return SDK_VERSION;
}
/**
* Stop the SDK and terminate all listeners.
*/
@Deprecated
public void stop() {
subscriptionManager.stop();
}
/**
* Destroy the SDK to evict the connection pools.
*/
public void destroy() {
subscriptionManager.destroy();
retrofitManager.destroy();
}
/**
* Perform a Reconnect to the network
*/
public void reconnect() {
subscriptionManager.reconnect();
}
/**
* Perform a disconnect from the listeners
*/
public void disconnect() {
subscriptionManager.disconnect();
}
public Publish fire() {
return publish().shouldStore(false).replicate(false);
}
public List<String> getSubscribedChannels() {
return subscriptionManager.getSubscribedChannels();
}
public List<String> getSubscribedChannelGroups() {
return subscriptionManager.getSubscribedChannelGroups();
}
public void unsubscribeAll() {
subscriptionManager.unsubscribeAll();
}
}