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 2017/11/01 09:19:29 UTC

[camel] 02/03: CAMEL-11971: SFTP consumer to support useList

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

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

commit 8b193694566b7f41115408bc90958ca860c250ad
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Nov 1 09:51:11 2017 +0100

    CAMEL-11971: SFTP consumer to support useList
---
 .../file/remote/RemoteFileConfiguration.java       |  2 -
 .../camel/component/file/remote/SftpConsumer.java  | 22 ++++++--
 .../file/remote/SftpRemoteFileSingle.java          | 56 ++++++++++++++++++++
 .../file/remote/sftp/SftpUseListFalseTest.java     | 59 ++++++++++++++++++++++
 4 files changed, 133 insertions(+), 6 deletions(-)

diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConfiguration.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConfiguration.java
index 4ba1c6a..31883a7 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConfiguration.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConfiguration.java
@@ -359,8 +359,6 @@ public abstract class RemoteFileConfiguration extends GenericFileConfiguration {
      * Notice when using this option, then the specific file to download does <b>not</b>
      * include meta-data information such as file size, timestamp, permissions etc, because
      * those information is only possible to retrieve when LIST command is in use.
-     *
-     * This option is not available for SFTP.
      */
     public void setUseList(boolean useList) {
         this.useList = useList;
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 e42ada3..8eb3e23 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
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.component.file.remote;
 
+import java.util.ArrayList;
 import java.util.List;
 
 import com.jcraft.jsch.ChannelSftp;
@@ -28,6 +29,7 @@ import org.apache.camel.component.file.GenericFileOperationFailedException;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.URISupport;
+import org.apache.commons.net.ftp.FTPFile;
 
 /**
  * Secure FTP consumer
@@ -114,12 +116,24 @@ public class SftpConsumer extends RemoteFileConsumer<SftpRemoteFile> {
         }
 
         log.trace("Polling directory: {}", dir);
-        List<SftpRemoteFile> files;
-        if (isStepwise()) {
-            files = operations.listFiles();
+        List<SftpRemoteFile> files = null;
+        if (isUseList()) {
+            if (isStepwise()) {
+                files = operations.listFiles();
+            } else {
+                files = operations.listFiles(dir);
+            }
         } else {
-            files = operations.listFiles(dir);
+            // we cannot use the LIST command(s) so we can only poll a named file
+            // so created a pseudo file with that name
+            fileExpressionResult = evaluateFileExpression();
+            if (fileExpressionResult != null) {
+                SftpRemoteFile file = new SftpRemoteFileSingle(fileExpressionResult);
+                files = new ArrayList<SftpRemoteFile>(1);
+                files.add(file);
+            }
         }
+
         if (files == null || files.isEmpty()) {
             // no files in this directory to poll
             log.trace("No files found in directory: {}", dir);
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpRemoteFileSingle.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpRemoteFileSingle.java
new file mode 100644
index 0000000..8d85ff8
--- /dev/null
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpRemoteFileSingle.java
@@ -0,0 +1,56 @@
+/**
+ * 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;
+
+public class SftpRemoteFileSingle implements SftpRemoteFile<Object> {
+
+    private final String filename;
+
+    public SftpRemoteFileSingle(String filename) {
+        this.filename = filename;
+    }
+
+    @Override
+    public Object getRemoteFile() {
+        return null;
+    }
+
+    @Override
+    public String getFilename() {
+        return filename;
+    }
+
+    @Override
+    public String getLongname() {
+        return null;
+    }
+
+    @Override
+    public boolean isDirectory() {
+        return false;
+    }
+
+    @Override
+    public long getFileLength() {
+        return -1;
+    }
+
+    @Override
+    public long getLastModified() {
+        return -1;
+    }
+}
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpUseListFalseTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpUseListFalseTest.java
new file mode 100644
index 0000000..6c797be
--- /dev/null
+++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpUseListFalseTest.java
@@ -0,0 +1,59 @@
+/**
+ * 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.Test;
+
+public class SftpUseListFalseTest extends SftpServerTestSupport {
+
+    @Test
+    public void testSftpUseListFalse() throws Exception {
+        if (!canTest()) {
+            return;
+        }
+
+        String expected = "Hello World";
+
+        // create file using regular file
+        template.sendBodyAndHeader("file://" + FTP_ROOT_DIR, expected, Exchange.FILE_NAME, "report.txt");
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedHeaderReceived(Exchange.FILE_NAME, "report.txt");
+        mock.expectedBodiesReceived(expected);
+        
+        context.startRoute("foo");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("sftp://localhost:" + getPort() + "/" + FTP_ROOT_DIR
+                    + "?username=admin&password=admin&delay=10s&disconnect=true&stepwise=false&useList=false&fileName=report.txt&delete=true")
+                    .routeId("foo").noAutoStartup()
+                    .to("mock:result");
+            }
+        };
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@camel.apache.org" <co...@camel.apache.org>.