You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 18 Next »

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:

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).

Variables

Variable

Type

Description

id

String

the input message id

body

Object

the input body

in.body

Object

the input body

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

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

exception.message

String

New in Camel 2.0. Refer to the exception.messsage on the exchange, is <tt>null</tt> if no exception set on exchange

date:command:pattern

String

New in Camel 1.5. Date formatting using the java.text.SimepleDataFormat patterns. Supported commands are: now for current timestamp, in.header.xxx or header.xxx to use the Date object in the IN header with the key xxx. out.header.xxx to use the Date object in the OUT header with the key xxx.

bean:bean expression

Object

New in 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.

Operator support

Avaiable 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:

  • ==
  • <
  • <=
  • >
  • >=
  • !=

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 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}")

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>

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 (wink).

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");

Dependencies

The Bean language is part of camel-core.

  • No labels