Author: davsclaus Date: Sat Feb 28 17:37:44 2009 New Revision: 748891 URL: http://svn.apache.org/viewvc?rev=748891&view=rev Log: CAMEL-1408: Fixes setting absolute paths using the file component.
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerAbsolutePathTest.java - copied, changed from r748883, camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCommitRenameAbsolutePathStrategyTest.java camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerLocalWorkDirectoryAsAbsolutePathTest.java - copied, changed from r748781, camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerLocalWorkDirectoryTest.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileExchange.java camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameExclusiveReadLockStrategy.java camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCommitRenameAbsolutePathStrategyTest.java camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java?rev=748891&r1=748890&r2=748891&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java Sat Feb 28 17:37:44 2009 @@ -105,6 +105,7 @@ answer.setFile(file); answer.setFileLength(file.length()); answer.setFileName(file.getName()); + answer.setAbsolute(file.isAbsolute()); answer.setAbsoluteFileName(file.getAbsolutePath()); try { answer.setCanonicalFileName(file.getCanonicalPath()); @@ -112,10 +113,14 @@ // ignore } answer.setLastModified(file.lastModified()); - if (file.getParent() != null) { - answer.setRelativeFileName(file.getParent() + File.separator + file.getName()); + if (file.isAbsolute()) { + answer.setRelativeFileName(null); } else { - answer.setRelativeFileName(file.getName()); + if (file.getParent() != null) { + answer.setRelativeFileName(file.getParent() + File.separator + file.getName()); + } else { + answer.setRelativeFileName(file.getName()); + } } // use file as body as we have converters if needed as stream answer.setBody(file); 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=748891&r1=748890&r2=748891&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 Sat Feb 28 17:37:44 2009 @@ -35,7 +35,8 @@ private long fileLength; private long lastModified; private T file; - private GenericFileBinding<T> binding; + private GenericFileBinding<T> binding; + private boolean absolute; @Override public GenericFile<T> clone() { @@ -56,22 +57,23 @@ } catch (Exception e) { throw ObjectHelper.wrapRuntimeCamelException(e); } + result.setAbsolute(source.isAbsolute()); result.setAbsoluteFileName(source.getAbsoluteFileName()); result.setCanonicalFileName(source.getCanonicalFileName()); - result.setRelativeFileName(source.getRelativeFileName()); - result.setFileName(source.getFileName()); + result.setRelativeFileName(source.getRelativeFileName()); + result.setFileName(source.getFileName()); result.setFileLength(source.getFileLength()); result.setLastModified(source.getLastModified()); result.setFile(source.getFile()); result.setBody(source.getBody()); result.setBinding(source.getBinding()); return result; - } - + } + public boolean needToNormalize() { return true; } - + public String getFileSeparator() { return File.separator; } @@ -84,13 +86,28 @@ */ public void changeFileName(String newName) { newName = needToNormalize() - // must normalize path to cater for Windows and other OS - ? FileUtil.normalizePath(newName) - // for the remote file we don't need to do that - : newName; + // must normalize path to cater for Windows and other OS + ? FileUtil.normalizePath(newName) + // for the remote file we don't need to do that + : newName; + + // is it relative or absolute + boolean absolute = isAbsolutePath(newName); + + if (absolute) { + setAbsolute(true); + setAbsoluteFileName(newName); + // no relative filename for absolute files + setRelativeFileName(null); + String fileName = newName.substring(newName.lastIndexOf(getFileSeparator()) + 1); + setFileName(fileName); + return; + } + // the rest is complex relative path computation + setAbsolute(false); setAbsoluteFileName(getParent() + getFileSeparator() + newName); - + // relative name is a bit more complex to set as newName itself can contain // folders we need to consider as well String baseNewName = null; @@ -117,6 +134,15 @@ setFileName(newName); } + private boolean isAbsolutePath(String path) { + if (file instanceof File) { + // let java.io.File deal with it as its better than me + return new File(path).isAbsolute(); + } + // otherwise absolute is considered if we start with the separator + return path.startsWith(getFileSeparator()); + } + public String getRelativeFileName() { return relativeFileName; } @@ -165,7 +191,7 @@ getBinding().setBody(this, os); } - public String getParent() { + public String getParent() { if (getAbsoluteFileName().lastIndexOf(getFileSeparator()) > 0) { return getAbsoluteFileName().substring(0, getAbsoluteFileName().lastIndexOf(getFileSeparator())); } else { @@ -186,10 +212,10 @@ public void setAbsoluteFileName(String absoluteFileName) { this.absoluteFileName = needToNormalize() - // must normalize path to cater for Windows and other OS - ? FileUtil.normalizePath(absoluteFileName) - // we don't need to do that for Remote File - : absoluteFileName; + // must normalize path to cater for Windows and other OS + ? FileUtil.normalizePath(absoluteFileName) + // we don't need to do that for Remote File + : absoluteFileName; } public String getAbsoluteFileName() { @@ -204,8 +230,16 @@ this.canonicalFileName = canonicalFileName; } + public boolean isAbsolute() { + return absolute; + } + + public void setAbsolute(boolean absolute) { + this.absolute = absolute; + } + @Override public String toString() { - return "GenericFile[" + relativeFileName + "]"; + return "GenericFile[" + (absolute ? absoluteFileName : relativeFileName) + "]"; } } Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java?rev=748891&r1=748890&r2=748891&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java Sat Feb 28 17:37:44 2009 @@ -420,7 +420,7 @@ message.setBody(file); // compute the name that was written, it should be relative to the endpoint configuraion - String name = file.getRelativeFileName(); + String name = file.isAbsolute() ? file.getAbsoluteFileName() : file.getRelativeFileName(); if (isDirectory() && name.startsWith(getConfiguration().getFile())) { // remove the file path configured on the endpoint for directory=true Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileExchange.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileExchange.java?rev=748891&r1=748890&r2=748891&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileExchange.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileExchange.java Sat Feb 28 17:37:44 2009 @@ -24,6 +24,7 @@ import org.apache.camel.Exchange; import org.apache.camel.ExchangePattern; import org.apache.camel.impl.DefaultExchange; +import org.apache.camel.util.ObjectHelper; public class GenericFileExchange<T> extends DefaultExchange { @@ -62,11 +63,19 @@ protected void populateHeaders(GenericFile<T> file) { if (file != null) { getIn().setHeader("CamelFileName", file.getFileName()); + getIn().setHeader("CamelFileAbsolute", file.isAbsolute()); getIn().setHeader("CamelFileAbsolutePath", file.getAbsoluteFileName()); // set the parent if there is a parent folder - if (file.getRelativeFileName().lastIndexOf(File.separator) != -1) { + if (file.isAbsolute()) { + String parent = file.getAbsoluteFileName().substring(0, file.getAbsoluteFileName().lastIndexOf(File.separator)); + if (ObjectHelper.isNotEmpty(parent)) { + getIn().setHeader("CamelFileParent", parent); + } + } else if (file.getRelativeFileName().lastIndexOf(File.separator) != -1) { String parent = file.getRelativeFileName().substring(0, file.getRelativeFileName().lastIndexOf(File.separator)); - getIn().setHeader("CamelFileParent", parent); + if (ObjectHelper.isNotEmpty(parent)) { + getIn().setHeader("CamelFileParent", parent); + } } getIn().setHeader("CamelFilePath", file.getRelativeFileName()); getIn().setHeader("CamelFileCanonicalPath", file.getCanonicalFileName()); Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java?rev=748891&r1=748890&r2=748891&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/FileLockExclusiveReadLockStrategy.java Sat Feb 28 17:37:44 2009 @@ -123,7 +123,7 @@ ObjectHelper.close(channel, "while acquiring exclusive read lock for file: " + lockFileName, LOG); // need to delete the lock file if (LOG.isTraceEnabled()) { - LOG.trace("Deleting lock file: " + lockFileName); + LOG.trace("Deleting (releasing) exclusive lock file: " + lockFileName); } File lockfile = new File(lockFileName); lockfile.delete(); Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameExclusiveReadLockStrategy.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameExclusiveReadLockStrategy.java?rev=748891&r1=748890&r2=748891&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameExclusiveReadLockStrategy.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/strategy/GenericFileRenameExclusiveReadLockStrategy.java Sat Feb 28 17:37:44 2009 @@ -62,8 +62,8 @@ exclusive = operations.renameFile(file.getAbsoluteFileName(), newFile.getAbsoluteFileName()); if (exclusive) { - if (LOG.isDebugEnabled()) { - LOG.debug("Acquired exclusive read lock to file: " + file); + if (LOG.isTraceEnabled()) { + LOG.trace("Acquired exclusive read lock to file: " + file); } // rename it back so we can read it operations.renameFile(newFile.getAbsoluteFileName(), file.getAbsoluteFileName()); Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerAbsolutePathTest.java (from r748883, camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCommitRenameAbsolutePathStrategyTest.java) URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerAbsolutePathTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerAbsolutePathTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCommitRenameAbsolutePathStrategyTest.java&r1=748883&r2=748891&rev=748891&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCommitRenameAbsolutePathStrategyTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerAbsolutePathTest.java Sat Feb 28 17:37:44 2009 @@ -24,29 +24,23 @@ import org.apache.camel.component.mock.MockEndpoint; /** - * Unit test for the FileRenameStrategy using move options with absolute paths + * Unit test for consuming from an absolute path */ -public class FileConsumerCommitRenameAbsolutePathStrategyTest extends ContextTestSupport { +public class FileConsumerAbsolutePathTest extends ContextTestSupport { private String base; @Override protected void setUp() throws Exception { - deleteDirectory("target/done"); deleteDirectory("target/reports"); // use current dir as base as aboslute path - base = new File(".").getPath(); + base = new File("").getAbsolutePath() + "/target/reports"; super.setUp(); } - // TODO: - public void TestRenameSuccess() throws Exception { - } - - public void xxxTestRenameSuccess() throws Exception { + public void testConsumeFromAbsolutePath() throws Exception { MockEndpoint mock = getMockEndpoint("mock:report"); mock.expectedBodiesReceived("Hello Paris"); - mock.expectedFileExists("./target/done/paris.txt", "Hello Paris"); template.sendBodyAndHeader("file:target/reports", "Hello Paris", Exchange.FILE_NAME, "paris.txt"); @@ -56,7 +50,7 @@ protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { public void configure() throws Exception { - from("file://target/reports?moveExpression=" + base + "/done/${file:name}&consumer.delay=5000").to("mock:report"); + from("file://" + base + "?delete=true").to("mock:report"); } }; } Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCommitRenameAbsolutePathStrategyTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCommitRenameAbsolutePathStrategyTest.java?rev=748891&r1=748890&r2=748891&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCommitRenameAbsolutePathStrategyTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCommitRenameAbsolutePathStrategyTest.java Sat Feb 28 17:37:44 2009 @@ -35,15 +35,11 @@ deleteDirectory("target/done"); deleteDirectory("target/reports"); // use current dir as base as aboslute path - base = new File(".").getPath(); + base = new File("").getAbsolutePath() + "/target"; super.setUp(); } - // TODO: - public void TestRenameSuccess() throws Exception { - } - - public void xxxTestRenameSuccess() throws Exception { + public void testRenameSuccess() throws Exception { MockEndpoint mock = getMockEndpoint("mock:report"); mock.expectedBodiesReceived("Hello Paris"); mock.expectedFileExists("./target/done/paris.txt", "Hello Paris"); 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=748891&r1=748890&r2=748891&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 Sat Feb 28 17:37:44 2009 @@ -100,6 +100,8 @@ answer.setLastModified(file.getTimestamp().getTimeInMillis()); } answer.setHostname(((RemoteFileConfiguration) endpoint.getConfiguration()).getHost()); + // all ftp files is consider as relative + answer.setAbsolute(false); String absoluteFileName = (ObjectHelper.isNotEmpty(directory) ? directory + "/" : "") + file.getName(); answer.setAbsoluteFileName(absoluteFileName); Copied: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerLocalWorkDirectoryAsAbsolutePathTest.java (from r748781, camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerLocalWorkDirectoryTest.java) URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerLocalWorkDirectoryAsAbsolutePathTest.java?p2=camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerLocalWorkDirectoryAsAbsolutePathTest.java&p1=camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerLocalWorkDirectoryTest.java&r1=748781&r2=748891&rev=748891&view=diff ============================================================================== --- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerLocalWorkDirectoryTest.java (original) +++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerLocalWorkDirectoryAsAbsolutePathTest.java Sat Feb 28 17:37:44 2009 @@ -30,10 +30,13 @@ /** * @version $Revision$ */ -public class FtpConsumerLocalWorkDirectoryTest extends FtpServerTestSupport { +public class FtpConsumerLocalWorkDirectoryAsAbsolutePathTest extends FtpServerTestSupport { + + private String base; protected String getFtpUrl() { - return "ftp://ad...@localhost:" + getPort() + "/lwd/?password=admin&delay=5000&localWorkDirectory=target/lwd"; + base = new File("target/lwd").getAbsolutePath(); + return "ftp://ad...@localhost:" + getPort() + "/lwd/?password=admin&delay=5000&localWorkDirectory=" + base; } @Override @@ -83,8 +86,9 @@ public void process(Exchange exchange) throws Exception { File body = exchange.getIn().getBody(File.class); assertNotNull(body); - assertTrue("Local work file should exists", body.exists()); - assertEquals(FileUtil.normalizePath("target/lwd/hello.txt"), body.getPath()); + assertTrue("Should be absolute path", body.isAbsolute()); + assertTrue("Local work file should exists", body.exists()); + assertEquals(FileUtil.normalizePath(base + "/hello.txt"), body.getPath()); } }).to("mock:result", "file://target/out"); }