forked from ringcentral/pubnub-jtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHistory.java
More file actions
199 lines (166 loc) · 6.91 KB
/
Copy pathHistory.java
File metadata and controls
199 lines (166 loc) · 6.91 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
package com.pubnub.api.endpoints;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.pubnub.api.PubNub;
import com.pubnub.api.PubNubException;
import com.pubnub.api.builder.PubNubErrorBuilder;
import com.pubnub.api.enums.PNOperationType;
import com.pubnub.api.managers.MapperManager;
import com.pubnub.api.managers.RetrofitManager;
import com.pubnub.api.managers.TelemetryManager;
import com.pubnub.api.models.consumer.history.PNHistoryItemResult;
import com.pubnub.api.models.consumer.history.PNHistoryResult;
import com.pubnub.api.vendor.Crypto;
import lombok.Setter;
import lombok.experimental.Accessors;
import retrofit2.Call;
import retrofit2.Response;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@Accessors(chain = true, fluent = true)
public class History extends Endpoint<JsonElement, PNHistoryResult> {
private static final int MAX_COUNT = 100;
@Setter
private String channel;
@Setter
private Long start;
@Setter
private Long end;
@Setter
private Boolean reverse;
@Setter
private Integer count;
@Setter
private Boolean includeTimetoken;
@Setter
private Boolean includeMeta;
public History(PubNub pubnub, TelemetryManager telemetryManager, RetrofitManager retrofit) {
super(pubnub, telemetryManager, retrofit);
}
@Override
protected List<String> getAffectedChannels() {
return Collections.singletonList(channel);
}
@Override
protected List<String> getAffectedChannelGroups() {
return null;
}
@Override
protected void validateParams() throws PubNubException {
if (this.getPubnub().getConfiguration().getSubscribeKey() == null
|| this.getPubnub().getConfiguration().getSubscribeKey().isEmpty()) {
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_SUBSCRIBE_KEY_MISSING).build();
}
if (channel == null || channel.isEmpty()) {
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_CHANNEL_MISSING).build();
}
if (includeMeta == null) {
includeMeta = false;
}
if (includeTimetoken == null) {
includeTimetoken = false;
}
}
@Override
protected Call<JsonElement> doWork(Map<String, String> params) {
if (reverse != null) {
params.put("reverse", String.valueOf(reverse));
}
if (includeTimetoken != null) {
params.put("include_token", String.valueOf(includeTimetoken));
}
if (includeMeta) {
params.put("include_meta", String.valueOf(includeMeta));
}
if (count != null && count > 0 && count <= MAX_COUNT) {
params.put("count", String.valueOf(count));
} else {
params.put("count", "100");
}
if (start != null) {
params.put("start", Long.toString(start).toLowerCase());
}
if (end != null) {
params.put("end", Long.toString(end).toLowerCase());
}
return this.getRetrofit().getHistoryService().fetchHistory(this.getPubnub().getConfiguration()
.getSubscribeKey(), channel, params);
}
@Override
protected PNHistoryResult createResponse(Response<JsonElement> input) throws PubNubException {
PNHistoryResult.PNHistoryResultBuilder historyData = PNHistoryResult.builder();
List<PNHistoryItemResult> messages = new ArrayList<>();
MapperManager mapper = getPubnub().getMapper();
if (input.body() != null) {
Long startTimeToken = mapper.elementToLong(mapper.getArrayElement(input.body(), 1));
Long endTimeToken = mapper.elementToLong(mapper.getArrayElement(input.body(), 2));
historyData.startTimetoken(startTimeToken);
historyData.endTimetoken(endTimeToken);
if (mapper.getArrayElement(input.body(), 0).isJsonArray()) {
Iterator<JsonElement> it = mapper.getArrayIterator(mapper.getArrayElement(input.body(), 0));
while (it.hasNext()) {
JsonElement historyEntry = it.next();
PNHistoryItemResult.PNHistoryItemResultBuilder historyItem = PNHistoryItemResult.builder();
JsonElement message;
if (includeTimetoken || includeMeta) {
message = processMessage(mapper.getField(historyEntry, "message"));
if (includeTimetoken) {
historyItem.timetoken(mapper.elementToLong(historyEntry, "timetoken"));
}
if (includeMeta) {
historyItem.meta(mapper.getField(historyEntry, "meta"));
}
} else {
message = processMessage(historyEntry);
}
historyItem.entry(message);
messages.add(historyItem.build());
}
} else {
throw PubNubException.builder()
.pubnubError(PubNubErrorBuilder.PNERROBJ_HTTP_ERROR)
.errormsg("History is disabled")
.jso(input.body())
.build();
}
historyData.messages(messages);
}
return historyData.build();
}
@Override
protected PNOperationType getOperationType() {
return PNOperationType.PNHistoryOperation;
}
@Override
protected boolean isAuthRequired() {
return true;
}
private JsonElement processMessage(JsonElement message) throws PubNubException {
// if we do not have a crypto key, there is no way to process the node; let's return.
if (this.getPubnub().getConfiguration().getCipherKey() == null) {
return message;
}
Crypto crypto = new Crypto(this.getPubnub().getConfiguration().getCipherKey(), this.getPubnub().getConfiguration().isUseRandomInitializationVector());
MapperManager mapper = getPubnub().getMapper();
String inputText;
String outputText;
JsonElement outputObject;
if (mapper.isJsonObject(message) && mapper.hasField(message, "pn_other")) {
inputText = mapper.elementToString(message, "pn_other");
} else {
inputText = mapper.elementToString(message);
}
outputText = crypto.decrypt(inputText);
outputObject = this.getPubnub().getMapper().fromJson(outputText, JsonElement.class);
// inject the decoded response into the payload
if (mapper.isJsonObject(message) && mapper.hasField(message, "pn_other")) {
JsonObject objectNode = mapper.getAsObject(message);
mapper.putOnObject(objectNode, "pn_other", outputObject);
outputObject = objectNode;
}
return outputObject;
}
}