camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > FreeMarker
Date Thu, 28 Apr 2011 11:40: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/FreeMarker">FreeMarker</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~webmaster@fernandoribeiro.eti.br">Fernando
Ribeiro</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" >|| Header || Type || Description ||
Support Version || <br>| FreemarkerConstants.FREEMARKER_RESOURCE | org.springframework.core.io.Resource
| The template resource | &lt;= 1.6.2, &lt;= 2.1 | <br></td></tr>
            <tr><td class="diff-changed-lines" >| FreemarkerConstants.FREEMARKER_RESOURCE_URI
| String | A URI for the template resource to use instead of the endpoint configured. | <span
class="diff-added-words"style="background-color: #dfd;">&gt;= 2.1</span> | <br></td></tr>
            <tr><td class="diff-unchanged" >| FreemarkerConstants.FREEMARKER_TEMPLATE
| String | The template to use instead of the endpoint configured. | &gt;= 2.1 | <br>{div}
<br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="FreeMarker-FreeMarker"></a>FreeMarker</h2>
<p><b>Available as of Camel 1.6</b></p>

<p>The <b>freemarker:</b> component allows you to process a message using
a <a href="http://freemarker.org/" class="external-link" rel="nofollow">FreeMarker</a>
template. 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="code-xml">
<span class="code-tag">&lt;dependency&gt;</span>
    <span class="code-tag">&lt;groupId&gt;</span>org.apache.camel<span
class="code-tag">&lt;/groupId&gt;</span>
    <span class="code-tag">&lt;artifactId&gt;</span>camel-freemarker<span
class="code-tag">&lt;/artifactId&gt;</span>
    <span class="code-tag">&lt;version&gt;</span>x.x.x<span class="code-tag">&lt;/version&gt;</span>
<span class="code-tag"><span class="code-comment">&lt;!-- use the same version
as your Camel core version --&gt;</span></span>
<span class="code-tag">&lt;/dependency&gt;</span>
</pre>
</div></div>

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

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
freemarker: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 (eg: <a href="file://folder/myfile.ftl"
class="external-link" rel="nofollow">file://folder/myfile.ftl</a>).</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="FreeMarker-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>true</tt> </td>
<td class='confluenceTd'> Cache for the resource content when its loaded. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>encoding</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Character encoding of the resource content. </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="FreeMarker-Headers"></a>Headers</h3>

<p>Headers set during the FreeMarker evaluation are returned to the message and added
as headers. Then its kinda possible to return values from FreeMarker to the Message.</p>

<p>An example: Set the header value of <tt>fruit</tt> in the FreeMarker
template:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
${request.setHeader('fruit', 'Apple')}
</pre>
</div></div>

<p>The header, <tt>fruit</tt>, is now accessible from the <tt>message.out.headers</tt>.</p>

<h3><a name="FreeMarker-FreeMarkerContext"></a>FreeMarker Context</h3>
<p>Camel will provide exchange information in the FreeMarker context (just a <tt>Map</tt>).
The <tt>Exchange</tt> 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>body</tt> </td>
<td class='confluenceTd'> The In message body. </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="FreeMarker-Hotreloading"></a>Hot reloading</h3>
<p>The FreeMarker template resource is by default <b>not</b> hot reloadable
for both file and classpath resources (expanded jar). If you set <tt>contentCache=false</tt>,
then Camel will not cache the resource and hot reloading is thus enabled. This scenario can
be used in development.</p>

<h3><a name="FreeMarker-Dynamictemplates"></a>Dynamic templates</h3>

<p>Camel provides two headers by which you can define a different resource location
for a template or the template content itself. If any of these headers is set then Camel uses
this over the endpoint configured resource. This allows you to provide a dynamic template
at runtime.</p>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Header </th>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'> Description </th>
<th class='confluenceTh'> Support Version </th>
</tr>
<tr>
<td class='confluenceTd'> FreemarkerConstants.FREEMARKER_RESOURCE </td>
<td class='confluenceTd'> org.springframework.core.io.Resource </td>
<td class='confluenceTd'> The template resource </td>
<td class='confluenceTd'> &lt;= 1.6.2, &lt;= 2.1 </td>
</tr>
<tr>
<td class='confluenceTd'> FreemarkerConstants.FREEMARKER_RESOURCE_URI </td>
<td class='confluenceTd'> String </td>
<td class='confluenceTd'> A URI for the template resource to use instead of the endpoint
configured. </td>
<td class='confluenceTd'> &gt;= 2.1 </td>
</tr>
<tr>
<td class='confluenceTd'> FreemarkerConstants.FREEMARKER_TEMPLATE </td>
<td class='confluenceTd'> String </td>
<td class='confluenceTd'> The template to use instead of the endpoint configured. </td>
<td class='confluenceTd'> &gt;= 2.1 </td>
</tr>
</tbody></table>
</div>
</div>

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

<p>For example you could use something like:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"activemq:My.Queue"</span>).
  to(<span class="code-quote">"freemarker:com/acme/MyResponse.ftl"</span>);
</pre>
</div></div>

<p>To use a FreeMarker template to formulate a response for a message for InOut message
exchanges (where there is a <tt>JMSReplyTo</tt> header).</p>

<p>If you want to use InOnly and consume the message and send it to another destination
you could use:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"activemq:My.Queue"</span>).
  to(<span class="code-quote">"freemarker:com/acme/MyResponse.ftl"</span>).
  to(<span class="code-quote">"activemq:Another.Queue"</span>);
</pre>
</div></div>

<p>And to disable the content cache, e.g. for development usage where the <tt>.ftl</tt>
template should be hot reloaded:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"activemq:My.Queue"</span>).
  to(<span class="code-quote">"freemarker:com/acme/MyResponse.ftl?contentCache=<span
class="code-keyword">false</span>"</span>).
  to(<span class="code-quote">"activemq:Another.Queue"</span>);
</pre>
</div></div>

<p>And a file-based resource:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"activemq:My.Queue"</span>).
  to(<span class="code-quote">"freemarker:file:<span class="code-comment">//myfolder/MyResponse.ftl?contentCache=<span
class="code-keyword">false</span>"</span>).
</span>  to(<span class="code-quote">"activemq:Another.Queue"</span>);
</pre>
</div></div>

<p>In <b>Camel 2.1</b> it's possible to specify what template the component
should use dynamically via a header, so for example:</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(FreemarkerConstants.FREEMARKER_RESOURCE_URI).constant(<span class="code-quote">"path/to/my/template.ftl"</span>).
  to(<span class="code-quote">"freemarker:dummy"</span>);
</pre>
</div></div>

<h3><a name="FreeMarker-TheEmailSample"></a>The Email Sample</h3>
<p>In this sample we want to use FreeMarker templating for an order confirmation email.
The email template is laid out in FreeMarker as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Dear ${headers.lastName}, ${headers.firstName}

Thanks <span class="code-keyword">for</span> the order of ${headers.item}.

Regards Camel Riders Bookstore
${body}
</pre>
</div></div>

<p>And the java code:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">private</span> Exchange
createLetter() {
    Exchange exchange = context.getEndpoint(<span class="code-quote">"direct:a"</span>).createExchange();

    Message msg = exchange.getIn();
    msg.setHeader(<span class="code-quote">"firstName"</span>, <span class="code-quote">"Claus"</span>);
    msg.setHeader(<span class="code-quote">"lastName"</span>, <span class="code-quote">"Ibsen"</span>);
    msg.setHeader(<span class="code-quote">"item"</span>, <span class="code-quote">"Camel
in Action"</span>);
    msg.setBody(<span class="code-quote">"PS: Next beer is on me, James"</span>);

    <span class="code-keyword">return</span> exchange;
}

@Test
<span class="code-keyword">public</span> void testFreemarkerLetter() <span
class="code-keyword">throws</span> Exception {
    MockEndpoint mock = getMockEndpoint(<span class="code-quote">"mock:result"</span>);
    mock.expectedMessageCount(1);
    mock.expectedBodiesReceived(<span class="code-quote">"Dear Ibsen, Claus\n\nThanks
<span class="code-keyword">for</span> the order of Camel in Action."</span>
            + <span class="code-quote">"\n\nRegards Camel Riders Bookstore\nPS: Next
beer is on me, James"</span>);

    template.send(<span class="code-quote">"direct:a"</span>, createLetter());

    mock.assertIsSatisfied();
}

<span class="code-keyword">protected</span> RouteBuilder createRouteBuilder()
<span class="code-keyword">throws</span> Exception {
    <span class="code-keyword">return</span> <span class="code-keyword">new</span>
RouteBuilder() {
        <span class="code-keyword">public</span> void configure() <span class="code-keyword">throws</span>
Exception {
            from(<span class="code-quote">"direct:a"</span>)
                .to(<span class="code-quote">"freemarker:org/apache/camel/component/freemarker/letter.ftl"</span>)
                .to(<span class="code-quote">"mock:result"</span>);
        }
    };
}
</pre>
</div></div>

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

Mime
View raw message