Skip to content
Permalink
Browse files
CAMEL-18130: camel-file/camel-ftp - Route with last processor setting…
… OUT message causes file/ftp to not be moved correctly when its uow is done. Thanks to Manuel Shenavai for reporting and unit test.
  • Loading branch information
davsclaus committed May 20, 2022
1 parent d070747 commit 1cf18beaa859eb2c3ced26be6eb13f93bcd9090e
Showing 4 changed files with 250 additions and 1 deletion.
@@ -129,7 +129,11 @@ private GenericFileMessage<T> commonBindToExchange(Exchange exchange) {
GenericFileMessage<T> msg = new GenericFileMessage<>(exchange, this);

headers = exchange.getMessage().hasHeaders() ? exchange.getMessage().getHeaders() : null;
exchange.setMessage(msg);
// force storing on IN as that is what Camel expects
exchange.setIn(msg);
if (exchange.hasOut()) {
exchange.setOut(null);
}

// preserve any existing (non file) headers, before we re-populate
// headers
@@ -0,0 +1,95 @@
/*
* 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.sftp.integration;

import java.io.File;
import java.util.concurrent.TimeUnit;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.support.DefaultMessage;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.condition.EnabledIf;

import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Test that the existence of a outMessage in an exchange will not break the move-file post-processing
*/
@EnabledIf(value = "org.apache.camel.component.file.remote.services.SftpEmbeddedService#hasRequiredAlgorithms")
public class SftpMoveWithOutMessageTest extends SftpServerTestSupport {

@Timeout(value = 30)
@Test
public void testMoveFileForMultiplePollEnrich() throws Exception {
String expected = "Hello World";

// create two files using regular file
template.sendBodyAndHeader("file://" + service.getFtpRootDir(), expected, Exchange.FILE_NAME, "hello1.txt");
template.sendBodyAndHeader("file://" + service.getFtpRootDir(), expected, Exchange.FILE_NAME, "hello2.txt");

ProducerTemplate triggerTemplate = context.createProducerTemplate();
triggerTemplate.sendBody("vm:trigger", "");

File fileInArchive = ftpFile("archive/hello1.txt").toFile();
await().atMost(15, TimeUnit.SECONDS)
.untilAsserted(() -> assertTrue(fileInArchive.exists(), "The file should exist in the archive folder"));

File fileInArchive2 = ftpFile("archive/hello2.txt").toFile();
await().atMost(15, TimeUnit.SECONDS)
.untilAsserted(() -> assertTrue(fileInArchive2.exists(), "The file should exist in the archive folder"));

File originalFile = ftpFile("hello1.txt").toFile();
await().atMost(15, TimeUnit.SECONDS)
.untilAsserted(() -> assertFalse(originalFile.exists(), "The file should have been moved"));

File originalFile2 = ftpFile("hello2.txt").toFile();
await().atMost(15, TimeUnit.SECONDS)
.untilAsserted(() -> assertFalse(originalFile2.exists(), "The file should have been moved"));
}

@Override
protected RouteBuilder[] createRouteBuilders() throws Exception {
TestProcessor processor = new TestProcessor();
return new RouteBuilder[] { new RouteBuilder() {
@Override
public void configure() throws Exception {
from("vm:trigger")
.pollEnrich(
"sftp://localhost:{{ftp.server.port}}/{{ftp.root.dir}}?username=admin&password=admin&delay=10000&disconnect=true&move=archive")
.pollEnrich(
"sftp://localhost:{{ftp.server.port}}/{{ftp.root.dir}}?username=admin&password=admin&delay=10000&disconnect=true&move=archive")
.process(processor);
}
} };
}

private static class TestProcessor implements Processor {
@Override
public void process(Exchange exchange) throws Exception {
DefaultMessage msg = new DefaultMessage(exchange);
msg.setBody(exchange.getIn().getBody());
msg.setHeaders(exchange.getIn().getHeaders());
exchange.setOut(msg); // uses OUT on purpose for testing
}
}
}
@@ -0,0 +1,75 @@
/*
* 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.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.support.DefaultMessage;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.concurrent.TimeUnit;

import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class FileMoveWithInMessageTest extends ContextTestSupport {

@Test
public void testMove() throws Exception {
String uri = fileUri();
template.sendBodyAndHeader(uri, "Hello World1", Exchange.FILE_NAME, "hello1.txt");
template.sendBodyAndHeader(uri, "Hello World2", Exchange.FILE_NAME, "hello2.txt");

// trigger
template.sendBody("vm:trigger", "");

File file1 = new File(testDirectory().toFile(), "archive/hello1.txt");
await().atMost(10, TimeUnit.SECONDS)
.untilAsserted(() -> assertTrue(file1.exists(), "The file should exist in the archive folder"));

File file2 = new File(testDirectory().toFile(), "archive/hello2.txt");
await().atMost(10, TimeUnit.SECONDS)
.untilAsserted(() -> assertTrue(file2.exists(), "The file should exist in the archive folder"));
}

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("vm:trigger")
.pollEnrich(fileUri() + "?move=archive")
.pollEnrich(fileUri() + "?move=archive")
.process(new TestProcessor());
}
};
}

private static class TestProcessor implements Processor {

@Override
public void process(Exchange exchange) throws Exception {
DefaultMessage msg = new DefaultMessage(exchange);
msg.setBody(exchange.getIn().getBody());
msg.setHeaders(exchange.getIn().getHeaders());
exchange.setIn(msg);
}
}
}
@@ -0,0 +1,75 @@
/*
* 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.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.support.DefaultMessage;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.concurrent.TimeUnit;

import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class FileMoveWithOutMessageTest extends ContextTestSupport {

@Test
public void testMove() throws Exception {
String uri = fileUri();
template.sendBodyAndHeader(uri, "Hello World1", Exchange.FILE_NAME, "hello1.txt");
template.sendBodyAndHeader(uri, "Hello World2", Exchange.FILE_NAME, "hello2.txt");

// trigger
template.sendBody("vm:trigger", "");

File file1 = new File(testDirectory().toFile(), "archive/hello1.txt");
await().atMost(10, TimeUnit.SECONDS)
.untilAsserted(() -> assertTrue(file1.exists(), "The file should exist in the archive folder"));

File file2 = new File(testDirectory().toFile(), "archive/hello2.txt");
await().atMost(10, TimeUnit.SECONDS)
.untilAsserted(() -> assertTrue(file2.exists(), "The file should exist in the archive folder"));
}

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("vm:trigger")
.pollEnrich(fileUri() + "?move=archive")
.pollEnrich(fileUri() + "?move=archive")
.process(new TestProcessor());
}
};
}

private static class TestProcessor implements Processor {

@Override
public void process(Exchange exchange) throws Exception {
DefaultMessage msg = new DefaultMessage(exchange);
msg.setBody(exchange.getIn().getBody());
msg.setHeaders(exchange.getIn().getHeaders());
exchange.setOut(msg); // uses OUT on purpose for testing
}
}
}

0 comments on commit 1cf18be

Please sign in to comment.