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 eff41a2 CAMEL-16861: Polished SpEL language eff41a2 is described below commit eff41a237e68d572c003c0d2a1d5c4933c1a0f67 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Sep 14 10:30:39 2021 +0200 CAMEL-16861: Polished SpEL language --- .../camel-mvel/src/main/docs/mvel-language.adoc | 2 + .../camel-spring/src/main/docs/spel-language.adoc | 133 ++++++++++----------- .../org/apache/camel/language/spel/RootObject.java | 13 ++ 3 files changed, 75 insertions(+), 73 deletions(-) diff --git a/components/camel-mvel/src/main/docs/mvel-language.adoc b/components/camel-mvel/src/main/docs/mvel-language.adoc index e315ff0..ff5b577 100644 --- a/components/camel-mvel/src/main/docs/mvel-language.adoc +++ b/components/camel-mvel/src/main/docs/mvel-language.adoc @@ -51,6 +51,8 @@ The MVEL language supports 1 options, which are listed below. == Variables +The following Camel related variables are made available: + [width="100%",cols="10%,10%,80%",options="header",] |======================================================================= |Variable |Type |Description diff --git a/components/camel-spring/src/main/docs/spel-language.adoc b/components/camel-spring/src/main/docs/spel-language.adoc index b147142..b64ddff 100644 --- a/components/camel-spring/src/main/docs/spel-language.adoc +++ b/components/camel-spring/src/main/docs/spel-language.adoc @@ -16,41 +16,10 @@ to be used as an Expression or Predicate in the DSL or XML Configuration. [NOTE] ==== It is recommended to use SpEL in Spring runtimes. However, you can -use SpEL in other runtimes (there may be functionality SpEL cannot do when not running in a Spring runtime) +use SpEL in other runtimes (there are some functionality which SpEL can only do in a Spring runtime) ==== -== Variables - -The following variables are available in expressions and predicates written in SpEL: - -[width="100%",cols="10%,10%,80%",options="header",] -|=== -|Variable |Type |Description - -|this |Exchange |the Exchange is the root object - -|exchange |Exchange |the Exchange object - -|exception |Throwable |the Exchange exception (if any) - -|exchangeId |String |the exchange id - -|fault |Message |the Fault message (if any) - -|body |Object | The IN message body. - -|request |Message |the exchange.in message - -|response |Message |the exchange.out message (if any) - -|properties |Map |the exchange properties - -|property(name) |Object |the property by the given name - -|property(name, type) |Type |the property by the given name as the given type -|=== - -== Options +== SpEL Options // language options: START The SpEL language supports 1 options, which are listed below. @@ -64,7 +33,53 @@ The SpEL language supports 1 options, which are listed below. |=== // language options: END -== Samples +== Variables + +The following Camel related variables are made available: + +[width="100%",cols="10%,10%,80%",options="header",] +|======================================================================= +|Variable |Type |Description +|*this* |Exchange |the Exchange is the root object +|context |CamelContext |the CamelContext +|exchange |Exchange |the Exchange +|exchangeId |String |the exchange id +|exception |Throwable |the Exchange exception (if any) +|request |Message |the message +|message |Message |the message +|headers |Map |the message headers +|header(name) |Object |the message header by the given name +|header(name, type) |Type |the message header by the given name as the given type +|properties |Map |the exchange properties +|property(name) |Object |the exchange property by the given name +|property(name, type) |Type |the exchange property by the given name as the given type +|======================================================================= + +== Example + +You can use SpEL as an expression for xref:{eip-vc}:eips:recipientList-eip.adoc[Recipient +List] or as a predicate inside a xref:{eip-vc}:eips:filter-eip.adoc[Message +Filter]: + +[source,xml] +---- +<route> + <from uri="direct:foo"/> + <filter> + <spel>#{request.headers.foo == 'bar'}</spel> + <to uri="direct:bar"/> + </filter> +</route> +---- + +And the equivalent in Java DSL: + +[source,java] +---- +from("direct:foo") + .filter().spel("#{request.headers.foo == 'bar'}") + .to("direct:bar"); +---- === Expression templating @@ -82,9 +97,9 @@ from("direct:example") .to("mock:result"); ---- -In the route above, notice spel is a static method which we need to +In the route above, notice `spel` is a static method which we need to import from `org.apache.camel.language.spel.SpelExpression.spel`, as we -use spel as an Expression passed in as a parameter +use `spel` as an Expression passed in as a parameter to the `setBody` method. Though if we use the fluent API we can do this instead: @@ -96,10 +111,9 @@ from("direct:example") ---- Notice we now use the `spel` method from the `setBody()` method. And -this does not require us to static import the spel method from -`org.apache.camel.language.spel.SpelExpression.spel`. +this does not require us to static import the `spel` method. -And sent a message with the string "World" in the body, and a header +Then we send a message with the string "World" in the body, and a header "dayOrNight" with value "day": [source,java] @@ -112,50 +126,23 @@ day"_ === Bean integration -You can reference beans defined in the Registry -(most likely an `ApplicationContext`) in your SpEL expressions. For -example if you have a bean named "foo" in your `ApplicationContext` you -can invoke the "bar" method on this bean like this: +You can reference beans defined in the xref:manual::registry.adoc[Registry] +in your SpEL expressions. For example if you have a bean named "foo" +registered in the Spring `ApplicationContext`. You +can then invoke the "bar" method on this bean like this: [source,text] ---- #{@foo.bar == 'xyz'} ---- -=== SpEL in enterprise integration patterns - -You can use SpEL as an expression for xref:{eip-vc}:eips:recipientList-eip.adoc[Recipient -List] or as a predicate inside a xref:{eip-vc}:eips:filter-eip.adoc[Message -Filter]: - -[source,xml] ----- -<route> - <from uri="direct:foo"/> - <filter> - <spel>#{request.headers.foo == 'bar'}</spel> - <to uri="direct:bar"/> - </filter> -</route> ----- - -And the equivalent in Java DSL: - -[source,java] ----- -from("direct:foo") - .filter().spel("#{request.headers.foo == 'bar'}") - .to("direct:bar"); ----- == Loading script from external resource -*Since Camel 2.11* - You can externalize the script and have Camel load it from a resource -such as `"classpath:"`, `"file:"`, or `"http:"`. + - This is done using the following syntax: `"resource:scheme:location"`, -eg to refer to a file on the classpath you can do: +such as `"classpath:"`, `"file:"`, or `"http:"`. +This is done using the following syntax: `"resource:scheme:location"`, +e.g. to refer to a file on the classpath you can do: [source,java] ---- diff --git a/components/camel-spring/src/main/java/org/apache/camel/language/spel/RootObject.java b/components/camel-spring/src/main/java/org/apache/camel/language/spel/RootObject.java index 6d971a1..8f6c4ce 100644 --- a/components/camel-spring/src/main/java/org/apache/camel/language/spel/RootObject.java +++ b/components/camel-spring/src/main/java/org/apache/camel/language/spel/RootObject.java @@ -73,4 +73,17 @@ public final class RootObject { public <T> T getProperty(String name, Class<T> type) { return exchange.getProperty(name, type); } + + public Map<String, Object> getHeaders() { + return exchange.getMessage().getHeaders(); + } + + public Object getHeader(String name) { + return exchange.getMessage().getHeader(name); + } + + public <T> T getHeader(String name, Class<T> type) { + return exchange.getMessage().getHeader(name, type); + } + }