INT-1001 polishing JsonOutboundMessageMapper
This commit is contained in:
@@ -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<String> {
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String> {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -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<String> 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<String> 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<String> 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<TestBean> 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<TestBean> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user