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 0066c102dd1e404edeab47aa693cb91c03892b7d Author: smjain <[email protected]> AuthorDate: Wed Sep 30 08:06:51 2026 +0530 CAMEL-25154: camel-vertx-http - send a file body that is not a local java.io.File Since CAMEL-22044 the producer handles File and WrappedFile bodies in their own branch, which converts the body to java.io.File and sends nothing when the conversion gives null. That is the case for the remote file consumers without a localWorkDirectory (camel-ftp, sftp, ftps, smb): the RemoteFile holds the downloaded content as byte[] or an InputStream, and there is no conversion to File. Nothing was sent, the callback was never called and the exchange never completed, so the route hung. Before 4.12.0 such a body was sent through the conversion to a Vert.x Buffer. When the file body is not a local file, send its content, as a multipart file upload named by the CamelFileNameOnly header (default: the multipartUploadName) when multipartUpload is enabled. A conversion failure now fails the exchange through the existing error handling. Co-Authored-By: Claude Opus 5.5 <[email protected]> --- .../component/vertx/http/VertxHttpProducer.java | 31 ++++--- .../vertx/http/VertxHttpRemoteFileBodyTest.java | 95 ++++++++++++++++++++++ 2 files changed, 114 insertions(+), 12 deletions(-) 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 6f6bae571f16..d3ba891e0c12 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 @@ -98,21 +98,28 @@ public class VertxHttpProducer extends DefaultAsyncProducer { if (body instanceof File || body instanceof WrappedFile<?>) { // file based (could potentially also be a FTP file etc) File file = message.getBody(File.class); + Buffer buf; + String fileName; if (file != null) { try (InputStream is = new FileInputStream(file)) { - Buffer buf = VertxBufferConverter.toBuffer(is); - if (multipart) { - String type = MimeTypeHelper.probeMimeType(file.getName()); - if (type == null) { - type = "application/octet-stream"; // default binary - } - MultipartForm form - = MultipartForm.create().binaryFileUpload(multipartName, file.getName(), buf, type); - request.sendMultipartForm(form, resultHandler); - } else { - request.sendBuffer(buf, resultHandler); - } + buf = VertxBufferConverter.toBuffer(is); } + fileName = file.getName(); + } else { + // 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); + } + if (multipart) { + String type = MimeTypeHelper.probeMimeType(fileName); + if (type == null) { + type = "application/octet-stream"; // default binary + } + MultipartForm form = MultipartForm.create().binaryFileUpload(multipartName, fileName, buf, type); + request.sendMultipartForm(form, resultHandler); + } else { + request.sendBuffer(buf, resultHandler); } } else if (body instanceof String str) { // Try to extract URL encoded form data from the message body 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 new file mode 100644 index 000000000000..d7e94f973e58 --- /dev/null +++ b/components/camel-vertx/camel-vertx-http/src/test/java/org/apache/camel/component/vertx/http/VertxHttpRemoteFileBodyTest.java @@ -0,0 +1,95 @@ +/* + * 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.vertx.http; + +import java.nio.charset.StandardCharsets; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; + +import jakarta.activation.DataHandler; + +import org.apache.camel.Exchange; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.file.GenericFile; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; + +/** + * A file body that is not a local {@link java.io.File}, such as a file of the ftp or sftp consumers without a + * localWorkDirectory whose content is in memory, is sent with its content. + */ +public class VertxHttpRemoteFileBodyTest extends VertxHttpTestSupport { + + @Test + public void testRemoteFileBody() throws Exception { + Exchange out = send(getProducerUri() + "/echo"); + + assertFalse(out.isFailed(), "Should not fail"); + assertEquals("Hello World", out.getMessage().getBody(String.class)); + } + + @Test + public void testRemoteFileBodyMultipartUpload() throws Exception { + Exchange out = send(getProducerUri() + "/upload?multipartUpload=true&multipartUploadName=cheese"); + + assertFalse(out.isFailed(), "Should not fail"); + assertEquals("hello.txt=Hello World", out.getMessage().getBody(String.class)); + } + + private Exchange send(String uri) throws Exception { + CompletableFuture<Exchange> future = template.asyncSend(uri, exchange -> { + exchange.getMessage().setBody(createRemoteFile()); + 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() { + // like a remote file: the file handle is not a java.io.File, and the content is downloaded in memory + 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)); + return file; + } + + @Override + protected RoutesBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from(getTestServerUri() + "/echo") + .convertBodyTo(String.class); + + from(getTestServerUri() + "/upload") + .process(exchange -> { + // undertow stores the multipart form as a map in the message + DataHandler dh = (DataHandler) exchange.getMessage().getBody(Map.class).get("cheese"); + String content = new String( + dh.getInputStream().readAllBytes(), StandardCharsets.UTF_8); + exchange.getMessage().setBody(dh.getDataSource().getName() + "=" + content); + }); + } + }; + } +}
