Skip to content

Commit 0e4da0f

Browse files
Merge pull request ably#630 from ably/add-connection-key
Add connectionKey attribute missing from the Message object
2 parents 7fc5dd3 + cc1df58 commit 0e4da0f

4 files changed

Lines changed: 110 additions & 14 deletions

File tree

lib/src/main/java/io/ably/lib/types/BaseMessage.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ public class BaseMessage implements Cloneable {
6060
private static final String DATA = "data";
6161

6262
/**
63-
* Generate a String summary of this BaseMessage
64-
* @return string
63+
* Generate a String summary of this BaseMessage.
6564
*/
6665
public void getDetails(StringBuilder builder) {
6766
if(clientId != null)

lib/src/main/java/io/ably/lib/types/Message.java

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,23 @@
2525
public class Message extends BaseMessage {
2626

2727
/**
28-
* The event name, if available
28+
* The event name, if available.
2929
*/
3030
public String name;
3131

3232
/**
33-
* Extras, if available
33+
* Extras, if available.
3434
*/
3535
public MessageExtras extras;
3636

37+
/**
38+
* Key needed only in case one client is publishing this message on behalf of another client.
39+
*/
40+
public String connectionKey;
41+
3742
private static final String NAME = "name";
3843
private static final String EXTRAS = "extras";
44+
private static final String CONNECTION_KEY = "connectionKey";
3945

4046
/**
4147
* Default constructor
@@ -44,29 +50,44 @@ public Message() {
4450
}
4551

4652
/**
47-
* Construct a message from event name and data
48-
* @param name
49-
* @param data
53+
* Construct a message from event name and data.
54+
*
55+
* @param name the event name
56+
* @param data the message payload
5057
*/
5158
public Message(String name, Object data) {
5259
this(name, data, null, null);
5360
}
5461

55-
62+
/**
63+
* Construct a message from name, data, and client id.
64+
*
65+
* @param name the event name
66+
* @param data the message payload
67+
* @param clientId the client identifier
68+
*/
5669
public Message(String name, Object data, String clientId) {
5770
this(name, data, clientId, null);
5871
}
5972

73+
/**
74+
* Construct a message from name, data, and extras.
75+
*
76+
* @param name the event name
77+
* @param data the message payload
78+
* @param extras extra information to be sent with this message
79+
*/
6080
public Message(String name, Object data, MessageExtras extras) {
6181
this(name, data, null, extras);
6282
}
6383

6484
/**
65-
* Generic constructor
66-
* @param name
67-
* @param data
68-
* @param clientId
69-
* @param extras
85+
* Construct a message from name, data, client id, and extras.
86+
*
87+
* @param name the event name
88+
* @param data the message payload
89+
* @param clientId the client identifier
90+
* @param extras extra information to be sent with this message
7091
*/
7192
public Message(String name, Object data, String clientId, MessageExtras extras) {
7293
this.name = name;
@@ -273,6 +294,9 @@ public JsonElement serialize(Message message, Type typeOfMessage, JsonSerializat
273294
if (message.extras != null) {
274295
json.add(EXTRAS, Serialisation.gson.toJsonTree(message.extras));
275296
}
297+
if (message.connectionKey != null) {
298+
json.addProperty(CONNECTION_KEY, message.connectionKey);
299+
}
276300
return json;
277301
}
278302

lib/src/test/java/io/ably/lib/test/rest/RestRequestTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ public void onError(ErrorInfo reason) {
464464
/**
465465
* Publish a message using the request() API
466466
* Spec: RSC19a, RSC19b
467-
*
467+
*
468468
*/
469469
@Test
470470
public void request_post() {
@@ -717,4 +717,28 @@ public void onError(ErrorInfo reason) {
717717
}
718718
}
719719

720+
/**
721+
* Trying to publish a message on behalf of another client with invalid connection key.
722+
* Specs: TM2h
723+
*/
724+
@Test
725+
public void request_post_with_invalid_connection_key() throws AblyException {
726+
// Given
727+
DebugOptions opts = new DebugOptions(testVars.keys[0].keyStr);
728+
fillInOptions(opts);
729+
opts.httpListener = new RawHttpTracker();
730+
AblyRest ably = new AblyRest(opts);
731+
Message message = new Message("Test event", "Test data (invalid key)");
732+
message.connectionKey = "invalid";
733+
HttpUtils.JsonRequestBody requestBody = new HttpUtils.JsonRequestBody(message);
734+
735+
// When
736+
HttpPaginatedResponse publishResponse = ably.request(HttpConstants.Methods.POST, channelMessagesPath, null, requestBody, null);
737+
738+
// Then
739+
assertFalse("Verify failure is indicated", publishResponse.success);
740+
assertNotNull("Verify error is indicated", publishResponse.errorMessage);
741+
assertEquals("Verify statusCode is present", publishResponse.statusCode, 400);
742+
assertEquals("Verify errorCode is present", publishResponse.errorCode, 40006);
743+
}
720744
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.ably.lib.types;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNull;
5+
6+
import com.google.gson.JsonElement;
7+
import com.google.gson.JsonObject;
8+
import io.ably.lib.types.Message.Serializer;
9+
import org.junit.Test;
10+
11+
public class MessageTest {
12+
13+
private final Serializer serializer = new Serializer();
14+
15+
@Test
16+
public void serialize_message() {
17+
// Given
18+
Message message = new Message("test-name", "test-data");
19+
message.clientId = "test-client-id";
20+
message.connectionKey = "test-key";
21+
22+
// When
23+
JsonElement serializedElement = serializer.serialize(message, null, null);
24+
25+
// Then
26+
JsonObject serializedObject = serializedElement.getAsJsonObject();
27+
assertEquals("test-client-id", serializedObject.get("clientId").getAsString());
28+
assertEquals("test-key", serializedObject.get("connectionKey").getAsString());
29+
assertEquals("test-data", serializedObject.get("data").getAsString());
30+
assertEquals("test-name", serializedObject.get("name").getAsString());
31+
}
32+
33+
@Test
34+
public void serialize_message_with_name_and_data() {
35+
// Given
36+
Message message = new Message("test-name", "test-data");
37+
38+
// When
39+
JsonElement serializedElement = serializer.serialize(message, null, null);
40+
41+
// Then
42+
JsonObject serializedObject = serializedElement.getAsJsonObject();
43+
assertNull(serializedObject.get("clientId"));
44+
assertNull(serializedObject.get("connectionKey"));
45+
assertNull(serializedObject.get("extras"));
46+
assertEquals("test-data", serializedObject.get("data").getAsString());
47+
assertEquals("test-name", serializedObject.get("name").getAsString());
48+
}
49+
}

0 commit comments

Comments
 (0)