camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r751614 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/component/file/ camel-core/src/main/java/org/apache/camel/util/ components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/ components/camel-ftp/src/test/ja...
Date Mon, 09 Mar 2009 08:19:08 GMT
Author: davsclaus
Date: Mon Mar  9 08:19:07 2009
New Revision: 751614

URL: http://svn.apache.org/viewvc?rev=751614&view=rev
Log:
CAMEL-1428: SFTP problems with Windows

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.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/RemoteFile.java
    camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/UriConfigurationTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java?rev=751614&r1=751613&r2=751614&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
Mon Mar  9 08:19:07 2009
@@ -39,6 +39,10 @@
     private GenericFileBinding<T> binding;
     private boolean absolute;
 
+    public String getFileSeparator() {
+        return File.separator;
+    }
+
     @Override
     public GenericFile<T> clone() {
         return copyFrom(this);
@@ -72,14 +76,6 @@
         return result;
     }
 
-    public boolean needToNormalize() {
-        return true;
-    }
-
-    public String getFileSeparator() {
-        return File.separator;
-    }
-
     /**
      * Changes the name of this remote file. This method alters the absolute and
      * relative names as well.
@@ -87,12 +83,12 @@
      * @param newName the new name
      */
     public void changeFileName(String newName) {
-        newName = needToNormalize() ? FileUtil.normalizePath(newName) : newName;
-        boolean absolute = isAbsolutePath(newName);
-        boolean nameChangeOnly = newName.indexOf(getFileSeparator()) == -1;
+        newName = FileUtil.normalizePath(newName);
 
         // use java.io.File to help us with computing name changes
         File file = new File(newName);
+        boolean absolute = file.isAbsolute();
+        boolean nameChangeOnly = newName.indexOf(getFileSeparator()) == -1;
 
         // store the file name only
         setFileNameOnly(file.getName());
@@ -130,16 +126,12 @@
         }
     }
 
-    private boolean isAbsolutePath(String path) {
-        return new File(path).isAbsolute();
-    }
-
     public String getRelativeFilePath() {
         return relativeFilePath;
     }
 
     public void setRelativeFilePath(String relativeFilePath) {
-        this.relativeFilePath = needToNormalize() ? FileUtil.normalizePath(relativeFilePath)
: relativeFilePath;
+        this.relativeFilePath = normalizePathToProtocol(relativeFilePath);
     }
 
     public String getFileName() {
@@ -147,7 +139,7 @@
     }
 
     public void setFileName(String fileName) {
-        this.fileName = fileName;
+        this.fileName = normalizePathToProtocol(fileName);
     }
 
     public long getFileLength() {
@@ -183,15 +175,17 @@
     }
 
     public String getParent() {
+        String parent;
         if (isAbsolute()) {
             String name = getAbsoluteFilePath();
             File path = new File(name);
-            return path.getParent();
+            parent = path.getParent();
         } else {
             String name = getRelativeFilePath();
             File path = new File(endpointPath, name);
-            return path.getParent();
+            parent = path.getParent();
         }
+        return normalizePathToProtocol(parent);
     }
 
     public GenericFileBinding<T> getBinding() {
@@ -206,7 +200,7 @@
     }
 
     public void setAbsoluteFilePath(String absoluteFilePath) {
-        this.absoluteFilePath = needToNormalize() ? FileUtil.normalizePath(absoluteFilePath)
: absoluteFilePath;
+        this.absoluteFilePath = normalizePathToProtocol(absoluteFilePath);
     }
 
     public String getAbsoluteFilePath() {
@@ -226,7 +220,7 @@
     }
 
     public void setEndpointPath(String endpointPath) {
-        this.endpointPath = needToNormalize() ? FileUtil.normalizePath(endpointPath) : endpointPath;
+        this.endpointPath = normalizePathToProtocol(endpointPath);
     }
 
     public String getFileNameOnly() {
@@ -237,6 +231,13 @@
         this.fileNameOnly = fileNameOnly;
     }
 
+    /**
+     * Fixes the path separator to be according to the protocol
+     */
+    protected String normalizePathToProtocol(String path) {
+        return path.replaceAll("/|\\\\", getFileSeparator());
+    }
+
     @Override
     public String toString() {
         return "GenericFile[" + (absolute ? absoluteFilePath : relativeFilePath) + "]";

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java?rev=751614&r1=751613&r2=751614&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java Mon Mar  9 08:19:07
2009
@@ -317,8 +317,8 @@
         if (name == null) {
             return null;
         }
-        if (name.startsWith("/") || name.startsWith(File.separator)) {
-            return name.substring(1);
+        while (name.startsWith("/") || name.startsWith(File.separator)) {
+            name = name.substring(1);
         }
         return name;
     }
@@ -330,8 +330,8 @@
         if (name == null) {
             return null;
         }
-        if (name.endsWith("/") || name.endsWith(File.separator)) {
-            return name.substring(0, name.length() - 1);
+        while (name.endsWith("/") || name.endsWith(File.separator)) {
+            name = name.substring(0, name.length() - 1);
         }
         return name;
     }

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=751614&r1=751613&r2=751614&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
Mon Mar  9 08:19:07 2009
@@ -118,12 +118,12 @@
         }
     }
 
-    public boolean deleteFile(FTPClient client, String name) throws GenericFileOperationFailedException
{
+    public boolean deleteFile(String name) throws GenericFileOperationFailedException {
         if (LOG.isDebugEnabled()) {
             LOG.debug("Deleteing file: " + name);
         }
         try {
-            return client.deleteFile(name);
+            return this.client.deleteFile(name);
         } catch (IOException e) {
             throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(),
e.getMessage(), e);
         }
@@ -310,14 +310,6 @@
         }
     }
 
-    public boolean deleteFile(String name) throws GenericFileOperationFailedException {
-        try {
-            return this.client.deleteFile(name);
-        } catch (IOException e) {
-            throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(),
e.getMessage(), e);
-        }
-    }
-
     private boolean buildDirectoryChunks(String dirName) throws IOException {
         final StringBuilder sb = new StringBuilder(dirName.length());
         final String[] dirs = dirName.split("/|\\\\");

Modified: camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFile.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFile.java?rev=751614&r1=751613&r2=751614&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFile.java
(original)
+++ camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFile.java
Mon Mar  9 08:19:07 2009
@@ -17,6 +17,8 @@
 package org.apache.camel.component.file.remote;
 
 import org.apache.camel.component.file.GenericFile;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.FileUtil;
 
 /**
  * Represents a remote file of some sort of backing object
@@ -35,28 +37,41 @@
         this.hostname = hostname;
     }
     
-    @Override
-    public boolean needToNormalize() {
-        return false;
-    }
-    
     public String getFileSeparator() {
         // always use / as separator for FTP
         return "/";
     }
 
+    @SuppressWarnings("unchecked")
     public RemoteFile<T> copyFrom(RemoteFile<T> source) {
-        RemoteFile<T> result = (RemoteFile<T>) source.clone();
+        RemoteFile<T> result;
+        try {
+            result = source.getClass().newInstance();
+        } catch (Exception e) {
+            throw ObjectHelper.wrapRuntimeCamelException(e);
+        }
+
+        // align these setters with GenericFile
+        result.setEndpointPath(source.getEndpointPath());
         result.setAbsolute(source.isAbsolute());
         result.setAbsoluteFilePath(source.getAbsoluteFilePath());
         result.setRelativeFilePath(source.getRelativeFilePath());
         result.setFileName(source.getFileName());
+        result.setFileNameOnly(source.getFileNameOnly());
         result.setFileLength(source.getFileLength());
         result.setLastModified(source.getLastModified());
         result.setFile(source.getFile());
         result.setBody(source.getBody());
         result.setBinding(source.getBinding());
+
         result.setHostname(source.getHostname());
         return result;
     }
+
+    protected String normalizePathToProtocol(String path) {
+        path = super.normalizePathToProtocol(path);
+        // strip leading / for FTP protocol to avoid files with absolute paths
+        return FileUtil.stripLeadingSeparator(path);
+    }
+
 }

Modified: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/UriConfigurationTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/UriConfigurationTest.java?rev=751614&r1=751613&r2=751614&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/UriConfigurationTest.java
(original)
+++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/UriConfigurationTest.java
Mon Mar  9 08:19:07 2009
@@ -92,9 +92,9 @@
         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//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");
@@ -103,8 +103,8 @@
         assertRemoteFileEndpointFile("sftp://hostname/", "");
         assertRemoteFileEndpointFile("sftp://hostname", "");
         assertRemoteFileEndpointFile("sftp://hostname//", "");
-        assertRemoteFileEndpointFile("sftp://hostname//foo/bar", "/foo/bar");
-        assertRemoteFileEndpointFile("sftp://hostname//foo/bar/", "/foo/bar");
+        assertRemoteFileEndpointFile("sftp://hostname//foo/bar", "foo/bar");
+        assertRemoteFileEndpointFile("sftp://hostname//foo/bar/", "foo/bar");
     }
 
     private void assertRemoteFileEndpointFile(String endpointUri, String expectedFile) {



Mime
View raw message