<html>
<head>
<base href="https://cwiki.apache.org/confluence">
<link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&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/Scripting+Languages+Context">Scripting
Languages Context</a></h2>
<h4>Page <b>edited</b> by <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
</h4>
<br/>
<h4>Changes (2)</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" >| request | {{org.apache.camel.Message}}
| The IN message | <br>| response | {{org.apache.camel.Message}} | The OUT message |
<br></td></tr>
<tr><td class="diff-added-lines" style="background-color: #dfd;">|
properties | {{org.apache.camel.builder.script.PropertiesFunction}} | *Camel 2.9:* Function
with a {{resolve}} method to make it easier to use Camels [Properties] component from scripts.
See further below for example. | <br></td></tr>
<tr><td class="diff-unchanged" > <br></td></tr>
<tr><td class="diff-unchanged" >h3. Attributes <br> <br></td></tr>
<tr><td class="diff-snipped" >...<br></td></tr>
<tr><td class="diff-unchanged" > <br> <br></td></tr>
<tr><td class="diff-added-lines" style="background-color: #dfd;">h3.
Using properties function <br>*Available as of Camel 2.9* <br> <br>If you
need to use the [Properties] component from a script to lookup property placeholders, then
its a bit cumbersome to do so. <br>For example to set a header name myHeader with a
value from a property placeholder, which key is provided in a header named "foo".
<br>{code} <br>.setHeader("myHeader").groovy("context.resolvePropertyPlaceholders('{{'
+ request.headers.get('foo') + '}}')") <br>{code}
<br> <br>From Camel 2.9 onwards you can now use the properties function and the
same example is simpler: <br>{code} <br>.setHeader("myHeader").groovy("properties.resolve(request.headers.get('foo'))")
<br>{code} <br> <br></td></tr>
<tr><td class="diff-unchanged" >h3. Dependencies <br> <br></td></tr>
<tr><td class="diff-snipped" >...<br></td></tr>
</table>
</div> <h4>Full Content</h4>
<div class="notificationGreySide">
<h3><a name="ScriptingLanguagesContext-ScriptContext"></a>ScriptContext</h3>
<p>The JSR-223 scripting languages ScriptContext is pre configured with the following
attributes all set at <tt>ENGINE_SCOPE</tt>:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Attribute </th>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'> Value </th>
</tr>
<tr>
<td class='confluenceTd'> context </td>
<td class='confluenceTd'> <tt>org.apache.camel.CamelContext</tt> </td>
<td class='confluenceTd'> The Camel Context </td>
</tr>
<tr>
<td class='confluenceTd'> exchange </td>
<td class='confluenceTd'> <tt>org.apache.camel.Exchange</tt> </td>
<td class='confluenceTd'> The current Exchange </td>
</tr>
<tr>
<td class='confluenceTd'> request </td>
<td class='confluenceTd'> <tt>org.apache.camel.Message</tt> </td>
<td class='confluenceTd'> The IN message </td>
</tr>
<tr>
<td class='confluenceTd'> response </td>
<td class='confluenceTd'> <tt>org.apache.camel.Message</tt> </td>
<td class='confluenceTd'> The OUT message </td>
</tr>
<tr>
<td class='confluenceTd'> properties </td>
<td class='confluenceTd'> <tt>org.apache.camel.builder.script.PropertiesFunction</tt>
</td>
<td class='confluenceTd'> <b>Camel 2.9:</b> Function with a <tt>resolve</tt>
method to make it easier to use Camels <a href="/confluence/display/CAMEL/Properties" title="Properties">Properties</a>
component from scripts. See further below for example. </td>
</tr>
</tbody></table>
</div>
<h3><a name="ScriptingLanguagesContext-Attributes"></a>Attributes</h3>
<p>You can add your own attributes with the <tt>attribute(name, value)</tt>
DSL method, such as:</p>
<p>In the sample below we add an attribute <tt>user</tt> that is an object
we already have instantiated as myUser. This object has a getFirstName() method that we want
to set as header on the message. We use the groovy language to concat the first and last name
into a single string that is returned.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:in"</span>).setHeader(<span class="code-quote">"name"</span>).groovy(<span
class="code-quote">"'$user.firstName $user.lastName'"</span>).attribute(<span
class="code-quote">"user"</span>, myUser).to(<span class="code-quote">"seda:users"</span>);
</pre>
</div></div>
<h3><a name="ScriptingLanguagesContext-Anyscriptinglanguage"></a>Any scripting
language</h3>
<p>Camel can run any JSR-223 scripting languages using the <tt>script</tt>
DSL method 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:in"</span>).setHeader(<span class="code-quote">"firstName"</span>).script(<span
class="code-quote">"jaskel"</span>, <span class="code-quote">"user.firstName"</span>).attribute(<span
class="code-quote">"user"</span>, myUser).to(<span class="code-quote">"seda:users"</span>);
</pre>
</div></div>
<p>This is a bit different using the Spring DSL where you use the <b>expression</b>
element that doesn't support setting attributes (yet):</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag"><from uri=<span class="code-quote">"direct:in"</span>/></span>
<span class="code-tag"><setHeader headerName=<span class="code-quote">"firstName"</span>></span>
<span class="code-tag"><expression language=<span class="code-quote">"jaskel"</span>></span>user.firstName<span
class="code-tag"></expression></span>
<span class="code-tag"></setHeader></span>
<span class="code-tag"><to uri=<span class="code-quote">"seda:users"</span>/></span>
</pre>
</div></div>
<p>You can also use predicates e.g. in a Filter:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag"><filter></span>
<span class="code-tag"><language language=<span class="code-quote">"beanshell"</span>></span>request.getHeaders().get(<span
class="code-quote">"Foo"</span>).equals(<span class="code-quote">"Bar"</span>)<span
class="code-tag"></language></span>
<span class="code-tag"><to uri=<span class="code-quote">"direct:next"</span>
/></span>
<span class="code-tag"></filter></span>
</pre>
</div></div>
<p>See <a href="/confluence/display/CAMEL/Scripting+Languages" title="Scripting Languages">Scripting
Languages</a> for the list of languages with explicit DSL support.</p>
<p>Some languages without specific DSL support but known to work with these generic
methods include:</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Language</th>
<th class='confluenceTh'>Implementation</th>
<th class='confluenceTh'>language="..." value</th>
</tr>
<tr>
<td class='confluenceTd'>BeanShell</td>
<td class='confluenceTd'><a href="http://svn.apache.org/repos/asf/servicemix/m2-repo/org/beanshell/bsh/2.0b5/bsh-2.0b5.jar"
class="external-link" rel="nofollow">BeanShell 2.0b5</a></td>
<td class='confluenceTd'><tt>beanshell</tt> or <tt>bsh</tt></td>
</tr>
</tbody></table>
</div>
<h3><a name="ScriptingLanguagesContext-AdditionalargumentstoScriptingEngine"></a>Additional
arguments to ScriptingEngine</h3>
<p><b>Available as of Camel 2.8</b></p>
<p>You can provide additional arguments to the <tt>ScriptingEngine</tt>
using a header on the Camel message with the key <tt>CamelScriptArguments</tt>.<br/>
See this example:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> void testArgumentsExample()
<span class="code-keyword">throws</span> Exception {
<span class="code-keyword">if</span> (!ScriptTestHelper.canRunTestOnThisPlatform())
{
<span class="code-keyword">return</span>;
}
getMockEndpoint(<span class="code-quote">"mock:result"</span>).expectedMessageCount(0);
getMockEndpoint(<span class="code-quote">"mock:unmatched"</span>).expectedMessageCount(1);
<span class="code-comment">// additional arguments to ScriptEngine
</span> Map<<span class="code-object">String</span>, <span
class="code-object">Object</span>> arguments = <span class="code-keyword">new</span>
HashMap<<span class="code-object">String</span>, <span class="code-object">Object</span>>();
arguments.put(<span class="code-quote">"foo"</span>, <span class="code-quote">"bar"</span>);
arguments.put(<span class="code-quote">"baz"</span>, 7);
<span class="code-comment">// those additional arguments is provided as a header
on the Camel Message
</span> template.sendBodyAndHeader(<span class="code-quote">"direct:start"</span>,
<span class="code-quote">"hello"</span>, ScriptBuilder.ARGUMENTS, arguments);
assertMockEndpointsSatisfied();
}
</pre>
</div></div>
<h3><a name="ScriptingLanguagesContext-Usingpropertiesfunction"></a>Using
properties function</h3>
<p><b>Available as of Camel 2.9</b></p>
<p>If you need to use the <a href="/confluence/display/CAMEL/Properties" title="Properties">Properties</a>
component from a script to lookup property placeholders, then its a bit cumbersome to do so.<br/>
For example to set a header name myHeader with a value from a property placeholder, which
key is provided in a header named "foo".</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
.setHeader(<span class="code-quote">"myHeader"</span>).groovy(<span class="code-quote">"context.resolvePropertyPlaceholders('{{'
+ request.headers.get('foo') + '}}')"</span>)
</pre>
</div></div>
<p>From Camel 2.9 onwards you can now use the properties function and the same example
is simpler:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
.setHeader(<span class="code-quote">"myHeader"</span>).groovy(<span class="code-quote">"properties.resolve(request.headers.get('foo'))"</span>)
</pre>
</div></div>
<h3><a name="ScriptingLanguagesContext-Dependencies"></a>Dependencies</h3>
<p>To use scripting languages in your camel routes you need to add the a dependency
on <b>camel-script</b> which integrates the JSR-223 scripting engine. </p>
<p>If you use maven you could just add the following to your pom.xml, substituting the
version number for the latest & greatest release (see <a href="/confluence/display/CAMEL/Download"
title="Download">the download page for the latest versions</a>).</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-script</artifactId>
<version>x.x.x</version>
</dependency>
</pre>
</div></div>
</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/Scripting+Languages+Context">View
Online</a>
|
<a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=97905&revisedVersion=12&originalVersion=11">View
Changes</a>
|
<a href="https://cwiki.apache.org/confluence/display/CAMEL/Scripting+Languages+Context?showComments=true&showCommentArea=true#addcomment">Add
Comment</a>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
|