LibraryPrintFeedback

Expression and Predicates Languages

Version 2.1

March 2012
Trademark Disclaimer
Third Party Acknowledgements

Updated: 09 Oct 2012

Table of Contents

1. Introduction
2. BeanShell
3. Constant
4. The Unified Expression Language(EL)
5. Groovy
6. Header
7. JavaScript
8. MVEL
9. The Object-Graph Navigation Language(OGNL)
10. PHP
11. Property
12. Python
13. Ruby
14. The Simple Language
Basic syntax
OGNL expressions
Predicates
15. SQL
16. XPath
17. XQuery

List of Tables

1.1. Available languages
2.1. BeanShell attributes
4.1. EL variables
5.1. Groovy attributes
7.1. JavaScript attributes
8.1. MVEL variables
9.1. OGNL variables
10.1. PHP attributes
12.1. Python attributes
13.1. Ruby attributes
14.1. Simple language variable
14.2. Simple language operators
15.1. SQL variables
16.1. XPath variables
16.2. Aditional XPath functions
17.1. XQuery variables

List of Examples

2.1. Adding the camel-script dependency
2.2. Routes using BeanShell
3.1. Using the constant language
4.1. Adding the camel-juel dependency
4.2. Routes using EL
5.1. Adding the camel-script dependency
5.2. Routes using Groovy
6.1. Using the header language
7.1. Adding the camel-script dependency
7.2. Route using JavaScript
8.1. Adding the camel-mvel dependency
8.2. Route using MVEL
9.1. Adding the camel-ognl dependency
9.2. Route using OGNL
10.1. Adding the camel-script dependency
10.2. Route using PHP
12.1. Adding the camel-script dependency
12.2. Route using Python
13.1. Adding the camel-script dependency
13.2. Route using Ruby
15.1. Adding the camel-script dependency
15.2. Route using SQL
16.1. Route using XPath
17.1. Adding the camel-script dependency
17.2. Route using XQuery

Table 1.1 lists the values for the Language attribute.

Table 1.1. Available languages

LanguageAttribute ValueDescription
BeanShellbeanshellThe BeanShell scripting language (see http://www.beanshell.org/).
ConstantconstantAllows the use of constants as an expression.
ELelThe Unified Expression Language (EL), originally developed as part of the JSP standard (see http://juel.sourceforge.net/).
GroovygroovyThe Groovy scripting language (see http://groovy.codehaus.org/).
HeaderheaderExtracts the value of a named header.
JavaScriptjavaScriptThe JavaScript scripting language (see http://developer.mozilla.org/en/docs/JavaScript), also known as ECMAScript (see http://www.ecmascript.org/).
MVELmvelThe MVEL expression language (see http://mvel.codehaus.org/).
OGNLognlThe OGNL (Object Graph Navigation Language) language (see http://www.ognl.org/).
PHPphpThe PHP scripting language (see http://www.php.net/).
PropertypropertyExtracts the value of a named property.
PythonpythonThe Python scripting language (see http://www.python.org/).
RubyrubyThe Ruby scripting language (see http://www.ruby-lang.org/).
SimplesimpleA simple expression language, native to Apache Camel.
SQLsqlThe JoSQL language, which is a language for extracting and manipulating data from collections of Java objects, using a SQL-like syntax (see http://josql.sourceforge.net/).
XPathxpathThe XPath language, which is used to select element, attribute, and text nodes from XML documents (see http://www.w3schools.com/xpath/default.asp). The XPath expression is applied to the current message.
XQueryxqueryThe XQuery language, which is an extension of XPath (see http://www.w3schools.com/xquery/default.asp). The XQuery expression is applied to the current message.

MVEL (http://mvel.codehaus.org/) is a Java-based dynamic language that is similar to OGNL, but is reported to be much faster. The MVEL support is in the camel-mvel module.

MVEL (http://www.opensymphony.com/ognl/) is an expression language for getting and setting properties of Java objects. You use the same expression for both getting and setting the value of a property. The OGNL support is in the camel-ognl module.

The simple language provides various elementary expressions that return different parts of a message exchange. For example, the expression, header.timeOfDay, would return the contents of a header called timeOfDay from the incoming message. You can also construct predicates by testing expressions for equality. For example, the predicate, ${header.timeOfDay} == '14:30', tests whether the timeOfDay header in the incoming message is equal to 14:30.

Expressions using the Simple language are typically a combination of variables and text strings that are used to create messages.

Table 14.1 shows all of the variables supported by the simple language.

Table 14.1. Simple language variable

VariableTypeDescription
exchangeIdStringThe exchange's ID value.
idStringThe In message ID value.
body Object

The In message body. Supports OGNL expressions.

in.bodyObjectThe In message body. Supports OGNL expressions.
out.body Object

The Out message body.

bodyAs(Type)TypeThe In message body, converted to the specified type. All types, Type, must be specified using their fully-qualified Java name, except for the types: byte[], String, Integer, and Long. The converted body can be null.
mandatoryBodyAs(Type)TypeThe In message body, converted to the specified type. All types, Type, must be specified using their fully-qualified Java name, except for the types: byte[], String, Integer, and Long. The converted body is expected to be non-null.
header. HeaderNameObject

The In message's HeaderName header. Supports OGNL expressions.

headers. HeaderNameObjectThe In message's HeaderName header.
in.header. HeaderNameObjectThe In message's HeaderName header. Supports OGNL expressions.
in.headers. HeaderNameObjectThe In message's HeaderName header. Supports OGNL expressions.
out.header. HeaderName Object

The Out message's HeaderName header.

out.headers. HeaderNameObjectThe Out message's HeaderName header.
headerAs(Key,Type)TypeThe Key header, converted to the specified type. All types, Type, must be specified using their fully-qualified Java name, except for the types: byte[], String, Integer, and Long. The converted value can be null.
property. PropertyName Object

The PropertyName property on the exchange.

sys.SysPropertyNameStringThe SysPropertyName Java system property.
sysenv.SysEnvVarStringThe SysEnvVar system environment variable.
exceptionStringEither the exception object from Exchange.getException() or, if this value is null, the caught exception from the Exchange.EXCEPTION_CAUGHT property; otherwise null. Supports OGNL expressions.
exception.messageStringThe exception message on the exchange or null, if no exception is set.
date:command:patternStringA date formatted using a java.text.SimpleDateFormat pattern. The following commands are supported: now, for the current date and time; header.HeaderName, or in.header.HeaderName to use a java.util.Data object in the HeaderName header from the In message; out.header.HeaderName to use a java.util.Data object in the HeaderName header from the Out message;
bean:beanRefObjectInvokes a method on the referenced bean. To specify a method name, you can either append a dot, ., followed by the method name; or you can use the ?method=methodName syntax.
properties:KeyStringThe value of the Key property placeholder.
properties:Location:KeyStringThe value of the Key property placeholder, where the location of the properties file is given by Location.
threadNameStringThe name of the current thread.

The Object Graph Navigation Language (OGNL) is a notation for invoking bean methods in a chain-like fashion. If a message body contains a Java bean, you can easily access its bean properties using OGNL notation. For example, if the message body is a Java object with a getAddress() accessor, you can access the Address object and the Address object's properties as follows:

<language language="simple">${body.address}</language>
<language language="simple">${body.address.street}</language>
<language language="simple">${body.address.zip}</language>
<language language="simple">${body.address.city}</language>

Where the notation, ${body.address.street}, is shorthand for ${body.getAddress.getStreet}.

You can also test various parts of an exchange (headers, message body, and so on) using simple predicates. Simple predicates have the following general syntax:

Where the variable on the left hand side, LHSVariable, is one of the variables shown in Table 14.1 and the value on the right hand side, RHSValue, is one of the following:

  • Another variable, ${RHSVariable}.

  • A string literal, enclosed in single quotes, ' '.

  • A string literal, not enclosed in quotes (no spaces allowed).

  • A numeric constant.

  • The null object, null.

The simple language always attempts to convert the RHS value to the type of the LHS value.

The SQL support is added by JoSQL(http://josql.sourceforge.net/) and is primarily used for performing SQL queries on in-memory objects. The SQL support is part of the camel-josql module.