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/24 18:49:51 UTC

(camel) branch main updated: Exchange properties (#12898)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 83aeb9ed762 Exchange properties (#12898)
83aeb9ed762 is described below

commit 83aeb9ed762e69b080e5023dbe9b0d8899064316
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Jan 24 19:49:45 2024 +0100

    Exchange properties (#12898)
    
    * CAMEL-20359: camel-groovy - Consistent name to refer to exchangeProperties. Note this will also apply to other template based components like freemarker.
    
    * Regen
---
 .../component/freemarker/FreemarkerEndpoint.java   |  3 ++-
 .../apache/camel/component/freemarker/example.ftl  |  1 -
 .../src/main/docs/groovy-language.adoc             | 27 ++++++++++++++++++++++
 .../camel/language/groovy/GroovyLanguageTest.java  | 12 ++++++++++
 .../stringtemplate/StringTemplateEndpoint.java     |  3 ++-
 .../component/thymeleaf/ThymeleafEndpoint.java     | 22 +++++++++++-------
 .../camel/component/velocity/VelocityEndpoint.java |  3 ++-
 .../org/apache/camel/support/ExchangeHelper.java   |  1 +
 8 files changed, 60 insertions(+), 12 deletions(-)

diff --git a/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java b/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java
index 0860231d200..12c30df4491 100644
--- a/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java
+++ b/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java
@@ -36,7 +36,8 @@ import org.apache.camel.util.ObjectHelper;
  * Transform messages using FreeMarker templates.
  */
 @UriEndpoint(firstVersion = "2.10.0", scheme = "freemarker", title = "Freemarker", syntax = "freemarker:resourceUri",
-             remote = false, producerOnly = true, category = { Category.TRANSFORMATION }, headersClass = FreemarkerConstants.class)
+             remote = false, producerOnly = true, category = { Category.TRANSFORMATION },
+             headersClass = FreemarkerConstants.class)
 public class FreemarkerEndpoint extends ResourceEndpoint {
 
     @UriParam(defaultValue = "false")
diff --git a/components/camel-freemarker/src/test/resources/org/apache/camel/component/freemarker/example.ftl b/components/camel-freemarker/src/test/resources/org/apache/camel/component/freemarker/example.ftl
index 4a575467956..b7e6e721a68 100644
--- a/components/camel-freemarker/src/test/resources/org/apache/camel/component/freemarker/example.ftl
+++ b/components/camel-freemarker/src/test/resources/org/apache/camel/component/freemarker/example.ftl
@@ -12,7 +12,6 @@
     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.
 
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/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java b/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java
index 58a1e7a1d13..112228358c5 100644
--- a/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java
+++ b/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java
@@ -37,7 +37,8 @@ import org.stringtemplate.v4.STGroup;
  */
 @UriEndpoint(firstVersion = "1.2.0", scheme = "string-template", title = "String Template",
              syntax = "string-template:resourceUri", producerOnly = true,
-             remote = false, category = { Category.TRANSFORMATION, Category.SCRIPT }, headersClass = StringTemplateConstants.class)
+             remote = false, category = { Category.TRANSFORMATION, Category.SCRIPT },
+             headersClass = StringTemplateConstants.class)
 public class StringTemplateEndpoint extends ResourceEndpoint {
 
     @UriParam(defaultValue = "false")
diff --git a/components/camel-thymeleaf/src/main/java/org/apache/camel/component/thymeleaf/ThymeleafEndpoint.java b/components/camel-thymeleaf/src/main/java/org/apache/camel/component/thymeleaf/ThymeleafEndpoint.java
index 4dd4d1dae8f..8a5e5e3e7ce 100644
--- a/components/camel-thymeleaf/src/main/java/org/apache/camel/component/thymeleaf/ThymeleafEndpoint.java
+++ b/components/camel-thymeleaf/src/main/java/org/apache/camel/component/thymeleaf/ThymeleafEndpoint.java
@@ -43,25 +43,31 @@ import org.thymeleaf.web.servlet.JakartaServletWebApplication;
  * Transform messages using a Thymeleaf template.
  */
 @UriEndpoint(firstVersion = "4.1.0", scheme = "thymeleaf", title = "Thymeleaf", syntax = "thymeleaf:resourceUri",
-             remote = false, producerOnly = true, category = { Category.TRANSFORMATION }, headersClass = ThymeleafConstants.class)
+             remote = false, producerOnly = true, category = { Category.TRANSFORMATION },
+             headersClass = ThymeleafConstants.class)
 public class ThymeleafEndpoint extends ResourceEndpoint {
 
     private TemplateEngine templateEngine;
     private String template;
     private JakartaServletWebApplication jakartaServletWebApplication;
 
-    @UriParam(label = "advanced", defaultValue = "CLASS_LOADER", description = "The type of resolver to be used by the template engine.",
+    @UriParam(label = "advanced", defaultValue = "CLASS_LOADER",
+              description = "The type of resolver to be used by the template engine.",
               javaType = "org.apache.camel.component.thymeleaf.ThymeleafResolverType")
     private ThymeleafResolverType resolver = ThymeleafResolverType.CLASS_LOADER;
-    @UriParam(description = "The template mode to be applied to templates.", defaultValue = "HTML", enums = "HTML,XML,TEXT,JAVASCRIPT,CSS,RAW")
+    @UriParam(description = "The template mode to be applied to templates.", defaultValue = "HTML",
+              enums = "HTML,XML,TEXT,JAVASCRIPT,CSS,RAW")
     private String templateMode;
-    @UriParam(label = "advanced", description = "An optional prefix added to template names to convert them into resource names.")
+    @UriParam(label = "advanced",
+              description = "An optional prefix added to template names to convert them into resource names.")
     private String prefix;
-    @UriParam(label = "advanced", description = "An optional suffix added to template names to convert them into resource names.")
+    @UriParam(label = "advanced",
+              description = "An optional suffix added to template names to convert them into resource names.")
     private String suffix;
     @UriParam(label = "advanced", description = "The character encoding to be used for reading template resources.")
     private String encoding;
-    @UriParam(label = "advanced", description = "The order in which this template will be resolved as part of the resolver chain.")
+    @UriParam(label = "advanced",
+              description = "The order in which this template will be resolved as part of the resolver chain.")
     private Integer order;
     @UriParam(description = "Whether a template resources will be checked for existence before being returned.")
     private Boolean checkExistence;
@@ -192,8 +198,8 @@ public class ThymeleafEndpoint extends ResourceEndpoint {
     }
 
     /**
-     * Sets whether template resources will be checked for existence before being returned or not.
-     * Default value is {@code FALSE}.
+     * Sets whether template resources will be checked for existence before being returned or not. Default value is
+     * {@code FALSE}.
      *
      * @param checkExistence {@code true} if resource existence should be checked, {@code false} if not
      */
diff --git a/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java b/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java
index 600e21548f7..ad429b18c04 100644
--- a/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java
+++ b/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java
@@ -45,7 +45,8 @@ import org.slf4j.LoggerFactory;
  * Transform messages using a Velocity template.
  */
 @UriEndpoint(firstVersion = "1.2.0", scheme = "velocity", title = "Velocity", syntax = "velocity:resourceUri",
-             remote = false, producerOnly = true, category = { Category.TRANSFORMATION }, headersClass = VelocityConstants.class)
+             remote = false, producerOnly = true, category = { Category.TRANSFORMATION },
+             headersClass = VelocityConstants.class)
 public class VelocityEndpoint extends ResourceEndpoint {
 
     private VelocityEngine velocityEngine;
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