org.apache.velocity.tools.view
Class VelocityViewFilter

java.lang.Object
  extended by org.apache.velocity.tools.view.VelocityViewFilter
All Implemented Interfaces:
javax.servlet.Filter

public class VelocityViewFilter
extends java.lang.Object
implements javax.servlet.Filter

A filter to ensure VelocityTools Toolboxes are available in the request/session/application scopes. This can simplify the process of integration with other frameworks.

VelocityViewFilter supports the following configuration parameters in web.xml:

org.apache.velocity.tools
Path and name of the toolbox configuration file. The path must be relative to the web application root directory. If this parameter is not found, the servlet will check for a toolbox file at '/WEB-INF/tools.xml'.
org.apache.velocity.properties
Path and name of the Velocity configuration file. The path must be relative to the web application root directory. If this parameter is not present, Velocity will check for a properties file at '/WEB-INF/velocity.properties'. If no file is found there, then Velocity is initialized with the settings in the classpath at 'org.apache.velocity.tools.view.velocity.properties'.
org.apache.velocity.tools.loadDefaults
By default, this is true. If set to false, then the default toolbox configuration will not be added to your (if any) custom configuration. NOTE: The default configuration will also be suppressed if you are using a deprecated toolbox.xml format and do not explicitly set this to true.
org.apache.velocity.tools.cleanConfiguration
By default, this is false. If set to true, then then the final toolbox configuration (the combination of any custom one(s) provided by yourself and/or the default configuration(s)) will have all invalid tools, properties, and/or data removed prior to configuring the ToolboxFactory for this servlet by a ConfigurationCleaner
org.apache.velocity.tools.shared.config
By default, this is true. If set to false, then the VelocityView used by this filter will not be shared with other VelocityViewFilters, VelocityViewServlets or VelocityViewTags in the application.
org.apache.velocity.tools.context.key
If you set a value for this property, this filter will create and prepare a ViewToolContext for each request, and then place it into the request attributes under the key you set. This is primarily for those who have this filter NOT share a config (i.e. non-shared VelocityView) and thus will find it easier to retrieve a working context from the request attributes than it would be to get the VelocityView for this filter and have it create the context for them. Most users will have no trouble getting a shared VelocityView and creating the context themselves.

Version:
$Id: VelocityViewFilter.java 611011 2008-01-11 01:32:59Z nbubna $

Field Summary
private  javax.servlet.FilterConfig config
           
static java.lang.String CONTEXT_KEY
           
private  java.lang.String contextKey
           
private  VelocityView view
           
 
Constructor Summary
VelocityViewFilter()
           
 
Method Summary
protected  org.apache.velocity.context.Context createContext(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
           
 void destroy()
           
 void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)
          Simply prepares the request (and/or session) toolbox(es) for other filters, servlets or whatnot to use.
protected  java.lang.String findInitParameter(java.lang.String key)
          Looks up an init parameter with the specified key in either the FilterConfig or, failing that, in the ServletContext.
protected  java.lang.String getContextKey()
           
protected  javax.servlet.FilterConfig getFilterConfig()
           
protected  VelocityView getVelocityView()
           
 void init(javax.servlet.FilterConfig config)
          Initializes VelocityView used to process requests.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CONTEXT_KEY

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

view

private VelocityView view

config

private javax.servlet.FilterConfig config

contextKey

private java.lang.String contextKey
Constructor Detail

VelocityViewFilter

public VelocityViewFilter()
Method Detail

init

public void init(javax.servlet.FilterConfig config)
          throws javax.servlet.ServletException

Initializes VelocityView used to process requests. Called by the servlet container on loading.

Specified by:
init in interface javax.servlet.Filter
Parameters:
config - filter configuation
Throws:
javax.servlet.ServletException

getFilterConfig

protected javax.servlet.FilterConfig getFilterConfig()

getVelocityView

protected VelocityView getVelocityView()

getContextKey

protected java.lang.String getContextKey()

findInitParameter

protected java.lang.String findInitParameter(java.lang.String key)
Looks up an init parameter with the specified key in either the FilterConfig or, failing that, in the ServletContext.


doFilter

public void doFilter(javax.servlet.ServletRequest request,
                     javax.servlet.ServletResponse response,
                     javax.servlet.FilterChain chain)
              throws java.io.IOException,
                     javax.servlet.ServletException
Simply prepares the request (and/or session) toolbox(es) for other filters, servlets or whatnot to use. If a context key has been provided in the init-params of the filter or webapp, then this will also create a ViewToolContext and put it in the request attributes under that key.

Specified by:
doFilter in interface javax.servlet.Filter
Throws:
java.io.IOException
javax.servlet.ServletException

createContext

protected org.apache.velocity.context.Context createContext(javax.servlet.http.HttpServletRequest request,
                                                            javax.servlet.http.HttpServletResponse response)

destroy

public void destroy()
Specified by:
destroy in interface javax.servlet.Filter


Copyright (c) 2003-2007 Apache Software Foundation