ARTEMIS-3315 Fix JSON serialization of AMQP messages

Replace the JSON serialization errors for unknown types with their respective string representation.
This commit is contained in:
Domenico Francesco Bruscino 2021-05-26 16:50:50 +02:00 committed by Clebert Suconic
parent cdaa00d470
commit ba6422d45f
2 changed files with 50 additions and 3 deletions

View File

@ -34,7 +34,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.apache.activemq.artemis.core.client.ActiveMQClientMessageBundle;
import org.apache.activemq.artemis.utils.Base64; import org.apache.activemq.artemis.utils.Base64;
import org.apache.activemq.artemis.utils.JsonLoader; import org.apache.activemq.artemis.utils.JsonLoader;
import org.apache.activemq.artemis.utils.ObjectInputStreamWithClassLoader; import org.apache.activemq.artemis.utils.ObjectInputStreamWithClassLoader;
@ -159,6 +158,8 @@ public final class JsonUtil {
jsonObjectBuilder.add(key, (Short) param); jsonObjectBuilder.add(key, (Short) param);
} else if (param instanceof Byte) { } else if (param instanceof Byte) {
jsonObjectBuilder.add(key, ((Byte) param).shortValue()); jsonObjectBuilder.add(key, ((Byte) param).shortValue());
} else if (param instanceof Number) {
jsonObjectBuilder.add(key, ((Number)param).doubleValue());
} else if (param instanceof SimpleString) { } else if (param instanceof SimpleString) {
jsonObjectBuilder.add(key, param.toString()); jsonObjectBuilder.add(key, param.toString());
} else if (param == null) { } else if (param == null) {
@ -173,7 +174,7 @@ public final class JsonUtil {
} }
jsonObjectBuilder.add(key, objectArrayBuilder); jsonObjectBuilder.add(key, objectArrayBuilder);
} else { } else {
throw ActiveMQClientMessageBundle.BUNDLE.invalidManagementParam(param.getClass().getName()); jsonObjectBuilder.add(key, param.toString());
} }
} }
@ -195,6 +196,8 @@ public final class JsonUtil {
jsonArrayBuilder.add((Short) param); jsonArrayBuilder.add((Short) param);
} else if (param instanceof Byte) { } else if (param instanceof Byte) {
jsonArrayBuilder.add(((Byte) param).shortValue()); jsonArrayBuilder.add(((Byte) param).shortValue());
} else if (param instanceof Number) {
jsonArrayBuilder.add(((Number)param).doubleValue());
} else if (param == null) { } else if (param == null) {
jsonArrayBuilder.addNull(); jsonArrayBuilder.addNull();
} else if (param instanceof byte[]) { } else if (param instanceof byte[]) {
@ -216,7 +219,7 @@ public final class JsonUtil {
} }
jsonArrayBuilder.add(objectArrayBuilder); jsonArrayBuilder.add(objectArrayBuilder);
} else { } else {
throw ActiveMQClientMessageBundle.BUNDLE.invalidManagementParam(param.getClass().getName()); jsonArrayBuilder.add(param.toString());
} }
} }

View File

@ -29,6 +29,9 @@ import org.apache.activemq.transport.amqp.client.AmqpMessage;
import org.apache.activemq.transport.amqp.client.AmqpSender; import org.apache.activemq.transport.amqp.client.AmqpSender;
import org.apache.activemq.transport.amqp.client.AmqpSession; import org.apache.activemq.transport.amqp.client.AmqpSession;
import org.apache.qpid.proton.amqp.Binary; import org.apache.qpid.proton.amqp.Binary;
import org.apache.qpid.proton.amqp.Decimal128;
import org.apache.qpid.proton.amqp.Decimal32;
import org.apache.qpid.proton.amqp.Decimal64;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -39,8 +42,10 @@ import javax.jms.Session;
import javax.jms.TextMessage; import javax.jms.TextMessage;
import javax.json.JsonArray; import javax.json.JsonArray;
import javax.json.JsonObject; import javax.json.JsonObject;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Map; import java.util.Map;
import java.util.UUID;
public class JMXManagementTest extends JMSClientTestSupport { public class JMXManagementTest extends JMSClientTestSupport {
@ -137,6 +142,45 @@ public class JMXManagementTest extends JMSClientTestSupport {
} }
} }
@Test
public void testGetFirstMessageWithAMQPTypes() throws Exception {
AmqpClient client = createAmqpClient();
AmqpConnection connection = addConnection(client.connect());
try {
UUID uuid = UUID.randomUUID();
Character character = new Character('C');
AmqpSession session = connection.createSession();
AmqpSender sender = session.createSender(getQueueName());
session.begin();
AmqpMessage message = new AmqpMessage();
message.setApplicationProperty("TEST_UUID", uuid);
message.setApplicationProperty("TEST_CHAR", character);
message.setApplicationProperty("TEST_DECIMAL_32", new Decimal32(BigDecimal.ONE));
message.setApplicationProperty("TEST_DECIMAL_64", new Decimal64(BigDecimal.ONE));
message.setApplicationProperty("TEST_DECIMAL_128", new Decimal128(BigDecimal.ONE));
sender.send(message);
session.commit();
SimpleString queue = new SimpleString(getQueueName());
QueueControl queueControl = createManagementControl(queue, queue);
String firstMessageAsJSON = queueControl.getFirstMessageAsJSON();
Assert.assertNotNull(firstMessageAsJSON);
JsonObject firstMessageObject = JsonUtil.readJsonArray(firstMessageAsJSON).getJsonObject(0);
Assert.assertEquals(uuid.toString(), firstMessageObject.getString("TEST_UUID"));
Assert.assertEquals(character.toString(), firstMessageObject.getString("TEST_CHAR"));
Assert.assertNotNull(firstMessageObject.getJsonNumber("TEST_DECIMAL_32"));
Assert.assertNotNull(firstMessageObject.getJsonNumber("TEST_DECIMAL_64"));
Assert.assertNotNull(firstMessageObject.getJsonNumber("TEST_DECIMAL_128"));
} finally {
connection.close();
}
}
@Test @Test
public void testAddressSizeOnDelete() throws Exception { public void testAddressSizeOnDelete() throws Exception {
AmqpClient client = createAmqpClient(); AmqpClient client = createAmqpClient();