Author: davsclaus
Date: Sun Jul 27 23:04:36 2008
New Revision: 680260

URL: http://svn.apache.org/viewvc?rev=680260&view=rev
Log:
CAMEL-764: Added move options for camel-ftp. This commit is only for the 
regular FTP consumer. The SFTP consumer is pending. WORK IN PROGRESS.

Added:
    
activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpMoveFilePostfixTest.java
    
activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpMoveFilePrefixTest.java
      - copied, changed from r680254, 
activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpDeleteFileTest.java
    
activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpMoveFileTest.java
Modified:
    
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
    
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpProducer.java
    
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpUtils.java
    
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConsumer.java

Modified: 
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java?rev=680260&r1=680259&r2=680260&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
 (original)
+++ 
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
 Sun Jul 27 23:04:36 2008
@@ -190,6 +190,32 @@
                     // ignore just log a warning
                     LOG.warn("Could not delete file: " + ftpFile.getName() + " 
from: " + remoteServer());
                 }
+            } else if (isMoveFile()) {
+                String fromName = ftpFile.getName();
+                String toName = getMoveFileName(fromName);
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Moving file: " + fromName + " to: " + toName);
+                }
+
+                // delete any existing file
+                boolean deleted = client.deleteFile(toName);
+                if (!deleted) {
+                    // if we could not delete any existing file then maybe the 
folder is missing
+                    // build folder if needed
+                    int lastPathIndex = toName.lastIndexOf('/');
+                    if (lastPathIndex != -1) {
+                        String directory = toName.substring(0, lastPathIndex);
+                        if (!FtpUtils.buildDirectory(client, directory)) {
+                            LOG.warn("Couldn't build directory: " + directory 
+ " (could be because of denied permissions)");
+                        }
+                    }
+                }
+
+                // try to rename
+                boolean success = client.rename(fromName, toName);
+                if (!success) {
+                    LOG.warn("Could not move file: " + fromName + " to: " + 
toName);
+                }
             }
 
             getProcessor().process(exchange);

Modified: 
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpProducer.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpProducer.java?rev=680260&r1=680259&r2=680260&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpProducer.java
 (original)
+++ 
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpProducer.java
 Sun Jul 27 23:04:36 2008
@@ -83,7 +83,7 @@
             int lastPathIndex = fileName.lastIndexOf('/');
             if (lastPathIndex != -1) {
                 String directory = fileName.substring(0, lastPathIndex);
-                if (!buildDirectory(client, directory)) {
+                if (!FtpUtils.buildDirectory(client, directory)) {
                     LOG.warn("Couldn't build directory: " + directory + " 
(could be because of denied permissions)");
                 }
             }
@@ -101,48 +101,4 @@
         }
     }
 
-    protected boolean buildDirectory(FTPClient ftpClient, String dirName) 
throws IOException {
-        String originalDirectory = ftpClient.printWorkingDirectory();
-
-        boolean success = false;
-        try {
-            // maybe the full directory already exsits
-            success = ftpClient.changeWorkingDirectory(dirName);
-            if (!success) {
-                if (LOG.isDebugEnabled()) {
-                    LOG.debug("Trying to build remote directory: " + dirName);
-                }
-                success = ftpClient.makeDirectory(dirName);
-                if (!success) {
-                    // we are here if the server side doesn't create 
intermediate folders
-                    // so create the folder one by one
-                    buildDirectoryChunks(ftpClient, dirName);
-                }
-            }
-        } finally {
-            // change back to original directory
-            ftpClient.changeWorkingDirectory(originalDirectory);
-        }
-
-        return success;
-    }
-
-    private boolean buildDirectoryChunks(FTPClient ftpClient, String dirName) 
throws IOException {
-        final StringBuilder sb = new StringBuilder(dirName.length());
-        final String[] dirs = dirName.split("\\/");
-
-        boolean success = false;
-        for (String dir : dirs) {
-            sb.append(dir).append('/');
-            String directory = sb.toString();
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Trying to build remote directory: " + directory);
-            }
-
-            success = ftpClient.makeDirectory(directory);
-        }
-
-        return success;
-    }
-
 }

Modified: 
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpUtils.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpUtils.java?rev=680260&r1=680259&r2=680260&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpUtils.java
 (original)
+++ 
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpUtils.java
 Sun Jul 27 23:04:36 2008
@@ -18,12 +18,15 @@
 
 import java.io.IOException;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.commons.net.ftp.FTPClient;
 
 /**
  * Utility methods for FTP.
  */
 public final class FtpUtils {
+    private static final transient Log LOG = LogFactory.getLog(FtpUtils.class);
 
     private FtpUtils() {
     }
@@ -52,4 +55,48 @@
         return new FTPClient();
     }
 
+    public static boolean buildDirectory(FTPClient ftpClient, String dirName) 
throws IOException {
+        String originalDirectory = ftpClient.printWorkingDirectory();
+
+        boolean success = false;
+        try {
+            // maybe the full directory already exsits
+            success = ftpClient.changeWorkingDirectory(dirName);
+            if (!success) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Trying to build remote directory: " + dirName);
+                }
+                success = ftpClient.makeDirectory(dirName);
+                if (!success) {
+                    // we are here if the server side doesn't create 
intermediate folders
+                    // so create the folder one by one
+                    buildDirectoryChunks(ftpClient, dirName);
+                }
+            }
+        } finally {
+            // change back to original directory
+            ftpClient.changeWorkingDirectory(originalDirectory);
+        }
+
+        return success;
+    }
+
+    public static boolean buildDirectoryChunks(FTPClient ftpClient, String 
dirName) throws IOException {
+        final StringBuilder sb = new StringBuilder(dirName.length());
+        final String[] dirs = dirName.split("\\/");
+
+        boolean success = false;
+        for (String dir : dirs) {
+            sb.append(dir).append('/');
+            String directory = sb.toString();
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("Trying to build remote directory: " + directory);
+            }
+
+            success = ftpClient.makeDirectory(directory);
+        }
+
+        return success;
+    }
+
 }

Modified: 
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConsumer.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConsumer.java?rev=680260&r1=680259&r2=680260&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConsumer.java
 (original)
+++ 
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConsumer.java
 Sun Jul 27 23:04:36 2008
@@ -33,6 +33,8 @@
     protected boolean setNames = true;
     protected boolean exclusiveRead = true;
     protected boolean deleteFile;
+    protected String moveNamePrefix;
+    protected String moveNamePostfix;
 
     public RemoteFileConsumer(RemoteFileEndpoint<T> endpoint, Processor 
processor) {
         super(endpoint, processor);
@@ -69,6 +71,31 @@
         return result;
     }
 
+    /**
+     * Should the file be moved after consuming?
+     */
+    protected boolean isMoveFile() {
+        return moveNamePostfix != null || moveNamePrefix != null;
+    }
+
+    /**
+     * Gets the to filename for moving.
+     * 
+     * @param name the original filename
+     * @return the move filename
+     */
+    protected String getMoveFileName(String name) {
+        StringBuffer buffer = new StringBuffer();
+        if (moveNamePrefix != null) {
+            buffer.append(moveNamePrefix);
+        }
+        buffer.append(name);
+        if (moveNamePostfix != null) {
+            buffer.append(moveNamePostfix);
+        }
+        return buffer.toString();
+    }
+
     protected String remoteServer() {
         return endpoint.getConfiguration().remoteServerInformation();
     }
@@ -121,4 +148,20 @@
         this.deleteFile = deleteFile;
     }
 
+    public String getMoveNamePrefix() {
+        return moveNamePrefix;
+    }
+
+    public void setMoveNamePrefix(String moveNamePrefix) {
+        this.moveNamePrefix = moveNamePrefix;
+    }
+
+    public String getMoveNamePostfix() {
+        return moveNamePostfix;
+    }
+
+    public void setMoveNamePostfix(String moveNamePostfix) {
+        this.moveNamePostfix = moveNamePostfix;
+    }
+
 }

Added: 
activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpMoveFilePostfixTest.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpMoveFilePostfixTest.java?rev=680260&view=auto
==============================================================================
--- 
activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpMoveFilePostfixTest.java
 (added)
+++ 
activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpMoveFilePostfixTest.java
 Sun Jul 27 23:04:36 2008
@@ -0,0 +1,87 @@
+/**
+ * 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.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Producer;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.file.FileComponent;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * Unit test to test consumer.moveNamePostfix option.
+ */
+public class FromFtpMoveFilePostfixTest extends FtpServerTestSupport {
+
+    private String port = "20031";
+    private String ftpUrl = "ftp://[EMAIL PROTECTED]:" + port + 
"/movefile?password=admin&binary=false"
+        + "&consumer.moveNamePostfix=.old";
+
+    public String getPort() {
+        return port;
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        deleteDirectory("./res/home/movefile");
+        prepareFtpServer();
+    }
+
+    private void prepareFtpServer() throws Exception {
+        // prepares the FTP Server by creating a file on the server that we 
want to unit
+        // test that we can pool and store as a local file
+        Endpoint endpoint = context.getEndpoint(ftpUrl);
+        Exchange exchange = endpoint.createExchange();
+        exchange.getIn().setBody("Hello World this file will be moved");
+        exchange.getIn().setHeader(FileComponent.HEADER_FILE_NAME, 
"hello.txt");
+        Producer producer = endpoint.createProducer();
+        producer.start();
+        producer.process(exchange);
+        producer.stop();
+
+        // assert file is created
+        File file = new File("./res/home/movefile/hello.txt");
+        file = file.getAbsoluteFile();
+        assertTrue("The file should exists", file.exists());
+    }
+
+    public void testPollFileAndShouldBeMoved() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedBodiesReceived("Hello World this file will be moved");
+
+        mock.assertIsSatisfied();
+
+        // assert the file is deleted
+        File file = new File("./res/home/movefile/hello.txt.old");
+        file = file.getAbsoluteFile();
+        assertTrue("The file should have been moved", file.exists());
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from(ftpUrl).to("mock:result");
+            }
+        };
+    }
+
+}
\ No newline at end of file

Copied: 
activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpMoveFilePrefixTest.java
 (from r680254, 
activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpDeleteFileTest.java)
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpMoveFilePrefixTest.java?p2=activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpMoveFilePrefixTest.java&p1=activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpDeleteFileTest.java&r1=680254&r2=680260&rev=680260&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpDeleteFileTest.java
 (original)
+++ 
activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpMoveFilePrefixTest.java
 Sun Jul 27 23:04:36 2008
@@ -26,12 +26,13 @@
 import org.apache.camel.component.mock.MockEndpoint;
 
 /**
- * Unit test to test consumer.deleteFile option.
+ * Unit test to test consumer.moveNamePrefix option.
  */
-public class FromFtpDeleteFileTest extends FtpServerTestSupport {
+public class FromFtpMoveFilePrefixTest extends FtpServerTestSupport {
 
-    private String port = "20022";
-    private String ftpUrl = "ftp://[EMAIL PROTECTED]:" + port + 
"/deletefile?password=admin&binary=false&consumer.deleteFile=true";
+    private String port = "20030";
+    private String ftpUrl = "ftp://[EMAIL PROTECTED]:" + port + 
"/movefile?password=admin&binary=false"
+        + "&consumer.moveNamePrefix=done/";
 
     public String getPort() {
         return port;
@@ -40,6 +41,7 @@
     @Override
     protected void setUp() throws Exception {
         super.setUp();
+        deleteDirectory("./res/home/movefile");
         prepareFtpServer();
     }
 
@@ -48,7 +50,7 @@
         // test that we can pool and store as a local file
         Endpoint endpoint = context.getEndpoint(ftpUrl);
         Exchange exchange = endpoint.createExchange();
-        exchange.getIn().setBody("Hello World this file will be deleted");
+        exchange.getIn().setBody("Hello World this file will be moved");
         exchange.getIn().setHeader(FileComponent.HEADER_FILE_NAME, 
"hello.txt");
         Producer producer = endpoint.createProducer();
         producer.start();
@@ -56,22 +58,22 @@
         producer.stop();
 
         // assert file is created
-        File file = new File("./res/home/deletefile/hello.txt");
+        File file = new File("./res/home/movefile/hello.txt");
         file = file.getAbsoluteFile();
         assertTrue("The file should exists", file.exists());
     }
 
-    public void testPollFileAndShouldBeDeleted() throws Exception {
+    public void testPollFileAndShouldBeMoved() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedMessageCount(1);
-        mock.expectedBodiesReceived("Hello World this file will be deleted");
+        mock.expectedBodiesReceived("Hello World this file will be moved");
 
         mock.assertIsSatisfied();
 
         // assert the file is deleted
-        File file = new File("./res/home/deletefile/hello.txt");
+        File file = new File("./res/home/movefile/done/hello.txt");
         file = file.getAbsoluteFile();
-        assertFalse("The file should have been deleted", file.exists());
+        assertTrue("The file should have been moved", file.exists());
     }
 
     protected RouteBuilder createRouteBuilder() throws Exception {
@@ -82,4 +84,4 @@
         };
     }
 
-}
+}
\ No newline at end of file

Added: 
activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpMoveFileTest.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpMoveFileTest.java?rev=680260&view=auto
==============================================================================
--- 
activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpMoveFileTest.java
 (added)
+++ 
activemq/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpMoveFileTest.java
 Sun Jul 27 23:04:36 2008
@@ -0,0 +1,87 @@
+/**
+ * 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.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Producer;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.file.FileComponent;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * Unit test to test both consumer.moveNamePrefix and consumer.moveNamePostfix 
options.
+ */
+public class FromFtpMoveFileTest extends FtpServerTestSupport {
+
+    private String port = "20032";
+    private String ftpUrl = "ftp://[EMAIL PROTECTED]:" + port + 
"/movefile?password=admin&binary=false"
+        + "&consumer.moveNamePrefix=done/sub2/&consumer.moveNamePostfix=.old";
+
+    public String getPort() {
+        return port;
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        deleteDirectory("./res/home/movefile");
+        prepareFtpServer();
+    }
+
+    private void prepareFtpServer() throws Exception {
+        // prepares the FTP Server by creating a file on the server that we 
want to unit
+        // test that we can pool and store as a local file
+        Endpoint endpoint = context.getEndpoint(ftpUrl);
+        Exchange exchange = endpoint.createExchange();
+        exchange.getIn().setBody("Hello World this file will be moved");
+        exchange.getIn().setHeader(FileComponent.HEADER_FILE_NAME, 
"hello.txt");
+        Producer producer = endpoint.createProducer();
+        producer.start();
+        producer.process(exchange);
+        producer.stop();
+
+        // assert file is created
+        File file = new File("./res/home/movefile/hello.txt");
+        file = file.getAbsoluteFile();
+        assertTrue("The file should exists", file.exists());
+    }
+
+    public void testPollFileAndShouldBeMoved() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedBodiesReceived("Hello World this file will be moved");
+
+        mock.assertIsSatisfied();
+
+        // assert the file is deleted
+        File file = new File("./res/home/movefile/done/sub2/hello.txt.old");
+        file = file.getAbsoluteFile();
+        assertTrue("The file should have been moved", file.exists());
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from(ftpUrl).to("mock:result");
+            }
+        };
+    }
+
+}
\ No newline at end of file


Reply via email to