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 Tue, 27 Mar 2012 12:07:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true" type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/Simple">Simple</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~mark@massfords.com">Mark Ford</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        Expression placeholders ${ } were missing around spring xml example<br />
    </div>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >    &lt;from uri=&quot;seda:orders&quot;&gt; <br>       &lt;filter&gt; <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words">&lt;simple&gt;<span class="diff-added-chars"style="background-color: #dfd;">${</span>in.header.foo<span class="diff-added-chars"style="background-color: #dfd;">}</span>&lt;/simple&gt;</span> <br></td></tr>
            <tr><td class="diff-unchanged" >           &lt;to uri=&quot;mock:fooOrders&quot;/&gt; <br>       &lt;/filter&gt; <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="Simple-SimpleExpressionLanguage"></a>Simple Expression Language</h2>

<p>The Simple Expression Language was a really simple language you can use, but has since grown more powerful. Its primarily intended for being a really small and simple language for evaluating <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a> and <a href="/confluence/display/CAMEL/Predicate" title="Predicate">Predicate</a> 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. Its ideal to cover 95% of the common use cases when you need a little bit of expression based script in your Camel routes.</p>

<p>However for much more complex use cases you are generally recommended to choose a more expressive and powerful language such as: </p>
<ul>
	<li><a href="/confluence/display/CAMEL/JavaScript" title="JavaScript">JavaScript</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><a href="/confluence/display/CAMEL/Mvel" title="Mvel">Mvel</a></li>
	<li><a href="/confluence/display/CAMEL/Groovy" title="Groovy">Groovy</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>

<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>Alternative syntax</b><br />From Camel 2.5 onwards you can also use the alternative syntax which uses $simple{ } as placeholders.<br/>
This can be used in situations to avoid clashes when using for example Spring property placeholder together with Camel.</td></tr></table></div>

<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>Configuring result type</b><br />From Camel 2.8 onwards you can configure the result type of the <a href="/confluence/display/CAMEL/Simple" title="Simple">Simple</a> expression. For example to set the type as a <tt>java.lang.Boolean</tt> or a <tt>java.lang.Integer</tt> etc.</td></tr></table></div>

<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 />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.</td></tr></table></div>

<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>Simple Language Changes in Camel 2.9 onwards</b><br />The <a href="/confluence/display/CAMEL/Simple" title="Simple">Simple</a> language have been improved from Camel 2.9 onwards to use a better syntax parser, which can do index precise error messages, so you know exactly what is wrong and where the problem is. For example if you have made a typo in one of the operators, then previously the parser would not be able to detect this, and cause the evaluation to be true. There is a few changes in the syntax which are no longer backwards compatible. When using <a href="/confluence/display/CAMEL/Simple" title="Simple">Simple</a> language as a <a href="/confluence/display/CAMEL/Predicate" title="Predicate">Predicate</a> then the literal text <b>must</b> be enclosed in either single or double quotes. For example: <tt>"${body} == 'Camel'"</tt>. Notice how we have single quotes around the literal. The old style of using <tt>"body"</tt> and <tt>"header.foo"</tt> to refer to the message body and header is @deprecated, and its encouraged to always use ${ } tokens for the built-in functions.<br/>
The range operator now requires the range to be in single quote as well as shown: <tt>"${header.zip} between '30000..39999'"</tt>.</td></tr></table></div>

<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 functions in the same expression: <tt>"Hello ${in.header.name} this is ${in.header.me} speaking"</tt>.<br/>
However you can <b>not</b> nest functions in Camel 2.8.x or older (i.e. having another ${ } placeholder in an existing, is not allowed).<br/>
>From <b>Camel 2.9</b> onwards you can nest functions.</p>

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

<div class='table-wrap'>
<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'> camelId </td>
<td class='confluenceTd'> String </td>
<td class='confluenceTd'> <b>Camel 2.10:</b> the CamelContext name </td>
</tr>
<tr>
<td class='confluenceTd'> exchangeId </td>
<td class='confluenceTd'> String </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> the exchange id </td>
</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. The converted body can be null. </td>
</tr>
<tr>
<td class='confluenceTd'> mandatoryBodyAs(<em>type</em>) </td>
<td class='confluenceTd'> Type </td>
<td class='confluenceTd'> <b>Camel 2.5:</b> Converts the body to the given type determined by its classname, and expects the body to be not null. </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'> headerAs(<em>key</em>,<em>type</em>) </td>
<td class='confluenceTd'> Type </td>
<td class='confluenceTd'> <b>Camel 2.5:</b> Converts the header to the given type determined by its classname </td>
</tr>
<tr>
<td class='confluenceTd'> headers </td>
<td class='confluenceTd'> Map </td>
<td class='confluenceTd'> <b>Camel 2.9:</b> refer to the input headers </td>
</tr>
<tr>
<td class='confluenceTd'> in.headers </td>
<td class='confluenceTd'> Map </td>
<td class='confluenceTd'> <b>Camel 2.9:</b> refer to the input headers </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'> property.foo.<b>OGNL</b> </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> <b>Camel 2.8:</b> refer to the foo property on the exchange and invoke its value using a Camel OGNL expression. </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 </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> <b>Camel 2.4:</b> Refer to the exception object 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'> exception.<b>OGNL</b> </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> <b>Camel 2.4:</b> Refer to the exchange exception invoked using a Camel OGNL expression object </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'> exception.stacktrace </td>
<td class='confluenceTd'> String </td>
<td class='confluenceTd'> <b>Camel 2.6.</b> Refer to the exception.stracktrace 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>
<tr>
<td class='confluenceTd'> threadName </td>
<td class='confluenceTd'> String </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> Returns the name of the current thread. Can be used for logging purpose. </td>
</tr>
<tr>
<td class='confluenceTd'> ref:xxx </td>
<td class='confluenceTd'> Object </td>
<td class='confluenceTd'> <b>Camel 2.6:</b> To lookup a bean from the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a> with the given id. </td>
</tr>
</tbody></table>
</div>


<h3><a name="Simple-OGNLexpressionsupport"></a>OGNL expression 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 null safe 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>You can access the <tt>Map</tt> or <tt>List</tt> objects directly using their key name (with or without dots) :</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
simple(<span class="code-quote">"${body[foo]}"</span>)
simple(<span class="code-quote">"${body[<span class="code-keyword">this</span>.is.foo]}"</span>)
</pre>
</div></div>

<p>Suppose there was no value with the key <tt>foo</tt> then you can use the null safe 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>There is a special <tt>last</tt> keyword which can be used to get the last value from a list.</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[last]}"</span>)
</pre>
</div></div>

<p>And to get the 2nd last you can subtract a number, so we can use <tt>last-1</tt> to indicate this:</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[last-1]}"</span>)
</pre>
</div></div>

<p>And the 3rd last is of course:</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[last-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 ${ }.</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>Important</b><br />There <b>must</b> be spaces around the operator.</td></tr></table></div>

<p>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 are supported:</p>
<div class='table-wrap'>
<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>. From <b>Camel 2.9</b> onwards the range values must be enclosed in single quotes. </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>. From <b>Camel 2.9</b> onwards the range values must be enclosed in single quotes. </td>
</tr>
</tbody></table>
</div>


<p>And the following unary operators can be used:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Operator </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> &#43;&#43; </td>
<td class='confluenceTd'> <b>Camel 2.9:</b> To increment a number by one. </td>
</tr>
<tr>
<td class='confluenceTd'> &#45;&#45; </td>
<td class='confluenceTd'> <b>Camel 2.9:</b> To decrement a number by one. </td>
</tr>
</tbody></table>
</div>


<p>And the following logical operators can be used to group expressions:</p>
<div class='table-wrap'>
<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'> <b>deprecated</b> use &amp;&amp; instead. The logical and operator is used to group two expressions. </td>
</tr>
<tr>
<td class='confluenceTd'> or </td>
<td class='confluenceTd'> <b>deprecated</b> use &#124;&#124; instead. The logical or operator is used to group two expressions. </td>
</tr>
<tr>
<td class='confluenceTd'> &amp;&amp; </td>
<td class='confluenceTd'> <b>Camel 2.9:</b> The logical and operator is used to group two expressions. </td>
</tr>
<tr>
<td class='confluenceTd'> &#124;&#124; </td>
<td class='confluenceTd'> <b>Camel 2.9:</b> The logical or operator is used to group two expressions. </td>
</tr>
</tbody></table>
</div>


<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>Using and,or operators</b><br />In <b>Camel 2.4 or older</b> the <tt>and</tt> or <tt>or</tt> can only be used <b>once</b> in a simple language expression. From <b>Camel 2.5</b> onwards you can use these operators multiple times.</td></tr></table></div>

<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">// 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>)
</pre>
</div></div>

<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>Comparing with different types</b><br />When you compare with different types such as String and int, then you have to take a bit care. Camel will use the type from the left hand side as 1st priority. And fallback to the right hand side type if both values couldn't be compared based on that type.<br/>
This means you can flip the values to enforce a specific type. Suppose the bar value above is a String. Then you can flip the equation:
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
simple(<span class="code-quote">"100 &lt; ${in.header.bar}"</span>)
</pre>
</div></div>
<p>which then ensures the int type is used as 1st priority.</p>

<p>This may change in the future if the Camel team improves the binary comparison operations to prefer numeric types over String based. It's most often the String type which causes problem when comparing with numbers.</p></td></tr></table></div>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<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 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 are also supported. The range interval requires numbers and both from and end are 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>

<p>From <b>Camel 2.9</b> onwards the range value must be in single quotes</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>


<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 />As the Spring XML does not have all the power as the Java DSL with all its various builder methods, you have 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: 
<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.</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>Camel 2.9 onwards</b><br />Use &amp;&amp; or &#124;&#124; from Camel 2.9 onwards.</td></tr></table></div>

<p>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 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 allow Camel 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 are a few types which have a shorthand notation, so 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 exist <tt>null</tt> is returned.</p>

<p>From Camel 2.9 onwards you can nest functions, such as shown below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;setHeader headerName=<span class="code-quote">"myHeader"</span>&gt;</span>
  <span class="code-tag">&lt;simple&gt;</span>${properties:${header.someKey}}<span class="code-tag">&lt;/simple&gt;</span>
<span class="code-tag">&lt;/setHeader&gt;</span>
</pre>
</div></div>

<h3><a name="Simple-Settingresulttype"></a>Setting result type</h3>
<p><b>Available as of Camel 2.8</b></p>

<p>You can now provide a result type to the <a href="/confluence/display/CAMEL/Simple" title="Simple">Simple</a> expression, which means the result of the evaluation will be converted to the desired type. This is most useable to define types such as booleans, integers, etc.</p>

<p>For example to set a header as a boolean type you can do:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
.setHeader(<span class="code-quote">"cool"</span>, simple(<span class="code-quote">"<span class="code-keyword">true</span>"</span>, <span class="code-object">Boolean</span>.class))
</pre>
</div></div>

<p>And in XML DSL</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
      <span class="code-tag">&lt;setHeader headerName=<span class="code-quote">"cool"</span>&gt;</span>
        <span class="code-tag"><span class="code-comment">&lt;!-- use resultType to indicate that the type should be a java.lang.Boolean --&gt;</span></span>
        <span class="code-tag">&lt;simple resultType=<span class="code-quote">"java.lang.Boolean"</span>&gt;</span>true<span class="code-tag">&lt;/simple&gt;</span>
      <span class="code-tag">&lt;/setHeader&gt;</span>
</pre>
</div></div>

<h3><a name="Simple-Changingfunctionstartandendtokens"></a>Changing function start and end tokens</h3>
<p><b>Available as of Camel 2.9.1</b></p>

<p>You can configure the function start and end tokens - ${ } using the setters <tt>changeFunctionStartToken</tt> and <tt>changeFunctionEndToken</tt> on <tt>SimpleLanguage</tt>, using Java code. From Spring XML you can define a &lt;bean&gt; tag with the new changed tokens in the properties as shown below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
    <span class="code-tag"><span class="code-comment">&lt;!-- configure Simple to use custom prefix/suffix tokens --&gt;</span></span>
    <span class="code-tag">&lt;bean id=<span class="code-quote">"simple"</span> class=<span class="code-quote">"org.apache.camel.language.simple.SimpleLanguage"</span>&gt;</span>
      <span class="code-tag">&lt;property name=<span class="code-quote">"functionStartToken"</span> value=<span class="code-quote">"["</span>/&gt;</span>
      <span class="code-tag">&lt;property name=<span class="code-quote">"functionEndToken"</span> value=<span class="code-quote">"]"</span>/&gt;</span>
    <span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<p>In the example above we use [ ] as the changed tokens.</p>

<p>Notice by changing the start/end token you change those in all the Camel applications which share the same <b>camel-core</b> on their classpath.<br/>
For example in an OSGi server this may affect many applications, where as a Web Application as a WAR file it only affects the Web Application.</p>

<h3><a name="Simple-Dependencies"></a>Dependencies</h3>
<p>The <a href="/confluence/display/CAMEL/Simple" title="Simple">Simple</a> language is part of <b>camel-core</b>.</p>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action" class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Simple">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=70160&revisedVersion=64&originalVersion=63">View Changes</a>
                |
        <a href="https://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