Skip to content

Commit 0c35310

Browse files
artembilangaryrussell
authored andcommitted
INT-4451: ObjectToJsonTrans: add ResultType.BYTES
JIRA: https://jira.spring.io/browse/INT-4451
1 parent 2f1b55f commit 0c35310

File tree

5 files changed

+52
-11
lines changed

5 files changed

+52
-11
lines changed

spring-integration-core/src/main/java/org/springframework/integration/json/ObjectToJsonTransformer.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,10 @@
1616

1717
package org.springframework.integration.json;
1818

19-
import org.springframework.integration.support.AbstractIntegrationMessageBuilder;
19+
import java.io.ByteArrayOutputStream;
20+
import java.io.OutputStreamWriter;
21+
import java.util.Map;
22+
2023
import org.springframework.integration.support.json.JsonObjectMapper;
2124
import org.springframework.integration.support.json.JsonObjectMapperProvider;
2225
import org.springframework.integration.transformer.AbstractTransformer;
@@ -52,12 +55,13 @@
5255
* @author Oleg Zhurakousky
5356
* @author Gary Russell
5457
* @author Artem Bilan
58+
*
5559
* @since 2.0
5660
*/
5761
public class ObjectToJsonTransformer extends AbstractTransformer {
5862

5963
public enum ResultType {
60-
STRING, NODE
64+
STRING, NODE, BYTES
6165
}
6266

6367
public static final String JSON_CONTENT_TYPE = "application/json";
@@ -108,12 +112,9 @@ public String getComponentType() {
108112

109113
@Override
110114
protected Object doTransform(Message<?> message) throws Exception {
111-
Object payload = ResultType.STRING.equals(this.resultType)
112-
? this.jsonObjectMapper.toJson(message.getPayload())
113-
: this.jsonObjectMapper.toJsonNode(message.getPayload());
114-
AbstractIntegrationMessageBuilder<Object> messageBuilder = this.getMessageBuilderFactory().withPayload(payload);
115+
Object payload = buildJsonPayload(message.getPayload());
115116

116-
LinkedCaseInsensitiveMap<Object> headers = new LinkedCaseInsensitiveMap<Object>();
117+
Map<String, Object> headers = new LinkedCaseInsensitiveMap<>();
117118
headers.putAll(message.getHeaders());
118119

119120
if (headers.containsKey(MessageHeaders.CONTENT_TYPE)) {
@@ -130,8 +131,30 @@ else if (StringUtils.hasLength(this.contentType)) {
130131

131132
this.jsonObjectMapper.populateJavaTypes(headers, message.getPayload());
132133

133-
messageBuilder.copyHeaders(headers);
134-
return messageBuilder.build();
134+
return getMessageBuilderFactory()
135+
.withPayload(payload)
136+
.copyHeaders(headers)
137+
.build();
138+
}
139+
140+
private Object buildJsonPayload(Object payload) throws Exception {
141+
switch (this.resultType) {
142+
143+
case STRING:
144+
return this.jsonObjectMapper.toJson(payload);
145+
146+
case NODE:
147+
return this.jsonObjectMapper.toJsonNode(payload);
148+
149+
case BYTES:
150+
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
151+
this.jsonObjectMapper.toJson(payload, new OutputStreamWriter(baos));
152+
return baos.toByteArray();
153+
}
154+
155+
default:
156+
throw new IllegalArgumentException("Unsupported ResultType provided: " + this.resultType);
157+
}
135158
}
136159

137160
}

spring-integration-core/src/main/resources/org/springframework/integration/config/spring-integration-5.1.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2591,6 +2591,7 @@
25912591
<xsd:restriction base="xsd:token">
25922592
<xsd:enumeration value="STRING" />
25932593
<xsd:enumeration value="NODE" />
2594+
<xsd:enumeration value="BYTES" />
25942595
</xsd:restriction>
25952596
</xsd:simpleType>
25962597

spring-integration-core/src/test/java/org/springframework/integration/json/ObjectToJsonTransformerTests.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,6 +52,7 @@
5252
* @author Oleg Zhurakousky
5353
* @author Gary Russell
5454
* @author Artem Bilan
55+
*
5556
* @since 2.0
5657
*/
5758
public class ObjectToJsonTransformerTests {
@@ -118,6 +119,14 @@ public void simpleIntegerPayload() throws Exception {
118119
assertEquals("123", result);
119120
}
120121

122+
@Test
123+
public void simpleIntegerAsBytesPayload() {
124+
ObjectToJsonTransformer transformer = new ObjectToJsonTransformer(ObjectToJsonTransformer.ResultType.BYTES);
125+
Object result = transformer.transform(new GenericMessage<>(123)).getPayload();
126+
assertThat(result, instanceOf(byte[].class));
127+
assertEquals("123", new String((byte[]) result));
128+
}
129+
121130
@Test
122131
public void objectPayload() throws Exception {
123132
ObjectToJsonTransformer transformer = new ObjectToJsonTransformer();

src/reference/asciidoc/transformer.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@ The node JSON representation provides efficiency for using the `JsonPropertyAcce
382382
See <<spel-property-accessors>>.
383383
When using Boon, the `NODE` representation is a `Map<String, Object>`
384384

385+
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.
386+
385387
[[transformer-annotation]]
386388
===== Configuring a Transformer with Annotations
387389

src/reference/asciidoc/whats-new.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,9 @@ Previously:
3838

3939
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()`.
4040
Previously, interceptors were not applied when beans were created after the application context was refreshed.
41+
42+
==== ObjectToJsonTransformer
43+
44+
A new `ResultType.BYTES` mode is introduced for the `ObjectToJsonTransformer`.
45+
46+
See <<json-transformers>> for more information.

0 commit comments

Comments
 (0)