This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch var-headers in repository https://gitbox.apache.org/repos/asf/camel.git
commit c8be513a82afb7bb873c7d13d2864c3ed9cce765 Author: Claus Ibsen <[email protected]> AuthorDate: Wed Jan 31 09:21:12 2024 +0100 CAMEL-19749: variables - Should also copy message headers into variable when using EIP variables --- .../main/java/org/apache/camel/reifier/RouteReifier.java | 15 ++++++--------- .../java/org/apache/camel/processor/FromVariableTest.java | 6 +++--- docs/user-manual/modules/ROOT/pages/variables.adoc | 9 ++++++--- .../org/apache/camel/dsl/yaml/FromVariableTest.groovy | 4 ++-- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/RouteReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/RouteReifier.java index 3bdd8679223..31a92c2582e 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/RouteReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/RouteReifier.java @@ -45,7 +45,6 @@ import org.apache.camel.reifier.rest.RestBindingReifier; import org.apache.camel.spi.CamelInternalProcessorAdvice; import org.apache.camel.spi.Contract; import org.apache.camel.spi.ErrorHandlerAware; -import org.apache.camel.spi.HeadersMapFactory; import org.apache.camel.spi.InternalProcessor; import org.apache.camel.spi.LifecycleStrategy; import org.apache.camel.spi.ManagementInterceptStrategy; @@ -332,7 +331,7 @@ public class RouteReifier extends ProcessorReifier<RouteDefinition> { // wrap with variable if (variable != null) { - internal.addAdvice(new VariableAdvice(camelContext, variable)); + internal.addAdvice(new VariableAdvice(variable)); } // and create the route that wraps all of this @@ -423,23 +422,21 @@ public class RouteReifier extends ProcessorReifier<RouteDefinition> { } /** - * Advice for copying the message body into a variable + * Advice for moving message body into a variable when using variableReceive mode */ private static class VariableAdvice implements CamelInternalProcessorAdvice<Object> { - private final HeadersMapFactory factory; private final String name; - public VariableAdvice(CamelContext camelContext, String name) { - this.factory = camelContext.getCamelContextExtension().getHeadersMapFactory(); + public VariableAdvice(String name) { this.name = name; } @Override public Object before(Exchange exchange) throws Exception { - Object body = exchange.getMessage().getBody(); - ExchangeHelper.setVariable(exchange, name, body); - ExchangeHelper.setVariable(exchange, name + ".headers", factory.newMap(exchange.getMessage().getHeaders())); + // move body to variable + ExchangeHelper.setVariableFromMessageBodyAndHeaders(exchange, name, exchange.getMessage()); + exchange.getMessage().setBody(null); return null; } diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/FromVariableTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/FromVariableTest.java index e1227bb9bce..1d06aeabecb 100644 --- a/core/camel-core/src/test/java/org/apache/camel/processor/FromVariableTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/processor/FromVariableTest.java @@ -27,7 +27,7 @@ public class FromVariableTest extends ContextTestSupport { @Test public void testOriginalBody() throws Exception { - getMockEndpoint("mock:foo").expectedBodiesReceived("Bye World"); + getMockEndpoint("mock:foo").expectedBodiesReceived("Bye "); getMockEndpoint("mock:result").expectedBodiesReceived("World"); template.sendBody("direct:start", "World"); @@ -37,10 +37,10 @@ public class FromVariableTest extends ContextTestSupport { @Test public void testOriginalHeaders() throws Exception { - getMockEndpoint("mock:foo").expectedBodiesReceived("Bye World"); + getMockEndpoint("mock:foo").expectedBodiesReceived("Bye "); getMockEndpoint("mock:foo").expectedHeaderReceived("foo", 456); getMockEndpoint("mock:foo").whenAnyExchangeReceived(e -> { - Map m = e.getVariable("myKey.headers", Map.class); + Map m = e.getVariable("header:myKey", Map.class); Assertions.assertNotNull(m); Assertions.assertEquals(1, m.size()); Assertions.assertEquals(123, m.get("foo")); diff --git a/docs/user-manual/modules/ROOT/pages/variables.adoc b/docs/user-manual/modules/ROOT/pages/variables.adoc index e392cd26fcc..23b7d92ad00 100644 --- a/docs/user-manual/modules/ROOT/pages/variables.adoc +++ b/docs/user-manual/modules/ROOT/pages/variables.adoc @@ -336,10 +336,10 @@ myVar.header.level=gold IMPORTANT: Notice the headers are stored with the syntax `variable.header.key`. In the example above the variable name is `myVar`, and the header key is `level`, which gives: `myVar.header.level`. -=== Using variable to store a copy of the incoming message body +=== Using variable to store incoming message body -You can configure the `from` to store a copy of the message body into a variable. This makes it easy to have quick access -to the original incoming message body via the variable. +You can configure the `from` to store the message body into a variable, instead of the `Message`. This makes it easy to have quick access +to the original incoming message body via the variable. Notice that the body on the `Message` will be `null`. The following example from a unit test shows how to do this. Notice how Java DSL uses `fromV` to make it possible to specify the name of the variable. In XML and YAML DSL you specify this using the `variableReceive` parameter. @@ -389,6 +389,9 @@ from: ---- ==== +NOTE: In the examples above the transform `Bye $\{body}` will result as `Bye ` because the `Message` has no message body, as the incoming +message body is stored in the variable `myKey` instead. + === Using variables when sending and receiving messages to an endpoint You can configure the `to` to use variables for any of the following (or both) when sending and receiving: diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/FromVariableTest.groovy b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/FromVariableTest.groovy index 5984b103e00..5112cb1a9ca 100644 --- a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/FromVariableTest.groovy +++ b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/FromVariableTest.groovy @@ -44,9 +44,9 @@ class FromVariableTest extends YamlTestSupport { ''' withMock('mock:foo') { - expectedBodiesReceived 'Bye World' + expectedBodiesReceived 'Bye ' whenAnyExchangeReceived { e -> { - Map m = e.getVariable("myKey.headers", Map.class) + Map m = e.getVariable("header:myKey", Map.class) Assertions.assertNotNull(m) Assertions.assertEquals(1, m.size()) Assertions.assertEquals(123, m.get("foo"))
