Repository navigation
Expand file tree
/
Copy pathHistory.java
More file actions
153 lines (124 loc) · 4.98 KB
/
Copy pathHistory.java
File metadata and controls
153 lines (124 loc) · 4.98 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
package com.pubnub.api.endpoints;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.pubnub.api.vendor.Crypto;
import com.pubnub.api.PubNub;
import com.pubnub.api.PubNubError;
import com.pubnub.api.PubNubException;
import com.pubnub.api.enums.PNOperationType;
import com.pubnub.api.models.consumer.history.PNHistoryItemResult;
import com.pubnub.api.models.consumer.history.PNHistoryResult;
import lombok.Setter;
import lombok.experimental.Accessors;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.QueryMap;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Accessors(chain = true, fluent = true)
public class History extends Endpoint<JsonNode, 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;
public History(PubNub pubnubInstance) {
super(pubnubInstance);
}
private interface HistoryService {
@GET("v2/history/sub-key/{subKey}/channel/{channel}")
Call<JsonNode> fetchHistory(@Path("subKey") String subKey,
@Path("channel") String channel,
@QueryMap Map<String, String> options);
}
@Override
protected void validateParams() throws PubNubException {
if (channel == null || channel.isEmpty()) {
throw PubNubException.builder().pubnubError(PubNubError.PNERROBJ_CHANNEL_MISSING).build();
}
}
@Override
protected Call<JsonNode> doWork(Map<String, String> params) {
HistoryService service = this.createRetrofit().create(HistoryService.class);
if (reverse != null) {
params.put("reverse", String.valueOf(reverse));
}
if (includeTimetoken != null) {
params.put("include_token", String.valueOf(includeTimetoken));
}
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 service.fetchHistory(this.getPubnub().getConfiguration().getSubscribeKey(), channel, params);
}
@Override
protected PNHistoryResult createResponse(Response<JsonNode> input) throws PubNubException {
PNHistoryResult.PNHistoryResultBuilder historyData = PNHistoryResult.builder();
List<PNHistoryItemResult> messages = new ArrayList<>();
if (input.body() != null) {
historyData.startTimeToken(input.body().get(1).asLong());
historyData.endTimeToken(input.body().get(2).asLong());
ArrayNode historyItems = (ArrayNode) input.body().get(0);
for (final JsonNode historyEntry : historyItems) {
PNHistoryItemResult.PNHistoryItemResultBuilder historyItem = PNHistoryItemResult.builder();
Object message;
if (includeTimetoken != null && includeTimetoken) {
historyItem.timetoken(historyEntry.get("timetoken").asLong());
message = processMessage(historyEntry.get("message"));
} else {
message = processMessage(historyEntry);
}
historyItem.entry(message);
messages.add(historyItem.build());
}
historyData.messages(messages);
}
return historyData.build();
}
protected int getConnectTimeout() {
return this.getPubnub().getConfiguration().getConnectTimeout();
}
protected int getRequestTimeout() {
return this.getPubnub().getConfiguration().getNonSubscribeRequestTimeout();
}
@Override
protected PNOperationType getOperationType() {
return PNOperationType.PNHistoryOperation;
}
private Object processMessage(JsonNode message) throws PubNubException {
if (this.getPubnub().getConfiguration().getCipherKey() == null) {
return message;
}
Crypto crypto = new Crypto(this.getPubnub().getConfiguration().getCipherKey());
String outputText = crypto.decrypt(message.asText());
ObjectMapper mapper = new ObjectMapper();
Object outputObject;
try {
outputObject = mapper.readValue(outputText, JsonNode.class);
} catch (IOException e) {
throw PubNubException.builder().pubnubError(PubNubError.PNERROBJ_PARSING_ERROR).errormsg(e.getMessage()).build();
}
return outputObject;
}
}