To provide greater flexibility when parsing and processing messages, FUSE Mediation Router 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 FUSE Mediation Router 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. To be usable as an expression or a predicate, the script builder classes implement the following interfaces:
org.apache.camel.Expression<E> org.apache.camel.Predicate<E>
In addition to this, the ScriptBuilder
class (which wraps scripting
languages such as JavaScript) inherits from the following interface:
org.apache.camel.Processor
This implies that the languages associated with the ScriptBuilder
class can also be used as message processors (see Custom processor).
The simple language is a very limited expression language that is built into the router core. This language is useful when you need to eliminate dependencies on third-party libraries during testing; otherwise, you should use one of the other languages. To use the simple language in your application code, include the following import statement in your Java source files:
import static org.apache.camel.language.simple.SimpleLanguage.simple;
The simple language provides various elementary expressions that return different parts
of a message exchange. For example, the expression,
simple("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,
simple("header.timeOfDay = '14:30'")
, tests whether the
timeOfDay
header in the incoming message is equal to
14:30
. Table 2.1 shows the list of elementary
expressions supported by the simple language.
Table 2.1. Properties for Simple Language
Elementary Expression | Description |
---|---|
body
|
Accesses the body of the incoming message. |
out.body
|
Accesses the body of the outgoing message. |
header.
HeaderName
|
Accesses the contents of the |
out.header.
HeaderName
|
Accesses the contents of the |
property.
PropertyName
|
Accesses the |
The xpath()
static method parses message content using the
XPath language (to learn about XPath, see the W3 Schools tutorial at http://www.w3schools.com/xpath/default.asp). To use the XPath language in your
application code, include the following import statement in your Java source files:
import static org.apache.camel.builder.xml.XPathBuilder.xpath;
You can pass an XPath expression to xpath()
as a string argument. The
XPath expression 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. For example, if you
are processing an XML message with the following content:
<person user="paddington"> <firstName>Paddington</firstName> <lastName>Bear</lastName> <city>London</city> </person>
Then you could choose which target endpoint to route the message to, based on the
content of the city
element, by using the following rule:
from("file:src/data?noop=true"). choice(). when(xpath("/person/city = 'London'")).to("file:target/messages/uk"). otherwise().to("file:target/messages/others");
Where the return value of xpath()
is treated as a predicate in this
example.
The xquery()
static method parses message content using the XQuery
language (to learn about XQuery, see the W3 Schools tutorial, http://www.w3schools.com/xquery/default.asp). XQuery is a superset of the
XPath language; hence, any valid XPath expression is also a valid XQuery expression. To use
the XQuery language in your application code, include the following import statement in your
Java source files:
import static org.apache.camel.builder.saxon.XQueryBuilder.xquery;
You can pass an XQuery expression to xquery()
in several ways. For
simple expressions, you can pass the XQuery expressions as a string
(java.lang.String
). For longer XQuery expressions, you might prefer to
store the expression in a file, which you can then reference by passing a
java.io.File
argument or a java.net.URL
argument to
the overloaded xquery()
method. The XQuery expression implicitly acts on
the message content and returns a node set as the 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.
The sql()
static method enables you to call on the JoSQL (SQL for
Java objects) language to evaluate predicates and expressions in FUSE Mediation Router. JoSQL employs a
SQL-like query syntax to perform selection and ordering operations on data from in-memory
Java objects—however, JoSQL is not a database. In the JoSQL
syntax, each Java object instance is treated like a table row and each object method is
treated like a column name. Using this syntax, it is possible to construct powerful
statements for extracting and compiling data from collections of Java objects. For details,
see http://josql.sourceforge.net/.
To use the JoSQL language in your application code, include the following import statement in your Java source files:
import static org.apache.camel.builder.sql.SqlBuilder.sql;
The ognl()
static method enables you to call on OGNL (Object
Graph Navigation Language) expressions, which can then be used as predicates and expressions
in a router rule. For details, see http://www.ognl.org/.
To use the OGNL language in your application code, include the following import statement in your Java source files:
import static org.apache.camel.language.ognl.OgnlExpression.ognl;
The el()
static method enables you to call on the Unified Expression
Language (EL) to construct predicates and expressions in a router rule. The EL was
originally specified as part of the JSP 2.1 standard (JSR-245), but it is now available as a
standalone language. FUSE Mediation Router integrates with JUEL (http://juel.sourceforge.net/), which is an open source implementation of the
EL language.
To use the EL language in your application code, include the following import statement in your Java source files:
import static org.apache.camel.language.juel.JuelExpression.el;
The groovy()
static method enables you to call on the Groovy
scripting language to construct predicates and expressions in a route. To use the Groovy
language in your application code, include the following import statement in your Java
source files:
import static org.apache.camel.builder.camel.script.ScriptBuilder.*;
The javaScript()
static method enables you to call on the JavaScript
scripting language to construct predicates and expressions in a route. To use the JavaScript
language in your application code, include the following import statement in your Java
source files:
import static org.apache.camel.builder.camel.script.ScriptBuilder.*;
The php()
static method enables you to call on the PHP scripting
language to construct predicates and expressions in a route. To use the PHP language in your
application code, include the following import statement in your Java source files:
import static org.apache.camel.builder.camel.script.ScriptBuilder.*;
The python()
static method enables you to call on the Python
scripting language to construct predicates and expressions in a route. To use the Python
language in your application code, include the following import statement in your Java
source files:
import static org.apache.camel.builder.camel.script.ScriptBuilder.*;
The ruby()
static method enables you to call on the Ruby scripting
language to construct predicates and expressions in a route. To use the Ruby language in
your application code, include the following import statement in your Java source
files:
import static org.apache.camel.builder.camel.script.ScriptBuilder.*;
You can also use Java beans to evaluate predicates and expressions. For example, to
evaluate the predicate on a filter using the isGoldCustomer()
method on
the bean instance, myBean
, you can use a rule like the following:
from("SourceURL
") .filter().method("myBean", "isGoldCustomer") .to("TargetURL
");
A discussion of bean integration in FUSE Mediation Router is beyond the scope of this Defining Routes guide. For details, see http://activemq.apache.org/camel/bean-language.html.