Author: davsclaus Date: Wed Mar 11 11:41:39 2009 New Revision: 752427 URL: http://svn.apache.org/viewvc?rev=752427&view=rev Log: CAMEL-1439: Added flattern option to Camel VFS
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileToFileWithFlatternTest.java (with props) Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.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/GenericFileProducer.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java?rev=752427&r1=752426&r2=752427&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java Wed Mar 11 11:41:39 2009 @@ -76,6 +76,8 @@ return true; } + // TODO: Check bug for double starting directory + File path; if (absolute) { path = new File(directory); 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=752427&r1=752426&r2=752427&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 Wed Mar 11 11:41:39 2009 @@ -95,11 +95,18 @@ if (LOG.isTraceEnabled()) { LOG.trace("Changing name to: " + newName); } + // Make sure the newName is normalized. String newFileName = FileUtil.normalizePath(newName); - File file = new File(newFileName); + + if (LOG.isTraceEnabled()) { + LOG.trace("Normalized endpointPath: " + endpointPath); + LOG.trace("Normalized newFileName: " + newFileName); + } + + File file = new File(newFileName); if (!absolute) { - // for relative then we should avoid having the endpoint path duplicated so clip it + // for relative then we should avoid having the endpoint path duplicated so clip it if (ObjectHelper.isNotEmpty(endpointPath) && newFileName.startsWith(endpointPath)) { // clip starting endpoint in case it was added newFileName = ObjectHelper.after(newFileName, endpointPath + getFileSeparator()); @@ -132,6 +139,11 @@ } if (LOG.isTraceEnabled()) { + LOG.trace("FileNameOnly: " + getFileNameOnly()); + LOG.trace("FileName: " + getFileName()); + LOG.trace("Absolute: " + isAbsolute()); + LOG.trace("Relative path: " + getRelativeFilePath()); + LOG.trace("Absolute path: " + getAbsoluteFilePath()); LOG.trace("Name changed to: " + this); } } 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=752427&r1=752426&r2=752427&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 Wed Mar 11 11:41:39 2009 @@ -59,6 +59,7 @@ protected boolean noop; protected boolean recursive; protected boolean delete; + protected boolean flattern; protected String tempPrefix; protected String include; protected String exclude; @@ -186,6 +187,14 @@ this.delete = delete; } + public boolean isFlattern() { + return flattern; + } + + public void setFlattern(boolean flattern) { + this.flattern = flattern; + } + public Expression getMove() { return move; } @@ -386,17 +395,22 @@ public void configureMessage(GenericFile<T> file, Message message) { message.setBody(file); - // compute name to set on header that should be relative to starting directory - String name = file.isAbsolute() ? file.getAbsoluteFilePath() : file.getRelativeFilePath(); + if (flattern) { + // when flattern the file name should not contain any paths + message.setHeader(Exchange.FILE_NAME, file.getFileNameOnly()); + } else { + // compute name to set on header that should be relative to starting directory + String name = file.isAbsolute() ? file.getAbsoluteFilePath() : file.getRelativeFilePath(); + + // skip leading endpoint configured directory + String endpointPath = getConfiguration().getDirectory(); + if (ObjectHelper.isNotEmpty(endpointPath) && name.startsWith(endpointPath)) { + name = ObjectHelper.after(name, getConfiguration().getDirectory() + File.separator); + } - // skip leading endpoint configured directory - String endpointPath = getConfiguration().getDirectory(); - if (ObjectHelper.isNotEmpty(endpointPath) && name.startsWith(endpointPath)) { - name = ObjectHelper.after(name, getConfiguration().getDirectory() + File.separator); + // adjust filename + message.setHeader(Exchange.FILE_NAME, name); } - - // adjust filename - message.setHeader(Exchange.FILE_NAME, name); } /** 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=752427&r1=752426&r2=752427&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 Mar 11 11:41:39 2009 @@ -122,7 +122,7 @@ // skip trailing / directory = FileUtil.stripLeadingSeparator(directory); if (!operations.buildDirectory(directory, false)) { - log.debug("Can not build directory [" + directory + "] (could be because of denied permissions)"); + log.debug("Cannot build directory [" + directory + "] (could be because of denied permissions)"); } } } @@ -170,12 +170,25 @@ name = expression.evaluate(exchange, String.class); } - String endpointFile = endpoint.getConfiguration().getDirectory(); + + // flattern name + if (endpoint.isFlattern()) { + int pos = name.lastIndexOf(File.separator); + if (pos == -1) { + pos = name.lastIndexOf('/'); + } + if (pos != -1) { + name = name.substring(pos + 1); + } + } + + // 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 (endpointFile.length() > 0) { - baseDir = endpointFile + (endpointFile.endsWith(File.separator) ? "" : File.separator); + if (endpointPath.length() > 0) { + baseDir = endpointPath + (endpointPath.endsWith(File.separator) ? "" : File.separator); } if (name != null) { answer = baseDir + name; Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileToFileWithFlatternTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileToFileWithFlatternTest.java?rev=752427&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileToFileWithFlatternTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileToFileWithFlatternTest.java Wed Mar 11 11:41:39 2009 @@ -0,0 +1,102 @@ +/** + * 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; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; + +/** + * @version $Revision$ + */ +public class FileToFileWithFlatternTest extends ContextTestSupport { + + private String fileUrl = "file://target/flattern-in"; + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @Override + protected void setUp() throws Exception { + deleteDirectory("./target/flattern-in"); + deleteDirectory("./target/flattern-out"); + super.setUp(); + template.sendBodyAndHeader(fileUrl, "Bye World", Exchange.FILE_NAME, "bye.txt"); + template.sendBodyAndHeader(fileUrl, "Hello World", Exchange.FILE_NAME, "sub/hello.txt"); + template.sendBodyAndHeader(fileUrl, "Goodday World", Exchange.FILE_NAME, "sub/sub2/goodday.txt"); + } + + @Override + protected void tearDown() throws Exception { + context.stop(); + super.tearDown(); + } + + public void testFlatternConsumer() throws Exception { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("file://target/flattern-in?recursive=true&flattern=true").to("file://target/flattern-out", "mock:result"); + } + }); + context.start(); + + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(3); + + // flattern files + mock.expectedFileExists("./target/flattern-out/bye.txt"); + mock.expectedFileExists("./target/flattern-out/hello.txt"); + mock.expectedFileExists("./target/flattern-out/goodday.txt"); + + // default move files + mock.expectedFileExists("./target/flattern-in/.camel/bye.txt"); + mock.expectedFileExists("./target/flattern-in/sub/.camel/hello.txt"); + mock.expectedFileExists("./target/flattern-in/sub/sub2/.camel/goodday.txt"); + + assertMockEndpointsSatisfied(); + } + + public void testFlatternProducer() throws Exception { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("file://target/flattern-in?recursive=true").to("file://target/flattern-out?flattern=true", "mock:result"); + } + }); + context.start(); + + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(3); + + // flattern files + mock.expectedFileExists("./target/flattern-out/bye.txt"); + mock.expectedFileExists("./target/flattern-out/hello.txt"); + mock.expectedFileExists("./target/flattern-out/goodday.txt"); + + // default move files + mock.expectedFileExists("./target/flattern-in/.camel/bye.txt"); + mock.expectedFileExists("./target/flattern-in/sub/.camel/hello.txt"); + mock.expectedFileExists("./target/flattern-in/sub/sub2/.camel/goodday.txt"); + + assertMockEndpointsSatisfied(); + } + +} Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileToFileWithFlatternTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileToFileWithFlatternTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date