From 680d082a179902d14cf38a8afde7c2c12a062106 Mon Sep 17 00:00:00 2001 From: Timothy Bish Date: Tue, 23 Aug 2016 09:58:05 -0400 Subject: [PATCH] https://issues.apache.org/jira/browse/AMQ-6404 Add check for unmarshalled text body and handle that case. (cherry picked from commit 1030fb184216d00e3fd43a416931acd3b2fc6794) --- .../amqp/message/ActiveMQJMSVendor.java | 3 ++ .../JMSMappingOutboundTransformerTest.java | 37 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/activemq-amqp/src/main/java/org/apache/activemq/transport/amqp/message/ActiveMQJMSVendor.java b/activemq-amqp/src/main/java/org/apache/activemq/transport/amqp/message/ActiveMQJMSVendor.java index efd50174e9..9b5a4ab015 100644 --- a/activemq-amqp/src/main/java/org/apache/activemq/transport/amqp/message/ActiveMQJMSVendor.java +++ b/activemq-amqp/src/main/java/org/apache/activemq/transport/amqp/message/ActiveMQJMSVendor.java @@ -17,6 +17,7 @@ package org.apache.activemq.transport.amqp.message; import java.io.DataInputStream; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import java.util.Set; @@ -343,6 +344,8 @@ public class ActiveMQJMSVendor { // Message includes a size prefix of four bytes for the OpenWire marshaler result = new Binary(contents.getData(), contents.getOffset() + 4, contents.getLength() - 4); } + } else if (textMessage.getText() != null) { + result = new Binary(textMessage.getText().getBytes(StandardCharsets.UTF_8)); } return result; diff --git a/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/message/JMSMappingOutboundTransformerTest.java b/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/message/JMSMappingOutboundTransformerTest.java index 9184101c1c..021ab85339 100644 --- a/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/message/JMSMappingOutboundTransformerTest.java +++ b/activemq-amqp/src/test/java/org/apache/activemq/transport/amqp/message/JMSMappingOutboundTransformerTest.java @@ -605,6 +605,27 @@ public class JMSMappingOutboundTransformerTest { assertEquals(contentString, contents); } + @Test + public void testConvertTextMessageContentNotStoredCreatesBodyUsingOriginalEncodingWithDataSection() throws Exception { + String contentString = "myTextMessageContent"; + ActiveMQTextMessage outbound = createTextMessage(contentString); + outbound.setShortProperty(AMQP_ORIGINAL_ENCODING_KEY, AMQP_DATA); + outbound.onSend(); + + ActiveMQJMSVendor vendor = createVendor(); + JMSMappingOutboundTransformer transformer = new JMSMappingOutboundTransformer(vendor); + + Message amqp = transformer.convert(outbound); + + assertNotNull(amqp.getBody()); + assertTrue(amqp.getBody() instanceof Data); + assertTrue(((Data) amqp.getBody()).getValue() instanceof Binary); + + Binary data = ((Data) amqp.getBody()).getValue(); + String contents = new String(data.getArray(), data.getArrayOffset(), data.getLength(), StandardCharsets.UTF_8); + assertEquals(contentString, contents); + } + @Test public void testConvertTextMessageCreatesAmqpValueStringBody() throws Exception { String contentString = "myTextMessageContent"; @@ -622,6 +643,22 @@ public class JMSMappingOutboundTransformerTest { assertEquals(contentString, ((AmqpValue) amqp.getBody()).getValue()); } + @Test + public void testConvertTextMessageContentNotStoredCreatesAmqpValueStringBody() throws Exception { + String contentString = "myTextMessageContent"; + ActiveMQTextMessage outbound = createTextMessage(contentString); + outbound.onSend(); + + ActiveMQJMSVendor vendor = createVendor(); + JMSMappingOutboundTransformer transformer = new JMSMappingOutboundTransformer(vendor); + + Message amqp = transformer.convert(outbound); + + assertNotNull(amqp.getBody()); + assertTrue(amqp.getBody() instanceof AmqpValue); + assertEquals(contentString, ((AmqpValue) amqp.getBody()).getValue()); + } + @Test public void testConvertCompressedTextMessageCreatesDataSectionBody() throws Exception { String contentString = "myTextMessageContent";