diff --git a/spring-integration-core/src/main/java/org/springframework/integration/json/JsonOutboundMessageMapper.java b/spring-integration-core/src/main/java/org/springframework/integration/json/JsonOutboundMessageMapper.java new file mode 100644 index 0000000000..4f8f0e6fe0 --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/json/JsonOutboundMessageMapper.java @@ -0,0 +1,54 @@ +/* + * 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. + * 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.json; + +import java.io.StringWriter; + +import org.codehaus.jackson.map.ObjectMapper; + +import org.springframework.integration.Message; +import org.springframework.integration.mapping.OutboundMessageMapper; + +/** + * {@link OutboundMessageMapper} implementation the converts a {@link Message} to a JSON string representation. + * + * @author Jeremy Grelle + * @since 2.0 + */ +public class JsonOutboundMessageMapper implements OutboundMessageMapper { + + private volatile boolean shouldExtractPayload = false; + + private final ObjectMapper objectMapper = new ObjectMapper(); + + + public void setShouldExtractPayload(boolean shouldExtractPayload) { + this.shouldExtractPayload = shouldExtractPayload; + } + + public String fromMessage(Message message) throws Exception { + StringWriter writer = new StringWriter(); + if (this.shouldExtractPayload) { + this.objectMapper.writeValue(writer, message.getPayload()); + } + else { + this.objectMapper.writeValue(writer, message); + } + return writer.toString(); + } + +} diff --git a/spring-integration-core/src/main/java/org/springframework/integration/json/OutboundJsonMessageMapper.java b/spring-integration-core/src/main/java/org/springframework/integration/json/OutboundJsonMessageMapper.java deleted file mode 100644 index a13d21aded..0000000000 --- a/spring-integration-core/src/main/java/org/springframework/integration/json/OutboundJsonMessageMapper.java +++ /dev/null @@ -1,35 +0,0 @@ -package org.springframework.integration.json; - -import java.io.StringWriter; - -import org.codehaus.jackson.map.ObjectMapper; -import org.springframework.integration.Message; -import org.springframework.integration.mapping.OutboundMessageMapper; - -/** - * {@link OutboundMessageMapper} implementation the converts a {@link Message} to a JSON string representation. - * - * TODO - We might need to add special handling for MessageHistory - * - * @author Jeremy Grelle - */ -public class OutboundJsonMessageMapper implements OutboundMessageMapper { - - private boolean shouldExtractPayload = false; - - private ObjectMapper objectMapper = new ObjectMapper(); - - public String fromMessage(Message message) throws Exception { - StringWriter writer = new StringWriter(); - if (shouldExtractPayload) { - objectMapper.writeValue(writer, message.getPayload()); - } else { - objectMapper.writeValue(writer, message); - } - return writer.toString(); - } - - public void setShouldExtractPayload(boolean shouldExtractPayload) { - this.shouldExtractPayload = shouldExtractPayload; - } -} diff --git a/spring-integration-core/src/test/java/org/springframework/integration/json/OutboundJsonMessageMapperTests.java b/spring-integration-core/src/test/java/org/springframework/integration/json/JsonOutboundMessageMapperTests.java similarity index 61% rename from spring-integration-core/src/test/java/org/springframework/integration/json/OutboundJsonMessageMapperTests.java rename to spring-integration-core/src/test/java/org/springframework/integration/json/JsonOutboundMessageMapperTests.java index 0b7f2d8ffc..7f869dacd0 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/json/OutboundJsonMessageMapperTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/json/JsonOutboundMessageMapperTests.java @@ -16,7 +16,8 @@ package org.springframework.integration.json; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.io.IOException; @@ -26,34 +27,60 @@ import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.JsonToken; import org.codehaus.jackson.map.ObjectMapper; import org.junit.Test; + import org.springframework.integration.Message; +import org.springframework.integration.context.NamedComponent; +import org.springframework.integration.history.MessageHistory; import org.springframework.integration.support.MessageBuilder; /** * @author Jeremy Grelle * @since 2.0 */ -public class OutboundJsonMessageMapperTests { - - private JsonFactory jsonFactory = new JsonFactory(); - private ObjectMapper objectMapper = new ObjectMapper(); - +public class JsonOutboundMessageMapperTests { + + private final JsonFactory jsonFactory = new JsonFactory(); + + private final ObjectMapper objectMapper = new ObjectMapper(); + + @Test public void testFromMessageWithHeadersAndStringPayload() throws Exception { Message testMessage = MessageBuilder.withPayload("myPayloadStuff").build(); - OutboundJsonMessageMapper mapper = new OutboundJsonMessageMapper(); + JsonOutboundMessageMapper mapper = new JsonOutboundMessageMapper(); String result = mapper.fromMessage(testMessage); assertTrue(result.contains("\"headers\":{")); assertTrue(result.contains("\"$timestamp\":"+testMessage.getHeaders().getTimestamp())); assertTrue(result.contains("\"$id\":\""+testMessage.getHeaders().getId()+"\"")); assertTrue(result.contains("\"payload\":\"myPayloadStuff\"")); } - + + @Test + public void testFromMessageWithMessageHistory() throws Exception { + Message testMessage = MessageBuilder.withPayload("myPayloadStuff").build(); + testMessage = MessageHistory.write(testMessage, new TestNamedComponent(1)); + testMessage = MessageHistory.write(testMessage, new TestNamedComponent(2)); + testMessage = MessageHistory.write(testMessage, new TestNamedComponent(3)); + JsonOutboundMessageMapper mapper = new JsonOutboundMessageMapper(); + String result = mapper.fromMessage(testMessage); + assertTrue(result.contains("\"headers\":{")); + assertTrue(result.contains("\"$timestamp\":"+testMessage.getHeaders().getTimestamp())); + assertTrue(result.contains("\"$id\":\""+testMessage.getHeaders().getId()+"\"")); + assertTrue(result.contains("\"payload\":\"myPayloadStuff\"")); + assertTrue(result.contains("\"$history\":")); + assertTrue(result.contains("testName-1")); + assertTrue(result.contains("testType-1")); + assertTrue(result.contains("testName-2")); + assertTrue(result.contains("testType-2")); + assertTrue(result.contains("testName-3")); + assertTrue(result.contains("testType-3")); + } + @Test public void testFromMessageExtractStringPayload() throws Exception { Message testMessage = MessageBuilder.withPayload("myPayloadStuff").build(); String expected = "\"myPayloadStuff\""; - OutboundJsonMessageMapper mapper = new OutboundJsonMessageMapper(); + JsonOutboundMessageMapper mapper = new JsonOutboundMessageMapper(); mapper.setShouldExtractPayload(true); String result = mapper.fromMessage(testMessage); assertEquals(expected, result); @@ -63,7 +90,7 @@ public class OutboundJsonMessageMapperTests { public void testFromMessageWithHeadersAndBeanPayload() throws Exception { TestBean payload = new TestBean(); Message testMessage = MessageBuilder.withPayload(payload).build(); - OutboundJsonMessageMapper mapper = new OutboundJsonMessageMapper(); + JsonOutboundMessageMapper mapper = new JsonOutboundMessageMapper(); String result = mapper.fromMessage(testMessage); assertTrue(result.contains("\"headers\":{")); assertTrue(result.contains("\"$timestamp\":"+testMessage.getHeaders().getTimestamp())); @@ -76,7 +103,7 @@ public class OutboundJsonMessageMapperTests { public void testFromMessageExtractBeanPayload() throws Exception { TestBean payload = new TestBean(); Message testMessage = MessageBuilder.withPayload(payload).build(); - OutboundJsonMessageMapper mapper = new OutboundJsonMessageMapper(); + JsonOutboundMessageMapper mapper = new JsonOutboundMessageMapper(); mapper.setShouldExtractPayload(true); String result = mapper.fromMessage(testMessage); assertTrue(!result.contains("headers")); @@ -92,4 +119,24 @@ public class OutboundJsonMessageMapperTests { parser.nextToken(); return objectMapper.readValue(parser, TestBean.class); } + + + private static class TestNamedComponent implements NamedComponent { + + private final int id; + + private TestNamedComponent(int id) { + this.id = id; + } + + public String getComponentName() { + return "testName-" + this.id; + } + + public String getComponentType() { + return "testType-" + this.id; + } + + } + }