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

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

commit 9e322045f54d686f36263bcf2401f75f8b61e86c
Author: Roberto Flores (Beto) <49126609+betoflow...@users.noreply.github.com>
AuthorDate: Tue May 21 02:54:07 2019 -0500

    CAMEL-13400 Camel FTP Cannot list directory with 'File not found' 
prepending additional '/' in front of directory automatically (#2931)
    
    * CAMEL-13400 Camel FTP Cannot list directory with 'File not found' 
prepending additional '/' in front of directory automatically
    
    * CAMEL-13400 Fix for SFTP
---
 .../camel/component/file/remote/FtpConsumer.java   |  2 +-
 .../camel/component/file/remote/SftpConsumer.java  |  2 +-
 .../FromFtpRecursiveNotStepwiseNoBasePath.java     | 64 ++++++++++++++++++++
 .../FromSftpRecursiveNotStepwiseNoBasePath.java    | 69 ++++++++++++++++++++++
 .../file/remote/sftp/SftpServerTestSupport.java    |  6 ++
 5 files changed, 141 insertions(+), 2 deletions(-)

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 3d3c384..882cc71 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
@@ -186,7 +186,7 @@ public class FtpConsumer extends 
RemoteFileConsumer<FTPFile> {
                 if (endpoint.isRecursive() && depth < endpoint.getMaxDepth() 
&& isValidFile(remote, true, files)) {
                     // recursive scan and add the sub files and folders
                     String subDirectory = file.getName();
-                    String path = absolutePath + "/" + subDirectory;
+                    String path = ObjectHelper.isNotEmpty(absolutePath) ? 
absolutePath + "/" + subDirectory : subDirectory;
                     boolean canPollMore = pollSubDirectory(path, subDirectory, 
fileList, depth);
                     if (!canPollMore) {
                         return false;
diff --git 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
index 8b2fb33..ad110d1 100644
--- 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
+++ 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
@@ -173,7 +173,7 @@ public class SftpConsumer extends 
RemoteFileConsumer<SftpRemoteFile> {
                 if (endpoint.isRecursive() && depth < endpoint.getMaxDepth() 
&& isValidFile(remote, true, files)) {
                     // recursive scan and add the sub files and folders
                     String subDirectory = file.getFilename();
-                    String path = absolutePath + "/" + subDirectory;
+                    String path = ObjectHelper.isNotEmpty(absolutePath) ? 
absolutePath + "/" + subDirectory : subDirectory;
                     boolean canPollMore = pollSubDirectory(path, subDirectory, 
fileList, depth);
                     if (!canPollMore) {
                         return false;
diff --git 
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRecursiveNotStepwiseNoBasePath.java
 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRecursiveNotStepwiseNoBasePath.java
new file mode 100644
index 0000000..6112d17
--- /dev/null
+++ 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRecursiveNotStepwiseNoBasePath.java
@@ -0,0 +1,64 @@
+/*
+ * 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;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Before;
+import org.junit.Test;
+
+public class FromFtpRecursiveNotStepwiseNoBasePath extends 
FtpServerTestSupport {
+
+  protected String getFtpUrl() {
+    return "ftp://admin@localhost:"; + getPort() + 
"?password=admin&initialDelay=3000&stepwise=false"
+        + "&recursive=true";
+  }
+
+  @Override
+  @Before
+  public void setUp() throws Exception {
+    super.setUp();
+    prepareFtpServer();
+  }
+
+  @Test
+  public void testRecursiveNotStepwiseNoBasePath() throws Exception {
+    //CAMEL-13400
+    MockEndpoint mock = getMockEndpoint("mock:result");
+    mock.expectedBodiesReceivedInAnyOrder("Bye World", "Hello World", "Goodday 
World");
+    assertMockEndpointsSatisfied();
+  }
+
+  @Override
+  protected RouteBuilder createRouteBuilder() throws Exception {
+    return new RouteBuilder() {
+      @Override
+      public void configure() throws Exception {
+        from(getFtpUrl())
+            .convertBodyTo(String.class)
+            .to("log:ftp")
+            .to("mock:result");
+      }
+    };
+  }
+
+  private void prepareFtpServer() throws Exception {
+    sendFile(getFtpUrl(), "Bye World", "bye.txt");
+    sendFile(getFtpUrl(), "Hello World", "sub/hello.txt");
+    sendFile(getFtpUrl(), "Goodday World", "sub/sub2/godday.txt");
+  }
+}
diff --git 
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/FromSftpRecursiveNotStepwiseNoBasePath.java
 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/FromSftpRecursiveNotStepwiseNoBasePath.java
new file mode 100644
index 0000000..001a1c9
--- /dev/null
+++ 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/FromSftpRecursiveNotStepwiseNoBasePath.java
@@ -0,0 +1,69 @@
+/*
+ * 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.sftp;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Before;
+import org.junit.Test;
+
+public class FromSftpRecursiveNotStepwiseNoBasePath extends 
SftpServerTestSupport {
+
+  protected String getSftpUrl() {
+    return "sftp://admin@localhost:"; + getPort() + 
"?password=admin&initialDelay=3000&stepwise=false"
+        + "&recursive=true";
+  }
+
+  @Override
+  @Before
+  public void setUp() throws Exception {
+    rootDirMode = true;
+    super.setUp();
+    prepareFtpServer();
+  }
+
+  @Test
+  public void testRecursiveNotStepwiseNoBasePath() throws Exception {
+    //CAMEL-13400
+    MockEndpoint mock = getMockEndpoint("mock:result");
+    mock.expectedBodiesReceivedInAnyOrder("Bye World", "Hello World", "Goodday 
World");
+    assertMockEndpointsSatisfied();
+  }
+
+  @Override
+  protected RouteBuilder createRouteBuilder() throws Exception {
+    return new RouteBuilder() {
+      @Override
+      public void configure() throws Exception {
+        from(getSftpUrl())
+            .convertBodyTo(String.class)
+            .to("mock:result");
+      }
+    };
+  }
+
+  private void prepareFtpServer() throws Exception {
+    sendFile("Bye World", "bye.txt");
+    sendFile("Hello World", "sub/hello.txt");
+    sendFile("Goodday World", "sub/sub2/godday.txt");
+  }
+
+  public void sendFile(Object body, String fileName) {
+    template.sendBodyAndHeader("file://" + FTP_ROOT_DIR, body, 
Exchange.FILE_NAME, fileName);
+  }
+}
diff --git 
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpServerTestSupport.java
 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpServerTestSupport.java
index 9a7370a..f4dbf12 100644
--- 
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpServerTestSupport.java
+++ 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpServerTestSupport.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.file.remote.sftp;
 
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.FileSystems;
 import java.nio.file.Paths;
 import java.security.NoSuchAlgorithmException;
 import java.util.Collections;
@@ -26,6 +27,7 @@ import java.util.List;
 import org.apache.camel.component.file.remote.BaseServerTestSupport;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.commons.io.FileUtils;
+import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;
 import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
 import org.apache.sshd.common.session.helpers.AbstractSession;
 import org.apache.sshd.server.SshServer;
@@ -43,6 +45,7 @@ public class SftpServerTestSupport extends 
BaseServerTestSupport {
     protected SshServer sshd;
     protected boolean canTest;
     protected String oldUserHome;
+    protected boolean rootDirMode = false;
 
     @Override
     @Before
@@ -76,6 +79,9 @@ public class SftpServerTestSupport extends 
BaseServerTestSupport {
             sshd.setCommandFactory(new ScpCommandFactory());
             sshd.setPasswordAuthenticator((username, password, session) -> 
true);
             sshd.setPublickeyAuthenticator((username, password, session) -> 
true);
+            if (rootDirMode) {
+              sshd.setFileSystemFactory(new 
VirtualFileSystemFactory(FileSystems.getDefault().getPath(System.getProperty("user.dir")
 + "/target/res")));
+            }
             sshd.start();
         } catch (Exception e) {
             // ignore if algorithm is not on the OS

Reply via email to