From 0c35310f3488e21dc74ffd5ee7a428d2d3a937eb Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Fri, 20 Apr 2018 14:41:05 -0400 Subject: [PATCH] INT-4451: ObjectToJsonTrans: add ResultType.BYTES JIRA: https://jira.spring.io/browse/INT-4451 --- .../json/ObjectToJsonTransformer.java | 43 ++++++++++++++----- .../config/spring-integration-5.1.xsd | 1 + .../json/ObjectToJsonTransformerTests.java | 11 ++++- src/reference/asciidoc/transformer.adoc | 2 + src/reference/asciidoc/whats-new.adoc | 6 +++ 5 files changed, 52 insertions(+), 11 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/json/ObjectToJsonTransformer.java b/spring-integration-core/src/main/java/org/springframework/integration/json/ObjectToJsonTransformer.java index 996686c6d9..3f5289c501 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/json/ObjectToJsonTransformer.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/json/ObjectToJsonTransformer.java @@ -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 messageBuilder = this.getMessageBuilderFactory().withPayload(payload); + Object payload = buildJsonPayload(message.getPayload()); - LinkedCaseInsensitiveMap headers = new LinkedCaseInsensitiveMap(); + Map 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); + } } } diff --git a/spring-integration-core/src/main/resources/org/springframework/integration/config/spring-integration-5.1.xsd b/spring-integration-core/src/main/resources/org/springframework/integration/config/spring-integration-5.1.xsd index 3d02d23d59..f739d55209 100644 --- a/spring-integration-core/src/main/resources/org/springframework/integration/config/spring-integration-5.1.xsd +++ b/spring-integration-core/src/main/resources/org/springframework/integration/config/spring-integration-5.1.xsd @@ -2591,6 +2591,7 @@ + diff --git a/spring-integration-core/src/test/java/org/springframework/integration/json/ObjectToJsonTransformerTests.java b/spring-integration-core/src/test/java/org/springframework/integration/json/ObjectToJsonTransformerTests.java index f05afef62a..6de8a77132 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/json/ObjectToJsonTransformerTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/json/ObjectToJsonTransformerTests.java @@ -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(); diff --git a/src/reference/asciidoc/transformer.adoc b/src/reference/asciidoc/transformer.adoc index 51118bc627..8d1f094dc2 100644 --- a/src/reference/asciidoc/transformer.adoc +++ b/src/reference/asciidoc/transformer.adoc @@ -382,6 +382,8 @@ The node JSON representation provides efficiency for using the `JsonPropertyAcce See <>. When using Boon, the `NODE` representation is a `Map` +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 diff --git a/src/reference/asciidoc/whats-new.adoc b/src/reference/asciidoc/whats-new.adoc index a299c0c413..d1b0655c33 100644 --- a/src/reference/asciidoc/whats-new.adoc +++ b/src/reference/asciidoc/whats-new.adoc @@ -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 <> for more information.