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
268 lines (214 loc) · 9.28 KB
/
Copy pathPubNub.java
File metadata and controls
268 lines (214 loc) · 9.28 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
package com.pubnub.api;
import com.pubnub.api.builder.PubNubErrorBuilder;
import com.pubnub.api.callbacks.SubscribeCallback;
import com.pubnub.api.builder.SubscribeBuilder;
import com.pubnub.api.builder.UnsubscribeBuilder;
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.RemoveChannelChannelGroup;
import com.pubnub.api.endpoints.channel_groups.ListAllChannelGroup;
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.RemoveChannelsFromPush;
import com.pubnub.api.endpoints.push.RemoveAllPushChannelsForDevice;
import com.pubnub.api.endpoints.push.ListPushProvisions;
import com.pubnub.api.managers.BasePathManager;
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.AccessLevel;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import java.util.Date;
import java.util.List;
@Getter
@Slf4j
public class PubNub {
private PNConfiguration configuration;
@Getter(AccessLevel.NONE)
private SubscriptionManager subscriptionManager;
@Getter(AccessLevel.NONE)
private BasePathManager basePathManager;
@Getter(AccessLevel.NONE)
private PublishSequenceManager publishSequenceManager;
@Getter(AccessLevel.NONE)
private RetrofitManager retrofitManager;
private static final int TIMESTAMP_DIVIDER = 1000;
private static final int MAX_SEQUENCE = 65535;
private static final String SDK_VERSION = "4.0.9";
public PubNub(final PNConfiguration initialConfig) {
this.configuration = initialConfig;
this.basePathManager = new BasePathManager(initialConfig);
this.retrofitManager = new RetrofitManager(this);
this.subscriptionManager = new SubscriptionManager(this, retrofitManager);
this.publishSequenceManager = new PublishSequenceManager(MAX_SEQUENCE);
}
public String getBaseUrl() {
return this.basePathManager.getBasePath();
}
//
public final void addListener(SubscribeCallback listener) {
subscriptionManager.addListener(listener);
}
public final void removeListener(SubscribeCallback listener) {
subscriptionManager.removeListener(listener);
}
public final SubscribeBuilder subscribe() {
return new SubscribeBuilder(this.subscriptionManager);
}
public final UnsubscribeBuilder unsubscribe() {
return new UnsubscribeBuilder(this.subscriptionManager);
}
// start push
public final AddChannelsToPush addPushNotificationsOnChannels() {
return new AddChannelsToPush(this, this.retrofitManager.getTransactionInstance());
}
public final RemoveChannelsFromPush removePushNotificationsFromChannels() {
return new RemoveChannelsFromPush(this, this.retrofitManager.getTransactionInstance());
}
public final RemoveAllPushChannelsForDevice removeAllPushNotificationsFromDeviceWithPushToken() {
return new RemoveAllPushChannelsForDevice(this, this.retrofitManager.getTransactionInstance());
}
public final ListPushProvisions auditPushChannelProvisions() {
return new ListPushProvisions(this, this.retrofitManager.getTransactionInstance());
}
// end push
public final WhereNow whereNow() {
return new WhereNow(this, this.retrofitManager.getTransactionInstance());
}
public final HereNow hereNow() {
return new HereNow(this, this.retrofitManager.getTransactionInstance());
}
public final Time time() {
return new Time(this, this.retrofitManager.getTransactionInstance());
}
public final History history() {
return new History(this, this.retrofitManager.getTransactionInstance());
}
public final Audit audit() {
return new Audit(this, this.retrofitManager.getTransactionInstance());
}
public final Grant grant() {
return new Grant(this, this.retrofitManager.getTransactionInstance());
}
public final GetState getPresenceState() {
return new GetState(this, this.retrofitManager.getTransactionInstance());
}
public final SetState setPresenceState() {
return new SetState(this, subscriptionManager, this.retrofitManager.getTransactionInstance());
}
public final Publish publish() {
return new Publish(this, publishSequenceManager, this.retrofitManager.getTransactionInstance());
}
public final ListAllChannelGroup listAllChannelGroups() {
return new ListAllChannelGroup(this, this.retrofitManager.getTransactionInstance());
}
public final AllChannelsChannelGroup listChannelsForChannelGroup() {
return new AllChannelsChannelGroup(this, this.retrofitManager.getTransactionInstance());
}
public final AddChannelChannelGroup addChannelsToChannelGroup() {
return new AddChannelChannelGroup(this, this.retrofitManager.getTransactionInstance());
}
public final RemoveChannelChannelGroup removeChannelsFromChannelGroup() {
return new RemoveChannelChannelGroup(this, this.retrofitManager.getTransactionInstance());
}
public final 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 final 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 final String decrypt(final String inputString, final 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 final String encrypt(final 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 final String encrypt(final String inputString, final 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 version of the SDK.
*/
public String getVersion() {
return SDK_VERSION;
}
/**
* Stop the SDK and terminate all listeners.
*/
public final void stop() {
subscriptionManager.stop();
}
/**
* Perform a Reconnect to the network
*/
public final void reconnect() {
subscriptionManager.reconnect();
}
public final Publish fire() {
return publish().shouldStore(false).replicate(false);
}
public final List<String> getSubscribedChannels() {
return subscriptionManager.getSubscribedChannels();
}
public final List<String> getSubscribedChannelGroups() {
return subscriptionManager.getSubscribedChannelGroups();
}
public final void unsubscribeAll() {
subscriptionManager.unsubscribeAll();
}
}