Repository: camel Updated Branches: refs/heads/master 4c4f24c0f -> cdd5b735f
Added spel-language to Gitbook Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/cdd5b735 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/cdd5b735 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/cdd5b735 Branch: refs/heads/master Commit: cdd5b735f4fb5ce73c77e14268ef44edbdb875a1 Parents: 4c4f24c Author: Andrea Cosentino <[email protected]> Authored: Tue Aug 23 14:27:07 2016 +0200 Committer: Andrea Cosentino <[email protected]> Committed: Tue Aug 23 14:29:59 2016 +0200 ---------------------------------------------------------------------- .../src/main/docs/spel-language.adoc | 182 +++++++++++++++++++ 1 file changed, 182 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/cdd5b735/components/camel-spring/src/main/docs/spel-language.adoc ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/main/docs/spel-language.adoc b/components/camel-spring/src/main/docs/spel-language.adoc new file mode 100644 index 0000000..ac64f46 --- /dev/null +++ b/components/camel-spring/src/main/docs/spel-language.adoc @@ -0,0 +1,182 @@ +[[SpEL-SpringExpressionLanguageSpEL]] +Spring Expression Language (SpEL) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +*Available as of Camel 2.7* + +Camel allows +http://static.springsource.org/spring/docs/current/spring-framework-reference/htmlsingle/spring-framework-reference.html#expressions[SpEL] +to be used as an link:expression.html[Expression] or +link:predicate.html[Predicate] in the link:dsl.html[DSL] or +link:xml-configuration.html[Xml Configuration]. + +[[SpEL-Variables]] +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 |Camel 2.11: 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 +|======================================================================= + +[[SpEL-Options]] +Options +^^^^^^^ + +// language options: START +The SpEL language supports 1 options which are listed below. + + + +{% raw %} +[width="100%",cols="2,1m,1m,6",options="header"] +|======================================================================= +| Name | Default | Java Type | Description +| trim | true | Boolean | Whether to trim the value to remove leading and trailing whitespaces and line breaks +|======================================================================= +{% endraw %} +// language options: END + +[[SpEL-Samples]] +Samples +^^^^^^^ + +[[SpEL-Expressiontemplating]] +Expression templating ++++++++++++++++++++++ + +SpEL expressions need to be surrounded by `#{` `}` delimiters since +expression templating is enabled. This allows you to combine SpEL +expressions with regular text and use this as extremely lightweight +template language. + +For example if you construct the following route: + +[source,java] +----------------------------------------------------------------------------------------------------------------------------------- +from("direct:example").setBody(spel("Hello #{request.body}! What a beautiful #{request.headers['dayOrNight']}")).to("mock:result"); +----------------------------------------------------------------------------------------------------------------------------------- + +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 link:expression.html[Expression] passed in as a parameter +to the `setBody` method. Though if we use the fluent API we can do this +instead: + +[source,java] +------------------------------------------------------------------------------------------------------------------------------------ +from("direct:example").setBody().spel("Hello #{request.body}! What a beautiful #{request.headers['dayOrNight']}").to("mock:result"); +------------------------------------------------------------------------------------------------------------------------------------ + +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`. + +And sent a message with the string "World" in the body, and a header +"dayOrNight" with value "day": + +[source,java] +--------------------------------------------------------------------------- +template.sendBodyAndHeader("direct:example", "World", "dayOrNight", "day"); +--------------------------------------------------------------------------- + +The output on `mock:result` will be _"Hello World! What a beautiful +day"_ + +[[SpEL-Beanintegration]] +Bean integration +++++++++++++++++ + +You can reference beans defined in the link:registry.html[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: + +[source,java] +-------------------- +#{@foo.bar == 'xyz'} +-------------------- + +[[SpEL-SpELinenterpriseintegrationpatterns]] +SpEL in enterprise integration patterns ++++++++++++++++++++++++++++++++++++++++ + +You can use SpEL as an expression for link:recipient-list.html[Recipient +List] or as a predicate inside a link:message-filter.html[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"); +------------------------------------------------------------------------------------------- + +[[SpEL-Loadingscriptfromexternalresource]] +Loading script from external resource +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +*Available as of 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: + +[source,java] +------------------------------------------------------------ +.setHeader("myHeader").spel("resource:classpath:myspel.txt") +------------------------------------------------------------ + +[[SpEL-Dependencies]] +Dependencies +^^^^^^^^^^^^ + +You need Spring 3.0 or higher to use Spring Expression Language. If you +use Maven you could just add the following to your `pom.xml`: + +[source,xml] +---------------------------------------------------------- +<dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-spring</artifactId> + <version>xxx</version> + <!-- use the same version as your Camel core version --> +</dependency> +----------------------------------------------------------
