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 2d160cc156c3 CAMEL-26984: camel-ftp - a directory listing the server 
refuses fails the poll instead of looking empty (#30041)
2d160cc156c3 is described below

commit 2d160cc156c3afde47a7687bc188538d9f8ca656
Author: Federico Mariani <[email protected]>
AuthorDate: Sun Nov 15 19:03:28 2026 +0100

    CAMEL-26984: camel-ftp - a directory listing the server refuses fails the 
poll instead of looking empty (#30041)
    
    Co-authored-by: Claude <[email protected]>
---
 .../apache/camel/catalog/docs/ftp-component.adoc   |   1 +
 .../camel-ftp/src/main/docs/ftp-component.adoc     |   1 +
 .../camel/component/file/remote/FtpOperations.java |  27 ++++-
 .../FtpConsumerActiveModeRefusedIT.java            | 109 +++++++++++++++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc    |   9 ++
 5 files changed, 144 insertions(+), 3 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/ftp-component.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/ftp-component.adoc
index a5bc8373d03a..cb25b8cec464 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/ftp-component.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/ftp-component.adoc
@@ -839,6 +839,7 @@ problems.
 
 The xref:components::ftp-component.adoc[FTP] component has many options. So 
make sure you have configured it properly.
 Also, a common issue is that you have to use either active or passive mode. So 
you may have to set `passiveMode=true` on the endpoint configuration.
+When the server refuses the listing of the directory, for example the `PORT` 
command of active mode, the poll fails and the consumer logs the reply of the 
server.
 
 == Examples
 
diff --git a/components/camel-ftp/src/main/docs/ftp-component.adoc 
b/components/camel-ftp/src/main/docs/ftp-component.adoc
index a5bc8373d03a..cb25b8cec464 100644
--- a/components/camel-ftp/src/main/docs/ftp-component.adoc
+++ b/components/camel-ftp/src/main/docs/ftp-component.adoc
@@ -839,6 +839,7 @@ problems.
 
 The xref:components::ftp-component.adoc[FTP] component has many options. So 
make sure you have configured it properly.
 Also, a common issue is that you have to use either active or passive mode. So 
you may have to set `passiveMode=true` on the endpoint configuration.
+When the server refuses the listing of the directory, for example the `PORT` 
command of active mode, the poll fails and the consumer logs the reply of the 
server.
 
 == Examples
 
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 56965c73e4da..572f426f3a75 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
@@ -978,8 +978,7 @@ public class FtpOperations implements 
RemoteFileOperations<FTPFile> {
         log.trace("Listing remote files");
         
clientActivityListener.onScanningForFiles(endpoint.remoteServerInformation(), 
null);
         try {
-
-            return client.listFiles();
+            return checkListing(client.listFiles(), "the current directory");
         } catch (IOException e) {
             
clientActivityListener.onGeneralError(endpoint.getConfiguration().remoteServerInformation(),
 e.getMessage());
             throw new 
GenericFileOperationFailedException(client.getReplyCode(), 
client.getReplyString(), e.getMessage(), e);
@@ -997,13 +996,35 @@ public class FtpOperations implements 
RemoteFileOperations<FTPFile> {
         }
 
         try {
-            return client.listFiles(path);
+            return checkListing(client.listFiles(path), "directory: " + path);
         } catch (IOException e) {
             
clientActivityListener.onGeneralError(endpoint.getConfiguration().remoteServerInformation(),
 e.getMessage());
             throw new 
GenericFileOperationFailedException(client.getReplyCode(), 
client.getReplyString(), e.getMessage(), e);
         }
     }
 
+    /**
+     * The FTP client returns an empty listing, without an exception, when the 
server refuses the data connection (PORT,
+     * EPRT, PASV, EPSV) or the LIST command, so an empty listing fails unless 
the server completed it. The replies 450
+     * and 550 still give an empty listing, as servers use them for a 
directory that is missing or empty.
+     */
+    private FTPFile[] checkListing(FTPFile[] files, String what) {
+        if (files != null && files.length > 0) {
+            return files;
+        }
+        int code = client.getReplyCode();
+        if (FTPReply.isPositiveCompletion(code) || code == 
FTPReply.FILE_ACTION_NOT_TAKEN
+                || code == FTPReply.FILE_UNAVAILABLE) {
+            return files;
+        }
+        String message = "Cannot list " + what;
+        if (!endpoint.getConfiguration().isPassiveMode()) {
+            message += ". Set passiveMode=true if the server or a firewall 
refuses data connections in active mode";
+        }
+        
clientActivityListener.onGeneralError(endpoint.getConfiguration().remoteServerInformation(),
 message);
+        throw new GenericFileOperationFailedException(code, 
client.getReplyString(), message);
+    }
+
     @Override
     public boolean sendNoop() throws GenericFileOperationFailedException {
         log.trace("sendNoOp");
diff --git 
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/integration/FtpConsumerActiveModeRefusedIT.java
 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/integration/FtpConsumerActiveModeRefusedIT.java
new file mode 100644
index 000000000000..8097de6c1506
--- /dev/null
+++ 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/integration/FtpConsumerActiveModeRefusedIT.java
@@ -0,0 +1,109 @@
+/*
+ * 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.file.remote.integration;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.file.GenericFileOperationFailedException;
+import org.apache.camel.component.file.remote.BaseServerTestSupport;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.infra.ftp.services.embedded.EmbeddedConfiguration;
+import org.apache.camel.test.infra.ftp.services.embedded.FtpEmbeddedService;
+import org.apache.ftpserver.DataConnectionConfigurationFactory;
+import org.apache.ftpserver.FtpServerFactory;
+import org.apache.ftpserver.listener.ListenerFactory;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * The server refuses active mode data connections (PORT), so the listing of 
the consumer in active mode fails, and the
+ * poll must fail too instead of finding an empty directory.
+ */
+public class FtpConsumerActiveModeRefusedIT extends BaseServerTestSupport {
+
+    @RegisterExtension
+    public FtpEmbeddedService service = new FtpEmbeddedService() {
+        @Override
+        protected FtpServerFactory 
createFtpServerFactory(EmbeddedConfiguration embeddedConfiguration) {
+            FtpServerFactory factory = 
super.createFtpServerFactory(embeddedConfiguration);
+            DataConnectionConfigurationFactory dataConnection = new 
DataConnectionConfigurationFactory();
+            dataConnection.setActiveEnabled(false);
+            ListenerFactory listener = new 
ListenerFactory(factory.getListener(DEFAULT_LISTENER));
+            
listener.setDataConnectionConfiguration(dataConnection.createDataConnectionConfiguration());
+            factory.addListener(DEFAULT_LISTENER, listener.createListener());
+            return factory;
+        }
+    };
+
+    @Test
+    public void testRefusedListingFailsThePoll() throws Exception {
+        writeFileOnServer();
+
+        MockEndpoint error = getMockEndpoint("mock:error");
+        error.expectedMinimumMessageCount(1);
+
+        context.getRouteController().startRoute("active");
+
+        MockEndpoint.assertIsSatisfied(context);
+        GenericFileOperationFailedException cause = 
error.getReceivedExchanges().get(0)
+                .getProperty(Exchange.EXCEPTION_CAUGHT, 
GenericFileOperationFailedException.class);
+        assertNotNull(cause, "The poll should fail as the server refused the 
listing");
+        assertEquals(501, cause.getCode());
+        assertTrue(cause.getMessage().contains("Cannot list the current 
directory"), cause.getMessage());
+        assertTrue(cause.getMessage().contains("passiveMode=true"), 
cause.getMessage());
+        assertEquals(0, getMockEndpoint("mock:result").getReceivedCounter());
+    }
+
+    @Test
+    public void testPassiveModeConsumesTheFile() throws Exception {
+        writeFileOnServer();
+
+        MockEndpoint result = getMockEndpoint("mock:result");
+        result.expectedBodiesReceived("Hello World");
+        getMockEndpoint("mock:error").expectedMessageCount(0);
+
+        context.getRouteController().startRoute("passive");
+
+        MockEndpoint.assertIsSatisfied(context);
+    }
+
+    private void writeFileOnServer() {
+        template.sendBodyAndHeader("file://" + service.getFtpRootDir() + 
"/refused", "Hello World", Exchange.FILE_NAME,
+                "hello.txt");
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                
onException(GenericFileOperationFailedException.class).handled(true).to("mock:error");
+
+                String url = 
"ftp://admin@localhost:{{ftp.server.port}}/refused?password=admin&delay=100";
+                             + "&bridgeErrorHandler=true";
+                from(url).routeId("active").autoStartup(false)
+                        .convertBodyTo(String.class).to("mock:result");
+                from(url + 
"&passiveMode=true").routeId("passive").autoStartup(false)
+                        .convertBodyTo(String.class).to("mock:result");
+            }
+        };
+    }
+}
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index 37f01a4d2be3..028d217aef2c 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -359,6 +359,15 @@ the `include` or `antInclude` option.
 
 See the xref:components::sftp-component.adoc[SFTP component].
 
+=== camel-ftp - a directory listing the server refuses fails the poll
+
+The `ftp` and `ftps` consumers now fail the poll when the server refuses the 
listing of a directory, for example the
+`PORT` command of active mode (the default) when the server or a firewall only 
allows passive mode. The
+`GenericFileOperationFailedException` holds the reply code and text of the 
server, and is logged like any failed poll.
+Before, the refused listing was taken as an empty directory: the polls found 
no files and logged nothing. The replies
+450 and 550 (a directory that is missing or empty) still give an empty 
listing. If such a consumer now fails, set
+`passiveMode=true`. See the xref:components::ftp-component.adoc[FTP component].
+
 === camel-kafka - exactlyOnce: a rejected send that a doCatch catches skips 
the record
 
 With `exactlyOnce`, a Kafka send through the source producer after an earlier 
send failed in the same transaction is

Reply via email to