PageRenderTime 18ms CodeModel.GetById 10ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/scalate-website/src/camel.page

http://github.com/scalate/scalate
Visualforce Page | 135 lines | 98 code | 37 blank | 0 comment | 0 complexity | e498d1ccd9124fba2c015ab9021b1d40 MD5 | raw file
  1---
  2title: Scalate Camel Component
  3in_menu: true
  4sort_info: 5
  5--- name:overview
  6# Scalate Camel Component
  7
  8--- name:content pipeline:conf
  9
 10h2. Scalate Camel Component
 11
 12The *scalate:* [camel|http://camel.apache.org/] component allows you to process a message using [Scalate|http://scalate.fusesource.org/] template, which supports various template languages such as
 13
 14* [SSP|http://scalate.fusesource.org/documentation/ssp-reference.html] for text generation using a JSP / Velocity like syntax
 15* [Jade|http://scalate.fusesource.org/documentation/jade.html] for generating HTML markup using a really DRY and concise template engine
 16
 17This can be ideal when using [Templating|http://camel.apache.org/templating.html] to generate responses for requests.
 18
 19Maven users will need to add the following dependency to their {{pom.xml}} for this component, and use the correct version of Scalate:
 20
 21{pygmentize:lang=xml}
 22<dependency>
 23    <groupId>org.fusesource.scalate</groupId>
 24    <artifactId>scalate-camel_${scala_compat_tag}</artifactId>
 25    <version>${scalate.version}</version>
 26</dependency>
 27{pygmentize}
 28
 29h3. URI format
 30
 31{pygmentize:lang=xml}
 32scalate:templateName[?options]
 33{pygmentize}
 34
 35Where *templateName* is the classpath-local URI of the template to invoke; or the complete URL of the remote template (eg: file://folder/myfile.ssp).
 36
 37You can append query options to the URI in the following format, {{?option=value&option=value&...}}
 38
 39h3. Message Headers
 40
 41The scalate component sets a couple headers on the message (you can't set these yourself and from Camel 2.1 scalate component will not set these headers which will cause some side effect on the dynamic template support):
 42{div:class=confluenceTableSmall}
 43|| Header || Description ||
 44| {{CamelScalateResource}} | The resource as an {{org.springframework.core.io.Resource}} object. |
 45| {{CamelScalateResourceUri}} | The *templateName* as a {{String}} object. |
 46{div}
 47
 48Headers set during the Scalate evaluation are returned to the message and added as headers. Then its kinda possible to return values from Scalate to the Message.
 49
 50For example, to set the header value of {{fruit}} in the Scalate template {{.tm}}:
 51
 52{pygmentize:lang=ssp}
 53<% in.setHeader('fruit', 'Apple') %>
 54{pygmentize}
 55
 56The {{fruit}} header is now accessible from the {{message.out.headers}}.
 57
 58h3. Scalate Context
 59Camel will provide exchange information in the Scalate context (just a {{Map}}). The {{Exchange}} is transfered as:
 60{div:class=confluenceTableSmall}
 61|| key || value ||
 62| {{exchange}} | The {{Exchange}} itself. |
 63| {{headers}} | The headers of the In message. |
 64| {{camelContext}} | The Camel Context intance. |
 65| {{request}} | The In message. |
 66| {{in}} | The In message. |
 67| {{body}} | The In message body. | 
 68| {{out}} | The Out message (only for InOut message exchange pattern). |
 69| {{response}} | The Out message (only for InOut message exchange pattern). |
 70{div}
 71
 72h3. Hot reloading
 73The Scalate template resource is, by default, hot reloadable for both file and classpath resources (expanded jar). 
 74
 75h3. Dynamic templates
 76
 77Camel 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.
 78{div:class=confluenceTableSmall}
 79|| Header || Type || Description ||
 80| CamelScalateResourceUri | String | An URI for the template resource to use instead of the endpoint configured. |
 81| CamelScalateTemplate | String | The template to use instead of the endpoint configured. |
 82{div}
 83
 84h3. Samples
 85
 86For example you could use something like
 87
 88{pygmentize:lang=java}
 89from("activemq:My.Queue").
 90  to("scalate:com/acme/MyResponse.ssp");
 91{pygmentize}
 92
 93To use a Scalate template to formulate a response to a message for InOut message exchanges (where there is a {{JMSReplyTo}} header).
 94
 95If you want to use InOnly and consume the message and send it to another destination, you could use the following route:
 96
 97{pygmentize:lang=java}
 98from("activemq:My.Queue").
 99  to("scalate:com/acme/MyResponse.scaml").
100  to("activemq:Another.Queue");
101{pygmentize}
102
103It's possible to specify what template the component should use dynamically via a header, so for example:
104
105{pygmentize:lang=java}
106from("direct:in").
107  setHeader("CamelScalateResourceUri").
108    constant("path/to/my/template.scaml").
109  to("scalate:dummy");
110{pygmentize}
111
112It's possible to specify a template directly as a header the component should use dynamically via a header, so for example:
113
114{pygmentize:lang=java}
115from("direct:in").
116  setHeader("CamelScalateTemplate").
117    constant("<%@ attribute body: Object %>\n" + 
118      "Hi this is a scalate template that can do templating ${body}").
119  to("scalate:dummy");
120{pygmentize}
121
122h3. The Email Sample
123In this sample we want to use Scalate templating for an order confirmation email. The email template is laid out in Scalate as:
124{pygmentize:lang=ssp}
125<%@ attribute in: org.apache.camel.scala.RichMessage %>
126
127Dear ${in("firstName")}
128
129Thanks for the order of ${in("item")}.
130
131Regards Camel Riders Bookstore
132${in.body}
133
134{pygmentize}
135