Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ably/ably-java
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: f8401c3393a3c06ad4ce56c343ca3b99cbc06f8f
Choose a base ref
..
head repository: ably/ably-java
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 9c1459207a7e5571d9882b21ed0e20eeb781e623
Choose a head ref
Showing with 122 additions and 1 deletion.
  1. +122 −1 lib/src/main/java/io/ably/lib/types/Message.java
123 changes: 122 additions & 1 deletion lib/src/main/java/io/ably/lib/types/Message.java
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.JsonArray;
@@ -95,6 +96,73 @@ public static class Operation {
public String clientId;
public String description;
public Map<String, String> metadata;

void write(MessagePacker packer) throws IOException {
packer.packMapHeader(3);
if(clientId != null) {
packer.packString("clientId");
packer.packString(clientId);
}
if(description != null) {
packer.packString("description");
packer.packString(description);
}
if(metadata != null) {
packer.packString("metadata");
packer.packMapHeader(metadata.size());
for(Map.Entry<String, String> entry : metadata.entrySet()) {
packer.packString(entry.getKey());
packer.packString(entry.getValue());
}
}
}

protected static Operation read(final MessageUnpacker unpacker) throws IOException {
Operation operation = new Operation();
int fieldCount = unpacker.unpackMapHeader();
for (int i = 0; i < fieldCount; i++) {
String fieldName = unpacker.unpackString().intern();
switch (fieldName) {
case "clientId":
operation.clientId = unpacker.unpackString();
break;
case "description":
operation.description = unpacker.unpackString();
break;
case "metadata":
int mapSize = unpacker.unpackMapHeader();
operation.metadata = new HashMap<>(mapSize);
for (int j = 0; j < mapSize; j++) {
String key = unpacker.unpackString();
String value = unpacker.unpackString();
operation.metadata.put(key, value);
}
break;
default:
unpacker.skipValue();
break;
}
}
return operation;
}

protected static Operation read(final JsonObject jsonObject) throws MessageDecodeException {
Operation operation = new Operation();
if (jsonObject.has("clientId")) {
operation.clientId = jsonObject.get("clientId").getAsString();
}
if (jsonObject.has("description")) {
operation.description = jsonObject.get("description").getAsString();
}
if (jsonObject.has("metadata")) {
JsonObject metadataObject = jsonObject.getAsJsonObject("metadata");
operation.metadata = new HashMap<>();
for (Map.Entry<String, JsonElement> entry : metadataObject.entrySet()) {
operation.metadata.put(entry.getKey(), entry.getValue().getAsString());
}
}
return operation;
}
}

private static final String NAME = "name";
@@ -104,6 +172,9 @@ public static class Operation {
private static final String VERSION = "version";
private static final String ACTION = "action";
private static final String CREATED_AT = "createdAt";
private static final String REF_SERIAL = "refSerial";
private static final String REF_TYPE = "refType";
private static final String OPERATION = "operation";

/**
* Default constructor
@@ -183,10 +254,15 @@ void writeMsgpack(MessagePacker packer) throws IOException {
int fieldCount = super.countFields();
if(name != null) ++fieldCount;
if(extras != null) ++fieldCount;
if(connectionKey != null) ++fieldCount;
if(serial != null) ++fieldCount;
if(version != null) ++fieldCount;
if(action != null) ++fieldCount;
if(createdAt != null) ++fieldCount;
if(refSerial != null) ++fieldCount;
if(refType != null) ++fieldCount;
if(operation != null) ++fieldCount;

packer.packMapHeader(fieldCount);
super.writeFields(packer);
if(name != null) {
@@ -197,6 +273,10 @@ void writeMsgpack(MessagePacker packer) throws IOException {
packer.packString(EXTRAS);
extras.write(packer);
}
if(connectionKey != null) {
packer.packString(CONNECTION_KEY);
packer.packString(connectionKey);
}
if(serial != null) {
packer.packString(SERIAL);
packer.packString(serial);
@@ -213,6 +293,18 @@ void writeMsgpack(MessagePacker packer) throws IOException {
packer.packString(CREATED_AT);
packer.packLong(createdAt);
}
if(refSerial != null) {
packer.packString(REF_SERIAL);
packer.packString(refSerial);
}
if(refType != null) {
packer.packString(REF_TYPE);
packer.packString(refType);
}
if(operation != null) {
packer.packString(OPERATION);
operation.write(packer);
}
}

Message readMsgpack(MessageUnpacker unpacker) throws IOException {
@@ -232,6 +324,8 @@ Message readMsgpack(MessageUnpacker unpacker) throws IOException {
name = unpacker.unpackString();
} else if (fieldName.equals(EXTRAS)) {
extras = MessageExtras.read(unpacker);
} else if (fieldName.equals(CONNECTION_KEY)) {
connectionKey = unpacker.unpackString();
} else if (fieldName.equals(SERIAL)) {
serial = unpacker.unpackString();
} else if (fieldName.equals(VERSION)) {
@@ -240,7 +334,14 @@ Message readMsgpack(MessageUnpacker unpacker) throws IOException {
action = MessageAction.tryFindByOrdinal(unpacker.unpackInt());
} else if (fieldName.equals(CREATED_AT)) {
createdAt = unpacker.unpackLong();
} else {
} else if (fieldName.equals(REF_SERIAL)) {
refSerial = unpacker.unpackString();
} else if (fieldName.equals(REF_TYPE)) {
refType = unpacker.unpackString();
} else if (fieldName.equals(OPERATION)) {
operation = Operation.read(unpacker);
}
else {
Log.v(TAG, "Unexpected field: " + fieldName);
unpacker.skipValue();
}
@@ -396,12 +497,23 @@ protected void read(final JsonObject map) throws MessageDecodeException {
}
extras = MessageExtras.read((JsonObject) extrasElement);
}
connectionKey = readString(map, CONNECTION_KEY);

serial = readString(map, SERIAL);
version = readString(map, VERSION);
Integer actionOrdinal = readInt(map, ACTION);
action = actionOrdinal == null ? null : MessageAction.tryFindByOrdinal(actionOrdinal);
createdAt = readLong(map, CREATED_AT);
refSerial = readString(map, REF_SERIAL);
refType = readString(map, REF_TYPE);

final JsonElement operationElement = map.get(OPERATION);
if (null != operationElement) {
if (!(operationElement instanceof JsonObject)) {
throw MessageDecodeException.fromDescription("Message operation is of type \"" + operationElement.getClass() + "\" when expected a JSON object.");
}
operation = Operation.read((JsonObject) operationElement);
}
}

public static class Serializer implements JsonSerializer<Message>, JsonDeserializer<Message> {
@@ -429,6 +541,15 @@ public JsonElement serialize(Message message, Type typeOfMessage, JsonSerializat
if (message.createdAt != null) {
json.addProperty(CREATED_AT, message.createdAt);
}
if (message.refSerial != null) {
json.addProperty(REF_SERIAL, message.refSerial);
}
if (message.refType != null) {
json.addProperty(REF_TYPE, message.refType);
}
if (message.operation != null) {
json.add(OPERATION, Serialisation.gson.toJsonTree(message.operation));
}
return json;
}