This is an automated email from the ASF dual-hosted git repository.
Croway pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 092fb5b88216 CAMEL-27078, CAMEL-27080: camel-smb - the route starts
when the starting directory cannot be created, and the WARN says so with the
reason (#30237)
092fb5b88216 is described below
commit 092fb5b882162e05fd22b0afd5bf7cdca4421bba
Author: Federico Mariani <[email protected]>
AuthorDate: Tue Nov 17 13:21:40 2026 +0100
CAMEL-27078, CAMEL-27080: camel-smb - the route starts when the starting
directory cannot be created, and the WARN says so with the reason (#30237)
* CAMEL-27078: camel-smb - a starting directory that cannot be created is
logged as such, with the reason
The auto-create WARN of SmbConsumer.prePollCheck was never logged: the
catch block set loggedIn = false before the
check. The poll logged "Cannot connect/login to: ..." instead, or nothing
when an earlier poll had already logged it.
prePollCheck now records whether it connected before the failure, and logs
"Cannot auto-create starting directory
<directory> at: ... due to: <reason>. Will skip this poll." once. The
connect WARN gives the reason too, as
RemoteFileConsumer does since CAMEL-27019.
* CAMEL-27080: camel-smb - a starting directory the server refuses to
create no longer fails the route start
SmbConsumer.doStart logs a WARN and starts the consumer when the starting
directory cannot be created, but it only
catches GenericFileOperationFailedException, and
SmbOperations.buildDirectory rethrew the smbj SMBRuntimeException as
it is, such as STATUS_ACCESS_DENIED on a read-only share, so the route
failed to start. buildDirectory now wraps it in
a GenericFileOperationFailedException with the same message, as the ftp and
sftp operations do, so the route starts
and each poll tries to create the directory again, as CAMEL-22177 intended.
* CAMEL-27080: camel-smb - the upgrade guide says the producer and the
moves still fail when a directory cannot be created
Only the consumer start was changed to go on when the starting directory
cannot be created. The producer and the moves
that create a directory still fail as before; they only get a
GenericFileOperationFailedException with the smbj
exception as its cause, which an onException or doCatch for the smbj
exception still matches.
---
.../apache/camel/catalog/docs/smb-component.adoc | 3 +
.../camel-smb/src/main/docs/smb-component.adoc | 3 +
.../apache/camel/component/smb/SmbConsumer.java | 37 ++++--
.../apache/camel/component/smb/SmbOperations.java | 3 +-
.../smb/SmbConsumerAutoCreateDeniedIT.java | 75 +++++++++++
.../component/smb/SmbConsumerSkipPollWarnTest.java | 137 +++++++++++++++++++++
.../component/smb/SmbConsumerWarnCapture.java | 64 ++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 16 +++
8 files changed, 329 insertions(+), 9 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/smb-component.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/smb-component.adoc
index e307bcefc110..8e5b44695b71 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/smb-component.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/smb-component.adoc
@@ -164,6 +164,9 @@ The exact permissions depend on the operations being
performed:
* **Reading files**: requires `Read Data` permission on the file.
* **Writing files**: requires `Write Data` permission on the file and `Create
Files` on the parent directory.
* **Deleting files** (e.g., when using `delete=true` on a consumer): requires
`Delete` permission on the file.
+* **Creating the starting directory** of a consumer that does not exist
(`autoCreate`, enabled by default): requires
+`Create Folders` on the parent directory. When the directory cannot be
created, the route still starts: the consumer
+logs a WARN and tries again on each poll.
=== STATUS_ACCESS_DENIED (0xc0000022) when deleting files
diff --git a/components/camel-smb/src/main/docs/smb-component.adoc
b/components/camel-smb/src/main/docs/smb-component.adoc
index e307bcefc110..8e5b44695b71 100644
--- a/components/camel-smb/src/main/docs/smb-component.adoc
+++ b/components/camel-smb/src/main/docs/smb-component.adoc
@@ -164,6 +164,9 @@ The exact permissions depend on the operations being
performed:
* **Reading files**: requires `Read Data` permission on the file.
* **Writing files**: requires `Write Data` permission on the file and `Create
Files` on the parent directory.
* **Deleting files** (e.g., when using `delete=true` on a consumer): requires
`Delete` permission on the file.
+* **Creating the starting directory** of a consumer that does not exist
(`autoCreate`, enabled by default): requires
+`Create Folders` on the parent directory. When the directory cannot be
created, the route still starts: the consumer
+logs a WARN and tries again on each poll.
=== STATUS_ACCESS_DENIED (0xc0000022) when deleting files
diff --git
a/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbConsumer.java
b/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbConsumer.java
index be785f25de11..99422d45fabb 100644
---
a/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbConsumer.java
+++
b/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbConsumer.java
@@ -20,6 +20,7 @@ import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Supplier;
+import java.util.stream.Collectors;
import com.hierynomus.msfscc.FileAttributes;
import com.hierynomus.msfscc.fileinformation.FileIdBothDirectoryInformation;
@@ -271,28 +272,30 @@ public class SmbConsumer extends
GenericFileConsumer<FileIdBothDirectoryInformat
if (LOG.isTraceEnabled()) {
LOG.trace("prePollCheck on {}", getEndpoint());
}
- Exception cause;
+ Exception cause = null;
+ boolean connected = false;
+ boolean autoCreateFailed = false;
try {
getOperations().connectIfNecessary();
loggedIn = true;
+ connected = true;
if (!autoCreatedDone) {
autoCreateIfNecessary();
autoCreatedDone = true;
}
} catch (Exception e) {
cause = e;
- String msg = "Cannot connect/login to: " + remoteServer();
- if (loggedIn && !autoCreatedDone) {
- msg = "Cannot auto-create starting directory at: " +
remoteServer();
- }
+ // connected, so the starting directory could not be created
+ autoCreateFailed = connected;
+ String msg = autoCreateFailed ? cannotAutoCreate() : "Cannot
connect/login to: " + remoteServer();
LOG.debug(msg, cause);
loggedIn = false;
if (configuration.isThrowExceptionOnConnectFailed()) {
throw e;
}
}
- if (loggedIn && !autoCreatedDone) {
- String message = "Cannot auto-create starting directory at: " +
remoteServer() + ". Will skip this poll.";
+ if (autoCreateFailed) {
+ String message = cannotAutoCreate() + reason(cause) + ". Will skip
this poll.";
if (!autoCreateWarning) {
LOG.warn(message);
autoCreateWarning = true;
@@ -300,7 +303,7 @@ public class SmbConsumer extends
GenericFileConsumer<FileIdBothDirectoryInformat
return false;
}
if (!loggedIn) {
- String message = "Cannot connect/login to: " + remoteServer() + ".
Will skip this poll.";
+ String message = "Cannot connect/login to: " + remoteServer() +
reason(cause) + ". Will skip this poll.";
if (!loggedInWarning) {
LOG.warn(message);
loggedInWarning = true;
@@ -317,6 +320,24 @@ public class SmbConsumer extends
GenericFileConsumer<FileIdBothDirectoryInformat
return true;
}
+ private String cannotAutoCreate() {
+ return "Cannot auto-create starting directory " +
configuration.getDirectory() + " at: " + remoteServer();
+ }
+
+ /**
+ * Why the poll is skipped, for the WARN: the message of the cause when
there is one, as the smb operations wrap the
+ * error of the SMB client in an exception whose message also names the
server. On one line.
+ */
+ private static String reason(Exception cause) {
+ if (cause == null) {
+ return "";
+ }
+ Throwable reason = cause.getCause() != null ? cause.getCause() : cause;
+ String message = reason.getMessage() != null ? reason.getMessage() :
reason.getClass().getName();
+ return " due to: "
+ + message.lines().map(String::strip).filter(line ->
!line.isEmpty()).collect(Collectors.joining(" "));
+ }
+
protected void autoCreateIfNecessary() throws
GenericFileOperationFailedException {
if (endpoint.isAutoCreate() && hasStartingDirectory()) {
String dir = endpoint.getConfiguration().getDirectory();
diff --git
a/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbOperations.java
b/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbOperations.java
index 144ebd453828..f1b695c33828 100644
---
a/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbOperations.java
+++
b/components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbOperations.java
@@ -281,7 +281,8 @@ public class SmbOperations implements SmbFileOperations {
files.mkdirs(share, normalize(directory));
} catch (SMBRuntimeException smbre) {
safeDisconnect(smbre);
- throw smbre;
+ // the consumer starts when the starting directory cannot be
created, and tries again on each poll
+ throw new GenericFileOperationFailedException(smbre.getMessage(),
smbre);
}
return true;
}
diff --git
a/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbConsumerAutoCreateDeniedIT.java
b/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbConsumerAutoCreateDeniedIT.java
new file mode 100644
index 000000000000..68ec01b4ef08
--- /dev/null
+++
b/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbConsumerAutoCreateDeniedIT.java
@@ -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.smb;
+
+import java.util.List;
+
+import org.apache.camel.ServiceStatus;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+
+/**
+ * A starting directory the server does not allow to create (a read-only
share, with autoCreate enabled by default): the
+ * route starts, and each poll tries to create the directory again.
+ */
+public class SmbConsumerAutoCreateDeniedIT extends SmbServerTestSupport {
+
+ private final SmbConsumerWarnCapture warnings = new
SmbConsumerWarnCapture();
+
+ @BeforeEach
+ void captureWarnings() {
+ warnings.attach();
+ }
+
+ @AfterEach
+ void releaseWarnings() {
+ warnings.detach();
+ }
+
+ @Test
+ void routeStartsWhenTheStartingDirectoryCannotBeCreated() throws Exception
{
+ // the scheduler is not started, so the test runs the polls
+ String uri =
String.format("smb:%s/data-ro/inbox?username=%s&password=%s&startScheduler=false",
+ service.address(), service.userName(), service.password());
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from(uri).routeId("readOnly").to("mock:result");
+ }
+ });
+
+ assertEquals(ServiceStatus.Started,
context.getRouteController().getRouteStatus("readOnly"));
+ String denied = "STATUS_ACCESS_DENIED (0xc0000022): Create failed for
\\\\" + service.hostname() + "\\data-ro\\inbox";
+ String startWarn = "Error auto creating directory: inbox due " +
denied + ". This exception is ignored.";
+ assertEquals(List.of(startWarn), warnings.messages());
+
+ SmbConsumer consumer = (SmbConsumer)
context.getRoute("readOnly").getConsumer();
+ assertFalse(consumer.prePollCheck());
+ assertFalse(consumer.prePollCheck());
+
+ // both polls are skipped, the WARN is logged once
+ String server = "smb://" + service.userName() + "@" +
service.address();
+ String pollWarn = "Cannot auto-create starting directory inbox at: " +
server + " due to: " + denied
+ + ". Will skip this poll.";
+ assertEquals(List.of(startWarn, pollWarn), warnings.messages());
+ }
+}
diff --git
a/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbConsumerSkipPollWarnTest.java
b/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbConsumerSkipPollWarnTest.java
new file mode 100644
index 000000000000..992897caad8c
--- /dev/null
+++
b/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbConsumerSkipPollWarnTest.java
@@ -0,0 +1,137 @@
+/*
+ * 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.net.ConnectException;
+import java.util.List;
+
+import com.hierynomus.mserref.NtStatus;
+import com.hierynomus.mssmb2.SMB2MessageCommandCode;
+import com.hierynomus.mssmb2.SMBApiException;
+import org.apache.camel.component.file.GenericFileOperationFailedException;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * The WARN of the smb consumer when it skips a poll: it tells a starting
directory that cannot be created apart from a
+ * server that cannot be connected to, and gives the reason.
+ */
+class SmbConsumerSkipPollWarnTest extends CamelTestSupport {
+
+ private final SmbConsumerWarnCapture warnings = new
SmbConsumerWarnCapture();
+ private StubSmbOperations operations;
+ private SmbConsumer consumer;
+
+ @BeforeEach
+ void createConsumer() {
+ warnings.attach();
+ SmbEndpoint endpoint = context.getEndpoint(
+
"smb:localhost:4450/data-rw/inbox?username=camel&password=secret",
SmbEndpoint.class);
+ operations = new StubSmbOperations(endpoint.getConfiguration());
+ consumer = new SmbConsumer(endpoint, exchange -> {
+ }, operations, null);
+ }
+
+ @AfterEach
+ void releaseWarnings() {
+ warnings.detach();
+ operations.forceDisconnect();
+ }
+
+ @Test
+ void autoCreateFailureIsLoggedWithTheDirectoryAndTheReason() throws
Exception {
+ // as SmbOperations.buildDirectory throws it
+ SMBApiException denied = new SMBApiException(
+ NtStatus.STATUS_ACCESS_DENIED.getValue(),
SMB2MessageCommandCode.SMB2_CREATE,
+ "Create failed for \\\\localhost\\data-rw\\inbox", null);
+ operations.buildDirectoryFailure = new
GenericFileOperationFailedException(denied.getMessage(), denied);
+
+ assertFalse(consumer.prePollCheck());
+ assertFalse(consumer.prePollCheck());
+
+ // logged once, until the directory is created
+ assertEquals(List.of("Cannot auto-create starting directory inbox at:
smb://camel@localhost:4450"
+ + " due to: STATUS_ACCESS_DENIED (0xc0000022):
Create failed for \\\\localhost\\data-rw\\inbox."
+ + " Will skip this poll."),
+ warnings.messages());
+
+ // the next poll creates the starting directory, and polls
+ operations.buildDirectoryFailure = null;
+
+ assertTrue(consumer.prePollCheck());
+ assertTrue(consumer.prePollCheck());
+ assertEquals(1, warnings.messages().size());
+ assertEquals(3, operations.buildDirectoryCalls);
+ }
+
+ @Test
+ void connectFailureIsLoggedWithTheReason() throws Exception {
+ operations.connectFailure = new GenericFileOperationFailedException(
+ "Cannot connect to: localhost:4450 due to: Connection
refused", new ConnectException("Connection refused"));
+
+ assertFalse(consumer.prePollCheck());
+
+ assertEquals(List.of("Cannot connect/login to:
smb://camel@localhost:4450 due to: Connection refused."
+ + " Will skip this poll."),
+ warnings.messages());
+ // no connection, so no attempt to create the starting directory
+ assertEquals(0, operations.buildDirectoryCalls);
+
+ // connected again, the starting directory is created
+ operations.connectFailure = null;
+
+ assertTrue(consumer.prePollCheck());
+ assertEquals(1, warnings.messages().size());
+ assertEquals(1, operations.buildDirectoryCalls);
+ }
+
+ /**
+ * Operations that connect, or fail to, without a server.
+ */
+ private static final class StubSmbOperations extends SmbOperations {
+
+ private RuntimeException connectFailure;
+ private RuntimeException buildDirectoryFailure;
+ private int buildDirectoryCalls;
+
+ StubSmbOperations(SmbConfiguration configuration) {
+ super(configuration);
+ }
+
+ @Override
+ protected void connectIfNecessary() {
+ if (connectFailure != null) {
+ throw connectFailure;
+ }
+ }
+
+ @Override
+ public boolean buildDirectory(String directory, boolean absolute) {
+ buildDirectoryCalls++;
+ if (buildDirectoryFailure != null) {
+ throw buildDirectoryFailure;
+ }
+ return true;
+ }
+ }
+}
diff --git
a/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbConsumerWarnCapture.java
b/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbConsumerWarnCapture.java
new file mode 100644
index 000000000000..5b4bae1a81d7
--- /dev/null
+++
b/components/camel-smb/src/test/java/org/apache/camel/component/smb/SmbConsumerWarnCapture.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.smb;
+
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+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.LoggerContext;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.config.Property;
+
+/**
+ * Captures the WARN messages of the smb consumer, between {@link #attach()}
and {@link #detach()}.
+ */
+final class SmbConsumerWarnCapture extends AbstractAppender {
+
+ private final List<String> messages = new CopyOnWriteArrayList<>();
+
+ SmbConsumerWarnCapture() {
+ super("SmbConsumerWarnCapture", null, null, true,
Property.EMPTY_ARRAY);
+ }
+
+ void attach() {
+ start();
+ LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
+ ctx.getConfiguration().getRootLogger().addAppender(this, Level.WARN,
null);
+ ctx.updateLoggers();
+ }
+
+ void detach() {
+ LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
+ ctx.getConfiguration().getRootLogger().removeAppender(getName());
+ ctx.updateLoggers();
+ stop();
+ }
+
+ List<String> messages() {
+ return messages;
+ }
+
+ @Override
+ public void append(LogEvent event) {
+ if (SmbConsumer.class.getName().equals(event.getLoggerName())) {
+ messages.add(event.getMessage().getFormattedMessage());
+ }
+ }
+}
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index 16c750325a91..5da921562c7e 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -548,6 +548,22 @@ starting directory (`autoCreate=true`), it now logs
`Cannot auto-create starting directory <directory> at: ... due to: ... Will
skip this poll.` instead.
See the xref:components::sftp-component.adoc[SFTP component].
+=== camel-smb - a starting directory that cannot be created
+
+When the server refuses to create the starting directory of the `smb` consumer
as the route starts (`autoCreate=true`),
+such as `STATUS_ACCESS_DENIED` on a read-only share, the route now starts and
the consumer tries again on each poll, as
+the
xref:camel-4x-upgrade-guide-4_13.adoc#_camel_file_camel_ftp_camel_smb_camel_azure_files[Camel
4.13 upgrade guide]
+describes. Before, the route failed to start. `SmbOperations.buildDirectory`
now throws a
+`GenericFileOperationFailedException` with the smbj exception as its cause, as
the ftp and sftp operations do. The
+producer and the moves that create a directory get this exception type too,
and still fail as before when the server
+refuses to create it. An `onException` or `doCatch` for the smbj exception
still matches it, as the cause.
+
+When the `smb` consumer connects but cannot create its starting directory
(`autoCreate=true`), it now logs
+`Cannot auto-create starting directory <directory> at: ... due to: ... Will
skip this poll.`, once. Before, it logged
+`Cannot connect/login to: ... Will skip this poll.`, or nothing when an
earlier poll had already logged that it could
+not connect. The `Cannot connect/login to: ... Will skip this poll.` WARN now
gives the reason before
+`Will skip this poll.`, such as `due to: Connection refused`, on one line.
+
=== camel-kafka - exactlyOnce: a rejected send that a doCatch catches skips
the record
With `exactlyOnce`, a Kafka send through the source producer after an earlier
send failed in the same transaction is