Version 2.1
Copyright © 2012 FuseSource Corp. All rights reserved.
Updated: 09 Oct 2012
Table of Contents
List of Tables
List of Examples
To provide greater flexibility when parsing and processing messages, Apache Camel supports language plug-ins for various scripting languages. For example, if an incoming message is formatted as XML, it is relatively easy to extract the contents of particular XML elements or attributes from the message using a language such as XPath. The Apache Camel implements script builder classes, which encapsulate the imported languages. Each language is accessed through a static method that takes a script expression as its argument, processes the current message using that script, and then returns an expression or a predicate.
Fuse IDE's Property Editor uses a drop-down list to specify
the language used in an expression. The combination of the Expression
field and the associated Language field is translated into a
language element in the generated XML. The
language element has a language
attribute that specifies the language used to interpret the expression. The content of the
element must be a script written in the relevant language. At runtime, the return value
of the script is read by the parent element.
Table 1.1 lists the values for the Language attribute.
Table 1.1. Available languages
| Language | Attribute Value | Description |
|---|---|---|
| BeanShell | beanshell | The BeanShell scripting language (see http://www.beanshell.org/). |
| Constant | constant | Allows the use of constants as an expression. |
| EL | el | The Unified Expression Language (EL), originally developed as part of the JSP standard (see http://juel.sourceforge.net/). |
| Groovy | groovy | The Groovy scripting language (see http://groovy.codehaus.org/). |
| Header | header | Extracts the value of a named header. |
| JavaScript | javaScript | The JavaScript scripting language (see http://developer.mozilla.org/en/docs/JavaScript), also known as ECMAScript (see http://www.ecmascript.org/). |
| MVEL | mvel | The MVEL expression language (see http://mvel.codehaus.org/). |
| OGNL | ognl | The OGNL (Object Graph Navigation Language) language (see http://www.ognl.org/). |
| PHP | php | The PHP scripting language (see http://www.php.net/). |
| Property | property | Extracts the value of a named property. |
| Python | python | The Python scripting language (see http://www.python.org/). |
| Ruby | ruby | The Ruby scripting language (see http://www.ruby-lang.org/). |
| Simple | simple | A simple expression language, native to Apache Camel. |
| SQL | sql | The 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/). |
| XPath | xpath | The 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. |
| XQuery | xquery | The 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. |
BeanShell is a Java-based scripting language that allows quick parsing of
object. The BeanShell support is part of the camel-script module.
![]() | Important |
|---|---|
You must use BeanShell 2.0b5 or greater. |
To use BeanShell in your routes you need to add a dependency on
camel-script to your project as shown in
Example 2.1.
Example 2.1. Adding the camel-script dependency
<!-- Maven POM File -->
<properties>
<camel-version>2.8.0-fuse-00-05</camel-version>
...
</properties>
<dependencies>
...
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-script</artifactId>
<version>${camel-version}</version>
</dependency>
...
</dependencies>Table 2.1 lists the built-in attributes that are accessible when using BeanShell.
Table 2.1. BeanShell attributes
| Attribute | Type | Value |
|---|---|---|
context | org.apache.camel.CamelContext | The Camel Context |
exchange | org.apache.camel.Exchange | The current Exchange |
request | org.apache.camel.Message | The IN message |
response | org.apache.camel.Message | The OUT message |
The attributes all set at ENGINE_SCOPE.
Example 2.2 shows two routes that use BeanShell scripts.
Example 2.2. Routes using BeanShell
<camelContext>
<route>
<from uri=""mock:mock0" />
<filter>
<language language="beanshell">request.getHeaders().get("Foo") == null</language>
<to uri="mock:mock1" />
</filter>
</route>
<route>
<from uri="direct:in"/>
<setHeader headerName="firstName">
<expression language="beanshell">user.firstName</expression>
</setHeader>
<to uri="seda:users"/>
</route>
</camelContext>The constant language is a trivial built-in language that is used to specify a plain text string. This makes it possible to provide a plain text string in any context where an expression type is expected.
The code shown in Example 3.1 sets the username
header to the value Jane Doe.
Example 3.1. Using the constant language
<camelContext>
<route>
<from uri="SourceURL"/>
<setHeader headerName="username">
<constant>Jane Doe</constant>
</setHeader>
<to uri="TargetURL"/>
</route>
</camelContext>EL was originally specified as part of the JSP 2.1 standard (JSR-245), but it is now available as a standalone language. Apache Camel integrates with JUEL (http://juel.sourceforge.net/), which is an open source implementation of the EL language.
To use EL in your routes you need to add a dependency on
camel-juel to your project as shown in
Example 4.1.
Example 4.1. Adding the camel-juel dependency
<!-- Maven POM File -->
<properties>
<camel-version>2.8.0-fuse-00-05</camel-version>
...
</properties>
<dependencies>
...
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-juel</artifactId>
<version>${camel-version}</version>
</dependency>
...
</dependencies>Table 4.1 lists the variables that are accessible when using EL.
Table 4.1. EL variables
| Variable | Type | Value |
|---|---|---|
exchange | org.apache.camel.Exchange | The current Exchange |
in | org.apache.camel.Message | The IN message |
out | org.apache.camel.Message | The OUT message |
Example 4.2 shows two routes that use EL.
Example 4.2. Routes using EL
<camelContext>
<route>
<from uri="seda:foo"/>
<filter>
<language language="el">${in.headers.foo == 'bar'}</language>
<to uri="seda:bar"/>
</filter>
</route>
<route>
<from uri="seda:foo2"/>
<filter>
<language language="el">${in.headers['My Header'] == 'bar'}</language>
<to uri="seda:bar"/>
</filter>
</route>
</camelContext>Groovy is a Java-based scripting language that allows quick parsing of
object. The Groovy support is part of the camel-script module.
To use Groovy in your routes you need to add a dependency on
camel-script to your project as shown in
Example 5.1.
Example 5.1. Adding the camel-script dependency
<!-- Maven POM File -->
<properties>
<camel-version>2.8.0-fuse-00-05</camel-version>
...
</properties>
<dependencies>
...
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-script</artifactId>
<version>${camel-version}</version>
</dependency>
...
</dependencies>Table 5.1 lists the built-in attributes that are accessible when using Groovy.
Table 5.1. Groovy attributes
| Attribute | Type | Value |
|---|---|---|
context | org.apache.camel.CamelContext | The Camel Context |
exchange | org.apache.camel.Exchange | The current Exchange |
request | org.apache.camel.Message | The IN message |
response | org.apache.camel.Message | The OUT message |
The attributes all set at ENGINE_SCOPE.
Example 5.2 shows two routes that use Groovy scripts.
Example 5.2. Routes using Groovy
<camelContext>
<route>
<from uri=""mock:mock0" />
<filter>
<language language="groovy">request.lineItems.any { i -> i.value > 100 }</language>
<to uri="mock:mock1" />
</filter>
</route>
<route>
<from uri="direct:in"/>
<setHeader headerName="firstName">
<expression language="groovy">$user.firstName $user.lastName</expression>
</setHeader>
<to uri="seda:users"/>
</route>
</camelContext>The header language provides a convenient way of accessing header values in the current message. When you supply a header name, the header language performs a case-insensitive lookup and returns the corresponding header value.
Example 6.1 shows a route that resequences incoming exchanges
according to the value of a SequenceNumber header (where the sequence
number must be a positive integer).
Example 6.1. Using the header language
<camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="SourceURL"/> <resequence> <language language="header">SequenceNumber</language> </resequence> <to uri="TargetURL"/> </route> </camelContext>
JavaScript, also known as ECMAScript is a Java-based scripting language that allows quick
parsing of object. The JavaScript support is part of the camel-script module.
To use JavaScript in your routes you need to add a dependency on
camel-script to your project as shown in
Example 7.1.
Example 7.1. Adding the camel-script dependency
<!-- Maven POM File -->
<properties>
<camel-version>2.8.0-fuse-00-05</camel-version>
...
</properties>
<dependencies>
...
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-script</artifactId>
<version>${camel-version}</version>
</dependency>
...
</dependencies>Table 7.1 lists the built-in attributes that are accessible when using JavaScript.
Table 7.1. JavaScript attributes
| Attribute | Type | Value |
|---|---|---|
context | org.apache.camel.CamelContext | The Camel Context |
exchange | org.apache.camel.Exchange | The current Exchange |
request | org.apache.camel.Message | The IN message |
response | org.apache.camel.Message | The OUT message |
The attributes all set at ENGINE_SCOPE.
Example 7.2 shows a route that uses JavaScript.
Example 7.2. Route using JavaScript
<camelContext>
<route>
<from uri="direct:start"/>
<choice>
<when>
<langauge langauge="javaScript">request.headers.get('user') == 'admin'</langauge>
<to uri="seda:adminQueue"/>
</when>
<otherwise>
<to uri="seda:regularQueue"/>
</otherwise>
</choice>
</route>
</camelContext>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.
To use MVEL in your routes you need to add a dependency on
camel-mvel to your project as shown in
Example 8.1.
Example 8.1. Adding the camel-mvel dependency
<!-- Maven POM File -->
<properties>
<camel-version>2.8.0-fuse-00-05</camel-version>
...
</properties>
<dependencies>
...
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-mvel</artifactId>
<version>${camel-version}</version>
</dependency>
...
</dependencies>Table 8.1 lists the built-in variables that are accessible when using MVEL.
Table 8.1. MVEL variables
| Name | Type | Description |
|---|---|---|
this | org.apache.camel.Exchange | The current Exchange |
exchange | org.apache.camel.Exchange | The current Exchange |
exception | Throwable | the Exchange exception (if any) |
exchangeID | String | the Exchange ID |
fault | org.apache.camel.Message | The Fault message(if any) |
request | org.apache.camel.Message | The IN message |
response | org.apache.camel.Message | The OUT message |
properties | Map | The Exchange properties |
property( | Object | The value of the named Exchange property |
property( | | The typed value of the named Exchange property |
Example 8.2 shows a route that uses MVEL.
Example 8.2. Route using MVEL
<camelContext>
<route>
<from uri="seda:foo"/>
<filter>
<language langauge="mvel">request.headers.foo == 'bar'</language>
<to uri="seda:bar"/>
</filter>
</route>
</camelContext>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.
To use OGNL in your routes you need to add a dependency on
camel-ognl to your project as shown in
Example 9.1.
Example 9.1. Adding the camel-ognl dependency
<!-- Maven POM File -->
...
<dependencies>
...
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-ognl</artifactId>
<version>${camel-version}</version>
</dependency>
...
</dependencies>Table 9.1 lists the built-in variables that are accessible when using OGNL.
Table 9.1. OGNL variables
| Name | Type | Description |
|---|---|---|
this | org.apache.camel.Exchange | The current Exchange |
exchange | org.apache.camel.Exchange | The current Exchange |
exception | Throwable | the Exchange exception (if any) |
exchangeID | String | the Exchange ID |
fault | org.apache.camel.Message | The Fault message(if any) |
request | org.apache.camel.Message | The IN message |
response | org.apache.camel.Message | The OUT message |
properties | Map | The Exchange properties |
property( | Object | The value of the named Exchange property |
property( | | The typed value of the named Exchange property |
Example 9.2 shows a route that uses OGNL.
Example 9.2. Route using OGNL
<camelContext>
<route>
<from uri="seda:foo"/>
<filter>
<language langauge="ognl">request.headers.foo == 'bar'</language>
<to uri="seda:bar"/>
</filter>
</route>
</camelContext>PHP is a widely-used general-purpose scripting language that is especially suited for
Web development. The PHP support is part of the camel-script module.
To use PHP in your routes you need to add a dependency on
camel-script to your project as shown in
Example 10.1.
Example 10.1. Adding the camel-script dependency
<!-- Maven POM File -->
...
<dependencies>
...
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-script</artifactId>
<version>${camel-version}</version>
</dependency>
...
</dependencies>Table 10.1 lists the built-in attributes that are accessible when using PHP.
Table 10.1. PHP attributes
| Attribute | Type | Value |
|---|---|---|
context | org.apache.camel.CamelContext | The Camel Context |
exchange | org.apache.camel.Exchange | The current Exchange |
request | org.apache.camel.Message | The IN message |
response | org.apache.camel.Message | The OUT message |
The attributes all set at ENGINE_SCOPE.
Example 10.2 shows a route that uses PHP.
Example 10.2. Route using PHP
<camelContext>
<route>
<from uri="direct:start"/>
<choice>
<when>
<langauge langauge="php">strpos(request.headers.get('user'), 'admin')!== FALSE</langauge>
<to uri="seda:adminQueue"/>
</when>
<otherwise>
<to uri="seda:regularQueue"/>
</otherwise>
</choice>
</route>
</camelContext>The property language provides a convenient way of accessing exchange properties. When you supply a key that matches one of the exchange property names, the property language returns the corresponding value.
Python is a remarkably powerful dynamic programming language that is used in a wide
variety of application domains. Python is often compared to Tcl, Perl, Ruby, Scheme or Java.
The Python support is part of the camel-script module.
To use Python in your routes you need to add a dependency on
camel-script to your project as shown in
Example 12.1.
Example 12.1. Adding the camel-script dependency
<!-- Maven POM File -->
...
<dependencies>
...
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-script</artifactId>
<version>${camel-version}</version>
</dependency>
...
</dependencies>Table 12.1 lists the built-in attributes that are accessible when using Python.
Table 12.1. Python attributes
| Attribute | Type | Value |
|---|---|---|
context | org.apache.camel.CamelContext | The Camel Context |
exchange | org.apache.camel.Exchange | The current Exchange |
request | org.apache.camel.Message | The IN message |
response | org.apache.camel.Message | The OUT message |
The attributes all set at ENGINE_SCOPE.
Example 12.2 shows a route that uses Python.
Example 12.2. Route using Python
<camelContext>
<route>
<from uri="direct:start"/>
<choice>
<when>
<langauge langauge="python">if request.headers.get('user') = 'admin'</langauge>
<to uri="seda:adminQueue"/>
</when>
<otherwise>
<to uri="seda:regularQueue"/>
</otherwise>
</choice>
</route>
</camelContext>Ruby is a dynamic, open source programming language with a focus on simplicity and
productivity. It has an elegant syntax that is natural to read and easy to write. The Ruby
support is part of the camel-script module.
To use Ruby in your routes you need to add a dependency on
camel-script to your project as shown in
Example 13.1.
Example 13.1. Adding the camel-script dependency
<!-- Maven POM File -->
...
<dependencies>
...
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-script</artifactId>
<version>${camel-version}</version>
</dependency>
...
</dependencies>Table 13.1 lists the built-in attributes that are accessible when using Ruby.
Table 13.1. Ruby attributes
| Attribute | Type | Value |
|---|---|---|
context | org.apache.camel.CamelContext | The Camel Context |
exchange | org.apache.camel.Exchange | The current Exchange |
request | org.apache.camel.Message | The IN message |
response | org.apache.camel.Message | The OUT message |
The attributes all set at ENGINE_SCOPE.
Example 13.2 shows a route that uses Ruby.
Example 13.2. Route using Ruby
<camelContext>
<route>
<from uri="direct:start"/>
<choice>
<when>
<langauge langauge="ruby">$request.headers['user'] == 'admin'</langauge>
<to uri="seda:adminQueue"/>
</when>
<otherwise>
<to uri="seda:regularQueue"/>
</otherwise>
</choice>
</route>
</camelContext>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.
Simple language expressions and predicates combine plain text strings, variables, and property placeholders in a straight forward manner. The most basic expression will contain a single variable. More complex expressions will use variables to fill in portions of text strings.
Simple language expressions can also use OGNL notation as discussed in OGNL expressions.
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
| Variable | Type | Description |
|---|---|---|
exchangeId | String | The exchange's ID value. |
id | String | The In message ID value. |
body
| Object |
The In message body. Supports OGNL expressions. |
in.body | Object | The In message body. Supports OGNL expressions. |
out.body
| Object |
The Out message body. |
bodyAs( | Type | The 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 | The 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.
HeaderName | Object |
The In message's |
headers.
HeaderName | Object | The In message's HeaderName
header. |
in.header.
HeaderName | Object | The In message's HeaderName
header. Supports OGNL expressions. |
in.headers.
HeaderName | Object | The In message's HeaderName
header. Supports OGNL expressions. |
out.header.
HeaderName
| Object |
The Out message's |
out.headers.
HeaderName | Object | The Out message's HeaderName
header. |
headerAs( | Type | The 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 |
sys.SysPropertyName | String | The SysPropertyName Java system property. |
sysenv.SysEnvVar | String | The SysEnvVar system environment variable. |
exception | String | Either 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.message | String | The exception message on the exchange or null, if no exception is
set. |
date:command:pattern | String | A 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:beanRef | Object | Invokes 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: | String | The value of the Key property placeholder. |
properties: | String | The value of the Key property placeholder, where the
location of the properties file is given by Location. |
threadName | String | The name of the current thread. |
You can use the simple language to define string expressions, based on the variables
provided. For example, you can use a variable of the form,
in.header., to obtain the value of the
HeaderNameHeaderName header, as follows:
<language language="simple">in.header.foo</language>
You can embed simple variables in a string expression, but in this case you must
enclose the variables in ${ } (when you reference a variable on its own,
the enclosing braces are optional)—for example:
<language language="simple"> Received a message from ${in.header.user} on $(date:in.header.date:yyyyMMdd}. </language>
Sometimes—for example, if you have enabled Spring property placeholders or OSGi
blueprint property placeholders—you might find that the
${ syntax clashes with another property
placeholder syntax. In this case, you can disambiguate the placeholder using the
alternative syntax, Expression}$simple{, for the
simple expression. For example:Expression}
<simple>Hello $simple{in.header.name}, how are you?</simple>As well as providing variables that access all of the different parts of an exchange, the simple language also provides special variables for formatting dates:
date:command:pattern
For example, you can use the date variables as follows:
<language language="simple">Todays date is ${date:now:yyyyMMdd}</language>
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 use the null-safe operator, ?., to avoid encountering
null-pointer exceptions, in case the body does not have an
address. For example:
<language language="simple">${body?.address?.street}</language>
If the body is a java.util.Map type, you can look up a value in the
map with the key, foo, using the following notation:
<language language="simple">simple("${body[foo]?.name}")</language>
You can also use square brackets notation, [k], to access the
elements of a list. For example:
<language language="simple">${body.address.lines[0]}</language> <language language="simple">${body.address.lines[1]}</language> <language language="simple">${body.address.lines[2]}</language>
The last keyword returns the index of the last element of a list. For
example, you can access the second last element of a list, as
follows:
<language language="simple">${body.address.lines[last-1]}</language>
You can construct predicates by testing expressions for equality. For example, the
predicate, simple("${header.timeOfDay} == '14:30'"), tests
whether the timeOfDay header in the incoming message is equal to
14:30.
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:
${LHSVariable} Op RHSValueWhere 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 complete set of operators for simple language predicates is shown in Table 14.2.
Table 14.2. Simple language operators
| Operator | Description |
|---|---|
== | Equals. |
> | Greater than. |
>= | Greater than or equals. |
< | Less than. |
<= | Less than or equals. |
!= | Not equal to. |
contains | Test if LHS string contains RHS string. |
not contains | Test if LHS string does not contain RHS string. |
regex | Test if LHS string matches RHS regular expression. |
not regex | Test if LHS string does not match RHS regular expression. |
in | Test if LHS string appears in the RHS comma-separated list. |
not in | Test if LHS string does not appear in the RHS comma-separated list. |
is | Test if LHS is an instance of RHS Java type (using Java instanceof
operator). |
not is | Test if LHS is not an instance of RHS Java type (using
Java instanceof operator). |
range | Test if LHS number lies in the RHS range (where range has the format,
min...max). |
not range | Test if LHS number does not lie in the RHS
range (where range has the format,
min...max). |
For example, you can perform simple string comparisons and numerical comparisons as follows:
<language language="simple">${in.header.user} == 'john'</language> <!-- Quotes are optional here --> <language language="simple">${in.header.user} == john</language> <language language="simple">${in.header.number} > 100</language> <!-- String literal can be converted to integer --> <language language="simple">${in.header.number} > '100'</language>
You can test whether the left hand side is a member of a comma-separated list, as follows:
<language language="simple">${in.header.type} not in 'gold,silver'</language>
You can test whether the left hand side matches a regular expression, as follows:
<language language="simple">${in.header.number} regex '\d{4}'</language>
You can test the type of the left hand side using the is operator, as
follows:
<language language="simple">${in.header.type} is 'java.lang.String'</language> <!-- You can abbreviate java.lang. types --> <language language="simple">${in.header.type} is String</langauge>
You can test whether the left hand side lies in a specified numerical range, as follows:
<language language="simple">${in.header.number} range 100..199</language>
You can also combine predicates using the logical conjunctions, and
and or. But these conjunctions are currently subject to the restriction
that a logical conjunction can appear only once in a simple
expression.
For example, here is an expression using the and conjunction:
<language language="simple"> ${in.header.title} contains 'Camel' and ${in.header.type} == 'gold' </language>
And here is an expression using the or conjunction:
<language language="simple"> ${in.header.title} contains 'Camel' or ${in.header.type} == 'gold' </langauge>
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.
To use Ruby in your routes you need to add a dependency on
camel-josql to your project as shown in
Example 15.1.
Example 15.1. Adding the camel-script dependency
<!-- Maven POM File -->
...
<dependencies>
...
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-josql</artifactId>
<version>${camel-version}</version>
</dependency>
...
</dependencies>Table 15.1 lists the variables that are accessible when using SQL.
Table 15.1. SQL variables
| Name | Type | Description |
|---|---|---|
exchange | org.apache.camel.Exchange | The current Exchange |
in | org.apache.camel.Message | The IN message |
out | org.apache.camel.Message | The OUT message |
property | Object | the Exchange property whose key is property |
header | Object | the IN message header whose key is header |
variable | Object | the variable whose key is variable |
Example 15.2 shows a route that uses SQL.
Example 15.2. Route using SQL
<camelContext>
<route>
<from uri="direct:start"/>
<setBody>
<langauge langauge="sql">select * from MyType</langauge>
</setBody>
<to uri="seda:regularQueue"/>
</route>
</camelContext>XPath expressions implicitly acts on the message content and returns a node set as its result. Depending on the context, the return value is interpreted either as a predicate (where an empty node set is interpreted as false) or as an expression.
When processing documents whose elements belong to one or more XML schemas, it is typically necessary to associate namespace URIs with prefixes, so that you can identify element names unambiguously in your XPath expressions. Because Spring DSL is itself written in XML, it is possible to use the standard XML mechanism for associating prefixes with namespace URIs. That is, you can set an attribute as follows:
xmlns:prefix="namespaceURI"
For example, to associate the prefix, cust, with the namespace,
http://acme.com/customer/record, and then extract the contents of
the element, /cust:person/cust:name, you could define a route like the
following:
<beans ... xmlns:cust="http://acme.com/customer/record" ...> <camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="queue:foo"/> <setHeader headerName="user"> <xpath>/cust:person/cust:name/text()</xpath> </setHeader> <to uri="direct:tie"/> </route> </camelContext> ... </beans>
When Fuse IDE encounters an element that is in a namespace that is not explicitly declared it uses Apache Camel's default namespace resolution scheme. Apache Camel will try to resolve a variable in the following steps:
from variables that has been set using the variable(name, value) fluent
builder
from message.in.header if there is a header with the given key
from exchange.properties if there is a property with the given
key
Table 16.1 lists the variables that are accessible when using XPath.
Table 16.1. XPath variables
| Namespace | Local Part | Type | Description |
|---|---|---|---|
http://camel.apache.org/xml/in/ | in | Message | The IN message |
http://camel.apache.org/xml/out/ | out | Message | The OUT message |
http://camel.apache.org/xml/variables/exchange-property | | Object | the Exchange property whose key is property |
http://camel.apache.org/xml/functions/ | functions | Object | Additional functions described in Table 16.2 |
http://camel.apache.org/xml/variables/environment-variables | env | Object | OS environment variables |
http://camel.apache.org/xml/variables/system-properties | system | Object | Java System properties |
Apache Camel adds the following XPath functions that can be used to access the exchange:
Table 16.2. Aditional XPath functions
| Function | Argument | Type | Description |
|---|---|---|---|
in:body | Object | Returns the in message body. | |
in:header | headerName | Object | Return the specified in message header. |
out:body | Object | Returns the out message body. | |
out:header | headerName | Object | Return the specified out message header. |
function:properties | propertyKey | String | Looks up a property using the Properties component. |
function:simple | expression | Object | Evaluates a Simple expression. |
Example 16.1 shows a route that uses XPath.
Example 16.1. Route using XPath
<camelContext>
<route>
<from uri="direct:in"/>
<choice>
<when>
<language langauge="xpath">$type = 'Camel'</language>
<to uri="mock:camel"/>
</when>
<when>
<language langauge="xpath">//name = 'Kong'</language>
<to uri="mock:donkey"/>
</when>
<otherwise>
<to uri="mock:other"/>
</otherwise>
</choice>
</route>
</camelContext>XQuery was devised primarily as a query language for data stored in XML form. Its main
role is to get information out of XML databases. XQuery support is supplied by the
camel-saxon module.
To use XQuery in your routes you need to add a dependency on
camel-saxon to your project as shown in
Example 17.1.
Example 17.1. Adding the camel-script dependency
<!-- Maven POM File -->
...
<dependencies>
...
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-saxon</artifactId>
<version>${camel-version}</version>
</dependency>
...
</dependencies>Table 17.1 lists the variables that are accessible when using XQuery.
Table 17.1. XQuery variables
| Variable | Type | Description |
|---|---|---|
exchange | Exchange | The current Exchange |
in.body | Object | The body of the IN message |
out.body | Object | The body of the OUT message |
in.headers. | Object | The IN message header whose key is key |
out.headers. | Object | The OUT message header whose key is key |
key | Object | The Exchange property whose key is key |
Example 17.2 shows a route that uses XQuery.
Example 17.2. Route using XQuery
<camelContext>
<route>
<from uri="activemq:MyQueue"/>
<filter>
<language langauge="xquery">/foo:person[@name='James']</language>
<to uri="mqseries:SomeOtherQueue"/>
</filter>
</route>
</camelContext>