This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-4.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.22.x by this push:
new 2d0111e08a53 [backport camel-4.22.x] CAMEL-27116: camel-file,
camel-ftp, camel-smb - warn when noop is set with delete or move (#30292)
2d0111e08a53 is described below
commit 2d0111e08a535989056e9a90a213b6972ab5dcfa
Author: Federico Mariani <[email protected]>
AuthorDate: Tue Nov 17 20:13:19 2026 +0100
[backport camel-4.22.x] CAMEL-27116: camel-file, camel-ftp, camel-smb -
warn when noop is set with delete or move (#30292)
With noop=true and delete=true the consumer deletes the files (delete wins),
and with noop=true and move it does not move them (noop wins). Creating the
consumer now logs a WARN that says which option wins and what to remove,
unless a custom processStrategy is set.
Manual port of the noop part of commit
4ae654fe19116de265d34d205ebe3c42cf6e138a: the other changes of that commit
are not included.
Co-Authored-By: Claude <[email protected]>
---
.../apache/camel/component/file/FileEndpoint.java | 1 +
.../camel/component/file/GenericFileEndpoint.java | 25 +++
.../component/file/remote/RemoteFileEndpoint.java | 1 +
...FileEndpointConflictingConsumerOptionsTest.java | 124 +++++++++++++++
.../apache/camel/component/smb/SmbEndpoint.java | 1 +
.../SmbEndpointConflictingConsumerOptionsTest.java | 99 ++++++++++++
.../file/FileConsumerNoopWithDeleteOrMoveTest.java | 169 +++++++++++++++++++++
7 files changed, 420 insertions(+)
diff --git
a/components/camel-file/src/main/java/org/apache/camel/component/file/FileEndpoint.java
b/components/camel-file/src/main/java/org/apache/camel/component/file/FileEndpoint.java
index 5c78164127b9..2796f7e5ddf4 100644
---
a/components/camel-file/src/main/java/org/apache/camel/component/file/FileEndpoint.java
+++
b/components/camel-file/src/main/java/org/apache/camel/component/file/FileEndpoint.java
@@ -118,6 +118,7 @@ public class FileEndpoint extends GenericFileEndpoint<File>
{
LOG.info("Using default memory based idempotent repository with
cache max size: {}", DEFAULT_IDEMPOTENT_CACHE_SIZE);
idempotentRepository =
MemoryIdempotentRepository.memoryIdempotentRepository(DEFAULT_IDEMPOTENT_CACHE_SIZE);
}
+ warnIfConsumerOptionsConflict();
if (ObjectHelper.isNotEmpty(getReadLock())) {
readLockCheck();
diff --git
a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
index efc91aa8ffbb..4aaeaf12d4f2 100644
---
a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
+++
b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
@@ -974,6 +974,31 @@ public abstract class GenericFileEndpoint<T> extends
ScheduledPollEndpoint imple
return readLock != null && IDEMPOTENT_READ_LOCKS.contains(readLock);
}
+ /**
+ * Logs a WARN for consumer options that start but do not do what they say:
+ * <ul>
+ * <li>noop=true with delete=true: delete wins, and the consumer deletes
the files.</li>
+ * <li>noop=true with move: noop wins, and the consumer does not move the
files.</li>
+ * </ul>
+ * A custom process strategy decides itself what happens to the files, so
there is no WARN then.
+ */
+ protected void warnIfConsumerOptionsConflict() {
+ if (noop && processStrategy == null) {
+ if (delete) {
+ LOG.warn("The consumer of {} has both noop=true and
delete=true: delete wins, and the files are deleted"
+ + " after they are processed. Remove delete=true to
leave the files in place, or noop=true to"
+ + " delete them.",
+ this);
+ }
+ if (move != null) {
+ LOG.warn("The consumer of {} has both noop=true and the move
option: noop wins, and the files are not moved"
+ + " after they are processed. Remove noop=true to
move the files, or the move option to leave them"
+ + " in place.",
+ this);
+ }
+ }
+ }
+
public String getCharset() {
return charset;
}
diff --git
a/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java
b/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java
index 678012a536c2..74ed70d0738c 100644
---
a/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java
+++
b/components/camel-ftp-common/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java
@@ -175,6 +175,7 @@ public abstract class RemoteFileEndpoint<T> extends
GenericFileEndpoint<T> imple
LOG.info("Using default memory based idempotent repository with
cache max size: {}", DEFAULT_IDEMPOTENT_CACHE_SIZE);
idempotentRepository =
MemoryIdempotentRepository.memoryIdempotentRepository(DEFAULT_IDEMPOTENT_CACHE_SIZE);
}
+ warnIfConsumerOptionsConflict();
if (!getConfiguration().isUseList() && getFileName() == null) {
throw new IllegalArgumentException(
diff --git
a/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/RemoteFileEndpointConflictingConsumerOptionsTest.java
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/RemoteFileEndpointConflictingConsumerOptionsTest.java
new file mode 100644
index 000000000000..c621e63ad8d2
--- /dev/null
+++
b/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/RemoteFileEndpointConflictingConsumerOptionsTest.java
@@ -0,0 +1,124 @@
+/*
+ * 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.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.stream.Stream;
+
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.Endpoint;
+import org.apache.camel.component.file.GenericFileEndpoint;
+import org.apache.camel.component.file.GenericFileProcessStrategy;
+import org.apache.camel.component.file.strategy.GenericFileNoOpProcessStrategy;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.Logger;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.config.Property;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * The ftp, ftps and sftp consumers share the options of the file consumer:
noop=true with delete=true deletes the
+ * files, and noop=true with move does not move them. Creating the consumer
logs a WARN for each.
+ */
+class RemoteFileEndpointConflictingConsumerOptionsTest extends
CamelTestSupport {
+
+ private static final String[] SCHEMES = { "ftp", "ftps", "sftp" };
+
+ @BindToRegistry("myStrategy")
+ private final GenericFileProcessStrategy<?> myStrategy = new
GenericFileNoOpProcessStrategy<>();
+
+ private final List<String> warnings = new CopyOnWriteArrayList<>();
+ private final AbstractAppender appender
+ = new
AbstractAppender("RemoteFileEndpointConflictingConsumerOptions", null, null,
true, Property.EMPTY_ARRAY) {
+ @Override
+ public void append(LogEvent event) {
+ if (event.getLevel().isMoreSpecificThan(Level.WARN)) {
+ warnings.add(event.getMessage().getFormattedMessage());
+ }
+ }
+ };
+
+ static Stream<Arguments> conflictingOptions() {
+ return Stream.of(SCHEMES).flatMap(scheme -> Stream.of(
+ Arguments.of(scheme, "noop=true&delete=true",
+ " has both noop=true and delete=true: delete wins, and
the files are deleted after they are"
+ + " processed.
Remove delete=true to leave the files in"
+ + " place, or
noop=true to delete them."),
+ Arguments.of(scheme, "noop=true&move=done",
+ " has both noop=true and the move option: noop wins,
and the files are not moved after they"
+ + " are processed.
Remove noop=true to move the files, or"
+ + " the move
option to leave them in place.")));
+ }
+
+ static Stream<Arguments> optionsThatDoWhatTheySay() {
+ return Stream.of(SCHEMES).flatMap(scheme -> Stream.of(
+ "noop=true", "delete=true", "move=done",
"noop=true&moveFailed=error",
+ "noop=true&delete=true&processStrategy=#myStrategy")
+ .map(options -> Arguments.of(scheme, options)));
+ }
+
+ @BeforeEach
+ void captureWarnings() {
+ appender.start();
+ endpointLogger().addAppender(appender);
+ // addAppender creates a non-additive logger config, which would hide
this logger from the root appenders
+ endpointLogger().setAdditive(true);
+ }
+
+ @AfterEach
+ void removeAppender() {
+ endpointLogger().removeAppender(appender);
+ appender.stop();
+ }
+
+ @ParameterizedTest
+ @MethodSource("conflictingOptions")
+ void conflictingOptionsLogWarn(String scheme, String options, String
warning) throws Exception {
+ Endpoint endpoint = context.getEndpoint(scheme + "://localhost/inbox?"
+ options);
+
+ endpoint.createConsumer(exchange -> {
+ });
+
+ assertEquals(List.of("The consumer of " + endpoint + warning),
warnings);
+ }
+
+ @ParameterizedTest
+ @MethodSource("optionsThatDoWhatTheySay")
+ void optionsThatDoWhatTheySayDoNotWarn(String scheme, String options)
throws Exception {
+ Endpoint endpoint = context.getEndpoint(scheme + "://localhost/inbox?"
+ options);
+
+ endpoint.createConsumer(exchange -> {
+ });
+
+ assertTrue(warnings.isEmpty(), "warnings: " + warnings);
+ }
+
+ private static Logger endpointLogger() {
+ return (Logger) LogManager.getLogger(GenericFileEndpoint.class);
+ }
+}
diff --git
a/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbEndpoint.java
b/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbEndpoint.java
index a7f76fdeab77..730eeef124bd 100644
---
a/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbEndpoint.java
+++
b/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbEndpoint.java
@@ -150,6 +150,7 @@ public class SmbEndpoint extends
GenericFileEndpoint<FileIdBothDirectoryInformat
LOG.info("Using default memory based idempotent repository with
cache max size: {}", DEFAULT_IDEMPOTENT_CACHE_SIZE);
idempotentRepository =
MemoryIdempotentRepository.memoryIdempotentRepository(DEFAULT_IDEMPOTENT_CACHE_SIZE);
}
+ warnIfConsumerOptionsConflict();
if (ObjectHelper.isNotEmpty(getReadLock())) {
readLockCheck();
diff --git
a/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbEndpointConflictingConsumerOptionsTest.java
b/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbEndpointConflictingConsumerOptionsTest.java
new file mode 100644
index 000000000000..13344c83bd7e
--- /dev/null
+++
b/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbEndpointConflictingConsumerOptionsTest.java
@@ -0,0 +1,99 @@
+/*
+ * 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.smb;
+
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.component.file.GenericFileEndpoint;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.Logger;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.config.Property;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * The smb consumer shares the options of the file consumer: noop=true with
delete=true deletes the files, and noop=true
+ * with move does not move them. Creating the consumer logs a WARN for each.
+ */
+class SmbEndpointConflictingConsumerOptionsTest extends CamelTestSupport {
+
+ private final List<String> warnings = new CopyOnWriteArrayList<>();
+ private final AbstractAppender appender
+ = new AbstractAppender("SmbEndpointConflictingConsumerOptions",
null, null, true, Property.EMPTY_ARRAY) {
+ @Override
+ public void append(LogEvent event) {
+ if (event.getLevel().isMoreSpecificThan(Level.WARN)) {
+ warnings.add(event.getMessage().getFormattedMessage());
+ }
+ }
+ };
+
+ @BeforeEach
+ void captureWarnings() {
+ appender.start();
+ endpointLogger().addAppender(appender);
+ // addAppender creates a non-additive logger config, which would hide
this logger from the root appenders
+ endpointLogger().setAdditive(true);
+ }
+
+ @AfterEach
+ void removeAppender() {
+ endpointLogger().removeAppender(appender);
+ appender.stop();
+ }
+
+ @ParameterizedTest
+ @CsvSource(delimiter = '|', value = {
+ "noop=true&delete=true | has both noop=true and delete=true:
delete wins",
+ "noop=true&move=done | has both noop=true and the move option:
noop wins" })
+ void conflictingOptionsLogWarn(String options, String warning) throws
Exception {
+ Endpoint endpoint = context.getEndpoint("smb:localhost/share?" +
options);
+
+ endpoint.createConsumer(exchange -> {
+ });
+
+ assertEquals(1, warnings.size(), "warnings: " + warnings);
+ assertTrue(warnings.get(0).startsWith("The consumer of " + endpoint +
" " + warning), warnings.get(0));
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = { "noop=true", "delete=true", "move=done" })
+ void optionsThatDoWhatTheySayDoNotWarn(String options) throws Exception {
+ Endpoint endpoint = context.getEndpoint("smb:localhost/share?" +
options);
+
+ endpoint.createConsumer(exchange -> {
+ });
+
+ assertTrue(warnings.isEmpty(), "warnings: " + warnings);
+ }
+
+ private static Logger endpointLogger() {
+ return (Logger) LogManager.getLogger(GenericFileEndpoint.class);
+ }
+}
diff --git
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerNoopWithDeleteOrMoveTest.java
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerNoopWithDeleteOrMoveTest.java
new file mode 100644
index 000000000000..7fac2b8b4dc9
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerNoopWithDeleteOrMoveTest.java
@@ -0,0 +1,169 @@
+/*
+ * 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.nio.file.Files;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.camel.Consumer;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Endpoint;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.file.strategy.GenericFileNoOpProcessStrategy;
+import org.apache.camel.component.log.ConsumingAppender;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spi.Registry;
+import org.apache.camel.support.DefaultPollingConsumerPollStrategy;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.parallel.Isolated;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * noop=true with delete=true deletes the files (delete wins), and noop=true
with move does not move them (noop wins).
+ * Both start as before, and the consumer logs a WARN that says which option
wins.
+ */
+// isolated: the test captures the log of GenericFileEndpoint
+@Isolated
+class FileConsumerNoopWithDeleteOrMoveTest extends ContextTestSupport {
+
+ private static final String LOGGER = GenericFileEndpoint.class.getName();
+
+ private final PollCounter polls = new PollCounter();
+ private final List<String> warnings = new CopyOnWriteArrayList<>();
+
+ @Override
+ public boolean isUseRouteBuilder() {
+ return false;
+ }
+
+ @Override
+ protected Registry createCamelRegistry() throws Exception {
+ Registry registry = super.createCamelRegistry();
+ registry.bind("polls", polls);
+ registry.bind("myStrategy", new GenericFileNoOpProcessStrategy<>());
+ return registry;
+ }
+
+ @BeforeEach
+ void captureWarnings() {
+ ConsumingAppender.newAppender(LOGGER, getClass().getSimpleName(),
Level.WARN, event -> {
+ if (event.getLevel().isMoreSpecificThan(Level.WARN)) {
+ warnings.add(event.getMessage().getFormattedMessage());
+ }
+ });
+ }
+
+ @AfterEach
+ void removeAppender() {
+ LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
+ ctx.getConfiguration().removeLogger(LOGGER);
+ ctx.updateLoggers();
+ }
+
+ @Test
+ void noopWithDeleteLogsWarnAndDeletesTheFile() throws Exception {
+ Files.writeString(testFile("a.txt"), "a");
+
+ Endpoint endpoint = route("noop=true&delete=true");
+
+ getMockEndpoint("mock:result").expectedBodiesReceived("a");
+ MockEndpoint.assertIsSatisfied(context);
+ // delete wins, as before
+ await().atMost(10, TimeUnit.SECONDS).until(() ->
!Files.exists(testFile("a.txt")));
+
+ assertEquals(List.of("The consumer of " + endpoint + " has both
noop=true and delete=true: delete wins, and the"
+ + " files are deleted after they are processed.
Remove delete=true to leave the files in"
+ + " place, or noop=true to delete them."),
+ warnings);
+ }
+
+ @Test
+ void noopWithMoveLogsWarnAndDoesNotMoveTheFile() throws Exception {
+ Files.writeString(testFile("a.txt"), "a");
+
+ Endpoint endpoint = route("noop=true&move=done");
+
+ getMockEndpoint("mock:result").expectedBodiesReceived("a");
+ MockEndpoint.assertIsSatisfied(context);
+ // the file is committed before the next poll: it is still in place,
as before
+ int start = polls.getPolls();
+ await().atMost(10, TimeUnit.SECONDS).until(() -> polls.getPolls() >=
start + 3);
+ assertTrue(Files.exists(testFile("a.txt")));
+ assertFalse(Files.exists(testFile("done/a.txt")));
+
+ assertEquals(List.of("The consumer of " + endpoint + " has both
noop=true and the move option: noop wins, and"
+ + " the files are not moved after they are
processed. Remove noop=true to move the files,"
+ + " or the move option to leave them in place."),
+ warnings);
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {
+ "noop=true", "delete=true", "move=done", "noop=true&preMove=work",
"noop=true&moveFailed=error",
+ "noop=true&delete=true&processStrategy=#myStrategy" })
+ void optionsThatDoWhatTheySayDoNotWarn(String options) throws Exception {
+ Endpoint endpoint = context.getEndpoint(fileUri("?" + options));
+
+ endpoint.createConsumer(exchange -> {
+ });
+
+ assertTrue(warnings.isEmpty(), "warnings: " + warnings);
+ }
+
+ private Endpoint route(String options) throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from(fileUri("?" + options +
"&initialDelay=0&delay=10&pollStrategy=#polls")).routeId("r")
+ .convertBodyTo(String.class).to("mock:result");
+ }
+ });
+ context.start();
+ return context.getRoute("r").getEndpoint();
+ }
+
+ /**
+ * Counts the completed polls, so a test can wait for a number of polls
after a file was processed.
+ */
+ private static final class PollCounter extends
DefaultPollingConsumerPollStrategy {
+
+ private final AtomicInteger polls = new AtomicInteger();
+
+ @Override
+ public void commit(Consumer consumer, Endpoint endpoint, int
polledMessages) {
+ polls.incrementAndGet();
+ }
+
+ int getPolls() {
+ return polls.get();
+ }
+ }
+}