diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/transformer/AbstractPayloadTransformer.java b/org.springframework.integration/src/main/java/org/springframework/integration/transformer/AbstractPayloadTransformer.java index 8ddfc6a4b0..2d0ebc501c 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/transformer/AbstractPayloadTransformer.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/transformer/AbstractPayloadTransformer.java @@ -33,7 +33,8 @@ public abstract class AbstractPayloadTransformer implements Transformer { @SuppressWarnings("unchecked") U result = this.transformPayload((T) message.getPayload()); return MessageBuilder.withPayload(result).copyHeaders(message.getHeaders()).build(); - } catch (Exception e) { + } + catch (Exception e) { throw new MessagingException(message, "failed to transform message payload", e); } } diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/transformer/ObjectToStringTransformer.java b/org.springframework.integration/src/main/java/org/springframework/integration/transformer/ObjectToStringTransformer.java new file mode 100644 index 0000000000..df3af1de3a --- /dev/null +++ b/org.springframework.integration/src/main/java/org/springframework/integration/transformer/ObjectToStringTransformer.java @@ -0,0 +1,33 @@ +/* + * 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; + +/** + * A simple transformer that creates an outbound payload by invoking the + * inbound payload Object's toString() method. + * + * @author Mark Fisher + * @since 1.0.1 + */ +public class ObjectToStringTransformer extends AbstractPayloadTransformer { + + @Override + protected String transformPayload(Object payload) throws Exception { + return payload.toString(); + } + +} diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/transformer/ObjectToStringTransformerTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/transformer/ObjectToStringTransformerTests.java new file mode 100644 index 0000000000..54e842ce4c --- /dev/null +++ b/org.springframework.integration/src/test/java/org/springframework/integration/transformer/ObjectToStringTransformerTests.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 static org.junit.Assert.assertEquals; + +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 ObjectToStringTransformerTests { + + @Test + public void stringPayload() { + Transformer transformer = new ObjectToStringTransformer(); + Message result = transformer.transform(new StringMessage("foo")); + assertEquals("foo", result.getPayload()); + } + + @Test + public void objectPayload() { + Transformer transformer = new ObjectToStringTransformer(); + Message result = transformer.transform(new GenericMessage(new TestBean())); + assertEquals("test", result.getPayload()); + } + + + private static class TestBean { + + public String toString() { + return "test"; + } + } + +}