camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggrzy...@apache.org
Subject camel git commit: [CAMEL-8916] handle autoCreate=true for ftp/ftps/sftp consumers
Date Mon, 29 Jun 2015 10:19:24 GMT
Repository: camel
Updated Branches:
  refs/heads/master 364161d1e -> 162f3a5cf


[CAMEL-8916] handle autoCreate=true for ftp/ftps/sftp consumers


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/162f3a5c
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/162f3a5c
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/162f3a5c

Branch: refs/heads/master
Commit: 162f3a5cfdfe713083b2d9b3c105ae6c0ad08d94
Parents: 364161d
Author: Grzegorz Grzybek <gr.grzybek@gmail.com>
Authored: Mon Jun 29 12:18:55 2015 +0200
Committer: Grzegorz Grzybek <gr.grzybek@gmail.com>
Committed: Mon Jun 29 12:18:55 2015 +0200

----------------------------------------------------------------------
 .../component/file/remote/FtpConsumer.java      | 26 +++++++
 .../component/file/remote/SftpConsumer.java     | 27 +++++++
 .../file/remote/FtpConsumerAutoCreateTest.java  | 70 ++++++++++++++++++
 ...ConsumerThrowExceptionOnLoginFailedTest.java |  2 +-
 .../remote/sftp/SftpConsumerAutoCreateTest.java | 78 ++++++++++++++++++++
 5 files changed, 202 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/162f3a5c/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
----------------------------------------------------------------------
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 f591caa..110a30f 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
@@ -41,6 +41,32 @@ public class FtpConsumer extends RemoteFileConsumer<FTPFile> {
     }
 
     @Override
+    protected void doStart() throws Exception {
+        // turn off scheduler first, so autoCreate is handled before scheduler starts
+        boolean startScheduler = isStartScheduler();
+        setStartScheduler(false);
+        try {
+            super.doStart();
+            if (endpoint.isAutoCreate()) {
+                log.debug("Auto creating \"" + endpoint.getConfiguration().getDirectory());
+                try {
+                    connectIfNecessary();
+                    operations.buildDirectory(endpoint.getConfiguration().getDirectory(),
true);
+                } catch (GenericFileOperationFailedException e) {
+                    if (getEndpoint().getConfiguration().isThrowExceptionOnConnectFailed())
{
+                        throw e;
+                    }
+                }
+            }
+        } finally {
+            if (startScheduler) {
+                setStartScheduler(true);
+                startScheduler();
+            }
+        }
+    }
+
+    @Override
     protected boolean pollDirectory(String fileName, List<GenericFile<FTPFile>>
fileList, int depth) {
         String currentDir = null;
         if (isStepwise()) {

http://git-wip-us.apache.org/repos/asf/camel/blob/162f3a5c/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
----------------------------------------------------------------------
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 29c7532..0c269fc 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
@@ -23,6 +23,7 @@ import com.jcraft.jsch.SftpException;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.component.file.GenericFile;
+import org.apache.camel.component.file.GenericFileOperationFailedException;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.ObjectHelper;
 
@@ -39,6 +40,32 @@ public class SftpConsumer extends RemoteFileConsumer<ChannelSftp.LsEntry>
{
     }
 
     @Override
+    protected void doStart() throws Exception {
+        // turn off scheduler first, so autoCreate is handled before scheduler starts
+        boolean startScheduler = isStartScheduler();
+        setStartScheduler(false);
+        try {
+            super.doStart();
+            if (endpoint.isAutoCreate()) {
+                log.debug("Auto creating \"" + endpoint.getConfiguration().getDirectory());
+                try {
+                    connectIfNecessary();
+                    operations.buildDirectory(endpoint.getConfiguration().getDirectory(),
true);
+                } catch (GenericFileOperationFailedException e) {
+                    if (getEndpoint().getConfiguration().isThrowExceptionOnConnectFailed())
{
+                        throw e;
+                    }
+                }
+            }
+        } finally {
+            if (startScheduler) {
+                setStartScheduler(true);
+                startScheduler();
+            }
+        }
+    }
+
+    @Override
     protected boolean pollDirectory(String fileName, List<GenericFile<ChannelSftp.LsEntry>>
fileList, int depth) {
         String currentDir = null;
         if (isStepwise()) {

http://git-wip-us.apache.org/repos/asf/camel/blob/162f3a5c/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerAutoCreateTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerAutoCreateTest.java
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerAutoCreateTest.java
new file mode 100644
index 0000000..79783c1
--- /dev/null
+++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerAutoCreateTest.java
@@ -0,0 +1,70 @@
+/**
+ * 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 java.io.File;
+
+import org.apache.camel.component.file.GenericFileOperationFailedException;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+
+/**
+ * @version 
+ */
+public class FtpConsumerAutoCreateTest extends FtpServerTestSupport {
+
+    protected String getFtpUrl() {
+        return "ftp://admin@localhost:" + getPort() + "///foo/bar/baz/xxx?password=admin&autoCreate=true";
+    }
+
+    @Override
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+    }
+
+    @Test
+    public void testAutoCreate() throws Exception {
+        FtpEndpoint endpoint = (FtpEndpoint) this.getMandatoryEndpoint(getFtpUrl());
+        endpoint.start();
+        endpoint.getExchanges();
+        assertTrue(new File("target/res/home/foo/bar/baz/xxx").exists());
+        // producer should create necessary subdirs
+        sendFile(getFtpUrl(), "Hello World", "sub1/sub2/hello.txt");
+        assertTrue(new File("target/res/home/foo/bar/baz/xxx/sub1/sub2").exists());
+
+        // to see if another connect causes problems with autoCreate=true
+        endpoint.stop();
+        endpoint.start();
+        endpoint.getExchanges();
+    }
+
+    @Test
+    public void testNoAutoCreate() throws Exception {
+        FtpEndpoint endpoint = (FtpEndpoint) this.getMandatoryEndpoint(getFtpUrl() + "&autoCreate=false");
+        endpoint.start();
+        try {
+            endpoint.getExchanges();
+            fail("Should fail with 550 No such directory.");
+        } catch (GenericFileOperationFailedException e) {
+            assertThat(e.getCode(), equalTo(550));
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/162f3a5c/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerThrowExceptionOnLoginFailedTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerThrowExceptionOnLoginFailedTest.java
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerThrowExceptionOnLoginFailedTest.java
index b6f7147..d910b7c 100644
--- a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerThrowExceptionOnLoginFailedTest.java
+++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerThrowExceptionOnLoginFailedTest.java
@@ -37,7 +37,7 @@ public class FtpConsumerThrowExceptionOnLoginFailedTest extends FtpServerTestSup
 
     private String getFtpUrl() {
         return "ftp://dummy@localhost:" + getPort() + "/badlogin?password=cantremember"
-                + "&throwExceptionOnConnectFailed=true&maximumReconnectAttempts=0&pollStrategy=#myPoll";
+                + "&throwExceptionOnConnectFailed=true&maximumReconnectAttempts=0&pollStrategy=#myPoll&autoCreate=false";
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/162f3a5c/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpConsumerAutoCreateTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpConsumerAutoCreateTest.java
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpConsumerAutoCreateTest.java
new file mode 100644
index 0000000..11386c6
--- /dev/null
+++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpConsumerAutoCreateTest.java
@@ -0,0 +1,78 @@
+/**
+ * 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 org.apache.camel.Exchange;
+import org.apache.camel.component.file.GenericFileOperationFailedException;
+import org.apache.camel.component.file.remote.FtpEndpoint;
+import org.apache.camel.component.file.remote.FtpServerTestSupport;
+import org.apache.camel.component.file.remote.FtpsEndpoint;
+import org.apache.camel.component.file.remote.SftpEndpoint;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+
+/**
+ * @version 
+ */
+public class SftpConsumerAutoCreateTest extends SftpServerTestSupport {
+
+    protected String getFtpUrl() {
+        return "sftp://admin@localhost:" + getPort() + "/" + FTP_ROOT_DIR + "/foo/bar/baz/xxx?password=admin&autoCreate=true";
+    }
+
+    @Override
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+    }
+
+    @Test
+    public void testAutoCreate() throws Exception {
+        if (!canTest()) {
+            return;
+        }
+        SftpEndpoint endpoint = (SftpEndpoint) this.getMandatoryEndpoint(getFtpUrl());
+        endpoint.start();
+        endpoint.getExchanges();
+        assertTrue(new File(FTP_ROOT_DIR + "/foo/bar/baz/xxx").exists());
+        // producer should create necessary subdirs
+        template.sendBodyAndHeader(getFtpUrl(), "Hello World", Exchange.FILE_NAME, "sub1/sub2/hello.txt");
+        assertTrue(new File(FTP_ROOT_DIR + "/foo/bar/baz/xxx/sub1/sub2").exists());
+
+        // to see if another connect causes problems with autoCreate=true
+        endpoint.stop();
+        endpoint.start();
+        endpoint.getExchanges();
+    }
+
+    @Test
+    public void testNoAutoCreate() throws Exception {
+        SftpEndpoint endpoint = (SftpEndpoint) this.getMandatoryEndpoint(getFtpUrl() + "&autoCreate=false");
+        endpoint.start();
+        try {
+            endpoint.getExchanges();
+            fail("Should fail with 550 No such directory.");
+        } catch (GenericFileOperationFailedException ignored) {
+            // ignore
+        }
+    }
+
+}


Mime
View raw message