Simple Expression Language
The Simple Expression Language is a really simple language you can use. Its primarily intended for being a really small and simple language for testing without requiring any new dependencies or knowledge of XPath; so its ideal for testing in camel-core. However for real world use cases you are generally recommended to choose a more expressive and powerful language such as:
- Bean Language
- EL
- OGNL
- one of the supported Scripting Languages
The simple language uses ${body
} placeholders for complex expressions where the expression contains constant literals. The ${ } placeholders can be omitted if the expression is only the token itself.
To get the body of the in message: "body"
, or "in.body"
or "${body}"
.
A complex expression must use ${ } placeholders, such as: "Hello ${in.header.name} how are you?"
.
You can have multiple tokens in the same expression: "Hello ${in.header.name} this is ${in.header.me} speaking"
.
However you can not nest tokens (i.e. having another ${ } placeholder in an existing, is not allowed).
File language is now merged with Simple language
From Camel 2.2 onwards, the File Language is now merged with Simple language which means you can use all the file syntax directly within the simple language.
Variables
Variable |
Type |
Description |
||
---|---|---|---|---|
exchangeId |
String |
Camel 2.3: the exchange id |
||
id |
String |
the input message id |
||
body |
Object |
the input body |
||
in.body |
Object |
the input body |
||
body.OGNL |
Object |
Camel 2.3: the input body invoked using a Camel OGNL expression. |
||
in.body.OGNL |
Object |
Camel 2.3: the input body invoked using a Camel OGNL expression. |
||
bodyAs(type) |
Type |
Camel 2.3: Converts the body to the given type determined by its classname |
||
out.body |
Object |
the output body |
||
header.foo |
Object |
refer to the input foo header |
||
headers.foo |
Object |
refer to the input foo header |
||
in.header.foo |
Object |
refer to the input foo header |
||
in.headers.foo |
Object |
refer to the input foo header |
||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7ab1dd2b-5e0f-4e88-942d-89aaab38b5f7"><ac:plain-text-body><![CDATA[ |
header.foo[bar] |
Object |
Camel 2.3: regard input foo header as a map and perform lookup on the map with bar as key |
]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7078b004-c5cb-4312-8ca5-2ccc5c7e1495"><ac:plain-text-body><![CDATA[ |
in.header.foo[bar] |
Object |
Camel 2.3: regard input foo header as a map and perform lookup on the map with bar as key |
]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="16c84c61-93ee-4a1b-81c0-31f2c60b001a"><ac:plain-text-body><![CDATA[ |
in.headers.foo[bar] |
Object |
Camel 2.3: regard input foo header as a map and perform lookup on the map with bar as key |
]]></ac:plain-text-body></ac:structured-macro> |
header.foo.OGNL |
Object |
Camel 2.3: refer to the input foo header and invoke its value using a Camel OGNL expression. |
||
in.header.foo.OGNL |
Object |
Camel 2.3: refer to the input foo header and invoke its value using a Camel OGNL expression. |
||
in.headers.foo.OGNL |
Object |
Camel 2.3: refer to the input foo header and invoke its value using a Camel OGNL expression. |
||
out.header.foo |
Object |
refer to the out header foo |
||
out.headers.foo |
Object |
refer to the out header foo |
||
property.foo |
Object |
refer to the foo property on the exchange |
||
sys.foo |
String |
refer to the system property |
||
sysenv.foo |
String |
Camel 2.3: refer to the system environment |
||
exception |
Object |
Camel 2.4: Refer to the exception object on the exchange, is null if no exception set on exchange. Will fallback and grab caught exceptions ( |
||
exception.OGNL |
Object |
Camel 2.4: Refer to the exchange exception invoked using a Camel OGNLE expression object |
||
exception.message |
String |
Camel 2.0. Refer to the exception.message on the exchange, is null if no exception set on exchange. Will fallback and grab caught exceptions ( |
||
date:command:pattern |
String |
Camel 1.5. Date formatting using the |
||
bean:bean expression |
Object |
Camel 1.5. Invoking a bean expression using the Bean language. Specifying a method name you must use dot as separator. In Camel 2.0 we also support the ?method=methodname syntax that is used by the Bean component. |
||
properties:locations:key |
String |
Camel 2.3: Lookup a property with the given key. The |
||
threadName |
String |
Camel 2.3: Returns the name of the current thread. Can be used for logging purpose. |
OGNL support
Available as of Camel 2.3
The Simple and Bean language now supports a Camel OGNL notation for invoking beans in a chain like fashion.
Suppose the Message IN body contains a POJO which has a getAddress()
method.
Then you can use Camel OGNL notation to access the address object:
simple("${body.address}") simple("${body.address.street}") simple("${body.address.zip}")
Camel understands the shorthand names for getters, but you can invoke any method or use the real name such as:
simple("${body.address}") simple("${body.getAddress.getStreet}") simple("${body.address.getZip}") simple("${body.doSomething}")
You can also use the null safe operator (?.
) to avoid NPE if for example the body does NOT have an address
simple("${body?.address?.street}")
Its also possible to index in Map
or List
types, so you can do:
simple("${body[foo].name}")
To assume the body is Map
based and lookup the value with foo
as key, and invoke the getName
method on that value.
Suppose there was no value with the key foo
then you can use the elvis operator to avoid the NPE as shown:
simple("${body[foo]?.name}")
You can also access List
types, for example to get lines from the address you can do:
simple("${body.address.lines[0]}") simple("${body.address.lines[1]}") simple("${body.address.lines[2]}")
There is a special last
keyword which can be used to get the last value from a list.
simple("${body.address.lines[last]}")
And to get the 2nd last you can subtract a number, so we can use last-1
to indicate this:
simple("${body.address.lines[last-1]}")
And the 3rd last is of course:
simple("${body.address.lines[last-2]}")
And yes you can combine this with the operator support as shown below:
simple("${body.address.zip} > 1000")
Operator support
Available as of Camel 2.0
We added a basic set of operators supported in the simple language in Camel 2.0. The parser is limited to only support a single operator.
To enable it the left value must be enclosed in ${ }. The syntax is:
${leftValue} OP rightValue
Where the rightValue
can be a String literal enclosed in ' '
, null
, a constant value or another expression enclosed in ${ }.
Camel will automatically type convert the rightValue type to the leftValue type, so its able to eg. convert a string into a numeric so you can use > comparison for numeric values.
The following operators is supported:
Operator |
Description |
---|---|
== |
equals |
> |
greater than |
>= |
greater than or equals |
< |
less than |
<= |
less than or equals |
!= |
not equals |
contains |
For testing if contains in a string based value |
not contains |
For testing if not contains in a string based value |
regex |
For matching against a given regular expression pattern defined as a String value |
not regex |
For not matching against a given regular expression pattern defined as a String value |
in |
For matching if in a set of values, each element must be separated by comma. |
not in |
For matching if not in a set of values, each element must be separated by comma. |
is |
For matching if the left hand side type is an instanceof the value. |
not is |
For matching if the left hand side type is not an instanceof the value. |
range |
For matching if the left hand side is within a range of values defined as numbers: |
not range |
For matching if the left hand side is not within a range of values defined as numbers: |
And the following operators can be used to group expressions:
Operator |
Description |
---|---|
and |
and is used to group two expressions |
or |
or is used to group two expressions |
Notice: Currently and
or or
can only be used once in a simple language expression. This might change in the future.
The syntax for AND is:
${leftValue} OP rightValue and ${leftValue} OP rightValue
And the syntax for OR is:
${leftValue} OP rightValue or ${leftValue} OP rightValue
Some examples:
simple("${in.header.foo} == 'foo'") // ' ' can be omitted simple("${in.header.foo} == foo") // here Camel will type convert '100' into the type of in.header.bar and if its an Integer '100' will also be converter to an Integer simple("${in.header.bar} == '100'") simple("${in.header.bar} == 100") // 100 will be converter to the type of in.header.bar so we can do > comparison simple("${in.header.bar} > 100") // testing for null simple("${in.header.baz} == null") // testing for not null simple("${in.header.baz} != null")
And a bit more advanced example where the right value is another expression
simple("${in.header.date} == ${date:now:yyyyMMdd}") simple("${in.header.type} == ${bean:orderService?method=getOrderType}")
And an example with contains, testing if the title contains the word Camel
simple("${in.header.title} contains 'Camel'")
And an example with regex, testing if the number header is a 4 digit value:
simple("${in.header.number} regex '\d{4}'")
And finally an example if the header equals any of the values in the list. Each element must be separated by comma, and no space around.
This also works for numbers etc, as Camel will convert each element into the type of the left hand side.
simple("${in.header.type} in 'gold,silver'")
And for all the last 3 we also support the negate test using not:
simple("${in.header.type} not in 'gold,silver'")
And you can test for if the type is a certain instance, eg for instance a String
simple("${in.header.type} is 'java.lang.String'")
We have added a shorthand for all java.lang
types so you can write it as:
simple("${in.header.type} is String")
Ranges is also supported. The range interval requires numbers and both from and end is inclusive. For instance to test whether a value is between 100 and 199:
simple("${in.header.number} range 100..199")
Notice we use ..
in the range without spaces. Its based on the same syntax as Groovy.
Can be used in Spring XML
As the Spring XML does not have all the power as the Java DSL with all its various builder methods, you had to resort to use some other languages
for testing with simple operators. Now you can do this with the simple language. In the sample below we want to test if the header is a widget order:
<from uri="seda:orders"> <filter> <simple>${in.header.type} == 'widget'</simple> <to uri="bean:orderService?method=handleWidget"/> </filter> </from>
Using and / or
If you have two expressions you can combine them with the and
or or
operator.
For instance:
simple("${in.header.title} contains 'Camel' and ${in.header.type'} == 'gold'")
And of course the or
is also supported. The sample example would be:
simple("${in.header.title} contains 'Camel' or ${in.header.type'} == 'gold'")
Notice: Currently and
or or
can only be used once in a simple language expression. This might change in the future.
So you cannot do:
simple("${in.header.title} contains 'Camel' and ${in.header.type'} == 'gold' and ${in.header.number} range 100..200")
Samples
In the Spring XML sample below we filter based on a header value:
<from uri="seda:orders"> <filter> <simple>in.header.foo</simple> <to uri="mock:fooOrders"/> </filter> </from>
The Simple language can be used for the predicate test above in the Message Filter pattern, where we test if the in message has a foo
header (a header with the key foo
exists). If the expression evaluates to true then the message is routed to the mock:foo
endpoint, otherwise its lost in the deep blue sea .
The same example in Java DSL:
from("seda:orders") .filter().simple("in.header.foo").to("seda:fooOrders");
You can also use the simple language for simple text concatenations such as:
from("direct:hello").transform().simple("Hello ${in.header.user} how are you?").to("mock:reply");
Notice that we must use ${ } placeholders in the expression now to let Camel be able to parse it correctly.
And this sample uses the date command to output current date.
from("direct:hello").transform().simple("The today is ${date:now:yyyyMMdd} and its a great day.").to("mock:reply");
And in the sample below we invoke the bean language to invoke a method on a bean to be included in the returned string:
from("direct:order").transform().simple("OrderId: ${bean:orderIdGenerator}").to("mock:reply");
Where orderIdGenerator
is the id of the bean registered in the Registry. If using Spring then its the Spring bean id.
If we want to declare which method to invoke on the order id generator bean we must prepend .method name
such as below where we invoke the generateId
method.
from("direct:order").transform().simple("OrderId: ${bean:orderIdGenerator.generateId}").to("mock:reply");
And in Camel 2.0 we can use the ?method=methodname
option that we are familiar with the Bean component itself:
from("direct:order").transform().simple("OrderId: ${bean:orderIdGenerator?method=generateId}").to("mock:reply");
And from Camel 2.3 onwards you can also convert the body to a given type, for example to ensure its a String you can do:
<transform> <simple>Hello ${bodyAs(String)} how are you?</simple> </transform>
There is a few types which have a shorthand notation, hence why we can use String
instead of java.lang.String
. These are: byte[], String, Integer, Long
. All other types must use their FQN name, e.g. org.w3c.dom.Document
.
Its also possible to lookup a value from a header Map
in Camel 2.3 onwards:
<transform> <simple>The gold value is ${header.type[gold]}</simple> </transform>
In the code above we lookup the header with name type
and regard it as a java.util.Map
and we then lookup with the key gold
and return the value.
If the header is not convertible to Map an exception is thrown. If the header with name type
does not exists null
is returned.
Dependencies
The Bean language is part of camel-core.