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