From ac129df0c18329fdb3cfafe35d2c1d3c4b5a7282 Mon Sep 17 00:00:00 2001 From: Chris Pimlott Date: Tue, 24 Feb 2015 16:47:19 -0800 Subject: [PATCH] restore original CamelFileName when using CamelOverruleFileName with remote file producer --- .../file/remote/RemoteFileProducer.java | 15 ++++- ...RemoteFileProduceOverruleOnlyOnceTest.java | 64 +++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/RemoteFileProduceOverruleOnlyOnceTest.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 d1ed72327ea41..ab102543be64e 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 String normalizePath(String name) { 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 getOperations() { 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 0000000000000..176306d9944b4 --- /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 headers = new HashMap(); + 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"); + } + }; + } +}