This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27669-rest-producer-options-query in repository https://gitbox.apache.org/repos/asf/camel.git
commit 5d8ba03f6f446da8f06e696395bc39001c796d02 Author: Claus Ibsen <[email protected]> AuthorDate: Fri Nov 27 16:29:26 2026 +0100 CAMEL-27669: camel-rest - producer component options on the rest URI are not sent as query parameters The parameters of a rest endpoint uri that the endpoint does not know are sent as query parameters, except the options of the producer component. These were left out only when producerComponentName was set on the endpoint or the component. With the producerComponent of the rest configuration, or an auto-detected producer component, an option such as authPassword of camel-http was applied to the http endpoint and also sent in the query string of the request URL. The rest configuration is now used too, and the producer leaves out the options of the producer component it found when it is created. Unknown parameters stay query parameters, and the queryParameters option is kept. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../org/apache/camel/catalog/components/rest.json | 2 +- .../apache/camel/catalog/docs/rest-component.adoc | 18 +++ .../http/HttpRestProducerQueryParametersTest.java | 109 +++++++++++++ .../org/apache/camel/component/rest/rest.json | 2 +- .../camel-rest/src/main/docs/rest-component.adoc | 18 +++ .../apache/camel/component/rest/RestComponent.java | 42 +---- .../apache/camel/component/rest/RestEndpoint.java | 70 ++++++++- .../rest/RestProducerComponentOptionsTest.java | 171 +++++++++++++++++++++ .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 12 ++ .../endpoint/dsl/RestEndpointBuilderFactory.java | 4 +- 10 files changed, 407 insertions(+), 41 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/rest.json b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/rest.json index d58e4f64583a..ff4cf9eee56c 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/rest.json +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/rest.json @@ -67,7 +67,7 @@ "bindingMode": { "index": 14, "kind": "parameter", "displayName": "Binding Mode", "group": "producer", "label": "producer", "required": false, "type": "enum", "javaType": "org.apache.camel.spi.RestConfiguration.RestBindingMode", "enum": [ "auto", "off", "json", "xml", "json_xml" ], "deprecated": false, "autowired": false, "secret": false, "description": "The binding mode of the producer, which replaces the bindingMode of the rest configuration (off by default). With json, xml or json [...] "host": { "index": 15, "kind": "parameter", "displayName": "Host", "group": "producer", "label": "producer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "The scheme, host and port of the REST service to call, with an optional base path, such as http:\/\/myserver:8080\/foo. The scheme is http when it is not given. If not set, the host option of the component, or else the host and port of [...] "producerComponentName": { "index": 16, "kind": "parameter", "displayName": "Producer Component Name", "group": "producer", "label": "producer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "The name of the Camel component that sends the HTTP request, such as http or vertx-http. If not set, the producerComponent of the rest configuration is used. If that is not set either, the producer ta [...] - "queryParameters": { "index": 17, "kind": "parameter", "displayName": "Query Parameters", "group": "producer", "label": "producer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "The query string of the request, such as foo=123&bar=456. A value {name} is filled with the header or the exchange variable of that name, and a parameter with the value {name?} is left out when neither is set. The [...] + "queryParameters": { "index": 17, "kind": "parameter", "displayName": "Query Parameters", "group": "producer", "label": "producer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "The query string of the request, such as foo=123&bar=456. A value {name} is filled with the header or the exchange variable of that name, and a parameter with the value {name?} is left out when neither is set. The [...] "lazyStartProducer": { "index": 18, "kind": "parameter", "displayName": "Lazy Start Producer", "group": "producer (advanced)", "label": "producer,advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a produ [...] } } diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/rest-component.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/rest-component.adoc index 48119ad7dc0a..bde31fa4f3ab 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/rest-component.adoc +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/rest-component.adoc @@ -312,6 +312,24 @@ from("direct:start") .to("rest:get:hello/{me}"); ---- +The parameters of the endpoint URI that are not options of the rest endpoint are sent as query parameters of +the request, except the options of the component that sends the request. For example, with http as the producer +component, the following route calls `+http://myserver:8080/foo/hello/{me}?page=2+`, and logs in with the +`authUsername` and `authPassword` options of the http component. +This is the same whether the producer component is set with the `producerComponentName` option, with the +`producerComponent` of the rest configuration, or is detected automatically. +To send a query parameter that has the same name as an option of the producer component, set it in the +`queryParameters` option. + +._Java-only: Java programmatic REST configuration API_ +[source,java] +---- +restConfiguration().host("myserver:8080/foo").producerComponent("http"); + +from("direct:start") + .to("rest:get:hello/{me}?page=2&authUsername=scott&authPassword=RAW(tiger)"); +---- + === Rest producer binding diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRestProducerQueryParametersTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRestProducerQueryParametersTest.java new file mode 100644 index 000000000000..48010cfe5259 --- /dev/null +++ b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpRestProducerQueryParametersTest.java @@ -0,0 +1,109 @@ +/* + * 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.http; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.concurrent.atomic.AtomicReference; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpServer; +import org.apache.camel.Exchange; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.impl.DefaultCamelContext; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * The http options given on a rest endpoint uri are applied to the http producer and are not sent as query parameters, + * however the http component is chosen as the producer component of the rest endpoint. + */ +public class HttpRestProducerQueryParametersTest { + + private static final String OPTIONS = "authMethod=Basic&authenticationPreemptive=true" + + "&authUsername=ann&authPassword=RAW(secret)&page=2"; + + private final AtomicReference<String> query = new AtomicReference<>(); + private final AtomicReference<String> authorization = new AtomicReference<>(); + + private HttpServer server; + private DefaultCamelContext context; + + @BeforeEach + public void start() throws IOException { + server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); + server.createContext("/api/items", this::handle); + server.start(); + + context = new DefaultCamelContext(); + context.start(); + } + + @AfterEach + public void stop() { + context.stop(); + server.stop(0); + } + + @Test + public void shouldNotSendHttpOptionsWithProducerComponentOfRestConfiguration() { + context.getRestConfiguration().setProducerComponent("http"); + + send("rest:get:api/items?host=" + host() + "&" + OPTIONS); + + assertThat(query.get()).isEqualTo("page=2"); + assertThat(authorization.get()).isEqualTo("Basic YW5uOnNlY3JldA=="); + } + + @Test + public void shouldNotSendHttpOptionsWithAutoDetectedProducerComponent() { + send("rest:get:api/items?host=" + host() + "&" + OPTIONS); + + assertThat(query.get()).isEqualTo("page=2"); + assertThat(authorization.get()).isEqualTo("Basic YW5uOnNlY3JldA=="); + } + + @Test + public void shouldNotSendHttpOptionsWithProducerComponentName() { + send("rest:get:api/items?host=" + host() + "&producerComponentName=http&" + OPTIONS); + + assertThat(query.get()).isEqualTo("page=2"); + assertThat(authorization.get()).isEqualTo("Basic YW5uOnNlY3JldA=="); + } + + private String host() { + return "localhost:" + server.getAddress().getPort(); + } + + private void send(String uri) { + ProducerTemplate template = context.createProducerTemplate(); + Exchange exchange = template.request(uri, null); + assertThat(exchange.getException()).isNull(); + assertThat(exchange.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class)).isEqualTo(200); + } + + private void handle(HttpExchange exchange) throws IOException { + query.set(exchange.getRequestURI().getRawQuery()); + authorization.set(exchange.getRequestHeaders().getFirst("Authorization")); + exchange.sendResponseHeaders(200, -1); + exchange.close(); + } +} diff --git a/components/camel-rest/src/generated/resources/META-INF/org/apache/camel/component/rest/rest.json b/components/camel-rest/src/generated/resources/META-INF/org/apache/camel/component/rest/rest.json index d58e4f64583a..ff4cf9eee56c 100644 --- a/components/camel-rest/src/generated/resources/META-INF/org/apache/camel/component/rest/rest.json +++ b/components/camel-rest/src/generated/resources/META-INF/org/apache/camel/component/rest/rest.json @@ -67,7 +67,7 @@ "bindingMode": { "index": 14, "kind": "parameter", "displayName": "Binding Mode", "group": "producer", "label": "producer", "required": false, "type": "enum", "javaType": "org.apache.camel.spi.RestConfiguration.RestBindingMode", "enum": [ "auto", "off", "json", "xml", "json_xml" ], "deprecated": false, "autowired": false, "secret": false, "description": "The binding mode of the producer, which replaces the bindingMode of the rest configuration (off by default). With json, xml or json [...] "host": { "index": 15, "kind": "parameter", "displayName": "Host", "group": "producer", "label": "producer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "The scheme, host and port of the REST service to call, with an optional base path, such as http:\/\/myserver:8080\/foo. The scheme is http when it is not given. If not set, the host option of the component, or else the host and port of [...] "producerComponentName": { "index": 16, "kind": "parameter", "displayName": "Producer Component Name", "group": "producer", "label": "producer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "The name of the Camel component that sends the HTTP request, such as http or vertx-http. If not set, the producerComponent of the rest configuration is used. If that is not set either, the producer ta [...] - "queryParameters": { "index": 17, "kind": "parameter", "displayName": "Query Parameters", "group": "producer", "label": "producer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "The query string of the request, such as foo=123&bar=456. A value {name} is filled with the header or the exchange variable of that name, and a parameter with the value {name?} is left out when neither is set. The [...] + "queryParameters": { "index": 17, "kind": "parameter", "displayName": "Query Parameters", "group": "producer", "label": "producer", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "The query string of the request, such as foo=123&bar=456. A value {name} is filled with the header or the exchange variable of that name, and a parameter with the value {name?} is left out when neither is set. The [...] "lazyStartProducer": { "index": 18, "kind": "parameter", "displayName": "Lazy Start Producer", "group": "producer (advanced)", "label": "producer,advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a produ [...] } } diff --git a/components/camel-rest/src/main/docs/rest-component.adoc b/components/camel-rest/src/main/docs/rest-component.adoc index 48119ad7dc0a..bde31fa4f3ab 100644 --- a/components/camel-rest/src/main/docs/rest-component.adoc +++ b/components/camel-rest/src/main/docs/rest-component.adoc @@ -312,6 +312,24 @@ from("direct:start") .to("rest:get:hello/{me}"); ---- +The parameters of the endpoint URI that are not options of the rest endpoint are sent as query parameters of +the request, except the options of the component that sends the request. For example, with http as the producer +component, the following route calls `+http://myserver:8080/foo/hello/{me}?page=2+`, and logs in with the +`authUsername` and `authPassword` options of the http component. +This is the same whether the producer component is set with the `producerComponentName` option, with the +`producerComponent` of the rest configuration, or is detected automatically. +To send a query parameter that has the same name as an option of the producer component, set it in the +`queryParameters` option. + +._Java-only: Java programmatic REST configuration API_ +[source,java] +---- +restConfiguration().host("myserver:8080/foo").producerComponent("http"); + +from("direct:start") + .to("rest:get:hello/{me}?page=2&authUsername=scott&authPassword=RAW(tiger)"); +---- + === Rest producer binding diff --git a/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestComponent.java b/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestComponent.java index 2af48f680d7e..fd726292eef8 100644 --- a/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestComponent.java +++ b/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestComponent.java @@ -25,9 +25,7 @@ import org.apache.camel.spi.annotations.Component; import org.apache.camel.support.CamelContextHelper; import org.apache.camel.support.HeaderFilterStrategyComponent; import org.apache.camel.util.FileUtil; -import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.StringHelper; -import org.apache.camel.util.URISupport; /** * Rest component. @@ -98,26 +96,12 @@ public class RestComponent extends HeaderFilterStrategyComponent { setProperties(answer, parameters); if (!parameters.isEmpty()) { // use only what remains and at this point parameters that have been used have been removed - // without overwriting any query parameters set via queryParameters endpoint option - final Map<String, Object> queryParameters = new LinkedHashMap<>(parameters); - - // filter out known options from the producer, as they should not be in the query parameters - EndpointUriFactory factory = getEndpointUriFactory(pname); - if (factory != null) { - for (String key : parameters.keySet()) { - if (factory.propertyNames().contains(key)) { - queryParameters.remove(key); - } - } - } - - final Map<String, Object> existingQueryParameters = URISupport.parseQuery(answer.getQueryParameters()); - queryParameters.putAll(existingQueryParameters); - - final String remainingParameters = URISupport.createQueryString(queryParameters); - if (ObjectHelper.isNotEmpty(remainingParameters)) { - answer.setQueryParameters(remainingParameters); - } + // without overwriting any query parameters set via queryParameters endpoint option. + // the options of the producer component are not query parameters: they are left out here when the + // producer component is configured (endpoint, component or rest configuration), and else when the + // producer is created, as then the producer component is known + String producer = pname != null ? pname : config.getProducerComponent(); + answer.initUriQueryParameters(new LinkedHashMap<>(parameters), producer); } answer.setParameters(parameters); @@ -197,18 +181,4 @@ public class RestComponent extends HeaderFilterStrategyComponent { this.host = host; } - // **************************************** - // Helpers - // **************************************** - - private EndpointUriFactory getEndpointUriFactory(String name) { - if (name != null) { - UriFactoryResolver resolver - = getCamelContext().getCamelContextExtension().getContextPlugin(UriFactoryResolver.class); - if (resolver != null) { - return resolver.resolveFactory(name, getCamelContext()); - } - } - return null; - } } diff --git a/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestEndpoint.java b/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestEndpoint.java index 0c9a92a967dc..00f06f66d785 100644 --- a/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestEndpoint.java +++ b/components/camel-rest/src/main/java/org/apache/camel/component/rest/RestEndpoint.java @@ -16,6 +16,8 @@ */ package org.apache.camel.component.rest; +import java.net.URISyntaxException; +import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import java.util.StringJoiner; @@ -30,12 +32,14 @@ import org.apache.camel.Producer; import org.apache.camel.spi.BodyType; import org.apache.camel.spi.BodyType.Carrier; import org.apache.camel.spi.BodyTypes; +import org.apache.camel.spi.EndpointUriFactory; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.RestConfiguration; import org.apache.camel.spi.RestConsumerFactory; import org.apache.camel.spi.RestProducerFactory; import org.apache.camel.spi.RestRegistry; import org.apache.camel.spi.UriEndpoint; +import org.apache.camel.spi.UriFactoryResolver; import org.apache.camel.spi.UriParam; import org.apache.camel.spi.UriPath; import org.apache.camel.support.CamelContextHelper; @@ -47,6 +51,7 @@ import org.apache.camel.util.ArtifactUtils; import org.apache.camel.util.HostUtils; import org.apache.camel.util.MimeTypeHelper; import org.apache.camel.util.ObjectHelper; +import org.apache.camel.util.URISupport; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -157,7 +162,7 @@ public class RestEndpoint extends DefaultEndpoint { description = "The query string of the request, such as foo=123&bar=456. A value {name} is filled with the" + " header or the exchange variable of that name, and a parameter with the value {name?} is" + " left out when neither is set. The endpoint also adds the parameters of its URI that it" - + " does not know, except the options of the component that producerComponentName names.") + + " does not know, except the options of the component that sends the HTTP request.") private String queryParameters; @UriParam(label = "producer", enums = "auto,off,json,xml,json_xml", description = "The binding mode of the producer, which replaces the bindingMode of the rest configuration" @@ -181,6 +186,11 @@ public class RestEndpoint extends DefaultEndpoint { private String consumerComponentName; private Map<String, Object> parameters; + // the parameters of the endpoint uri that the endpoint does not know, which are added to the query parameters, + // except the options of the producer component (which may only be known when the producer is created) + private volatile Map<String, Object> uriQueryParameters; + // the queryParameters option as configured on the endpoint + private String configuredQueryParameters; public RestEndpoint(String endpointUri, RestComponent component) { super(endpointUri, component); @@ -351,6 +361,10 @@ public class RestEndpoint extends DefaultEndpoint { LOG.debug("Using RestProducerFactory: {}", factoryResult.factory); + // the producer component is known now, so its options are not query parameters + // (when it was not configured, but auto-detected, they are still in the query parameters) + removeProducerOptionsFromQueryParameters(factoryResult.name); + RestConfiguration config = CamelContextHelper.getRestConfiguration(getCamelContext(), null, factoryResult.name); Producer producer = createProducerInstance(factoryResult.factory, apiDocFactory, config); @@ -362,6 +376,60 @@ public class RestEndpoint extends DefaultEndpoint { return answer; } + /** + * Adds the parameters of the endpoint uri that the endpoint does not know to the query parameters, except the + * options of the given producer component. + * + * @param uriParameters the parameters of the endpoint uri that the endpoint does not know + * @param producerComponentName the name of the producer component when it is configured, or null + */ + void initUriQueryParameters(Map<String, Object> uriParameters, String producerComponentName) + throws URISyntaxException { + this.configuredQueryParameters = queryParameters; + this.uriQueryParameters = withoutProducerOptions(uriParameters, producerComponentName); + updateQueryParameters(); + } + + private void removeProducerOptionsFromQueryParameters(String producerComponentName) throws URISyntaxException { + Map<String, Object> current = uriQueryParameters; + if (current == null || current.isEmpty()) { + return; + } + Map<String, Object> answer = withoutProducerOptions(current, producerComponentName); + if (answer.size() != current.size()) { + uriQueryParameters = answer; + updateQueryParameters(); + } + } + + private Map<String, Object> withoutProducerOptions(Map<String, Object> uriParameters, String producerComponentName) { + Map<String, Object> answer = new LinkedHashMap<>(uriParameters); + EndpointUriFactory factory = getEndpointUriFactory(producerComponentName); + if (factory != null) { + answer.keySet().removeIf(factory.propertyNames()::contains); + } + return answer; + } + + private void updateQueryParameters() throws URISyntaxException { + // the queryParameters option of the endpoint takes precedence + Map<String, Object> query = new LinkedHashMap<>(uriQueryParameters); + query.putAll(URISupport.parseQuery(configuredQueryParameters)); + String answer = URISupport.createQueryString(query); + queryParameters = ObjectHelper.isNotEmpty(answer) ? answer : configuredQueryParameters; + } + + private EndpointUriFactory getEndpointUriFactory(String name) { + if (name != null) { + UriFactoryResolver resolver + = getCamelContext().getCamelContextExtension().getContextPlugin(UriFactoryResolver.class); + if (resolver != null) { + return resolver.resolveFactory(name, getCamelContext()); + } + } + return null; + } + private void validateHost() { if (ObjectHelper.isEmpty(host)) { throw new IllegalArgumentException( diff --git a/components/camel-rest/src/test/java/org/apache/camel/component/rest/RestProducerComponentOptionsTest.java b/components/camel-rest/src/test/java/org/apache/camel/component/rest/RestProducerComponentOptionsTest.java new file mode 100644 index 000000000000..b4f6af4e1654 --- /dev/null +++ b/components/camel-rest/src/test/java/org/apache/camel/component/rest/RestProducerComponentOptionsTest.java @@ -0,0 +1,171 @@ +/* + * 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.rest; + +import java.util.Map; +import java.util.Set; + +import org.apache.camel.CamelContext; +import org.apache.camel.Endpoint; +import org.apache.camel.Producer; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.spi.RestConfiguration; +import org.apache.camel.spi.RestProducerFactory; +import org.apache.camel.support.DefaultComponent; +import org.apache.camel.support.component.EndpointUriFactorySupport; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +/** + * The options of the producer component given on the rest endpoint uri are applied to the producer, and are not sent as + * query parameters, however the producer component is chosen. + */ +class RestProducerComponentOptionsTest { + + private CamelContext context; + private DummyRestProducerComponent dummy; + + @BeforeEach + void setUp() throws Exception { + context = new DefaultCamelContext(); + // the generated uri factory of the dummy component, which knows the options of the dummy component + context.getRegistry().bind("dummyUriFactory", new DummyEndpointUriFactory()); + dummy = new DummyRestProducerComponent(); + context.addComponent("dummy", dummy); + context.start(); + } + + @AfterEach + void tearDown() { + context.stop(); + } + + @Test + void testProducerComponentFromEndpoint() throws Exception { + RestEndpoint endpoint = context.getEndpoint( + "rest:get:users?host=localhost&producerComponentName=dummy&authPassword=secret&foo=bar", + RestEndpoint.class); + assertThat(endpoint.getQueryParameters()).isEqualTo("foo=bar"); + + endpoint.createProducer(); + assertThat(dummy.queryParameters).isEqualTo("foo=bar"); + assertThat(dummy.parameters).containsEntry("authPassword", "secret"); + } + + @Test + void testProducerComponentFromRestConfiguration() throws Exception { + context.getRestConfiguration().setProducerComponent("dummy"); + + RestEndpoint endpoint = context.getEndpoint( + "rest:get:users?host=localhost&authPassword=secret&foo=bar", RestEndpoint.class); + assertThat(endpoint.getQueryParameters()).isEqualTo("foo=bar"); + + endpoint.createProducer(); + assertThat(dummy.queryParameters).isEqualTo("foo=bar"); + assertThat(dummy.parameters).containsEntry("authPassword", "secret"); + } + + @Test + void testProducerComponentAutoDetected() throws Exception { + RestEndpoint endpoint = context.getEndpoint( + "rest:get:users?host=localhost&authPassword=secret&foo=bar", RestEndpoint.class); + + endpoint.createProducer(); + assertThat(dummy.queryParameters).isEqualTo("foo=bar"); + assertThat(dummy.parameters).containsEntry("authPassword", "secret"); + assertThat(endpoint.getQueryParameters()).isEqualTo("foo=bar"); + } + + @Test + void testOnlyProducerOptions() throws Exception { + RestEndpoint endpoint = context.getEndpoint( + "rest:get:users?host=localhost&authPassword=secret", RestEndpoint.class); + + endpoint.createProducer(); + assertThat(dummy.queryParameters).isNull(); + assertThat(endpoint.getQueryParameters()).isNull(); + } + + @Test + void testExplicitQueryParametersAreKept() throws Exception { + RestEndpoint endpoint = context.getEndpoint( + "rest:get:users?host=localhost&queryParameters=RAW(page=1&authPassword=given)&authPassword=secret&foo=bar", + RestEndpoint.class); + + endpoint.createProducer(); + assertThat(dummy.queryParameters).isEqualTo("foo=bar&page=1&authPassword=given"); + assertThat(dummy.parameters).containsEntry("authPassword", "secret"); + } + + private static final class DummyRestProducerComponent extends DefaultComponent implements RestProducerFactory { + + private String queryParameters; + private Map<String, Object> parameters; + + @Override + protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) { + throw new UnsupportedOperationException(); + } + + @Override + public Producer createProducer( + CamelContext camelContext, String host, String verb, String basePath, String uriTemplate, + String queryParameters, String consumes, String produces, RestConfiguration configuration, + Map<String, Object> parameters) { + this.queryParameters = queryParameters; + this.parameters = parameters; + return mock(Producer.class); + } + } + + private static final class DummyEndpointUriFactory extends EndpointUriFactorySupport { + + @Override + public boolean isEnabled(String scheme) { + return "dummy".equals(scheme); + } + + @Override + public String buildUri(String scheme, Map<String, Object> properties, boolean encode) { + return scheme; + } + + @Override + public Set<String> propertyNames() { + return Set.of("authPassword", "authUsername"); + } + + @Override + public Set<String> secretPropertyNames() { + return Set.of("authPassword", "authUsername"); + } + + @Override + public Map<String, String> multiValuePrefixes() { + return Map.of(); + } + + @Override + public boolean isLenientProperties() { + return false; + } + } +} 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 25708c2cd51b..7f4f31b46801 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 @@ -12297,6 +12297,18 @@ With `allowUrlBody=true`, a body that does not start with `version: 1` and is no with a space or a relative name, now fails with an `InvalidPayloadException`, as a URL with an unknown scheme already did. Before, it failed with an `IllegalArgumentException`. A URL that cannot be fetched keeps its own exception. +=== camel-rest - the options of the producer component are not sent as query parameters + +A `rest` producer sends the parameters of its endpoint URI that are not `rest` options as query parameters of the +request, except the options of the component that sends the request (such as `authUsername` and `authPassword` of +`http`). These options are now left out of the query parameters also when the producer component is set with the +`producerComponent` of the rest configuration, or is detected automatically. Before, they were left out only when the +`producerComponentName` option was set on the endpoint or on the `rest` component: otherwise they were applied to the +producer and also sent in the query string of the request URL. + +A query parameter that has the same name as an option of the producer component must be set in the `queryParameters` +option. + === Kamelets - removed Kamelets The Kamelet catalog (camel-kamelets 4.24) removes Kamelets that could not work on Camel 4.23 or 4.24: diff --git a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/RestEndpointBuilderFactory.java b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/RestEndpointBuilderFactory.java index d610a9f8745d..5f3ea4429aa1 100644 --- a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/RestEndpointBuilderFactory.java +++ b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/RestEndpointBuilderFactory.java @@ -493,8 +493,8 @@ public interface RestEndpointBuilderFactory { * {name} is filled with the header or the exchange variable of that * name, and a parameter with the value {name?} is left out when neither * is set. The endpoint also adds the parameters of its URI that it does - * not know, except the options of the component that - * producerComponentName names. + * not know, except the options of the component that sends the HTTP + * request. * * The option is a: <code>java.lang.String</code> type. *
