camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [3/7] git commit: CAMEL-6734: Added header for consumed file name in file components. Thanks to James Carman for the patch.
Date Thu, 12 Sep 2013 07:40:47 GMT
CAMEL-6734: Added header for consumed file name in file components. Thanks to James Carman
for the patch.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c3a9964e
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c3a9964e
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c3a9964e

Branch: refs/heads/camel-2.12.x
Commit: c3a9964e81d2a463e4dd17bd43039d8eaede5f8f
Parents: ccc19d8
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Thu Sep 12 09:25:59 2013 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Thu Sep 12 09:29:50 2013 +0200

----------------------------------------------------------------------
 .../main/java/org/apache/camel/Exchange.java    |  1 +
 .../camel/component/file/GenericFile.java       |  1 +
 .../component/file/GenericFileProducer.java     |  4 +-
 .../file/FileConsumerConsumedFileNameTest.java  | 55 +++++++++++++++++
 ...eProducerConsumedFileNameEvaluationTest.java | 62 ++++++++++++++++++++
 5 files changed, 122 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/c3a9964e/camel-core/src/main/java/org/apache/camel/Exchange.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/Exchange.java b/camel-core/src/main/java/org/apache/camel/Exchange.java
index 0aa0762..9f7a22d 100644
--- a/camel-core/src/main/java/org/apache/camel/Exchange.java
+++ b/camel-core/src/main/java/org/apache/camel/Exchange.java
@@ -119,6 +119,7 @@ public interface Exchange {
     String FILE_NAME            = "CamelFileName";
     String FILE_NAME_ONLY       = "CamelFileNameOnly";
     String FILE_NAME_PRODUCED   = "CamelFileNameProduced";
+    String FILE_NAME_CONSUMED   = "CamelFileNameConsumed";
     String FILE_PATH            = "CamelFilePath";
     String FILE_PARENT          = "CamelFileParent";
     String FILE_LAST_MODIFIED   = "CamelFileLastModified";

http://git-wip-us.apache.org/repos/asf/camel/blob/c3a9964e/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java b/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
index 1c6db53..5c36320 100644
--- a/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
+++ b/camel-core/src/main/java/org/apache/camel/component/file/GenericFile.java
@@ -128,6 +128,7 @@ public class GenericFile<T> implements WrappedFile<T>  {
         if (message != null) {
             message.setHeader(Exchange.FILE_NAME_ONLY, getFileNameOnly());
             message.setHeader(Exchange.FILE_NAME, getFileName());
+            message.setHeader(Exchange.FILE_NAME_CONSUMED, getFileName());
             message.setHeader("CamelFileAbsolute", isAbsolute());
             message.setHeader("CamelFileAbsolutePath", getAbsoluteFilePath());
     

http://git-wip-us.apache.org/repos/asf/camel/blob/c3a9964e/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
index 2f2bbab..af9d0db 100644
--- a/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
+++ b/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
@@ -287,6 +287,7 @@ public class GenericFileProducer<T> extends DefaultProducer {
 
         // overrule takes precedence
         String overrule = exchange.getIn().getHeader(Exchange.OVERRULE_FILE_NAME, String.class);
+        String consumed = exchange.getIn().getHeader(Exchange.FILE_NAME_CONSUMED, String.class);
         String name = overrule == null ? exchange.getIn().getHeader(Exchange.FILE_NAME, String.class)
: overrule;
 
         // if we have an overrule then override the existing header to use the overrule computed
name from this point forward
@@ -296,7 +297,8 @@ public class GenericFileProducer<T> extends DefaultProducer {
 
         // expression support
         Expression expression = endpoint.getFileName();
-        if (name != null) {
+
+        if (name != null && !name.equals(consumed)) {
             // the header name can be an expression too, that should override
             // whatever configured on the endpoint
             if (StringHelper.hasStartToken(name, "simple")) {

http://git-wip-us.apache.org/repos/asf/camel/blob/c3a9964e/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerConsumedFileNameTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerConsumedFileNameTest.java
b/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerConsumedFileNameTest.java
new file mode 100644
index 0000000..30de2ce
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerConsumedFileNameTest.java
@@ -0,0 +1,55 @@
+/**
+ * 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 test that checks for the existence of the CamelFileNameConsumed header.
+ */
+public class FileConsumerConsumedFileNameTest extends ContextTestSupport {
+
+    @Override
+    protected void setUp() throws Exception {
+        deleteDirectory("target/consumedfilename");
+        super.setUp();
+        // the file name is also starting with target/consumedfilename
+        template.sendBodyAndHeader("file:target/consumedfilename", "Hello World", Exchange.FILE_NAME,
"hello.txt");
+    }
+
+    public void testValidFilenameOnExchange() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.message(0).header(Exchange.FILE_NAME).isEqualTo("hello.txt");
+        mock.message(0).header(Exchange.FILE_NAME_CONSUMED).isEqualTo("hello.txt");
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("file:target/consumedfilename").to("mock:result");
+            }
+        };
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/c3a9964e/camel-core/src/test/java/org/apache/camel/component/file/FileProducerConsumedFileNameEvaluationTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileProducerConsumedFileNameEvaluationTest.java
b/camel-core/src/test/java/org/apache/camel/component/file/FileProducerConsumedFileNameEvaluationTest.java
new file mode 100644
index 0000000..64da59d
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/file/FileProducerConsumedFileNameEvaluationTest.java
@@ -0,0 +1,62 @@
+/**
+ * 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.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Map;
+import java.util.TreeMap;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+
+/**
+ * @version
+ */
+public class FileProducerConsumedFileNameEvaluationTest extends ContextTestSupport {
+
+    @Override
+    protected void setUp() throws Exception {
+        deleteDirectory("target/producerconsumedfilename");
+        super.setUp();
+    }
+
+    public void testFileNameNotEvaluatedWhenMatchingConsumed() throws Exception {
+        Map<String, Object> headers = new TreeMap<String, Object>();
+        headers.put(Exchange.FILE_NAME, "file-${date:now:yyyyMMdd}");
+        headers.put(Exchange.FILE_NAME_CONSUMED, "file-${date:now:yyyyMMdd}");
+        template.sendBodyAndHeaders("file://target/producerconsumedfilename", "Hello World",
headers);
+        assertFileExists("target/producerconsumedfilename/file-${date:now:yyyyMMdd}");
+    }
+
+    public void testFileNameEvaluatedWhenNotMatchingConsumed() throws Exception {
+        Map<String, Object> headers = new TreeMap<String, Object>();
+        headers.put(Exchange.FILE_NAME, "file-${date:now:yyyyMMdd}.txt");
+        headers.put(Exchange.FILE_NAME_CONSUMED, "file-consumed");
+        template.sendBodyAndHeaders("file://target/producerconsumedfilename", "Hello World",
headers);
+        String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
+        assertFileExists("target/producerconsumedfilename/file-" + date + ".txt");
+    }
+
+    public void testFileNameEvaluatedWhenConsumedNull() throws Exception {
+        Map<String, Object> headers = new TreeMap<String, Object>();
+        headers.put(Exchange.FILE_NAME, "file-${date:now:yyyyMMdd}.txt");
+        template.sendBodyAndHeaders("file://target/producerconsumedfilename", "Hello World",
headers);
+        String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
+        assertFileExists("target/producerconsumedfilename/file-" + date + ".txt");
+    }
+}


Mime
View raw message