Freemarker
Available as of Camel 1.6
The freemarker: component allows you to process a message using a Freemarker template. This can be ideal when using Templating to generate responses for requests.
URI format
freemarker:templateName[?options]
Where templateName is the classpath-local URI of the template to invoke; or the complete URL of the remote template (eg: file://folder/myfile.ftl).
You can append query options to the URI in the following format, ?option=value&option=value&...
Options
Option |
Default |
Description |
---|---|---|
|
|
Cache for the resource content when its loaded. |
|
|
Character encoding of the resource content. |
Headers
Camel will store a reference to the resource in the message header in the key org.apache.camel.freemarker.resource
. The Resource is an org.springframework.core.io.Resource
object. And the key org.apache.camel.freemarker.resourceUri
holds the templateName as a String object.
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.
An example: Set the header value of fruit
in the Freemarker template:
${request.setHeader('fruit', 'Apple')}
The header, fruit
, is now accessible from the message.out.headers
.
Freemarker Context
Camel will provide exchange information in the Freemarker context (just a Map
). The Exchange
is transfered as:
key |
value |
---|---|
|
The Exchange itself. |
|
The headers of the In message. |
|
The Camel Context. |
|
The In message. |
|
The In message body. |
|
The Out message (only for InOut message exchange pattern). |
Hot reloading
The Freemarker template resource is by default not hot reloadable for both file and classpath resources (expanded jar). If you set contentCache=false
, then Camel will not cache the resource and hot reloading is thus enabled. This scenario can be used in development.
Samples
For example you could use something like:
from("activemq:My.Queue"). to("freemarker:com/acme/MyResponse.ftl");
To use a Freemarker template to formulate a response for a message for InOut message exchanges (where there is a JMSReplyTo
header).
If you want to use InOnly and consume the message and send it to another destination you could use:
from("activemq:My.Queue"). to("freemarker:com/acme/MyResponse.ftl"). to("activemq:Another.Queue");
And to disable the content cache, e.g. for development usage where the .ftl
template should be hot reloaded:
from("activemq:My.Queue"). to("freemarker:com/acme/MyResponse.ftl?contentCache=false"). to("activemq:Another.Queue");
And a file-based resource:
from("activemq:My.Queue"). to("freemarker:file://myfolder/MyResponse.ftl?contentCache=false"). to("activemq:Another.Queue");
The Email Sample
In this sample we want to use Freemarker templating for an order confirmation email. The email template is laid out in Freemarker as:
Dear ${headers.lastName}, ${headers.firstName} Thanks for the order of ${headers.item}. Regards Camel Riders Bookstore ${body}
And the java code: