Author: davsclaus
Date: Tue Jul 29 09:23:54 2008
New Revision: 680754

URL: http://svn.apache.org/viewvc?rev=680754&view=rev
Log:
CAMEL-764: Added move options for camel-ftp. This commit is for the missing 
SFTP consumer.

Added:
    
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpUtils.java
   (contents, props changed)
      - copied, changed from r680738, 
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpUtils.java
Modified:
    
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
    
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpProducer.java

Modified: 
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java?rev=680754&r1=680753&r2=680754&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
 (original)
+++ 
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
 Tue Jul 29 09:23:54 2008
@@ -28,8 +28,6 @@
 
 import org.apache.camel.Processor;
 import org.apache.camel.component.file.FileComponent;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 
 public class SftpConsumer extends RemoteFileConsumer<RemoteFileExchange> {
     private final SftpEndpoint endpoint;
@@ -200,11 +198,34 @@
                 if (LOG.isDebugEnabled()) {
                     LOG.debug("Deleteing file: " + sftpFile.getFilename() + " 
from: " + remoteServer());
                 }
+                deleteFile(sftpFile.getFilename());
+            } else if (isMoveFile()) {
+                String fromName = sftpFile.getFilename();
+                String toName = getMoveFileName(fromName);
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Moving file: " + fromName + " to: " + toName);
+                }
+
+                // delete any existing file
+                boolean deleted = 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 (!SftpUtils.buildDirectory(channel, directory)) {
+                            LOG.warn("Couldn't build directory: " + directory 
+ " (could be because of denied permissions)");
+                        }
+                    }
+                }
+
+                // try to rename
                 try {
-                    channel.rm(sftpFile.getFilename());
+                    channel.rename(fromName, toName);
                 } catch (SftpException e) {
                     // ignore just log a warning
-                    LOG.warn("Could not delete file: " + 
sftpFile.getFilename() + " from: " + remoteServer());
+                    LOG.warn("Could not move file: " + fromName + " to: " + 
toName);
                 }
             }
 
@@ -212,6 +233,17 @@
         }
     }
 
+    private boolean deleteFile(String filename) {
+        try {
+            channel.rm(filename);
+            return true;
+        } catch (SftpException e) {
+            // ignore just log a warning
+            LOG.warn("Could not delete file: " + filename + " from: " + 
remoteServer());
+            return false;
+        }
+    }
+
     protected void acquireExclusiveRead(ChannelSftp.LsEntry sftpFile) throws 
SftpException {
         if (LOG.isTraceEnabled()) {
             LOG.trace("Waiting for exclusive lock to file: " + sftpFile);

Modified: 
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpProducer.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpProducer.java?rev=680754&r1=680753&r2=680754&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpProducer.java
 (original)
+++ 
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpProducer.java
 Tue Jul 29 09:23:54 2008
@@ -16,13 +16,11 @@
  */
 package org.apache.camel.component.file.remote;
 
-import java.io.IOException;
 import java.io.InputStream;
 
 import com.jcraft.jsch.ChannelSftp;
 import com.jcraft.jsch.JSchException;
 import com.jcraft.jsch.Session;
-import com.jcraft.jsch.SftpException;
 
 import org.apache.camel.Exchange;
 
@@ -96,7 +94,7 @@
             int lastPathIndex = fileName.lastIndexOf('/');
             if (lastPathIndex != -1) {
                 String directory = fileName.substring(0, lastPathIndex);
-                boolean success = buildDirectory(channel, directory);
+                boolean success = SftpUtils.buildDirectory(channel, directory);
                 if (!success) {
                     LOG.warn("Couldn't build directory: " + directory + " 
(could be because of denied permissions)");
                 }
@@ -112,64 +110,4 @@
         }
     }
 
-    protected boolean buildDirectory(ChannelSftp sftpClient, String dirName)
-        throws IOException, SftpException {
-        String originalDirectory = sftpClient.pwd();
-
-        boolean success = false;
-        try {
-            // maybe the full directory already exsits
-            try {
-                sftpClient.cd(dirName);
-                success = true;
-            } catch (SftpException e) {
-                // ignore, we could not change directory so try to create it 
instead
-            }
-
-            if (!success) {
-                if (LOG.isDebugEnabled()) {
-                    LOG.debug("Trying to build remote directory: " + dirName);
-                }
-                
-                try {
-                       sftpClient.mkdir(dirName);
-                       success = true;
-                } catch (SftpException e) {
-                    // we are here if the server side doesn't create 
intermediate folders
-                    // so create the folder one by one
-                    success = buildDirectoryChunks(sftpClient, dirName);
-                }
-            }
-        } finally {
-            // change back to original directory
-            sftpClient.cd(originalDirectory);
-        }
-
-        return success;
-    }
-
-    private boolean buildDirectoryChunks(ChannelSftp sftpClient, String 
dirName)
-       throws IOException, SftpException {
-        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);
-            }
-
-            try {
-               sftpClient.mkdir(directory);
-               success = true;
-            } catch (SftpException e) {
-               // ignore keep trying to create the rest of the path
-            }
-        }
-        
-        return success;
-    }
-    
 }

Copied: 
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpUtils.java
 (from r680738, 
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/SftpUtils.java?p2=activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpUtils.java&p1=activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpUtils.java&r1=680738&r2=680754&rev=680754&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/SftpUtils.java
 Tue Jul 29 09:23:54 2008
@@ -1,102 +1,95 @@
-/**
- * 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.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() {
-    }
-
-    public static void connect(FTPClient client, RemoteFileConfiguration 
config) throws IOException {
-        String host = config.getHost();
-        int port = config.getPort();
-        String username = config.getUsername();
-
-        client.connect(host, port);
-        if (username != null) {
-            client.login(username, config.getPassword());
-        } else {
-            client.login("anonymous", null);
-        }
-        client.setFileType(config.isBinary() ? FTPClient.BINARY_FILE_TYPE : 
FTPClient.ASCII_FILE_TYPE);
-    }
-
-    public static void disconnect(FTPClient client) throws IOException {
-        if (client.isConnected()) {
-            client.disconnect();
-        }
-    }
-
-    public static FTPClient createNewFtpClient() {
-        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;
-    }
-
-}
+/**
+ * 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.IOException;
+
+import com.jcraft.jsch.ChannelSftp;
+import com.jcraft.jsch.SftpException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * Utility methods for SFTP.
+ */
+public final class SftpUtils {
+    private static final transient Log LOG = 
LogFactory.getLog(SftpUtils.class);
+
+    private SftpUtils() {
+    }
+
+    public static boolean buildDirectory(ChannelSftp sftpClient, String 
dirName)
+        throws IOException, SftpException {
+        String originalDirectory = sftpClient.pwd();
+
+        boolean success = false;
+        try {
+            // maybe the full directory already exsits
+            try {
+                sftpClient.cd(dirName);
+                success = true;
+            } catch (SftpException e) {
+                // ignore, we could not change directory so try to create it 
instead
+            }
+
+            if (!success) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Trying to build remote directory: " + dirName);
+                }
+
+                try {
+                    sftpClient.mkdir(dirName);
+                    success = true;
+                } catch (SftpException e) {
+                    // we are here if the server side doesn't create 
intermediate folders
+                    // so create the folder one by one
+                    success = buildDirectoryChunks(sftpClient, dirName);
+                }
+            }
+        } finally {
+            // change back to original directory
+            sftpClient.cd(originalDirectory);
+        }
+
+        return success;
+    }
+
+    public static boolean buildDirectoryChunks(ChannelSftp sftpClient, String 
dirName)
+        throws IOException, SftpException {
+        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);
+            }
+
+            try {
+                sftpClient.mkdir(directory);
+                success = true;
+            } catch (SftpException e) {
+                // ignore keep trying to create the rest of the path
+            }
+        }
+
+        return success;
+    }
+}
\ No newline at end of file

Propchange: 
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
activemq/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpUtils.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date


Reply via email to