You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2012/08/16 11:31:27 UTC

svn commit: r1373764 - in /camel/branches/camel-2.10.x: ./ camel-core/src/main/java/org/apache/camel/component/file/ camel-core/src/test/java/org/apache/camel/component/file/

Author: davsclaus
Date: Thu Aug 16 09:31:27 2012
New Revision: 1373764

URL: http://svn.apache.org/viewvc?rev=1373764&view=rev
Log:
CAMEL-5407 Better error message when trying to write a null body as file

Added:
    camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyFileAlreadyExistsTest.java
      - copied unchanged from r1372259, camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyFileAlreadyExistsTest.java
    camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyTest.java
      - copied unchanged from r1372259, camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyTest.java
Modified:
    camel/branches/camel-2.10.x/   (props changed)
    camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
    camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java

Propchange: camel/branches/camel-2.10.x/
------------------------------------------------------------------------------
  Merged /camel/trunk:r1372259

Propchange: camel/branches/camel-2.10.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java?rev=1373764&r1=1373763&r2=1373764&view=diff
==============================================================================
--- camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java (original)
+++ camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java Thu Aug 16 09:31:27 2012
@@ -160,7 +160,7 @@ public class FileOperations implements G
         ObjectHelper.notNull(endpoint, "endpoint");
 
         File file = new File(fileName);
-
+        
         // if an existing file already exists what should we do?
         if (file.exists()) {
             if (endpoint.getFileExist() == GenericFileExist.Ignore) {
@@ -169,6 +169,21 @@ public class FileOperations implements G
                 return true;
             } else if (endpoint.getFileExist() == GenericFileExist.Fail) {
                 throw new GenericFileOperationFailedException("File already exist: " + file + ". Cannot write new file.");
+            } 
+        }
+        
+        // Do an explicit test for a null body and decide what to do
+        if (exchange.getIn().getBody() == null) {
+            if (endpoint.isAllowNullBody()) {
+                LOG.trace("The in message of exchange body was null.");
+                try {
+                    writeFileEmptyBody(file);
+                    return true;
+                } catch (IOException e) {
+                    throw new GenericFileOperationFailedException("Cannot store file: " + file, e);
+                }
+            } else {
+                throw new GenericFileOperationFailedException("Cannot write null body to file.");
             }
         }
 
@@ -326,6 +341,25 @@ public class FileOperations implements G
             IOHelper.close(out, target.getName(), LOG);
         }
     }
+    
+    /**
+     * Creates a new file if the file doesn't exist.
+     * If the endpoint's existing file logic is set to 'Override' then the target file will be truncated
+     */
+    private void writeFileEmptyBody(File target) throws IOException {
+        if (!target.exists()) {
+            target.createNewFile();
+        } else if (endpoint.getFileExist() == GenericFileExist.Override) {
+            LOG.trace("Truncating file as it already exists and endpoint set to Override file.");
+            FileChannel out = new FileOutputStream(target).getChannel();
+            try {
+                out.truncate(0);
+            } finally {
+                IOHelper.force(out, target.getName(), LOG);
+                IOHelper.close(out, target.getName(), LOG);
+            }
+        }
+    }
 
     /**
      * Creates and prepares the output file channel. Will position itself in correct position if the file is writable

Modified: camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java?rev=1373764&r1=1373763&r2=1373764&view=diff
==============================================================================
--- camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java (original)
+++ camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java Thu Aug 16 09:31:27 2012
@@ -97,6 +97,7 @@ public abstract class GenericFileEndpoin
     protected GenericFileExclusiveReadLockStrategy<T> exclusiveReadLockStrategy;
     protected boolean keepLastModified;
     protected String doneFileName;
+    protected boolean allowNullBody;
 
     public GenericFileEndpoint() {
     }
@@ -625,6 +626,14 @@ public abstract class GenericFileEndpoin
         this.keepLastModified = keepLastModified;
     }
 
+    public boolean isAllowNullBody() {
+        return allowNullBody;
+    }
+    
+    public void setAllowNullBody(boolean allowNullBody) {
+        this.allowNullBody = allowNullBody;
+    }
+    
     /**
      * Configures the given message with the file which sets the body to the
      * file object.