Skip to content

Commit

Permalink
INT-1459 added namespace support for 'converter' on payload-serializi…
Browse files Browse the repository at this point in the history
…ng and payload-deserializing transformers
  • Loading branch information
markfisher committed Sep 16, 2010
1 parent c0afad4 commit 0967c93
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ protected String getTransformerClassName() {

@Override
protected void parseTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "converter");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected String getTransformerClassName() {

@Override
protected void parseTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "converter");
}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -32,19 +32,18 @@
* @since 1.0.1
*/
public class PayloadDeserializingTransformer extends PayloadTypeConvertingTransformer<byte[], Object> {


@Override
public void setConverter(Converter<byte[], Object> 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<byte[], Object> converter) {
this.converter = converter;
return this.converter.convert(payload);
}


}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -33,18 +33,17 @@
*/
public class PayloadSerializingTransformer extends PayloadTypeConvertingTransformer<Object, byte[]> {


@Override
public void setConverter(Converter<Object, byte[]> 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<Object, byte[]> converter) {
this.converter = converter;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1009,8 +1009,8 @@
<xsd:element name="delayer" type="delayer-type" />
<xsd:element name="gateway" type="innerGatewayType" />
<xsd:element name="poller" type="innerPollerType" />
<xsd:element name="payload-serializing-transformer" type="specialized-transformer-type" />
<xsd:element name="payload-deserializing-transformer" type="specialized-transformer-type" />
<xsd:element name="payload-serializing-transformer" type="payload-serializing-transformer-type" />
<xsd:element name="payload-deserializing-transformer" type="payload-deserializing-transformer-type" />
<xsd:element name="object-to-string-transformer" type="specialized-transformer-type" />
<xsd:element name="object-to-map-transformer" type="specialized-transformer-type" />
<xsd:element name="map-to-object-transformer" type="map-to-object-transformer-type" />
Expand Down Expand Up @@ -1629,6 +1629,74 @@
</xsd:attribute>
</xsd:complexType>

<xsd:element name="payload-serializing-transformer">
<xsd:annotation>
<xsd:documentation>
Defines a Transformer that converts an object payload to a byte array.
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="payload-serializing-transformer-type">
<xsd:attributeGroup ref="inputOutputChannelGroup" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>

<xsd:complexType name="payload-serializing-transformer-type">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element ref="poller" />
</xsd:choice>
<xsd:attribute name="converter" use="optional">
<xsd:annotation>
<xsd:documentation>
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.
</xsd:documentation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="org.springframework.core.convert.converter.Converter" />
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>

<xsd:element name="payload-deserializing-transformer">
<xsd:annotation>
<xsd:documentation>
Defines a Transformer that converts a byte array to an object.
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="payload-deserializing-transformer-type">
<xsd:attributeGroup ref="inputOutputChannelGroup" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>

<xsd:complexType name="payload-deserializing-transformer-type">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element ref="poller" />
</xsd:choice>
<xsd:attribute name="converter" use="optional">
<xsd:annotation>
<xsd:documentation>
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.
</xsd:documentation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="org.springframework.core.convert.converter.Converter" />
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>

<xsd:element name="claim-check-in" type="claimCheckTransformerType">
<xsd:annotation>
<xsd:documentation>
Expand Down Expand Up @@ -1670,39 +1738,6 @@
<xsd:attributeGroup ref="inputOutputChannelGroup" />
</xsd:complexType>

<xsd:element name="payload-serializing-transformer">
<xsd:annotation>
<xsd:documentation>
Defines a Transformer that serializes any Object payload that implements
Serializable into a byte
array.
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="specialized-transformer-type">
<xsd:attributeGroup ref="inputOutputChannelGroup" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>

<xsd:element name="payload-deserializing-transformer">
<xsd:annotation>
<xsd:documentation>
Defines a Transformer that deserializes a byte array payload into an
Object.
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="specialized-transformer-type">
<xsd:attributeGroup ref="inputOutputChannelGroup" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>

<xsd:complexType name="specialized-transformer-type">
<xsd:choice minOccurs="1" maxOccurs="unbounded">
<xsd:any processContents="strict" namespace="##other" minOccurs="0" maxOccurs="unbounded" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@
<queue capacity="1"/>
</channel>

<channel id="customConverterInput"/>

<channel id="output">
<queue capacity="1"/>
</channel>

<payload-deserializing-transformer input-channel="directInput" output-channel="output"/>

<payload-deserializing-transformer input-channel="queueInput" output-channel="output">
<poller>
<interval-trigger interval="10000"/>
</poller>
<poller fixed-delay="10000"/>
</payload-deserializing-transformer>

<payload-deserializing-transformer input-channel="customConverterInput" output-channel="output" converter="customConverter"/>

<beans:bean id="customConverter" class="org.springframework.integration.config.xml.PayloadDeserializingTransformerParserTests$TestDeserializingConverter"/>

</beans:beans>
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -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;


Expand Down Expand Up @@ -103,6 +104,15 @@ public void invalidPayload() {
directInput.send(new GenericMessage<byte[]>(bytes));
}

@Test
public void customConverter() throws Exception {
customConverterInput.send(new GenericMessage<byte[]>("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();
Expand All @@ -119,4 +129,12 @@ private static class TestBean implements Serializable {

}


public static class TestDeserializingConverter implements Converter<byte[], Object> {

public Object convert(byte[] source) {
return new String(source, Charset.forName("UTF-8")).toUpperCase();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@
<queue capacity="1"/>
</channel>

<channel id="customConverterInput"/>

<channel id="output">
<queue capacity="1"/>
</channel>

<payload-serializing-transformer input-channel="directInput" output-channel="output"/>

<payload-serializing-transformer input-channel="queueInput" output-channel="output">
<poller>
<interval-trigger interval="10000"/>
</poller>
<poller fixed-delay="10000"/>
</payload-serializing-transformer>

<payload-serializing-transformer input-channel="customConverterInput" output-channel="output" converter="customConverter"/>

<beans:bean id="customConverter" class="org.springframework.integration.config.xml.PayloadSerializingTransformerParserTests$TestSerializingConverter"/>

</beans:beans>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;


Expand Down Expand Up @@ -101,6 +102,15 @@ public void invalidPayload() {
directInput.send(new GenericMessage<Object>(new Object()));
}

@Test
public void customConverter() throws Exception {
customConverterInput.send(new GenericMessage<String>("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);
Expand All @@ -116,4 +126,12 @@ private static class TestBean implements Serializable {

}


public static class TestSerializingConverter implements Converter<Object, byte[]> {

public byte[] convert(Object source) {
return source.toString().toUpperCase().getBytes(Charset.forName("UTF-8"));
}
}

}

0 comments on commit 0967c93

Please sign in to comment.