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 5e8588e244c0 CAMEL-27151: camel-ftp - Cannot store file says why, when 
the body is not file content, as camel-file does (#30324)
5e8588e244c0 is described below

commit 5e8588e244c006b0309d90cefc7a9c9573041480
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Nov 18 13:22:00 2026 +0100

    CAMEL-27151: camel-ftp - Cannot store file says why, when the body is not 
file content, as camel-file does (#30324)
    
    When the body cannot be written as a file, for example a List after 
unmarshal: csv, the ftp and
    sftp producers failed with "Cannot store file: <name>" alone, and the 
producer logged only that.
    The reason was in the cause, which no log line showed. The message reads 
like a problem with the
    folder, but the folder is fine. The file producer already says why 
(CAMEL-25751): what the body
    is, the step that set it, and the marshal that writes it.
    
    The ftp and sftp producers now read the body with the same helper, moved 
from FileOperations to
    GenericFileHelper.mandatoryBodyStream, and "Cannot store file" ends with 
its message. In the
    local-model benchmark (connect-sftp-transfer step 2, r30 to r35), 19 of 35 
step runs got the bare
    message, and 12 of them failed.
    
    Claude-Session: https://claude.ai/code/session_016Y4dDi5iQNgtHrDdz2NpwF
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../camel/component/file/FileOperations.java       | 28 ++--------------------
 .../camel/component/file/GenericFileHelper.java    | 24 +++++++++++++++++++
 .../camel/component/file/remote/FtpOperations.java |  5 ++--
 .../component/file/remote/SftpOperations.java      |  7 ++++--
 .../FtpChangeDirectoryFailureMessageIT.java        | 20 ++++++++++++++++
 .../integration/SftpOperationFailureMessageIT.java | 16 +++++++++++++
 6 files changed, 70 insertions(+), 30 deletions(-)

diff --git 
a/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java
 
b/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java
index e24b4d0ccbbc..c9e73f3baa0a 100644
--- 
a/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java
+++ 
b/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java
@@ -48,9 +48,7 @@ import java.util.concurrent.locks.ReentrantLock;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.InvalidPayloadException;
-import org.apache.camel.Message;
 import org.apache.camel.WrappedFile;
-import org.apache.camel.support.BodyFacts;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
@@ -427,28 +425,6 @@ public class FileOperations implements 
GenericFileOperations<File> {
         }
     }
 
-    /**
-     * The body as a stream to write. A Map, a List or an object (a body 
unmarshalled from JSON, XML or CSV, or built in
-     * the route) cannot become one, and the converter's message only names 
the conversion: say what the body is, the
-     * step that set it, and the marshal that writes it, in the data format 
the file name names (CAMEL-25751,
-     * CAMEL-25749).
-     */
-    static InputStream mandatoryBodyStream(Exchange exchange, Message message, 
File file) throws InvalidPayloadException {
-        try {
-            return message.getMandatoryBody(InputStream.class);
-        } catch (InvalidPayloadException e) {
-            String hint = BodyFacts.hint(exchange, BodyFacts.Need.BYTES, 
file.getName());
-            if (hint != null) {
-                InvalidPayloadException answer = new InvalidPayloadException(
-                        exchange, InputStream.class, message,
-                        BodyFacts.describe(exchange) + ", which is not file 
content: " + hint);
-                answer.initCause(e.getCause() != null ? e.getCause() : e);
-                throw answer;
-            }
-            throw e;
-        }
-    }
-
     @Override
     public boolean storeFileDirectly(String name, String payload) throws 
GenericFileOperationFailedException {
         File file = new File(name);
@@ -470,7 +446,7 @@ public class FileOperations implements 
GenericFileOperations<File> {
             is = exchange.getMessage().getBody(FileInputStream.class);
         }
         if (is == null) {
-            is = mandatoryBodyStream(exchange, exchange.getIn(), file);
+            is = GenericFileHelper.mandatoryBodyStream(exchange, 
exchange.getIn(), file.getName());
         }
         writeFileByStream(is, file, digest);
     }
@@ -487,7 +463,7 @@ public class FileOperations implements 
GenericFileOperations<File> {
         if (in == null) {
             // okay no direct reader conversion, so use an input stream
             // (which a lot can be converted as)
-            in = new InputStreamReader(mandatoryBodyStream(exchange, 
exchange.getIn(), file));
+            in = new 
InputStreamReader(GenericFileHelper.mandatoryBodyStream(exchange, 
exchange.getIn(), file.getName()));
         }
         // buffer the reader
         in = IOHelper.buffered(in);
diff --git 
a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileHelper.java
 
b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileHelper.java
index e644dffb93b0..d6bb7ab44db3 100644
--- 
a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileHelper.java
+++ 
b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileHelper.java
@@ -32,6 +32,7 @@ import java.util.function.Supplier;
 import org.apache.camel.Exchange;
 import org.apache.camel.InvalidPayloadException;
 import org.apache.camel.Message;
+import org.apache.camel.support.BodyFacts;
 import org.apache.camel.support.MessageHelper;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.IOHelper;
@@ -226,6 +227,29 @@ public final class GenericFileHelper {
         return compactTarget.equals(dir) || compactTarget.startsWith(dir + 
separator);
     }
 
+    /**
+     * The body as a stream to write to a file of the given name. A Map, a 
List or an object (a body unmarshalled from
+     * JSON, XML or CSV, or built in the route) cannot become one, and the 
converter's message only names the
+     * conversion: says what the body is, the step that set it, and the 
marshal that writes it, in the data format the
+     * file name names (CAMEL-25751, CAMEL-25749). The file, ftp and sftp 
producers read the body with it (CAMEL-27151).
+     */
+    public static InputStream mandatoryBodyStream(Exchange exchange, Message 
message, String fileName)
+            throws InvalidPayloadException {
+        try {
+            return message.getMandatoryBody(InputStream.class);
+        } catch (InvalidPayloadException e) {
+            String hint = BodyFacts.hint(exchange, BodyFacts.Need.BYTES, 
fileName);
+            if (hint != null) {
+                InvalidPayloadException answer = new InvalidPayloadException(
+                        exchange, InputStream.class, message,
+                        BodyFacts.describe(exchange) + ", which is not file 
content: " + hint);
+                answer.initCause(e.getCause() != null ? e.getCause() : e);
+                throw answer;
+            }
+            throw e;
+        }
+    }
+
     /**
      * Gets the message body as an input stream with the content encoded in 
the given charset.
      * <p/>
diff --git 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
index be176170ce81..b49b9c98b5ef 100644
--- 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
+++ 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
@@ -844,7 +844,7 @@ public class FtpOperations implements 
RemoteFileOperations<FTPFile> {
                     is = GenericFileHelper.toInputStream(exchange, charset);
                     log.trace("Using InputStream {} with charset {}.", is, 
charset);
                 } else {
-                    is = exchange.getIn().getMandatoryBody(InputStream.class);
+                    is = GenericFileHelper.mandatoryBodyStream(exchange, 
exchange.getIn(), FileUtil.stripPath(name));
                 }
             }
 
@@ -883,7 +883,8 @@ public class FtpOperations implements 
RemoteFileOperations<FTPFile> {
         } catch (IOException e) {
             throw new 
GenericFileOperationFailedException(client.getReplyCode(), 
client.getReplyString(), e.getMessage(), e);
         } catch (InvalidPayloadException e) {
-            throw new GenericFileOperationFailedException("Cannot store file: 
" + name, e);
+            // say why: the body cannot be written as it is (CAMEL-27151)
+            throw new GenericFileOperationFailedException("Cannot store file: 
" + name + ": " + e.getMessage(), e);
         } finally {
             IOHelper.close(is, "store: " + name, log);
         }
diff --git 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
index 1250230e5cd8..590882685a91 100644
--- 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
+++ 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
@@ -1550,7 +1550,7 @@ public class SftpOperations
                     is = GenericFileHelper.toInputStream(exchange, charset);
                     LOG.trace("Using InputStream {} with charset {}.", is, 
charset);
                 } else {
-                    is = exchange.getIn().getMandatoryBody(InputStream.class);
+                    is = GenericFileHelper.mandatoryBodyStream(exchange, 
exchange.getIn(), FileUtil.stripPath(name));
                 }
             }
 
@@ -1585,7 +1585,10 @@ public class SftpOperations
         } catch (SftpException e) {
             createResultHeadersFromExchange(e, exchange);
             throw operationFailed("Cannot store file: " + name, e);
-        } catch (IOException | InvalidPayloadException e) {
+        } catch (InvalidPayloadException e) {
+            // say why: the body cannot be written as it is (CAMEL-27151)
+            throw new GenericFileOperationFailedException("Cannot store file: 
" + name + ": " + e.getMessage(), e);
+        } catch (IOException e) {
             throw new GenericFileOperationFailedException("Cannot store file: 
" + name, e);
         } finally {
             IOHelper.close(is, "store: " + name, LOG);
diff --git 
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/integration/FtpChangeDirectoryFailureMessageIT.java
 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/integration/FtpChangeDirectoryFailureMessageIT.java
index a37637ed234c..870bff25e4ce 100644
--- 
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/integration/FtpChangeDirectoryFailureMessageIT.java
+++ 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/integration/FtpChangeDirectoryFailureMessageIT.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.file.remote.integration;
 
 import java.nio.file.Files;
+import java.util.List;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.component.file.GenericFileOperationFailedException;
@@ -24,6 +25,7 @@ import org.junit.jupiter.api.Test;
 
 import static org.apache.camel.test.junit6.TestSupport.assertIsInstanceOf;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
@@ -78,4 +80,22 @@ class FtpChangeDirectoryFailureMessageIT extends 
FtpServerTestSupport {
         assertNull(out.getException());
         
assertTrue(Files.exists(service.getFtpRootDir().resolve("existing/hello.txt")));
     }
+
+    @Test
+    void storeOfABodyThatIsNotFileContentSaysWhy() throws Exception {
+        // a List, as unmarshal: csv leaves it, to a directory that exists: 
the body is not file content (CAMEL-27151)
+        Files.createDirectories(service.getFtpRootDir().resolve("failed"));
+        Exchange out = 
template.send("ftp://admin@localhost:{{ftp.server.port}}/failed?password=admin";,
 e -> {
+            e.getIn().setBody(List.of(List.of("INV-3006", "12.x")));
+            e.getIn().setHeader(Exchange.FILE_NAME, "invoices.csv");
+        });
+
+        GenericFileOperationFailedException e
+                = 
assertIsInstanceOf(GenericFileOperationFailedException.class, 
out.getException());
+        assertTrue(e.getMessage().startsWith("Cannot store file: "), 
e.getMessage());
+        assertTrue(e.getMessage().contains("the body is a List, which is not 
file content: marshal it first (marshal: csv,"
+                                           + " for the file name 
invoices.csv)"),
+                e.getMessage());
+        
assertFalse(Files.exists(service.getFtpRootDir().resolve("failed/invoices.csv")));
+    }
 }
diff --git 
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/integration/SftpOperationFailureMessageIT.java
 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/integration/SftpOperationFailureMessageIT.java
index b9a1cef56f96..31d463c0e5d9 100644
--- 
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/integration/SftpOperationFailureMessageIT.java
+++ 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/integration/SftpOperationFailureMessageIT.java
@@ -150,6 +150,22 @@ class SftpOperationFailureMessageIT extends 
BaseServerTestSupport {
         assertEquals(ChannelSftp.SSH_FX_NO_SUCH_FILE, 
out.getIn().getHeader(FtpConstants.FTP_REPLY_CODE));
     }
 
+    @Test
+    void storeOfABodyThatIsNotFileContentSaysWhy() {
+        // a List, as unmarshal: csv leaves it: the directory is fine, the 
body is not file content (CAMEL-27151)
+        Exchange out = template.send(uri(dir("failed"), ""), e -> {
+            e.getIn().setBody(List.of(List.of("INV-3006", "12.x")));
+            e.getIn().setHeader(Exchange.FILE_NAME, "invoices.csv");
+        });
+
+        GenericFileOperationFailedException e
+                = 
assertIsInstanceOf(GenericFileOperationFailedException.class, 
out.getException());
+        assertTrue(e.getMessage().startsWith("Cannot store file: "), 
e.getMessage());
+        assertTrue(e.getMessage().contains("the body is a List, which is not 
file content: marshal it first (marshal: csv,"
+                                           + " for the file name 
invoices.csv)"),
+                e.getMessage());
+    }
+
     @Test
     void deleteAndListFailuresGiveTheStatus() {
         SftpEndpoint endpoint = context.getEndpoint(uri("", ""), 
SftpEndpoint.class);

Reply via email to