This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new bd8ab8f CAMEL-17470: camel-ahc - Sending byte[] as body payload should not use chunked transfer-encoding. bd8ab8f is described below commit bd8ab8f13642f7f50fee73ee1f57043ee9919597 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Mon Jan 17 15:25:33 2022 +0100 CAMEL-17470: camel-ahc - Sending byte[] as body payload should not use chunked transfer-encoding. --- .../camel/component/ahc/DefaultAhcBinding.java | 9 ++++--- .../camel/component/ahc/AhcProducePostTest.java | 30 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/DefaultAhcBinding.java b/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/DefaultAhcBinding.java index 3b88d58..e9da994 100644 --- a/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/DefaultAhcBinding.java +++ b/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/DefaultAhcBinding.java @@ -183,14 +183,15 @@ public class DefaultAhcBinding implements AhcBinding { try { Object data = in.getBody(); if (data != null) { - if (contentType != null && AhcConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(contentType)) { - + if (data instanceof BodyGenerator) { + // use existing ahc body generator + body = (BodyGenerator) data; + } else if (AhcConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(contentType)) { if (!endpoint.getComponent().isAllowJavaSerializedObject()) { throw new CamelExchangeException( "Content-type " + AhcConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT + " is not allowed", exchange); } - // serialized java object Serializable obj = in.getMandatoryBody(Serializable.class); // write object to output stream @@ -201,6 +202,8 @@ public class DefaultAhcBinding implements AhcBinding { if (file != null) { body = new FileBodyGenerator(file); } + } else if (data instanceof byte[]) { + body = new ByteArrayBodyGenerator((byte[]) data); } else if (data instanceof String) { // be a bit careful with String as any type can most likely be converted to String // so we only do an instanceof check and accept String if the body is really a String diff --git a/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcProducePostTest.java b/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcProducePostTest.java index cd1ec98..eab677d 100644 --- a/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcProducePostTest.java +++ b/components/camel-ahc/src/test/java/org/apache/camel/component/ahc/AhcProducePostTest.java @@ -16,6 +16,9 @@ */ package org.apache.camel.component.ahc; +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; + import org.apache.camel.Exchange; import org.apache.camel.builder.RouteBuilder; import org.junit.jupiter.api.Test; @@ -34,6 +37,31 @@ public class AhcProducePostTest extends BaseAhcTest { } @Test + public void testAhcProduceByteArray() throws Exception { + getMockEndpoint("mock:input").expectedBodiesReceived("World"); + // should not use chunked when its byte array + getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.CONTENT_LENGTH, 5); + getMockEndpoint("mock:input").message(0).header(Exchange.TRANSFER_ENCODING).isNull(); + getMockEndpoint("mock:result").expectedBodiesReceived("Bye World"); + + template.sendBody("direct:start", "World".getBytes(StandardCharsets.UTF_8)); + + assertMockEndpointsSatisfied(); + } + + @Test + public void testAhcProduceStream() throws Exception { + getMockEndpoint("mock:input").expectedBodiesReceived("World"); + // should use chunked for stream + getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.TRANSFER_ENCODING, "chunked"); + getMockEndpoint("mock:result").expectedBodiesReceived("Bye World"); + + template.sendBody("direct:start", new ByteArrayInputStream("World".getBytes(StandardCharsets.UTF_8))); + + assertMockEndpointsSatisfied(); + } + + @Test public void testAhcProduceInOut() throws Exception { getMockEndpoint("mock:result").expectedBodiesReceived("Bye World"); @@ -67,6 +95,8 @@ public class AhcProducePostTest extends BaseAhcTest { .to("mock:result"); from(getTestServerEndpointUri()) + .convertBodyTo(String.class) + .to("mock:input") .transform(simple("Bye ${body}")); } };