Skip to content

Commit a0bf9e5

Browse files
author
Max Presman
committed
crypto on subscribe
1 parent 259d690 commit a0bf9e5

3 files changed

Lines changed: 99 additions & 14 deletions

File tree

‎src/main/java/com/pubnub/api/managers/SubscriptionManager.java‎

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package com.pubnub.api.managers;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
35
import com.pubnub.api.callbacks.SubscribeCallback;
46
import com.pubnub.api.callbacks.PNCallback;
7+
import com.pubnub.api.core.Crypto;
58
import com.pubnub.api.core.Pubnub;
9+
import com.pubnub.api.core.PubnubError;
10+
import com.pubnub.api.core.PubnubException;
611
import com.pubnub.api.core.enums.PNOperationType;
12+
import com.pubnub.api.core.enums.PNStatusCategory;
713
import com.pubnub.api.core.enums.SubscriptionType;
814
import com.pubnub.api.core.models.*;
915
import com.pubnub.api.core.models.consumer_facing.*;
@@ -15,6 +21,7 @@
1521
import lombok.extern.slf4j.Slf4j;
1622
import retrofit2.Call;
1723

24+
import java.io.IOException;
1825
import java.util.*;
1926

2027
@Slf4j
@@ -161,10 +168,10 @@ public void onResponse(SubscribeEnvelope result, PNErrorStatus status) {
161168
});
162169
}
163170

164-
private void performHeartbeatLoop(){
171+
private void performHeartbeatLoop() {
165172
log.debug("performingHeartbeatLoop");
166173

167-
if (heartbeatCall != null && heartbeatCall.isCanceled() == false && heartbeatCall.isExecuted() == false) {
174+
if (heartbeatCall != null && !heartbeatCall.isCanceled() && !heartbeatCall.isExecuted()) {
168175
heartbeatCall.cancel();
169176
}
170177

@@ -229,28 +236,73 @@ private void processIncomingMessages(List<SubscribeMessage> messages) {
229236
pnPresenceEventResult.setData(pnPresenceEventData);
230237
pnPresenceEventResult.setOperation(PNOperationType.PNSubscribeOperation);
231238

232-
for (SubscribeCallback subscribeCallback: listeners) {
233-
subscribeCallback.presence(this.pubnub, pnPresenceEventResult);
234-
}
239+
announce(pnPresenceEventResult);
235240
} else {
241+
Object extractedMessage = null;
242+
243+
try {
244+
extractedMessage = processMessage(message.getPayload());
245+
} catch (PubnubException e) {
246+
PNStatus pnStatus = new PNStatus();
247+
pnStatus.setError(true);
248+
pnStatus.setOperation(PNOperationType.PNSubscribeOperation);
249+
pnStatus.setCategory(PNStatusCategory.PNDecryptionErrorCategory);
250+
251+
announce(pnStatus);
252+
}
253+
236254
PNMessageResult pnMessageResult = new PNMessageResult();
237255
PNMessageData pnMessageData = new PNMessageData();
238256

239257
pnMessageResult.setOperation(PNOperationType.PNSubscribeOperation);
240-
pnMessageData.setMessage(message.getPayload());
258+
pnMessageData.setMessage(extractedMessage);
241259

242260
pnMessageData.setActualChannel((subscriptionMatch != null) ? channel : null);
243261
pnMessageData.setSubscribedChannel(subscriptionMatch != null ? subscriptionMatch : channel);
244262
pnMessageData.setTimetoken(timetoken);
245263

246264
pnMessageResult.setData(pnMessageData);
247265

248-
for (SubscribeCallback subscribeCallback: listeners) {
249-
subscribeCallback.message(this.pubnub, pnMessageResult);
250-
}
251-
266+
announce(pnMessageResult);
252267
}
253268
}
254269
}
255270

271+
private Object processMessage(Object input) throws PubnubException {
272+
if (pubnub.getConfiguration().getCipherKey() == null) {
273+
return input;
274+
}
275+
276+
Crypto crypto = new Crypto(pubnub.getConfiguration().getCipherKey());
277+
String outputText = crypto.decrypt(input.toString());
278+
279+
ObjectMapper mapper = new ObjectMapper();
280+
Object outputObject;
281+
try {
282+
outputObject = mapper.readValue(outputText, JsonNode.class);
283+
} catch (IOException e) {
284+
throw new PubnubException(PubnubError.PNERROBJ_PARSING_ERROR);
285+
}
286+
287+
return outputObject;
288+
}
289+
290+
private void announce(PNStatus status) {
291+
for (SubscribeCallback subscribeCallback: listeners) {
292+
subscribeCallback.status(this.pubnub, status);
293+
}
294+
}
295+
296+
private void announce(PNMessageResult message) {
297+
for (SubscribeCallback subscribeCallback: listeners) {
298+
subscribeCallback.message(this.pubnub, message);
299+
}
300+
}
301+
302+
private void announce(PNPresenceEventResult presence) {
303+
for (SubscribeCallback subscribeCallback: listeners) {
304+
subscribeCallback.presence(this.pubnub, presence);
305+
}
306+
}
307+
256308
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
import com.pubnub.api.core.PnConfiguration;
55
import com.pubnub.api.core.Pubnub;
66
import com.pubnub.api.core.PubnubException;
7-
import com.pubnub.api.core.models.consumer_facing.PNHistoryResult;
87
import com.pubnub.api.core.models.consumer_facing.PNMessageResult;
98
import com.pubnub.api.core.models.consumer_facing.PNPresenceEventResult;
109
import com.pubnub.api.core.models.consumer_facing.PNStatus;
1110
import lombok.extern.slf4j.Slf4j;
1211

13-
import java.util.Arrays;
1412
import java.util.HashMap;
1513
import java.util.Map;
1614

@@ -47,7 +45,7 @@ public void presence(Pubnub pubnub, PNPresenceEventResult presence) {
4745
Map<String, Object> state = new HashMap<>();
4846
state.put("max", "moose");
4947

50-
//pubnub.subscribe().channel("max-ch1").withPresence().execute();
48+
pubnub.subscribe().channel("coolChannelENC").withPresence().execute();
5149
//pubnub.setPresenceState().channel("max-ch1").state(state).build().async(new PNCallback<PNSetStateResult>() {
5250
// @Override
5351
// public void onResponse(PNSetStateResult result, PNErrorStatus status) {
@@ -69,11 +67,13 @@ public void presence(Pubnub pubnub, PNPresenceEventResult presence) {
6967
*/
7068

7169

70+
/*
7271
pubnub.publish()
7372
.channel("coolChannelENC2").usePOST(false).shouldStore(true)
7473
.message(Arrays.asList("m1", "m2", "m3")).build().sync();
7574
7675
PNHistoryResult moose = pubnub.history().channel("coolChannelENC2").build().sync();
76+
*/
7777

7878
int max = 10;
7979

‎src/test/java/com.pubnub.api/endpoints/HistoryEndpointTest.java‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.databind.JsonNode;
44
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.pubnub.api.core.Pubnub;
56
import com.pubnub.api.core.PubnubException;
67
import com.pubnub.api.core.models.consumer_facing.PNHistoryResult;
78
import okhttp3.mockwebserver.MockResponse;
@@ -20,13 +21,16 @@ public class HistoryEndpointTest extends EndpointTest {
2021

2122
private MockWebServer server;
2223
private History.HistoryBuilder partialHistory;
24+
private Pubnub pubnub;
2325

2426

2527
@Before
2628
public void beforeEach() throws IOException {
2729
server = new MockWebServer();
2830
server.start();
29-
partialHistory = this.createPubNubInstance(server).history();
31+
32+
pubnub = this.createPubNubInstance(server);
33+
partialHistory = pubnub.history();
3034
}
3135

3236

@@ -75,6 +79,35 @@ public void testSyncSuccess() throws IOException, PubnubException {
7579
Assert.assertEquals(((JsonNode) response.getMessages().get(1).getEntry()).get("b").asInt(), 44);
7680
}
7781

82+
83+
@org.junit.Test
84+
public void testSyncEncryptedSuccess() throws IOException, PubnubException {
85+
pubnub.getConfiguration().setCipherKey("testCipher");
86+
87+
88+
server.enqueue(new MockResponse().setBody("[[\"EGwV+Ti43wh2TprPIq7o0KMuW5j6B3yWy352ucWIOmU=\\n\",\"EGwV+Ti43wh2TprPIq7o0KMuW5j6B3yWy352ucWIOmU=\\n\",\"EGwV+Ti43wh2TprPIq7o0KMuW5j6B3yWy352ucWIOmU=\\n\"],14606134331557853,14606134485013970]"));
89+
PNHistoryResult response = partialHistory.channel("niceChannel").includeTimetoken(false).build().sync();
90+
91+
Assert.assertTrue(response.getStartTimeToken().equals(14606134331557853L));
92+
Assert.assertTrue(response.getEndTimeToken().equals(14606134485013970L));
93+
94+
Assert.assertEquals(response.getMessages().size(), 3);
95+
96+
Assert.assertEquals(response.getMessages().get(0).getTimetoken(), null);
97+
Assert.assertEquals("m1", ((JsonNode) response.getMessages().get(0).getEntry()).get(0).asText());
98+
Assert.assertEquals("m2", ((JsonNode) response.getMessages().get(0).getEntry()).get(1).asText());
99+
Assert.assertEquals("m3", ((JsonNode) response.getMessages().get(0).getEntry()).get(2).asText());
100+
101+
Assert.assertEquals("m1", ((JsonNode) response.getMessages().get(1).getEntry()).get(0).asText());
102+
Assert.assertEquals("m2", ((JsonNode) response.getMessages().get(1).getEntry()).get(1).asText());
103+
Assert.assertEquals("m3", ((JsonNode) response.getMessages().get(1).getEntry()).get(2).asText());
104+
105+
Assert.assertEquals("m1", ((JsonNode) response.getMessages().get(2).getEntry()).get(0).asText());
106+
Assert.assertEquals("m2", ((JsonNode) response.getMessages().get(2).getEntry()).get(1).asText());
107+
Assert.assertEquals("m3", ((JsonNode) response.getMessages().get(2).getEntry()).get(2).asText());
108+
109+
}
110+
78111
@org.junit.Test
79112
public void testSyncSuccessWithoutTimeToken() throws IOException, PubnubException {
80113
List<Object> testArray = new ArrayList<Object>();

0 commit comments

Comments
 (0)