camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Simple
Date Sun, 14 Mar 2010 11:48:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/CAMEL/Simple">Simple</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
     
          <br/>
     <div class="notificationGreySide">
         <h2><a name="Simple-SimpleExpressionLanguage"></a>Simple Expression
Language</h2>

<p>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 <a href="/confluence/display/CAMEL/XPath" title="XPath">XPath</a>;
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: </p>

<ul>
	<li><a href="/confluence/display/CAMEL/Bean+Language" title="Bean Language">Bean
Language</a></li>
	<li><a href="/confluence/display/CAMEL/EL" title="EL">EL</a></li>
	<li><a href="/confluence/display/CAMEL/OGNL" title="OGNL">OGNL</a></li>
	<li>one of the supported <a href="/confluence/display/CAMEL/Scripting+Languages"
title="Scripting Languages">Scripting Languages</a></li>
</ul>


<p>The simple language uses <tt>${body</tt>} placeholders for complex expressions
where the expression contains constant literals. The ${ } placeholders can be omitted if the
expression is only the token itself.</p>

<p>To get the body of the in message: <tt>"body"</tt>, or <tt>"in.body"</tt>
or <tt>"${body}"</tt>.</p>

<p>A complex expression must use ${ } placeholders, such as: <tt>"Hello ${in.header.name}
how are you?"</tt>.</p>

<p>You can have multiple tokens in the same expression: <tt>"Hello ${in.header.name}
this is ${in.header.me} speaking"</tt>.<br/>
However you can <b>not</b> nest tokens (i.e. having another ${ } placeholder in
an existing, is not allowed).</p>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>File language
is now merged with Simple language</b><br /><p>From Camel 2.2 onwards, the
<a href="/confluence/display/CAMEL/File+Language" title="File Language">File Language</a>
is now merged with <a href="/confluence/display/CAMEL/Simple" title="Simple">Simple</a>
language which means you can use all the file syntax directly within the simple language.</p></td></tr></table></div>

<h3><a name="Simple-Variables"></a>Variables</h3>

<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Variable </th>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'>  Description </th>
</tr>
<tr>
<td class='confluenceTd'> id </td>
<td class='confluenceTd'> String </td>
<td class='confluenceTd'> the input message id </td>
</tr>
<tr>
<td class='confluenceTd'> body </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> the input body </td>
</tr>
<tr>
<td class='confluenceTd'> in.body </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> the input body </td>
</tr>
<tr>
<td class='confluenceTd'> body.<b>OGNL</b> </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> the input body invoked using
a Camel OGNL expression. </td>
</tr>
<tr>
<td class='confluenceTd'> in.body.<b>OGNL</b> </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> the input body invoked using
a Camel OGNL expression. </td>
</tr>
<tr>
<td class='confluenceTd'> bodyAs(<em>type</em>) </td>
<td class='confluenceTd'> Type </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> Converts the body to the given
type determined by its classname </td>
</tr>
<tr>
<td class='confluenceTd'> out.body </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> the output body </td>
</tr>
<tr>
<td class='confluenceTd'> header.foo </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> refer to the input foo header </td>
</tr>
<tr>
<td class='confluenceTd'> headers.foo </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> refer to the input foo header </td>
</tr>
<tr>
<td class='confluenceTd'> in.header.foo </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> refer to the input foo header </td>
</tr>
<tr>
<td class='confluenceTd'> in.headers.foo </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> refer to the input foo header </td>
</tr>
<tr>
<td class='confluenceTd'> header.foo[bar] </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> regard input foo header as a
map and perform lookup on the map with bar as key </td>
</tr>
<tr>
<td class='confluenceTd'> in.header.foo[bar] </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> regard input foo header as a
map and perform lookup on the map with bar as key </td>
</tr>
<tr>
<td class='confluenceTd'> in.headers.foo[bar] </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> regard input foo header as a
map and perform lookup on the map with bar as key </td>
</tr>
<tr>
<td class='confluenceTd'> header.foo.<b>OGNL</b> </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> refer to the input foo header
and invoke its value using a Camel OGNL expression. </td>
</tr>
<tr>
<td class='confluenceTd'> in.header.foo.<b>OGNL</b> </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> refer to the input foo header
and invoke its value using a Camel OGNL expression. </td>
</tr>
<tr>
<td class='confluenceTd'> in.headers.foo.<b>OGNL</b> </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> refer to the input foo header
and invoke its value using a Camel OGNL expression. </td>
</tr>
<tr>
<td class='confluenceTd'> out.header.foo </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> refer to the out header foo </td>
</tr>
<tr>
<td class='confluenceTd'> out.headers.foo </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> refer to the out header foo </td>
</tr>
<tr>
<td class='confluenceTd'> property.foo </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> refer to the foo property on the exchange </td>
</tr>
<tr>
<td class='confluenceTd'> sys.foo </td>
<td class='confluenceTd'> String </td>
<td class='confluenceTd'> refer to the system property </td>
</tr>
<tr>
<td class='confluenceTd'> sysenv.foo </td>
<td class='confluenceTd'> String </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> refer to the system environment
</td>
</tr>
<tr>
<td class='confluenceTd'> exception.message </td>
<td class='confluenceTd'> String </td>
<td class='confluenceTd'> <b>Camel 2.0.</b> Refer to the exception.message
on the exchange, is <b>null</b> if no exception set on exchange. Will fallback
and grab caught exceptions (<tt>Exchange.EXCEPTION_CAUGHT</tt>) if the Exchange
has any. </td>
</tr>
<tr>
<td class='confluenceTd'> date:<em>command:pattern</em> </td>
<td class='confluenceTd'> String </td>
<td class='confluenceTd'> <b>Camel 1.5.</b> Date formatting using the <tt>java.text.SimpleDataFormat</tt>
patterns. Supported commands are: <b>now</b> for current timestamp, <b>in.header.xxx</b>
or <b>header.xxx</b> to use the Date object in the IN header with the key xxx.
<b>out.header.xxx</b> to use the Date object in the OUT header with the key xxx.</td>
</tr>
<tr>
<td class='confluenceTd'> bean:<em>bean expression</em> </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> <b>Camel 1.5.</b> Invoking a bean expression using
the <a href="/confluence/display/CAMEL/Bean" title="Bean">Bean</a> 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 <a href="/confluence/display/CAMEL/Bean" title="Bean">Bean</a>
component. </td>
</tr>
<tr>
<td class='confluenceTd'> properties:<em>locations:key</em> </td>
<td class='confluenceTd'> String </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> Lookup a property with the given
key. The <tt>locations</tt> option is optional. See more at <a href="/confluence/display/CAMEL/Using+PropertyPlaceholder"
title="Using PropertyPlaceholder">Using PropertyPlaceholder</a>. </td>
</tr>
</tbody></table>

<h3><a name="Simple-OGNLsupport"></a>OGNL support</h3>
<p><b>Available as of Camel 2.3</b></p>

<p>The <a href="/confluence/display/CAMEL/Simple" title="Simple">Simple</a>
and <a href="/confluence/display/CAMEL/Bean" title="Bean">Bean</a> language now
supports a Camel OGNL notation for invoking beans in a chain like fashion.<br/>
Suppose the Message IN body contains a POJO which has a <tt>getAddress()</tt>
method.</p>

<p>Then you can use Camel OGNL notation to access the address object:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
simple(<span class="code-quote">"${body.address}"</span>)
simple(<span class="code-quote">"${body.address.street}"</span>)
simple(<span class="code-quote">"${body.address.zip}"</span>)
</pre>
</div></div>

<p>Camel understands the shorthand names for getters, but you can invoke any method
or use the real name such as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
simple(<span class="code-quote">"${body.address}"</span>)
simple(<span class="code-quote">"${body.getAddress.getStreet}"</span>)
simple(<span class="code-quote">"${body.address.getZip}"</span>)
simple(<span class="code-quote">"${body.doSomething}"</span>)
</pre>
</div></div>

<p>You can also use the elvis operator (<tt>?.</tt>) to avoid NPE if for
example the body does NOT have an address</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
simple(<span class="code-quote">"${body?.address?.street}"</span>)
</pre>
</div></div>

<p>Its also possible to index in <tt>Map</tt> or <tt>List</tt>
types, so you can do:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
simple(<span class="code-quote">"${body[foo].name}"</span>)
</pre>
</div></div>
<p>To assume the body is <tt>Map</tt> based and lookup the value with <tt>foo</tt>
as key, and invoke the <tt>getName</tt> method on that value.</p>

<p>Suppose there was no value with the key <tt>foo</tt> then you can use
the elvis operator to avoid the NPE as shown:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
simple(<span class="code-quote">"${body[foo]?.name}"</span>)
</pre>
</div></div>

<p>You can also access <tt>List</tt> types, for example to get lines from
the address you can do:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
simple(<span class="code-quote">"${body.address.lines[0]}"</span>)
simple(<span class="code-quote">"${body.address.lines[1]}"</span>)
simple(<span class="code-quote">"${body.address.lines[2]}"</span>)
</pre>
</div></div>

<p>And yes you can combine this with the operator support as shown below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
simple(<span class="code-quote">"${body.address.zip} &gt; 1000"</span>)
</pre>
</div></div>

<h3><a name="Simple-Operatorsupport"></a>Operator support</h3>
<p><b>Available as of Camel 2.0</b><br/>
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. </p>

<p>To enable it the left value must be enclosed in ${ }. The syntax is:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
${leftValue} OP rightValue
</pre>
</div></div>
<p>Where the <tt>rightValue</tt> can be a String literal enclosed in <tt>'
'</tt>, <tt>null</tt>, a constant value or another expression enclosed in
${ }.<br/>
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 &gt; comparison for numeric values.</p>

<p>The following operators is supported:</p>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Operator </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> == </td>
<td class='confluenceTd'> equals </td>
</tr>
<tr>
<td class='confluenceTd'> &gt; </td>
<td class='confluenceTd'> greater than </td>
</tr>
<tr>
<td class='confluenceTd'> &gt;= </td>
<td class='confluenceTd'> greater than or equals </td>
</tr>
<tr>
<td class='confluenceTd'> &lt; </td>
<td class='confluenceTd'> less than </td>
</tr>
<tr>
<td class='confluenceTd'> &lt;= </td>
<td class='confluenceTd'> less than or equals </td>
</tr>
<tr>
<td class='confluenceTd'> != </td>
<td class='confluenceTd'> not equals </td>
</tr>
<tr>
<td class='confluenceTd'> contains </td>
<td class='confluenceTd'> For testing if contains in a string based value </td>
</tr>
<tr>
<td class='confluenceTd'> not contains </td>
<td class='confluenceTd'> For testing if not contains in a string based value </td>
</tr>
<tr>
<td class='confluenceTd'> regex </td>
<td class='confluenceTd'> For matching against a given regular expression pattern defined
as a String value </td>
</tr>
<tr>
<td class='confluenceTd'> not regex </td>
<td class='confluenceTd'> For not matching against a given regular expression pattern
defined as a String value </td>
</tr>
<tr>
<td class='confluenceTd'> in </td>
<td class='confluenceTd'> For matching if in a set of values, each element must be separated
by comma. </td>
</tr>
<tr>
<td class='confluenceTd'> not in </td>
<td class='confluenceTd'> For matching if not in a set of values, each element must
be separated by comma. </td>
</tr>
<tr>
<td class='confluenceTd'> is </td>
<td class='confluenceTd'> For matching if the left hand side type is an instanceof the
value. </td>
</tr>
<tr>
<td class='confluenceTd'> not is </td>
<td class='confluenceTd'> For matching if the left hand side type is not an instanceof
the value. </td>
</tr>
<tr>
<td class='confluenceTd'> range </td>
<td class='confluenceTd'> For matching if the left hand side is within a range of values
defined as numbers: <tt>from..to</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> not range </td>
<td class='confluenceTd'> For matching if the left hand side is not within a range of
values defined as numbers: <tt>from..to</tt> </td>
</tr>
</tbody></table>

<p>And the following operators can be used to group expressions:</p>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Operator </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> and </td>
<td class='confluenceTd'> and is used to group two expressions </td>
</tr>
<tr>
<td class='confluenceTd'> or </td>
<td class='confluenceTd'> or is used to group two expressions </td>
</tr>
</tbody></table>

<p><b>Notice:</b> Currently <tt>and</tt> or <tt>or</tt>
can only be used once in a simple language expression. This might change in the future.</p>

<p>The syntax for AND is:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
${leftValue} OP rightValue and ${leftValue} OP rightValue 
</pre>
</div></div>

<p>And the syntax for OR is:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
${leftValue} OP rightValue or ${leftValue} OP rightValue 
</pre>
</div></div>

<p>Some examples:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
simple(<span class="code-quote">"${in.header.foo} == 'foo'"</span>)

<span class="code-comment">// ' ' can be omitted
</span>simple(<span class="code-quote">"${in.header.foo} == foo"</span>)

<span class="code-comment">// here Camel will type convert '100' into the type of in.header.bar
and <span class="code-keyword">if</span> its an <span class="code-object">Integer</span>
'100' will also be converter to an <span class="code-object">Integer</span>
</span>simple(<span class="code-quote">"${in.header.bar} == '100'"</span>)

simple(<span class="code-quote">"${in.header.bar} == 100"</span>)

<span class="code-comment">// 100 will be converter to the type of in.header.bar so
we can <span class="code-keyword">do</span> &gt; comparison
</span>simple(<span class="code-quote">"${in.header.bar} &gt; 100"</span>)

<span class="code-comment">// testing <span class="code-keyword">for</span>
<span class="code-keyword">null</span>
</span>simple(<span class="code-quote">"${in.header.baz} == <span class="code-keyword">null</span>"</span>)

<span class="code-comment">// testing <span class="code-keyword">for</span>
not <span class="code-keyword">null</span>
</span>simple(<span class="code-quote">"${in.header.baz} != <span class="code-keyword">null</span>"</span>)
</pre>
</div></div>

<p>And a bit more advanced example where the right value is another expression</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
simple(<span class="code-quote">"${in.header.date} == ${date:now:yyyyMMdd}"</span>)

simple(<span class="code-quote">"${in.header.type} == ${bean:orderService?method=getOrderType}"</span>)
</pre>
</div></div>

<p>And an example with contains, testing if the title contains the word Camel</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
simple(<span class="code-quote">"${in.header.title} contains 'Camel'"</span>)
</pre>
</div></div>

<p>And an example with regex, testing if the number header is a 4 digit value:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
simple(<span class="code-quote">"${in.header.number} regex '\d{4}'"</span>)
</pre>
</div></div>

<p>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.<br/>
This also works for numbers etc, as Camel will convert each element into the type of the left
hand side.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
simple(<span class="code-quote">"${in.header.type} in 'gold,silver'"</span>)
</pre>
</div></div>

<p>And for all the last 3 we also support the negate test using not:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
simple(<span class="code-quote">"${in.header.type} not in 'gold,silver'"</span>)
</pre>
</div></div>

<p>And you can test for if the type is a certain instance, eg for instance a String</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
simple(<span class="code-quote">"${in.header.type} is 'java.lang.<span class="code-object">String</span>'"</span>)
</pre>
</div></div>
<p>We have added a shorthand for all <tt>java.lang</tt> types so you can
write it as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
simple(<span class="code-quote">"${in.header.type} is <span class="code-object">String</span>"</span>)
</pre>
</div></div>

<p>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:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
simple(<span class="code-quote">"${in.header.number} range 100..199"</span>)
</pre>
</div></div>
<p>Notice we use <tt>..</tt> in the range without spaces. Its based on the
same syntax as Groovy.</p>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Can be used in Spring
XML</b><br /><p>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<br/>
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: </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
    <span class="code-tag">&lt;from uri=<span class="code-quote">"seda:orders"</span>&gt;</span>
       <span class="code-tag">&lt;filter&gt;</span>
           <span class="code-tag">&lt;simple&gt;</span>${in.header.type}
== 'widget'<span class="code-tag">&lt;/simple&gt;</span>
           <span class="code-tag">&lt;to uri=<span class="code-quote">"bean:orderService?method=handleWidget"</span>/&gt;</span>
       <span class="code-tag">&lt;/filter&gt;</span>
    <span class="code-tag">&lt;/from&gt;</span>
</pre>
</div></div></td></tr></table></div> 

<h4><a name="Simple-Usingand%2For"></a>Using and / or</h4>
<p>If you have two expressions you can combine them with the <tt>and</tt>
or <tt>or</tt> operator.<br/>
For instance:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
simple(<span class="code-quote">"${in.header.title} contains 'Camel' and ${in.header.type'}
== 'gold'"</span>)
</pre>
</div></div>

<p>And of course the <tt>or</tt> is also supported. The sample example would
be:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
simple(<span class="code-quote">"${in.header.title} contains 'Camel' or ${in.header.type'}
== 'gold'"</span>)
</pre>
</div></div>

<p><b>Notice:</b> Currently <tt>and</tt> or <tt>or</tt>
can only be used <b>once</b> in a simple language expression. This might change
in the future.<br/>
So you <b>cannot</b> do:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
simple(<span class="code-quote">"${in.header.title} contains 'Camel' and ${in.header.type'}
== 'gold' and ${in.header.number} range 100..200"</span>)
</pre>
</div></div>

<h3><a name="Simple-Samples"></a>Samples</h3>
<p>In the Spring XML sample below we filter based on a header value:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
    <span class="code-tag">&lt;from uri=<span class="code-quote">"seda:orders"</span>&gt;</span>
       <span class="code-tag">&lt;filter&gt;</span>
           <span class="code-tag">&lt;simple&gt;</span>in.header.foo<span
class="code-tag">&lt;/simple&gt;</span>
           <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:fooOrders"</span>/&gt;</span>
       <span class="code-tag">&lt;/filter&gt;</span>
    <span class="code-tag">&lt;/from&gt;</span>
</pre>
</div></div>

<p>The Simple language can be used for the predicate test above in the <a href="/confluence/display/CAMEL/Message+Filter"
title="Message Filter">Message Filter</a> pattern, where we test if the in message
has a <tt>foo</tt> header (a header with the key <tt>foo</tt> exists).
If the expression evaluates to <b>true</b> then the message is routed to the <tt>mock:foo</tt>
endpoint, otherwise its lost in the deep blue sea <img class="emoticon" src="/confluence/images/icons/emoticons/wink.gif"
height="20" width="20" align="absmiddle" alt="" border="0"/>.</p>

<p>The same example in Java DSL:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
    from(<span class="code-quote">"seda:orders"</span>)
        .filter().simple(<span class="code-quote">"in.header.foo"</span>).to(<span
class="code-quote">"seda:fooOrders"</span>);
</pre>
</div></div>

<p>You can also use the simple language for simple text concatenations such as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
   from(<span class="code-quote">"direct:hello"</span>).transform().simple(<span
class="code-quote">"Hello ${in.header.user} how are you?"</span>).to(<span class="code-quote">"mock:reply"</span>);
</pre>
</div></div>
<p>Notice that we must use ${ } placeholders in the expression now to let Camel be able
to parse it correctly. </p>

<p>And this sample uses the date command to output current date.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
   from(<span class="code-quote">"direct:hello"</span>).transform().simple(<span
class="code-quote">"The today is ${date:now:yyyyMMdd} and its a great day."</span>).to(<span
class="code-quote">"mock:reply"</span>);
</pre>
</div></div>

<p>And in the sample below we invoke the bean language to invoke a method on a bean
to be included in the returned string:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
   from(<span class="code-quote">"direct:order"</span>).transform().simple(<span
class="code-quote">"OrderId: ${bean:orderIdGenerator}"</span>).to(<span class="code-quote">"mock:reply"</span>);
</pre>
</div></div>
<p>Where <tt>orderIdGenerator</tt> is the id of the bean registered in the
<a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>. If
using Spring then its the Spring bean id.</p>

<p>If we want to declare which method to invoke on the order id generator bean we must
prepend <tt>.method name</tt> such as below where we invoke the <tt>generateId</tt>
method.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
   from(<span class="code-quote">"direct:order"</span>).transform().simple(<span
class="code-quote">"OrderId: ${bean:orderIdGenerator.generateId}"</span>).to(<span
class="code-quote">"mock:reply"</span>);
</pre>
</div></div>

<p>And in Camel 2.0 we can use the <tt>?method=methodname</tt> option that
we are familiar with the <a href="/confluence/display/CAMEL/Bean" title="Bean">Bean</a>
component itself:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
   from(<span class="code-quote">"direct:order"</span>).transform().simple(<span
class="code-quote">"OrderId: ${bean:orderIdGenerator?method=generateId}"</span>).to(<span
class="code-quote">"mock:reply"</span>);
</pre>
</div></div>

<p>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:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
  <span class="code-tag">&lt;transform&gt;</span>
    <span class="code-tag">&lt;simple&gt;</span>Hello ${bodyAs(String)}
how are you?<span class="code-tag">&lt;/simple&gt;</span>
  <span class="code-tag">&lt;/transform&gt;</span>
</pre>
</div></div>

<p>There is a few types which have a shorthand notation, hence why we can use <tt>String</tt>
instead of <tt>java.lang.String</tt>. These are: <tt>byte[], String, Integer,
Long</tt>. All other types must use their FQN name, e.g. <tt>org.w3c.dom.Document</tt>.</p>

<p>Its also possible to lookup a value from a header <tt>Map</tt> in <b>Camel
2.3</b> onwards:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
  <span class="code-tag">&lt;transform&gt;</span>
    <span class="code-tag">&lt;simple&gt;</span>The gold value is ${header.type[gold]}<span
class="code-tag">&lt;/simple&gt;</span>
  <span class="code-tag">&lt;/transform&gt;</span>
</pre>
</div></div>

<p>In the code above we lookup the header with name <tt>type</tt> and regard
it as a <tt>java.util.Map</tt> and we then lookup with the key <tt>gold</tt>
and return the value.<br/>
If the header is not convertible to Map an exception is thrown. If the header with name <tt>type</tt>
does not exists <tt>null</tt> is returned.</p>

<h3><a name="Simple-Dependencies"></a>Dependencies</h3>
<p>The Bean language is part of <b>camel-core</b>.</p>
     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Simple">View Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=70160&revisedVersion=34&originalVersion=33">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Simple?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message