Author: davsclaus
Date: Fri Oct 23 10:42:01 2009
New Revision: 828994

URL: http://svn.apache.org/viewvc?rev=828994&view=rev
Log:
CAMEL-2058: Various fixes to using tempPrefix with file/ftp component. Better 
deleting/renaming files that cater for Windows OS.

Added:
    
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerTempFileExistsIssueTest.java
   (with props)
    
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProdcerTempFileExistIssueTest.java
   (with props)
Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.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/SftpOperations.java

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java?rev=828994&r1=828993&r2=828994&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
 Fri Oct 23 10:42:01 2009
@@ -91,6 +91,12 @@
 
     public GenericFileProducer<File> createProducer() throws Exception {
         ObjectHelper.notNull(operations, "operations");
+
+        // you cannot use temp prefix and file exists append
+        if (getFileExist() == GenericFileExist.Append && getTempPrefix() != 
null) {
+            throw new IllegalArgumentException("You cannot set both 
fileExist=Append and tempPrefix options");
+        }
+
         return new GenericFileProducer<File>(this, operations);
     }
 

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=828994&r1=828993&r2=828994&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
 Fri Oct 23 10:42:01 2009
@@ -80,9 +80,40 @@
             if (writeAsTempAndRename) {
                 // compute temporary name with the temp prefix
                 tempTarget = createTempFileName(target);
+
+                // cater for file exists option on the real target as
+                // the file operations code will work on the temp file
+
+                // if an existing file already exists what should we do?
+                if (operations.existsFile(target)) {
+                    if (endpoint.getFileExist() == GenericFileExist.Ignore) {
+                        // ignore but indicate that the file was written
+                        if (log.isTraceEnabled()) {
+                            log.trace("An existing file already exists: " + 
target + ". Ignore and do not override it.");
+                        }
+                        return;
+                    } else if (endpoint.getFileExist() == 
GenericFileExist.Fail) {
+                        throw new GenericFileOperationFailedException("File 
already exist: " + target + ". Cannot write new file.");
+                    } else if (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
+                        if (log.isTraceEnabled()) {
+                            log.trace("Deleting existing file: " + tempTarget);
+                        }
+                        operations.deleteFile(target);
+                    }
+                }
+
+                // delete any pre existing temp file
+                if (operations.existsFile(tempTarget)) {
+                    if (log.isTraceEnabled()) {
+                        log.trace("Deleting existing temp file: " + 
tempTarget);
+                    }
+                    operations.deleteFile(tempTarget);
+                }
             }
 
-            // upload the file
+            // write/upload the file
             writeFile(exchange, tempTarget != null ? tempTarget : target);
 
             // if we did write to a temporary name then rename it to the real

Added: 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerTempFileExistsIssueTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerTempFileExistsIssueTest.java?rev=828994&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerTempFileExistsIssueTest.java
 (added)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerTempFileExistsIssueTest.java
 Fri Oct 23 10:42:01 2009
@@ -0,0 +1,130 @@
+/**
+ * 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 java.io.File;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+
+/**
+ * @version $Revision$
+ */
+public class FileProducerTempFileExistsIssueTest extends ContextTestSupport {
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    public void testIllegalConfiguration() throws Exception {
+        try {
+            
context.getEndpoint("file://target/tempprefix?fileExist=Append&tempPrefix=foo").createProducer();
+        } catch (IllegalArgumentException e) {
+            assertEquals("You cannot set both fileExist=Append and tempPrefix 
options", e.getMessage());
+        }
+    }
+
+    public void testWriteUsingTempPrefixButFileExist() throws Exception {
+        deleteDirectory("target/tempprefix");
+
+        template.sendBodyAndHeader("file://target/tempprefix", "Hello World", 
Exchange.FILE_NAME, "hello.txt");
+
+        Thread.sleep(200);
+
+        template.sendBodyAndHeader("file://target/tempprefix?tempPrefix=foo", 
"Bye World", Exchange.FILE_NAME, "hello.txt");
+
+        Thread.sleep(200);
+
+        File file = new File("target/tempprefix/hello.txt").getAbsoluteFile();
+        assertEquals(true, file.exists());
+        assertEquals("Bye World", 
context.getTypeConverter().convertTo(String.class, file));
+    }
+
+    public void testWriteUsingTempPrefixButBothFileExist() throws Exception {
+        deleteDirectory("target/tempprefix");
+
+        template.sendBodyAndHeader("file://target/tempprefix", "Hello World", 
Exchange.FILE_NAME, "hello.txt");
+        template.sendBodyAndHeader("file://target/tempprefix", "Hello World", 
Exchange.FILE_NAME, "foohello.txt");
+
+        Thread.sleep(200);
+
+        template.sendBodyAndHeader("file://target/tempprefix?tempPrefix=foo", 
"Bye World", Exchange.FILE_NAME, "hello.txt");
+
+        Thread.sleep(200);
+
+        File file = new File("target/tempprefix/hello.txt").getAbsoluteFile();
+        assertEquals(true, file.exists());
+        assertEquals("Bye World", 
context.getTypeConverter().convertTo(String.class, file));
+    }
+
+    public void testWriteUsingTempPrefixButFileExistOverride() throws 
Exception {
+        deleteDirectory("target/tempprefix");
+
+        template.sendBodyAndHeader("file://target/tempprefix", "Hello World", 
Exchange.FILE_NAME, "hello.txt");
+
+        Thread.sleep(200);
+
+        
template.sendBodyAndHeader("file://target/tempprefix?tempPrefix=foo&fileExist=Override",
 "Bye World", Exchange.FILE_NAME, "hello.txt");
+
+        Thread.sleep(200);
+
+        File file = new File("target/tempprefix/hello.txt").getAbsoluteFile();
+        assertEquals(true, file.exists());
+        assertEquals("Bye World", 
context.getTypeConverter().convertTo(String.class, file));
+    }
+
+    public void testWriteUsingTempPrefixButFileExistIgnore() throws Exception {
+        deleteDirectory("target/tempprefix");
+
+        template.sendBodyAndHeader("file://target/tempprefix", "Hello World", 
Exchange.FILE_NAME, "hello.txt");
+
+        Thread.sleep(200);
+
+        
template.sendBodyAndHeader("file://target/tempprefix?tempPrefix=foo&fileExist=Ignore",
 "Bye World", Exchange.FILE_NAME, "hello.txt");
+
+        Thread.sleep(200);
+
+        File file = new File("target/tempprefix/hello.txt").getAbsoluteFile();
+        assertEquals(true, file.exists());
+        // should not write new file as we should ignore
+        assertEquals("Hello World", 
context.getTypeConverter().convertTo(String.class, file));
+    }
+
+    public void testWriteUsingTempPrefixButFileExistFail() throws Exception {
+        deleteDirectory("target/tempprefix");
+
+        template.sendBodyAndHeader("file://target/tempprefix", "Hello World", 
Exchange.FILE_NAME, "hello.txt");
+
+        Thread.sleep(200);
+
+        try {
+            
template.sendBodyAndHeader("file://target/tempprefix?tempPrefix=foo&fileExist=Fail",
 "Bye World", Exchange.FILE_NAME, "hello.txt");
+            fail("Should have thrown an exception");
+        } catch (GenericFileOperationFailedException e) {
+            assertTrue(e.getMessage().startsWith("File already exist"));
+        }
+
+        Thread.sleep(200);
+
+        File file = new File("target/tempprefix/hello.txt").getAbsoluteFile();
+        assertEquals(true, file.exists());
+
+        // should not write new file as we should fail
+        assertEquals("Hello World", 
context.getTypeConverter().convertTo(String.class, file));
+    }
+}

Propchange: 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerTempFileExistsIssueTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerTempFileExistsIssueTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

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=828994&r1=828993&r2=828994&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
 Fri Oct 23 10:42:01 2009
@@ -205,7 +205,7 @@
 
             boolean success;
             try {
-                // maybe the full directory already exsits
+                // maybe the full directory already exists
                 success = client.changeWorkingDirectory(directory);
                 if (!success) {
                     if (LOG.isTraceEnabled()) {
@@ -273,12 +273,12 @@
 
             // delete any existing files
             if (temp.exists()) {
-                if (!temp.delete()) {
+                if (!FileUtil.deleteFile(temp)) {
                     throw new GenericFileOperationFailedException("Cannot 
delete existing local work file: " + temp);
                 }
             }
             if (local.exists()) {
-                if (!local.delete()) {
+                if (!FileUtil.deleteFile(local)) {
                     throw new GenericFileOperationFailedException("Cannot 
delete existing local work file: " + local);
                 }                
             }
@@ -310,13 +310,12 @@
         }  finally {
             // need to close the stream before rename it
             ObjectHelper.close(os, "retrieve: " + name, LOG);
-        }   
-            
+        }
+
         // rename temp to local after we have retrieved the data
-        if (!temp.renameTo(local)) {                
+        if (!FileUtil.renameFile(temp, local)) {
             throw new GenericFileOperationFailedException("Cannot rename local 
work file from: " + temp + " to: " + local);
         }
-        
 
         return result;
     }

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=828994&r1=828993&r2=828994&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
 Fri Oct 23 10:42:01 2009
@@ -346,12 +346,12 @@
 
             // delete any existing files
             if (temp.exists()) {
-                if (!temp.delete()) {
+                if (!FileUtil.deleteFile(temp)) {
                     throw new GenericFileOperationFailedException("Cannot 
delete existing local work file: " + temp);
                 }
             }
             if (local.exists()) {
-                if (!local.delete()) {
+                if (!FileUtil.deleteFile(local)) {
                     throw new GenericFileOperationFailedException("Cannot 
delete existing local work file: " + local);
                 }
             }
@@ -376,7 +376,7 @@
             channel.get(name, os);
 
             // rename temp to local after we have retrieved the data
-            if (!temp.renameTo(local)) {
+            if (!FileUtil.renameFile(temp, local)) {
                 throw new GenericFileOperationFailedException("Cannot rename 
local work file from: " + temp + " to: " + local);
             }
         } catch (SftpException e) {

Added: 
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProdcerTempFileExistIssueTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProdcerTempFileExistIssueTest.java?rev=828994&view=auto
==============================================================================
--- 
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProdcerTempFileExistIssueTest.java
 (added)
+++ 
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProdcerTempFileExistIssueTest.java
 Fri Oct 23 10:42:01 2009
@@ -0,0 +1,139 @@
+/**
+ * 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.remote;
+
+import java.io.File;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.component.file.GenericFileOperationFailedException;
+import org.junit.Test;
+
+/**
+ * @version $Revision$
+ */
+public class FtpProdcerTempFileExistIssueTest extends FtpServerTestSupport {
+
+    private String getFtpUrl() {
+        return "ftp://ad...@localhost:"; + getPort() + 
"/tempprefix/?password=admin";
+    }
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    @Test
+    public void testIllegalConfiguration() throws Exception {
+        try {
+            context.getEndpoint(getFtpUrl() + 
"&fileExist=Append&tempPrefix=foo").createProducer();
+        } catch (IllegalArgumentException e) {
+            assertEquals("You cannot set both fileExist=Append and tempPrefix 
options", e.getMessage());
+        }
+    }
+
+    @Test
+    public void testWriteUsingTempPrefixButFileExist() throws Exception {
+        deleteDirectory(FTP_ROOT_DIR + "tempprefix");
+
+        template.sendBodyAndHeader(getFtpUrl(), "Hello World", 
Exchange.FILE_NAME, "hello.txt");
+
+        Thread.sleep(500);
+
+        template.sendBodyAndHeader(getFtpUrl() + "&tempPrefix=foo", "Bye 
World", Exchange.FILE_NAME, "hello.txt");
+
+        Thread.sleep(500);
+
+        File file = new File(FTP_ROOT_DIR + 
"tempprefix/hello.txt").getAbsoluteFile();
+        assertEquals(true, file.exists());
+        assertEquals("Bye World", 
context.getTypeConverter().convertTo(String.class, file));
+    }
+
+    @Test
+    public void testWriteUsingTempPrefixButBothFileExist() throws Exception {
+        deleteDirectory(FTP_ROOT_DIR + "tempprefix");
+
+        template.sendBodyAndHeader(getFtpUrl(), "Hello World", 
Exchange.FILE_NAME, "hello.txt");
+        template.sendBodyAndHeader(getFtpUrl(), "Hello World", 
Exchange.FILE_NAME, "foohello.txt");
+
+        Thread.sleep(500);
+
+        template.sendBodyAndHeader(getFtpUrl() + "&tempPrefix=foo", "Bye 
World", Exchange.FILE_NAME, "hello.txt");
+
+        Thread.sleep(500);
+
+        File file = new File(FTP_ROOT_DIR + 
"tempprefix/hello.txt").getAbsoluteFile();
+        assertEquals(true, file.exists());
+        assertEquals("Bye World", 
context.getTypeConverter().convertTo(String.class, file));
+    }
+
+    @Test
+    public void testWriteUsingTempPrefixButFileExistOverride() throws 
Exception {
+        deleteDirectory(FTP_ROOT_DIR + "tempprefix");
+
+        template.sendBodyAndHeader(getFtpUrl(), "Hello World", 
Exchange.FILE_NAME, "hello.txt");
+
+        Thread.sleep(500);
+
+        template.sendBodyAndHeader(getFtpUrl() + 
"&tempPrefix=foo&fileExist=Override", "Bye World", Exchange.FILE_NAME, 
"hello.txt");
+
+        Thread.sleep(500);
+
+        File file = new File(FTP_ROOT_DIR + 
"tempprefix/hello.txt").getAbsoluteFile();
+        assertEquals(true, file.exists());
+        assertEquals("Bye World", 
context.getTypeConverter().convertTo(String.class, file));
+    }
+
+    @Test
+    public void testWriteUsingTempPrefixButFileExistIgnore() throws Exception {
+        deleteDirectory(FTP_ROOT_DIR + "tempprefix");
+
+        template.sendBodyAndHeader(getFtpUrl(), "Hello World", 
Exchange.FILE_NAME, "hello.txt");
+
+        Thread.sleep(500);
+
+        template.sendBodyAndHeader(getFtpUrl() + 
"&tempPrefix=foo&fileExist=Ignore", "Bye World", Exchange.FILE_NAME, 
"hello.txt");
+
+        Thread.sleep(500);
+
+        File file = new File(FTP_ROOT_DIR + 
"tempprefix/hello.txt").getAbsoluteFile();
+        // should not write new file as we should ignore
+        assertEquals("Hello World", 
context.getTypeConverter().convertTo(String.class, file));
+    }
+
+    @Test
+    public void testWriteUsingTempPrefixButFileExistFail() throws Exception {
+        deleteDirectory(FTP_ROOT_DIR + "tempprefix");
+
+        template.sendBodyAndHeader(getFtpUrl(), "Hello World", 
Exchange.FILE_NAME, "hello.txt");
+
+        Thread.sleep(500);
+
+        try {
+            template.sendBodyAndHeader(getFtpUrl() + 
"&tempPrefix=foo&fileExist=Fail", "Bye World", Exchange.FILE_NAME, "hello.txt");
+            fail("Should have thrown an exception");
+        } catch (GenericFileOperationFailedException e) {
+            assertTrue(e.getMessage().startsWith("File already exist"));
+        }
+
+        Thread.sleep(500);
+
+        File file = new File(FTP_ROOT_DIR + 
"tempprefix/hello.txt").getAbsoluteFile();
+        // should not write new file as we should ignore
+        assertEquals("Hello World", 
context.getTypeConverter().convertTo(String.class, file));
+    }
+
+}

Propchange: 
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProdcerTempFileExistIssueTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProdcerTempFileExistIssueTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date


Reply via email to