Skip to content

Commit 0c46a9c

Browse files
committed
HistoryForChannels
1 parent bccbfcb commit 0c46a9c

5 files changed

Lines changed: 279 additions & 0 deletions

File tree

‎src/main/java/com/pubnub/api/PubNub.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.pubnub.api.builder.SubscribeBuilder;
66
import com.pubnub.api.builder.UnsubscribeBuilder;
77
import com.pubnub.api.endpoints.History;
8+
import com.pubnub.api.endpoints.HistoryForChannels;
89
import com.pubnub.api.endpoints.Time;
910
import com.pubnub.api.endpoints.access.Audit;
1011
import com.pubnub.api.endpoints.access.Grant;
@@ -125,6 +126,9 @@ public final History history() {
125126
return new History(this, this.retrofitManager.getTransactionInstance());
126127
}
127128

129+
public final HistoryForChannels historyForChannels() {
130+
return new HistoryForChannels(this, this.retrofitManager.getTransactionInstance());
131+
}
128132

129133
public final Audit audit() {
130134
return new Audit(this, this.retrofitManager.getTransactionInstance());
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package com.pubnub.api.endpoints;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.databind.node.ObjectNode;
6+
import com.pubnub.api.PubNub;
7+
import com.pubnub.api.PubNubException;
8+
import com.pubnub.api.PubNubUtil;
9+
import com.pubnub.api.builder.PubNubErrorBuilder;
10+
import com.pubnub.api.enums.PNOperationType;
11+
import com.pubnub.api.models.server.HistoryForChannelsEnvelope;
12+
import com.pubnub.api.models.server.HistoryForChannelsItem;
13+
import com.pubnub.api.vendor.Crypto;
14+
import lombok.Setter;
15+
import lombok.experimental.Accessors;
16+
import retrofit2.Call;
17+
import retrofit2.Response;
18+
import retrofit2.Retrofit;
19+
import retrofit2.http.GET;
20+
import retrofit2.http.Path;
21+
import retrofit2.http.QueryMap;
22+
23+
import java.io.IOException;
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
import java.util.Map;
27+
28+
@Accessors(chain = true, fluent = true)
29+
public class HistoryForChannels extends Endpoint<HistoryForChannelsEnvelope, HistoryForChannelsEnvelope> {
30+
private static final int MAX_CHANNELS = 100;
31+
private static final int MAX_MESSAGES = 25;
32+
@Setter
33+
private List<String> channels;
34+
@Setter
35+
private Integer maximumPerChannel;
36+
@Setter
37+
private Long start;
38+
@Setter
39+
private Long end;
40+
41+
public HistoryForChannels(PubNub pubnub, Retrofit retrofit) {
42+
super(pubnub, retrofit);
43+
channels = new ArrayList<>();
44+
maximumPerChannel = 1;
45+
}
46+
47+
private interface HistoryForChannelsService {
48+
@GET("v3/history/sub-key/{subKey}/channel/{channels}")
49+
Call<HistoryForChannelsEnvelope> fetchHistoryForChannels(@Path("subKey") String subKey,
50+
@Path("channels") String channels,
51+
@QueryMap Map<String, String> options);
52+
}
53+
54+
@Override
55+
protected void validateParams() throws PubNubException {
56+
if (channels == null || channels.size() == 0) {
57+
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_CHANNEL_MISSING).build();
58+
}
59+
if (maximumPerChannel != null && maximumPerChannel > MAX_MESSAGES) {
60+
maximumPerChannel = MAX_MESSAGES;
61+
} else if (maximumPerChannel == null) {
62+
maximumPerChannel = 1;
63+
}
64+
}
65+
66+
@Override
67+
protected Call<HistoryForChannelsEnvelope> doWork(Map<String, String> params) {
68+
69+
HistoryForChannelsService service = this.getRetrofit().create(HistoryForChannelsService.class);
70+
71+
params.put("max", String.valueOf(maximumPerChannel));
72+
73+
if (start != null) {
74+
params.put("start", Long.toString(start).toLowerCase());
75+
}
76+
if (end != null) {
77+
params.put("end", Long.toString(end).toLowerCase());
78+
}
79+
80+
return service.fetchHistoryForChannels(this.getPubnub().getConfiguration().getSubscribeKey(), PubNubUtil.joinString(channels, ","), params);
81+
}
82+
83+
@Override
84+
protected HistoryForChannelsEnvelope createResponse(Response<HistoryForChannelsEnvelope> input) throws PubNubException {
85+
if (input.body() == null) {
86+
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_PARSING_ERROR).build();
87+
}
88+
89+
HistoryForChannelsEnvelope envelope = input.body();
90+
Map<String, List<HistoryForChannelsItem>> channelsList = envelope.getChannels();
91+
92+
for (Map.Entry<String, List<HistoryForChannelsItem>> entry : channelsList.entrySet()) {
93+
for (HistoryForChannelsItem item: entry.getValue()) {
94+
JsonNode message = processMessage(item.getMessage());
95+
item.setMessage(message);
96+
}
97+
98+
}
99+
100+
return envelope;
101+
}
102+
103+
@Override
104+
protected PNOperationType getOperationType() {
105+
return PNOperationType.PNHistoryOperation;
106+
}
107+
108+
@Override
109+
protected boolean isAuthRequired() {
110+
return true;
111+
}
112+
113+
private JsonNode processMessage(JsonNode message) throws PubNubException {
114+
// if we do not have a crypto key, there is no way to process the node; let's return.
115+
if (this.getPubnub().getConfiguration().getCipherKey() == null) {
116+
return message;
117+
}
118+
119+
Crypto crypto = new Crypto(this.getPubnub().getConfiguration().getCipherKey());
120+
ObjectMapper mapper = new ObjectMapper();
121+
String inputText;
122+
String outputText;
123+
JsonNode outputObject;
124+
125+
if (message.isObject() && message.has("message")) {
126+
inputText = message.get("message").asText();
127+
} else {
128+
inputText = message.asText();
129+
}
130+
131+
outputText = crypto.decrypt(inputText);
132+
133+
try {
134+
outputObject = mapper.readValue(outputText, JsonNode.class);
135+
} catch (IOException e) {
136+
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_PARSING_ERROR).errormsg(e.getMessage()).build();
137+
}
138+
139+
// inject the decoded resposne into the payload
140+
if (message.isObject() && message.has("message")) {
141+
ObjectNode objectNode = (ObjectNode) message;
142+
objectNode.set("message", outputObject);
143+
outputObject = objectNode;
144+
}
145+
146+
return outputObject;
147+
}
148+
149+
}
150+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.pubnub.api.models.server;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import lombok.Getter;
6+
import lombok.Setter;
7+
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
@JsonIgnoreProperties(ignoreUnknown = true)
12+
@Getter
13+
@Setter
14+
public class HistoryForChannelsEnvelope {
15+
16+
@JsonProperty("channels")
17+
private Map<String, List<HistoryForChannelsItem>> channels;
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.pubnub.api.models.server;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.fasterxml.jackson.databind.JsonNode;
6+
import lombok.Getter;
7+
import lombok.Setter;
8+
9+
@JsonIgnoreProperties(ignoreUnknown = true)
10+
@Getter
11+
@Setter
12+
public class HistoryForChannelsItem {
13+
14+
@JsonProperty("message")
15+
@Getter private JsonNode message;
16+
17+
@JsonProperty("timetoken")
18+
@Getter private Long timeToken;
19+
20+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.pubnub.api.endpoints;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
5+
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
6+
import com.pubnub.api.PubNub;
7+
import com.pubnub.api.PubNubException;
8+
import com.pubnub.api.models.server.HistoryForChannelsEnvelope;
9+
import org.junit.Assert;
10+
import org.junit.Before;
11+
import org.junit.Rule;
12+
import org.junit.Test;
13+
14+
import java.io.IOException;
15+
import java.util.*;
16+
17+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
18+
import static org.junit.Assert.assertEquals;
19+
20+
21+
public class HistoryForChannelsEndpointTest extends TestHarness {
22+
23+
private HistoryForChannels partialHistory;
24+
private PubNub pubnub;
25+
26+
@Rule
27+
public WireMockRule wireMockRule = new WireMockRule();
28+
29+
@Before
30+
public void beforeEach() throws IOException {
31+
pubnub = this.createPubNubInstance(8080);
32+
partialHistory = pubnub.historyForChannels();
33+
wireMockRule.start();
34+
}
35+
36+
37+
@Test
38+
public void testSyncSuccess() throws IOException, PubNubException {
39+
stubFor(get(urlPathEqualTo("/v3/history/sub-key/mySubscribeKey/channel/mychannel,my_channel"))
40+
.willReturn(aResponse().withBody("{\"status\": 200, \"error\": false, \"error_message\": \"\", \"channels\": {\"my_channel\":[{\"message\":\"hihi\",\"timetoken\":\"14698320467224036\"},{\"message\":\"hihi\",\"timetoken\":\"14698320468265639\"}],\"mychannel\":[{\"message\":\"sample message\",\"timetoken\":\"14369823849575729\"}]}}")));
41+
42+
HistoryForChannelsEnvelope response = partialHistory.channels(Arrays.asList("mychannel,my_channel")).maximumPerChannel(25).sync();
43+
44+
Assert.assertEquals(response.getChannels().size(), 2);
45+
Assert.assertEquals(response.getChannels().containsKey("mychannel"), true);
46+
Assert.assertEquals(response.getChannels().containsKey("my_channel"), true);
47+
Assert.assertEquals(response.getChannels().get("mychannel").size(),1);
48+
Assert.assertEquals(response.getChannels().get("my_channel").size(),2);
49+
}
50+
51+
@Test
52+
public void testSyncAuthSuccess() throws PubNubException, JsonProcessingException {
53+
stubFor(get(urlPathEqualTo("/v3/history/sub-key/mySubscribeKey/channel/mychannel,my_channel"))
54+
.willReturn(aResponse().withBody("{\"status\": 200, \"error\": false, \"error_message\": \"\", \"channels\": {\"my_channel\":[{\"message\":\"hihi\",\"timetoken\":\"14698320467224036\"},{\"message\":\"hihi\",\"timetoken\":\"14698320468265639\"}],\"mychannel\":[{\"message\":\"sample message\",\"timetoken\":\"14369823849575729\"}]}}")));
55+
56+
pubnub.getConfiguration().setAuthKey("authKey");
57+
58+
HistoryForChannelsEnvelope response = partialHistory.channels(Arrays.asList("mychannel,my_channel")).maximumPerChannel(25).sync();
59+
60+
Assert.assertEquals(response.getChannels().size(), 2);
61+
Assert.assertEquals(response.getChannels().containsKey("mychannel"), true);
62+
Assert.assertEquals(response.getChannels().containsKey("my_channel"), true);
63+
Assert.assertEquals(response.getChannels().get("mychannel").size(),1);
64+
Assert.assertEquals(response.getChannels().get("my_channel").size(),2);
65+
66+
List<LoggedRequest> requests = findAll(getRequestedFor(urlMatching("/.*")));
67+
assertEquals("authKey", requests.get(0).queryParameter("auth").firstValue());
68+
assertEquals(1, requests.size());
69+
}
70+
71+
@org.junit.Test
72+
public void testSyncEncryptedSuccess() throws IOException, PubNubException {
73+
pubnub.getConfiguration().setCipherKey("testCipher");
74+
75+
stubFor(get(urlPathEqualTo("/v3/history/sub-key/mySubscribeKey/channel/mychannel,my_channel"))
76+
.willReturn(aResponse().withBody("{\"status\": 200, \"error\": false, \"error_message\": \"\", \"channels\": {\"my_channel\":[{\"message\":\"jC/yJ2y99BeYFYMQ7c53pg==\",\"timetoken\":\"14797423056306675\"}],\"mychannel\":[{\"message\":\"jC/yJ2y99BeYFYMQ7c53pg==\",\"timetoken\":\"14797423056306675\"}]}}")));
77+
78+
HistoryForChannelsEnvelope response = partialHistory.channels(Arrays.asList("mychannel,my_channel")).maximumPerChannel(25).sync();
79+
80+
Assert.assertEquals(response.getChannels().size(), 2);
81+
Assert.assertEquals(response.getChannels().containsKey("mychannel"), true);
82+
Assert.assertEquals(response.getChannels().containsKey("my_channel"), true);
83+
Assert.assertEquals(response.getChannels().get("mychannel").size(),1);
84+
Assert.assertEquals(response.getChannels().get("my_channel").size(),1);
85+
}
86+
87+
}

0 commit comments

Comments
 (0)