Author: ningjiang
Date: Mon Aug 13 04:27:07 2012
New Revision: 1372259
URL: http://svn.apache.org/viewvc?rev=1372259&view=rev
Log:
CAMEL-5407 Better error message when trying to write a null body as file
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyFileAlreadyExistsTest.java
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyTest.java
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/GenericFileEndpoint.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=1372259&r1=1372258&r2=1372259&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
Mon Aug 13 04:27:07 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/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=1372259&r1=1372258&r2=1372259&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
Mon Aug 13 04:27:07 2012
@@ -96,6 +96,7 @@ public abstract class GenericFileEndpoin
protected GenericFileExclusiveReadLockStrategy<T>
exclusiveReadLockStrategy;
protected boolean keepLastModified;
protected String doneFileName;
+ protected boolean allowNullBody;
public GenericFileEndpoint() {
}
@@ -616,6 +617,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.
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyFileAlreadyExistsTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyFileAlreadyExistsTest.java?rev=1372259&view=auto
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyFileAlreadyExistsTest.java
(added)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyFileAlreadyExistsTest.java
Mon Aug 13 04:27:07 2012
@@ -0,0 +1,74 @@
+/**
+ * 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;
+
+/**
+ * Unit tests to ensure that when the option allowNullBody is set to true then
+ * If the fileExist option is set to Append the file's contents will not be
modified
+ * If the fileExist option is set to Override the file's contents will be empty
+ */
+public class FileProducerAllowNullBodyFileAlreadyExistsTest extends
ContextTestSupport {
+ @Override
+ protected void setUp() throws Exception {
+ deleteDirectory("target/allow");
+ super.setUp();
+ template.sendBodyAndHeader("file://target/allow", "Hello world",
Exchange.FILE_NAME, "hello.txt");
+ }
+
+ public void testFileExistAppendAllowNullBody() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:appendTypeAppendResult");
+ mock.expectedMessageCount(1);
+ mock.expectedFileExists("target/allow/hello.txt", "Hello world");
+
+ template.sendBody("direct:appendTypeAppend", null);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ public void testFileExistOverrideAllowNullBody() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:appendTypeOverrideResult");
+ mock.expectedMessageCount(1);
+ mock.expectedFileExists("target/allow/hello.txt", "");
+
+ template.sendBody("direct:appendTypeOverride", null);
+
+ assertMockEndpointsSatisfied();
+ }
+
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ public void configure() {
+ from("direct:appendTypeAppend")
+ .setHeader(Exchange.FILE_NAME, constant("hello.txt"))
+
.to("file://target/allow?allowNullBody=true&fileExist=Append")
+ .to("mock:appendTypeAppendResult");
+
+ from("direct:appendTypeOverride")
+ .setHeader(Exchange.FILE_NAME, constant("hello.txt"))
+
.to("file://target/allow?allowNullBody=true&fileExist=Override")
+ .to("mock:appendTypeOverrideResult");
+ }
+ };
+ }
+}
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyTest.java?rev=1372259&view=auto
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyTest.java
(added)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyTest.java
Mon Aug 13 04:27:07 2012
@@ -0,0 +1,53 @@
+/**
+ * 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.CamelExecutionException;
+import org.apache.camel.ContextTestSupport;
+
+/**
+ * Unit tests to ensure that
+ * When the allowNullBody option is set to true it will create an empty file
and not throw an exception
+ * When the allowNullBody option is set to false it will throw an exception of
"Cannot write null body to file."
+ */
+public class FileProducerAllowNullBodyTest extends ContextTestSupport {
+
+ @Override
+ protected void setUp() throws Exception {
+ deleteDirectory("target/allow");
+ super.setUp();
+ }
+
+ public void testAllowNullBodyTrue() throws Exception {
+
template.sendBody("file://target/allow?allowNullBody=true&fileName=allowNullBody.txt",
null);
+ assertFileExists("./target/allow/allowNullBody.txt");
+ }
+
+ public void testAllowNullBodyFalse() throws Exception {
+ try {
+
template.sendBody("file://target/allow?fileName=allowNullBody.txt", null);
+ fail("Should have thrown a GenericFileOperationFailedException");
+ } catch (CamelExecutionException e) {
+ GenericFileOperationFailedException cause =
assertIsInstanceOf(GenericFileOperationFailedException.class, e.getCause());
+ assertEquals("Cannot write null body to file.",
cause.getMessage());
+ }
+
+ assertFalse("allowNullBody set to false with null body should not
create a new file", new File("./target/allow/allowNullBody.txt").exists());
+ }
+}