CAMEL-8401 Restore original CamelFileName when using CamelOverruleFileName with 
remote file producer


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

Branch: refs/heads/master
Commit: a1bbf2bf5f3f9c54ce2330b88b7fc312b0b033d1
Parents: e1ad426
Author: Chris Pimlott <ch...@spartansoftwareinc.com>
Authored: Tue Feb 24 16:47:19 2015 -0800
Committer: Willem Jiang <willem.ji...@gmail.com>
Committed: Wed Feb 25 14:46:38 2015 +0800

----------------------------------------------------------------------
 .../file/remote/RemoteFileProducer.java         | 15 ++++-
 .../RemoteFileProduceOverruleOnlyOnceTest.java  | 64 ++++++++++++++++++++
 2 files changed, 78 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a1bbf2bf/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.java
 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.java
index d1ed723..ab10254 100644
--- 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.java
+++ 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.java
@@ -44,9 +44,22 @@ public class RemoteFileProducer<T> extends 
GenericFileProducer<T> implements Ser
         return name;
     }
 
+    @Override
     public void process(Exchange exchange) throws Exception {
+        // store any existing file header which we want to keep and propagate
+        final String existing = exchange.getIn().getHeader(Exchange.FILE_NAME, 
String.class);
+
+        // create the target file name
         String target = createFileName(exchange);
-        processExchange(exchange, target);
+
+        try {
+            processExchange(exchange, target);
+        } finally {
+            // remove the write file name header as we only want to use it 
once (by design)
+            exchange.getIn().removeHeader(Exchange.OVERRULE_FILE_NAME);
+            // and restore existing file name
+            exchange.getIn().setHeader(Exchange.FILE_NAME, existing);
+        }
     }
 
     protected RemoteFileOperations<T> getOperations() {

http://git-wip-us.apache.org/repos/asf/camel/blob/a1bbf2bf/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/RemoteFileProduceOverruleOnlyOnceTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/RemoteFileProduceOverruleOnlyOnceTest.java
 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/RemoteFileProduceOverruleOnlyOnceTest.java
new file mode 100644
index 0000000..176306d
--- /dev/null
+++ 
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/RemoteFileProduceOverruleOnlyOnceTest.java
@@ -0,0 +1,64 @@
+/**
+ * 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.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class RemoteFileProduceOverruleOnlyOnceTest extends 
FtpServerTestSupport {
+
+    @Test
+    public void testFileToFtp() throws Exception {
+        Map<String, Object> headers = new HashMap<String, Object>();
+        headers.put(Exchange.FILE_NAME, "/sub/hello.txt");
+        headers.put(Exchange.OVERRULE_FILE_NAME, "/sub/ruled.txt");
+        template.sendBodyAndHeaders("direct:input", "Hello World", headers);
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedFileExists(FTP_ROOT_DIR + "/out/sub/ruled.txt", "Hello 
World");
+        mock.expectedFileExists("target/out/sub/hello.txt", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    public void setUp() throws Exception {
+        deleteDirectory("target/out");
+        super.setUp();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:input")
+                    .to("ftp://admin:admin@localhost:"; + getPort()
+                        + "/out/")
+                    .to("file://target/out", "mock:result");
+            }
+        };
+    }
+}

Reply via email to