This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch exchangeProperties in repository https://gitbox.apache.org/repos/asf/camel.git
commit 67a38be2f0ecbb041d41860da1a4ff15a019d48c Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Wed Jan 24 19:28:27 2024 +0100 CAMEL-20359: camel-groovy - Consistent name to refer to exchangeProperties. Note this will also apply to other template based components like freemarker. --- .../src/main/docs/groovy-language.adoc | 27 ++++++++++++++++++++++ .../camel/language/groovy/GroovyLanguageTest.java | 12 ++++++++++ .../org/apache/camel/support/ExchangeHelper.java | 1 + 3 files changed, 40 insertions(+) diff --git a/components/camel-groovy/src/main/docs/groovy-language.adoc b/components/camel-groovy/src/main/docs/groovy-language.adoc index 7a5765eecd9..82250fd595d 100644 --- a/components/camel-groovy/src/main/docs/groovy-language.adoc +++ b/components/camel-groovy/src/main/docs/groovy-language.adoc @@ -54,6 +54,33 @@ And in XML DSL: </route> ---- +== Groovy Context + +Camel will provide exchange information in the Groovy context (just +a `Map`). The `Exchange` is transferred as: + +[width="100%",cols="50%,50%",options="header",] +|======================================================================= +|key |value + +|`exchange` |The `Exchange` itself. + +|`exchangeProperties` |The `Exchange` properties. + +|`variables` |The variables + +|`headers` |The headers of the In message. + +|`camelContext` |The Camel Context. + +|`request` |The In message. + +|`body` |The In message body. + +|`response` |The Out message (only for InOut message exchange pattern). +|======================================================================= + + == How to get the result from multiple statements script As the Groovy script engine evaluate method just return a `Null` if it runs a diff --git a/components/camel-groovy/src/test/java/org/apache/camel/language/groovy/GroovyLanguageTest.java b/components/camel-groovy/src/test/java/org/apache/camel/language/groovy/GroovyLanguageTest.java index 4e5aadfc201..dbf9968df54 100644 --- a/components/camel-groovy/src/test/java/org/apache/camel/language/groovy/GroovyLanguageTest.java +++ b/components/camel-groovy/src/test/java/org/apache/camel/language/groovy/GroovyLanguageTest.java @@ -28,6 +28,18 @@ public class GroovyLanguageTest extends LanguageTestSupport { assertExpression("headers.foo", "abc"); } + @Test + public void testGroovyExchangeProperty() { + exchange.setProperty("myProp1", "myValue"); + exchange.setProperty("myProp2", 123); + + assertExpression("exchange.properties.myProp1", "myValue"); + assertExpression("exchange.properties.myProp2", 123); + + assertExpression("exchangeProperties.myProp1", "myValue"); + assertExpression("exchangeProperties.myProp2", 123); + } + @Override protected String getLanguageName() { return "groovy"; 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 f1bd96981c1..d55f4e1ce57 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 @@ -481,6 +481,7 @@ public final class ExchangeHelper { map.put("in", in); map.put("request", in); map.put("exchange", exchange); + map.put("exchangeProperties", exchange.getAllProperties()); if (isOutCapable(exchange)) { // if we are out capable then set out and response as well // however only grab OUT if it exists, otherwise reuse IN