diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/xml/PayloadDeserializingTransformerParser.java b/spring-integration-core/src/main/java/org/springframework/integration/config/xml/PayloadDeserializingTransformerParser.java index 0021d7329..d089dc454 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/xml/PayloadDeserializingTransformerParser.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/xml/PayloadDeserializingTransformerParser.java @@ -35,6 +35,7 @@ protected String getTransformerClassName() { @Override protected void parseTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { + IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "converter"); } } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/xml/PayloadSerializingTransformerParser.java b/spring-integration-core/src/main/java/org/springframework/integration/config/xml/PayloadSerializingTransformerParser.java index 7df110879..9c65d5271 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/xml/PayloadSerializingTransformerParser.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/xml/PayloadSerializingTransformerParser.java @@ -36,6 +36,7 @@ protected String getTransformerClassName() { @Override protected void parseTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { + IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "converter"); } } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadDeserializingTransformer.java b/spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadDeserializingTransformer.java index 92a0d04d5..faf9638dd 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadDeserializingTransformer.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadDeserializingTransformer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2010 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. @@ -32,19 +32,18 @@ * @since 1.0.1 */ public class PayloadDeserializingTransformer extends PayloadTypeConvertingTransformer { - + + @Override + public void setConverter(Converter converter) { + this.converter = converter; + } + @Override protected Object transformPayload(byte[] payload) throws Exception { if (this.converter == null) { this.converter = new DeserializingConverter(new JavaStreamingConverter()); } - return converter.convert(payload); - } - - @Override - public void setConverter(Converter converter) { - this.converter = converter; + return this.converter.convert(payload); } - } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadSerializingTransformer.java b/spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadSerializingTransformer.java index 7e94db690..0d2edb1c8 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadSerializingTransformer.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/transformer/PayloadSerializingTransformer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2010 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. @@ -33,18 +33,17 @@ */ public class PayloadSerializingTransformer extends PayloadTypeConvertingTransformer { - + @Override + public void setConverter(Converter converter) { + this.converter = converter; + } + @Override protected byte[] transformPayload(Object payload) throws Exception { if (this.converter == null) { this.converter = new SerializingConverter(new JavaStreamingConverter()); } - return converter.convert(payload); + return this.converter.convert(payload); } - @Override - public void setConverter(Converter converter) { - this.converter = converter; - } - } diff --git a/spring-integration-core/src/main/resources/org/springframework/integration/config/xml/spring-integration-2.0.xsd b/spring-integration-core/src/main/resources/org/springframework/integration/config/xml/spring-integration-2.0.xsd index 66fe1e2fd..b814cdc8b 100644 --- a/spring-integration-core/src/main/resources/org/springframework/integration/config/xml/spring-integration-2.0.xsd +++ b/spring-integration-core/src/main/resources/org/springframework/integration/config/xml/spring-integration-2.0.xsd @@ -1009,8 +1009,8 @@ - - + + @@ -1629,6 +1629,74 @@ + + + + Defines a Transformer that converts an object payload to a byte array. + + + + + + + + + + + + + + + + + + + Reference to a Converter instance that converts from an object to a byte array. + This is optional. The default Converter will use standard Java serialization. + + + + + + + + + + + + + + Defines a Transformer that converts a byte array to an object. + + + + + + + + + + + + + + + + + + + Reference to a Converter instance that converts from a byte array to an object. + This is optional. The default Converter will use standard Java deserialization. + + + + + + + + + + @@ -1670,39 +1738,6 @@ - - - - Defines a Transformer that serializes any Object payload that implements - Serializable into a byte - array. - - - - - - - - - - - - - - - Defines a Transformer that deserializes a byte array payload into an - Object. - - - - - - - - - - - diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/PayloadDeserializingTransformerParserTests-context.xml b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/PayloadDeserializingTransformerParserTests-context.xml index 8d53ce85a..4f1699331 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/PayloadDeserializingTransformerParserTests-context.xml +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/PayloadDeserializingTransformerParserTests-context.xml @@ -13,6 +13,8 @@ + + @@ -20,9 +22,11 @@ - - - + + + + + diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/PayloadDeserializingTransformerParserTests.java b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/PayloadDeserializingTransformerParserTests.java index 957c5f0d1..e61b51793 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/PayloadDeserializingTransformerParserTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/PayloadDeserializingTransformerParserTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2010 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. @@ -23,12 +23,13 @@ import java.io.ByteArrayOutputStream; import java.io.ObjectOutputStream; import java.io.Serializable; +import java.nio.charset.Charset; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.core.convert.converter.Converter; import org.springframework.integration.Message; import org.springframework.integration.MessageChannel; import org.springframework.integration.core.PollableChannel; @@ -45,15 +46,15 @@ public class PayloadDeserializingTransformerParserTests { @Autowired - @Qualifier("directInput") private MessageChannel directInput; @Autowired - @Qualifier("queueInput") private MessageChannel queueInput; @Autowired - @Qualifier("output") + private MessageChannel customConverterInput; + + @Autowired private PollableChannel output; @@ -103,6 +104,15 @@ public void invalidPayload() { directInput.send(new GenericMessage(bytes)); } + @Test + public void customConverter() throws Exception { + customConverterInput.send(new GenericMessage("test".getBytes(Charset.forName("UTF-8")))); + Message result = output.receive(3000); + assertNotNull(result); + assertEquals(String.class, result.getPayload().getClass()); + assertEquals("TEST", result.getPayload()); + } + private static byte[] serialize(Object object) throws Exception { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); @@ -119,4 +129,12 @@ private static class TestBean implements Serializable { } + + public static class TestDeserializingConverter implements Converter { + + public Object convert(byte[] source) { + return new String(source, Charset.forName("UTF-8")).toUpperCase(); + } + } + } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/PayloadSerializingTransformerParserTests-context.xml b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/PayloadSerializingTransformerParserTests-context.xml index 128f6ea1f..61d4eb9d3 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/PayloadSerializingTransformerParserTests-context.xml +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/PayloadSerializingTransformerParserTests-context.xml @@ -13,6 +13,8 @@ + + @@ -20,9 +22,11 @@ - - - + + + + + diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/PayloadSerializingTransformerParserTests.java b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/PayloadSerializingTransformerParserTests.java index 06d285d32..64350a2d6 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/PayloadSerializingTransformerParserTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/PayloadSerializingTransformerParserTests.java @@ -23,12 +23,13 @@ import java.io.ByteArrayInputStream; import java.io.ObjectInputStream; import java.io.Serializable; +import java.nio.charset.Charset; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.core.convert.converter.Converter; import org.springframework.integration.Message; import org.springframework.integration.MessageChannel; import org.springframework.integration.core.PollableChannel; @@ -45,15 +46,15 @@ public class PayloadSerializingTransformerParserTests { @Autowired - @Qualifier("directInput") private MessageChannel directInput; @Autowired - @Qualifier("queueInput") private MessageChannel queueInput; @Autowired - @Qualifier("output") + private MessageChannel customConverterInput; + + @Autowired private PollableChannel output; @@ -101,6 +102,15 @@ public void invalidPayload() { directInput.send(new GenericMessage(new Object())); } + @Test + public void customConverter() throws Exception { + customConverterInput.send(new GenericMessage("test")); + Message result = output.receive(3000); + assertNotNull(result); + assertEquals(byte[].class, result.getPayload().getClass()); + assertEquals("TEST", new String((byte[]) result.getPayload(), Charset.forName("UTF-8"))); + } + private static Object deserialize(byte[] bytes) throws Exception { ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes); @@ -116,4 +126,12 @@ private static class TestBean implements Serializable { } + + public static class TestSerializingConverter implements Converter { + + public byte[] convert(Object source) { + return source.toString().toUpperCase().getBytes(Charset.forName("UTF-8")); + } + } + }