You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by gg...@apache.org on 2015/06/08 11:49:42 UTC

camel git commit: [CAMEL-8844] Convert absolute FTP directories in endpoint URIs to relative with WARN message

Repository: camel
Updated Branches:
  refs/heads/master 85f9361f1 -> ca6d74205


[CAMEL-8844] Convert absolute FTP directories in endpoint URIs to relative with WARN message


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

Branch: refs/heads/master
Commit: ca6d74205815269b7b3caf32ca57cb73c1a7299a
Parents: 85f9361
Author: Grzegorz Grzybek <gr...@gmail.com>
Authored: Mon Jun 8 11:38:07 2015 +0200
Committer: Grzegorz Grzybek <gr...@gmail.com>
Committed: Mon Jun 8 11:38:55 2015 +0200

----------------------------------------------------------------------
 .../component/file/remote/FtpComponent.java     |  2 +
 .../camel/component/file/remote/FtpUtils.java   | 25 +++++++
 .../component/file/remote/FtpsComponent.java    |  2 +
 .../component/file/remote/SftpComponent.java    |  2 +
 .../remote/FtpEndpointURISanitizedTest.java     | 10 ++-
 ...tpSimpleConsumeStreamingPartialReadTest.java |  4 +-
 .../remote/FtpSimpleConsumeStreamingTest.java   |  4 +-
 ...leConsumeStreamingWithMultipleFilesTest.java |  4 +-
 .../file/remote/UriConfigurationTest.java       | 23 ++++---
 ...ftpSimpleConsumeAbsoluteNotStepwiseTest.java | 38 -----------
 .../sftp/SftpSimpleConsumeAbsoluteTest.java     | 72 --------------------
 11 files changed, 55 insertions(+), 131 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ca6d7420/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpComponent.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpComponent.java
index 9aeee72..ddba282 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpComponent.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpComponent.java
@@ -46,6 +46,8 @@ public class FtpComponent extends RemoteFileComponent<FTPFile> {
         // must pass on baseUri to the configuration (see above)
         FtpConfiguration config = new FtpConfiguration(new URI(baseUri));
 
+        FtpUtils.ensureRelativeFtpDirectory(this, config);
+
         FtpEndpoint<FTPFile> answer = new FtpEndpoint<FTPFile>(uri, this, config);
         extractAndSetFtpClientConfigParameters(parameters, answer);
         extractAndSetFtpClientParameters(parameters, answer);

http://git-wip-us.apache.org/repos/asf/camel/blob/ca6d7420/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpUtils.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpUtils.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpUtils.java
index 18cdb44..e190243 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpUtils.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpUtils.java
@@ -20,13 +20,18 @@ import java.io.File;
 import java.util.Iterator;
 import java.util.Stack;
 
+import org.apache.camel.Component;
 import org.apache.camel.util.FileUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Various FTP utils.
  */
 public final class FtpUtils {
 
+    private static final Logger LOG = LoggerFactory.getLogger(FtpUtils.class);
+
     private FtpUtils() {
     }
 
@@ -105,4 +110,24 @@ public final class FtpUtils {
         return sb.toString();
     }
 
+    /**
+     * Checks whether directory used in ftp/ftps/sftp endpoint URI is relative.
+     * Absolute path will be converted to relative path and a WARN will be printed.
+     * @see <a href="http://camel.apache.org/ftp2.html">FTP/SFTP/FTPS Component</a>
+     * @param ftpComponent
+     * @param configuration
+     */
+    public static void ensureRelativeFtpDirectory(Component ftpComponent, RemoteFileConfiguration configuration) {
+        if (FileUtil.hasLeadingSeparator(configuration.getDirectoryName())) {
+            String relativePath = FileUtil.stripLeadingSeparator(configuration.getDirectoryName());
+            LOG.warn(String.format("%s doesn't support absolute paths, \"%s\" will be converted to \"%s\". " +
+                            "After Camel 2.16, absolute paths will be invalid.",
+                    ftpComponent.getClass().getSimpleName(),
+                    configuration.getDirectoryName(),
+                    relativePath));
+            configuration.setDirectory(relativePath);
+            configuration.setDirectoryName(relativePath);
+        }
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/ca6d7420/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpsComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpsComponent.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpsComponent.java
index 428ba7d..31f3c99 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpsComponent.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpsComponent.java
@@ -50,6 +50,8 @@ public class FtpsComponent extends FtpComponent {
         // must pass on baseUri to the configuration (see above)
         FtpsConfiguration config = new FtpsConfiguration(new URI(baseUri));
 
+        FtpUtils.ensureRelativeFtpDirectory(this, config);
+
         FtpsEndpoint endpoint = new FtpsEndpoint(uri, this, config);
         extractAndSetFtpClientKeyStoreParameters(parameters, endpoint);
         extractAndSetFtpClientTrustStoreParameters(parameters, endpoint);

http://git-wip-us.apache.org/repos/asf/camel/blob/ca6d7420/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpComponent.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpComponent.java
index 82c1247..625bc21 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpComponent.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpComponent.java
@@ -51,6 +51,8 @@ public class SftpComponent extends RemoteFileComponent<ChannelSftp.LsEntry> {
         // customize its own version
         SftpConfiguration config = new SftpConfiguration(new URI(baseUri));
 
+        FtpUtils.ensureRelativeFtpDirectory(this, config);
+
         return new SftpEndpoint(uri, this, config);
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/ca6d7420/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpEndpointURISanitizedTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpEndpointURISanitizedTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpEndpointURISanitizedTest.java
index 69b14fe..52455f2 100644
--- a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpEndpointURISanitizedTest.java
+++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpEndpointURISanitizedTest.java
@@ -22,6 +22,8 @@ import org.apache.camel.Endpoint;
 import org.apache.camel.Producer;
 import org.junit.Test;
 
+import static org.hamcrest.CoreMatchers.equalTo;
+
 /**
  * Test to ensure the FtpEndpoint URI is sanitized.
  */
@@ -30,7 +32,13 @@ public class FtpEndpointURISanitizedTest extends FtpServerTestSupport {
     private String password = "secret";
 
     protected String getFtpUrl() {
-        return "ftp://admin@localhost:" + getPort() + "///foo?password=" + password + "&delay=5000";
+        return "ftp://admin@localhost:" + getPort() + "/////foo?password=" + password + "&delay=5000";
+    }
+
+    @Test
+    public void testFtpDirectoryRelative() throws Exception {
+        Endpoint endpoint = context.getEndpoint(getFtpUrl());
+        assertThat(((FtpEndpoint) endpoint).getConfiguration().getDirectoryName(), equalTo("foo"));
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/camel/blob/ca6d7420/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeStreamingPartialReadTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeStreamingPartialReadTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeStreamingPartialReadTest.java
index 2de444e..de58772 100644
--- a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeStreamingPartialReadTest.java
+++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeStreamingPartialReadTest.java
@@ -68,10 +68,8 @@ public class FtpSimpleConsumeStreamingPartialReadTest extends FtpServerTestSuppo
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                // notice we use an absolute starting path: /tmp/mytemp
-                // - we must remember to use // slash because of the url separator
                 from("ftp://localhost:" + getPort()
-                         + "//tmp/mytemp?username=admin&password=admin&delay=10s&disconnect=true&streamDownload=true"
+                         + "/tmp/mytemp?username=admin&password=admin&delay=10s&disconnect=true&streamDownload=true"
                          + "&move=done&moveFailed=failed")
                     .routeId("foo").noAutoStartup()
                     .process(new Processor() {

http://git-wip-us.apache.org/repos/asf/camel/blob/ca6d7420/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeStreamingTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeStreamingTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeStreamingTest.java
index 8c0f92c..5b616e3 100644
--- a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeStreamingTest.java
+++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeStreamingTest.java
@@ -59,9 +59,7 @@ public class FtpSimpleConsumeStreamingTest extends FtpServerTestSupport {
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                // notice we use an absolute starting path: /tmp/mytemp
-                // - we must remember to use // slash because of the url separator
-                from("ftp://localhost:" + getPort() + "//tmp/mytemp?username=admin&password=admin&delay=10s&disconnect=true&streamDownload=true")
+                from("ftp://localhost:" + getPort() + "/tmp/mytemp?username=admin&password=admin&delay=10s&disconnect=true&streamDownload=true")
                     .routeId("foo").noAutoStartup()
                     .to("mock:result");
             }

http://git-wip-us.apache.org/repos/asf/camel/blob/ca6d7420/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeStreamingWithMultipleFilesTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeStreamingWithMultipleFilesTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeStreamingWithMultipleFilesTest.java
index d242935..e33d71e 100644
--- a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeStreamingWithMultipleFilesTest.java
+++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeStreamingWithMultipleFilesTest.java
@@ -64,9 +64,7 @@ public class FtpSimpleConsumeStreamingWithMultipleFilesTest extends FtpServerTes
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                // notice we use an absolute starting path: /tmp/mytemp
-                // - we must remember to use // slash because of the url separator
-                from("ftp://localhost:" + getPort() + "//tmp/mytemp?username=admin&password=admin&delay=10s&disconnect=true&streamDownload=true")
+                from("ftp://localhost:" + getPort() + "/tmp/mytemp?username=admin&password=admin&delay=10s&disconnect=true&streamDownload=true")
                     .routeId("foo").noAutoStartup()
                     .to("mock:result");
             }

http://git-wip-us.apache.org/repos/asf/camel/blob/ca6d7420/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/UriConfigurationTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/UriConfigurationTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/UriConfigurationTest.java
index 8d6783a..a2b3070 100644
--- a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/UriConfigurationTest.java
+++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/UriConfigurationTest.java
@@ -134,10 +134,10 @@ public class UriConfigurationTest extends CamelTestSupport {
         assertRemoteFileEndpointFile("ftp://hostname/foo", "foo");
         assertRemoteFileEndpointFile("ftp://hostname/", "");
         assertRemoteFileEndpointFile("ftp://hostname", "");
-        assertRemoteFileEndpointFile("ftp://hostname//", "/");
-        assertRemoteFileEndpointFile("ftp://hostname//foo/bar", "/foo/bar");
-        assertRemoteFileEndpointFile("ftp://hostname//foo/bar/", "/foo/bar/");
-        assertRemoteFileEndpointFile("sftp://user@hostname:123//foo/bar?password=secret", "/foo/bar");
+        assertRemoteFileEndpointFile("ftp://hostname//", "");
+        assertRemoteFileEndpointFile("ftp://hostname//foo/bar", "foo/bar");
+        assertRemoteFileEndpointFile("ftp://hostname//foo/bar/", "foo/bar/");
+        assertRemoteFileEndpointFile("sftp://user@hostname:123//foo/bar?password=secret", "foo/bar");
         assertRemoteFileEndpointFile("sftp://user@hostname:123?password=secret", "");
         assertRemoteFileEndpointFile("sftp://hostname/foo/bar", "foo/bar");
         assertRemoteFileEndpointFile("sftp://hostname/foo/bar/", "foo/bar/");
@@ -145,10 +145,10 @@ public class UriConfigurationTest extends CamelTestSupport {
         assertRemoteFileEndpointFile("sftp://hostname/foo", "foo");
         assertRemoteFileEndpointFile("sftp://hostname/", "");
         assertRemoteFileEndpointFile("sftp://hostname", "");
-        assertRemoteFileEndpointFile("sftp://hostname//", "/");
-        assertRemoteFileEndpointFile("sftp://hostname//foo/bar", "/foo/bar");
-        assertRemoteFileEndpointFile("sftp://hostname//foo/bar/", "/foo/bar/");
-        assertRemoteFileEndpointFile("ftps://user@hostname:123//foo/bar?password=secret", "/foo/bar");
+        assertRemoteFileEndpointFile("sftp://hostname//", "");
+        assertRemoteFileEndpointFile("sftp://hostname//foo/bar", "foo/bar");
+        assertRemoteFileEndpointFile("sftp://hostname//foo/bar/", "foo/bar/");
+        assertRemoteFileEndpointFile("ftps://user@hostname:123//foo/bar?password=secret", "foo/bar");
         assertRemoteFileEndpointFile("ftps://user@hostname:123?password=secret", "");
         assertRemoteFileEndpointFile("ftps://hostname/foo/bar", "foo/bar");
         assertRemoteFileEndpointFile("ftps://hostname/foo/bar/", "foo/bar/");
@@ -156,9 +156,10 @@ public class UriConfigurationTest extends CamelTestSupport {
         assertRemoteFileEndpointFile("ftps://hostname/foo", "foo");
         assertRemoteFileEndpointFile("ftps://hostname/", "");
         assertRemoteFileEndpointFile("ftps://hostname", "");
-        assertRemoteFileEndpointFile("ftps://hostname//", "/");
-        assertRemoteFileEndpointFile("ftps://hostname//foo/bar", "/foo/bar");
-        assertRemoteFileEndpointFile("ftps://hostname//foo/bar/", "/foo/bar/");
+        assertRemoteFileEndpointFile("ftps://hostname//", "");
+        assertRemoteFileEndpointFile("ftps://hostname//foo/bar", "foo/bar");
+        assertRemoteFileEndpointFile("ftps://hostname//foo/bar/", "foo/bar/");
+        assertRemoteFileEndpointFile("ftps://hostname//////foo/bar/", "foo/bar/");
     }
 
     private void assertRemoteFileEndpointFile(String endpointUri, String expectedFile) {

http://git-wip-us.apache.org/repos/asf/camel/blob/ca6d7420/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleConsumeAbsoluteNotStepwiseTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleConsumeAbsoluteNotStepwiseTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleConsumeAbsoluteNotStepwiseTest.java
deleted file mode 100644
index 537e634..0000000
--- a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleConsumeAbsoluteNotStepwiseTest.java
+++ /dev/null
@@ -1,38 +0,0 @@
-/**
- * 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.builder.RouteBuilder;
-
-/**
- * @version 
- */
-public class SftpSimpleConsumeAbsoluteNotStepwiseTest extends SftpSimpleConsumeAbsoluteTest {
-
-    @Override
-    protected RouteBuilder createRouteBuilder() throws Exception {
-        return new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                // we must remember to use // slash because of the url separator
-                from("sftp://localhost:" + getPort() + "//" + createAbsolutePath() + "?username=admin&password=admin&delay=10s&disconnect=true&stepwise=false")
-                    .routeId("foo").noAutoStartup()
-                    .to("mock:result");
-            }
-        };
-    }
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/ca6d7420/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleConsumeAbsoluteTest.java
----------------------------------------------------------------------
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleConsumeAbsoluteTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleConsumeAbsoluteTest.java
deleted file mode 100644
index 44bb898..0000000
--- a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleConsumeAbsoluteTest.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/**
- * 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;
-
-/**
- * @version 
- */
-public class SftpSimpleConsumeAbsoluteTest extends SftpServerTestSupport {
-
-    @Override
-    protected boolean canTest() {
-        // cannot test on windows
-        return super.canTest() && !isPlatform("windows");
-    }
-
-    protected static String createAbsolutePath() {
-        String answer = System.getProperty("user.dir") + "/" + FTP_ROOT_DIR + "/tmp/mytemp";
-        return answer;
-    }
-
-    @Test
-    public void testSftpSimpleConsumeAbsolute() throws Exception {
-        if (!canTest()) {
-            return;
-        }
-
-        String expected = "Hello World";
-
-        template.sendBodyAndHeader("file:" + createAbsolutePath(), expected, Exchange.FILE_NAME, "hello.txt");
-
-        MockEndpoint mock = getMockEndpoint("mock:result");
-        mock.expectedMessageCount(1);
-        mock.expectedBodiesReceived(expected);
-        mock.expectedHeaderReceived(Exchange.FILE_NAME_ONLY, "hello.txt");
-
-        context.startRoute("foo");
-
-        assertMockEndpointsSatisfied();
-    }
-
-    @Override
-    protected RouteBuilder createRouteBuilder() throws Exception {
-        return new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                // we must remember to use // slash because of the url separator
-                from("sftp://localhost:" + getPort() + "//" + createAbsolutePath()  + "?username=admin&password=admin&delay=10s&disconnect=true")
-                    .routeId("foo").noAutoStartup()
-                    .to("mock:result");
-            }
-        };
-    }
-}