This is an automated email from the ASF dual-hosted git repository.

Croway pushed a commit to branch camel-4.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 82af5c0b24cdfa7e1dcb7236bee3bb5400446490
Author: croway <[email protected]>
AuthorDate: Tue Nov 24 10:16:18 2026 +0100

    CAMEL-27453: camel-ftp, camel-mina-sftp - address review feedback
    
    - A stepwise recursive poll changes into a directory listed with a trailing 
/, such as sub/, as it did before.
    - Move isDirectoryOrParentOf from SftpOperations and MinaSftpOperations to 
FtpUtils.
    - Compile the separator pattern of GenericFileHelper once, use {@code} in 
the new Javadoc, and name the operations
      that the Javadoc of RemoteFileProducer.isBackslashNormalized describes.
    - FtpConsumerListedDirectoryNameTest waits with Awaitility instead of 
sleeping.
    
    The upgrade guide notes for this release are on main.
---
 .../camel/component/file/GenericFileHelper.java    |  9 ++---
 .../camel/component/file/remote/FtpUtils.java      | 31 ++++++++++++----
 .../component/file/remote/RemoteFileProducer.java  |  6 ++--
 .../component/file/remote/SftpOperations.java      | 10 +-----
 .../remote/FtpConsumerListedDirectoryNameTest.java | 42 +++++++++++++++++++++-
 .../file/remote/mina/MinaSftpOperations.java       | 11 ++----
 6 files changed, 77 insertions(+), 32 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 3bae35e9a14d..9b1a75a1261c 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
@@ -26,6 +26,7 @@ import java.nio.file.attribute.BasicFileAttributes;
 import java.util.ArrayDeque;
 import java.util.Deque;
 import java.util.function.Supplier;
+import java.util.regex.Pattern;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.support.MessageHelper;
@@ -34,7 +35,7 @@ import org.apache.camel.util.FileUtil;
 public final class GenericFileHelper {
 
     // the separators that split a path into segments when compacting it for a 
containment check
-    private static final String SEPARATORS = "[/\\\\]";
+    private static final Pattern SEPARATORS = Pattern.compile("[/\\\\]");
 
     private GenericFileHelper() {
     }
@@ -253,7 +254,7 @@ public final class GenericFileHelper {
      * on the server and that platform, unless its backslashes are replaced by 
/ before it is used.
      *
      * @param  path the path
-     * @return      <tt>true</tt> if the path has both a backslash and a 
{@code ..} segment
+     * @return      {@code true} if the path has both a backslash and a {@code 
..} segment
      */
     public static boolean isAmbiguousRemotePath(String path) {
         return path != null && path.indexOf('\\') != -1 && 
hasParentSegment(path);
@@ -278,7 +279,7 @@ public final class GenericFileHelper {
         }
         boolean absolute = !path.isEmpty() && (path.charAt(0) == '/' || 
path.charAt(0) == '\\');
         Deque<String> segments = new ArrayDeque<>();
-        for (String segment : path.split(SEPARATORS)) {
+        for (String segment : SEPARATORS.split(path)) {
             if (segment.isEmpty() || ".".equals(segment)) {
                 continue;
             }
@@ -293,7 +294,7 @@ public final class GenericFileHelper {
     }
 
     private static boolean hasParentSegment(String path) {
-        for (String segment : path.split(SEPARATORS)) {
+        for (String segment : SEPARATORS.split(path)) {
             if ("..".equals(segment)) {
                 return true;
             }
diff --git 
a/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/FtpUtils.java
 
b/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/FtpUtils.java
index 7989662e579a..b191180944ea 100644
--- 
a/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/FtpUtils.java
+++ 
b/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/FtpUtils.java
@@ -141,16 +141,35 @@ public final class FtpUtils {
 
     /**
      * Whether a directory name from a listing of the server is the name of 
one directory in the listed directory: not
-     * blank, not . or .., and without a / or a \. A stepwise recursive poll 
changes into such a directory one segment
-     * at a time, and back to its parent afterwards, so a name such as sub/.. 
or a\b would leave the poll in another
-     * directory than the listed one.
+     * blank, not . or .., and without a / or a \, once any trailing / is 
removed, as some servers list a directory with
+     * a trailing /. A stepwise recursive poll changes into such a directory 
one segment at a time, and back to its
+     * parent afterwards, so a name such as sub/.. or a\b would leave the poll 
in another directory than the listed one.
      *
      * @param  name the name of the directory, as listed
-     * @return      <tt>true</tt> if the poll can change into the directory
+     * @return      {@code true} if the poll can change into the directory
      */
     public static boolean isSubDirectoryName(String name) {
-        return name != null && !name.isBlank() && !".".equals(name) && 
!"..".equals(name) && name.indexOf('/') == -1
-                && name.indexOf('\\') == -1;
+        if (name == null) {
+            return false;
+        }
+        String directory = name;
+        while (directory.length() > 1 && directory.endsWith("/")) {
+            directory = directory.substring(0, directory.length() - 1);
+        }
+        return !directory.isBlank() && !".".equals(directory) && 
!"..".equals(directory) && directory.indexOf('/') == -1
+                && directory.indexOf('\\') == -1;
+    }
+
+    /**
+     * Whether a path is a directory itself or one of its parent directories. 
A path that only starts with the same
+     * characters, such as /home/cam for /home/camel, is not.
+     *
+     * @param  path      the path
+     * @param  directory the directory
+     * @return           {@code true} if the path is the directory or one of 
its parent directories
+     */
+    public static boolean isDirectoryOrParentOf(String path, String directory) 
{
+        return directory.equals(path) || 
directory.startsWith(path.endsWith("/") ? path : path + "/");
     }
 
     public static String absoluteFilePath(String absolutePath, String name) {
diff --git 
a/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.java
 
b/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.java
index ba32d68633d7..6ae48299dac7 100644
--- 
a/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.java
+++ 
b/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.java
@@ -54,9 +54,9 @@ public class RemoteFileProducer<T> extends 
GenericFileProducer<T> {
     }
 
     /**
-     * The operations replace a backslash in the name of the file they store 
by / when the separator option is UNIX (the
-     * default). They use the name as given to check, delete or rename a file, 
and to write a checksum file, so not when
-     * the producer writes a temp file, a done file or a checksum file.
+     * The ftp, ftps and sftp operations replace a backslash in the name of 
the file they store by / when the separator
+     * option is UNIX (the default). They use the name as given to check, 
delete or rename a file, and to write a
+     * checksum file, so not when the producer writes a temp file, a done file 
or a checksum file.
      */
     @Override
     protected boolean isBackslashNormalized() {
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 441bd38d7f87..276825cbc30f 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
@@ -837,7 +837,7 @@ public class SftpOperations implements 
RemoteFileOperations<SftpRemoteFile>, Sft
                 return;
             }
             String current = getCurrentDirectory();
-            if (isDirectoryOrParentOf(path, current)) {
+            if (FtpUtils.isDirectoryOrParentOf(path, current)) {
                 // extract the path segment relative to the target path and 
make
                 // sure it keeps the preceding '/' for the regex op
                 String p = current.substring(path.length() - 
(path.endsWith("/") ? 1 : 0));
@@ -884,14 +884,6 @@ public class SftpOperations implements 
RemoteFileOperations<SftpRemoteFile>, Sft
         }
     }
 
-    /**
-     * Whether the path is the directory itself or one of its parent 
directories. A path that only starts with the same
-     * characters, such as /home/cam for /home/camel, is not.
-     */
-    private static boolean isDirectoryOrParentOf(String path, String 
directory) {
-        return directory.equals(path) || 
directory.startsWith(path.endsWith("/") ? path : path + "/");
-    }
-
     private void doChangeDirectory(String path) {
         if (path == null || ".".equals(path) || ObjectHelper.isEmpty(path)) {
             return;
diff --git 
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerListedDirectoryNameTest.java
 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerListedDirectoryNameTest.java
index 873f81b26638..8fbe672a4862 100644
--- 
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerListedDirectoryNameTest.java
+++ 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerListedDirectoryNameTest.java
@@ -74,7 +74,7 @@ class FtpConsumerListedDirectoryNameTest extends 
CamelTestSupport {
 
         await().atMost(10, TimeUnit.SECONDS).until(() -> received.size() >= 2);
         // nothing more arrives: the poll does not list the inbox again 
through sub/..
-        TimeUnit.MILLISECONDS.sleep(500);
+        await().during(500, TimeUnit.MILLISECONDS).atMost(2, 
TimeUnit.SECONDS).until(() -> received.size() == 2);
 
         assertEquals(2, received.size(), received.toString());
         assertTrue(received.contains("hello.txt"), received.toString());
@@ -83,6 +83,25 @@ class FtpConsumerListedDirectoryNameTest extends 
CamelTestSupport {
         assertFalse(server.commands().stream().anyMatch(c -> c.startsWith("CWD 
sub")), server.commands().toString());
     }
 
+    @Test
+    void pollsListedRelativeDirectoryWithTrailingSeparatorStepwise() throws 
Exception {
+        // a server can list a directory with a trailing /, which a stepwise 
poll changes into by its name
+        server.addFile("/inbox/child/report.txt", 
"report".getBytes(StandardCharsets.UTF_8));
+        server.addListedDirectory("/inbox", "child/");
+        List<String> received = new CopyOnWriteArrayList<>();
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("ftp://[email protected]:"; + server.port() + 
"/inbox?password=secret&passiveMode=true"
+                     + "&recursive=true&noop=true&initialDelay=0&delay=600000")
+                        .process(e -> 
received.add(e.getMessage().getHeader(Exchange.FILE_NAME, String.class)));
+            }
+        });
+
+        await().atMost(10, TimeUnit.SECONDS).until(() -> !received.isEmpty());
+        assertEquals(List.of("child/report.txt"), received);
+    }
+
     @Test
     void pollsListedDirectoryWithAbsoluteNameOfServerListingAbsolutePaths() 
throws Exception {
         // a server that lists the absolute path of each entry
@@ -124,5 +143,26 @@ class FtpConsumerListedDirectoryNameTest extends 
CamelTestSupport {
         assertFalse(FtpUtils.isSubDirectoryName("\\sub"));
         assertFalse(FtpUtils.isSubDirectoryName("a\\b"));
         assertFalse(FtpUtils.isSubDirectoryName(" "));
+        // a trailing / is not part of the name, as some servers list a 
directory that way
+        assertTrue(FtpUtils.isSubDirectoryName("sub/"));
+        assertTrue(FtpUtils.isSubDirectoryName("sub//"));
+        assertFalse(FtpUtils.isSubDirectoryName("/"));
+        assertFalse(FtpUtils.isSubDirectoryName("//"));
+        assertFalse(FtpUtils.isSubDirectoryName("./"));
+        assertFalse(FtpUtils.isSubDirectoryName("../"));
+        assertFalse(FtpUtils.isSubDirectoryName(" /"));
+        assertFalse(FtpUtils.isSubDirectoryName("sub/../"));
+        assertFalse(FtpUtils.isSubDirectoryName("sub\\"));
+        assertFalse(FtpUtils.isSubDirectoryName("sub\\/"));
+    }
+
+    @Test
+    void directoryOrParentOf() {
+        assertTrue(FtpUtils.isDirectoryOrParentOf("/home/camel", 
"/home/camel"));
+        assertTrue(FtpUtils.isDirectoryOrParentOf("/home", "/home/camel"));
+        assertTrue(FtpUtils.isDirectoryOrParentOf("/home/", "/home/camel"));
+        assertTrue(FtpUtils.isDirectoryOrParentOf("/", "/home/camel"));
+        assertFalse(FtpUtils.isDirectoryOrParentOf("/home/cam", 
"/home/camel"));
+        assertFalse(FtpUtils.isDirectoryOrParentOf("/home/camel/sub", 
"/home/camel"));
     }
 }
diff --git 
a/components/camel-mina-sftp/src/main/java/org/apache/camel/component/file/remote/mina/MinaSftpOperations.java
 
b/components/camel-mina-sftp/src/main/java/org/apache/camel/component/file/remote/mina/MinaSftpOperations.java
index 89b50634b4fb..52e3cbc84de5 100644
--- 
a/components/camel-mina-sftp/src/main/java/org/apache/camel/component/file/remote/mina/MinaSftpOperations.java
+++ 
b/components/camel-mina-sftp/src/main/java/org/apache/camel/component/file/remote/mina/MinaSftpOperations.java
@@ -45,6 +45,7 @@ import org.apache.camel.component.file.GenericFileExist;
 import org.apache.camel.component.file.GenericFileHelper;
 import org.apache.camel.component.file.GenericFileOperationFailedException;
 import org.apache.camel.component.file.remote.FtpConstants;
+import org.apache.camel.component.file.remote.FtpUtils;
 import org.apache.camel.component.file.remote.RemoteFile;
 import org.apache.camel.component.file.remote.RemoteFileConfiguration;
 import org.apache.camel.component.file.remote.RemoteFileOperations;
@@ -1048,7 +1049,7 @@ public class MinaSftpOperations implements 
RemoteFileOperations<SftpRemoteFile>,
                 return;
             }
             String current = getCurrentDirectory();
-            if (isDirectoryOrParentOf(path, current)) {
+            if (FtpUtils.isDirectoryOrParentOf(path, current)) {
                 // extract the path segment relative to the target path and 
make
                 // sure it keeps the preceding '/' for the regex op
                 String p = current.substring(path.length() - 
(path.endsWith("/") ? 1 : 0));
@@ -1095,14 +1096,6 @@ public class MinaSftpOperations implements 
RemoteFileOperations<SftpRemoteFile>,
         }
     }
 
-    /**
-     * Whether the path is the directory itself or one of its parent 
directories. A path that only starts with the same
-     * characters, such as /home/cam for /home/camel, is not.
-     */
-    private static boolean isDirectoryOrParentOf(String path, String 
directory) {
-        return directory.equals(path) || 
directory.startsWith(path.endsWith("/") ? path : path + "/");
-    }
-
     private void doChangeDirectory(String path) {
         if (path == null || ".".equals(path) || ObjectHelper.isEmpty(path)) {
             return;

Reply via email to