From fc0789b3eae88804df56717712f0ce027ee88071 Mon Sep 17 00:00:00 2001 From: Leon Ziegler Date: Fri, 11 Apr 2014 13:58:44 +0200 Subject: [PATCH] special rule for binary attachements --- src/rsb/converter/XOPConverter.java | 78 +++++++++++++++++++++--------- src/rsb/transport/Attachment.java | 11 +++-- test/rsb/transport/XOMXOPTest.java | 1 - test/rsb/transport/XOPConverterTest.java | 54 +++++++++++++++++++-- test/rsb/transport/XOPTest.java | 3 +- 5 files changed, 113 insertions(+), 34 deletions(-) diff --git a/src/rsb/converter/XOPConverter.java b/src/rsb/converter/XOPConverter.java index 5c74597..c85a1f0 100644 --- a/src/rsb/converter/XOPConverter.java +++ b/src/rsb/converter/XOPConverter.java @@ -3,23 +3,35 @@ package rsb.converter; import com.google.protobuf.ByteString; import java.nio.ByteBuffer; import java.util.Iterator; +import java.util.logging.Logger; import rsb.transport.XOP; import rsb.transport.XOP.SyntaxException; import rst.xml.XOPType; /** - * RSB converter for {@link rsb.xml.XOP} objects. - * + * RSB converter for {@link rsb.xml.XOP} objects. This converter is independent + * of the data that is set to the XOP's attachment. Is is expected that the + * attachment's data is convertible as well. + * * @author lkettenb */ public class XOPConverter implements Converter { + private static final Logger LOG = Logger.getLogger(XOPConverter.class + .getName()); + private ConverterSignature signatur = new ConverterSignature(".rst.xml.XOP", XOP.class); private ProtocolBufferConverter pbc; private ConverterSelectionStrategy serializationConverters = null; private ConverterSelectionStrategy deserializationConverters = null; + /** + * Signature for binary attachments. + */ + private final ConverterSignature byteSignature = new ConverterSignature( + "bytes", ByteBuffer.class); + public XOPConverter() { pbc = new ProtocolBufferConverter (XOPType.XOP.getDefaultInstance()); @@ -38,8 +50,7 @@ public class XOPConverter implements Converter { } @Override - public UserData deserialize(String wireSchema, ByteBuffer buffer) - throws ConversionException { + public UserData deserialize(String wireSchema, ByteBuffer buffer) throws ConversionException { UserData result = pbc.deserialize(wireSchema, buffer); XOPType.XOP data = (XOPType.XOP) result.getData(); XOP xop = convertRSTtoRSB(data); @@ -69,20 +80,34 @@ public class XOPConverter implements Converter { } for (XOPType.Attachment attachment : data.getAttachmentsList()) { - if (attachment.hasUrl() && attachment.hasData()) { + if (!attachment.hasUrl() || !attachment.hasData()) { + LOG.warning("received incomplete attachement!"); + continue; + } + + String wireSchema = attachment.getWireSchema().toStringUtf8(); + ByteBuffer dataBuffer = ByteBuffer.wrap(attachment.getData() + .toByteArray()); + rsb.transport.Attachment att; + + if (wireSchema.equals(byteSignature.getSchema())) { + att = new rsb.transport.Attachment(dataBuffer, + byteSignature.getDataType()); + } else { + Converter c = deserializationConverters - .getConverter(attachment.getWireSchema().toStringUtf8()); + .getConverter(wireSchema); - ByteBuffer dataBuffer - = ByteBuffer.wrap(attachment.getData().toByteArray()); - UserData deserializedAttachment - = c.deserialize(c.getSignature().getSchema(), dataBuffer); + @SuppressWarnings("rawtypes") + UserData deserializedAttachment = c.deserialize(c + .getSignature().getSchema(), dataBuffer); - rsb.transport.Attachment att - = new rsb.transport.Attachment(deserializedAttachment.getData(), - deserializedAttachment.getTypeInfo()); - xop.setAttachment(attachment.getUrl(), att); + att = new rsb.transport.Attachment( + deserializedAttachment.getData(), + deserializedAttachment.getTypeInfo()); } + xop.setAttachment(attachment.getUrl(), att); + } return xop; } @@ -117,14 +142,23 @@ public class XOPConverter implements Converter { XOPType.Attachment.Builder attachmentBuilder = XOPType.Attachment.newBuilder(); - Converter c - = serializationConverters - .getConverter(attachment.getType().getName()); - - WireContents result - = c.serialize(attachment.getType(), attachment.getData()); - attachmentBuilder.setWireSchema(ByteString.copyFrom(result.getWireSchema().getBytes())); - attachmentBuilder.setData(ByteString.copyFrom(result.getSerialization().asReadOnlyBuffer())); + if (attachment.getType().equals(byteSignature.getDataType())) { + ByteBuffer bb = (ByteBuffer) attachment.getData(); + attachmentBuilder.setWireSchema(ByteString + .copyFrom(byteSignature.getSchema().getBytes())); + attachmentBuilder.setData(ByteString.copyFrom(bb + .asReadOnlyBuffer())); + } else { + Converter c = serializationConverters + .getConverter(attachment.getType().getName()); + + WireContents result = c.serialize( + attachment.getType(), attachment.getData()); + attachmentBuilder.setWireSchema(ByteString.copyFrom(result + .getWireSchema().getBytes())); + attachmentBuilder.setData(ByteString.copyFrom(result + .getSerialization().asReadOnlyBuffer())); + } attachmentBuilder.setUrl(url); xopBuilder.addAttachments(attachmentBuilder.build()); } diff --git a/src/rsb/transport/Attachment.java b/src/rsb/transport/Attachment.java index 41b8961..989086b 100644 --- a/src/rsb/transport/Attachment.java +++ b/src/rsb/transport/Attachment.java @@ -1,5 +1,6 @@ package rsb.transport; +import java.nio.ByteBuffer; import java.util.Arrays; import java.util.logging.Level; import java.util.logging.Logger; @@ -25,12 +26,12 @@ public class Attachment implements Cloneable { private UserData data; /** - * Construct an {@link Attachment} containing the data. - * + * Constructs an {@link Attachment} containing the data. Attention: For + * transferring binary data use {@link ByteBuffer}. * * @param data The piece of data which should be stored in the attachment. * This can be any Java object, but the same restrictions as for event - * payloads apply. + * payloads apply. Use {@link ByteBuffer} for transferring binary data. * @param type Data type which can be interpreted by RSB's event handling * machinery. * @@ -46,14 +47,14 @@ public class Attachment implements Cloneable { * Copy constructor. * @param a */ - @SuppressWarnings("unchecked") + @SuppressWarnings({ "unchecked", "rawtypes" }) public Attachment(Attachment a) { data = new UserData(a.data.getData(), a.data.getTypeInfo()); } /** * Returns the type of this attachment. Use {@link Class#getName()} when - * looking for the appropirate converter of this attachment. + * looking for the appropriate converter of this attachment. * * @return Type of this attachment. */ diff --git a/test/rsb/transport/XOMXOPTest.java b/test/rsb/transport/XOMXOPTest.java index 4b02b8b..5abd5b8 100644 --- a/test/rsb/transport/XOMXOPTest.java +++ b/test/rsb/transport/XOMXOPTest.java @@ -90,7 +90,6 @@ public class XOMXOPTest { } @Test - @SuppressWarnings("deprecation") public void testNonXML() { XOMXOP xop = new XOMXOP(); try { diff --git a/test/rsb/transport/XOPConverterTest.java b/test/rsb/transport/XOPConverterTest.java index 780142d..09ea40d 100644 --- a/test/rsb/transport/XOPConverterTest.java +++ b/test/rsb/transport/XOPConverterTest.java @@ -33,7 +33,7 @@ public class XOPConverterTest { public void setUp() { // Register default converters (including String converter). DefaultConverters.register(); - ConverterRepository rep = DefaultConverterRepository.getDefaultConverterRepository(); + ConverterRepository rep = DefaultConverterRepository.getDefaultConverterRepository(); rep.addConverter( new ProtocolBufferConverter( Gaussian1DType.Gaussian1D.getDefaultInstance())); @@ -52,7 +52,7 @@ public class XOPConverterTest { WireContents serializedData = converter.serialize(XOP.class, xop1); - UserData deserializedUserData = + UserData deserializedUserData = converter.deserialize( serializedData.getWireSchema(), serializedData.getSerialization()); @@ -76,6 +76,49 @@ public class XOPConverterTest { } @Test + public void testDeSerializeBinaryAttachment() throws ConversionException, SyntaxException { + String xml1 = "" + "" + + " " + " " + + ""; + long id = 156546546; + XOP xop1 = new XOP(xml1, id); + + // create data + float[] floatData = {0.0f,1.0f,2.0f}; + ByteBuffer bb = ByteBuffer.allocate(4 * floatData.length); + for (int i = 0; i < floatData.length; i++) { + bb.putFloat(floatData[i]); + } + + rsb.transport.Attachment a1 = new rsb.transport.Attachment(bb, ByteBuffer.class); + xop1.setAttachment("attachment", a1); + + WireContents serializedData = converter.serialize(XOP.class, xop1); + + UserData deserializedUserData = + converter.deserialize( + serializedData.getWireSchema(), + serializedData.getSerialization()); + + assertTrue(deserializedUserData.getTypeInfo().equals(xop1.getClass())); + + XOP deserializedXOP = (XOP) deserializedUserData.getData(); + assertTrue(deserializedXOP.getID() == xop1.getID()); + assertTrue(deserializedXOP.getID() != 156546545); + assertTrue(deserializedXOP.hasID()); + + assertTrue(deserializedXOP.hasAttachment("attachment")); + rsb.transport.Attachment a2 = deserializedXOP.getAttachment("attachment"); + + ByteBuffer a1bb = (ByteBuffer) a1.getData(); + ByteBuffer a2bb = (ByteBuffer) a2.getData(); + assertTrue(a1bb.equals(a2bb)); + + assertTrue(a1.equals(a2)); + assertTrue(deserializedXOP.equals(xop1)); + } + + @Test public void testDeSerializeUint64() throws ConversionException, SyntaxException { String xml1 = "" + "" + " " + " " @@ -88,7 +131,7 @@ public class XOPConverterTest { WireContents serializedData = converter.serialize(XOP.class, xop1); - UserData deserializedUserData = + UserData deserializedUserData = converter.deserialize( serializedData.getWireSchema(), serializedData.getSerialization()); @@ -125,7 +168,7 @@ public class XOPConverterTest { WireContents serializedData = converter.serialize(XOP.class, xop1); - UserData deserializedUserData = + UserData deserializedUserData = converter.deserialize( serializedData.getWireSchema(), serializedData.getSerialization()); @@ -167,7 +210,8 @@ public class XOPConverterTest { WireContents serializedData = xopsConverter.serialize(XOPs.class, xops); - UserData deserializedUserData = + @SuppressWarnings("rawtypes") + UserData deserializedUserData = xopsConverter.deserialize( serializedData.getWireSchema(), serializedData.getSerialization()); diff --git a/test/rsb/transport/XOPTest.java b/test/rsb/transport/XOPTest.java index 130567f..0468a1e 100644 --- a/test/rsb/transport/XOPTest.java +++ b/test/rsb/transport/XOPTest.java @@ -53,7 +53,8 @@ public class XOPTest { @Test public void testInValidDoc() { try { - XOP xop = new XOP("test"); + @SuppressWarnings("unused") + XOP xop = new XOP("test"); } catch (SyntaxException ex) { assertTrue(true); } -- 1.7.9.5