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 93191eed9564 CAMEL-27104: camel-azure-files - the route starts when 
the share refuses to create the starting directory or refuses the connection 
(#30266)
93191eed9564 is described below

commit 93191eed956483924fa73e26aa9912a4c6036b35
Author: Federico Mariani <[email protected]>
AuthorDate: Tue Nov 17 17:00:54 2026 +0100

    CAMEL-27104: camel-azure-files - the route starts when the share refuses to 
create the starting directory or refuses the connection (#30266)
    
    * CAMEL-27104: camel-azure-files - the route starts when the share refuses 
to create the starting directory or refuses the connection
    
    FilesConsumer.doStart logs a WARN and starts the consumer when the starting 
directory cannot be created, but it only
    catches GenericFileOperationFailedException, and FilesOperations.connect 
and buildDirectory let the ShareStorageException
    of the Azure SDK through as it is, such as 403 
AuthorizationPermissionMismatch for an identity that may not create
    directories, or 403 AuthenticationFailed for a refused key, so the route 
failed to start. connect and buildDirectory now
    wrap the error in a GenericFileOperationFailedException with the same 
message, as the ftp and sftp operations do, so the
    route starts and each poll connects and tries to create the directory 
again, as CAMEL-22177 intended. The Azure error is
    the cause, so onException(ShareStorageException.class) still matches for 
the producer and the moves.
    
    * CAMEL-27104: camel-azure-files - each connect starts again from the share 
root, so refused polls no longer grow the directory stack
---
 .../camel/catalog/docs/azure-files-component.adoc  |   5 +
 .../src/main/docs/azure-files-component.adoc       |   5 +
 .../component/file/azure/FilesOperations.java      |  23 +-
 .../file/azure/FilesConsumerAutoCreateTest.java    | 263 +++++++++++++++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc    |  12 +
 5 files changed, 302 insertions(+), 6 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/azure-files-component.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/azure-files-component.adoc
index ee1055c04549..d35205788de3 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/azure-files-component.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/azure-files-component.adoc
@@ -57,6 +57,11 @@ performed to create the directory configured for the 
endpoint. The default
 value for `autoCreate` is `true`. On the contrary, the share must exist; it
 is not automatically created.
 
+When the consumer cannot create the directory as the route starts, for example
+because the identity is not allowed to create directories in the share, or the
+share refuses the credentials, the route still starts: the consumer logs a WARN
+and tries again on each poll.
+
 If no *port* number is provided, Camel will provide default values
 according to the protocol (https 443).
 
diff --git 
a/components/camel-azure/camel-azure-files/src/main/docs/azure-files-component.adoc
 
b/components/camel-azure/camel-azure-files/src/main/docs/azure-files-component.adoc
index ee1055c04549..d35205788de3 100644
--- 
a/components/camel-azure/camel-azure-files/src/main/docs/azure-files-component.adoc
+++ 
b/components/camel-azure/camel-azure-files/src/main/docs/azure-files-component.adoc
@@ -57,6 +57,11 @@ performed to create the directory configured for the 
endpoint. The default
 value for `autoCreate` is `true`. On the contrary, the share must exist; it
 is not automatically created.
 
+When the consumer cannot create the directory as the route starts, for example
+because the identity is not allowed to create directories in the share, or the
+share refuses the credentials, the route still starts: the consumer logs a WARN
+and tries again on each poll.
+
 If no *port* number is provided, Camel will provide default values
 according to the protocol (https 443).
 
diff --git 
a/components/camel-azure/camel-azure-files/src/main/java/org/apache/camel/component/file/azure/FilesOperations.java
 
b/components/camel-azure/camel-azure-files/src/main/java/org/apache/camel/component/file/azure/FilesOperations.java
index 8c66fc7e60c3..caca3ce165de 100644
--- 
a/components/camel-azure/camel-azure-files/src/main/java/org/apache/camel/component/file/azure/FilesOperations.java
+++ 
b/components/camel-azure/camel-azure-files/src/main/java/org/apache/camel/component/file/azure/FilesOperations.java
@@ -113,9 +113,15 @@ public class FilesOperations extends NormalizedOperations {
         log.trace("connect()");
         root = 
getClient().getShareClient(configuration.getShare()).getRootDirectoryClient();
         // TODO what about (starting) directory as the root?
+        // a new connection starts at the share root, also when the previous 
connect was refused
+        dirStack = new ArrayDeque<>();
         dirStack.push(root);
-        // TODO translate runtime exception to Camel one?
-        return existsRemote(root);
+        try {
+            return existsRemote(root);
+        } catch (RuntimeException e) {
+            // such as a share that refuses the credentials: the consumer 
starts, and connects again on each poll
+            throw new GenericFileOperationFailedException(e.getMessage(), e);
+        }
     }
 
     @Override
@@ -197,11 +203,16 @@ public class FilesOperations extends NormalizedOperations 
{
 
     @Override
     public boolean buildDirectory(String directory) throws 
GenericFileOperationFailedException {
-        boolean success = existsDirectory(directory);
-        if (!success) {
-            success = buildDirectoryStepByStep(directory);
+        try {
+            boolean success = existsDirectory(directory);
+            if (!success) {
+                success = buildDirectoryStepByStep(directory);
+            }
+            return success;
+        } catch (RuntimeException e) {
+            // such as no permission to create directories: the consumer 
starts, and tries again on each poll
+            throw new GenericFileOperationFailedException(e.getMessage(), e);
         }
-        return success;
     }
 
     private boolean buildDirectoryStepByStep(String dir) {
diff --git 
a/components/camel-azure/camel-azure-files/src/test/java/org/apache/camel/component/file/azure/FilesConsumerAutoCreateTest.java
 
b/components/camel-azure/camel-azure-files/src/test/java/org/apache/camel/component/file/azure/FilesConsumerAutoCreateTest.java
new file mode 100644
index 000000000000..10e87bf16fcb
--- /dev/null
+++ 
b/components/camel-azure/camel-azure-files/src/test/java/org/apache/camel/component/file/azure/FilesConsumerAutoCreateTest.java
@@ -0,0 +1,263 @@
+/*
+ * 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.azure;
+
+import java.time.Duration;
+import java.util.ArrayDeque;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.stream.Stream;
+
+import com.azure.core.http.HttpResponse;
+import com.azure.core.http.rest.PagedIterable;
+import com.azure.core.http.rest.SimpleResponse;
+import com.azure.core.util.Context;
+import com.azure.storage.file.share.ShareClient;
+import com.azure.storage.file.share.ShareDirectoryClient;
+import com.azure.storage.file.share.ShareServiceClient;
+import com.azure.storage.file.share.models.ShareFileItem;
+import com.azure.storage.file.share.models.ShareStorageException;
+import com.azure.storage.file.share.options.ShareDirectoryCreateOptions;
+import 
com.azure.storage.file.share.options.ShareListFilesAndDirectoriesOptions;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.ServiceStatus;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.file.GenericFileOperationFailedException;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.support.ScheduledPollConsumer;
+import org.apache.camel.test.junit6.CamelTestSupport;
+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.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.nullable;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * The consumer creates its starting directory as the route starts 
(autoCreate, the default). When the share refuses the
+ * connection or the creation of the directory, the route still starts, as 
with the ftp and smb consumers, and each poll
+ * tries again until it succeeds.
+ */
+class FilesConsumerAutoCreateTest extends CamelTestSupport {
+
+    private final ShareServiceClient service = mock(ShareServiceClient.class);
+    private final ShareClient share = mock(ShareClient.class);
+    private final ShareDirectoryClient root = mock(ShareDirectoryClient.class);
+    private final ShareDirectoryClient inbox = 
mock(ShareDirectoryClient.class);
+    private final AtomicBoolean connectRefused = new AtomicBoolean();
+    private final AtomicBoolean createRefused = new AtomicBoolean();
+    private final AtomicBoolean inboxExists = new AtomicBoolean();
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        context.addComponent("azure-files", new MockedShareComponent());
+        return context;
+    }
+
+    @BeforeEach
+    @SuppressWarnings("unchecked")
+    void mockShare() {
+        when(service.getShareClient("share")).thenReturn(share);
+        when(share.getRootDirectoryClient()).thenReturn(root);
+        when(root.existsWithResponse(nullable(Duration.class), 
any(Context.class))).thenAnswer(inv -> {
+            if (connectRefused.get()) {
+                throw forbidden("AuthenticationFailed");
+            }
+            return new SimpleResponse<>(null, 200, null, true);
+        });
+        when(root.getSubdirectoryClient("inbox")).thenReturn(inbox);
+        when(inbox.existsWithResponse(nullable(Duration.class), 
any(Context.class)))
+                .thenAnswer(inv -> new SimpleResponse<>(null, 
inboxExists.get() ? 200 : 404, null, inboxExists.get()));
+        when(root.createSubdirectoryIfNotExistsWithResponse(eq("inbox"), 
any(ShareDirectoryCreateOptions.class),
+                nullable(Duration.class), any(Context.class))).thenAnswer(inv 
-> {
+                    if (createRefused.get()) {
+                        throw forbidden("AuthorizationPermissionMismatch");
+                    }
+                    inboxExists.set(true);
+                    return new SimpleResponse<>(null, 201, null, inbox);
+                });
+        PagedIterable<ShareFileItem> empty = mock(PagedIterable.class);
+        when(empty.stream()).thenAnswer(inv -> Stream.empty());
+        
when(inbox.listFilesAndDirectories(any(ShareListFilesAndDirectoriesOptions.class),
 nullable(Duration.class),
+                any(Context.class))).thenReturn(empty);
+    }
+
+    @Test
+    void routeStartsWhenTheStartingDirectoryCannotBeCreated() throws Exception 
{
+        createRefused.set(true);
+
+        ScheduledPollConsumer consumer = startRoute();
+        verifyCreateAttempts(1);
+
+        // each poll tries again, and is skipped while the directory cannot be 
created
+        consumer.run();
+        verifyCreateAttempts(2);
+        verifyListings(0);
+
+        // permission granted: the next poll creates the directory and lists it
+        createRefused.set(false);
+        consumer.run();
+        verifyCreateAttempts(3);
+        verifyListings(1);
+    }
+
+    @Test
+    void routeStartsWhenTheShareRefusesTheConnection() throws Exception {
+        connectRefused.set(true);
+
+        ScheduledPollConsumer consumer = startRoute();
+        verifyCreateAttempts(0);
+
+        // each poll connects again, and is skipped while the connection is 
refused
+        consumer.run();
+        verifyCreateAttempts(0);
+        verifyListings(0);
+
+        // credentials fixed: the next poll connects, creates the directory 
and lists it
+        connectRefused.set(false);
+        consumer.run();
+        verifyCreateAttempts(1);
+        verifyListings(1);
+    }
+
+    @Test
+    void refusedConnectionsDoNotGrowTheDirectoryStack() throws Exception {
+        connectRefused.set(true);
+
+        ScheduledPollConsumer consumer = startRoute();
+        FilesOperations operations = ((FilesConsumer) 
consumer).getOperations();
+        for (int i = 0; i < 5; i++) {
+            consumer.run();
+        }
+
+        // each refused poll connected again: the current directory is still 
the share root, held once
+        assertEquals(1, ((ArrayDeque<?>) operations.backup()).size());
+    }
+
+    @Test
+    void routeStartsWhenTheStartingDirectoryExists() throws Exception {
+        inboxExists.set(true);
+
+        ScheduledPollConsumer consumer = startRoute();
+        consumer.run();
+
+        verifyCreateAttempts(0);
+        verifyListings(1);
+    }
+
+    @Test
+    void refusedCreationIsAnOperationFailureCausedByTheAzureError() {
+        createRefused.set(true);
+        FilesEndpoint endpoint = 
context.getEndpoint("azure-files://account/share/inbox", FilesEndpoint.class);
+        FilesOperations operations = endpoint.createRemoteFileOperations();
+        operations.connect(endpoint.getConfiguration(), null);
+
+        GenericFileOperationFailedException e = 
assertThrows(GenericFileOperationFailedException.class,
+                () -> operations.buildDirectory("/inbox", true));
+        assertEquals(403, assertInstanceOf(ShareStorageException.class, 
e.getCause()).getStatusCode());
+    }
+
+    @Test
+    void 
producerFailureToCreateTheDirectoryStillMatchesOnExceptionOfTheAzureError() 
throws Exception {
+        // the producer builds directories with the same operations: the error 
handler finds the Azure error as the cause
+        createRefused.set(true);
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() {
+                
onException(ShareStorageException.class).handled(true).to("mock:refused");
+                from("direct:write").to("azure-files://account/share/inbox");
+            }
+        });
+        getMockEndpoint("mock:refused").expectedMessageCount(1);
+
+        template.sendBodyAndHeader("direct:write", "hello", 
Exchange.FILE_NAME, "hello.txt");
+
+        MockEndpoint.assertIsSatisfied(context);
+        verifyCreateAttempts(1);
+    }
+
+    @Test
+    void refusedConnectionIsAnOperationFailureCausedByTheAzureError() {
+        connectRefused.set(true);
+        FilesEndpoint endpoint = 
context.getEndpoint("azure-files://account/share/inbox", FilesEndpoint.class);
+        FilesOperations operations = endpoint.createRemoteFileOperations();
+
+        GenericFileOperationFailedException e = 
assertThrows(GenericFileOperationFailedException.class,
+                () -> operations.connect(endpoint.getConfiguration(), null));
+        assertEquals(403, assertInstanceOf(ShareStorageException.class, 
e.getCause()).getStatusCode());
+    }
+
+    private ScheduledPollConsumer startRoute() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() {
+                
from("azure-files://account/share/inbox?startScheduler=false").routeId("inbox")
+                        .to("mock:result");
+            }
+        });
+        assertEquals(ServiceStatus.Started, 
context.getRouteController().getRouteStatus("inbox"));
+        return (ScheduledPollConsumer) context.getRoute("inbox").getConsumer();
+    }
+
+    private void verifyCreateAttempts(int attempts) {
+        verify(root, 
times(attempts)).createSubdirectoryIfNotExistsWithResponse(anyString(),
+                any(ShareDirectoryCreateOptions.class), 
nullable(Duration.class), any(Context.class));
+    }
+
+    private void verifyListings(int listings) {
+        verify(inbox, 
times(listings)).listFilesAndDirectories(any(ShareListFilesAndDirectoriesOptions.class),
+                nullable(Duration.class), any(Context.class));
+    }
+
+    private static ShareStorageException forbidden(String errorCode) {
+        HttpResponse response = mock(HttpResponse.class);
+        when(response.getStatusCode()).thenReturn(403);
+        return new ShareStorageException("Status code 403, " + errorCode, 
response, null);
+    }
+
+    /**
+     * Its endpoints use the mocked share instead of connecting to Azure.
+     */
+    private final class MockedShareComponent extends FilesComponent {
+
+        @Override
+        protected FilesEndpoint buildFileEndpoint(String uri, String 
remaining, Map<String, Object> parameters)
+                throws Exception {
+            return new FilesEndpoint(uri, this, new 
FilesConfiguration(FilesURIStrings.getBaseURI(uri))) {
+                @Override
+                public FilesOperations createRemoteFileOperations() {
+                    return new FilesOperations(this) {
+                        @Override
+                        public ShareServiceClient getClient() {
+                            return service;
+                        }
+                    };
+                }
+            };
+        }
+    }
+}
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 9821bfdd4f17..a10b41a19b9a 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
@@ -589,6 +589,18 @@ When the `smb` consumer connects but cannot create its 
starting directory (`auto
 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-azure-files - a starting directory that cannot be created, or a 
refused connection, no longer fails the route start
+
+When the share refuses to create the starting directory of the `azure-files` 
consumer as the route starts
+(`autoCreate=true`), such as `403 AuthorizationPermissionMismatch`, or refuses 
the connection, such as
+`403 AuthenticationFailed`, 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. `FilesOperations.connect` and 
`FilesOperations.buildDirectory` now throw a
+`GenericFileOperationFailedException` with the `ShareStorageException` of the 
Azure SDK as its cause, as the ftp and sftp
+operations do with the error of their client. The producer and the moves that 
connect or create a directory get this
+exception type too, and still fail as before when the share refuses; an 
`onException(ShareStorageException.class)`
+still matches it, as the cause.
+
 === 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

Reply via email to