camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Claus Ibsen (Confluence)" <conflue...@apache.org>
Subject [CONF] Apache Camel > Scripting Languages Context
Date Fri, 06 Sep 2013 07:55:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/en/2176/1/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/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 (3)</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" >| 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> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">h3.
Attributes <br> <br>You can add your own attributes with the {{attribute(name,
value)}} DSL method, such as: <br> <br>In the sample below we add an attribute
{{user}} 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. <br>{code:java} <br>from(&quot;direct:in&quot;).setHeader(&quot;name&quot;).groovy(&quot;&#39;$user.firstName
$user.lastName&#39;&quot;).attribute(&quot;user&quot;, myUser).to(&quot;seda:users&quot;);
<br>{code} <br> <br>h3. Any scripting language <br>Camel can run any
JSR-223 scripting languages using the {{script}} DSL method such as: <br>{code:java}
<br>from(&quot;direct:in&quot;).setHeader(&quot;firstName&quot;).script(&quot;jaskel&quot;,
&quot;user.firstName&quot;).attribute(&quot;user&quot;, myUser).to(&quot;seda:users&quot;);
<br>{code} <br> <br>This is a bit different using the Spring DSL where you
use the *expression* element that doesn&#39;t support setting attributes (yet): <br>{code:xml}
<br>    &lt;from uri=&quot;direct:in&quot;/&gt; <br>    &lt;setHeader
headerName=&quot;firstName&quot;&gt; <br>        &lt;language language=&quot;jaskel&quot;&gt;user.firstName&lt;/language&gt;
<br>    &lt;/setHeader&gt; <br>    &lt;to uri=&quot;seda:users&quot;/&gt;
<br>{code} <br> <br>You can also use predicates e.g. in a Filter: <br>{code:xml}
<br>    &lt;filter&gt; <br>        &lt;language language=&quot;beanshell&quot;&gt;request.getHeaders().get(&quot;Foo&quot;).equals(&quot;Bar&quot;)&lt;/language&gt;
<br>        &lt;to uri=&quot;direct:next&quot; /&gt; <br>    &lt;/filter&gt;
<br>{code} <br> <br></td></tr>
            <tr><td class="diff-unchanged" >See [Scripting Languages] for the
list of languages with explicit DSL support. <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">Some
languages without specific DSL support but known to work with these generic methods include:
<br>||Language||Implementation||language=&quot;...&quot; value|| <br>|BeanShell|[BeanShell
2.0b5|http://svn.apache.org/repos/asf/servicemix/m2-repo/org/beanshell/bsh/2.0b5/bsh-2.0b5.jar]|{{beanshell}}
or {{bsh}}| <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h3. Additional arguments to ScriptingEngine
<br>*Available as of Camel 2.8* <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >If you use maven you could just add
the following to your pom.xml, substituting the version number for the latest &amp; greatest
release (see [the download page for the latest versions|Download]). <br> <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words">{code<span
class="diff-added-chars"style="background-color: #dfd;">:xml</span>}</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >&lt;dependency&gt; <br>
 &lt;groupId&gt;org.apache.camel&lt;/groupId&gt; <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>


<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>

<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">
<script type="syntaxhighlighter" class="theme: Default; brush: java; gutter: false"><![CDATA[
public void testArgumentsExample() throws Exception {
    if (!ScriptTestHelper.canRunTestOnThisPlatform()) {
        return;
    }

    getMockEndpoint("mock:result").expectedMessageCount(0);
    getMockEndpoint("mock:unmatched").expectedMessageCount(1);

    // additional arguments to ScriptEngine
    Map&lt;String, Object&gt; arguments = new HashMap&lt;String, Object&gt;();
    arguments.put("foo", "bar");
    arguments.put("baz", 7);

    // those additional arguments is provided as a header on the Camel Message
    template.sendBodyAndHeader("direct:start", "hello", ScriptBuilder.ARGUMENTS, arguments);

    assertMockEndpointsSatisfied();
}
]]></script>
</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="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
.setHeader("myHeader").groovy("context.resolvePropertyPlaceholders('{{' + request.headers.get('foo')
+ '}}')")
</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="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
.setHeader("myHeader").groovy("properties.resolve(request.headers.get('foo'))")
</pre>
</div></div>


<h3><a name="ScriptingLanguagesContext-Loadingscriptfromexternalresource"></a>Loading
script from external resource</h3>
<p><b>Available as of Camel 2.11</b></p>

<p>You can externalize the script and have Camel load it from a resource such as <tt>"classpath:"</tt>,
<tt>"file:"</tt>, or <tt>"http:"</tt>.<br/>
This is done using the following syntax: <tt>"resource:scheme:location"</tt>,
eg to refer to a file on the classpath you can do:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
.setHeader("myHeader").groovy("resource:classpath:mygroovy.groovy")
</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 &amp; 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="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
&lt;dependency&gt;
  &lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
  &lt;artifactId&gt;camel-script&lt;/artifactId&gt;
  &lt;version&gt;x.x.x&lt;/version&gt;
&lt;/dependency&gt;
</pre>
</div></div>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;" class="grey">
                        <a href="https://cwiki.apache.org/confluence/users/removespacenotification.action?spaceKey=CAMEL">Stop
watching space</a>
            <span style="padding: 0px 5px;">|</span>
                <a href="https://cwiki.apache.org/confluence/users/editmyemailsettings.action">Change
email 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=15&originalVersion=14">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Scripting+Languages+Context?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message