compact byteSequence before xstream marshall so that only required bytes are base64 encoded

This commit is contained in:
gtully 2015-08-26 11:20:18 +01:00
parent 950dc92677
commit 547476d1bc
2 changed files with 42 additions and 2 deletions

View File

@ -19,9 +19,16 @@ package org.apache.activemq.transport.xstream;
import java.io.IOException;
import java.io.Reader;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.ConverterLookup;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import org.apache.activemq.command.MarshallAware;
import org.apache.activemq.command.MessageDispatch;
import org.apache.activemq.transport.util.TextWireFormat;
import org.apache.activemq.util.ByteSequence;
import org.apache.activemq.wireformat.WireFormat;
import com.thoughtworks.xstream.XStream;
@ -110,8 +117,27 @@ public class XStreamWireFormat extends TextWireFormat {
// Implementation methods
// -------------------------------------------------------------------------
protected XStream createXStream() {
XStream xstream = new XStream();
final XStream xstream = new XStream();
xstream.ignoreUnknownElements();
xstream.registerConverter(new Converter() {
final Converter delegate = xstream.getConverterLookup().lookupConverterForType(ByteSequence.class);
@Override
public void marshal(Object o, HierarchicalStreamWriter hierarchicalStreamWriter, MarshallingContext marshallingContext) {
ByteSequence byteSequence = (ByteSequence)o;
byteSequence.compact();
delegate.marshal(byteSequence, hierarchicalStreamWriter, marshallingContext);
}
@Override
public Object unmarshal(HierarchicalStreamReader hierarchicalStreamReader, UnmarshallingContext unmarshallingContext) {
return delegate.unmarshal(hierarchicalStreamReader, unmarshallingContext);
}
@Override
public boolean canConvert(Class aClass) {
return aClass == ByteSequence.class;
}
});
return xstream;
}

View File

@ -18,6 +18,8 @@ package org.apache.activemq.transport.xstream;
import java.io.IOException;
import junit.framework.Test;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.apache.activemq.command.Command;
import org.apache.activemq.command.MessageTest;
import org.apache.activemq.wireformat.WireFormat;
@ -33,7 +35,7 @@ public class XStreamWireFormatTest extends MessageTest {
public void assertBeanMarshalls(Object original) throws IOException {
super.assertBeanMarshalls(original);
String xml = getXStreamWireFormat().marshalText((Command) original);
String xml = getXStreamWireFormat().marshalText(original);
LOG.info(original.getClass().getName() + " as XML is:");
LOG.info(xml);
}
@ -45,4 +47,16 @@ public class XStreamWireFormatTest extends MessageTest {
protected WireFormat createWireFormat() {
return new XStreamWireFormat();
}
public static Test suite() {
return suite(XStreamWireFormatTest.class);
}
public void testXmlPayload() throws Exception {
ActiveMQTextMessage message = new ActiveMQTextMessage();
message.setText("<body val=\"Hi\"/>");
message.setStringProperty("body","Hi");
assertBeanMarshalls(message);
}
}