This is an automated email from the ASF dual-hosted git repository.

davsclaus 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 5d7230d6eb31 CAMEL-27168: camel-google-mail - the stream consumer 
marks as read the consumed mail (#30359)
5d7230d6eb31 is described below

commit 5d7230d6eb31ced646820c71cc4a90071c9ddfc7
Author: allthingssecurity <[email protected]>
AuthorDate: Thu Nov 19 19:20:24 2026 +0530

    CAMEL-27168: camel-google-mail - the stream consumer marks as read the 
consumed mail (#30359)
    
    With markAsRead (the default), GoogleMailStreamConsumer took the id of the
    mail to mark as read (or as unread again after a failure) from the
    CamelGoogleMailId header as the route left it. A route that removed the
    headers, or replaced the message, marked nothing, so the same mail was
    consumed at every poll (default query is:unread); a route that changed the
    header marked another mail.
    
    - The consumer takes the id of the mail before the exchange is routed and
      calls the new processCommit(Exchange, String, String) and
      processRollback(Exchange, String, String) with it; the former two-argument
      methods are deprecated and delegate with the header.
    - Component page and 4.24 upgrade guide updated.
    - GoogleMailStreamConsumerMarkAsReadTest: a route that removes the headers,
      and a route that sets the header to another id.
    
    Related: CAMEL-27015 (#30168), CAMEL-27064 (#30227).
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../catalog/docs/google-mail-stream-component.adoc |  3 +-
 .../main/docs/google-mail-stream-component.adoc    |  3 +-
 .../mail/stream/GoogleMailStreamConsumer.java      | 49 ++++++++---
 .../GoogleMailStreamConsumerMarkAsReadTest.java    | 94 ++++++++++++++++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc    | 11 +++
 5 files changed, 147 insertions(+), 13 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/google-mail-stream-component.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/google-mail-stream-component.adoc
index b9aa3fa91c69..ec9a63b4f19b 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/google-mail-stream-component.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/google-mail-stream-component.adoc
@@ -66,7 +66,8 @@ include::partial$component-endpoint-headers.adoc[]
 == Usage
 
 The consumer polls the mailbox for the messages that match `query`, and marks 
them as read.
-`is:unread` and `markAsRead` are the defaults. The headers carry the sender, 
the subject and the other fields:
+`is:unread` and `markAsRead` are the defaults. The consumer marks as read the 
message it consumed, whatever the route
+does with the `CamelGoogleMailId` header. The headers carry the sender, the 
subject and the other fields:
 
 [source,yaml]
 ----
diff --git 
a/components/camel-google/camel-google-mail/src/main/docs/google-mail-stream-component.adoc
 
b/components/camel-google/camel-google-mail/src/main/docs/google-mail-stream-component.adoc
index b9aa3fa91c69..ec9a63b4f19b 100644
--- 
a/components/camel-google/camel-google-mail/src/main/docs/google-mail-stream-component.adoc
+++ 
b/components/camel-google/camel-google-mail/src/main/docs/google-mail-stream-component.adoc
@@ -66,7 +66,8 @@ include::partial$component-endpoint-headers.adoc[]
 == Usage
 
 The consumer polls the mailbox for the messages that match `query`, and marks 
them as read.
-`is:unread` and `markAsRead` are the defaults. The headers carry the sender, 
the subject and the other fields:
+`is:unread` and `markAsRead` are the defaults. The consumer marks as read the 
message it consumed, whatever the route
+does with the `CamelGoogleMailId` header. The headers carry the sender, the 
subject and the other fields:
 
 [source,yaml]
 ----
diff --git 
a/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/stream/GoogleMailStreamConsumer.java
 
b/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/stream/GoogleMailStreamConsumer.java
index 6d11d3af72b9..012763c49f0a 100644
--- 
a/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/stream/GoogleMailStreamConsumer.java
+++ 
b/components/camel-google/camel-google-mail/src/main/java/org/apache/camel/component/google/mail/stream/GoogleMailStreamConsumer.java
@@ -153,12 +153,14 @@ public class GoogleMailStreamConsumer extends 
ScheduledBatchPollingConsumer {
             // update pending number of exchanges
             pendingExchanges = total - index - 1;
 
+            // the mail to mark as read (or unread) once the exchange is done. 
It is taken now, as the route may change
+            // or remove the mail id header, or replace the message, before it 
ends
             final String id = 
exchange.getIn().getHeader(GoogleMailStreamConstants.MAIL_ID, String.class);
             // add on completion to handle after work when the exchange is done
             exchange.getExchangeExtension().addOnCompletion(new 
Synchronization() {
                 public void onComplete(Exchange exchange) {
                     try {
-                        processCommit(exchange, unreadLabelId);
+                        processCommit(exchange, unreadLabelId, id);
                     } finally {
                         inProgress.remove(id);
                     }
@@ -166,7 +168,7 @@ public class GoogleMailStreamConsumer extends 
ScheduledBatchPollingConsumer {
 
                 public void onFailure(Exchange exchange) {
                     try {
-                        processRollback(exchange, unreadLabelId);
+                        processRollback(exchange, unreadLabelId, id);
                     } finally {
                         inProgress.remove(id);
                     }
@@ -187,21 +189,31 @@ public class GoogleMailStreamConsumer extends 
ScheduledBatchPollingConsumer {
     }
 
     /**
-     * Strategy to delete the message after being processed.
+     * Strategy to mark the mail of the {@link 
GoogleMailStreamConstants#MAIL_ID} header as read after being processed.
+     *
+     * @deprecated use {@link #processCommit(Exchange, String, String)}, which 
the consumer calls instead
      */
+    @Deprecated(since = "4.24.0")
     protected void processCommit(Exchange exchange, String unreadLabelId) {
+        processCommit(exchange, unreadLabelId, 
exchange.getIn().getHeader(GoogleMailStreamConstants.MAIL_ID, String.class));
+    }
+
+    /**
+     * Strategy to mark the mail as read after being processed.
+     *
+     * @param exchange      the exchange
+     * @param unreadLabelId the id of the UNREAD label
+     * @param id            the id of the mail the exchange was created from
+     */
+    protected void processCommit(Exchange exchange, String unreadLabelId, 
String id) {
         try {
             if (getConfiguration().isMarkAsRead()) {
-                String id = 
exchange.getIn().getHeader(GoogleMailStreamConstants.MAIL_ID, String.class);
-
                 LOG.trace("Marking email {} as read", id);
 
                 List<String> remove = new ArrayList<>();
                 remove.add(unreadLabelId);
                 ModifyMessageRequest mods = new 
ModifyMessageRequest().setRemoveLabelIds(remove);
-                getClient().users().messages()
-                        .modify("me", 
exchange.getIn().getHeader(GoogleMailStreamConstants.MAIL_ID, String.class), 
mods)
-                        .execute();
+                getClient().users().messages().modify("me", id, 
mods).execute();
 
                 LOG.trace("Marked email {} as read", id);
             }
@@ -212,9 +224,25 @@ public class GoogleMailStreamConsumer extends 
ScheduledBatchPollingConsumer {
     }
 
     /**
-     * Strategy when processing the exchange failed.
+     * Strategy when processing the exchange failed, for the mail of the 
{@link GoogleMailStreamConstants#MAIL_ID}
+     * header.
+     *
+     * @deprecated use {@link #processRollback(Exchange, String, String)}, 
which the consumer calls instead
      */
+    @Deprecated(since = "4.24.0")
     protected void processRollback(Exchange exchange, String unreadLabelId) {
+        processRollback(exchange, unreadLabelId,
+                exchange.getIn().getHeader(GoogleMailStreamConstants.MAIL_ID, 
String.class));
+    }
+
+    /**
+     * Strategy when processing the exchange failed.
+     *
+     * @param exchange      the exchange
+     * @param unreadLabelId the id of the UNREAD label
+     * @param id            the id of the mail the exchange was created from
+     */
+    protected void processRollback(Exchange exchange, String unreadLabelId, 
String id) {
         if (!getConfiguration().isMarkAsRead()) {
             // the mail was never marked as read, so there is nothing to roll 
back
             return;
@@ -225,8 +253,7 @@ public class GoogleMailStreamConsumer extends 
ScheduledBatchPollingConsumer {
             List<String> add = new ArrayList<>();
             add.add(unreadLabelId);
             ModifyMessageRequest mods = new 
ModifyMessageRequest().setAddLabelIds(add);
-            getClient().users().messages()
-                    .modify("me", 
exchange.getIn().getHeader(GoogleMailStreamConstants.MAIL_ID, String.class), 
mods).execute();
+            getClient().users().messages().modify("me", id, mods).execute();
         } catch (Exception e) {
             getExceptionHandler().handleException("Error occurred marking the 
mail as unread. This exception is ignored.",
                     exchange, e);
diff --git 
a/components/camel-google/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/stream/GoogleMailStreamConsumerMarkAsReadTest.java
 
b/components/camel-google/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/stream/GoogleMailStreamConsumerMarkAsReadTest.java
new file mode 100644
index 000000000000..8e7bb94dfcbb
--- /dev/null
+++ 
b/components/camel-google/camel-google-mail/src/test/java/org/apache/camel/component/google/mail/stream/GoogleMailStreamConsumerMarkAsReadTest.java
@@ -0,0 +1,94 @@
+/*
+ * 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.google.mail.stream;
+
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * With markAsRead, the consumer marks the mail it consumed as read, whatever 
the route did with the
+ * {@link GoogleMailStreamConstants#MAIL_ID} header.
+ */
+class GoogleMailStreamConsumerMarkAsReadTest extends CamelTestSupport {
+
+    private static final String URI = 
"google-mail-stream://index?clientId=id&clientSecret=secret&delay=10";
+
+    private final FakeGmailClientFactory gmail = new FakeGmailClientFactory();
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        context.getComponent("google-mail-stream", 
GoogleMailStreamComponent.class).setClientFactory(gmail);
+        return context;
+    }
+
+    @Test
+    void marksTheConsumedMailAsReadWhenTheRouteRemovedTheHeaders() throws 
Exception {
+        gmail.addUnread("m1", "hello");
+        gmail.addUnread("m2", "world");
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceivedInAnyOrder("hello", "world");
+        // consumed once: marked as read, so the later polls must not route it 
again
+        mock.setAssertPeriod(500);
+
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() {
+                from(URI).removeHeaders("*").to("mock:result");
+            }
+        });
+
+        mock.assertIsSatisfied();
+        await().atMost(10, TimeUnit.SECONDS).until(() -> !gmail.isUnread("m1") 
&& !gmail.isUnread("m2"));
+        // marked as read, so not listed again by the next polls
+        assertEquals(1, gmail.gets("m1"));
+        assertEquals(1, gmail.gets("m2"));
+        assertEquals(List.of(), gmail.unexpectedRequests());
+    }
+
+    @Test
+    void marksTheConsumedMailAsReadWhenTheRouteChangedTheHeader() throws 
Exception {
+        gmail.addUnread("m1", "hello");
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("hello");
+        // consumed once: marked as read, so the later polls must not route it 
again
+        mock.setAssertPeriod(500);
+
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() {
+                from(URI)
+                        .setHeader(GoogleMailStreamConstants.MAIL_ID, 
constant("another-mail"))
+                        .to("mock:result");
+            }
+        });
+
+        mock.assertIsSatisfied();
+        await().atMost(10, TimeUnit.SECONDS).until(() -> 
!gmail.isUnread("m1"));
+        // marked as read, so not listed again by the next polls
+        assertEquals(1, gmail.gets("m1"));
+    }
+}
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 d166f581099c..af92999f5615 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
@@ -7108,6 +7108,17 @@ poll. The failure is given to the exception handler of 
the consumer, with the ob
 whole bucket, one such object stopped the consumer from consuming any object. 
The poll still fails when none of the
 listed objects could be got.
 
+=== camel-google-mail - the stream consumer marks as read the mail it consumed
+
+With `markAsRead` (the default), the `google-mail-stream` consumer now marks 
as read the mail it consumed, and marks it
+as unread again when its exchange failed. Before, it took the id of the mail 
from the `CamelGoogleMailId` header as the
+route left it. A route that removed the headers, or replaced the message, 
marked nothing as read, so the mail was
+consumed again by every poll (with the default query `is:unread`). A route 
that set the header marked that other mail.
+
+The consumer now calls `processCommit(Exchange, String, String)` and 
`processRollback(Exchange, String, String)` with
+the id of the mail. A subclass of `GoogleMailStreamConsumer` that overrides 
`processCommit(Exchange, String)` or
+`processRollback(Exchange, String)` must override the new methods instead. The 
former methods are deprecated.
+
 === camel-google-storage, camel-minio, camel-alibaba-mns - the consumer 
deletes or moves what it consumed
 
 With `deleteAfterRead` or `moveAfterRead`, the `google-storage` and `minio` 
consumers now delete or move the object

Reply via email to