Added PayloadSerializingTransformer and PayloadDeserializingTransformer (INT-513).
This commit is contained in:
@@ -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.
|
||||
* <p>The byte array payload must be a result of serialization.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @since 1.0.1
|
||||
*/
|
||||
public class PayloadDeserializingTransformer extends AbstractPayloadTransformer<byte[], Object> {
|
||||
|
||||
@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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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.
|
||||
* <p>The payload instance must be Serializable.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @since 1.0.1
|
||||
*/
|
||||
public class PayloadSerializingTransformer extends AbstractPayloadTransformer<Object, byte[]> {
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<byte[]>(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<byte[]>(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<byte[]>(bytes));
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class TestBean implements Serializable {
|
||||
|
||||
private String name;
|
||||
|
||||
public TestBean(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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>(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<Object>(new Object()));
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class TestBean implements Serializable {
|
||||
|
||||
private String name;
|
||||
|
||||
public TestBean(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user