diff --git a/org.springframework.integration.file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java index ab2b07d7af..74e9117028 100644 --- a/org.springframework.integration.file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java +++ b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java @@ -31,7 +31,14 @@ import org.springframework.util.FileCopyUtils; * A {@link MessageHandler} implementation that writes the Message payload to a * file. If the payload is a File object, it will copy the File to this * consumer's directory. If the payload is a byte array or String, it will - * write it directly. Otherwise, it will invoke toString on the payload Object. + * write it directly. Otherwise, the payload type is unsupported, and an + * Exception will be thrown. + *

+ * Other transformers may be useful to precede this handler. For example, + * any Serializable object payload can be converted into a byte array by the + * {@link org.springframework.integration.transformer.PayloadSerializingTransformer}. + * Likewise, any Object can be converted to a String based on its toString() + * method by the {@link org.springframework.integration.transformer.ObjectToStringTransformer}. * * @author Mark Fisher */ diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/transformer/PayloadDeserializingTransformer.java b/org.springframework.integration/src/main/java/org/springframework/integration/transformer/PayloadDeserializingTransformer.java new file mode 100644 index 0000000000..cc429644ae --- /dev/null +++ b/org.springframework.integration/src/main/java/org/springframework/integration/transformer/PayloadDeserializingTransformer.java @@ -0,0 +1,54 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.transformer; + +import java.io.ByteArrayInputStream; +import java.io.ObjectInputStream; +import java.io.ObjectStreamException; + +/** + * Transformer that deserializes the inbound byte array payload to an object. + *

The byte array payload must be a result of serialization. + * + * @author Mark Fisher + * @since 1.0.1 + */ +public class PayloadDeserializingTransformer extends AbstractPayloadTransformer { + + @Override + protected Object transformPayload(byte[] payload) throws Exception { + ByteArrayInputStream byteStream = new ByteArrayInputStream(payload); + ObjectInputStream objectStream = null; + try { + objectStream = new ObjectInputStream(byteStream); + return objectStream.readObject(); + } + catch (ObjectStreamException e) { + throw new IllegalArgumentException( + "Failed to deserialize payload. Is the byte array a result of Object serialization?", e); + } + finally { + try { + objectStream.close(); + } + catch (Exception e) { + // ignore + } + } + } + +} diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/transformer/PayloadSerializingTransformer.java b/org.springframework.integration/src/main/java/org/springframework/integration/transformer/PayloadSerializingTransformer.java new file mode 100644 index 0000000000..3c8a02ecec --- /dev/null +++ b/org.springframework.integration/src/main/java/org/springframework/integration/transformer/PayloadSerializingTransformer.java @@ -0,0 +1,46 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.transformer; + +import java.io.ByteArrayOutputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +import org.springframework.util.Assert; + +/** + * Transformer that serializes the inbound payload into a byte array. + *

The payload instance must be Serializable. + * + * @author Mark Fisher + * @since 1.0.1 + */ +public class PayloadSerializingTransformer extends AbstractPayloadTransformer { + + @Override + protected byte[] transformPayload(Object payload) throws Exception { + Assert.isTrue(payload instanceof Serializable, this.getClass().getName() + + " requires a Serializable payload, but received [" + payload.getClass().getName() + "]"); + ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); + ObjectOutputStream objectStream = new ObjectOutputStream(byteStream); + objectStream.writeObject(payload); + objectStream.flush(); + objectStream.close(); + return byteStream.toByteArray(); + } + +} diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/transformer/PayloadDeserializingTransformerTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/transformer/PayloadDeserializingTransformerTests.java new file mode 100644 index 0000000000..cb066f6df8 --- /dev/null +++ b/org.springframework.integration/src/test/java/org/springframework/integration/transformer/PayloadDeserializingTransformerTests.java @@ -0,0 +1,83 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.transformer; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +import org.junit.Test; + +import org.springframework.integration.core.Message; +import org.springframework.integration.message.GenericMessage; + +/** + * @author Mark Fisher + */ +public class PayloadDeserializingTransformerTests { + + @Test + public void deserializeString() throws Exception { + ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); + ObjectOutputStream objectStream = new ObjectOutputStream(byteStream); + objectStream.writeObject("foo"); + byte[] serialized = byteStream.toByteArray(); + PayloadDeserializingTransformer transformer = new PayloadDeserializingTransformer(); + Message result = transformer.transform(new GenericMessage(serialized)); + Object payload = result.getPayload(); + assertNotNull(payload); + assertEquals(String.class, payload.getClass()); + assertEquals("foo", payload); + } + + @Test + public void deserializeObject() throws Exception { + TestBean testBean = new TestBean("test"); + ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); + ObjectOutputStream objectStream = new ObjectOutputStream(byteStream); + objectStream.writeObject(testBean); + byte[] serialized = byteStream.toByteArray(); + PayloadDeserializingTransformer transformer = new PayloadDeserializingTransformer(); + Message result = transformer.transform(new GenericMessage(serialized)); + Object payload = result.getPayload(); + assertNotNull(payload); + assertEquals(TestBean.class, payload.getClass()); + assertEquals(testBean.name, ((TestBean) payload).name); + } + + @Test(expected = MessageTransformationException.class) + public void invalidPayload() { + byte[] bytes = new byte[] { 1, 2, 3 }; + PayloadDeserializingTransformer transformer = new PayloadDeserializingTransformer(); + transformer.transform(new GenericMessage(bytes)); + } + + + @SuppressWarnings("serial") + private static class TestBean implements Serializable { + + private String name; + + public TestBean(String name) { + this.name = name; + } + } + +} diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/transformer/PayloadSerializingTransformerTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/transformer/PayloadSerializingTransformerTests.java new file mode 100644 index 0000000000..beb3fcfbf3 --- /dev/null +++ b/org.springframework.integration/src/test/java/org/springframework/integration/transformer/PayloadSerializingTransformerTests.java @@ -0,0 +1,83 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.transformer; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayInputStream; +import java.io.ObjectInputStream; +import java.io.Serializable; + +import org.junit.Test; + +import org.springframework.integration.core.Message; +import org.springframework.integration.message.GenericMessage; +import org.springframework.integration.message.StringMessage; + +/** + * @author Mark Fisher + */ +public class PayloadSerializingTransformerTests { + + @Test + public void serializeString() throws Exception { + PayloadSerializingTransformer transformer = new PayloadSerializingTransformer(); + Message result = transformer.transform(new StringMessage("foo")); + Object payload = result.getPayload(); + assertNotNull(payload); + assertTrue(payload instanceof byte[]); + ByteArrayInputStream byteStream = new ByteArrayInputStream((byte[]) payload); + ObjectInputStream objectStream = new ObjectInputStream(byteStream); + Object deserialized = objectStream.readObject(); + assertEquals("foo", deserialized); + } + + @Test + public void serializeObject() throws Exception { + PayloadSerializingTransformer transformer = new PayloadSerializingTransformer(); + TestBean testBean = new TestBean("test"); + Message result = transformer.transform(new GenericMessage(testBean)); + Object payload = result.getPayload(); + assertNotNull(payload); + assertTrue(payload instanceof byte[]); + ByteArrayInputStream byteStream = new ByteArrayInputStream((byte[]) payload); + ObjectInputStream objectStream = new ObjectInputStream(byteStream); + Object deserialized = objectStream.readObject(); + assertEquals(TestBean.class, deserialized.getClass()); + assertEquals(testBean.name, ((TestBean) deserialized).name); + } + + @Test(expected = MessageTransformationException.class) + public void invalidPayload() { + PayloadSerializingTransformer transformer = new PayloadSerializingTransformer(); + transformer.transform(new GenericMessage(new Object())); + } + + + @SuppressWarnings("serial") + private static class TestBean implements Serializable { + + private String name; + + public TestBean(String name) { + this.name = name; + } + } + +}