Skip to content

Commit e05ee83

Browse files
author
Max Presman
committed
adjust pn_other parsing
1 parent e5fe23c commit e05ee83

4 files changed

Lines changed: 108 additions & 3 deletions

File tree

src/main/java/com/pubnub/api/endpoints/History.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,25 @@ protected boolean isAuthRequired() {
130130
}
131131

132132
private JsonNode processMessage(JsonNode message) throws PubNubException {
133+
// if we do not have a crypto key, there is no way to process the node; let's return.
133134
if (this.getPubnub().getConfiguration().getCipherKey() == null) {
134135
return message;
135136
}
136137

137138
Crypto crypto = new Crypto(this.getPubnub().getConfiguration().getCipherKey());
138-
String outputText = crypto.decrypt(message.asText());
139-
140139
ObjectMapper mapper = new ObjectMapper();
140+
String inputText;
141+
String outputText;
141142
JsonNode outputObject;
143+
144+
if (message.isObject() && message.has("pn_other")) {
145+
inputText = message.get("pn_other").asText();
146+
} else {
147+
inputText = message.asText();
148+
}
149+
150+
outputText = crypto.decrypt(inputText);
151+
142152
try {
143153
outputObject = mapper.readValue(outputText, JsonNode.class);
144154
} catch (IOException e) {

src/main/java/com/pubnub/api/workers/SubscribeMessageWorker.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,24 @@ private void takeMessage() {
5858
}
5959

6060
private JsonNode processMessage(final JsonNode input) {
61+
// if we do not have a crypto key, there is no way to process the node; let's return.
6162
if (pubnub.getConfiguration().getCipherKey() == null) {
6263
return input;
6364
}
6465

6566
Crypto crypto = new Crypto(pubnub.getConfiguration().getCipherKey());
67+
String inputText;
6668
String outputText;
6769
JsonNode outputObject;
6870

71+
if (input.isObject() && input.has("pn_other")) {
72+
inputText = input.get("pn_other").asText();
73+
} else {
74+
inputText = input.asText();
75+
}
76+
6977
try {
70-
outputText = crypto.decrypt(input.toString());
78+
outputText = crypto.decrypt(inputText);
7179
} catch (PubNubException e) {
7280
PNStatus pnStatus = PNStatus.builder().error(true)
7381
.errorData(new PNErrorData(e.getMessage(), e))

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,25 @@ public void testSyncEncryptedSuccess() throws IOException, PubNubException {
162162

163163
}
164164

165+
@org.junit.Test
166+
public void testSyncEncryptedWithPNOtherSuccess() throws IOException, PubNubException {
167+
pubnub.getConfiguration().setCipherKey("hello");
168+
169+
stubFor(get(urlPathEqualTo("/v2/history/sub-key/mySubscribeKey/channel/niceChannel"))
170+
.willReturn(aResponse().withBody("[[{\"pn_other\":\"6QoqmS9CnB3W9+I4mhmL7w==\"}],14606134331557852,14606134485013970]")));
171+
172+
PNHistoryResult response = partialHistory.channel("niceChannel").includeTimetoken(false).sync();
173+
174+
Assert.assertTrue(response.getStartTimetoken().equals(14606134331557852L));
175+
Assert.assertTrue(response.getEndTimetoken().equals(14606134485013970L));
176+
177+
Assert.assertEquals(response.getMessages().size(), 1);
178+
179+
Assert.assertEquals(response.getMessages().get(0).getTimetoken(), null);
180+
Assert.assertEquals("hey", response.getMessages().get(0).getEntry().get("text").asText());
181+
182+
}
183+
165184
@org.junit.Test
166185
public void testSyncSuccessWithoutTimeToken() throws IOException, PubNubException {
167186
List<Object> testArray = new ArrayList<Object>();

src/test/java/com/pubnub/api/managers/SubscriptionManagerTest.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,74 @@ public void presence(PubNub pubnub, PNPresenceEventResult presence) {
420420

421421
}
422422

423+
@Test
424+
public void testSubscribeWithEncryption() {
425+
final AtomicInteger atomic = new AtomicInteger(0);
426+
stubFor(get(urlPathEqualTo("/v2/subscribe/mySubscribeKey/ch2,ch1/0"))
427+
.willReturn(aResponse().withBody("{\"t\":{\"t\":\"14718972508742569\",\"r\":1},\"m\":[{\"a\":\"4\",\"f\":512,\"i\":\"ff374d0b-b866-40db-9ced-42d205bb808b\",\"p\":{\"t\":\"14718972508739738\",\"r\":1},\"k\":\"demo-36\",\"c\":\"max_ch1\",\"d\":\"6QoqmS9CnB3W9+I4mhmL7w==\"}]}")));
428+
429+
pubnub.getConfiguration().setCipherKey("hello");
430+
431+
pubnub.addListener(new SubscribeCallback() {
432+
@Override
433+
public void status(PubNub pubnub, PNStatus status) {
434+
}
435+
436+
@Override
437+
public void message(PubNub pubnub, PNMessageResult message) {
438+
List<LoggedRequest> requests = findAll(getRequestedFor(urlMatching("/v2/subscribe.*")));
439+
assertTrue(requests.size() > 0);
440+
assertEquals("hey", message.getMessage().get("text").asText());
441+
atomic.addAndGet(1);
442+
}
443+
444+
@Override
445+
public void presence(PubNub pubnub, PNPresenceEventResult presence) {
446+
}
447+
});
448+
449+
450+
pubnub.subscribe().channels(Arrays.asList("ch1", "ch2")).execute();
451+
452+
Awaitility.await().atMost(5, TimeUnit.SECONDS)
453+
.untilAtomic(atomic, org.hamcrest.Matchers.greaterThan(0));
454+
455+
}
456+
457+
@Test
458+
public void testSubscribeWithEncryptionPNOther() {
459+
final AtomicInteger atomic = new AtomicInteger(0);
460+
stubFor(get(urlPathEqualTo("/v2/subscribe/mySubscribeKey/ch2,ch1/0"))
461+
.willReturn(aResponse().withBody("{\"t\":{\"t\":\"14718972508742569\",\"r\":1},\"m\":[{\"a\":\"4\",\"f\":512,\"i\":\"ff374d0b-b866-40db-9ced-42d205bb808b\",\"p\":{\"t\":\"14718972508739738\",\"r\":1},\"k\":\"demo-36\",\"c\":\"max_ch1\",\"d\":{\"pn_other\":\"6QoqmS9CnB3W9+I4mhmL7w==\"}}]}")));
462+
463+
pubnub.getConfiguration().setCipherKey("hello");
464+
465+
pubnub.addListener(new SubscribeCallback() {
466+
@Override
467+
public void status(PubNub pubnub, PNStatus status) {
468+
}
469+
470+
@Override
471+
public void message(PubNub pubnub, PNMessageResult message) {
472+
List<LoggedRequest> requests = findAll(getRequestedFor(urlMatching("/v2/subscribe.*")));
473+
assertTrue(requests.size() > 0);
474+
assertEquals("hey", message.getMessage().get("text").asText());
475+
atomic.addAndGet(1);
476+
}
477+
478+
@Override
479+
public void presence(PubNub pubnub, PNPresenceEventResult presence) {
480+
}
481+
});
482+
483+
484+
pubnub.subscribe().channels(Arrays.asList("ch1", "ch2")).execute();
485+
486+
Awaitility.await().atMost(5, TimeUnit.SECONDS)
487+
.untilAtomic(atomic, org.hamcrest.Matchers.greaterThan(0));
488+
489+
}
490+
423491
@Test
424492
public void testSubscribePresenceBuilder() {
425493
final AtomicInteger atomic = new AtomicInteger(0);

0 commit comments

Comments
 (0)