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 2020/12/20 15:32:30 UTC

[camel] branch master updated: CAMEL-15971: file consumer filter by fileName option should eval with current file instead of dummy empty exchange.

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 80d2b24  CAMEL-15971: file consumer filter by fileName option should eval with current file instead of dummy empty exchange.
80d2b24 is described below

commit 80d2b2422efd7b3965f0cad2fb7d1ca61ae2f93c
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun Dec 20 16:30:54 2020 +0100

    CAMEL-15971: file consumer filter by fileName option should eval with current file instead of dummy empty exchange.
---
 .../camel/component/file/GenericFileConsumer.java  | 29 +++++------
 .../camel/component/file/remote/FtpConsumer.java   | 14 +++---
 .../camel/component/file/remote/SftpConsumer.java  | 10 ++--
 .../file/FileConsumerFileNameFilterTest.java       | 56 ++++++++++++++++++++++
 4 files changed, 79 insertions(+), 30 deletions(-)

diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
index db7d261..3170e4e 100644
--- a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
+++ b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
@@ -51,7 +51,6 @@ public abstract class GenericFileConsumer<T> extends ScheduledBatchPollingConsum
     protected GenericFileEndpoint<T> endpoint;
     protected GenericFileOperations<T> operations;
     protected GenericFileProcessStrategy<T> processStrategy;
-    protected String fileExpressionResult;
     protected volatile ShutdownRunningTask shutdownRunningTask;
     protected volatile int pendingExchanges;
     protected Processor customProcessor;
@@ -110,7 +109,6 @@ public abstract class GenericFileConsumer<T> extends ScheduledBatchPollingConsum
         }
 
         // must reset for each poll
-        fileExpressionResult = null;
         shutdownRunningTask = null;
         pendingExchanges = 0;
 
@@ -674,19 +672,19 @@ public abstract class GenericFileConsumer<T> extends ScheduledBatchPollingConsum
             }
         }
 
-        // use file expression for a simple dynamic file filter
         if (endpoint.getFileName() != null) {
-            fileExpressionResult = evaluateFileExpression();
-            if (fileExpressionResult != null) {
-                if (!name.equals(fileExpressionResult)) {
+            // create a dummy exchange as Exchange is needed for expression evaluation
+            Exchange dummy = endpoint.createExchange(file);
+            String result = evaluateFileExpression(dummy);
+            if (result != null) {
+                if (!name.equals(result)) {
                     return false;
                 }
             }
         }
 
         if (endpoint.getFilterFile() != null) {
-            // create a dummy exchange as Exchange is needed for expression
-            // evaluation
+            // create a dummy exchange as Exchange is needed for expression evaluation
             Exchange dummy = endpoint.createExchange(file);
             boolean matches = endpoint.getFilterFile().matches(dummy);
             if (!matches) {
@@ -725,17 +723,12 @@ public abstract class GenericFileConsumer<T> extends ScheduledBatchPollingConsum
      */
     protected abstract boolean isMatched(GenericFile<T> file, String doneFileName, List<T> files);
 
-    protected String evaluateFileExpression() {
-        if (fileExpressionResult == null && endpoint.getFileName() != null) {
-            // create a dummy exchange as Exchange is needed for expression
-            // evaluation
-            Exchange dummy = endpoint.createExchange();
-            fileExpressionResult = endpoint.getFileName().evaluate(dummy, String.class);
-            if (dummy.getException() != null) {
-                throw RuntimeCamelException.wrapRuntimeCamelException(dummy.getException());
-            }
+    protected String evaluateFileExpression(Exchange exchange) {
+        String result = endpoint.getFileName().evaluate(exchange, String.class);
+        if (exchange.getException() != null) {
+            throw RuntimeCamelException.wrapRuntimeCamelException(exchange.getException());
         }
-        return fileExpressionResult;
+        return result;
     }
 
     @SuppressWarnings("unchecked")
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
index 75f8c5c..51b2acd 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpConsumer.java
@@ -150,13 +150,13 @@ public class FtpConsumer extends RemoteFileConsumer<FTPFile> {
                 }
             } else {
                 // we cannot use the LIST command(s) so we can only poll a named
-                // file
-                // so created a pseudo file with that name
-                FTPFile file = new FTPFile();
-                file.setType(FTPFile.FILE_TYPE);
-                fileExpressionResult = evaluateFileExpression();
-                if (fileExpressionResult != null) {
-                    file.setName(fileExpressionResult);
+                // file so created a pseudo file with that name
+                Exchange dummy = endpoint.createExchange();
+                String name = evaluateFileExpression(dummy);
+                if (name != null) {
+                    FTPFile file = new FTPFile();
+                    file.setType(FTPFile.FILE_TYPE);
+                    file.setName(name);
                     files = new ArrayList<>(1);
                     files.add(file);
                 }
diff --git a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
index edde9cb..332b679 100644
--- a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
+++ b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConsumer.java
@@ -142,11 +142,11 @@ public class SftpConsumer extends RemoteFileConsumer<SftpRemoteFile> {
                 }
             } else {
                 // we cannot use the LIST command(s) so we can only poll a named
-                // file
-                // so created a pseudo file with that name
-                fileExpressionResult = evaluateFileExpression();
-                if (fileExpressionResult != null) {
-                    SftpRemoteFile file = new SftpRemoteFileSingle(fileExpressionResult);
+                // file so created a pseudo file with that name
+                Exchange dummy = endpoint.createExchange();
+                String name = evaluateFileExpression(dummy);
+                if (name != null) {
+                    SftpRemoteFile file = new SftpRemoteFileSingle(name);
                     files = new ArrayList<>(1);
                     files.add(file);
                 }
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFileNameFilterTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFileNameFilterTest.java
new file mode 100644
index 0000000..016a77b
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFileNameFilterTest.java
@@ -0,0 +1,56 @@
+/*
+ * 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.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public class FileConsumerFileNameFilterTest extends ContextTestSupport {
+
+    @Override
+    @BeforeEach
+    public void setUp() throws Exception {
+        deleteDirectory("target/data/inbox");
+        super.setUp();
+    }
+
+    @Test
+    public void testFileConsumer() throws Exception {
+        getMockEndpoint("mock:txt").expectedBodiesReceivedInAnyOrder("Hello World", "Bye World");
+
+        template.sendBodyAndHeader("file:target/inbox", "Hello World", Exchange.FILE_NAME, "hello.txt");
+        template.sendBodyAndHeader("file:target/inbox", "<customer>123</customer>", Exchange.FILE_NAME, "customer.xml");
+        template.sendBodyAndHeader("file:target/inbox", "<book>Camel Rocks</book>", Exchange.FILE_NAME, "book.xml");
+        template.sendBodyAndHeader("file:target/inbox", "Bye World", Exchange.FILE_NAME, "bye.txt");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("file:target/inbox?initialDelay=0&delay=10&fileName=${file:onlyname.noext}.txt")
+                        .to("mock:txt");
+            }
+        };
+    }
+}