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:30:05 UTC
svn commit: r1373763 - in /camel/trunk:
camel-core/src/main/java/org/apache/camel/component/file/
camel-core/src/test/java/org/apache/camel/component/file/
components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/
components/camel-ftp/...
Author: davsclaus
Date: Thu Aug 16 09:30:05 2012
New Revision: 1373763
URL: http://svn.apache.org/viewvc?rev=1373763&view=rev
Log:
CAMEL-5407: Added option allowNullBody to ftp producer to allow writing empty files easier.
Added:
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerAllowNullBodyFileAlreadyExistTest.java (with props)
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerAllowNullBodyTest.java
- copied, changed from r1373754, camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerDisconnectTest.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
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
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/FileOperations.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java?rev=1373763&r1=1373762&r2=1373763&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 Thu Aug 16 09:30:05 2012
@@ -175,7 +175,7 @@ public class FileOperations implements G
// 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.");
+ LOG.trace("Writing empty file.");
try {
writeFileEmptyBody(file);
return true;
@@ -183,7 +183,7 @@ public class FileOperations implements G
throw new GenericFileOperationFailedException("Cannot store file: " + file, e);
}
} else {
- throw new GenericFileOperationFailedException("Cannot write null body to file.");
+ throw new GenericFileOperationFailedException("Cannot write null body to file: " + file);
}
}
@@ -348,9 +348,10 @@ public class FileOperations implements G
*/
private void writeFileEmptyBody(File target) throws IOException {
if (!target.exists()) {
+ LOG.debug("Creating new empty file: {}", target);
target.createNewFile();
} else if (endpoint.getFileExist() == GenericFileExist.Override) {
- LOG.trace("Truncating file as it already exists and endpoint set to Override file.");
+ LOG.debug("Truncating existing file: {}", target);
FileChannel out = new FileOutputStream(target).getChannel();
try {
out.truncate(0);
Modified: 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=1373763&r1=1373762&r2=1373763&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyFileAlreadyExistsTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyFileAlreadyExistsTest.java Thu Aug 16 09:30:05 2012
@@ -27,6 +27,7 @@ import org.apache.camel.component.mock.M
* 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");
Modified: 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=1373763&r1=1373762&r2=1373763&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileProducerAllowNullBodyTest.java Thu Aug 16 09:30:05 2012
@@ -45,7 +45,7 @@ public class FileProducerAllowNullBodyTe
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());
+ assertTrue(cause.getMessage().endsWith("allowNullBody.txt"));
}
assertFalse("allowNullBody set to false with null body should not create a new file", new File("./target/allow/allowNullBody.txt").exists());
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=1373763&r1=1373762&r2=1373763&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 Thu Aug 16 09:30:05 2012
@@ -16,6 +16,7 @@
*/
package org.apache.camel.component.file.remote;
+import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
@@ -498,8 +499,20 @@ public class FtpOperations implements Re
}
InputStream is = null;
+ if (exchange.getIn().getBody() == null) {
+ // Do an explicit test for a null body and decide what to do
+ if (endpoint.isAllowNullBody()) {
+ log.trace("Writing empty file.");
+ is = new ByteArrayInputStream(new byte[]{});
+ } else {
+ throw new GenericFileOperationFailedException("Cannot write null body to file: " + name);
+ }
+ }
+
try {
- is = exchange.getIn().getMandatoryBody(InputStream.class);
+ if (is == null) {
+ is = exchange.getIn().getMandatoryBody(InputStream.class);
+ }
if (endpoint.getFileExist() == GenericFileExist.Append) {
log.trace("Client appendFile: {}", targetName);
return client.appendFile(targetName, is);
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=1373763&r1=1373762&r2=1373763&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 Thu Aug 16 09:30:05 2012
@@ -16,6 +16,7 @@
*/
package org.apache.camel.component.file.remote;
+import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
@@ -672,11 +673,25 @@ public class SftpOperations implements R
}
InputStream is = null;
+ if (exchange.getIn().getBody() == null) {
+ // Do an explicit test for a null body and decide what to do
+ if (endpoint.isAllowNullBody()) {
+ LOG.trace("Writing empty file.");
+ is = new ByteArrayInputStream(new byte[]{});
+ } else {
+ throw new GenericFileOperationFailedException("Cannot write null body to file: " + name);
+ }
+ }
+
try {
- is = exchange.getIn().getMandatoryBody(InputStream.class);
+ if (is == null) {
+ is = exchange.getIn().getMandatoryBody(InputStream.class);
+ }
if (endpoint.getFileExist() == GenericFileExist.Append) {
+ LOG.trace("Client appendFile: {}", targetName);
channel.put(is, targetName, ChannelSftp.APPEND);
} else {
+ LOG.trace("Client storeFile: {}", targetName);
// override is default
channel.put(is, targetName);
}
Added: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerAllowNullBodyFileAlreadyExistTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerAllowNullBodyFileAlreadyExistTest.java?rev=1373763&view=auto
==============================================================================
--- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerAllowNullBodyFileAlreadyExistTest.java (added)
+++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerAllowNullBodyFileAlreadyExistTest.java Thu Aug 16 09:30:05 2012
@@ -0,0 +1,75 @@
+/**
+ * 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 org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+public class FtpProducerAllowNullBodyFileAlreadyExistTest extends FtpServerTestSupport {
+
+ private String getFtpUrl() {
+ return "ftp://admin@localhost:" + getPort() + "/allow?password=admin";
+ }
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ template.sendBodyAndHeader(getFtpUrl(), "Hello world", Exchange.FILE_NAME, "hello.txt");
+ }
+
+ @Test
+ public void testFileExistAppendAllowNullBody() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:appendTypeAppendResult");
+ mock.expectedMessageCount(1);
+ mock.expectedFileExists(FTP_ROOT_DIR + "/allow/hello.txt", "Hello world");
+
+ template.sendBody("direct:appendTypeAppend", null);
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ public void testFileExistOverrideAllowNullBody() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:appendTypeOverrideResult");
+ mock.expectedMessageCount(1);
+ mock.expectedFileExists(FTP_ROOT_DIR + "/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(getFtpUrl() + "&allowNullBody=true&fileExist=Append")
+ .to("mock:appendTypeAppendResult");
+
+ from("direct:appendTypeOverride")
+ .setHeader(Exchange.FILE_NAME, constant("hello.txt"))
+ .to(getFtpUrl() + "&allowNullBody=true&fileExist=Override")
+ .to("mock:appendTypeOverrideResult");
+ }
+ };
+ }
+
+}
\ No newline at end of file
Propchange: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerAllowNullBodyFileAlreadyExistTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerAllowNullBodyFileAlreadyExistTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Copied: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerAllowNullBodyTest.java (from r1373754, camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerDisconnectTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerAllowNullBodyTest.java?p2=camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerAllowNullBodyTest.java&p1=camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerDisconnectTest.java&r1=1373754&r2=1373763&rev=1373763&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerDisconnectTest.java (original)
+++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerAllowNullBodyTest.java Thu Aug 16 09:30:05 2012
@@ -16,17 +16,36 @@
*/
package org.apache.camel.component.file.remote;
+import java.io.File;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.component.file.GenericFileOperationFailedException;
import org.junit.Test;
-public class FtpProducerDisconnectTest extends FtpServerTestSupport {
+public class FtpProducerAllowNullBodyTest extends FtpServerTestSupport {
private String getFtpUrl() {
- return "ftp://admin@localhost:" + getPort() + "/done?password=admin&disconnect=true";
+ return "ftp://admin@localhost:" + getPort() + "/allownull?password=admin&fileName=allowNullBody.txt";
+ }
+
+ @Test
+ public void testAllowNullBodyTrue() throws Exception {
+ template.sendBody(getFtpUrl() + "&allowNullBody=true", null);
+
+ assertFileExists(FTP_ROOT_DIR + "/allownull/allowNullBody.txt");
}
@Test
- public void testDisconnectOnDone() throws Exception {
- sendFile(getFtpUrl(), "Hello World", "claus.txt");
+ public void testAllowNullBodyFalse() throws Exception {
+ try {
+ template.sendBody(getFtpUrl() + "&allowNullBody=false", null);
+ fail("Should have thrown a GenericFileOperationFailedException");
+ } catch (CamelExecutionException e) {
+ GenericFileOperationFailedException cause = assertIsInstanceOf(GenericFileOperationFailedException.class, e.getCause());
+ assertTrue(cause.getMessage().endsWith("allowNullBody.txt"));
+ }
+
+ assertFalse("allowNullBody set to false with null body should not create a new file", new File(FTP_ROOT_DIR + "/allownull/allowNullBody.txt").exists());
}
}
\ No newline at end of file