This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 00cb0f2ebc887026a521b41ee614c7d81123bb48
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Tue Dec 26 12:01:53 2017 +0100

    CAMEL-12094: file/ftp producer fixed so moveExisting works when using temp 
filename as well.
---
 .../camel/component/file/GenericFileProducer.java  | 29 +++++++++++++++++++++-
 .../file/FileProducerMoveExistingTest.java         | 13 ++++++++++
 .../file/remote/FtpProducerMoveExistingTest.java   | 12 +++++++++
 3 files changed, 53 insertions(+), 1 deletion(-)

diff --git 
a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
 
b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
index 5876cb1..ee40b91 100644
--- 
a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
+++ 
b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
@@ -117,7 +117,7 @@ public class GenericFileProducer<T> extends DefaultProducer 
{
             boolean writeAsTempAndRename = 
ObjectHelper.isNotEmpty(endpoint.getTempFileName());
             String tempTarget = null;
             // remember if target exists to avoid checking twice
-            Boolean targetExists = null;
+            Boolean targetExists;
             if (writeAsTempAndRename) {
                 // compute temporary name with the temp prefix
                 tempTarget = createTempFileName(exchange, target);
@@ -142,6 +142,9 @@ public class GenericFileProducer<T> extends DefaultProducer 
{
                             return;
                         } else if (endpoint.getFileExist() == 
GenericFileExist.Fail) {
                             throw new 
GenericFileOperationFailedException("File already exist: " + target + ". Cannot 
write new file.");
+                        } else if (endpoint.getFileExist() == 
GenericFileExist.Move) {
+                            // move any existing file first
+                            doMoveExistingFile(target);
                         } else if (endpoint.isEagerDeleteTargetFile() && 
endpoint.getFileExist() == GenericFileExist.Override) {
                             // we override the target so we do this by 
deleting it so the temp file can be renamed later
                             // with success as the existing target file have 
been deleted
@@ -231,6 +234,30 @@ public class GenericFileProducer<T> extends 
DefaultProducer {
         postWriteCheck(exchange);
     }
 
+    private void doMoveExistingFile(String fileName) throws 
GenericFileOperationFailedException {
+        // need to evaluate using a dummy and simulate the file first, to have 
access to all the file attributes
+        // create a dummy exchange as Exchange is needed for expression 
evaluation
+        // we support only the following 3 tokens.
+        Exchange dummy = endpoint.createExchange();
+        String parent = FileUtil.onlyPath(fileName);
+        String onlyName = FileUtil.stripPath(fileName);
+        dummy.getIn().setHeader(Exchange.FILE_NAME, fileName);
+        dummy.getIn().setHeader(Exchange.FILE_NAME_ONLY, onlyName);
+        dummy.getIn().setHeader(Exchange.FILE_PARENT, parent);
+
+        String to = endpoint.getMoveExisting().evaluate(dummy, String.class);
+        // we must normalize it (to avoid having both \ and / in the name 
which confuses java.io.File)
+        to = FileUtil.normalizePath(to);
+        if (ObjectHelper.isEmpty(to)) {
+            throw new GenericFileOperationFailedException("moveExisting 
evaluated as empty String, cannot move existing file: " + fileName);
+        }
+
+        boolean renamed = operations.renameFile(fileName, to);
+        if (!renamed) {
+            throw new GenericFileOperationFailedException("Cannot rename file 
from: " + fileName + " to: " + to);
+        }
+    }
+
     /**
      * If we fail writing out a file, we will call this method. This hook is
      * provided to disconnect from servers or clean up files we created (if 
needed).
diff --git 
a/camel-core/src/test/java/org/apache/camel/component/file/FileProducerMoveExistingTest.java
 
b/camel-core/src/test/java/org/apache/camel/component/file/FileProducerMoveExistingTest.java
index 947c868..a3f833e 100644
--- 
a/camel-core/src/test/java/org/apache/camel/component/file/FileProducerMoveExistingTest.java
+++ 
b/camel-core/src/test/java/org/apache/camel/component/file/FileProducerMoveExistingTest.java
@@ -54,6 +54,19 @@ public class FileProducerMoveExistingTest extends 
ContextTestSupport {
         assertEquals("Hello World", 
context.getTypeConverter().convertTo(String.class, new 
File("target/file/renamed-hello.txt")));
     }
 
+    public void testExistingFileExistsTempFileName() throws Exception {
+        
template.sendBodyAndHeader("file://target/file?tempFileName=${file:onlyname}.temp&fileExist=Move&moveExisting=${file:parent}/renamed-${file:onlyname}",
+                "Hello World", Exchange.FILE_NAME, "hello.txt");
+        
template.sendBodyAndHeader("file://target/file?tempFileName=${file:onlyname}.temp&fileExist=Move&moveExisting=${file:parent}/renamed-${file:onlyname}",
+                "Bye World", Exchange.FILE_NAME, "hello.txt");
+
+        assertFileExists("target/file/hello.txt");
+        assertEquals("Bye World", 
context.getTypeConverter().convertTo(String.class, new 
File("target/file/hello.txt")));
+
+        assertFileExists("target/file/renamed-hello.txt");
+        assertEquals("Hello World", 
context.getTypeConverter().convertTo(String.class, new 
File("target/file/renamed-hello.txt")));
+    }
+
     public void testExistingFileExistsMoveSubDir() throws Exception {
         
template.sendBodyAndHeader("file://target/file?fileExist=Move&moveExisting=backup",
 "Hello World", Exchange.FILE_NAME, "hello.txt");
         
template.sendBodyAndHeader("file://target/file?fileExist=Move&moveExisting=backup",
 "Bye World", Exchange.FILE_NAME, "hello.txt");
diff --git 
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerMoveExistingTest.java
 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerMoveExistingTest.java
index 2e3201a..f610b0b 100644
--- 
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerMoveExistingTest.java
+++ 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerMoveExistingTest.java
@@ -53,6 +53,18 @@ public class FtpProducerMoveExistingTest extends 
FtpServerTestSupport {
     }
 
     @Test
+    public void testExistingFileExistsTempFilename() throws Exception {
+        template.sendBodyAndHeader(getFtpUrl() + 
"&tempFileName=${file:onlyname}.temp&moveExisting=${file:parent}/renamed-${file:onlyname}",
 "Hello World", Exchange.FILE_NAME, "hello.txt");
+        template.sendBodyAndHeader(getFtpUrl() + 
"&tempFileName=${file:onlyname}.temp&moveExisting=${file:parent}/renamed-${file:onlyname}",
 "Bye World", Exchange.FILE_NAME, "hello.txt");
+
+        assertFileExists(FTP_ROOT_DIR + "/move/hello.txt");
+        assertEquals("Bye World", 
context.getTypeConverter().convertTo(String.class, new File(FTP_ROOT_DIR + 
"/move/hello.txt")));
+
+        assertFileExists(FTP_ROOT_DIR + "/move/renamed-hello.txt");
+        assertEquals("Hello World", 
context.getTypeConverter().convertTo(String.class, new File(FTP_ROOT_DIR + 
"/move/renamed-hello.txt")));
+    }
+
+    @Test
     public void testExistingFileExistsMoveSubDir() throws Exception {
         template.sendBodyAndHeader(getFtpUrl() + "&moveExisting=backup", 
"Hello World", Exchange.FILE_NAME, "hello.txt");
         template.sendBodyAndHeader(getFtpUrl() + "&moveExisting=backup", "Bye 
World", Exchange.FILE_NAME, "hello.txt");

-- 
To stop receiving notification emails like this one, please contact
"commits@camel.apache.org" <commits@camel.apache.org>.

Reply via email to