It was a tough question. I needed to go through the source files in org.restlet.ext.Freemarker package - Phew!
Here you can do it
If you need to create your OWN configuration object, set "templateLoader" to use, and then run "TemplateRepresentation" to render:
Configuration cfg = new Configuration(); ContextTemplateLoader loader = new ContextTemplateLoader(getContext(),"war:///WEB-INF"); cfg.setTemplateLoader(loader); TemplateRepresentation rep = null; Mail mail = new Mail(); //The data object you wish to populate - example from Restlet itself mail.setStatus("received"); mail.setSubject("Message to self"); mail.setContent("Doh!"); mail.setAccountRef(new Reference(getReference(), "..").getTargetRef() .toString()); Map<String, Object> data = new HashMap<String, Object>(); data.put("status", mail.getStatus()); data.put("subject", mail.getSubject()); data.put("content", mail.getContent()); data.put("accountRef", mail.getAccountRef()); rep = new TemplateRepresentation("Mail.ftl", cfg, data, MediaType.TEXT_HTML); return rep;
If you are satisfied by default and want to use the class loader based on template loading
//Load the FreeMarker template Representation mailFtl = new ClientResource( LocalReference.createClapReference(getClass().getPackage()) + "/Mail.ftl").get(); //Wraps the bean with a FreeMarker representation return new TemplateRepresentation(mailFtl, mail, MediaType.TEXT_HTML);
If you want to initialize the configuration object once and set the template by calling the setServletContextForTemplateLoading (...) method on the configuration object. You can always do this in ServletContextListener
public class Config implements ServletContextListener { private static Configuration cfg = new Configuration(); @Override public void contextInitialized(ServletContextEvent sce) { ServletContext sc = sce.getServletContext(); cfg.setServletContextForTemplateLoading(sc, "/WEB-INF"); } public static Configuration getFMConfig() { return cfg; } }
And then call static getFMConfig () and pass it to TemplateRepresentation, as in 1
Notes:
- If you receive an unsupported Exception protocol, this will be in case 2. It is assumed that ServerResource does not know which protocol to use to access the file - it will be the CLAP protocol for Restlet. You may need to configure init-params for RestletServlet in the web.xml file and have CLAP as one of the parameters
- TemplateRepresentation has many constructors - if you do not pass the configuration object during the instantiation (using another overloaded constructor), it will create a new configuration for you (). Thus, you do not need to configure the configuration as in 2 (this may seem obvious to you, but I assumed that you still need to install the configuration or it will “take it from somewhere”).
- If you want to create your OWN configuration, you MUST pass it to one of the constructors
- Take a look at the line "war: ///" in the ContextTemplateLoader constructor in 1. This is important . No where is mentioned what should be a reference to this baseUri link, even in Docs. I tried for quite some time to understand that this should be "war: ///", followed by the name of the folder in which the templates are stored.
- For case 2, you probably have to store the templates in the same package as the class file from which this code is accessed. If you see carefully, you will see the LocalReference parameter as an argument to ClientResource, which says that the resource must be locally present and therefore you need to use the user-specific CLAP protocol (access class access protocol)
A personal point of disappointment is why this is not yet explained in the documentation or in any case :)
Hope this helps someone who stumbled upon this post! Phew!
source share