diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/transformer/MessageTransformingChannelInterceptor.java b/org.springframework.integration/src/main/java/org/springframework/integration/transformer/MessageTransformingChannelInterceptor.java new file mode 100644 index 0000000000..e7df0b8e8f --- /dev/null +++ b/org.springframework.integration/src/main/java/org/springframework/integration/transformer/MessageTransformingChannelInterceptor.java @@ -0,0 +1,61 @@ +/* + * 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 org.springframework.integration.channel.ChannelInterceptor; +import org.springframework.integration.channel.MessageChannel; +import org.springframework.integration.channel.interceptor.ChannelInterceptorAdapter; +import org.springframework.integration.message.Message; + +/** + * a {@link ChannelInterceptor} which allows the application of a {@link MessageTransformer} on either send or receive to a channel + * @author Jonas Partner + * + */ +public class MessageTransformingChannelInterceptor extends ChannelInterceptorAdapter { + + private boolean convertOnSend = true; + + private final MessageTransformer transfomer; + + public MessageTransformingChannelInterceptor(MessageTransformer transformer) { + this.transfomer = transformer; + } + + public boolean getConvertOnSend() { + return convertOnSend; + } + + public void setConvertOnSend(boolean convertOnSend) { + this.convertOnSend = convertOnSend; + } + + @Override + public void postSend(Message message, MessageChannel channel, boolean sent) { + if(convertOnSend){ + transfomer.transform(message); + } + } + + @Override + public void postReceive(Message message, MessageChannel channel) { + if(!convertOnSend){ + transfomer.transform(message); + } + } + +} diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/transformer/MessageTransformingChannelInterceptorTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/transformer/MessageTransformingChannelInterceptorTests.java new file mode 100644 index 0000000000..e9dcd38f8a --- /dev/null +++ b/org.springframework.integration/src/test/java/org/springframework/integration/transformer/MessageTransformingChannelInterceptorTests.java @@ -0,0 +1,86 @@ +/* + * 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.*; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.integration.channel.AbstractMessageChannel; +import org.springframework.integration.channel.QueueChannel; +import org.springframework.integration.message.Message; +import org.springframework.integration.message.StringMessage; + +/** + * + * @author Jonas Partner + * + */ +public class MessageTransformingChannelInterceptorTests { + + AbstractMessageChannel channel; + + StringMessage message; + + TestTransformer transfomer; + + MessageTransformingChannelInterceptor channelInterceptor; + + @Before + public void setUp(){ + channel = new QueueChannel(); + message = new StringMessage("test"); + transfomer = new TestTransformer(); + channelInterceptor = new MessageTransformingChannelInterceptor(transfomer); + channel.addInterceptor(channelInterceptor); + } + + @Test + public void testTransformOnReceive(){ + channelInterceptor.setConvertOnSend(false); + channel.send(message); + assertFalse("Transfomrer on incorrectly invoked on send", transfomer.invoked); + Message msg = channel.receive(1); + assertEquals("Wrong message",message, msg); + assertTrue("Transfomer not invoked on receive", transfomer.invoked); + } + + @Test + public void testTransformOnSend(){ + channelInterceptor.setConvertOnSend(true); + channel.send(message); + assertTrue("Transfomrer not invoked on send", transfomer.invoked); + Message msg = channel.receive(1); + assertEquals("Wrong message",message, msg); + assertEquals("Transfomer invoked on receive", 1, transfomer.invokedCount); + } + + + private static class TestTransformer implements MessageTransformer{ + + boolean invoked = false; + + int invokedCount = 0; + + public void transform(Message message) { + invoked = true; + invokedCount++; + } + + } + +}