You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2024/01/02 14:50:19 UTC
(camel) 02/04: CAMEL-20289: camel-core - FluentProducerTemplate - Add withVariable and withProperty
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch fluent
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 42d3bca108bc0706b935bf88fe74bc2a9b49ff8b
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Jan 2 15:20:15 2024 +0100
CAMEL-20289: camel-core - FluentProducerTemplate - Add withVariable and withProperty
---
.../impl/engine/DefaultFluentProducerTemplate.java | 47 ++++++++++++----------
.../camel/builder/FluentProducerTemplateTest.java | 27 +++++++++++++
.../org/apache/camel/support/ExchangeHelper.java | 11 ++---
3 files changed, 58 insertions(+), 27 deletions(-)
diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultFluentProducerTemplate.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultFluentProducerTemplate.java
index 851bf2ce845..b1063041a6e 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultFluentProducerTemplate.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultFluentProducerTemplate.java
@@ -214,7 +214,8 @@ public class DefaultFluentProducerTemplate extends ServiceSupport implements Flu
DefaultFluentProducerTemplate clone = checkCloned();
if (clone.processorSupplier != null) {
- throw new IllegalArgumentException("Cannot use both withExchangeProperties and withProcessor with FluentProducerTemplate");
+ throw new IllegalArgumentException(
+ "Cannot use both withExchangeProperties and withProcessor with FluentProducerTemplate");
}
Map<String, Object> map = clone.exchangeProperties;
@@ -231,7 +232,8 @@ public class DefaultFluentProducerTemplate extends ServiceSupport implements Flu
DefaultFluentProducerTemplate clone = checkCloned();
if (clone.processorSupplier != null) {
- throw new IllegalArgumentException("Cannot use both withExchangeProperty and withProcessor with FluentProducerTemplate");
+ throw new IllegalArgumentException(
+ "Cannot use both withExchangeProperty and withProcessor with FluentProducerTemplate");
}
Map<String, Object> map = clone.exchangeProperties;
@@ -457,24 +459,23 @@ public class DefaultFluentProducerTemplate extends ServiceSupport implements Flu
// Determine the target endpoint
final Endpoint target = clone.target();
- Future<T> result =
- clone.template().asyncSend(target, exchange -> {
- // Make a copy of the headers and body so that async processing won't
- // be invalidated by subsequent reuse of the template
- Object bodyCopy = clone.body;
-
- exchange.setPattern(ExchangePattern.InOut);
- exchange.getMessage().setBody(bodyCopy);
- if (clone.headers != null) {
- exchange.getMessage().setHeaders(new HashMap<>(clone.headers));
- }
- if (clone.exchangeProperties != null) {
- exchange.getProperties().putAll(clone.exchangeProperties);
- }
- if (clone.variables != null) {
- clone.variables.forEach((k, v) -> ExchangeHelper.setVariable(exchange, k, v));
- }
- }).thenApply(answer -> answer.getContext().getTypeConverter().convertTo(type, answer));
+ Future<T> result = clone.template().asyncSend(target, exchange -> {
+ // Make a copy of the headers and body so that async processing won't
+ // be invalidated by subsequent reuse of the template
+ Object bodyCopy = clone.body;
+
+ exchange.setPattern(ExchangePattern.InOut);
+ exchange.getMessage().setBody(bodyCopy);
+ if (clone.headers != null) {
+ exchange.getMessage().setHeaders(new HashMap<>(clone.headers));
+ }
+ if (clone.exchangeProperties != null) {
+ exchange.getProperties().putAll(clone.exchangeProperties);
+ }
+ if (clone.variables != null) {
+ clone.variables.forEach((k, v) -> ExchangeHelper.setVariable(exchange, k, v));
+ }
+ }).thenApply(answer -> answer.getMessage().getBody(type));
// reset cloned flag so when we use it again it has to set values again
cloned = false;
@@ -624,8 +625,10 @@ public class DefaultFluentProducerTemplate extends ServiceSupport implements Flu
private Processor defaultAsyncProcessor() {
final Map<String, Object> headersCopy = ObjectHelper.isNotEmpty(this.headers) ? new HashMap<>(this.headers) : null;
- final Map<String, Object> propertiesCopy = ObjectHelper.isNotEmpty(this.exchangeProperties) ? new HashMap<>(this.exchangeProperties) : null;
- final Map<String, Object> variablesCopy = ObjectHelper.isNotEmpty(this.variables) ? new HashMap<>(this.variables) : null;
+ final Map<String, Object> propertiesCopy
+ = ObjectHelper.isNotEmpty(this.exchangeProperties) ? new HashMap<>(this.exchangeProperties) : null;
+ final Map<String, Object> variablesCopy
+ = ObjectHelper.isNotEmpty(this.variables) ? new HashMap<>(this.variables) : null;
final Object bodyCopy = this.body;
return exchange -> {
if (headersCopy != null) {
diff --git a/core/camel-core/src/test/java/org/apache/camel/builder/FluentProducerTemplateTest.java b/core/camel-core/src/test/java/org/apache/camel/builder/FluentProducerTemplateTest.java
index 7ef05345095..bbb9636c951 100644
--- a/core/camel-core/src/test/java/org/apache/camel/builder/FluentProducerTemplateTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/builder/FluentProducerTemplateTest.java
@@ -333,6 +333,30 @@ public class FluentProducerTemplateTest extends ContextTestSupport {
.to(context.getEndpoint("direct:inout")).request(Integer.class));
}
+ @Test
+ public void testWithVariable() {
+ FluentProducerTemplate template = DefaultFluentProducerTemplate.on(context);
+
+ assertEquals("Hello World", template.withVariable("foo", "World").withBody("Hello")
+ .to("direct:var").request(String.class));
+
+ assertEquals("Hello Moon", template.withVariable("foo", "Moon").withVariable("global:planet", "Mars").withBody("Hello")
+ .to("direct:var").request(String.class));
+ assertEquals("Mars", context.getVariable("planet"));
+ }
+
+ @Test
+ public void testWithExchangeProperty() {
+ FluentProducerTemplate template = DefaultFluentProducerTemplate.on(context);
+
+ assertEquals("Hello World", template.withExchangeProperty("foo", "World").withBody("Hello")
+ .to("direct:ep").request(String.class));
+
+ assertEquals("Hello Moon",
+ template.withExchangeProperty("foo", "Moon").withExchangeProperty("planet", "Mars").withBody("Hello")
+ .to("direct:ep").request(String.class));
+ }
+
@Test
public void testAsyncRequest() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:async");
@@ -473,6 +497,9 @@ public class FluentProducerTemplateTest extends ContextTestSupport {
from("direct:red").setBody().simple("Red ${body}");
+ from("direct:var").transform().simple("${body} ${variable.foo}");
+
+ from("direct:ep").transform().simple("${body} ${exchangeProperty.foo}");
}
};
}
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java
index 004d0dacb99..5ea365e297d 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java
@@ -1076,15 +1076,16 @@ public final class ExchangeHelper {
/**
* Sets the variable
*
- * @param exchange the exchange
- * @param name the variable name. Can be prefixed with repo-id:name to lookup the variable from a specific
- * repository. If no repo-id is provided, then the variable is set on the exchange
- * @param value the value of the variable
+ * @param exchange the exchange
+ * @param name the variable name. Can be prefixed with repo-id:name to lookup the variable from a specific
+ * repository. If no repo-id is provided, then the variable is set on the exchange
+ * @param value the value of the variable
*/
public static void setVariable(Exchange exchange, String name, Object value) {
String id = StringHelper.before(name, ":");
if (id != null) {
- VariableRepositoryFactory factory = exchange.getContext().getCamelContextExtension().getContextPlugin(VariableRepositoryFactory.class);
+ VariableRepositoryFactory factory
+ = exchange.getContext().getCamelContextExtension().getContextPlugin(VariableRepositoryFactory.class);
VariableRepository repo = factory.getVariableRepository(id);
if (repo != null) {
name = StringHelper.after(name, ":");