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 16:19:26 UTC

[camel] branch camel-2.25.x 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 camel-2.25.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-2.25.x by this push:
     new 04a9470  CAMEL-15971: file consumer filter by fileName option should eval with current file instead of dummy empty exchange.
04a9470 is described below

commit 04a947074de2b65a19a39c2608ac277895ecbce5
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun Dec 20 17:18:51 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  | 24 ++++------
 .../file/FileConsumerFileNameFilterTest.java       | 54 ++++++++++++++++++++++
 .../camel/component/file/remote/FtpConsumer.java   | 11 +++--
 .../camel/component/file/remote/SftpConsumer.java  |  7 +--
 4 files changed, 74 insertions(+), 22 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
index 1cc7d0d..816c6c7 100644
--- a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
+++ b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
@@ -48,7 +48,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;
@@ -106,7 +105,6 @@ public abstract class GenericFileConsumer<T> extends ScheduledBatchPollingConsum
         }
 
         // must reset for each poll
-        fileExpressionResult = null;
         shutdownRunningTask = null;
         pendingExchanges = 0;
 
@@ -634,9 +632,11 @@ 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;
                 }
             }
@@ -695,16 +695,12 @@ public abstract class GenericFileConsumer<T> extends ScheduledBatchPollingConsum
         return !endpoint.getInProgressRepository().add(key);
     }
 
-    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 ObjectHelper.wrapRuntimeCamelException(dummy.getException());
-            }
+    protected String evaluateFileExpression(Exchange exchange) {
+        String result = endpoint.getFileName().evaluate(exchange, String.class);
+        if (exchange.getException() != null) {
+            throw ObjectHelper.wrapRuntimeCamelException(exchange.getException());
         }
-        return fileExpressionResult;
+        return result;
     }
 
     @SuppressWarnings("unchecked")
diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFileNameFilterTest.java b/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFileNameFilterTest.java
new file mode 100644
index 0000000..d058969
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFileNameFilterTest.java
@@ -0,0 +1,54 @@
+/**
+ * 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.Test;
+
+public class FileConsumerFileNameFilterTest extends ContextTestSupport {
+
+    @Override
+    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");
+            }
+        };
+    }
+}
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 882cc71..d526afa 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
@@ -140,11 +140,12 @@ 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);
+                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 ad110d1..f949002 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
@@ -129,9 +129,10 @@ 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);
+                Exchange dummy = endpoint.createExchange();
+                String name = evaluateFileExpression(dummy);
+                if (name != null) {
+                    SftpRemoteFile file = new SftpRemoteFileSingle(name);
                     files = new ArrayList<>(1);
                     files.add(file);
                 }