org.apache.stanbol.contenthub.web.processor
Class FreemarkerViewProcessor

java.lang.Object
  extended by org.apache.stanbol.contenthub.web.processor.FreemarkerViewProcessor
All Implemented Interfaces:
com.sun.jersey.spi.template.ViewProcessor<freemarker.template.Template>

@Singleton
@Provider
public class FreemarkerViewProcessor
extends java.lang.Object
implements com.sun.jersey.spi.template.ViewProcessor<freemarker.template.Template>

Match a Viewable-named view with a Freemarker template. This class is based on the following original implementation: http://github.com/cwinters/jersey-freemarker/

You can configure the location of your templates with the context param 'freemarker.template.path'. If not assigned we'll use a default of WEB-INF/templates. Note that this uses Freemarker's WebappTemplateLoader to load/cache the templates, so check its docs (or crank up the logging under the 'freemarker.cache' package) if your templates aren't getting loaded.

This will put your Viewable's model object in the template variable "it", unless the model is a Map. If so, the values will be assigned to the template assuming the map is of type Map<String,Object>.

There are a number of methods you can override to change the behavior, such as handling processing exceptions, changing the default template extension, or adding variables to be assigned to every template context.

Author:
Chris Winters // original code, Olivier Grisel // ViewProcessor refactoring

Field Summary
protected  javax.servlet.ServletContext context
           
static java.lang.String FREEMARKER_TEMPLATE_PATH_INIT_PARAM
           
protected  freemarker.template.Configuration freemarkerConfig
           
protected  java.lang.String rootPath
           
 
Constructor Summary
FreemarkerViewProcessor()
           
 
Method Summary
protected  void assignFreemarkerConfig(freemarker.template.Configuration config, javax.servlet.ServletContext context)
          Modify freemarker configuration after we've created it and applied any settings from 'freemarker.properties' on the classpath.
protected  freemarker.template.Configuration getConfig()
           
protected  java.lang.String getDefaultExtension()
           
protected  java.util.Map<java.lang.String,java.lang.Object> getVariablesForTemplate(java.util.Map<java.lang.String,java.lang.Object> viewableVariables)
          Define additional variables to make available to the template.
protected  void onProcessException(java.lang.Throwable t, freemarker.template.Template template, java.util.Map<java.lang.String,java.lang.Object> templateContext, java.io.OutputStream out)
          Catch any exception generated during template processing.
 freemarker.template.Template resolve(java.lang.String path)
           
 void writeTo(freemarker.template.Template template, com.sun.jersey.api.view.Viewable viewable, java.io.OutputStream out)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FREEMARKER_TEMPLATE_PATH_INIT_PARAM

public static final java.lang.String FREEMARKER_TEMPLATE_PATH_INIT_PARAM
See Also:
Constant Field Values

freemarkerConfig

protected freemarker.template.Configuration freemarkerConfig

rootPath

protected java.lang.String rootPath

context

@Context
protected javax.servlet.ServletContext context
Constructor Detail

FreemarkerViewProcessor

public FreemarkerViewProcessor()
Method Detail

getDefaultExtension

protected java.lang.String getDefaultExtension()
Returns:
extension for templates, ".ftl" by default; if we don't see this at the end of your view we'll append it so we can find the template resource

getVariablesForTemplate

protected java.util.Map<java.lang.String,java.lang.Object> getVariablesForTemplate(java.util.Map<java.lang.String,java.lang.Object> viewableVariables)
Define additional variables to make available to the template.

Parameters:
viewableVariables - variables provided by whomever generated the viewable object; these are provided for reference only, there will be no effect if you modify this map
Returns:
new variables for the template context, which will override any defaults provided

onProcessException

protected void onProcessException(java.lang.Throwable t,
                                  freemarker.template.Template template,
                                  java.util.Map<java.lang.String,java.lang.Object> templateContext,
                                  java.io.OutputStream out)
                           throws java.io.IOException
Catch any exception generated during template processing.

Parameters:
t - throwable caught
templatePath - path of template we're executing
templateContext - context use when evaluating this template
out - output stream from servlet container
Throws:
java.io.IOException - on any write errors, or if you want to rethrow

assignFreemarkerConfig

protected void assignFreemarkerConfig(freemarker.template.Configuration config,
                                      javax.servlet.ServletContext context)
Modify freemarker configuration after we've created it and applied any settings from 'freemarker.properties' on the classpath.

Parameters:
config - configuration we've created so far
context - servlet context used to create the configuration

getConfig

protected freemarker.template.Configuration getConfig()

resolve

public freemarker.template.Template resolve(java.lang.String path)
Specified by:
resolve in interface com.sun.jersey.spi.template.ViewProcessor<freemarker.template.Template>

writeTo

public void writeTo(freemarker.template.Template template,
                    com.sun.jersey.api.view.Viewable viewable,
                    java.io.OutputStream out)
             throws java.io.IOException
Specified by:
writeTo in interface com.sun.jersey.spi.template.ViewProcessor<freemarker.template.Template>
Throws:
java.io.IOException


Copyright © 2011-2012 The Apache Software Foundation. All Rights Reserved.