Author: davsclaus Date: Wed Jul 20 11:23:59 2011 New Revision: 1148706 URL: http://svn.apache.org/viewvc?rev=1148706&view=rev Log: CAMEL-4252: file/ftp producer should cater for both unix/windows separators when calculating the file name, as it may contain mixed separators.
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FilerProducerFileNamesTest.java camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithPathPathSeparatorUnixNoStepwiseTest.java camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithPathPathSeparatorWindowsNoStepwiseTest.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java?rev=1148706&r1=1148705&r2=1148706&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java Wed Jul 20 11:23:59 2011 @@ -281,10 +281,8 @@ public class GenericFileProducer<T> exte // flatten name if (name != null && endpoint.isFlatten()) { - int pos = name.lastIndexOf(getFileSeparator()); - if (pos == -1) { - pos = name.lastIndexOf('/'); - } + // check for both windows and unix separators + int pos = Math.max(name.lastIndexOf("/"), name.lastIndexOf("\\")); if (pos != -1) { name = name.substring(pos + 1); } @@ -292,11 +290,15 @@ public class GenericFileProducer<T> exte // compute path by adding endpoint starting directory String endpointPath = endpoint.getConfiguration().getDirectory(); - // Its a directory so we should use it as a base path for the filename - // If the path isn't empty, we need to add a trailing / if it isn't already there String baseDir = ""; if (endpointPath.length() > 0) { - baseDir = endpointPath + (endpointPath.endsWith(getFileSeparator()) ? "" : getFileSeparator()); + // Its a directory so we should use it as a base path for the filename + // If the path isn't empty, we need to add a trailing / if it isn't already there + baseDir = endpointPath; + boolean trailingSlash = endpointPath.endsWith("/") || endpointPath.endsWith("\\"); + if (!trailingSlash) { + baseDir += getFileSeparator(); + } } if (name != null) { answer = baseDir + name; @@ -314,8 +316,7 @@ public class GenericFileProducer<T> exte } public String createTempFileName(Exchange exchange, String fileName) { - // must normalize path to cater for Windows and other OS - fileName = normalizePath(fileName); + String answer = fileName; String tempName; if (exchange.getIn().getHeader(Exchange.FILE_NAME) == null) { @@ -328,15 +329,24 @@ public class GenericFileProducer<T> exte tempName = endpoint.getTempFileName().evaluate(exchange, String.class); } - int path = fileName.lastIndexOf(getFileSeparator()); - if (path == -1) { - // no path - return tempName; + // check for both windows and unix separators + int pos = Math.max(answer.lastIndexOf("/"), answer.lastIndexOf("\\")); + if (pos == -1) { + // no path so use temp name as calculated + answer = tempName; } else { - StringBuilder sb = new StringBuilder(fileName.substring(0, path + 1)); + // path should be prefixed before the temp name + StringBuilder sb = new StringBuilder(answer.substring(0, pos + 1)); sb.append(tempName); - return sb.toString(); + answer = sb.toString(); } + + if (endpoint.getConfiguration().needToNormalize()) { + // must normalize path to cater for Windows and other OS + answer = normalizePath(answer); + } + + return answer; } @Override Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FilerProducerFileNamesTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FilerProducerFileNamesTest.java?rev=1148706&r1=1148705&r2=1148706&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FilerProducerFileNamesTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FilerProducerFileNamesTest.java Wed Jul 20 11:23:59 2011 @@ -28,6 +28,12 @@ import org.apache.camel.builder.RouteBui */ public class FilerProducerFileNamesTest extends ContextTestSupport { + @Override + protected void setUp() throws Exception { + deleteDirectory("target/reports"); + super.setUp(); + } + // START SNIPPET: e1 public void testProducerWithMessageIdAsFileName() throws Exception { Endpoint endpoint = context.getEndpoint("direct:report"); Modified: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithPathPathSeparatorUnixNoStepwiseTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithPathPathSeparatorUnixNoStepwiseTest.java?rev=1148706&r1=1148705&r2=1148706&view=diff ============================================================================== --- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithPathPathSeparatorUnixNoStepwiseTest.java (original) +++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithPathPathSeparatorUnixNoStepwiseTest.java Wed Jul 20 11:23:59 2011 @@ -34,7 +34,7 @@ public class FtpProducerFileWithPathPath Exchange out = template.send(getFtpUrl(), new Processor() { public void process(Exchange exchange) throws Exception { exchange.getIn().setBody("Hello World"); - exchange.getIn().setHeader(Exchange.FILE_NAME, "hello\\claus.txt"); + exchange.getIn().setHeader(Exchange.FILE_NAME, "hello/claus.txt"); } }); assertNotNull(out); @@ -44,7 +44,7 @@ public class FtpProducerFileWithPathPath assertTrue("The uploaded file should exists", file.exists()); assertEquals("Hello World", IOConverter.toString(file, null)); - assertEquals("upload/hello\\claus.txt", out.getIn().getHeader(Exchange.FILE_NAME_PRODUCED)); + assertEquals("upload/hello/claus.txt", out.getIn().getHeader(Exchange.FILE_NAME_PRODUCED)); } } \ No newline at end of file Modified: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithPathPathSeparatorWindowsNoStepwiseTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithPathPathSeparatorWindowsNoStepwiseTest.java?rev=1148706&r1=1148705&r2=1148706&view=diff ============================================================================== --- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithPathPathSeparatorWindowsNoStepwiseTest.java (original) +++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileWithPathPathSeparatorWindowsNoStepwiseTest.java Wed Jul 20 11:23:59 2011 @@ -34,7 +34,7 @@ public class FtpProducerFileWithPathPath Exchange out = template.send(getFtpUrl(), new Processor() { public void process(Exchange exchange) throws Exception { exchange.getIn().setBody("Hello World"); - exchange.getIn().setHeader(Exchange.FILE_NAME, "hello/claus.txt"); + exchange.getIn().setHeader(Exchange.FILE_NAME, "hello\\claus.txt"); } }); assertNotNull(out); @@ -44,7 +44,7 @@ public class FtpProducerFileWithPathPath assertTrue("The uploaded file should exists", file.exists()); assertEquals("Hello World", IOConverter.toString(file, null)); - assertEquals("upload/hello/claus.txt", out.getIn().getHeader(Exchange.FILE_NAME_PRODUCED)); + assertEquals("upload/hello\\claus.txt", out.getIn().getHeader(Exchange.FILE_NAME_PRODUCED)); } } \ No newline at end of file