Repository: camel
Updated Branches:
  refs/heads/master 6d9e2189a -> 37a72e5fc


CAMEL-11754 - add option to extract file name if ftp client directory
parser ends up with absolute paths


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

Branch: refs/heads/master
Commit: ffdec79302dc3ffbf6b085f9bcf2d8315730fde3
Parents: 2df6d5b
Author: onders86 <ondersez...@gmail.com>
Authored: Thu Sep 21 16:24:30 2017 +0300
Committer: onders86 <ondersez...@gmail.com>
Committed: Fri Sep 22 14:08:51 2017 +0300

----------------------------------------------------------------------
 .../component/file/remote/FtpConfiguration.java | 17 ++++++
 .../component/file/remote/FtpConsumer.java      |  6 +-
 .../camel/component/file/remote/FtpUtils.java   |  9 +++
 ...oryParseWithAbsoluteDepthNoStepwiseTest.java | 64 ++++++++++++++++++++
 ...UtilsExtractDirNameFromAbsolutePathTest.java | 47 ++++++++++++++
 5 files changed, 142 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ffdec793/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConfiguration.java
----------------------------------------------------------------------
diff --git 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConfiguration.java
 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConfiguration.java
index f8492f0..41708cb 100644
--- 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConfiguration.java
+++ 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConfiguration.java
@@ -35,6 +35,8 @@ public class FtpConfiguration extends RemoteFileConfiguration 
{
     private String activePortRange;
     @UriParam(label = "producer,advanced")
     private String chmod;
+    @UriParam(label = "consumer,advanced")
+    private boolean handleDirectoryParserAbsoluteResult;
 
     public FtpConfiguration() {
         setProtocol("ftp");
@@ -83,4 +85,19 @@ public class FtpConfiguration extends 
RemoteFileConfiguration {
     public String getChmod() {
         return chmod;
     }
+
+    public boolean isHandleDirectoryParserAbsoluteResult() {
+        return handleDirectoryParserAbsoluteResult;
+    }
+
+    /**
+     * Allows you to set how the consumer will handle subfolders and files
+     * in the path if the directory parser results in with absolute paths
+     * The reason for this is that some FTP servers may return file names 
+     * with absolute paths, and if so then the FTP component needs to handle
+     * this by converting the returned path into a relative path.
+     */
+    public void setHandleDirectoryParserAbsoluteResult(boolean 
handleDirectoryParserAbsoluteResult) {
+        this.handleDirectoryParserAbsoluteResult = 
handleDirectoryParserAbsoluteResult;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/ffdec793/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 6ece7e0..1d72438 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
@@ -235,7 +235,11 @@ public class FtpConsumer extends 
RemoteFileConsumer<FTPFile> {
 
         // create a pseudo absolute name
         String dir = FileUtil.stripTrailingSeparator(absolutePath);
-        String absoluteFileName = FileUtil.stripLeadingSeparator(dir + "/" + 
file.getName());
+        String fileName = file.getName();
+        if 
(((FtpConfiguration)endpoint.getConfiguration()).isHandleDirectoryParserAbsoluteResult())
 {
+            fileName = FtpUtils.extractDirNameFromAbsolutePath(file.getName());
+        }
+        String absoluteFileName =  FileUtil.stripLeadingSeparator(dir + "/" + 
fileName);
         // if absolute start with a leading separator otherwise let it be 
relative
         if (absolute) {
             absoluteFileName = "/" + absoluteFileName;

http://git-wip-us.apache.org/repos/asf/camel/blob/ffdec793/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 c092d93..e72e1e7 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
@@ -35,6 +35,15 @@ public final class FtpUtils {
 
     private FtpUtils() {
     }
+    
+    public static String extractDirNameFromAbsolutePath(String path) {
+        // default is unix so try with '/'
+        // otherwise force File.separator
+        if (path.endsWith("/") || path.endsWith(File.separator)) {
+            path = path.substring(0, path.length() - 1);
+        }
+        return FileUtil.stripPath(path);
+    }
 
     /**
      * Compacts a path by stacking it and reducing <tt>..</tt>,

http://git-wip-us.apache.org/repos/asf/camel/blob/ffdec793/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeDirectoryParseWithAbsoluteDepthNoStepwiseTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeDirectoryParseWithAbsoluteDepthNoStepwiseTest.java
 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeDirectoryParseWithAbsoluteDepthNoStepwiseTest.java
new file mode 100644
index 0000000..773588f
--- /dev/null
+++ 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeDirectoryParseWithAbsoluteDepthNoStepwiseTest.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.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+/**
+ * @version 
+ */
+public class FtpSimpleConsumeDirectoryParseWithAbsoluteDepthNoStepwiseTest 
extends FtpServerTestSupport {
+
+    @Test
+    public void testFtpSimpleConsumeAbsolute() throws Exception {
+        if (!canTest()) {
+            return;
+        }
+
+        String expected = "Hello World";
+
+        // create file using regular file
+
+        // FTP Server does not support absolute path, so lets simulate it
+        String path = FTP_ROOT_DIR + "/tmp/mytemp";
+        template.sendBodyAndHeader("file:" + path, expected, 
Exchange.FILE_NAME, "hello.txt");
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedHeaderReceived(Exchange.FILE_NAME, "hello.txt");
+
+        context.startRoute("foo");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("ftp://localhost:"; + getPort() + 
"//tmp/mytemp?username=admin&password=admin&delay=10s"
+                    + 
"&disconnect=true&download=true&stepwise=false&delete=false&handleDirectoryParserAbsoluteResult=true")
+                .routeId("foo").noAutoStartup()
+                    .to("mock:result");
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/ffdec793/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpUtilsExtractDirNameFromAbsolutePathTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpUtilsExtractDirNameFromAbsolutePathTest.java
 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpUtilsExtractDirNameFromAbsolutePathTest.java
new file mode 100644
index 0000000..ec5d583
--- /dev/null
+++ 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpUtilsExtractDirNameFromAbsolutePathTest.java
@@ -0,0 +1,47 @@
+/**
+ * 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.junit.Test;
+import static org.junit.Assert.assertEquals;
+
+public class FtpUtilsExtractDirNameFromAbsolutePathTest {
+
+    @Test
+    public void testExtractDirNameFromAbsolutePath() {
+        String path = "/test/depth1";
+        assertEquals("depth1", FtpUtils.extractDirNameFromAbsolutePath(path));
+        path = "/test/depth1/";
+        assertEquals("depth1", FtpUtils.extractDirNameFromAbsolutePath(path));
+        path = "test/depth1/";
+        assertEquals("depth1", FtpUtils.extractDirNameFromAbsolutePath(path));
+        path = "test/depth1";
+        assertEquals("depth1", FtpUtils.extractDirNameFromAbsolutePath(path));
+        path = "depth1";
+        assertEquals("depth1", FtpUtils.extractDirNameFromAbsolutePath(path));
+
+        path = "C:\\test\\depth1";
+        assertEquals("depth1", FtpUtils.extractDirNameFromAbsolutePath(path));
+        path = "C:\\test\\depth1\\";
+        assertEquals("depth1", FtpUtils.extractDirNameFromAbsolutePath(path));
+        path = "test\\depth1\\";
+        assertEquals("depth1", FtpUtils.extractDirNameFromAbsolutePath(path));
+        path = "test\\depth1";
+        assertEquals("depth1", FtpUtils.extractDirNameFromAbsolutePath(path));
+    }
+
+}

Reply via email to