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 2020/04/25 10:05:36 UTC

[camel] 01/02: CAMEL-14915: camel-ftp consumer should not try to create starting directory if its empty/home.

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 686fca65ab92a828bb8359608d15a80d0085c434
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Apr 25 11:43:23 2020 +0200

    CAMEL-14915: camel-ftp consumer should not try to create starting directory if its empty/home.
---
 .../camel/component/file/remote/FtpConsumer.java   |  9 ++--
 .../component/file/remote/RemoteFileConsumer.java  | 13 +++++
 .../camel/component/file/remote/SftpConsumer.java  |  9 ++--
 .../sftp/SftpSimpleConsumeNoStartingDirTest.java   | 63 ++++++++++++++++++++++
 4 files changed, 86 insertions(+), 8 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 ccc7a70..c278947 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
@@ -70,14 +70,15 @@ public class FtpConsumer extends RemoteFileConsumer<FTPFile> {
         setStartScheduler(false);
         try {
             super.doStart();
-            if (endpoint.isAutoCreate()) {
-                LOG.debug("Auto creating directory: {}", endpoint.getConfiguration().getDirectory());
+            if (endpoint.isAutoCreate() && hasStartingDirectory()) {
+                String dir = endpoint.getConfiguration().getDirectory();
+                LOG.debug("Auto creating directory: {}", dir);
                 try {
                     connectIfNecessary();
-                    operations.buildDirectory(endpoint.getConfiguration().getDirectory(), true);
+                    operations.buildDirectory(dir, true);
                 } catch (GenericFileOperationFailedException e) {
                     // log a WARN as we want to start the consumer.
-                    LOG.warn("Error auto creating directory: " + endpoint.getConfiguration().getDirectory() + " due " + e.getMessage() + ". This exception is ignored.", e);
+                    LOG.warn("Error auto creating directory: " + dir + " due " + e.getMessage() + ". This exception is ignored.", e);
                 }
             }
         } finally {
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConsumer.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConsumer.java
index 63e2635..aaca933 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConsumer.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConsumer.java
@@ -28,6 +28,7 @@ import org.apache.camel.component.file.GenericFileConsumer;
 import org.apache.camel.component.file.GenericFileOperationFailedException;
 import org.apache.camel.component.file.GenericFileProcessStrategy;
 import org.apache.camel.support.SynchronizationAdapter;
+import org.apache.camel.util.ObjectHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -150,6 +151,18 @@ public abstract class RemoteFileConsumer<T> extends GenericFileConsumer<T> {
         return getEndpoint().isDownload();
     }
 
+    /**
+     * Whether there is a starting directory configured.
+     */
+    protected boolean hasStartingDirectory() {
+        String dir = endpoint.getConfiguration().getDirectory();
+        if (ObjectHelper.isEmpty(dir)) {
+            return false;
+        }
+        // should not be a empty separator
+        return !dir.equals("/") && !dir.equals("\\");
+    }
+
     @Override
     protected void doStop() throws Exception {
         super.doStop();
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 e2a3c8c..58529f8 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
@@ -60,14 +60,15 @@ public class SftpConsumer extends RemoteFileConsumer<SftpRemoteFile> {
         setStartScheduler(false);
         try {
             super.doStart();
-            if (endpoint.isAutoCreate()) {
-                LOG.debug("Auto creating directory: {}", endpoint.getConfiguration().getDirectory());
+            if (endpoint.isAutoCreate() && hasStartingDirectory()) {
+                String dir = endpoint.getConfiguration().getDirectory();
+                LOG.debug("Auto creating directory: {}", dir);
                 try {
                     connectIfNecessary();
-                    operations.buildDirectory(endpoint.getConfiguration().getDirectory(), true);
+                    operations.buildDirectory(dir, true);
                 } catch (GenericFileOperationFailedException e) {
                     // log a WARN as we want to start the consumer.
-                    LOG.warn("Error auto creating directory: " + endpoint.getConfiguration().getDirectory() + " due " + e.getMessage() + ". This exception is ignored.", e);
+                    LOG.warn("Error auto creating directory: " + dir + " due " + e.getMessage() + ". This exception is ignored.", e);
                 }
             }
         } finally {
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleConsumeNoStartingDirTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleConsumeNoStartingDirTest.java
new file mode 100644
index 0000000..cfc6d22
--- /dev/null
+++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleConsumeNoStartingDirTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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 java.io.File;
+import java.io.FileOutputStream;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.util.FileUtil;
+import org.apache.camel.util.IOHelper;
+import org.junit.jupiter.api.Test;
+
+public class SftpSimpleConsumeNoStartingDirTest extends SftpServerTestSupport {
+
+    @Test
+    public void testSftpSimpleConsume() throws Exception {
+        if (!canTest()) {
+            return;
+        }
+
+        // create files using regular file
+        File file = new File("a.txt");
+        FileOutputStream fos = new FileOutputStream(file, false);
+        fos.write("ABC".getBytes());
+        fos.close();
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+
+        context.getRouteController().startRoute("foo");
+
+        assertMockEndpointsSatisfied();
+
+        FileUtil.deleteFile(file);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("sftp://localhost:" + getPort() + "/" + "?fileName=a.txt&username=admin&password=admin&delay=10000&disconnect=true").routeId("foo")
+                    .noAutoStartup().to("log:result", "mock:result");
+            }
+        };
+    }
+}