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 > StringTemplate
Date Mon, 30 Sep 2013 13:00: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/StringTemplate">StringTemplate</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <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" >| {{contentCache}} | {{false}} | Cache
for the resource content when its loaded. \\ Note : as of *Camel 2.9* cached resource content
can be cleared via JMX using the endpoint&#39;s {{clearContentCache}} operation. || <br>|
{{delimiterStart}} | {{null}} | *Since Camel 2.11.1*, configuring the variable start delimiter
| <br></td></tr>
            <tr><td class="diff-changed-lines" >| <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">{{delimiterEnd}}</span>
<span class="diff-added-words"style="background-color: #dfd;">{{delimiterStop}}</span>
| {{null}} | *Since Camel 2.11.1*, configuring the variable end delimiter | <br></td></tr>
            <tr><td class="diff-unchanged" >{div} <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="StringTemplate-StringTemplate"></a>String Template</h2>

<p>The <b>string-template:</b> component allows you to process a message
using a <a href="http://www.stringtemplate.org/" class="external-link" rel="nofollow">String
Template</a>. This can be ideal when using <a href="/confluence/display/CAMEL/Templating"
title="Templating">Templating</a> to generate responses for requests.</p>

<p>Maven users will need to add the following dependency to their <tt>pom.xml</tt>
for this component:</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-stringtemplate&lt;/artifactId&gt;
    &lt;version&gt;x.x.x&lt;/version&gt;
    &lt;!-- use the same version as your Camel core version --&gt;
&lt;/dependency&gt;
</pre>
</div></div>

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

<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;">
string-template:templateName[?options]
</pre>
</div></div>

<p>Where <b>templateName</b> is the classpath-local URI of the template
to invoke; or the complete URL of the remote template.</p>

<p>You can append query options to the URI in the following format, <tt>?option=value&amp;option=value&amp;...</tt></p>

<h3><a name="StringTemplate-Options"></a>Options</h3>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Option </th>
<th class='confluenceTh'> Default </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>contentCache</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Cache for the resource content when its loaded. <br class="atl-forced-newline"
/> Note : as of <b>Camel 2.9</b> cached resource content can be cleared via
JMX using the endpoint's <tt>clearContentCache</tt> operation. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>delimiterStart</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Since Camel 2.11.1</b>, configuring the variable
start delimiter </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>delimiterStop</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Since Camel 2.11.1</b>, configuring the variable
end delimiter </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="StringTemplate-Headers"></a>Headers</h3>
<p>Camel will store a reference to the resource in the message header with key, <tt>org.apache.camel.stringtemplate.resource</tt>.
The Resource is an <tt>org.springframework.core.io.Resource</tt> object.</p>

<h3><a name="StringTemplate-Hotreloading"></a>Hot reloading</h3>
<p>The string template resource is by default hot-reloadable for both file and classpath
resources (expanded jar). If you set <tt>contentCache=true</tt>, Camel loads the
resource only once and hot-reloading is not possible. This scenario can be used in production
when the resource never changes.</p>

<h3><a name="StringTemplate-StringTemplateAttributes"></a>StringTemplate
Attributes</h3>
<p>Camel will provide exchange information as attributes (just a <tt>java.util.Map</tt>)
to the string template. The Exchange is transfered as:</p>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> key </th>
<th class='confluenceTh'> value </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>exchange</tt> </td>
<td class='confluenceTd'> The Exchange itself. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>headers</tt> </td>
<td class='confluenceTd'> The headers of the In message. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>camelContext</tt> </td>
<td class='confluenceTd'> The Camel Context. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>request</tt> </td>
<td class='confluenceTd'> The In message. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>in</tt> </td>
<td class='confluenceTd'> The In message. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>body</tt> </td>
<td class='confluenceTd'> The In message body. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>out</tt> </td>
<td class='confluenceTd'> The Out message (only for InOut message exchange pattern).
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>response</tt> </td>
<td class='confluenceTd'> The Out message (only for InOut message exchange pattern).
</td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="StringTemplate-Samples"></a>Samples</h3>

<p>For example you could use a string template as follows in order to formulate a response
to a message:</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;">
from("activemq:My.Queue").
  to("string-template:com/acme/MyResponse.tm");
</pre>
</div></div>

<h3><a name="StringTemplate-TheEmailSample"></a>The Email Sample</h3>
<p>In this sample we want to use a string template to send an order confirmation email.
The email template is laid out in <tt>StringTemplate</tt> as:<br/>
This example works for <b>camel 2.11.0</b>. If your camel version is less than
2.11.0, the variables should be started and ended with $.</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;">
Dear &lt;headers.lastName&gt;, &lt;headers.firstName&gt;

Thanks for the order of &lt;headers.item&gt;.

Regards Camel Riders Bookstore
&lt;body&gt;
</pre>
</div></div>

<p>And the java code is as follows:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="theme: Default; brush: java; gutter: false"><![CDATA[
private Exchange createLetter() {
    Exchange exchange = context.getEndpoint("direct:a").createExchange();
    Message msg = exchange.getIn();
    msg.setHeader("firstName", "Claus");
    msg.setHeader("lastName", "Ibsen");
    msg.setHeader("item", "Camel in Action");
    msg.setBody("PS: Next beer is on me, James");
    return exchange;
}

@Test
public void testVelocityLetter() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    mock.expectedBodiesReceived("Dear Ibsen, Claus! Thanks for the order of Camel in Action.
Regards Camel Riders Bookstore PS: Next beer is on me, James");

    template.send("direct:a", createLetter());

    mock.assertIsSatisfied();
}

protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {
        public void configure() throws Exception {
            from("direct:a").to("string-template:org/apache/camel/component/stringtemplate/letter.tm").to("mock:result");
        }
    };
}
]]></script>
</div></div>

<h3><a name="StringTemplate-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>

    </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/StringTemplate">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=67648&revisedVersion=22&originalVersion=21">View
Changes</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message