This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-4.8.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.8.x by this push: new 545ebb1bc5c CAMEL-22125: camel-platform-http-vertx - Writing response should favour input stream over ByteBuffer 545ebb1bc5c is described below commit 545ebb1bc5c8d350f3d6105a334b7513147487f4 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Fri May 30 09:36:09 2025 +0200 CAMEL-22125: camel-platform-http-vertx - Writing response should favour input stream over ByteBuffer --- components/camel-platform-http-vertx/pom.xml | 5 ++ .../http/vertx/VertxPlatformHttpSupport.java | 19 +++-- .../vertx/VertxPlatformHttpFileResponseTest.java | 85 ++++++++++++++++++++++ .../src/test/resources/dummy.txt | 1 + 4 files changed, 104 insertions(+), 6 deletions(-) diff --git a/components/camel-platform-http-vertx/pom.xml b/components/camel-platform-http-vertx/pom.xml index 4b74dd582e0..a9d496fea94 100644 --- a/components/camel-platform-http-vertx/pom.xml +++ b/components/camel-platform-http-vertx/pom.xml @@ -84,6 +84,11 @@ <artifactId>camel-log</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-file</artifactId> + <scope>test</scope> + </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-rest-openapi</artifactId> diff --git a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java index 6ef9d71e958..7862c12185c 100644 --- a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java +++ b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java @@ -190,6 +190,8 @@ public final class VertxPlatformHttpSupport { } else if (body instanceof Buffer) { ctx.end((Buffer) body); promise.complete(); + } else if (body instanceof ByteBuffer bb) { + writeResponseAs(promise, ctx, bb); } else { writeResponseAsFallback(promise, camelExchange, body, ctx); } @@ -203,15 +205,20 @@ public final class VertxPlatformHttpSupport { private static void writeResponseAsFallback(Promise<Void> promise, Exchange camelExchange, Object body, RoutingContext ctx) throws NoTypeConversionAvailableException { final TypeConverter tc = camelExchange.getContext().getTypeConverter(); - // Try to convert to ByteBuffer for performance reason - final ByteBuffer bb = tc.tryConvertTo(ByteBuffer.class, camelExchange, body); + + // favour input stream first + InputStream is = tc.tryConvertTo(InputStream.class, camelExchange, body); + if (is != null) { + writeResponseAs(promise, ctx, is); + return; + } + // then fallback to byte buffer + ByteBuffer bb = tc.tryConvertTo(ByteBuffer.class, camelExchange, body); if (bb != null) { writeResponseAs(promise, ctx, bb); - } else { - // Otherwise fallback to most generic InputStream conversion - final InputStream is = tc.mandatoryConvertTo(InputStream.class, camelExchange, body); - writeResponseAs(promise, ctx, is); + return; } + throw new NoTypeConversionAvailableException(body, InputStream.class); } private static void writeResponseAs(Promise<Void> promise, RoutingContext ctx, ByteBuffer bb) { diff --git a/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpFileResponseTest.java b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpFileResponseTest.java new file mode 100644 index 00000000000..efafa702954 --- /dev/null +++ b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpFileResponseTest.java @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.camel.component.platform.http.vertx; + +import java.io.File; + +import org.apache.camel.CamelContext; +import org.apache.camel.builder.RouteBuilder; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.is; + +public class VertxPlatformHttpFileResponseTest { + + @Test + void testFileResponse() throws Exception { + final CamelContext context = VertxPlatformHttpEngineTest.createCamelContext(); + final File file = new File("src/test/resources/dummy.txt"); + + try { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("platform-http:/file") + .setBody(constant(file)); + } + }); + + context.start(); + + String requestBody = "Give me a file"; + given() + .body(requestBody) + .get("/file") + .then() + .statusCode(200) + .body(is("Hello World from this file")); + } finally { + context.stop(); + } + } + + @Test + void testFileEndpointResponse() throws Exception { + final CamelContext context = VertxPlatformHttpEngineTest.createCamelContext(); + + try { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("platform-http:/file") + .pollEnrich("file:src/test/resources/?fileName=dummy.txt&noop=true", 5000); + } + }); + + context.start(); + + String requestBody = "Give me a file"; + given() + .body(requestBody) + .get("/file") + .then() + .statusCode(200) + .body(is("Hello World from this file")); + } finally { + context.stop(); + } + } + +} diff --git a/components/camel-platform-http-vertx/src/test/resources/dummy.txt b/components/camel-platform-http-vertx/src/test/resources/dummy.txt new file mode 100644 index 00000000000..55a8594f6b1 --- /dev/null +++ b/components/camel-platform-http-vertx/src/test/resources/dummy.txt @@ -0,0 +1 @@ +Hello World from this file \ No newline at end of file