INT-4451: ObjectToJsonTrans: add ResultType.BYTES

JIRA: https://jira.spring.io/browse/INT-4451
This commit is contained in:
Artem Bilan
2018-04-20 14:41:05 -04:00
committed by Gary Russell
parent 2f1b55f48c
commit 0c35310f34
5 changed files with 52 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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.
@@ -16,7 +16,10 @@
package org.springframework.integration.json;
import org.springframework.integration.support.AbstractIntegrationMessageBuilder;
import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.util.Map;
import org.springframework.integration.support.json.JsonObjectMapper;
import org.springframework.integration.support.json.JsonObjectMapperProvider;
import org.springframework.integration.transformer.AbstractTransformer;
@@ -52,12 +55,13 @@ import org.springframework.util.StringUtils;
* @author Oleg Zhurakousky
* @author Gary Russell
* @author Artem Bilan
*
* @since 2.0
*/
public class ObjectToJsonTransformer extends AbstractTransformer {
public enum ResultType {
STRING, NODE
STRING, NODE, BYTES
}
public static final String JSON_CONTENT_TYPE = "application/json";
@@ -108,12 +112,9 @@ public class ObjectToJsonTransformer extends AbstractTransformer {
@Override
protected Object doTransform(Message<?> message) throws Exception {
Object payload = ResultType.STRING.equals(this.resultType)
? this.jsonObjectMapper.toJson(message.getPayload())
: this.jsonObjectMapper.toJsonNode(message.getPayload());
AbstractIntegrationMessageBuilder<Object> messageBuilder = this.getMessageBuilderFactory().withPayload(payload);
Object payload = buildJsonPayload(message.getPayload());
LinkedCaseInsensitiveMap<Object> headers = new LinkedCaseInsensitiveMap<Object>();
Map<String, Object> headers = new LinkedCaseInsensitiveMap<>();
headers.putAll(message.getHeaders());
if (headers.containsKey(MessageHeaders.CONTENT_TYPE)) {
@@ -130,8 +131,30 @@ public class ObjectToJsonTransformer extends AbstractTransformer {
this.jsonObjectMapper.populateJavaTypes(headers, message.getPayload());
messageBuilder.copyHeaders(headers);
return messageBuilder.build();
return getMessageBuilderFactory()
.withPayload(payload)
.copyHeaders(headers)
.build();
}
private Object buildJsonPayload(Object payload) throws Exception {
switch (this.resultType) {
case STRING:
return this.jsonObjectMapper.toJson(payload);
case NODE:
return this.jsonObjectMapper.toJsonNode(payload);
case BYTES:
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
this.jsonObjectMapper.toJson(payload, new OutputStreamWriter(baos));
return baos.toByteArray();
}
default:
throw new IllegalArgumentException("Unsupported ResultType provided: " + this.resultType);
}
}
}

View File

@@ -2591,6 +2591,7 @@
<xsd:restriction base="xsd:token">
<xsd:enumeration value="STRING" />
<xsd:enumeration value="NODE" />
<xsd:enumeration value="BYTES" />
</xsd:restriction>
</xsd:simpleType>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2018 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.
@@ -52,6 +52,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
* @author Oleg Zhurakousky
* @author Gary Russell
* @author Artem Bilan
*
* @since 2.0
*/
public class ObjectToJsonTransformerTests {
@@ -118,6 +119,14 @@ public class ObjectToJsonTransformerTests {
assertEquals("123", result);
}
@Test
public void simpleIntegerAsBytesPayload() {
ObjectToJsonTransformer transformer = new ObjectToJsonTransformer(ObjectToJsonTransformer.ResultType.BYTES);
Object result = transformer.transform(new GenericMessage<>(123)).getPayload();
assertThat(result, instanceOf(byte[].class));
assertEquals("123", new String((byte[]) result));
}
@Test
public void objectPayload() throws Exception {
ObjectToJsonTransformer transformer = new ObjectToJsonTransformer();

View File

@@ -382,6 +382,8 @@ The node JSON representation provides efficiency for using the `JsonPropertyAcce
See <<spel-property-accessors>>.
When using Boon, the `NODE` representation is a `Map<String, Object>`
Beginning with _version 5.1_, the `resultType` can be configured as `BYTES` to produce a message with the `byte[]` payload for convenience in downstream handlers which operate with this data type.
[[transformer-annotation]]
===== Configuring a Transformer with Annotations

View File

@@ -38,3 +38,9 @@ Previously:
Global channel interceptors are now applied to channels registered dynamically - such as via the `IntegrationFlowContext` when using the Java DSL, or beans that are initialized using `beanFactory.initializeBean()`.
Previously, interceptors were not applied when beans were created after the application context was refreshed.
==== ObjectToJsonTransformer
A new `ResultType.BYTES` mode is introduced for the `ObjectToJsonTransformer`.
See <<json-transformers>> for more information.