This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27160 in repository https://gitbox.apache.org/repos/asf/camel.git
commit a4e082dbba0f25e2a87ec31b065d4974a15b1325 Author: Claus Ibsen <[email protected]> AuthorDate: Thu Nov 19 11:03:58 2026 +0100 CAMEL-27160: A RAW() option value of a dynamic endpoint computed from the message is taken as literal text A RAW(...) option value of an endpoint whose uri is computed from the current message at runtime (toD, enrich, wireTap, pollEnrich, recipientList, routingSlip, dynamicRouter) is unwrapped but taken as literal text: a $simple{...} in it is not evaluated. RawParameterHelper reads the literal option binding scope, and the optimised SendDynamicAware and PollDynamicAware paths (such as the toD of the http components) do the same. Endpoints written in the route still evaluate it; in a toD uri, use RAW({{env:X}}), which is resolved when the route is created. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../http/HttpSendDynamicAwareRawSimpleTest.java | 100 +++++++++ .../DynamicEndpointMessageRawOptionTest.java | 224 +++++++++++++++++++++ .../support/component/RawParameterHelperTest.java | 9 + .../support/component/PollDynamicAwareSupport.java | 10 +- .../support/component/RawParameterHelper.java | 28 ++- .../support/component/SendDynamicAwareSupport.java | 10 +- .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 10 + docs/user-manual/modules/ROOT/pages/endpoint.adoc | 5 + 8 files changed, 387 insertions(+), 9 deletions(-) diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpSendDynamicAwareRawSimpleTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpSendDynamicAwareRawSimpleTest.java new file mode 100644 index 000000000000..50a957548b48 --- /dev/null +++ b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpSendDynamicAwareRawSimpleTest.java @@ -0,0 +1,100 @@ +/* + * 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.util.concurrent.atomic.AtomicReference; + +import org.apache.camel.Exchange; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.util.StringHelper; +import org.apache.hc.core5.http.impl.bootstrap.HttpServer; +import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * A RAW() query value that toD computes from the message is taken as literal text on the optimised http path: a + * $simple{} in it is not evaluated. + */ +public class HttpSendDynamicAwareRawSimpleTest extends BaseHttpTest { + + private static final String PROP = "camel.test.httpRawOption." + System.nanoTime(); + private static final String PROP_VALUE = "prop-value-" + System.nanoTime(); + + private final AtomicReference<String> receivedQuery = new AtomicReference<>(); + private HttpServer localServer; + + @BeforeAll + static void setSystemProperty() { + System.setProperty(PROP, PROP_VALUE); + } + + @AfterAll + static void clearSystemProperty() { + System.clearProperty(PROP); + } + + @Override + public void setupResources() throws Exception { + localServer = ServerBootstrap.bootstrap() + .setCanonicalHostName("localhost") + .setHttpProcessor(getBasicHttpProcessor()) + .setConnectionReuseStrategy(getConnectionReuseStrategy()) + .setResponseFactory(getHttpResponseFactory()) + .setSslContext(getSSLContext()) + .register("/dynamicAware", (request, response, context) -> { + receivedQuery.set(StringHelper.after(request.getRequestUri(), "?")); + response.setCode(200); + }) + .create(); + localServer.start(); + } + + @Override + public void cleanupResources() throws Exception { + if (localServer != null) { + localServer.stop(); + } + } + + @Override + protected RoutesBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:dynamicAware") + .toD("http://localhost:" + localServer.getLocalPort() + "/dynamicAware?q=${header.q}"); + } + }; + } + + @Test + public void testRawSimpleFromMessageIsLiteral() throws Exception { + Exchange e = fluentTemplate + .to("direct:dynamicAware") + .withHeader("q", "RAW($simple{sys." + PROP + "})") + .send(); + + String expected = "q=%24simple%7Bsys." + PROP + "%7D"; + assertEquals(expected, e.getIn().getHeader(Exchange.HTTP_QUERY)); + assertEquals(expected, receivedQuery.get()); + } +} diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/DynamicEndpointMessageRawOptionTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/DynamicEndpointMessageRawOptionTest.java new file mode 100644 index 000000000000..5f08f68fd75a --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/processor/DynamicEndpointMessageRawOptionTest.java @@ -0,0 +1,224 @@ +/* + * 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.processor; + +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.camel.CamelContext; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.log.LogEndpoint; +import org.apache.camel.support.DefaultExchange; +import org.apache.camel.support.component.PollDynamicAwareSupport; +import org.apache.camel.support.component.SendDynamicAwareSupport; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Companion to {@link DynamicEndpointMessageOptionLiteralTest}: a {@code RAW(...)} option value of an endpoint created + * from a uri computed from the current message is unwrapped, but taken as literal text: a {@code $simple{...}} in it is + * not evaluated. An endpoint written in the route (such as {@code to("log:x?marker=RAW($simple{...})")}) still + * evaluates it. + */ +class DynamicEndpointMessageRawOptionTest extends ContextTestSupport { + + private static final String PROP = "camel.test.rawOption." + System.nanoTime(); + private static final String PROP_VALUE = "prop-value-" + System.nanoTime(); + private static final AtomicInteger HITS = new AtomicInteger(); + + @BeforeAll + static void setSystemProperty() { + System.setProperty(PROP, PROP_VALUE); + } + + @AfterAll + static void clearSystemProperty() { + System.clearProperty(PROP); + } + + @BeforeEach + void resetHits() { + HITS.set(0); + } + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext context = super.createCamelContext(); + context.getRegistry().bind("probe", new Probe()); + return context; + } + + @Test + void toDRawSystemPropertyFunctionIsLiteral() { + assertToDMarkerIsLiteral("sys", "$simple{sys." + PROP + "}"); + } + + @Test + void toDRawEnvFunctionIsLiteral() { + assertToDMarkerIsLiteral("env", "$simple{env.HOME}"); + } + + @Test + void toDRawBeanFunctionIsLiteral() { + assertToDMarkerIsLiteral("bean", "$simple{bean:probe?method=hit}"); + assertThat(HITS.get()).isZero(); + } + + @Test + void toDRawCamelContextFunctionIsLiteral() { + assertToDMarkerIsLiteral("ctx", "$simple{camelContext.getName()}"); + } + + @Test + void toDRawValueIsStillUnwrapped() { + assertToDMarkerIsLiteral("plain", "a+b&c=d"); + } + + @Test + void toDRawPropertyPlaceholderInRouteIsResolved() { + template.sendBodyAndHeader("direct:todph", "Hello", "id", "ph"); + + assertThat(markerOf("log://todph-ph")).isEqualTo(PROP_VALUE); + } + + @Test + void staticEndpointRawSimpleIsEvaluated() { + template.sendBody("direct:static", "Hello"); + + assertThat(markerOf("log://static")).isEqualTo(PROP_VALUE); + } + + @Test + void sendDynamicAwareRawSimpleIsLiteral() throws Exception { + LenientSendDynamicAware aware = new LenientSendDynamicAware(); + aware.setCamelContext(context); + aware.setScheme("log"); + aware.init(); + Exchange exchange = new DefaultExchange(context); + String uri = "log:aware?marker=RAW($simple{sys." + PROP + "})&custom=RAW($simple{bean:probe?method=hit})"; + + Map<String, Object> known = aware.endpointProperties(exchange, uri); + Map<String, Object> lenient = aware.endpointLenientProperties(exchange, uri); + + assertThat(known).containsEntry("marker", "$simple{sys." + PROP + "}"); + assertThat(lenient).containsEntry("custom", "$simple{bean:probe?method=hit}"); + assertThat(HITS.get()).isZero(); + } + + @Test + void pollDynamicAwareRawSimpleIsLiteral() throws Exception { + LenientPollDynamicAware aware = new LenientPollDynamicAware(); + aware.setCamelContext(context); + aware.setScheme("log"); + aware.init(); + Exchange exchange = new DefaultExchange(context); + String uri = "log:aware?marker=RAW($simple{sys." + PROP + "})&custom=RAW($simple{bean:probe?method=hit})"; + + Map<String, Object> known = aware.endpointProperties(exchange, uri); + Map<String, Object> lenient = aware.endpointLenientProperties(exchange, uri); + + assertThat(known).containsEntry("marker", "$simple{sys." + PROP + "}"); + assertThat(lenient).containsEntry("custom", "$simple{bean:probe?method=hit}"); + assertThat(HITS.get()).isZero(); + } + + private void assertToDMarkerIsLiteral(String id, String value) { + template.sendBodyAndHeaders("direct:tod", "Hello", Map.of("id", id, "m", "RAW(" + value + ")")); + + assertThat(markerOf("log://tod-" + id)).isEqualTo(value); + } + + private String markerOf(String uriPrefix) { + for (Endpoint e : context.getEndpoints()) { + if (e instanceof LogEndpoint le && e.getEndpointUri().startsWith(uriPrefix)) { + return le.getMarker(); + } + } + throw new AssertionError("No log endpoint for " + uriPrefix); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:tod").toD("log:tod-${header.id}?marker=${header.m}"); + from("direct:todph").toD("log:todph-${header.id}?marker=RAW({{sys:" + PROP + "}})"); + from("direct:static").to("log:static?marker=RAW($simple{sys." + PROP + "})"); + } + }; + } + + public static class Probe { + public String hit() { + HITS.incrementAndGet(); + return "probe-called"; + } + } + + private static final class LenientSendDynamicAware extends SendDynamicAwareSupport { + @Override + public boolean isLenientProperties() { + return true; + } + + @Override + public DynamicAwareEntry prepare(Exchange exchange, String uri, String originalUri) { + return null; + } + + @Override + public String resolveStaticUri(Exchange exchange, DynamicAwareEntry entry) { + return null; + } + + @Override + public Processor createPreProcessor(Exchange exchange, DynamicAwareEntry entry) { + return null; + } + + @Override + public Processor createPostProcessor(Exchange exchange, DynamicAwareEntry entry) { + return null; + } + } + + private static final class LenientPollDynamicAware extends PollDynamicAwareSupport { + @Override + public boolean isLenientProperties() { + return true; + } + + @Override + public DynamicAwareEntry prepare(Exchange exchange, String uri, String originalUri) { + return null; + } + + @Override + public String resolveStaticUri(Exchange exchange, DynamicAwareEntry entry) { + return null; + } + } +} diff --git a/core/camel-core/src/test/java/org/apache/camel/support/component/RawParameterHelperTest.java b/core/camel-core/src/test/java/org/apache/camel/support/component/RawParameterHelperTest.java index 6d8cc4c968f5..8e87ee1a9cfd 100644 --- a/core/camel-core/src/test/java/org/apache/camel/support/component/RawParameterHelperTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/support/component/RawParameterHelperTest.java @@ -45,6 +45,15 @@ public class RawParameterHelperTest extends ContextTestSupport { Assertions.assertEquals("abc", map.get("username")); Assertions.assertEquals(context.getName(), map.get("password")); + // literal: the RAW token is removed, but the simple expression is not evaluated + map.put("password", "RAW($simple{camelId})"); + RawParameterHelper.resolveRawParameterValues(context, map, true); + Assertions.assertEquals("$simple{camelId}", map.get("password")); + + map.put("password", "RAW(sec*ret)"); + RawParameterHelper.resolveRawParameterValues(context, map, true); + Assertions.assertEquals("sec*ret", map.get("password")); + String usr = System.getenv("USER"); if (usr != null) { map.put("password", "RAW($simple{env:USER})"); diff --git a/core/camel-support/src/main/java/org/apache/camel/support/component/PollDynamicAwareSupport.java b/core/camel-support/src/main/java/org/apache/camel/support/component/PollDynamicAwareSupport.java index 1e0664b136b9..d3813fa9e759 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/component/PollDynamicAwareSupport.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/component/PollDynamicAwareSupport.java @@ -84,8 +84,9 @@ public abstract class PollDynamicAwareSupport extends ServiceSupport implements if (resolveRawParameterValues()) { // parameters using raw syntax: RAW(value) // should have the token removed, so its only the value we have in parameters, as we are about to create - // an endpoint and want to have the parameter values without the RAW tokens - RawParameterHelper.resolveRawParameterValues(exchange.getContext(), map); + // an endpoint and want to have the parameter values without the RAW tokens. The uri was computed from + // the current message, so the RAW values are taken as literal text (a $simple{} in them is not evaluated) + RawParameterHelper.resolveRawParameterValues(exchange.getContext(), map, true); } // okay so only add the known properties as they are the non lenient properties properties = new LinkedHashMap<>(); @@ -115,8 +116,9 @@ public abstract class PollDynamicAwareSupport extends ServiceSupport implements if (resolveRawParameterValues()) { // parameters using raw syntax: RAW(value) // should have the token removed, so its only the value we have in parameters, as we are about to create - // an endpoint and want to have the parameter values without the RAW tokens - RawParameterHelper.resolveRawParameterValues(exchange.getContext(), map); + // an endpoint and want to have the parameter values without the RAW tokens. The uri was computed from + // the current message, so the RAW values are taken as literal text (a $simple{} in them is not evaluated) + RawParameterHelper.resolveRawParameterValues(exchange.getContext(), map, true); } properties = new LinkedHashMap<>(); map.forEach((k, v) -> { diff --git a/core/camel-support/src/main/java/org/apache/camel/support/component/RawParameterHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/component/RawParameterHelper.java index 31254462ecd3..419244d45486 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/component/RawParameterHelper.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/component/RawParameterHelper.java @@ -21,6 +21,7 @@ import java.util.Map; import org.apache.camel.CamelContext; import org.apache.camel.Exchange; import org.apache.camel.support.ExchangeHelper; +import org.apache.camel.support.PropertyBindingSupport; import org.apache.camel.util.URISupport; public final class RawParameterHelper { @@ -36,12 +37,37 @@ public final class RawParameterHelper { * The value can refer to a simple expression using $simple{xxx} syntax which allows to refer to passwords defined * as ENV variables etc. * + * When the endpoint currently being created on this thread binds its option values as literal text (see + * {@link PropertyBindingSupport#isLiteralOptionBinding()}), the RAW tokens are removed but a $simple{xxx} in the + * value is not evaluated, see {@link #resolveRawParameterValues(CamelContext, Map, boolean)}. + * * @param camelContext the camel context * @param parameters the uri parameters */ public static void resolveRawParameterValues(CamelContext camelContext, Map<String, Object> parameters) { + resolveRawParameterValues(camelContext, parameters, PropertyBindingSupport.isLiteralOptionBinding()); + } + + /** + * Traverses the given parameters, and resolves any parameter values which use the RAW token syntax: + * <code>key=RAW(value)</code>. This method will then remove the RAW tokens, and replace the content of the value, + * with just the value. + * + * When {@code literal} is {@code false} the value can refer to a simple expression using $simple{xxx} syntax which + * allows to refer to passwords defined as ENV variables etc. When {@code literal} is {@code true} the value is + * taken as literal text and a $simple{xxx} in it is not evaluated. This is used for an endpoint whose uri was + * computed from the current message at runtime (for example the per-message recipient of {@code toD} or + * {@code pollEnrich}), as its option values come from the message and not from the route. + * + * @param camelContext the camel context + * @param parameters the uri parameters + * @param literal whether the RAW values are taken as literal text, without evaluating $simple{xxx} + * @since 4.24 + */ + public static void resolveRawParameterValues( + CamelContext camelContext, Map<String, Object> parameters, boolean literal) { URISupport.resolveRawParameterValues(parameters, s -> { - if (s != null && s.contains("$simple{")) { + if (!literal && s != null && s.contains("$simple{")) { Exchange dummy = ExchangeHelper.getDummy(camelContext); s = camelContext.resolveLanguage("simple").createExpression(s).evaluate(dummy, String.class); } diff --git a/core/camel-support/src/main/java/org/apache/camel/support/component/SendDynamicAwareSupport.java b/core/camel-support/src/main/java/org/apache/camel/support/component/SendDynamicAwareSupport.java index 117613441960..0d5ae4cab1d5 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/component/SendDynamicAwareSupport.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/component/SendDynamicAwareSupport.java @@ -84,8 +84,9 @@ public abstract class SendDynamicAwareSupport extends ServiceSupport implements if (resolveRawParameterValues()) { // parameters using raw syntax: RAW(value) // should have the token removed, so its only the value we have in parameters, as we are about to create - // an endpoint and want to have the parameter values without the RAW tokens - RawParameterHelper.resolveRawParameterValues(exchange.getContext(), map); + // an endpoint and want to have the parameter values without the RAW tokens. The uri was computed from + // the current message, so the RAW values are taken as literal text (a $simple{} in them is not evaluated) + RawParameterHelper.resolveRawParameterValues(exchange.getContext(), map, true); } // okay so only add the known properties as they are the non lenient properties properties = new LinkedHashMap<>(); @@ -115,8 +116,9 @@ public abstract class SendDynamicAwareSupport extends ServiceSupport implements if (resolveRawParameterValues()) { // parameters using raw syntax: RAW(value) // should have the token removed, so its only the value we have in parameters, as we are about to create - // an endpoint and want to have the parameter values without the RAW tokens - RawParameterHelper.resolveRawParameterValues(exchange.getContext(), map); + // an endpoint and want to have the parameter values without the RAW tokens. The uri was computed from + // the current message, so the RAW values are taken as literal text (a $simple{} in them is not evaluated) + RawParameterHelper.resolveRawParameterValues(exchange.getContext(), map, true); } properties = new LinkedHashMap<>(); map.forEach((k, v) -> { 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 29b551c2c8dd..76d7f1e2429c 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 @@ -166,6 +166,16 @@ What still works as before: * An endpoint in a static `to(...)`, or configured on the component, binds `#class:`, `#property:` and placeholders as before. +=== camel-core - RAW() option values of a dynamic endpoint computed from the message are literal text + +A `RAW(...)` option value of an endpoint whose uri is computed from the current message at runtime (`toD`, `enrich`, +`wireTap`, `pollEnrich`, `recipientList`, `routingSlip` and `dynamicRouter`) is still unwrapped, but its content is now +taken as literal text: a `+++$simple{...}+++` in it is no longer evaluated. Endpoints written in the route, such as +`+to("ftp:[email protected]?password=RAW($simple{env:MY_FTP_PASSWORD})")+`, are unchanged. As the whole uri of `toD` +is computed per message, this also applies to a `+++RAW($simple{env:MY_PASSWORD})+++` written in the `toD` uri itself; +use a property placeholder instead, such as `+toD("ftp:joe@${header.host}?password=RAW({{env:MY_PASSWORD}})")+`, which +is resolved once when the route is created. + === camel-core - Rest DSL binding mode json fails at startup without a JSON data format A Rest DSL service with `bindingMode` `json` or `json_xml` whose JSON data format (`jackson` unless `jsonDataFormat` diff --git a/docs/user-manual/modules/ROOT/pages/endpoint.adoc b/docs/user-manual/modules/ROOT/pages/endpoint.adoc index a6c82d05910a..2ad0a2c8c3d7 100644 --- a/docs/user-manual/modules/ROOT/pages/endpoint.adoc +++ b/docs/user-manual/modules/ROOT/pages/endpoint.adoc @@ -322,6 +322,11 @@ YAML:: ==== +NOTE: The `+++$simple{xxx}+++` in a `RAW(...)` value is evaluated only for endpoints written in the route. For an +endpoint whose uri is computed from the message at runtime, such as with `toD` or `pollEnrich`, a `RAW(...)` value is +taken as literal text. Use a property placeholder such as `+RAW({{env:MY_FTP_PASSWORD}})+` there, which is resolved +when the route is created. + === Endpoint URIs with property placeholders Camel has extensive support for using xref:using-propertyplaceholder.adoc[].
