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

fmariani 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 60da88364213 fix(pollEnrich): preserve original headers
60da88364213 is described below

commit 60da883642135f6cb879b86ac6c8ddf8a28db864
Author: Croway <[email protected]>
AuthorDate: Thu Nov 27 17:06:34 2025 +0100

    fix(pollEnrich): preserve original headers
---
 .../org/apache/camel/processor/PollEnricher.java   | 11 ++++
 .../file/PollEnrichHeaderPropagationTest.java      | 69 ++++++++++++++++++++++
 2 files changed, 80 insertions(+)

diff --git 
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/PollEnricher.java
 
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/PollEnricher.java
index cec2a0733974..ecddf04a03d2 100644
--- 
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/PollEnricher.java
+++ 
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/PollEnricher.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.processor;
 
+import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.camel.AggregationStrategy;
@@ -621,7 +622,17 @@ public class PollEnricher extends BaseProcessorSupport 
implements IdAware, Route
         @Override
         public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
             if (newExchange != null) {
+                // preserve original headers before copying results
+                Map<String, Object> originalHeaders = new 
HashMap<>(oldExchange.getMessage().getHeaders());
+
+                // copy results from polled resource
                 copyResultsPreservePattern(oldExchange, newExchange);
+
+                // restore original headers, but don't overwrite headers from 
the polled resource
+                Map<String, Object> currentHeaders = 
oldExchange.getMessage().getHeaders();
+                for (Map.Entry<String, Object> entry : 
originalHeaders.entrySet()) {
+                    currentHeaders.putIfAbsent(entry.getKey(), 
entry.getValue());
+                }
             } else {
                 // if no newExchange then there was no message from the 
external resource,
                 // and therefore we should set an empty body to indicate this 
fact
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/file/PollEnrichHeaderPropagationTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/file/PollEnrichHeaderPropagationTest.java
new file mode 100644
index 000000000000..f98a620a80a2
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/file/PollEnrichHeaderPropagationTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Test to verify that headers set before pollEnrich are preserved after 
pollEnrich operation.
+ */
+public class PollEnrichHeaderPropagationTest extends ContextTestSupport {
+
+    @Test
+    public void testHeadersPreservedAfterPollEnrich() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedBodiesReceived("Hello Camel from file");
+        mock.expectedHeaderReceived("foo", "bar");
+        mock.expectedHeaderReceived("customHeader", "customValue");
+
+        // Create a file to be consumed by pollEnrich
+        template.sendBodyAndHeader(fileUri(), "Hello Camel from file", 
Exchange.FILE_NAME, "file.txt");
+
+        // Trigger the route
+        template.sendBody("direct:start", "trigger");
+
+        assertMockEndpointsSatisfied();
+
+        // Verify headers are preserved
+        Exchange exchange = mock.getReceivedExchanges().get(0);
+        assertEquals("bar", exchange.getIn().getHeader("foo"));
+        assertEquals("customValue", 
exchange.getIn().getHeader("customHeader"));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:start")
+                        .setHeader("foo", constant("bar"))
+                        .setHeader("customHeader", constant("customValue"))
+                        .pollEnrich(fileUri("?fileName=file.txt"), 3000)
+                        .convertBodyTo(String.class)
+                        .log("${headers}")
+                        .to("mock:result");
+            }
+        };
+    }
+}

Reply via email to