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 2010/11/25 14:06:19 UTC

svn commit: r1039026 - in /camel/trunk/components/camel-ftp/src: main/java/org/apache/camel/component/file/remote/ test/java/org/apache/camel/component/file/remote/ test/java/org/apache/camel/component/file/remote/sftp/

Author: davsclaus
Date: Thu Nov 25 13:06:18 2010
New Revision: 1039026

URL: http://svn.apache.org/viewvc?rev=1039026&view=rev
Log:
CAMEL-3366: FtpProducer now also suppports stepwise option when uploading files.

Added:
    camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithPathNoStepwiseTest.java
    camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithPathTest.java
      - copied, changed from r1038957, camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerBuildDirectoryTest.java
    camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpProducerFileWithPathNoStepwiseTest.java
    camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpProducerFileWithPathTest.java
      - copied, changed from r1038957, camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleProduceTest.java
Modified:
    camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
    camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
    camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
    camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java

Modified: camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java?rev=1039026&r1=1039025&r2=1039026&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java (original)
+++ camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java Thu Nov 25 13:06:18 2010
@@ -47,7 +47,7 @@ public class FtpConsumer extends RemoteF
         fileName = FileUtil.stripTrailingSeparator(fileName);
 
         boolean answer = doPollDirectory(fileName, null, fileList);
-        if (isStepwise()) {
+        if (currentDir != null) {
             operations.changeCurrentDirectory(currentDir);
         }
 

Modified: camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java?rev=1039026&r1=1039025&r2=1039026&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java (original)
+++ camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java Thu Nov 25 13:06:18 2010
@@ -437,9 +437,43 @@ public class FtpOperations implements Re
             log.trace("storeFile(" + name + ")");
         }
 
+        boolean answer = false;
+        String currentDir = null;
+        String path = FileUtil.onlyPath(name);
+        String targetName = name;
+
+        try {
+            if (path != null && endpoint.getConfiguration().isStepwise()) {
+                // must remember current dir so we stay in that directory after the write
+                currentDir = getCurrentDirectory();
+
+                // change to path of name
+                changeCurrentDirectory(path);
+
+                // the target name should be without path, as we have changed directory
+                targetName = FileUtil.stripPath(name);
+            }
+
+            // store the file
+            answer = doStoreFile(name, targetName, exchange);
+        } finally {
+            // change back to current directory if we changed directory
+            if (currentDir != null) {
+                changeCurrentDirectory(currentDir);
+            }
+        }
+
+        return answer;
+    }
+
+    private boolean doStoreFile(String name, String targetName, Exchange exchange) throws GenericFileOperationFailedException {
+        if (log.isTraceEnabled()) {
+            log.trace("doStoreFile(" + targetName + ")");
+        }
+
         // if an existing file already exists what should we do?
         if (endpoint.getFileExist() == GenericFileExist.Ignore || endpoint.getFileExist() == GenericFileExist.Fail) {
-            boolean existFile = existsFile(name);
+            boolean existFile = existsFile(targetName);
             if (existFile && endpoint.getFileExist() == GenericFileExist.Ignore) {
                 // ignore but indicate that the file was written
                 if (log.isTraceEnabled()) {
@@ -455,9 +489,9 @@ public class FtpOperations implements Re
         try {
             is = exchange.getIn().getMandatoryBody(InputStream.class);
             if (endpoint.getFileExist() == GenericFileExist.Append) {
-                return client.appendFile(name, is);
+                return client.appendFile(targetName, is);
             } else {
-                return client.storeFile(name, is);
+                return client.storeFile(targetName, is);
             }
         } catch (IOException e) {
             throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
@@ -517,6 +551,9 @@ public class FtpOperations implements Re
             return;
         }
 
+        // must compact path so FTP server can traverse correctly
+        path = FileUtil.compactPath(path);
+
         // not stepwise should change directory in one operation
         if (!endpoint.getConfiguration().isStepwise()) {
             doChangeDirectory(path);
@@ -555,7 +592,12 @@ public class FtpOperations implements Re
         }
         boolean success;
         try {
-            success = client.changeWorkingDirectory(path);
+            if ("..".equals(path)) {
+                changeToParentDirectory();
+                success = true;
+            } else {
+                success = client.changeWorkingDirectory(path);
+            }
         } catch (IOException e) {
             throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
         }

Modified: camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java?rev=1039026&r1=1039025&r2=1039026&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java (original)
+++ camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java Thu Nov 25 13:06:18 2010
@@ -47,7 +47,7 @@ public class SftpConsumer extends Remote
         fileName = FileUtil.stripTrailingSeparator(fileName);
 
         boolean answer = doPollDirectory(fileName, null, fileList);
-        if (isStepwise()) {
+        if (currentDir != null) {
             operations.changeCurrentDirectory(currentDir);
         }
 

Modified: camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java?rev=1039026&r1=1039025&r2=1039026&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java (original)
+++ camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java Thu Nov 25 13:06:18 2010
@@ -356,6 +356,9 @@ public class SftpOperations implements R
             return;
         }
 
+        // must compact path so FTP server can traverse correctly
+        path = FileUtil.compactPath(path);
+
         // not stepwise should change directory in one operation
         if (!endpoint.getConfiguration().isStepwise()) {
             doChangeDirectory(path);
@@ -607,9 +610,43 @@ public class SftpOperations implements R
             LOG.trace("storeFile(" + name + ")");
         }
 
+        boolean answer = false;
+        String currentDir = null;
+        String path = FileUtil.onlyPath(name);
+        String targetName = name;
+
+        try {
+            if (path != null && endpoint.getConfiguration().isStepwise()) {
+                // must remember current dir so we stay in that directory after the write
+                currentDir = getCurrentDirectory();
+
+                // change to path of name
+                changeCurrentDirectory(path);
+
+                // the target name should be without path, as we have changed directory
+                targetName = FileUtil.stripPath(name);
+            }
+
+            // store the file
+            answer = doStoreFile(name, targetName, exchange);
+        } finally {
+            // change back to current directory if we changed directory
+            if (currentDir != null) {
+                changeCurrentDirectory(currentDir);
+            }
+        }
+
+        return answer;
+    }
+
+    private boolean doStoreFile(String name, String targetName, Exchange exchange) throws GenericFileOperationFailedException {
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("doStoreFile(" + targetName + ")");
+        }
+
         // if an existing file already exists what should we do?
         if (endpoint.getFileExist() == GenericFileExist.Ignore || endpoint.getFileExist() == GenericFileExist.Fail) {
-            boolean existFile = existsFile(name);
+            boolean existFile = existsFile(targetName);
             if (existFile && endpoint.getFileExist() == GenericFileExist.Ignore) {
                 // ignore but indicate that the file was written
                 if (LOG.isTraceEnabled()) {
@@ -625,10 +662,10 @@ public class SftpOperations implements R
         try {
             is = ExchangeHelper.getMandatoryInBody(exchange, InputStream.class);
             if (endpoint.getFileExist() == GenericFileExist.Append) {
-                channel.put(is, name, ChannelSftp.APPEND);
+                channel.put(is, targetName, ChannelSftp.APPEND);
             } else {
                 // override is default
-                channel.put(is, name);
+                channel.put(is, targetName);
             }
             return true;
         } catch (SftpException e) {

Added: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithPathNoStepwiseTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithPathNoStepwiseTest.java?rev=1039026&view=auto
==============================================================================
--- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithPathNoStepwiseTest.java (added)
+++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithPathNoStepwiseTest.java Thu Nov 25 13:06:18 2010
@@ -0,0 +1,39 @@
+/**
+ * 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.converter.IOConverter;
+import org.junit.Test;
+
+public class FtpProducerFileWithPathNoStepwiseTest extends FtpServerTestSupport {
+
+    private String getFtpUrl() {
+        return "ftp://admin@localhost:" + getPort() + "/upload?password=admin&stepwise=false";
+    }
+
+    @Test
+    public void testProducerFileWithPathNoStepwise() throws Exception {
+        sendFile(getFtpUrl(), "Hello World", "hello/claus.txt");
+
+        File file = new File(FTP_ROOT_DIR + "upload/hello/claus.txt");
+        file = file.getAbsoluteFile();
+        assertTrue("The uploaded file should exists", file.exists());
+        assertEquals("Hello World", IOConverter.toString(file, null));
+    }
+}
\ No newline at end of file

Copied: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithPathTest.java (from r1038957, camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerBuildDirectoryTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithPathTest.java?p2=camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithPathTest.java&p1=camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerBuildDirectoryTest.java&r1=1038957&r2=1039026&rev=1039026&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerBuildDirectoryTest.java (original)
+++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithPathTest.java Thu Nov 25 13:06:18 2010
@@ -21,20 +21,17 @@ import java.io.File;
 import org.apache.camel.converter.IOConverter;
 import org.junit.Test;
 
-/**
- * Unit test to verify that Camel can build remote directory on FTP server if missing (full or part of).
- */
-public class FtpProducerBuildDirectoryTest extends FtpServerTestSupport {
+public class FtpProducerFileWithPathTest extends FtpServerTestSupport {
 
     private String getFtpUrl() {
-        return "ftp://admin@localhost:" + getPort() + "/upload/user/claus?binary=false&password=admin";
+        return "ftp://admin@localhost:" + getPort() + "/upload?password=admin";
     }
 
     @Test
-    public void testProduceAndBuildFullRemotFolderTest() throws Exception {
-        sendFile(getFtpUrl(), "Hello World", "claus.txt");
+    public void testProducerFileWithPath() throws Exception {
+        sendFile(getFtpUrl(), "Hello World", "hello/claus.txt");
 
-        File file = new File(FTP_ROOT_DIR + "upload/user/claus/claus.txt");
+        File file = new File(FTP_ROOT_DIR + "upload/hello/claus.txt");
         file = file.getAbsoluteFile();
         assertTrue("The uploaded file should exists", file.exists());
         assertEquals("Hello World", IOConverter.toString(file, null));

Added: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpProducerFileWithPathNoStepwiseTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpProducerFileWithPathNoStepwiseTest.java?rev=1039026&view=auto
==============================================================================
--- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpProducerFileWithPathNoStepwiseTest.java (added)
+++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpProducerFileWithPathNoStepwiseTest.java Thu Nov 25 13:06:18 2010
@@ -0,0 +1,55 @@
+/**
+ * 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.converter.IOConverter;
+import org.junit.Ignore;
+import org.junit.Test;
+
+/**
+ * @version $Revision$
+ */
+@Ignore("Disabled due CI servers fails on full build running with these tests")
+public class SftpProducerFileWithPathNoStepwiseTest extends SftpServerTestSupport {
+
+    private String getFtpUrl() {
+        return "sftp://admin@localhost:" + getPort() + "/" + FTP_ROOT_DIR + "?password=admin&stepwise=false";
+    }
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    @Test
+    public void testProducerFileWithPathNoStepwise() throws Exception {
+        if (!canTest()) {
+            return;
+        }
+
+        template.sendBodyAndHeader(getFtpUrl(), "Hello World", Exchange.FILE_NAME,  "hello/claus.txt");
+
+        File file = new File(FTP_ROOT_DIR + "/hello/claus.txt");
+        file = file.getAbsoluteFile();
+        assertTrue("The uploaded file should exists", file.exists());
+        assertEquals("Hello World", IOConverter.toString(file, null));
+    }
+
+}

Copied: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpProducerFileWithPathTest.java (from r1038957, camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleProduceTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpProducerFileWithPathTest.java?p2=camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpProducerFileWithPathTest.java&p1=camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleProduceTest.java&r1=1038957&r2=1039026&rev=1039026&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleProduceTest.java (original)
+++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpProducerFileWithPathTest.java Thu Nov 25 13:06:18 2010
@@ -19,6 +19,7 @@ package org.apache.camel.component.file.
 import java.io.File;
 
 import org.apache.camel.Exchange;
+import org.apache.camel.converter.IOConverter;
 import org.junit.Ignore;
 import org.junit.Test;
 
@@ -26,7 +27,11 @@ import org.junit.Test;
  * @version $Revision$
  */
 @Ignore("Disabled due CI servers fails on full build running with these tests")
-public class SftpSimpleProduceTest extends SftpServerTestSupport {
+public class SftpProducerFileWithPathTest extends SftpServerTestSupport {
+
+    private String getFtpUrl() {
+        return "sftp://admin@localhost:" + getPort() + "/" + FTP_ROOT_DIR + "?password=admin";
+    }
 
     @Override
     public boolean isUseRouteBuilder() {
@@ -34,42 +39,17 @@ public class SftpSimpleProduceTest exten
     }
 
     @Test
-    public void testSftpSimpleProduce() throws Exception {
-        if (!canTest()) {
-            return;
-        }
-
-        template.sendBodyAndHeader("sftp://localhost:" + getPort() + "/" + FTP_ROOT_DIR + "?username=admin&password=admin", "Hello World", Exchange.FILE_NAME, "hello.txt");
-
-        File file = new File(FTP_ROOT_DIR + "/hello.txt").getAbsoluteFile();
-        assertTrue("File should exist: " + file, file.exists());
-        assertEquals("Hello World", context.getTypeConverter().convertTo(String.class, file));
-    }
-
-    @Test
-    public void testSftpSimpleSubPathProduce() throws Exception {
-        if (!canTest()) {
-            return;
-        }
-
-        template.sendBodyAndHeader("sftp://localhost:" + getPort() + "/" + FTP_ROOT_DIR + "/mysub?username=admin&password=admin", "Bye World", Exchange.FILE_NAME, "bye.txt");
-
-        File file = new File(FTP_ROOT_DIR + "/mysub/bye.txt").getAbsoluteFile();
-        assertTrue("File should exist: " + file, file.exists());
-        assertEquals("Bye World", context.getTypeConverter().convertTo(String.class, file));
-    }
-
-    @Test
-    public void testSftpSimpleTwoSubPathProduce() throws Exception {
+    public void testProducerFileWithPath() throws Exception {
         if (!canTest()) {
             return;
         }
 
-        template.sendBodyAndHeader("sftp://localhost:" + getPort() + "/" + FTP_ROOT_DIR + "/mysub/myother?username=admin&password=admin", "Farewell World", Exchange.FILE_NAME, "farewell.txt");
+        template.sendBodyAndHeader(getFtpUrl(), "Hello World", Exchange.FILE_NAME,  "hello/claus.txt");
 
-        File file = new File(FTP_ROOT_DIR + "/mysub/myother/farewell.txt").getAbsoluteFile();
-        assertTrue("File should exist: " + file, file.exists());
-        assertEquals("Farewell World", context.getTypeConverter().convertTo(String.class, file));
+        File file = new File(FTP_ROOT_DIR + "/hello/claus.txt");
+        file = file.getAbsoluteFile();
+        assertTrue("The uploaded file should exists", file.exists());
+        assertEquals("Hello World", IOConverter.toString(file, null));
     }
 
 }