This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-4.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.22.x by this push:
new dfb4d2e94384 [backport camel-4.22.x] CAMEL-28111: camel-core - a
dynamic uri keeps the uri text of the options that the endpoint evaluates for
each message (#31373)
dfb4d2e94384 is described below
commit dfb4d2e943843e33cb491b1163aaa44e53b83625
Author: Claus Ibsen <[email protected]>
AuthorDate: Sat Dec 5 21:36:30 2026 +0100
[backport camel-4.22.x] CAMEL-28111: camel-core - a dynamic uri keeps the
uri text of the options that the endpoint evaluates for each message (#31373)
With toD, enrich, pollEnrich and a dynamic wireTap, the options that a
component evaluates as an expression for each message (such as the meter
name and tags of camel-micrometer, the keyName of camel-aws2-s3 or the
move option of the file components) now keep the text of the uri as
written in the route, so the component evaluates it once against the
message, as with a static to. Such an option that is only in the computed
uri is rejected.
Backport to camel-4.22.x: this branch has no supportSimpleExpression
metadata, so the options are declared by the SendDynamicAware and
PollDynamicAware of each component (the ExpressionOptionsDynamicAware
interface in camel-support), and EndpointUriFactory is unchanged. New
dynamic awares for micrometer, opentelemetry-metrics, aws2-s3, jpa,
sql-stored, google-storage and mina-sftp; the file component awares
declare their options.
(cherry picked from commit cc5c41ebcf870df80bba7597dac1601a923215e9)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Claus Ibsen <[email protected]>
---
.../services/org/apache/camel/poll-dynamic/aws2-s3 | 2 +
.../services/org/apache/camel/send-dynamic/aws2-s3 | 2 +
.../camel/component/aws2/s3/AWS2S3Endpoint.java | 5 +
.../component/aws2/s3/AWS2S3PollDynamicAware.java | 37 +++
.../component/aws2/s3/AWS2S3SendDynamicAware.java | 32 ++
.../aws2/s3/AWS2S3ToDynamicKeyNameTest.java | 94 ++++++
.../file/GenericFilePollDynamicAware.java | 38 ++-
.../file/GenericFileSendDynamicAware.java | 9 +-
.../org/apache/camel/poll-dynamic/google-storage | 2 +
.../GoogleCloudStoragePollDynamicAware.java | 34 +++
.../services/org/apache/camel/send-dynamic/jpa | 2 +
.../camel/component/jpa/JpaSendDynamicAware.java | 34 +++
.../org/apache/camel/send-dynamic/micrometer | 2 +
.../micrometer/MicrometerSendDynamicAware.java | 35 +++
.../micrometer/MicrometerToDynamicTagTest.java | 110 +++++++
.../org/apache/camel/poll-dynamic/mina-sftp | 2 +
.../org/apache/camel/send-dynamic/mina-sftp | 2 +
.../file/remote/mina/MinaSftpPollDynamicAware.java | 35 +++
.../file/remote/mina/MinaSftpSendDynamicAware.java | 36 +++
.../camel/send-dynamic/opentelemetry-metrics | 2 +
.../metrics/OpenTelemetrySendDynamicAware.java | 35 +++
.../org/apache/camel/send-dynamic/sql-stored | 2 +
.../sql/stored/SqlStoredSendDynamicAware.java | 34 +++
.../apache/camel/processor/DynamicUriTemplate.java | 328 +++++++++++++++++++++
.../org/apache/camel/processor/PollEnricher.java | 27 ++
.../camel/processor/SendDynamicProcessor.java | 24 ++
.../file/PollEnrichFileExpressionOptionTest.java | 86 ++++++
.../processor/ToDynamicExpressionOptionTest.java | 281 ++++++++++++++++++
.../PollDynamicFileNameOptimizeDisabledTest.java | 8 +-
.../services/org/apache/camel/send-dynamic/greet | 18 ++
.../component/ExpressionOptionsDynamicAware.java | 49 +++
.../ExpressionOptionsPollDynamicAware.java | 57 ++++
.../ExpressionOptionsSendDynamicAware.java | 75 +++++
33 files changed, 1528 insertions(+), 11 deletions(-)
diff --git
a/components/camel-aws/camel-aws2-s3/src/generated/resources/META-INF/services/org/apache/camel/poll-dynamic/aws2-s3
b/components/camel-aws/camel-aws2-s3/src/generated/resources/META-INF/services/org/apache/camel/poll-dynamic/aws2-s3
new file mode 100644
index 000000000000..2995f6bced79
--- /dev/null
+++
b/components/camel-aws/camel-aws2-s3/src/generated/resources/META-INF/services/org/apache/camel/poll-dynamic/aws2-s3
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.component.aws2.s3.AWS2S3PollDynamicAware
diff --git
a/components/camel-aws/camel-aws2-s3/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/aws2-s3
b/components/camel-aws/camel-aws2-s3/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/aws2-s3
new file mode 100644
index 000000000000..ce1775f935dc
--- /dev/null
+++
b/components/camel-aws/camel-aws2-s3/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/aws2-s3
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.component.aws2.s3.AWS2S3SendDynamicAware
diff --git
a/components/camel-aws/camel-aws2-s3/src/main/java/org/apache/camel/component/aws2/s3/AWS2S3Endpoint.java
b/components/camel-aws/camel-aws2-s3/src/main/java/org/apache/camel/component/aws2/s3/AWS2S3Endpoint.java
index 7350a7bebd8f..209e36b83092 100644
---
a/components/camel-aws/camel-aws2-s3/src/main/java/org/apache/camel/component/aws2/s3/AWS2S3Endpoint.java
+++
b/components/camel-aws/camel-aws2-s3/src/main/java/org/apache/camel/component/aws2/s3/AWS2S3Endpoint.java
@@ -31,6 +31,7 @@ import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.UriEndpoint;
import org.apache.camel.spi.UriParam;
import org.apache.camel.spi.UriPath;
+import org.apache.camel.support.LanguageSupport;
import org.apache.camel.support.ScheduledPollEndpoint;
import
org.apache.camel.support.processor.idempotent.MemoryIdempotentRepository;
import org.apache.camel.support.service.ServiceHelper;
@@ -146,6 +147,10 @@ public class AWS2S3Endpoint extends ScheduledPollEndpoint
implements EndpointSer
}
String bucketName = getConfiguration().getBucketName();
+ if (LanguageSupport.hasSimpleFunction(bucketName)) {
+ LOG.trace("Bucket name [{}] is evaluated for each message, so
skipping bucket check...", bucketName);
+ return;
+ }
LOG.trace("Querying whether bucket [{}] already exists...",
bucketName);
try {
diff --git
a/components/camel-aws/camel-aws2-s3/src/main/java/org/apache/camel/component/aws2/s3/AWS2S3PollDynamicAware.java
b/components/camel-aws/camel-aws2-s3/src/main/java/org/apache/camel/component/aws2/s3/AWS2S3PollDynamicAware.java
new file mode 100644
index 000000000000..a89f860ae618
--- /dev/null
+++
b/components/camel-aws/camel-aws2-s3/src/main/java/org/apache/camel/component/aws2/s3/AWS2S3PollDynamicAware.java
@@ -0,0 +1,37 @@
+/*
+ * 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.aws2.s3;
+
+import java.util.Set;
+
+import org.apache.camel.spi.annotations.PollDynamic;
+import org.apache.camel.support.component.ExpressionOptionsPollDynamicAware;
+
+/**
+ * The options of the aws2-s3 endpoint that are evaluated as Simple
expressions for each message, which a dynamic uri
+ * (pollEnrich) keeps as written in the uri.
+ */
+@PollDynamic("aws2-s3")
+public class AWS2S3PollDynamicAware extends ExpressionOptionsPollDynamicAware {
+
+ static final Set<String> EXPRESSION_PROPERTY_NAMES
+ = Set.of("bucketNameOrArn", "destinationBucketPrefix",
"destinationBucketSuffix", "keyName");
+
+ public AWS2S3PollDynamicAware() {
+ super(EXPRESSION_PROPERTY_NAMES);
+ }
+}
diff --git
a/components/camel-aws/camel-aws2-s3/src/main/java/org/apache/camel/component/aws2/s3/AWS2S3SendDynamicAware.java
b/components/camel-aws/camel-aws2-s3/src/main/java/org/apache/camel/component/aws2/s3/AWS2S3SendDynamicAware.java
new file mode 100644
index 000000000000..13b47fb0e730
--- /dev/null
+++
b/components/camel-aws/camel-aws2-s3/src/main/java/org/apache/camel/component/aws2/s3/AWS2S3SendDynamicAware.java
@@ -0,0 +1,32 @@
+/*
+ * 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.aws2.s3;
+
+import org.apache.camel.spi.annotations.SendDynamic;
+import org.apache.camel.support.component.ExpressionOptionsSendDynamicAware;
+
+/**
+ * The options of the aws2-s3 endpoint that are evaluated as Simple
expressions for each message, which a dynamic uri
+ * (toD) keeps as written in the uri.
+ */
+@SendDynamic("aws2-s3")
+public class AWS2S3SendDynamicAware extends ExpressionOptionsSendDynamicAware {
+
+ public AWS2S3SendDynamicAware() {
+ super(AWS2S3PollDynamicAware.EXPRESSION_PROPERTY_NAMES,
"://bucketNameOrArn");
+ }
+}
diff --git
a/components/camel-aws/camel-aws2-s3/src/test/java/org/apache/camel/component/aws2/s3/AWS2S3ToDynamicKeyNameTest.java
b/components/camel-aws/camel-aws2-s3/src/test/java/org/apache/camel/component/aws2/s3/AWS2S3ToDynamicKeyNameTest.java
new file mode 100644
index 000000000000..8e9d48b1ff8f
--- /dev/null
+++
b/components/camel-aws/camel-aws2-s3/src/test/java/org/apache/camel/component/aws2/s3/AWS2S3ToDynamicKeyNameTest.java
@@ -0,0 +1,94 @@
+/*
+ * 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.aws2.s3;
+
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+import software.amazon.awssdk.services.s3.S3Client;
+import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
+import software.amazon.awssdk.services.s3.model.DeleteObjectResponse;
+import software.amazon.awssdk.services.s3.model.HeadBucketRequest;
+import software.amazon.awssdk.services.s3.model.HeadBucketResponse;
+import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
+import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * The key and the bucket of a dynamic uri (toD) are evaluated once, against
the message, as with a static uri.
+ */
+public class AWS2S3ToDynamicKeyNameTest extends CamelTestSupport {
+
+ private final S3Client s3Client = mock(S3Client.class);
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext context = super.createCamelContext();
+
when(s3Client.deleteObject(any(DeleteObjectRequest.class))).thenReturn(DeleteObjectResponse.builder().build());
+
when(s3Client.listObjectsV2(any(ListObjectsV2Request.class))).thenReturn(ListObjectsV2Response.builder().build());
+
when(s3Client.headBucket(any(HeadBucketRequest.class))).thenReturn(HeadBucketResponse.builder().build());
+ context.getRegistry().bind("s3", s3Client);
+ return context;
+ }
+
+ @Test
+ public void testKeyNameFromHeader() {
+ template.sendBodyAndHeaders("direct:key", "Hello", Map.of("key",
"${header.other}.txt", "other", "report"));
+
+ ArgumentCaptor<DeleteObjectRequest> captor =
ArgumentCaptor.forClass(DeleteObjectRequest.class);
+ verify(s3Client).deleteObject(captor.capture());
+ assertEquals("mybucket", captor.getValue().bucket());
+ // the key is the text of the header
+ assertEquals("${header.other}.txt", captor.getValue().key());
+ }
+
+ @Test
+ public void testBucketFromHeader() {
+ template.sendBodyAndHeaders("direct:bucket", "Hello", Map.of("bucket",
"b-${header.other}", "other", "report"));
+
+ // the bucket is used by the producer with the text of the header
+ ArgumentCaptor<ListObjectsV2Request> captor =
ArgumentCaptor.forClass(ListObjectsV2Request.class);
+ verify(s3Client).listObjectsV2(captor.capture());
+ assertEquals("b-${header.other}", captor.getValue().bucket());
+ // and the endpoint does not check a bucket that is evaluated for each
message when it starts
+ verify(s3Client, never()).headBucket(any(HeadBucketRequest.class));
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:key")
+
.toD("aws2-s3://mybucket?amazonS3Client=#s3&operation=deleteObject&keyName=${header.key}");
+
+ from("direct:bucket")
+
.toD("aws2-s3://${header.bucket}?amazonS3Client=#s3&operation=listObjects");
+ }
+ };
+ }
+}
diff --git
a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFilePollDynamicAware.java
b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFilePollDynamicAware.java
index 0e002f60df31..d364363998af 100644
---
a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFilePollDynamicAware.java
+++
b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFilePollDynamicAware.java
@@ -16,16 +16,39 @@
*/
package org.apache.camel.component.file;
+import java.util.List;
import java.util.Map;
+import java.util.Set;
import org.apache.camel.Exchange;
+import org.apache.camel.support.component.ExpressionOptionsDynamicAware;
import org.apache.camel.support.component.PollDynamicAwareSupport;
import org.apache.camel.util.URISupport;
-public abstract class GenericFilePollDynamicAware extends
PollDynamicAwareSupport {
+public abstract class GenericFilePollDynamicAware extends
PollDynamicAwareSupport implements ExpressionOptionsDynamicAware {
public static final String PROP_FILE_NAME = "fileName";
+ /**
+ * The options that the consumer evaluates as an expression, which keep
the text of the original uri
+ */
+ private static final List<String> EXPRESSION_OPTIONS = List.of(
+ PROP_FILE_NAME, "doneFileName", "idempotentKey", "move",
"moveFailed", "preMove", "filterFile",
+ "filterDirectory", "sortBy");
+
+ /**
+ * The options that the file endpoints evaluate as an expression for each
message, which a dynamic uri (such as toD
+ * and pollEnrich) keeps as written in the uri
+ */
+ static final Set<String> EXPRESSION_PROPERTY_NAMES = Set.of(
+ "doneFileName", PROP_FILE_NAME, "filterDirectory", "filterFile",
"idempotentKey", "move", "moveExisting",
+ "moveFailed", "preMove", "sortBy", "tempFileName");
+
+ @Override
+ public Set<String> expressionPropertyNames() {
+ return EXPRESSION_PROPERTY_NAMES;
+ }
+
@Override
public boolean isLenientProperties() {
return false;
@@ -44,13 +67,16 @@ public abstract class GenericFilePollDynamicAware extends
PollDynamicAwareSuppor
// which should then be reversed
uri = uri.replace("\t", "\\\\t");
- boolean fileName = entry.getProperties().containsKey(PROP_FILE_NAME);
- // if any of the above are in use, then they should not be pre
evaluated
+ Map<String, Object> params = entry.getProperties();
+ // if any of the expression options are in use, then they should not
be pre evaluated
// and we need to rebuild a new uri with them as-is
- if (fileName) {
- Map<String, Object> params = entry.getProperties();
+ if (EXPRESSION_OPTIONS.stream().anyMatch(params::containsKey)) {
Map<String, Object> originalParams =
URISupport.parseQuery(URISupport.extractQuery(entry.getOriginalUri()));
- compute(originalParams, PROP_FILE_NAME, params);
+ for (String option : EXPRESSION_OPTIONS) {
+ if (params.containsKey(option)) {
+ compute(originalParams, option, params);
+ }
+ }
return asEndpointUri(exchange, uri, params);
} else {
return uri;
diff --git
a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileSendDynamicAware.java
b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileSendDynamicAware.java
index 7f086fb1bd4a..b02b8c31d60a 100644
---
a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileSendDynamicAware.java
+++
b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileSendDynamicAware.java
@@ -17,13 +17,15 @@
package org.apache.camel.component.file;
import java.util.Map;
+import java.util.Set;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
+import org.apache.camel.support.component.ExpressionOptionsDynamicAware;
import org.apache.camel.support.component.SendDynamicAwareSupport;
import org.apache.camel.util.URISupport;
-public abstract class GenericFileSendDynamicAware extends
SendDynamicAwareSupport {
+public abstract class GenericFileSendDynamicAware extends
SendDynamicAwareSupport implements ExpressionOptionsDynamicAware {
public static final String PROP_FILE_NAME = "fileName";
public static final String PROP_TEMP_FILE_NAME = "tempFileName";
@@ -33,6 +35,11 @@ public abstract class GenericFileSendDynamicAware extends
SendDynamicAwareSuppor
public static final String PROP_PRE_MOVE = "preMove";
public static final String PROP_MOVE_EXISTING = "moveExisting";
+ @Override
+ public Set<String> expressionPropertyNames() {
+ return GenericFilePollDynamicAware.EXPRESSION_PROPERTY_NAMES;
+ }
+
@Override
public boolean isLenientProperties() {
return false;
diff --git
a/components/camel-google/camel-google-storage/src/generated/resources/META-INF/services/org/apache/camel/poll-dynamic/google-storage
b/components/camel-google/camel-google-storage/src/generated/resources/META-INF/services/org/apache/camel/poll-dynamic/google-storage
new file mode 100644
index 000000000000..bd7a1dd22538
--- /dev/null
+++
b/components/camel-google/camel-google-storage/src/generated/resources/META-INF/services/org/apache/camel/poll-dynamic/google-storage
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.component.google.storage.GoogleCloudStoragePollDynamicAware
diff --git
a/components/camel-google/camel-google-storage/src/main/java/org/apache/camel/component/google/storage/GoogleCloudStoragePollDynamicAware.java
b/components/camel-google/camel-google-storage/src/main/java/org/apache/camel/component/google/storage/GoogleCloudStoragePollDynamicAware.java
new file mode 100644
index 000000000000..dd90212ee5d0
--- /dev/null
+++
b/components/camel-google/camel-google-storage/src/main/java/org/apache/camel/component/google/storage/GoogleCloudStoragePollDynamicAware.java
@@ -0,0 +1,34 @@
+/*
+ * 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.google.storage;
+
+import java.util.Set;
+
+import org.apache.camel.spi.annotations.PollDynamic;
+import org.apache.camel.support.component.ExpressionOptionsPollDynamicAware;
+
+/**
+ * The downloadFileName option of the google-storage consumer is evaluated as
a Simple expression for each object, which
+ * a dynamic uri (pollEnrich) keeps as written in the uri.
+ */
+@PollDynamic("google-storage")
+public class GoogleCloudStoragePollDynamicAware extends
ExpressionOptionsPollDynamicAware {
+
+ public GoogleCloudStoragePollDynamicAware() {
+ super(Set.of("downloadFileName"));
+ }
+}
diff --git
a/components/camel-jpa/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/jpa
b/components/camel-jpa/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/jpa
new file mode 100644
index 000000000000..7bdb71e964ee
--- /dev/null
+++
b/components/camel-jpa/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/jpa
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.component.jpa.JpaSendDynamicAware
diff --git
a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaSendDynamicAware.java
b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaSendDynamicAware.java
new file mode 100644
index 000000000000..8d7c96d01ebe
--- /dev/null
+++
b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaSendDynamicAware.java
@@ -0,0 +1,34 @@
+/*
+ * 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.jpa;
+
+import java.util.Set;
+
+import org.apache.camel.spi.annotations.SendDynamic;
+import org.apache.camel.support.component.ExpressionOptionsSendDynamicAware;
+
+/**
+ * The parameters of the jpa producer are evaluated as Simple expressions for
each message, which a dynamic uri (toD)
+ * keeps as written in the uri.
+ */
+@SendDynamic("jpa")
+public class JpaSendDynamicAware extends ExpressionOptionsSendDynamicAware {
+
+ public JpaSendDynamicAware() {
+ super(Set.of("parameters."), null);
+ }
+}
diff --git
a/components/camel-micrometer/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/micrometer
b/components/camel-micrometer/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/micrometer
new file mode 100644
index 000000000000..91afa3247949
--- /dev/null
+++
b/components/camel-micrometer/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/micrometer
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.component.micrometer.MicrometerSendDynamicAware
diff --git
a/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/MicrometerSendDynamicAware.java
b/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/MicrometerSendDynamicAware.java
new file mode 100644
index 000000000000..c578ce3dfc3a
--- /dev/null
+++
b/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/MicrometerSendDynamicAware.java
@@ -0,0 +1,35 @@
+/*
+ * 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.micrometer;
+
+import java.util.Set;
+
+import org.apache.camel.spi.annotations.SendDynamic;
+import org.apache.camel.support.component.ExpressionOptionsSendDynamicAware;
+
+/**
+ * The options of the micrometer producer that are evaluated as Simple
expressions for each message, which a dynamic uri
+ * (toD) keeps as written in the uri.
+ */
+@SendDynamic("micrometer")
+public class MicrometerSendDynamicAware extends
ExpressionOptionsSendDynamicAware {
+
+ public MicrometerSendDynamicAware() {
+ super(Set.of("action", "decrement", "increment", "metricsDescription",
"metricsName", "tags.", "value"),
+ ":metricsType:metricsName");
+ }
+}
diff --git
a/components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/MicrometerToDynamicTagTest.java
b/components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/MicrometerToDynamicTagTest.java
new file mode 100644
index 000000000000..52534cf33298
--- /dev/null
+++
b/components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/MicrometerToDynamicTagTest.java
@@ -0,0 +1,110 @@
+/*
+ * 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.micrometer;
+
+import java.util.Map;
+
+import io.micrometer.core.instrument.Counter;
+import io.micrometer.core.instrument.MeterRegistry;
+import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.ResolveEndpointFailedException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static
org.apache.camel.component.micrometer.MicrometerConstants.METRICS_REGISTRY_NAME;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * The tags of a meter in a dynamic uri (toD) are evaluated once, against the
message, as with a static uri.
+ */
+public class MicrometerToDynamicTagTest extends CamelTestSupport {
+
+ private final MeterRegistry registry = new SimpleMeterRegistry();
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext context = super.createCamelContext();
+ context.getRegistry().bind(METRICS_REGISTRY_NAME, registry);
+ return context;
+ }
+
+ @Test
+ public void testTagValueFromHeader() {
+ template.sendBodyAndHeaders("direct:start", "Hello",
+ Map.of("region", "eu", "name", "orders"));
+ template.sendBodyAndHeaders("direct:start", "Hello",
+ Map.of("region", "${header.name}", "name", "orders"));
+
+ Counter eu = registry.find("orders").tag("region", "eu").counter();
+ assertNotNull(eu);
+ assertEquals(1.0d, eu.count(), 0.01d);
+
+ // the tag value is the text of the header
+ Counter text = registry.find("orders").tag("region",
"${header.name}").counter();
+ assertNotNull(text);
+ assertEquals(1.0d, text.count(), 0.01d);
+ }
+
+ @Test
+ public void testMeterNameFromHeader() {
+ template.sendBodyAndHeaders("direct:name", "Hello",
+ Map.of("meter", "${header.other}", "other", "payments"));
+
+ // the meter name is the text of the header
+ assertNotNull(registry.find("${header.other}").counter());
+ }
+
+ @Test
+ public void testMeterTypeFromHeader() {
+ // the meter type is computed from the message, and the meter name is
evaluated by the producer
+ template.sendBodyAndHeaders("direct:type", "Hello", Map.of("type",
"counter", "region", "eu"));
+ template.sendBodyAndHeaders("direct:type", "Hello", Map.of("type",
"summary", "region", "us", "amount", 42));
+
+ assertNotNull(registry.find("orders.eu").counter());
+ assertNotNull(registry.find("orders.us").summary());
+ assertEquals(42.0d,
registry.find("orders.us").summary().totalAmount(), 0.01d);
+ }
+
+ @Test
+ public void testTagNotInUri() {
+ Map<String, Object> headers = Map.of("name",
"orders&tags.region=${header.region}", "region", "eu");
+ CamelExecutionException e = assertThrows(CamelExecutionException.class,
+ () -> template.sendBodyAndHeaders("direct:description",
"Hello", headers));
+ assertInstanceOf(ResolveEndpointFailedException.class, e.getCause());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+
from("direct:start").toD("micrometer:counter:orders?tags.region=${header.region}");
+
+ from("direct:name").toD("micrometer:counter:${header.meter}");
+
+
from("direct:type").toD("micrometer:${header.type}:orders.${header.region}?value=${header.amount}");
+
from("direct:description").toD("micrometer:counter:orders?metricsDescription=${header.name}");
+ }
+ };
+ }
+}
diff --git
a/components/camel-mina-sftp/src/generated/resources/META-INF/services/org/apache/camel/poll-dynamic/mina-sftp
b/components/camel-mina-sftp/src/generated/resources/META-INF/services/org/apache/camel/poll-dynamic/mina-sftp
new file mode 100644
index 000000000000..bce622c72e90
--- /dev/null
+++
b/components/camel-mina-sftp/src/generated/resources/META-INF/services/org/apache/camel/poll-dynamic/mina-sftp
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.component.file.remote.mina.MinaSftpPollDynamicAware
diff --git
a/components/camel-mina-sftp/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/mina-sftp
b/components/camel-mina-sftp/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/mina-sftp
new file mode 100644
index 000000000000..db28e6ab265d
--- /dev/null
+++
b/components/camel-mina-sftp/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/mina-sftp
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.component.file.remote.mina.MinaSftpSendDynamicAware
diff --git
a/components/camel-mina-sftp/src/main/java/org/apache/camel/component/file/remote/mina/MinaSftpPollDynamicAware.java
b/components/camel-mina-sftp/src/main/java/org/apache/camel/component/file/remote/mina/MinaSftpPollDynamicAware.java
new file mode 100644
index 000000000000..9e8ba8ec293d
--- /dev/null
+++
b/components/camel-mina-sftp/src/main/java/org/apache/camel/component/file/remote/mina/MinaSftpPollDynamicAware.java
@@ -0,0 +1,35 @@
+/*
+ * 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.remote.mina;
+
+import java.util.Set;
+
+import org.apache.camel.spi.annotations.PollDynamic;
+import org.apache.camel.support.component.ExpressionOptionsPollDynamicAware;
+
+/**
+ * The options of the mina-sftp endpoint that are evaluated as expressions for
each message, which a dynamic uri
+ * (pollEnrich) keeps as written in the uri.
+ */
+@PollDynamic("mina-sftp")
+public class MinaSftpPollDynamicAware extends
ExpressionOptionsPollDynamicAware {
+
+ public MinaSftpPollDynamicAware() {
+ super(Set.of("doneFileName", "fileName", "filterDirectory",
"filterFile", "idempotentKey", "move", "moveExisting",
+ "moveFailed", "preMove", "sortBy", "tempFileName"));
+ }
+}
diff --git
a/components/camel-mina-sftp/src/main/java/org/apache/camel/component/file/remote/mina/MinaSftpSendDynamicAware.java
b/components/camel-mina-sftp/src/main/java/org/apache/camel/component/file/remote/mina/MinaSftpSendDynamicAware.java
new file mode 100644
index 000000000000..a469fd0f4455
--- /dev/null
+++
b/components/camel-mina-sftp/src/main/java/org/apache/camel/component/file/remote/mina/MinaSftpSendDynamicAware.java
@@ -0,0 +1,36 @@
+/*
+ * 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.remote.mina;
+
+import java.util.Set;
+
+import org.apache.camel.spi.annotations.SendDynamic;
+import org.apache.camel.support.component.ExpressionOptionsSendDynamicAware;
+
+/**
+ * The options of the mina-sftp endpoint that are evaluated as expressions for
each message, which a dynamic uri (toD)
+ * keeps as written in the uri.
+ */
+@SendDynamic("mina-sftp")
+public class MinaSftpSendDynamicAware extends
ExpressionOptionsSendDynamicAware {
+
+ public MinaSftpSendDynamicAware() {
+ super(Set.of("doneFileName", "fileName", "filterDirectory",
"filterFile", "idempotentKey", "move", "moveExisting",
+ "moveFailed", "preMove", "sortBy", "tempFileName"),
+ null);
+ }
+}
diff --git
a/components/camel-opentelemetry-metrics/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/opentelemetry-metrics
b/components/camel-opentelemetry-metrics/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/opentelemetry-metrics
new file mode 100644
index 000000000000..b7633fd5be30
--- /dev/null
+++
b/components/camel-opentelemetry-metrics/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/opentelemetry-metrics
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.opentelemetry.metrics.OpenTelemetrySendDynamicAware
diff --git
a/components/camel-opentelemetry-metrics/src/main/java/org/apache/camel/opentelemetry/metrics/OpenTelemetrySendDynamicAware.java
b/components/camel-opentelemetry-metrics/src/main/java/org/apache/camel/opentelemetry/metrics/OpenTelemetrySendDynamicAware.java
new file mode 100644
index 000000000000..8e2ec4187943
--- /dev/null
+++
b/components/camel-opentelemetry-metrics/src/main/java/org/apache/camel/opentelemetry/metrics/OpenTelemetrySendDynamicAware.java
@@ -0,0 +1,35 @@
+/*
+ * 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.opentelemetry.metrics;
+
+import java.util.Set;
+
+import org.apache.camel.spi.annotations.SendDynamic;
+import org.apache.camel.support.component.ExpressionOptionsSendDynamicAware;
+
+/**
+ * The options of the opentelemetry-metrics producer that are evaluated as
Simple expressions for each message, which a
+ * dynamic uri (toD) keeps as written in the uri.
+ */
+@SendDynamic("opentelemetry-metrics")
+public class OpenTelemetrySendDynamicAware extends
ExpressionOptionsSendDynamicAware {
+
+ public OpenTelemetrySendDynamicAware() {
+ super(Set.of("action", "attributes.", "decrement", "increment",
"metricName", "metricsDescription", "value"),
+ ":metricType:metricName");
+ }
+}
diff --git
a/components/camel-sql/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/sql-stored
b/components/camel-sql/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/sql-stored
new file mode 100644
index 000000000000..0b2ff228e6e2
--- /dev/null
+++
b/components/camel-sql/src/generated/resources/META-INF/services/org/apache/camel/send-dynamic/sql-stored
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.component.sql.stored.SqlStoredSendDynamicAware
diff --git
a/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredSendDynamicAware.java
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredSendDynamicAware.java
new file mode 100644
index 000000000000..0772195f368e
--- /dev/null
+++
b/components/camel-sql/src/main/java/org/apache/camel/component/sql/stored/SqlStoredSendDynamicAware.java
@@ -0,0 +1,34 @@
+/*
+ * 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.sql.stored;
+
+import java.util.Set;
+
+import org.apache.camel.spi.annotations.SendDynamic;
+import org.apache.camel.support.component.ExpressionOptionsSendDynamicAware;
+
+/**
+ * The template of the sql-stored producer has Simple expressions that are
evaluated for each message, which a dynamic
+ * uri (toD) keeps as written in the uri.
+ */
+@SendDynamic("sql-stored")
+public class SqlStoredSendDynamicAware extends
ExpressionOptionsSendDynamicAware {
+
+ public SqlStoredSendDynamicAware() {
+ super(Set.of("template"), ":template");
+ }
+}
diff --git
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/DynamicUriTemplate.java
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/DynamicUriTemplate.java
new file mode 100644
index 000000000000..5fa462b27124
--- /dev/null
+++
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/DynamicUriTemplate.java
@@ -0,0 +1,328 @@
+/*
+ * 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.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Function;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ResolveEndpointFailedException;
+import org.apache.camel.support.EndpointHelper;
+import org.apache.camel.support.ExchangeHelper;
+import org.apache.camel.support.component.ExpressionOptionsDynamicAware;
+import org.apache.camel.util.URISupport;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The uri template of a dynamic endpoint (toD, enrich, pollEnrich and wireTap
with a dynamic uri), for an endpoint that
+ * evaluates some of its options as an expression for each message (see
+ * {@link ExpressionOptionsDynamicAware#expressionPropertyNames()}), which its
+ * {@link org.apache.camel.spi.SendDynamicAware} or {@link
org.apache.camel.spi.PollDynamicAware} declares.
+ * <p>
+ * Such an option keeps the text that is written in the uri template, so the
endpoint evaluates the template once, as
+ * with a static uri, and a value taken from the message is used as it is.
Such an option that the uri template does not
+ * have (it is only in the computed uri) is rejected.
+ */
+final class DynamicUriTemplate {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(DynamicUriTemplate.class);
+
+ private final String uri;
+ private final String scheme;
+ private final String path;
+ private final PathSyntax pathSyntax;
+ private final Map<String, Object> parameters;
+ private final Set<String> expressionNames;
+
+ private DynamicUriTemplate(String uri, String scheme, String path,
PathSyntax pathSyntax, Map<String, Object> parameters,
+ Set<String> expressionNames) {
+ this.uri = uri;
+ this.scheme = scheme;
+ this.path = path;
+ this.pathSyntax = pathSyntax;
+ this.parameters = parameters;
+ this.expressionNames = expressionNames;
+ }
+
+ /**
+ * Creates the uri template of the given dynamic uri.
+ *
+ * @param context the camel context
+ * @param uri the dynamic uri as written in the route
+ * @param producer whether the uri is for a producer (and not a consumer,
such as with pollEnrich); the path
+ * options that are evaluated for each message are
producer options, so the path is only kept for a
+ * producer
+ * @param resolver resolves the send or poll dynamic aware of the scheme
of the uri (or returns <tt>null</tt>)
+ * @return the template, or <tt>null</tt> if the endpoint has no
option that is evaluated as an expression,
+ * or the scheme of the uri is not known before it is
computed
+ */
+ static DynamicUriTemplate create(
+ CamelContext context, String uri, boolean producer,
Function<String, Object> resolver) {
+ if (uri == null || uri.startsWith("language:")) {
+ return null;
+ }
+ try {
+ String text =
EndpointHelper.resolveEndpointUriPropertyPlaceholders(context, uri.trim());
+ String scheme = ExchangeHelper.resolveScheme(text);
+ if (scheme == null || scheme.isEmpty() || !isPlainScheme(scheme)) {
+ return null;
+ }
+ ExpressionOptionsDynamicAware options = resolver.apply(scheme)
instanceof ExpressionOptionsDynamicAware aware
+ ? aware : null;
+ if (options == null || options.expressionPropertyNames() == null
+ || options.expressionPropertyNames().isEmpty()) {
+ return null;
+ }
+ int pos = indexOfQuery(text);
+ String path = null;
+ PathSyntax pathSyntax = null;
+ if (producer && options.expressionPathSyntax() != null) {
+ path = text.substring(scheme.length() + 1, pos != -1 ? pos :
text.length());
+ pathSyntax = PathSyntax.parse(options.expressionPathSyntax());
+ }
+ Map<String, Object> parameters = pos != -1
+ ? URISupport.parseQuery(text.substring(pos + 1), true,
true)
+ : Map.of();
+ return new DynamicUriTemplate(text, scheme, path, pathSyntax,
parameters, options.expressionPropertyNames());
+ } catch (Exception e) {
+ LOG.debug("Cannot parse the dynamic uri: {} as an uri template due
to: {}. This exception is ignored.",
+ URISupport.sanitizeUri(uri), e.getMessage(), e);
+ return null;
+ }
+ }
+
+ /**
+ * Puts the text of the uri template back into the options that the
endpoint evaluates as an expression.
+ *
+ * @param computed the uri computed from the message
+ * @return the uri to use
+ * @throws ResolveEndpointFailedException if the computed uri has such an
option that the template does not have
+ */
+ String apply(String computed) throws URISyntaxException {
+ if (computed.equals(uri) || !computed.startsWith(scheme + ":")) {
+ // the same as the template, or another endpoint than the template
+ return computed;
+ }
+ boolean changed = false;
+ int pos = computed.indexOf('?');
+ String computedPath = computed.substring(scheme.length() + 1, pos !=
-1 ? pos : computed.length());
+ if (path != null && !path.equals(computedPath)) {
+ computedPath = restorePath(computedPath);
+ changed = true;
+ }
+ Map<String, Object> computedParameters = pos != -1
+ ? URISupport.parseQuery(computed.substring(pos + 1), true,
true)
+ : Map.of();
+ for (Map.Entry<String, Object> entry : computedParameters.entrySet()) {
+ String key = entry.getKey();
+ if (isExpression(key)) {
+ Object value = parameters.get(key);
+ if (value == null) {
+ throw new ResolveEndpointFailedException(
+ computed, "The option " + key + " is evaluated by
the " + scheme
+ + " endpoint for each message, and must
be in the uri as written in the route");
+ }
+ if (!value.equals(entry.getValue())) {
+ entry.setValue(value);
+ changed = true;
+ }
+ }
+ }
+ if (!changed) {
+ return computed;
+ }
+ StringBuilder sb = new StringBuilder(computed.length());
+ sb.append(scheme).append(':').append(computedPath);
+ char separator = '?';
+ for (Map.Entry<String, Object> entry : computedParameters.entrySet()) {
+ // the values are raw (as in the uri), so they are appended as
they are
+ List<?> values = entry.getValue() instanceof List<?> list ? list :
List.of(entry.getValue());
+ for (Object value : values) {
+
sb.append(separator).append(entry.getKey()).append('=').append(value);
+ separator = '&';
+ }
+ }
+ return sb.toString();
+ }
+
+ /**
+ * The path to use: the path properties that the endpoint evaluates as an
expression keep the text of the uri
+ * template, and the other path properties (such as the meter type of
micrometer) are taken from the computed uri.
+ * If the paths cannot be split by the syntax of the endpoint, the path of
the uri template is used as a whole.
+ */
+ private String restorePath(String computedPath) {
+ if (pathSyntax == null) {
+ return path;
+ }
+ List<String> template = pathSyntax.split(path);
+ List<String> computed = pathSyntax.split(computedPath);
+ if (template == null || computed == null) {
+ return path;
+ }
+ List<String> segments = new ArrayList<>(computed.size());
+ for (int i = 0; i < computed.size(); i++) {
+ segments.add(expressionNames.contains(pathSyntax.names.get(i)) ?
template.get(i) : computed.get(i));
+ }
+ return pathSyntax.join(segments);
+ }
+
+ private boolean isExpression(String key) {
+ if (expressionNames.contains(key)) {
+ return true;
+ }
+ for (String name : expressionNames) {
+ // a multi-valued option, by its prefix
+ if (name.endsWith(".") && key.startsWith(name)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private static boolean isPlainScheme(String scheme) {
+ for (int i = 0; i < scheme.length(); i++) {
+ char ch = scheme.charAt(i);
+ if (!Character.isLetterOrDigit(ch) && ch != '-' && ch != '+' && ch
!= '.' && ch != '_') {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * The index of the ? sign that starts the query of the uri template,
which is not inside an expression such as
+ * <tt>${header.name?.trim()}</tt>.
+ */
+ private static int indexOfQuery(String uri) {
+ int depth = 0;
+ for (int i = 0; i < uri.length(); i++) {
+ char ch = uri.charAt(i);
+ if (ch == '{') {
+ depth++;
+ } else if (ch == '}' && depth > 0) {
+ depth--;
+ } else if (ch == '?' && depth == 0) {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ /**
+ * The syntax of the path of an endpoint uri, such as
<tt>:metricsType:metricsName</tt> or
+ * <tt>://bucketNameOrArn</tt>: the names of the path properties, and the
delimiters before and between them. The
+ * last path property takes the rest of the path.
+ */
+ static final class PathSyntax {
+
+ private final String prefix;
+ private final List<String> names;
+ private final List<String> delimiters;
+
+ private PathSyntax(String prefix, List<String> names, List<String>
delimiters) {
+ this.prefix = prefix;
+ this.names = names;
+ this.delimiters = delimiters;
+ }
+
+ static PathSyntax parse(String syntax) {
+ // the path of a uri starts after the colon of the scheme
+ String text = syntax.startsWith(":") ? syntax.substring(1) :
syntax;
+ List<String> names = new ArrayList<>();
+ List<String> delimiters = new ArrayList<>();
+ StringBuilder prefix = new StringBuilder();
+ StringBuilder name = new StringBuilder();
+ StringBuilder delimiter = new StringBuilder();
+ for (int i = 0; i < text.length(); i++) {
+ char ch = text.charAt(i);
+ if (Character.isLetterOrDigit(ch) || ch == '_') {
+ if (!delimiter.isEmpty()) {
+ delimiters.add(delimiter.toString());
+ delimiter.setLength(0);
+ }
+ name.append(ch);
+ } else if (names.isEmpty() && name.isEmpty()) {
+ prefix.append(ch);
+ } else {
+ if (!name.isEmpty()) {
+ names.add(name.toString());
+ name.setLength(0);
+ }
+ delimiter.append(ch);
+ }
+ }
+ if (!name.isEmpty()) {
+ names.add(name.toString());
+ }
+ return names.isEmpty() || delimiters.size() != names.size() - 1
+ ? null : new PathSyntax(prefix.toString(), names,
delimiters);
+ }
+
+ /**
+ * Splits a path into the values of its path properties, or returns
<tt>null</tt> if the path does not match the
+ * syntax. A delimiter inside an expression, such as the colon of
<tt>${date:now}</tt>, does not split.
+ */
+ List<String> split(String path) {
+ if (!path.startsWith(prefix)) {
+ return null;
+ }
+ List<String> answer = new ArrayList<>(names.size());
+ int pos = prefix.length();
+ for (String delimiter : delimiters) {
+ int end = indexOf(path, delimiter, pos);
+ if (end == -1) {
+ return null;
+ }
+ answer.add(path.substring(pos, end));
+ pos = end + delimiter.length();
+ }
+ answer.add(path.substring(pos));
+ return answer;
+ }
+
+ String join(List<String> segments) {
+ StringBuilder sb = new StringBuilder(prefix);
+ for (int i = 0; i < segments.size(); i++) {
+ if (i > 0) {
+ sb.append(delimiters.get(i - 1));
+ }
+ sb.append(segments.get(i));
+ }
+ return sb.toString();
+ }
+
+ private static int indexOf(String text, String delimiter, int from) {
+ int depth = 0;
+ for (int i = from; i < text.length(); i++) {
+ char ch = text.charAt(i);
+ if (ch == '{') {
+ depth++;
+ } else if (ch == '}' && depth > 0) {
+ depth--;
+ } else if (depth == 0 && text.startsWith(delimiter, i)) {
+ return i;
+ }
+ }
+ return -1;
+ }
+ }
+}
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 8670c9999ac3..708cd2328f6c 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
@@ -97,6 +97,7 @@ public class PollEnricher extends BaseProcessorSupport
implements IdAware, Route
private Set<String> allowedSchemes;
private boolean autoStartupComponents = true;
private boolean allowOptimisedComponents = true;
+ private DynamicUriTemplate uriTemplate;
/**
* Creates a new {@link PollEnricher}.
@@ -288,6 +289,10 @@ public class PollEnricher extends BaseProcessorSupport
implements IdAware, Route
boolean prototype = cacheSize < 0;
try {
recipient = expression.evaluate(exchange, Object.class);
+ if (uriTemplate != null && recipient instanceof String computed) {
+ // the options that the endpoint evaluates as an expression
keep the text of the uri template
+ recipient = uriTemplate.apply(computed.trim());
+ }
if (dynamicAware != null) {
// if its the same scheme as the pre-resolved dynamic aware
then we can optimise to use it
String originalUri = uri;
@@ -668,11 +673,33 @@ public class PollEnricher extends BaseProcessorSupport
implements IdAware, Route
}
}
+ if (expression != null) {
+ // the options that the endpoint evaluates as an expression keep
the text of the uri template, also when not
+ // optimised
+ uriTemplate = DynamicUriTemplate.create(camelContext, uri, false,
this::resolveExpressionOptionsAware);
+ }
+
headersMapFactory =
camelContext.getCamelContextExtension().getHeadersMapFactory();
ServiceHelper.initService(consumerCache, aggregationStrategy,
dynamicAware);
}
+ private Object resolveExpressionOptionsAware(String scheme) {
+ if (dynamicAware != null && scheme.equals(dynamicAware.getScheme())) {
+ return dynamicAware;
+ }
+ PollDynamicAwareResolver resolver = new PollDynamicAwareResolver();
+ PollDynamicAware answer = resolver.resolve(camelContext, scheme);
+ if (answer == null) {
+ // fallback and try with default component name
+ Component comp = camelContext.getComponent(scheme, false,
isAutoStartupComponents());
+ if (comp != null && comp.getDefaultName() != null &&
!scheme.equals(comp.getDefaultName())) {
+ answer = resolver.resolve(camelContext, comp.getDefaultName());
+ }
+ }
+ return answer;
+ }
+
@Override
protected void doStart() throws Exception {
// ensure the component is started
diff --git
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/SendDynamicProcessor.java
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/SendDynamicProcessor.java
index b2773c979990..0cc93c62bd36 100644
---
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/SendDynamicProcessor.java
+++
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/SendDynamicProcessor.java
@@ -81,6 +81,7 @@ public class SendDynamicProcessor extends BaseProcessorSupport
protected boolean autoStartupComponents = true;
protected String allowedSchemes;
private Set<String> allowedSchemesSet;
+ private DynamicUriTemplate uriTemplate;
public SendDynamicProcessor(String uri, Expression expression) {
this.uri = uri;
@@ -146,6 +147,10 @@ public class SendDynamicProcessor extends
BaseProcessorSupport
boolean prototype = cacheSize < 0;
try {
recipient = expression.evaluate(exchange, Object.class);
+ if (uriTemplate != null && recipient instanceof String computed) {
+ // the options that the endpoint evaluates as an expression
keep the text of the uri template
+ recipient = uriTemplate.apply(computed.trim());
+ }
if (dynamicAware != null) {
// if its the same scheme as the pre-resolved dynamic aware
then we can optimise to use it
String originalUri = uri;
@@ -399,11 +404,30 @@ public class SendDynamicProcessor extends
BaseProcessorSupport
}
}
}
+ // the options that the endpoint evaluates as an expression keep the
text of the uri template, also when not optimised
+ uriTemplate = DynamicUriTemplate.create(camelContext, uri, true,
this::resolveExpressionOptionsAware);
+
ServiceHelper.initService(dynamicAware);
headersMapFactory =
camelContext.getCamelContextExtension().getHeadersMapFactory();
}
+ private Object resolveExpressionOptionsAware(String scheme) {
+ if (dynamicAware != null && scheme.equals(dynamicAware.getScheme())) {
+ return dynamicAware;
+ }
+ SendDynamicAwareResolver resolver = new SendDynamicAwareResolver();
+ SendDynamicAware answer = resolver.resolve(camelContext, scheme);
+ if (answer == null) {
+ // fallback and try with default component name
+ Component comp = camelContext.getComponent(scheme, false,
isAutoStartupComponents());
+ if (comp != null && comp.getDefaultName() != null &&
!scheme.equals(comp.getDefaultName())) {
+ answer = resolver.resolve(camelContext, comp.getDefaultName());
+ }
+ }
+ return answer;
+ }
+
@Override
protected void doStart() throws Exception {
// ensure the component is started
diff --git
a/core/camel-core/src/test/java/org/apache/camel/component/file/PollEnrichFileExpressionOptionTest.java
b/core/camel-core/src/test/java/org/apache/camel/component/file/PollEnrichFileExpressionOptionTest.java
new file mode 100644
index 000000000000..23429dca4cae
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/component/file/PollEnrichFileExpressionOptionTest.java
@@ -0,0 +1,86 @@
+/*
+ * 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 java.util.Map;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.ResolveEndpointFailedException;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * pollEnrich with a dynamic file uri keeps the uri text of the options that
the file consumer evaluates.
+ */
+public class PollEnrichFileExpressionOptionTest extends ContextTestSupport {
+
+ @Test
+ public void testFilterWithHeaderValue() throws Exception {
+ getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
+
+ template.sendBodyAndHeader(fileUri(), "Hello World",
Exchange.FILE_NAME, "hello.txt");
+
+ template.sendBodyAndHeader("direct:start", "Trigger", "wanted",
"hello.txt");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ public void testFilterHeaderTextIsUsedAsGiven() throws Exception {
+ // the header is compared as text with the file name, so no file
matches
+ getMockEndpoint("mock:result").expectedMessageCount(1);
+ getMockEndpoint("mock:result").message(0).body().isNull();
+
+ template.sendBodyAndHeader(fileUri(), "Hello World",
Exchange.FILE_NAME, "hello.txt");
+
+ template.sendBodyAndHeaders("direct:start", "Trigger",
Map.of("wanted", "${header.other}", "other", "hello.txt"));
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ public void testOptionNotInUriIsRejected() {
+ template.sendBodyAndHeader(fileUri(), "Hello World",
Exchange.FILE_NAME, "hello.txt");
+
+ Map<String, Object> headers = Map.of("name",
"hello.txt&move=${header.other}", "other", "done");
+ CamelExecutionException e = assertThrows(CamelExecutionException.class,
+ () -> template.sendBodyAndHeaders("direct:name", "Trigger",
headers));
+ assertInstanceOf(ResolveEndpointFailedException.class, e.getCause());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ public void configure() {
+ from("direct:start")
+ .pollEnrich().simple(fileUri() +
"?initialDelay=0&filterFile=${file:name} == ${header.wanted}")
+ .timeout(1000).end()
+ .to("mock:result");
+
+ from("direct:name")
+ .pollEnrich().simple(fileUri() +
"?initialDelay=0&fileName=${header.name}").timeout(1000).end()
+ .to("mock:result");
+ }
+ };
+ }
+
+}
diff --git
a/core/camel-core/src/test/java/org/apache/camel/processor/ToDynamicExpressionOptionTest.java
b/core/camel-core/src/test/java/org/apache/camel/processor/ToDynamicExpressionOptionTest.java
new file mode 100644
index 000000000000..05ff3f94bc6d
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/processor/ToDynamicExpressionOptionTest.java
@@ -0,0 +1,281 @@
+/*
+ * 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.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.Consumer;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.apache.camel.ResolveEndpointFailedException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.support.DefaultComponent;
+import org.apache.camel.support.DefaultEndpoint;
+import org.apache.camel.support.DefaultProducer;
+import org.apache.camel.support.component.EndpointUriFactorySupport;
+import org.apache.camel.support.component.ExpressionOptionsSendDynamicAware;
+import org.apache.camel.util.PropertiesHelper;
+import org.apache.camel.util.StringHelper;
+import org.junit.jupiter.api.Test;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A dynamic uri keeps the uri template text of the options that the endpoint
evaluates as an expression.
+ */
+public class ToDynamicExpressionOptionTest extends ContextTestSupport {
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext context = super.createCamelContext();
+ context.addComponent("greet", new GreetComponent());
+ context.getRegistry().bind("greetFactory", new
GreetEndpointUriFactory());
+ return context;
+ }
+
+ @Test
+ public void testHeaderValueIsUsedAsGiven() throws Exception {
+ getMockEndpoint("mock:result").expectedHeaderReceived("greeting",
"Hello ${header.name}");
+ getMockEndpoint("mock:result").expectedHeaderReceived("tag",
"${header.name}");
+ getMockEndpoint("mock:result").expectedHeaderReceived("label",
"orders");
+
+ template.sendBodyAndHeaders("direct:start", "Hi",
+ Map.of("greeting", "Hello ${header.name}", "tag",
"${header.name}", "label", "orders", "name", "World"));
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ public void testHeaderValueWithoutExpression() throws Exception {
+
getMockEndpoint("mock:result").expectedHeaderValuesReceivedInAnyOrder("greeting",
"Hello World", "Hello Camel");
+
getMockEndpoint("mock:result").expectedHeaderValuesReceivedInAnyOrder("tag",
"blue", "red");
+
getMockEndpoint("mock:result").allMessages().header("label").isEqualTo("orders");
+
+ template.sendBodyAndHeaders("direct:start", "Hi",
+ Map.of("greeting", "Hello World", "tag", "blue", "label",
"orders"));
+ template.sendBodyAndHeaders("direct:start", "Hi",
+ Map.of("greeting", "Hello Camel", "tag", "red", "label",
"orders"));
+
+ assertMockEndpointsSatisfied();
+
+ // the options that are evaluated by the endpoint keep the uri
template, so both messages use the same endpoint
+ long count = context.getEndpoints().stream().filter(e ->
e.getEndpointUri().startsWith("greet:")).count();
+ assertEquals(1, count);
+ }
+
+ @Test
+ public void testPathIsUsedAsGiven() throws Exception {
+ getMockEndpoint("mock:result").expectedHeaderReceived("kind", "loud");
+ getMockEndpoint("mock:result").expectedHeaderReceived("path",
"${header.other}");
+
+ template.sendBodyAndHeaders("direct:path", "Hi", Map.of("kind",
"loud", "path", "${header.other}", "other", "World"));
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ public void testPathPropertyThatIsNotAnExpression() throws Exception {
+ // the kind is a path property that the endpoint does not evaluate, so
it is computed from the message as before,
+ // while the name keeps the text of the uri template and is evaluated
by the endpoint
+
getMockEndpoint("mock:result").expectedHeaderValuesReceivedInAnyOrder("kind",
"loud", "quiet");
+
getMockEndpoint("mock:result").allMessages().header("path").isEqualTo("hello-World");
+
+ template.sendBodyAndHeaders("direct:pathKind", "Hi", Map.of("kind",
"loud", "name", "World"));
+ template.sendBodyAndHeaders("direct:pathKind", "Hi", Map.of("kind",
"quiet", "name", "World"));
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ public void testOptionNotInUriIsRejected() {
+ Map<String, Object> headers = Map.of("greeting", "Hi", "tag", "blue",
"label", "orders&tags.size=${header.name}",
+ "name", "World");
+ CamelExecutionException e = assertThrows(CamelExecutionException.class,
+ () -> template.sendBodyAndHeaders("direct:start", "Hi",
headers));
+ ResolveEndpointFailedException cause =
assertInstanceOf(ResolveEndpointFailedException.class, e.getCause());
+ assertTrue(cause.getMessage().contains("The option tags.size is
evaluated by the greet endpoint"), cause.getMessage());
+ }
+
+ @Test
+ public void testNotOptimised() throws Exception {
+ getMockEndpoint("mock:result").expectedHeaderReceived("greeting",
"Hello ${header.name}");
+
+ template.sendBodyAndHeaders("direct:notOptimised", "Hi",
Map.of("greeting", "Hello ${header.name}", "name", "World"));
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ public void testEnrich() throws Exception {
+ getMockEndpoint("mock:result").expectedHeaderReceived("greeting",
"Hello ${header.name}");
+
+ template.sendBodyAndHeaders("direct:enrich", "Hi", Map.of("greeting",
"Hello ${header.name}", "name", "World"));
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ public void testWireTap() {
+ GreetComponent component = context.getComponent("greet",
GreetComponent.class);
+
+ template.sendBodyAndHeaders("direct:wireTap", "Hi", Map.of("greeting",
"Hello ${header.name}", "name", "World"));
+
+ await().atMost(20, TimeUnit.SECONDS).until(() ->
!component.greetings.isEmpty());
+ assertEquals("Hello ${header.name}", component.greetings.get(0));
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:start")
+
.toD("greet:a:b?greeting=${header.greeting}&tags.color=${header.tag}&label=${header.label}")
+ .to("mock:result");
+
+
from("direct:path").toD("greet:${header.kind}:${header.path}").to("mock:result");
+
+
from("direct:pathKind").toD("greet:${header.kind}:hello-${header.name}").to("mock:result");
+
+
from("direct:notOptimised").toD("greet:a:b?greeting=${header.greeting}",
false).to("mock:result");
+
+
from("direct:enrich").enrich().simple("greet:a:b?greeting=${header.greeting}").end().to("mock:result");
+
+
from("direct:wireTap").wireTap("greet:a:b?greeting=${header.greeting}");
+ }
+ };
+ }
+
+ /**
+ * A component with the path kind:path that evaluates the path property,
its greeting option and its tags as Simple
+ * expressions for each message (and not the kind).
+ */
+ private static final class GreetComponent extends DefaultComponent {
+
+ private final List<String> greetings = new CopyOnWriteArrayList<>();
+
+ @Override
+ protected Endpoint createEndpoint(String uri, String remaining,
Map<String, Object> parameters) {
+ GreetEndpoint answer = new GreetEndpoint(uri, this);
+ answer.kind = StringHelper.before(remaining, ":", remaining);
+ answer.path = StringHelper.after(remaining, ":", "");
+ answer.greeting = (String) parameters.remove("greeting");
+ answer.label = (String) parameters.remove("label");
+ answer.tags = PropertiesHelper.extractProperties(parameters,
"tags.");
+ return answer;
+ }
+ }
+
+ private static final class GreetEndpoint extends DefaultEndpoint {
+
+ private String kind;
+ private String path;
+ private String greeting;
+ private String label;
+ private Map<String, Object> tags;
+
+ GreetEndpoint(String uri, GreetComponent component) {
+ super(uri, component);
+ }
+
+ @Override
+ public Producer createProducer() {
+ return new DefaultProducer(this) {
+ @Override
+ public void process(Exchange exchange) {
+ String text = evaluate(exchange, greeting);
+ ((GreetComponent) getComponent()).greetings.add(text);
+ exchange.getMessage().setHeader("greeting", text);
+ exchange.getMessage().setHeader("kind", kind);
+ exchange.getMessage().setHeader("path", evaluate(exchange,
path));
+ exchange.getMessage().setHeader("label", label);
+ Object tag = tags.get("color");
+ exchange.getMessage().setHeader("tag", evaluate(exchange,
tag != null ? tag.toString() : null));
+ }
+ };
+ }
+
+ @Override
+ public Consumer createConsumer(Processor processor) {
+ throw new UnsupportedOperationException("Producer only");
+ }
+
+ private static String evaluate(Exchange exchange, String text) {
+ if (text == null) {
+ return null;
+ }
+ return
exchange.getContext().resolveLanguage("simple").createExpression(text).evaluate(exchange,
String.class);
+ }
+ }
+
+ private static final class GreetEndpointUriFactory extends
EndpointUriFactorySupport {
+
+ @Override
+ public boolean isEnabled(String scheme) {
+ return "greet".equals(scheme);
+ }
+
+ @Override
+ public String buildUri(String scheme, Map<String, Object> properties,
boolean encode) {
+ // not in use for this test
+ return null;
+ }
+
+ @Override
+ public Set<String> propertyNames() {
+ return Set.of("kind", "path", "greeting", "label", "tags");
+ }
+
+ @Override
+ public Set<String> secretPropertyNames() {
+ return Set.of();
+ }
+
+ @Override
+ public Map<String, String> multiValuePrefixes() {
+ return Map.of("tags", "tags.");
+ }
+
+ @Override
+ public boolean isLenientProperties() {
+ return false;
+ }
+ }
+
+ /**
+ * Declares the options that the greet endpoint evaluates as an expression
(see the send-dynamic/greet resource).
+ */
+ public static final class GreetSendDynamicAware extends
ExpressionOptionsSendDynamicAware {
+
+ public GreetSendDynamicAware() {
+ super(Set.of("path", "greeting", "tags."), ":kind:path");
+ }
+ }
+}
diff --git
a/core/camel-core/src/test/java/org/apache/camel/processor/enricher/PollDynamicFileNameOptimizeDisabledTest.java
b/core/camel-core/src/test/java/org/apache/camel/processor/enricher/PollDynamicFileNameOptimizeDisabledTest.java
index fcf2c588d517..21b01190f4c3 100644
---
a/core/camel-core/src/test/java/org/apache/camel/processor/enricher/PollDynamicFileNameOptimizeDisabledTest.java
+++
b/core/camel-core/src/test/java/org/apache/camel/processor/enricher/PollDynamicFileNameOptimizeDisabledTest.java
@@ -37,10 +37,10 @@ public class PollDynamicFileNameOptimizeDisabledTest
extends ContextTestSupport
assertMockEndpointsSatisfied();
- // there should only be 1 file endpoint
+ // there should only be 1 file endpoint, as the fileName option keeps
the uri text, also when not optimised
long c = context.getEndpoints().stream()
.filter(e -> e.getEndpointKey().startsWith("file") &&
e.getEndpointUri().contains("?fileName=")).count();
- Assertions.assertEquals(2, c, "There should only be 2 file endpoints");
+ Assertions.assertEquals(1, c, "There should only be 1 file endpoint");
}
@Test
@@ -55,10 +55,10 @@ public class PollDynamicFileNameOptimizeDisabledTest
extends ContextTestSupport
assertMockEndpointsSatisfied();
- // there should only be 1 file endpoint
+ // there should only be 1 file endpoint, as the fileName option keeps
the uri text, also when not optimised
long c = context.getEndpoints().stream()
.filter(e -> e.getEndpointKey().startsWith("file") &&
e.getEndpointUri().contains("?fileName=")).count();
- Assertions.assertEquals(2, c, "There should only be 2 file endpoints");
+ Assertions.assertEquals(1, c, "There should only be 1 file endpoint");
}
@Override
diff --git
a/core/camel-core/src/test/resources/META-INF/services/org/apache/camel/send-dynamic/greet
b/core/camel-core/src/test/resources/META-INF/services/org/apache/camel/send-dynamic/greet
new file mode 100644
index 000000000000..35e9ed6f9cd7
--- /dev/null
+++
b/core/camel-core/src/test/resources/META-INF/services/org/apache/camel/send-dynamic/greet
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+class=org.apache.camel.processor.ToDynamicExpressionOptionTest$GreetSendDynamicAware
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/component/ExpressionOptionsDynamicAware.java
b/core/camel-support/src/main/java/org/apache/camel/support/component/ExpressionOptionsDynamicAware.java
new file mode 100644
index 000000000000..868af6039f32
--- /dev/null
+++
b/core/camel-support/src/main/java/org/apache/camel/support/component/ExpressionOptionsDynamicAware.java
@@ -0,0 +1,49 @@
+/*
+ * 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.support.component;
+
+import java.util.Set;
+
+/**
+ * Implemented by a {@link org.apache.camel.spi.SendDynamicAware} or {@link
org.apache.camel.spi.PollDynamicAware} of an
+ * endpoint that evaluates some of its options as an expression (such as the
Simple language) for each message, such as
+ * a file name or a meter name with <tt>${header.name}</tt> in it.
+ * <p>
+ * A dynamic endpoint uri (such as <tt>toD</tt>, <tt>enrich</tt>,
<tt>pollEnrich</tt> and <tt>wireTap</tt>) keeps the
+ * text that is written in its uri for these options, so the endpoint
evaluates that text, and not the result of the
+ * dynamic uri. This applies also when the dynamic uri is not optimised.
+ */
+public interface ExpressionOptionsDynamicAware {
+
+ /**
+ * Returns the names of the options that the endpoint evaluates as an
expression for each message.
+ * <p>
+ * A multi-valued option is returned by its prefix (such as
<tt>tags.</tt>), and the path properties are included as
+ * well.
+ */
+ Set<String> expressionPropertyNames();
+
+ /**
+ * The syntax of the path of the endpoint uri (what follows the scheme),
such as <tt>:metricsType:metricsName</tt>,
+ * when one of the path properties is evaluated as an expression for each
message (see
+ * {@link #expressionPropertyNames()}), or <tt>null</tt> otherwise. A
dynamic endpoint uri of a producer keeps the
+ * text that is written in its uri for those path properties, and computes
the other path properties as before.
+ */
+ default String expressionPathSyntax() {
+ return null;
+ }
+}
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/component/ExpressionOptionsPollDynamicAware.java
b/core/camel-support/src/main/java/org/apache/camel/support/component/ExpressionOptionsPollDynamicAware.java
new file mode 100644
index 000000000000..b539a911ec6d
--- /dev/null
+++
b/core/camel-support/src/main/java/org/apache/camel/support/component/ExpressionOptionsPollDynamicAware.java
@@ -0,0 +1,57 @@
+/*
+ * 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.support.component;
+
+import java.util.Set;
+
+import org.apache.camel.Exchange;
+
+/**
+ * A {@link org.apache.camel.spi.PollDynamicAware} that only declares the
options that the consumer evaluates as an
+ * expression for each message (see {@link ExpressionOptionsDynamicAware}),
and does not optimise the dynamic uri
+ * otherwise.
+ */
+public abstract class ExpressionOptionsPollDynamicAware extends
PollDynamicAwareSupport
+ implements ExpressionOptionsDynamicAware {
+
+ private final Set<String> expressionPropertyNames;
+
+ protected ExpressionOptionsPollDynamicAware(Set<String>
expressionPropertyNames) {
+ this.expressionPropertyNames = expressionPropertyNames;
+ }
+
+ @Override
+ public Set<String> expressionPropertyNames() {
+ return expressionPropertyNames;
+ }
+
+ @Override
+ public boolean isLenientProperties() {
+ return false;
+ }
+
+ @Override
+ public DynamicAwareEntry prepare(Exchange exchange, String uri, String
originalUri) throws Exception {
+ // the dynamic uri is not optimised
+ return null;
+ }
+
+ @Override
+ public String resolveStaticUri(Exchange exchange, DynamicAwareEntry entry)
throws Exception {
+ return null;
+ }
+}
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/component/ExpressionOptionsSendDynamicAware.java
b/core/camel-support/src/main/java/org/apache/camel/support/component/ExpressionOptionsSendDynamicAware.java
new file mode 100644
index 000000000000..0e20d26494da
--- /dev/null
+++
b/core/camel-support/src/main/java/org/apache/camel/support/component/ExpressionOptionsSendDynamicAware.java
@@ -0,0 +1,75 @@
+/*
+ * 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.support.component;
+
+import java.util.Set;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+
+/**
+ * A {@link org.apache.camel.spi.SendDynamicAware} that only declares the
options that the endpoint evaluates as an
+ * expression for each message (see {@link ExpressionOptionsDynamicAware}),
and does not optimise the dynamic uri
+ * otherwise.
+ */
+public abstract class ExpressionOptionsSendDynamicAware extends
SendDynamicAwareSupport
+ implements ExpressionOptionsDynamicAware {
+
+ private final Set<String> expressionPropertyNames;
+ private final String expressionPathSyntax;
+
+ protected ExpressionOptionsSendDynamicAware(Set<String>
expressionPropertyNames, String expressionPathSyntax) {
+ this.expressionPropertyNames = expressionPropertyNames;
+ this.expressionPathSyntax = expressionPathSyntax;
+ }
+
+ @Override
+ public Set<String> expressionPropertyNames() {
+ return expressionPropertyNames;
+ }
+
+ @Override
+ public String expressionPathSyntax() {
+ return expressionPathSyntax;
+ }
+
+ @Override
+ public boolean isLenientProperties() {
+ return false;
+ }
+
+ @Override
+ public DynamicAwareEntry prepare(Exchange exchange, String uri, String
originalUri) throws Exception {
+ // the dynamic uri is not optimised
+ return null;
+ }
+
+ @Override
+ public String resolveStaticUri(Exchange exchange, DynamicAwareEntry entry)
throws Exception {
+ return null;
+ }
+
+ @Override
+ public Processor createPreProcessor(Exchange exchange, DynamicAwareEntry
entry) throws Exception {
+ return null;
+ }
+
+ @Override
+ public Processor createPostProcessor(Exchange exchange, DynamicAwareEntry
entry) throws Exception {
+ return null;
+ }
+}