From e9bc2d67dc68c32631b65103604b7775d34f904b Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Fri, 13 Mar 2026 08:27:23 +0100 Subject: [PATCH] CAMEL-23052: Use type converter for HTTP query header in HttpSendDynamicAware Co-Authored-By: Claude Opus 4.6 --- .../camel/http/base/HttpSendDynamicAware.java | 4 +- .../http/base/HttpSendDynamicAwareTest.java | 121 ++++++++++++++++++ 2 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 components/camel-http-base/src/test/java/org/apache/camel/http/base/HttpSendDynamicAwareTest.java diff --git a/components/camel-http-base/src/main/java/org/apache/camel/http/base/HttpSendDynamicAware.java b/components/camel-http-base/src/main/java/org/apache/camel/http/base/HttpSendDynamicAware.java index 63f9b40faf15d..9119203f6bec8 100644 --- a/components/camel-http-base/src/main/java/org/apache/camel/http/base/HttpSendDynamicAware.java +++ b/components/camel-http-base/src/main/java/org/apache/camel/http/base/HttpSendDynamicAware.java @@ -108,10 +108,10 @@ public Processor createPreProcessor(Exchange exchange, DynamicAwareEntry entry) } if ((path == null || path.isEmpty()) && ObjectHelper.isNotEmpty(exchange.getIn().getHeader(Exchange.HTTP_PATH))) { - path = (String) exchange.getIn().getHeader(Exchange.HTTP_PATH); + path = exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class); } if (query == null && ObjectHelper.isNotEmpty(exchange.getIn().getHeader(Exchange.HTTP_QUERY))) { - query = (String) exchange.getIn().getHeader(Exchange.HTTP_QUERY); + query = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class); } if (path != null || query != null) { diff --git a/components/camel-http-base/src/test/java/org/apache/camel/http/base/HttpSendDynamicAwareTest.java b/components/camel-http-base/src/test/java/org/apache/camel/http/base/HttpSendDynamicAwareTest.java new file mode 100644 index 0000000000000..90994590c095e --- /dev/null +++ b/components/camel-http-base/src/test/java/org/apache/camel/http/base/HttpSendDynamicAwareTest.java @@ -0,0 +1,121 @@ +/* + * 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.http.base; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.spi.SendDynamicAware; +import org.apache.camel.support.DefaultExchange; +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.assertNotNull; + +class HttpSendDynamicAwareTest { + + private CamelContext camelContext; + + @BeforeEach + void setUp() throws Exception { + camelContext = new DefaultCamelContext(); + camelContext.start(); + } + + @AfterEach + void tearDown() throws Exception { + if (camelContext != null) { + camelContext.stop(); + } + } + + @Test + void testHttpQueryHeaderWithNonStringValue() throws Exception { + HttpSendDynamicAware aware = new HttpSendDynamicAware(); + aware.setScheme("http"); + + Exchange exchange = new DefaultExchange(camelContext); + // Set HTTP_QUERY as a non-String value (Integer) to verify type converter is used + exchange.getIn().setHeader(Exchange.HTTP_QUERY, 12345); + + Map properties = new LinkedHashMap<>(); + Map lenientProperties = new LinkedHashMap<>(); + SendDynamicAware.DynamicAwareEntry entry + = new SendDynamicAware.DynamicAwareEntry( + "http://localhost:8080", "http://localhost:8080", properties, + lenientProperties); + + Processor preProcessor = aware.createPreProcessor(exchange, entry); + assertNotNull(preProcessor, "PreProcessor should not be null when HTTP_QUERY header is set"); + + // Verify the pre-processor sets the query header correctly as a string + preProcessor.process(exchange); + assertEquals("12345", exchange.getIn().getHeader(Exchange.HTTP_QUERY)); + } + + @Test + void testHttpPathHeaderWithNonStringValue() throws Exception { + HttpSendDynamicAware aware = new HttpSendDynamicAware(); + aware.setScheme("http"); + + Exchange exchange = new DefaultExchange(camelContext); + // Set HTTP_PATH as a non-String value (Integer) to verify type converter is used + exchange.getIn().setHeader(Exchange.HTTP_PATH, 42); + + Map properties = new LinkedHashMap<>(); + Map lenientProperties = new LinkedHashMap<>(); + SendDynamicAware.DynamicAwareEntry entry + = new SendDynamicAware.DynamicAwareEntry( + "http://localhost:8080", "http://localhost:8080", properties, + lenientProperties); + + Processor preProcessor = aware.createPreProcessor(exchange, entry); + assertNotNull(preProcessor, "PreProcessor should not be null when HTTP_PATH header is set"); + + // Verify the pre-processor sets the path header correctly as a string + preProcessor.process(exchange); + assertEquals("42", exchange.getIn().getHeader(Exchange.HTTP_PATH)); + } + + @Test + void testHttpQueryHeaderWithStringValue() throws Exception { + HttpSendDynamicAware aware = new HttpSendDynamicAware(); + aware.setScheme("http"); + + Exchange exchange = new DefaultExchange(camelContext); + exchange.getIn().setHeader(Exchange.HTTP_QUERY, "foo=bar"); + + Map properties = new LinkedHashMap<>(); + Map lenientProperties = new LinkedHashMap<>(); + SendDynamicAware.DynamicAwareEntry entry + = new SendDynamicAware.DynamicAwareEntry( + "http://localhost:8080", "http://localhost:8080", properties, + lenientProperties); + + Processor preProcessor = aware.createPreProcessor(exchange, entry); + assertNotNull(preProcessor, "PreProcessor should not be null when HTTP_QUERY header is set"); + + preProcessor.process(exchange); + assertEquals("foo=bar", exchange.getIn().getHeader(Exchange.HTTP_QUERY)); + } +}