camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Language
Date Thu, 31 Jan 2013 12:32: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/Language">Language</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" >{code} <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">And
from Camel 2.11 onwards you can refer to an external resource for the script using same notation
as supported by the other [Language]s in Camel <br>{code} <br>language://languageName:resource:scheme:location][?options]
<br>{code} <br> <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h3. URI Options <br>The component
supports the following options.  <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/component/language/LanguageLoadScriptFromFileUpdateTest.java}
<br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">From
*Camel 2.11* onwards you can refer to the resource similar to the other [Language]s in Camel
by prefixing with {{&quot;resource:&quot;}} as shown below: <br>{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/component/language/LanguageResourceLoadScriptFromClasspathTest.java}
<br> <br></td></tr>
            <tr><td class="diff-unchanged" > <br>{include:Endpoint See Also}
<br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="Language-Language"></a>Language</h2>
<p><b>Available as of Camel 2.5</b></p>

<p>The language component allows you to send <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> to an endpoint which executes a script by any of the
supported <a href="/confluence/display/CAMEL/Languages" title="Languages">Languages</a>
in Camel.<br/>
By having a component to execute language scripts, it allows more dynamic routing capabilities.
For example by using the <a href="/confluence/display/CAMEL/Routing+Slip" title="Routing
Slip">Routing Slip</a> or <a href="/confluence/display/CAMEL/Dynamic+Router" title="Dynamic
Router">Dynamic Router</a> EIPs you can send messages to <tt>language</tt>
endpoints where the script is dynamic defined as well.</p>

<p>This component is provided out of the box in <tt>camel-core</tt> and
hence no additional JARs is needed. You only have to include additional Camel components if
the language of choice mandates it, such as using <a href="/confluence/display/CAMEL/Groovy"
title="Groovy">Groovy</a> or <a href="/confluence/display/CAMEL/JavaScript" title="JavaScript">JavaScript</a>
languages.</p>

<h3><a name="Language-URIformat"></a>URI format</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
language:<span class="code-comment">//languageName[:script][?options]</span>
</pre>
</div></div>

<p>And from Camel 2.11 onwards you can refer to an external resource for the script
using same notation as supported by the other <a href="/confluence/display/CAMEL/Language"
title="Language">Language</a>s in Camel</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
language:<span class="code-comment">//languageName:resource:scheme:location][?options]</span>
</pre>
</div></div>


<h3><a name="Language-URIOptions"></a>URI Options</h3>
<p>The component supports the following options. </p>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Default Value </th>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>languageName</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> The name of the <a href="/confluence/display/CAMEL/Languages"
title="Languages">Language</a> to use, such as <tt>simple</tt>, <tt>groovy</tt>,
<tt>javascript</tt> etc. This option is mandatory. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>script</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> The script to execute. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>transform</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> <tt>boolean</tt> </td>
<td class='confluenceTd'> Whether or not the result of the script should be used as
the new message body. By setting to <tt>false</tt> the script is executed but
the result of the script is discarded. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>contentCache</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> <tt>boolean</tt> </td>
<td class='confluenceTd'> <b>Camel 2.9:</b> Whether to cache the script
if loaded from a resource.<br/>
Note: from <b>Camel 2.10.3</b> a cached script can be forced to reload at runtime
via JMX using the clearContentCache operation. </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="Language-MessageHeaders"></a>Message Headers</h3>
<p>The following message headers can be used to affect the behavior of the component</p>

<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Header </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelLanguageScript</tt> </td>
<td class='confluenceTd'> The script to execute provided in the header. Takes precedence
over script configured on the endpoint. </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="Language-Examples"></a>Examples</h3>

<p>For example you can use the <a href="/confluence/display/CAMEL/Simple" title="Simple">Simple</a>
language to <a href="/confluence/display/CAMEL/Message+Translator" title="Message Translator">Message
Translator</a> a message:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-object">String</span> script =
URLEncoder.encode(<span class="code-quote">"Hello ${body}"</span>, <span class="code-quote">"UTF-8"</span>);
from(<span class="code-quote">"direct:start"</span>).to(<span class="code-quote">"language:simple:"</span>
+ script).to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>In case you want to convert the message body type you can do this as well:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-object">String</span> script =
URLEncoder.encode(<span class="code-quote">"${mandatoryBodyAs(<span class="code-object">String</span>)}"</span>,
<span class="code-quote">"UTF-8"</span>);
from(<span class="code-quote">"direct:start"</span>).to(<span class="code-quote">"language:simple:"</span>
+ script).to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>You can also use the <a href="/confluence/display/CAMEL/Groovy" title="Groovy">Groovy</a>
language, such as this example where the input message will by multiplied with 2:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-object">String</span> script =
URLEncoder.encode(<span class="code-quote">"request.body * 2"</span>, <span
class="code-quote">"UTF-8"</span>);
from(<span class="code-quote">"direct:start"</span>).to(<span class="code-quote">"language:groovy:"</span>
+ script).to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>You can also provide the script as a header as shown below. Here we use <a href="/confluence/display/CAMEL/XPath"
title="XPath">XPath</a> language to extract the text from the <tt>&lt;foo&gt;</tt>
tag.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-object">Object</span> out = producer.requestBodyAndHeader(<span
class="code-quote">"language:xpath"</span>, <span class="code-quote">"&lt;foo&gt;Hello
World&lt;/foo&gt;"</span>, Exchange.LANGUAGE_SCRIPT, <span class="code-quote">"/foo/text()"</span>);
assertEquals(<span class="code-quote">"Hello World"</span>, out);
</pre>
</div></div>

<h3><a name="Language-Loadingscriptsfromresources"></a>Loading scripts from
resources</h3>
<p><b>Available as of Camel 2.9</b></p>

<p>You can specify a resource uri for a script to load in either the endpoint uri, or
in the <tt>Exchange.LANGUAGE_SCRIPT</tt> header.<br/>
The uri must start with one of the following schemes: <a href="file:" class="external-link"
rel="nofollow">file:</a>, classpath:, or http:</p>

<p>For example to load a script from the classpath:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>)
    <span class="code-comment">// load the script from the classpath
</span>    .to(<span class="code-quote">"language:simple:classpath:org/apache/camel/component/language/mysimplescript.txt"</span>)
    .to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>By default the script is loaded once and cached. However you can disable the <tt>contentCache</tt>
option and have the script loaded on each evaluation.<br/>
For example if the file myscript.txt is changed on disk, then the updated script is used:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>)
    <span class="code-comment">// the script will be loaded on each message, as we disabled
cache
</span>    .to(<span class="code-quote">"language:simple:file:target/script/myscript.txt?contentCache=<span
class="code-keyword">false</span>"</span>)
    .to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>

<p>From <b>Camel 2.11</b> onwards you can refer to the resource similar
to the other <a href="/confluence/display/CAMEL/Language" title="Language">Language</a>s
in Camel by prefixing with <tt>"resource:"</tt> as shown below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">from(<span class="code-quote">"direct:start"</span>)
    <span class="code-comment">// load the script from the classpath
</span>    .to(<span class="code-quote">"language:simple:resource:classpath:org/apache/camel/component/language/mysimplescript.txt"</span>)
    .to(<span class="code-quote">"mock:result"</span>);
</pre>
</div></div>


<h3><a name="Language-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting
Started</a></li>
</ul>

<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/Languages" title="Languages">Languages</a></li>
	<li><a href="/confluence/display/CAMEL/Routing+Slip" title="Routing Slip">Routing
Slip</a></li>
	<li><a href="/confluence/display/CAMEL/Dynamic+Router" title="Dynamic Router">Dynamic
Router</a></li>
</ul>

    </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/Language">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=23336974&revisedVersion=7&originalVersion=6">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Language?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message