Author: davsclaus Date: Wed Oct 6 08:08:56 2010 New Revision: 1004923 URL: http://svn.apache.org/viewvc?rev=1004923&view=rev Log: CAMEL-3174: Fixed camel-ftp changing directory and downloading files when using absolute paths.
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java camel/trunk/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java 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/RemoteFile.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 camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeAbsoluteTest.java camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpServerTestSupport.java camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleConsumeAbsoluteTest.java 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=1004923&r1=1004922&r2=1004923&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 Wed Oct 6 08:08:56 2010 @@ -90,6 +90,19 @@ public final class FileUtil { } /** + * Does the name start with a leading separator + */ + public static boolean hasLeadingSeparator(String name) { + if (name == null) { + return false; + } + if (name.startsWith("/") || name.startsWith(File.separator)) { + return true; + } + return false; + } + + /** * Strip first leading separator */ public static String stripFirstLeadingSeparator(String name) { Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java?rev=1004923&r1=1004922&r2=1004923&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java Wed Oct 6 08:08:56 2010 @@ -43,6 +43,16 @@ public class FileUtilTest extends TestCa assertEquals("foo/bar", FileUtil.stripLeadingSeparator("///foo/bar")); } + public void testHasLeadingSeparator() { + assertEquals(false, FileUtil.hasLeadingSeparator(null)); + assertEquals(false, FileUtil.hasLeadingSeparator("foo")); + assertEquals(false, FileUtil.hasLeadingSeparator("foo/bar")); + assertEquals(false, FileUtil.hasLeadingSeparator("foo/")); + assertEquals(true, FileUtil.hasLeadingSeparator("/foo/bar")); + assertEquals(true, FileUtil.hasLeadingSeparator("//foo/bar")); + assertEquals(true, FileUtil.hasLeadingSeparator("///foo/bar")); + } + public void testStripFirstLeadingSeparator() { assertEquals(null, FileUtil.stripFirstLeadingSeparator(null)); assertEquals("foo", FileUtil.stripFirstLeadingSeparator("foo")); 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=1004923&r1=1004922&r2=1004923&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 Wed Oct 6 08:08:56 2010 @@ -135,12 +135,17 @@ public class FtpConsumer extends RemoteF } answer.setHostname(((RemoteFileConfiguration) endpoint.getConfiguration()).getHost()); - // all ftp files is considered as relative - answer.setAbsolute(false); + // absolute or relative path + boolean absolute = FileUtil.hasLeadingSeparator(absolutePath); + answer.setAbsolute(absolute); // create a pseudo absolute name String dir = FileUtil.stripTrailingSeparator(absolutePath); - String absoluteFileName = dir + "/" + file.getName(); + String absoluteFileName = FileUtil.stripLeadingSeparator(dir + "/" + file.getName()); + // if absolute start with a leading separator otherwise let it be relative + if (absolute) { + absoluteFileName = "/" + absoluteFileName; + } answer.setAbsoluteFilePath(absoluteFileName); // the relative filename, skip the leading endpoint configured path 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=1004923&r1=1004922&r2=1004923&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 Wed Oct 6 08:08:56 2010 @@ -501,7 +501,7 @@ public class FtpOperations implements Re } // if it starts with the root path then a little special handling for that - if (path.startsWith("/") || path.startsWith("\\")) { + if (FileUtil.hasLeadingSeparator(path)) { // change to root path doChangeDirectory(path.substring(0, 1)); path = path.substring(1); 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=1004923&r1=1004922&r2=1004923&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 Wed Oct 6 08:08:56 2010 @@ -18,7 +18,6 @@ package org.apache.camel.component.file. import org.apache.camel.component.file.GenericFile; import org.apache.camel.component.file.GenericFileMessage; -import org.apache.camel.util.FileUtil; /** * Represents a remote file of some sort of backing object @@ -73,9 +72,4 @@ public class RemoteFile<T> extends Gener remoteResult.setHostname(remoteSource.getHostname()); } - 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/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=1004923&r1=1004922&r2=1004923&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 Wed Oct 6 08:08:56 2010 @@ -133,12 +133,17 @@ public class SftpConsumer extends Remote answer.setLastModified(file.getAttrs().getMTime() * 1000L); answer.setHostname(((RemoteFileConfiguration) endpoint.getConfiguration()).getHost()); - // all ftp files is considered as relative - answer.setAbsolute(false); + // absolute or relative path + boolean absolute = FileUtil.hasLeadingSeparator(absolutePath); + answer.setAbsolute(absolute); // create a pseudo absolute name String dir = FileUtil.stripTrailingSeparator(absolutePath); - String absoluteFileName = dir + "/" + file.getFilename(); + String absoluteFileName = FileUtil.stripLeadingSeparator(dir + "/" + file.getFilename()); + // if absolute start with a leading separator otherwise let it be relative + if (absolute) { + absoluteFileName = "/" + absoluteFileName; + } answer.setAbsoluteFilePath(absoluteFileName); // the relative filename, skip the leading endpoint configured path 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=1004923&r1=1004922&r2=1004923&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 Wed Oct 6 08:08:56 2010 @@ -357,7 +357,7 @@ public class SftpOperations implements R } // if it starts with the root path then a little special handling for that - if (path.startsWith("/") || path.startsWith("\\")) { + if (FileUtil.hasLeadingSeparator(path)) { // change to root path doChangeDirectory(path.substring(0, 1)); path = path.substring(1); Modified: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeAbsoluteTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeAbsoluteTest.java?rev=1004923&r1=1004922&r2=1004923&view=diff ============================================================================== --- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeAbsoluteTest.java (original) +++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpSimpleConsumeAbsoluteTest.java Wed Oct 6 08:08:56 2010 @@ -19,13 +19,11 @@ package org.apache.camel.component.file. import org.apache.camel.Exchange; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; -import org.junit.Ignore; import org.junit.Test; /** * @version $Revision$ */ -...@ignore("Absolute do not work with Apache FTP Server") public class FtpSimpleConsumeAbsoluteTest extends FtpServerTestSupport { @Test @@ -37,7 +35,10 @@ public class FtpSimpleConsumeAbsoluteTes String expected = "Hello World"; // create file using regular file - template.sendBodyAndHeader("file:///tmp/mytemp", expected, Exchange.FILE_NAME, "hello.txt"); + + // 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); @@ -53,6 +54,8 @@ public class FtpSimpleConsumeAbsoluteTes return new RouteBuilder() { @Override public void configure() throws Exception { + // notice we use an absolute starting path: /tmp/mytemp + // - we must remember to use // slash because of the url separator from("ftp://localhost:" + getPort() + "//tmp/mytemp?username=admin&password=admin&delay=10s&disconnect=true") .routeId("foo").noAutoStartup() .to("mock:result"); Modified: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpServerTestSupport.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpServerTestSupport.java?rev=1004923&r1=1004922&r2=1004923&view=diff ============================================================================== --- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpServerTestSupport.java (original) +++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpServerTestSupport.java Wed Oct 6 08:08:56 2010 @@ -35,7 +35,7 @@ import org.junit.Before; */ public class SftpServerTestSupport extends BaseServerTestSupport { - protected static final String FTP_ROOT_DIR = "res/home/"; + protected static final String FTP_ROOT_DIR = "res/home"; protected SshServer sshd; protected boolean canTest; Modified: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleConsumeAbsoluteTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleConsumeAbsoluteTest.java?rev=1004923&r1=1004922&r2=1004923&view=diff ============================================================================== --- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleConsumeAbsoluteTest.java (original) +++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/SftpSimpleConsumeAbsoluteTest.java Wed Oct 6 08:08:56 2010 @@ -19,13 +19,11 @@ package org.apache.camel.component.file. import org.apache.camel.Exchange; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; -import org.junit.Ignore; import org.junit.Test; /** * @version $Revision$ */ -...@ignore("Absolute do not work with Apache SSHD") public class SftpSimpleConsumeAbsoluteTest extends SftpServerTestSupport { @Test @@ -36,8 +34,9 @@ public class SftpSimpleConsumeAbsoluteTe String expected = "Hello World"; - // create file using regular file - template.sendBodyAndHeader("file:///tmp/mytemp", expected, Exchange.FILE_NAME, "hello.txt"); + // 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); @@ -53,7 +52,9 @@ public class SftpSimpleConsumeAbsoluteTe return new RouteBuilder() { @Override public void configure() throws Exception { - from("sftp://localhost:" + getPort() + "//tmp/mytemp?username=admin&password=admin&delay=10s&disconnect=true") + // notice we use an absolute starting path: /res/home/tmp/mytemp + // - we must remember to use // slash because of the url separator + from("sftp://localhost:" + getPort() + "//" + FTP_ROOT_DIR + "/tmp/mytemp?username=admin&password=admin&delay=10s&disconnect=true") .routeId("foo").noAutoStartup() .to("mock:result"); }