Skip to content

Commit 698aaa4

Browse files
proggen-comproggen-com
andauthored
* Fixes a crash when an event comes in without escaped data json … (#339)
* * Fixes a crash when an event comes in without and escaped data json member. * update to com.google.code.gson:gson:2.9.1 * update to org.java-websocket:Java-WebSocket:1.5.3 * add test for already encoded json, like error messages serialze nulls * fix unittest * be able to return null from getUserId() * allow null as channelName Co-authored-by: proggen-com <[email protected]>
1 parent efed957 commit 698aaa4

File tree

7 files changed

+51
-35
lines changed

7 files changed

+51
-35
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# pusher-websocket-java changelog
22

3+
### Version 2.4.2 - 23th Sep 2022
4+
* Fixes a crash when an event comes in without and escaped data json member.
5+
* update to com.google.code.gson:gson:2.9.1
6+
* update to org.java-websocket:Java-WebSocket:1.5.3
7+
38
### Version 2.4.1 - 13th Sep 2022
49
* Refactoring and code cleanup of event handling in the SDK
510
* Fixes subscription_count events

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ repositories {
4646
}
4747

4848
dependencies {
49-
implementation "com.google.code.gson:gson:2.8.9"
50-
implementation "org.java-websocket:Java-WebSocket:1.5.1"
49+
implementation "com.google.code.gson:gson:2.9.1"
50+
implementation "org.java-websocket:Java-WebSocket:1.5.3"
5151

5252
testImplementation "org.mockito:mockito-all:1.8.5"
5353
testImplementation "org.powermock:powermock-module-junit4:1.4.11"
Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package com.pusher.client.channel;
22

33
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
45
import com.google.gson.JsonElement;
56
import com.google.gson.JsonObject;
7+
import com.google.gson.JsonPrimitive;
68
import com.google.gson.annotations.SerializedName;
79

810
import java.util.Map;
911

1012
public class PusherEvent {
1113

12-
@SerializedName("user_id")
13-
private final String userId;
14-
private final String data;
15-
private final String channel;
16-
private final String event;
14+
private JsonObject jsonObject = new JsonObject();
1715

1816
/**
1917
* getProperty returns the value associated with the key, or null.
@@ -36,7 +34,7 @@ public Object getProperty(String key) {
3634
case "channel":
3735
return getChannelName();
3836
case "data":
39-
return getData();
37+
return jsonObject.get("data");
4038
case "event":
4139
return getEventName();
4240
default:
@@ -45,47 +43,52 @@ public Object getProperty(String key) {
4543
}
4644

4745
public String getUserId() {
48-
return userId;
46+
return jsonObject.has("user_id") ? jsonObject.get("user_id").getAsString() : null;
4947
}
5048

5149
public String getChannelName() {
52-
return channel;
50+
return jsonObject.has("channel") ? jsonObject.get("channel").getAsString() : null;
5351
}
5452

5553
public String getEventName() {
56-
return event;
54+
return jsonObject.has("event") ? jsonObject.get("event").getAsString() : null;
5755
}
5856

5957
public String getData() {
60-
return data;
58+
JsonElement data = jsonObject.get("data");
59+
if (data.isJsonPrimitive()) {
60+
return data.getAsString();
61+
}
62+
final Gson gson = new GsonBuilder().serializeNulls().disableHtmlEscaping().create();
63+
return gson.toJson(data);
6164
}
6265

6366
public String toString() {
64-
return new Gson().toJson(this);
67+
return this.toJson();
6568
}
6669

6770
public PusherEvent(String event, String channel, String userId, String data) {
68-
this.event = event;
69-
this.channel = channel;
70-
this.userId = userId;
71-
this.data = data;
71+
jsonObject.addProperty("event", event);
72+
jsonObject.addProperty("channel", channel);
73+
jsonObject.addProperty("userId", userId);
74+
jsonObject.addProperty("data", data);
7275
}
7376

7477
public PusherEvent(String event, String channel, String userId, Map<String, Object> data) {
7578
this(event, channel, userId, new Gson().toJson(data));
7679
}
7780

81+
public PusherEvent(JsonObject jsonObject) {
82+
this.jsonObject = jsonObject;
83+
}
84+
7885
public String toJson() {
79-
return new Gson().toJson(this);
86+
final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
87+
return gson.toJson(jsonObject);
8088
}
8189

8290
public static PusherEvent fromJson(String json) {
83-
JsonObject o = new Gson().fromJson(json, JsonObject.class);
84-
return new PusherEvent(
85-
o.has("event") ? o.get("event").getAsString() : "",
86-
o.has("channel") ? o.get("channel").getAsString() : "",
87-
o.has("user_id") ? o.get("user_id").getAsString() : "",
88-
o.has("data") ? o.get("data").getAsString() : ""
89-
);
91+
final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
92+
return new PusherEvent(gson.fromJson(json, JsonObject.class));
9093
}
9194
}

src/main/java/com/pusher/client/channel/impl/ChannelManager.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,12 @@ public void unsubscribeFrom(final String channelName) {
100100
}
101101

102102
public void handleEvent(final PusherEvent event) {
103-
final InternalChannel channel = channelNameToChannelMap.get(event.getChannelName());
104-
105-
if (channel != null) {
106-
channel.handleEvent(event);
103+
String channelName = event.getChannelName();
104+
if (channelName != null) {
105+
final InternalChannel channel = channelNameToChannelMap.get(channelName);
106+
if (channel != null) {
107+
channel.handleEvent(event);
108+
}
107109
}
108110
}
109111

src/main/java/com/pusher/client/channel/impl/PrivateChannelImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public void trigger(final String eventName, final String data) {
6161
);
6262
}
6363

64-
connection.sendMessage(new PusherEvent(eventName, name, null, data).toJson());
64+
String json = new PusherEvent(eventName, name, null, data).toJson();
65+
connection.sendMessage(json);
6566
}
6667

6768
/* Base class overrides */

src/test/java/com/pusher/client/channel/PusherEventTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,11 @@ public void testUserIdIsExtracted() {
4545
);
4646
assertEquals("my-user-id", e.getUserId());
4747
}
48-
}
48+
@Test
49+
public void testErrorData() {
50+
final PusherEvent e = PusherEvent.fromJson(
51+
"{\"event\":\"pusher:error\",\"data\":{\"code\":null,\"message\":\"Invalid key in subscription auth data: '<YOUR PUSHER KEY>'\"}}"
52+
);
53+
assertEquals("{\"code\":null,\"message\":\"Invalid key in subscription auth data: '<YOUR PUSHER KEY>'\"}", e.getData());
54+
}
55+
}

src/test/java/com/pusher/client/channel/impl/PrivateChannelImplTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,7 @@ public void testTriggerWithValidEventSendsMessage() {
143143

144144
verify(mockConnection)
145145
.sendMessage(
146-
"{\"data\":\"{\\\"fish\\\":\\\"chips\\\"}\",\"channel\":\"" +
147-
getChannelName() +
148-
"\",\"event\":\"client-myEvent\"}"
146+
"{\"event\":\"client-myEvent\",\"channel\":\"" + getChannelName() + "\",\"data\":\"{\\\"fish\\\":\\\"chips\\\"}\"}"
149147
);
150148
}
151149

@@ -173,7 +171,7 @@ public void testTriggerWithString() {
173171
((PrivateChannelImpl) channel).trigger("client-myEvent", "string");
174172

175173
verify(mockConnection)
176-
.sendMessage("{\"data\":\"string\",\"channel\":\"" + channel.getName() + "\",\"event\":\"client-myEvent\"}");
174+
.sendMessage("{\"event\":\"client-myEvent\",\"channel\":\"" + channel.getName() + "\",\"data\":\"string\"}");
177175
}
178176

179177
@Test(expected = IllegalStateException.class)

0 commit comments

Comments
 (0)