This is an automated email from the ASF dual-hosted git repository. apupier pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 4c21404e957b3a04067ff6267e8df0033588b20f Author: smjain <[email protected]> AuthorDate: Wed Sep 30 13:41:58 2026 +0530 CAMEL-25154: camel-vertx-http - name a remote file upload after the file, and test a streamed remote file body Without the CamelFileNameOnly header, a remote file body that is a GenericFile is now uploaded under the name of the file (GenericFile.getFileNameOnly()) instead of the form field name. The header still takes precedence. Adds tests for an InputStream body (streamDownload=true), plain and as a multipart upload, and for the file name fallback. Co-Authored-By: Claude Opus 5.5 <[email protected]> --- components/camel-vertx/camel-vertx-http/pom.xml | 4 ++ .../component/vertx/http/VertxHttpProducer.java | 10 ++++- .../vertx/http/VertxHttpRemoteFileBodyTest.java | 45 ++++++++++++++++++---- 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/components/camel-vertx/camel-vertx-http/pom.xml b/components/camel-vertx/camel-vertx-http/pom.xml index fb7e6ec834ff..f0661c13e6ce 100644 --- a/components/camel-vertx/camel-vertx-http/pom.xml +++ b/components/camel-vertx/camel-vertx-http/pom.xml @@ -43,6 +43,10 @@ <groupId>org.apache.camel</groupId> <artifactId>camel-vertx-common</artifactId> </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-file</artifactId> + </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-web-client</artifactId> diff --git a/components/camel-vertx/camel-vertx-http/src/main/java/org/apache/camel/component/vertx/http/VertxHttpProducer.java b/components/camel-vertx/camel-vertx-http/src/main/java/org/apache/camel/component/vertx/http/VertxHttpProducer.java index d3ba891e0c12..fc5a571614fc 100644 --- a/components/camel-vertx/camel-vertx-http/src/main/java/org/apache/camel/component/vertx/http/VertxHttpProducer.java +++ b/components/camel-vertx/camel-vertx-http/src/main/java/org/apache/camel/component/vertx/http/VertxHttpProducer.java @@ -37,6 +37,7 @@ import org.apache.camel.CamelExchangeException; import org.apache.camel.Exchange; import org.apache.camel.Message; import org.apache.camel.WrappedFile; +import org.apache.camel.component.file.GenericFile; import org.apache.camel.component.vertx.common.VertxBufferConverter; import org.apache.camel.support.DefaultAsyncProducer; import org.apache.camel.support.MessageHelper; @@ -109,7 +110,14 @@ public class VertxHttpProducer extends DefaultAsyncProducer { // not a local file, such as a remote file (ftp, sftp, smb) with its content in memory, // so send its content buf = message.getMandatoryBody(Buffer.class); - fileName = message.getHeader(Exchange.FILE_NAME_ONLY, multipartName, String.class); + // the header first, so it can be overridden, then the name of the file itself + fileName = message.getHeader(Exchange.FILE_NAME_ONLY, String.class); + if (fileName == null && body instanceof GenericFile<?> gf) { + fileName = gf.getFileNameOnly(); + } + if (fileName == null) { + fileName = multipartName; + } } if (multipart) { String type = MimeTypeHelper.probeMimeType(fileName); diff --git a/components/camel-vertx/camel-vertx-http/src/test/java/org/apache/camel/component/vertx/http/VertxHttpRemoteFileBodyTest.java b/components/camel-vertx/camel-vertx-http/src/test/java/org/apache/camel/component/vertx/http/VertxHttpRemoteFileBodyTest.java index d7e94f973e58..27e00192d0fb 100644 --- a/components/camel-vertx/camel-vertx-http/src/test/java/org/apache/camel/component/vertx/http/VertxHttpRemoteFileBodyTest.java +++ b/components/camel-vertx/camel-vertx-http/src/test/java/org/apache/camel/component/vertx/http/VertxHttpRemoteFileBodyTest.java @@ -16,6 +16,7 @@ */ package org.apache.camel.component.vertx.http; +import java.io.ByteArrayInputStream; import java.nio.charset.StandardCharsets; import java.util.Map; import java.util.concurrent.CompletableFuture; @@ -40,7 +41,16 @@ public class VertxHttpRemoteFileBodyTest extends VertxHttpTestSupport { @Test public void testRemoteFileBody() throws Exception { - Exchange out = send(getProducerUri() + "/echo"); + Exchange out = send(getProducerUri() + "/echo", false, true); + + assertFalse(out.isFailed(), "Should not fail"); + assertEquals("Hello World", out.getMessage().getBody(String.class)); + } + + @Test + public void testRemoteFileStreamBody() throws Exception { + // streamDownload=true: the content is an InputStream + Exchange out = send(getProducerUri() + "/echo", true, true); assertFalse(out.isFailed(), "Should not fail"); assertEquals("Hello World", out.getMessage().getBody(String.class)); @@ -48,28 +58,49 @@ public class VertxHttpRemoteFileBodyTest extends VertxHttpTestSupport { @Test public void testRemoteFileBodyMultipartUpload() throws Exception { - Exchange out = send(getProducerUri() + "/upload?multipartUpload=true&multipartUploadName=cheese"); + Exchange out = send(getProducerUri() + "/upload?multipartUpload=true&multipartUploadName=cheese", false, true); assertFalse(out.isFailed(), "Should not fail"); assertEquals("hello.txt=Hello World", out.getMessage().getBody(String.class)); } - private Exchange send(String uri) throws Exception { + @Test + public void testRemoteFileStreamBodyMultipartUpload() throws Exception { + Exchange out = send(getProducerUri() + "/upload?multipartUpload=true&multipartUploadName=cheese", true, true); + + assertFalse(out.isFailed(), "Should not fail"); + assertEquals("hello.txt=Hello World", out.getMessage().getBody(String.class)); + } + + @Test + public void testRemoteFileBodyMultipartUploadFileNameFromFile() throws Exception { + // without the CamelFileNameOnly header the name of the file is used, not the form field name + Exchange out = send(getProducerUri() + "/upload?multipartUpload=true&multipartUploadName=cheese", false, false); + + assertFalse(out.isFailed(), "Should not fail"); + assertEquals("hello.txt=Hello World", out.getMessage().getBody(String.class)); + } + + private Exchange send(String uri, boolean stream, boolean fileNameHeader) throws Exception { CompletableFuture<Exchange> future = template.asyncSend(uri, exchange -> { - exchange.getMessage().setBody(createRemoteFile()); - exchange.getMessage().setHeader(Exchange.FILE_NAME_ONLY, "hello.txt"); + exchange.getMessage().setBody(createRemoteFile(stream)); + if (fileNameHeader) { + exchange.getMessage().setHeader(Exchange.FILE_NAME_ONLY, "hello.txt"); + } }); // the exchange must complete, without the content being sent it never did return future.get(10, TimeUnit.SECONDS); } - private static GenericFile<Object> createRemoteFile() { + private static GenericFile<Object> createRemoteFile(boolean stream) { // like a remote file: the file handle is not a java.io.File, and the content is downloaded in memory + // (byte[]), or streamed (an InputStream, with streamDownload=true) GenericFile<Object> file = new GenericFile<>(); file.setFile(new Object()); file.setFileNameOnly("hello.txt"); file.setFileName("hello.txt"); - file.setBody("Hello World".getBytes(StandardCharsets.UTF_8)); + byte[] content = "Hello World".getBytes(StandardCharsets.UTF_8); + file.setBody(stream ? new ByteArrayInputStream(content) : content); return file; }
