From 656b9e1924644b79ac9d0b6b5f7f8e4bf1a9174c Mon Sep 17 00:00:00 2001 From: Ingo Weiss Date: Wed, 28 Feb 2018 16:03:00 +0000 Subject: [PATCH] [ARTEMIS-1716] QueueControlImpl#listMessagesAsJSON fails when message contains a byte[] header Issue: https://issues.apache.org/jira/browse/ARTEMIS-1716 --- .../activemq/artemis/api/core/JsonUtil.java | 16 ++++++++++++ .../artemis/api/core/JsonUtilTest.java | 26 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/JsonUtil.java b/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/JsonUtil.java index 0bfae27d77..fdb5b52797 100644 --- a/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/JsonUtil.java +++ b/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/JsonUtil.java @@ -208,6 +208,9 @@ public final class JsonUtil { jsonObjectBuilder.add(key, param.toString()); } else if (param == null) { jsonObjectBuilder.addNull(key); + } else if (param instanceof byte[]) { + JsonArrayBuilder byteArrayObject = toJsonArrayBuilder((byte[]) param); + jsonObjectBuilder.add(key, byteArrayObject); } else { throw ActiveMQClientMessageBundle.BUNDLE.invalidManagementParam(param.getClass().getName()); } @@ -233,6 +236,9 @@ public final class JsonUtil { jsonArrayBuilder.add(((Byte) param).shortValue()); } else if (param == null) { jsonArrayBuilder.addNull(); + } else if (param instanceof byte[]) { + JsonArrayBuilder byteArrayObject = toJsonArrayBuilder((byte[]) param); + jsonArrayBuilder.add(byteArrayObject); } else { throw ActiveMQClientMessageBundle.BUNDLE.invalidManagementParam(param.getClass().getName()); } @@ -258,6 +264,16 @@ public final class JsonUtil { return jsonObjectBuilder.build(); } + public static JsonArrayBuilder toJsonArrayBuilder(byte[] byteArray) { + JsonArrayBuilder jsonArrayBuilder = JsonLoader.createArrayBuilder(); + if (byteArray != null) { + for (int i = 0; i < byteArray.length; i++) { + jsonArrayBuilder.add(((Byte) byteArray[i]).shortValue()); + } + } + return jsonArrayBuilder; + } + public static JsonArray readJsonArray(String jsonString) { return Json.createReader(new StringReader(jsonString)).readArray(); } diff --git a/artemis-core-client/src/test/java/org/apache/activemq/artemis/api/core/JsonUtilTest.java b/artemis-core-client/src/test/java/org/apache/activemq/artemis/api/core/JsonUtilTest.java index 2873bab568..f6740ddd07 100644 --- a/artemis-core-client/src/test/java/org/apache/activemq/artemis/api/core/JsonUtilTest.java +++ b/artemis-core-client/src/test/java/org/apache/activemq/artemis/api/core/JsonUtilTest.java @@ -53,4 +53,30 @@ public class JsonUtilTest { Assert.assertEquals(2, jsonArray.size()); } + + @Test + public void testAddByteArrayToJsonObject() { + JsonObjectBuilder jsonObjectBuilder = JsonLoader.createObjectBuilder(); + byte[] bytes = {0x0a, 0x1b, 0x2c, 0x3d, 0x4e, 0x5f}; + + JsonUtil.addToObject("not-null", "not-null", jsonObjectBuilder); + JsonUtil.addToObject("byteArray", bytes, jsonObjectBuilder); + JsonUtil.addToObject("null", null, jsonObjectBuilder); + + JsonObject jsonObject = jsonObjectBuilder.build(); + + Assert.assertTrue(jsonObject.containsKey("byteArray")); + Assert.assertEquals(6, jsonObject.getJsonArray("byteArray").size()); + } + + @Test public void testAddByteArrayToJsonArray() { + JsonArrayBuilder jsonArrayBuilder = JsonLoader.createArrayBuilder(); + byte[] bytes = {0x0a, 0x1b, 0x2c, 0x3d, 0x4e, 0x5f}; + + JsonUtil.addToArray(bytes, jsonArrayBuilder); + + JsonArray jsonArray = jsonArrayBuilder.build(); + + Assert.assertEquals(1, jsonArray.size()); + } }