You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2026/07/23 05:08:54 UTC

(camel) branch main updated: CAMEL-24231: camel-ftp - Fix pollNamedFile to use dynamic exchange for useList=false

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 cd318d31b9fb CAMEL-24231: camel-ftp - Fix pollNamedFile to use dynamic exchange for useList=false
cd318d31b9fb is described below

commit cd318d31b9fb35a27e9ddb1f2aa8c54b7609b434
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Jul 23 07:08:47 2026 +0200

    CAMEL-24231: camel-ftp - Fix pollNamedFile to use dynamic exchange for useList=false
    
    When pollEnrich uses useList=false, the FTP/SFTP consumers call pollNamedFile()
    to evaluate the fileName expression. Previously this used an empty dummy exchange
    (ExchangeHelper.getDummy), which lacked headers, variables, and properties from the
    dynamic source exchange. This caused fileName expressions referencing dynamic data
    (e.g., ${header.myFile}) to evaluate to null.
    
    Fix: thread the dynamic exchange through getFtpFiles/getSftpRemoteFiles into
    pollNamedFile, and use GenericFileHelper.createDummy(endpoint, dynamic) which
    enriches the dummy with the dynamic exchange's headers, variables, and properties.
    Also extract enrichFromDynamic() to deduplicate the enrichment logic in
    GenericFileHelper.
    
    Closes #25031
    
    Co-Authored-By: Claude Opus 4.6 <no...@anthropic.com>
---
 .../org/apache/camel/component/file/GenericFileHelper.java  | 13 +++++++++++--
 .../camel/component/file/remote/AbstractSftpConsumer.java   | 12 ++++++------
 .../org/apache/camel/component/file/remote/FtpConsumer.java | 12 ++++++------
 3 files changed, 23 insertions(+), 14 deletions(-)

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 47753ef25b7f..7c73d148ac67 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
@@ -90,8 +90,18 @@ public final class GenericFileHelper {
 
     public static <T> Exchange createDummy(GenericFileEndpoint<T> endpoint, Exchange dynamic, Supplier<GenericFile<T>> file) {
         Exchange dummy = endpoint.createExchange(file.get());
+        enrichFromDynamic(dummy, dynamic);
+        return dummy;
+    }
+
+    public static <T> Exchange createDummy(GenericFileEndpoint<T> endpoint, Exchange dynamic) {
+        Exchange dummy = endpoint.createExchange();
+        enrichFromDynamic(dummy, dynamic);
+        return dummy;
+    }
+
+    private static void enrichFromDynamic(Exchange dummy, Exchange dynamic) {
         if (dynamic != null) {
-            // enrich with data from dynamic source
             if (dynamic.getMessage().hasHeaders()) {
                 MessageHelper.copyHeaders(dynamic.getMessage(), dummy.getMessage(), true);
             }
@@ -102,7 +112,6 @@ public final class GenericFileHelper {
                 dummy.getProperties().putAll(dynamic.getProperties());
             }
         }
-        return dummy;
     }
 
 }
diff --git a/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/AbstractSftpConsumer.java b/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/AbstractSftpConsumer.java
index 90b955fa3522..f2ee39f1071d 100644
--- a/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/AbstractSftpConsumer.java
+++ b/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/AbstractSftpConsumer.java
@@ -24,9 +24,9 @@ import java.util.function.Supplier;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.component.file.GenericFile;
+import org.apache.camel.component.file.GenericFileHelper;
 import org.apache.camel.component.file.GenericFileOperationFailedException;
 import org.apache.camel.component.file.GenericFileProcessStrategy;
-import org.apache.camel.support.ExchangeHelper;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.StringHelper;
@@ -128,7 +128,7 @@ public abstract class AbstractSftpConsumer extends RemoteFileConsumer<SftpRemote
             dir = absolutePath;
         }
 
-        final SftpRemoteFile[] files = getSftpRemoteFiles(dir);
+        final SftpRemoteFile[] files = getSftpRemoteFiles(dynamic, dir);
 
         if (files == null || files.length == 0) {
             // no files in this directory to poll
@@ -198,14 +198,14 @@ public abstract class AbstractSftpConsumer extends RemoteFileConsumer<SftpRemote
         return operations.listFiles(dir);
     }
 
-    private SftpRemoteFile[] getSftpRemoteFiles(String dir) {
+    private SftpRemoteFile[] getSftpRemoteFiles(Exchange dynamic, String dir) {
         SftpRemoteFile[] files = null;
         try {
             LOG.trace("Polling directory: {}", dir);
             if (isUseList()) {
                 files = listFiles(dir);
             } else {
-                files = pollNamedFile();
+                files = pollNamedFile(dynamic);
             }
         } catch (GenericFileOperationFailedException e) {
             if (ignoreCannotRetrieveFile(null, null, e)) {
@@ -217,12 +217,12 @@ public abstract class AbstractSftpConsumer extends RemoteFileConsumer<SftpRemote
         return files;
     }
 
-    private SftpRemoteFile[] pollNamedFile() {
+    private SftpRemoteFile[] pollNamedFile(Exchange dynamic) {
         SftpRemoteFile[] files = null;
 
         // we cannot use the LIST command(s) so we can only poll a named
         // file so created a pseudo file with that name
-        Exchange dummy = ExchangeHelper.getDummy(getEndpoint().getCamelContext());
+        Exchange dummy = GenericFileHelper.createDummy(getEndpoint(), dynamic);
         String name = evaluateFileExpression(dummy);
         if (name != null) {
             SftpRemoteFile file = new SftpRemoteFileSingle(name);
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
index 1985a707f117..d504d868de05 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
@@ -27,9 +27,9 @@ import org.apache.camel.Processor;
 import org.apache.camel.api.management.ManagedAttribute;
 import org.apache.camel.api.management.ManagedResource;
 import org.apache.camel.component.file.GenericFile;
+import org.apache.camel.component.file.GenericFileHelper;
 import org.apache.camel.component.file.GenericFileOperationFailedException;
 import org.apache.camel.component.file.GenericFileProcessStrategy;
-import org.apache.camel.support.ExchangeHelper;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.StringHelper;
@@ -136,7 +136,7 @@ public class FtpConsumer extends RemoteFileConsumer<FTPFile> {
         // compute dir depending on stepwise is enabled or not
         final String dir = computeDir(absolutePath, dirName);
 
-        final FTPFile[] files = getFtpFiles(dir);
+        final FTPFile[] files = getFtpFiles(dynamic, dir);
 
         if (files == null || files.length == 0) {
             // no files in this directory to poll
@@ -233,11 +233,11 @@ public class FtpConsumer extends RemoteFileConsumer<FTPFile> {
         return dir;
     }
 
-    private FTPFile[] pollNamedFile() {
+    private FTPFile[] pollNamedFile(Exchange dynamic) {
         FTPFile[] files = null;
         // we cannot use the LIST command(s) so we can only poll a named
         // file so created a pseudo file with that name
-        Exchange dummy = ExchangeHelper.getDummy(getEndpoint().getCamelContext());
+        Exchange dummy = GenericFileHelper.createDummy(getEndpoint(), dynamic);
         String name = evaluateFileExpression(dummy);
         if (name != null) {
             FTPFile file = new FTPFile();
@@ -257,14 +257,14 @@ public class FtpConsumer extends RemoteFileConsumer<FTPFile> {
         return operations.listFiles(dir);
     }
 
-    private FTPFile[] getFtpFiles(String dir) {
+    private FTPFile[] getFtpFiles(Exchange dynamic, String dir) {
         FTPFile[] files = null;
         try {
             LOG.trace("Polling directory: {}", dir);
             if (isUseList()) {
                 files = listFiles(dir);
             } else {
-                files = pollNamedFile();
+                files = pollNamedFile(dynamic);
             }
         } catch (GenericFileOperationFailedException e) {
             if (ignoreCannotRetrieveFile(null, null, e)) {