This is an automated email from the ASF dual-hosted git repository. gnodet pushed a commit to branch backport/28863-to-camel-4.18.x in repository https://gitbox.apache.org/repos/asf/camel.git
commit b5b97f910d8ab8bec98218e123eaf4455c282207 Author: Guillaume Nodet <[email protected]> AuthorDate: Tue Nov 3 12:04:08 2026 +0000 [backport camel-4.18.x] CAMEL-26487: camel-netty-http - include the endpoint options in the endpoint URI --- .../netty/http/HttpClientInitializerFactory.java | 2 +- .../component/netty/http/NettyHttpComponent.java | 5 +- .../component/netty/http/NettyHttpEndpoint.java | 18 ++++- .../component/netty/http/NettyHttpHelper.java | 2 +- .../netty/http/ManagedNettyEndpointTest.java | 5 +- .../NettyHttpEndpointDifferentOptionsTest.java | 85 ++++++++++++++++++++++ 6 files changed, 111 insertions(+), 6 deletions(-) diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpClientInitializerFactory.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpClientInitializerFactory.java index 9aa91f14807b..83dc2cc18d19 100644 --- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpClientInitializerFactory.java +++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/HttpClientInitializerFactory.java @@ -171,7 +171,7 @@ public class HttpClientInitializerFactory extends ClientInitializerFactory { if (producer.getConfiguration().getSslHandler() != null) { return producer.getConfiguration().getSslHandler(); } else if (sslContext != null) { - URI uri = new URI(producer.getEndpoint().getEndpointUri()); + URI uri = new URI(producer.getEndpoint().getAddressUri()); SSLEngine engine = sslContext.createSSLEngine(uri.getHost(), uri.getPort()); engine.setUseClientMode(true); SSLParameters sslParameters = engine.getSSLParameters(); diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java index 5ef61be7fca7..5bef1e92ccda 100644 --- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java +++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpComponent.java @@ -181,7 +181,10 @@ public class NettyHttpComponent extends NettyComponent String addressUri = URISupport.createRemainingURI(u, parameters).toString(); - NettyHttpEndpoint answer = new NettyHttpEndpoint(addressUri, this, config); + // the endpoint uri is the full uri with the endpoint options, so endpoints that differ only in their options + // are not equal, and the address uri is the http address to call + NettyHttpEndpoint answer = new NettyHttpEndpoint(uri, this, config); + answer.setAddressUri(addressUri); answer.getConfiguration().setMuteException(muteException); setProperties(answer, parameters); diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java index 5dc0300ef7a2..3644266ae9a6 100644 --- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java +++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpEndpoint.java @@ -54,6 +54,9 @@ public class NettyHttpEndpoint extends NettyEndpoint implements HeaderFilterStra private static final Logger LOG = LoggerFactory.getLogger(NettyHttpEndpoint.class); static final String PROXY_NOT_SUPPORTED_MESSAGE = "Netty Http Producer does not support proxy mode"; + // the http address: scheme, host, port, path and the query parameters that are not endpoint options + private String addressUri; + @UriParam private NettyHttpConfiguration configuration; @UriParam(label = "advanced", name = "configuration", @@ -255,8 +258,21 @@ public class NettyHttpEndpoint extends NettyEndpoint implements HeaderFilterStra } } + /** + * The HTTP address of this endpoint, which the producer sends its requests to: the scheme, host, port and path, and + * the query parameters that are not endpoint options. + */ + public String getAddressUri() { + return addressUri != null ? addressUri : getEndpointUri(); + } + + void setAddressUri(String addressUri) { + this.addressUri = addressUri; + } + private boolean isProxyProtocol() { - URI baseUri = URI.create(getEndpointBaseUri()); + String address = getAddressUri(); + URI baseUri = URI.create(StringHelper.before(address, "?", address)); String protocol = baseUri.getScheme(); return protocol != null && protocol.equalsIgnoreCase("proxy"); } diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java index 98aa998b0f0d..048d49a6db7e 100644 --- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java +++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java @@ -188,7 +188,7 @@ public final class NettyHttpHelper { // rest producer may provide an override url to be used which we should discard if using (hence the remove) String uri = (String) exchange.getIn().removeHeader(Exchange.REST_HTTP_URI); if (uri == null) { - uri = endpoint.getEndpointUri(); + uri = endpoint.getAddressUri(); } // NOTE: property placeholders are resolved at build time on the endpoint uri written in the route, diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/ManagedNettyEndpointTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/ManagedNettyEndpointTest.java index 6912655ef3e9..e402eb21743f 100644 --- a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/ManagedNettyEndpointTest.java +++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/ManagedNettyEndpointTest.java @@ -30,6 +30,7 @@ import org.junit.jupiter.api.condition.DisabledOnOs; import org.junit.jupiter.api.condition.OS; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; @DisabledOnOs(OS.AIX) public class ManagedNettyEndpointTest extends BaseNettyTestSupport { @@ -64,8 +65,8 @@ public class ManagedNettyEndpointTest extends BaseNettyTestSupport { ObjectName on = ObjectName .getInstance("org.apache.camel:context=" + context.getManagementName() - + ",type=endpoints,name=\"http://0.0.0.0:" + getPort() + "/foo\""); - mbeanServer.isRegistered(on); + + ",type=endpoints,name=\"netty-http://http://0.0.0.0:" + getPort() + "/foo\""); + assertTrue(mbeanServer.isRegistered(on)); // should only be 2 endpoints in JMX Set<ObjectName> set = getMBeanServer() diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpEndpointDifferentOptionsTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpEndpointDifferentOptionsTest.java new file mode 100644 index 000000000000..5b08a1c993bc --- /dev/null +++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpEndpointDifferentOptionsTest.java @@ -0,0 +1,85 @@ +/* + * 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.netty.http; + +import org.apache.camel.CamelExecutionException; +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.support.DefaultExchange; +import org.junit.jupiter.api.Test; + +import static org.apache.camel.test.junit6.TestSupport.assertIsInstanceOf; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class NettyHttpEndpointDifferentOptionsTest extends BaseNettyTestSupport { + + @Test + public void testEndpointsWithDifferentOptionsAreNotEqual() { + Endpoint plain = context.getEndpoint("netty-http:http://localhost:{{port}}/foo?bar=123"); + Endpoint option = context.getEndpoint("netty-http:http://localhost:{{port}}/foo?bar=123&throwExceptionOnFailure=false"); + + assertNotEquals(plain, option); + assertNotEquals(plain.getEndpointUri(), option.getEndpointUri()); + + // both send to the same address, which keeps the query parameters that are not endpoint options + String url = "http://localhost:" + getPort() + "/foo?bar=123"; + assertEquals(url, NettyHttpHelper.createURL(new DefaultExchange(context), (NettyHttpEndpoint) plain)); + assertEquals(url, NettyHttpHelper.createURL(new DefaultExchange(context), (NettyHttpEndpoint) option)); + } + + @Test + public void testProducerTemplateUsesTheOptionsOfEachEndpoint() { + CamelExecutionException e = assertThrows(CamelExecutionException.class, + () -> template.requestBody("netty-http:http://localhost:{{port}}/fail", "Hello World", String.class)); + NettyHttpOperationFailedException cause = assertIsInstanceOf(NettyHttpOperationFailedException.class, e.getCause()); + assertEquals(500, cause.getStatusCode()); + + // the same address with another option is another endpoint, with a producer of its own + String out = template.requestBody("netty-http:http://localhost:{{port}}/fail?throwExceptionOnFailure=false", + "Hello World", String.class); + assertEquals("Failed", out); + } + + @Test + public void testToDUsesTheOptionsOfEachEndpoint() { + CamelExecutionException e = assertThrows(CamelExecutionException.class, + () -> template.requestBodyAndHeader("direct:start", "Hello World", "throwOnFailure", "true", String.class)); + NettyHttpOperationFailedException cause = assertIsInstanceOf(NettyHttpOperationFailedException.class, e.getCause()); + assertEquals(500, cause.getStatusCode()); + + String out = template.requestBodyAndHeader("direct:start", "Hello World", "throwOnFailure", "false", String.class); + assertEquals("Failed", out); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("netty-http:http://0.0.0.0:{{port}}/fail") + .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(500)) + .transform().constant("Failed"); + + from("direct:start") + .toD("netty-http:http://localhost:{{port}}/fail?throwExceptionOnFailure=${header.throwOnFailure}"); + } + }; + } +}
