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 2021/03/22 18:01:56 UTC

[camel] branch master updated: CAMEL-16015: camel-ftp - SFTP consumer in local work dir mode writing to file should use file rename instead of stream copy.

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


The following commit(s) were added to refs/heads/master by this push:
     new b637bcf  CAMEL-16015: camel-ftp - SFTP consumer in local work dir mode writing to file should use file rename instead of stream copy.
b637bcf is described below

commit b637bcfbdcf1aa1edbcba21430c1c850a65d89af
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Mar 22 19:00:49 2021 +0100

    CAMEL-16015: camel-ftp - SFTP consumer in local work dir mode writing to file should use file rename instead of stream copy.
---
 .../camel/component/file/FileOperations.java       |  11 ++-
 .../component/file/remote/SftpOperations.java      |   2 +-
 .../remote/SftpConsumerLocalWorkDirectoryTest.java | 105 +++++++++++++++++++++
 3 files changed, 116 insertions(+), 2 deletions(-)

diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java b/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java
index b0a6898..8bcd4f1 100644
--- a/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java
+++ b/components/camel-file/src/main/java/org/apache/camel/component/file/FileOperations.java
@@ -297,7 +297,16 @@ public class FileOperations implements GenericFileOperations<File> {
                 // using file directly (optimized)
                 Object body = exchange.getIn().getBody();
                 if (body instanceof WrappedFile) {
-                    body = ((WrappedFile<?>) body).getFile();
+                    WrappedFile wrapped = (WrappedFile) body;
+                    body = wrapped.getFile();
+                    if (!(body instanceof File)) {
+                        // the wrapped file may be from remote (FTP) which then can store
+                        // a local java.io.File handle if storing to local work-dir so check for that
+                        Object maybeFile = wrapped.getBody();
+                        if (maybeFile instanceof File) {
+                            body = maybeFile;
+                        }
+                    }
                 }
                 if (body instanceof File) {
                     source = (File) body;
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
index 722e9c5..34f7383 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
@@ -928,7 +928,7 @@ public class SftpOperations implements RemoteFileOperations<SftpRemoteFile> {
 
         LOG.trace("storeFile({})", name);
 
-        boolean answer = false;
+        boolean answer;
         String currentDir = null;
         String path = FileUtil.onlyPath(name);
         String targetName = name;
diff --git a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/SftpConsumerLocalWorkDirectoryTest.java b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/SftpConsumerLocalWorkDirectoryTest.java
new file mode 100644
index 0000000..b49157e
--- /dev/null
+++ b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/SftpConsumerLocalWorkDirectoryTest.java
@@ -0,0 +1,105 @@
+/*
+ * 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.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.apache.camel.builder.NotifyBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.file.remote.sftp.SftpServerTestSupport;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.util.FileUtil;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.apache.camel.test.junit5.TestSupport.assertFileExists;
+import static org.apache.camel.test.junit5.TestSupport.assertFileNotExists;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class SftpConsumerLocalWorkDirectoryTest extends SftpServerTestSupport {
+
+    protected String getFtpUrl() {
+        return "sftp://localhost:{{ftp.server.port}}/{{ftp.root.dir}}/?password=admin"
+               + "&localWorkDirectory=" + testDirectory("lwd")
+               + "&noop=true";
+    }
+
+    @Override
+    @BeforeEach
+    public void setUp() throws Exception {
+        super.setUp();
+        prepareFtpServer();
+    }
+
+    private void prepareFtpServer() throws Exception {
+        // prepares the FTP Server by creating a file on the server that we want
+        // to unit test that we can pool
+        Endpoint endpoint = context.getEndpoint(getFtpUrl());
+        Exchange exchange = endpoint.createExchange();
+        exchange.getIn().setBody("Hello World");
+        exchange.getIn().setHeader(Exchange.FILE_NAME, "hello.txt");
+        Producer producer = endpoint.createProducer();
+        producer.start();
+        producer.process(exchange);
+        producer.stop();
+    }
+
+    @Test
+    public void testLocalWorkDirectory() throws Exception {
+        NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).create();
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello World");
+        mock.expectedMessageCount(1);
+
+        context.getRouteController().startRoute("myRoute");
+
+        assertMockEndpointsSatisfied();
+
+        assertMockEndpointsSatisfied();
+        assertTrue(notify.matchesWaitTime());
+
+        // and the out file should exists
+        assertFileExists(testFile("out/hello.txt"), "Hello World");
+
+        // now the lwd file should be deleted
+        assertFileNotExists(testFile("lwd/hello.txt"));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from(getFtpUrl()).routeId("myRoute").noAutoStartup().process(new Processor() {
+                    public void process(Exchange exchange) throws Exception {
+                        File body = exchange.getIn().getBody(File.class);
+                        assertNotNull(body);
+                        assertTrue(body.exists(), "Local work file should exists");
+                        assertEquals(FileUtil.normalizePath(testFile("lwd/hello.txt").toString()), body.getPath());
+                    }
+                }).to("mock:result", fileUri("out"));
+            }
+        };
+    }
+
+}