org.sitemesh.config
Class ConfigurableSiteMeshFilter

java.lang.Object
  extended by org.sitemesh.config.ConfigurableSiteMeshFilter
All Implemented Interfaces:
javax.servlet.Filter

public class ConfigurableSiteMeshFilter
extends Object
implements javax.servlet.Filter

A SiteMesh filter that can be dropped in to web.xml, that does not require having to write any Java code.

Approach 1: Embed configuration as init-params

The minimum required to make this useful is to add a decoratorMappings init parameter.

/WEB-INF/web.xml

  <filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
    <init-param>
      <param-name>decoratorMappings</param-name>
      <param-value>
        /*=/decorators/my-decorator.html
        /admin/*=/decorators/admin-decorator.html
      </param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
 

Approach 2: Embed configuration as init-params

Alternatively the configuration can be specified in a separate XML file. This has the advantage that it can be changed at runtime without having to restart the web-application. Also, the same config file could potentially be reused to generate offline content.

/WEB-INF/web.xml

  <filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
 

/WEB-INF/sitemesh3.xml

  <sitemesh>
    <mapping name="/*" decorator="/decorators/my-decorator.html"/>
    <mapping name="/admin/*" decorator="/decorators/admin-decorator.html"/>
  </sitemesh>
 

The default configuration path of /WEB-INF/sitemesh3.xml can be changed by adding a configFile init-param to the filter.

Reference

decoratorMappings: A list of mappings of path patterns to decorators. Each entry should consist of pattern=decorator, separated by whitespace or commas. If multiple decorators are required, they should be delimited with a pipe | char (and no whitespace) e.g. /admin/*=/decorators/admin.html, *.secret=/decorators/secret.html|/decorators/common.html

mimeTypes (optional): A list of mime-types, separated by whitespace or commas, that should attempt to be decorated. Defaults to text/html.

tagRuleBundles (optional): The names of any additional TagRuleBundles to install, separated by whitespace or commas. These will be added to the default bundles (as set up in BaseSiteMeshBuilder.setupDefaults()): CoreHtmlTagRuleBundle and DecoratorTagRuleBundle. Note: The contentProcessor and tagRuleBundles are mutually exclusive - you should not set them both.

contentProcessor (optional): The name of the ContentProcessor to use. Note: The contentProcessor and tagRuleBundles are mutually exclusive - you should not set them both.

exclude (optional): A list of path patterns to exclude from decoration, separated by whitespace or commas. e.g. /javadoc/*, somepage.html, *.jsp

Where a name is used, this means the fully qualified class name, which must have a default constructor.

Author:
Joe Walnes
See Also:
PropertiesFilterConfigurator

Field Summary
static boolean AUTO_RELOAD_DEFAULT
           
static String AUTO_RELOAD_PARAM
           
static String CONFIG_FILE_DEFAULT
           
static String CONFIG_FILE_PARAM
           
 
Constructor Summary
ConfigurableSiteMeshFilter()
           
 
Method Summary
protected  void applyCustomConfiguration(SiteMeshFilterBuilder builder)
          Override this to apply custom configuration after after the default configuration mechanisms.
protected  void deployNewFilter(javax.servlet.Filter newFilter)
           
 void destroy()
           
 void doFilter(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse, javax.servlet.FilterChain filterChain)
           
protected  boolean getAutoReload()
          Whether the config file should be monitored for changes and automatically reloaded.
protected  String getConfigFileName()
          Gets the SiteMesh XML config file name.
protected  Map<String,String> getConfigProperties(javax.servlet.FilterConfig filterConfig)
          Return the configuration properties that are passed to PropertiesFilterConfigurator.
protected  ObjectFactory getObjectFactory()
           
 void init(javax.servlet.FilterConfig filterConfig)
           
protected  Element loadConfigXml(javax.servlet.FilterConfig filterConfig, String configFilePath)
          Load the XML config file.
protected  void reloadIfNecessary()
           
protected  boolean reloadRequired()
          Determine if a reload is required.
protected  javax.servlet.Filter setup()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CONFIG_FILE_PARAM

public static final String CONFIG_FILE_PARAM
See Also:
Constant Field Values

CONFIG_FILE_DEFAULT

public static final String CONFIG_FILE_DEFAULT
See Also:
Constant Field Values

AUTO_RELOAD_PARAM

public static final String AUTO_RELOAD_PARAM
See Also:
Constant Field Values

AUTO_RELOAD_DEFAULT

public static final boolean AUTO_RELOAD_DEFAULT
See Also:
Constant Field Values
Constructor Detail

ConfigurableSiteMeshFilter

public ConfigurableSiteMeshFilter()
Method Detail

init

public void init(javax.servlet.FilterConfig filterConfig)
          throws javax.servlet.ServletException
Specified by:
init in interface javax.servlet.Filter
Throws:
javax.servlet.ServletException

doFilter

public void doFilter(javax.servlet.ServletRequest servletRequest,
                     javax.servlet.ServletResponse servletResponse,
                     javax.servlet.FilterChain filterChain)
              throws IOException,
                     javax.servlet.ServletException
Specified by:
doFilter in interface javax.servlet.Filter
Throws:
IOException
javax.servlet.ServletException

destroy

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

getConfigProperties

protected Map<String,String> getConfigProperties(javax.servlet.FilterConfig filterConfig)
Return the configuration properties that are passed to PropertiesFilterConfigurator.

This implementation simply reads them from the Filter's <init-param>s in web.xml. To read from another place, override this.


setup

protected javax.servlet.Filter setup()
                              throws javax.servlet.ServletException
Throws:
javax.servlet.ServletException

applyCustomConfiguration

protected void applyCustomConfiguration(SiteMeshFilterBuilder builder)
Override this to apply custom configuration after after the default configuration mechanisms.


reloadRequired

protected boolean reloadRequired()
Determine if a reload is required. Override this to change the behavior.


getAutoReload

protected boolean getAutoReload()
Whether the config file should be monitored for changes and automatically reloaded.


getConfigFileName

protected String getConfigFileName()
Gets the SiteMesh XML config file name. Looks for a 'config' property in the Filter init-params. If not found, defaults to '/WEB-INF/sitemesh3.xml'.


getObjectFactory

protected ObjectFactory getObjectFactory()

loadConfigXml

protected Element loadConfigXml(javax.servlet.FilterConfig filterConfig,
                                String configFilePath)
                         throws javax.servlet.ServletException
Load the XML config file. Will try a number of locations until it finds the file.
 - Will first search for a file on disk relative to the root of the web-app.
 - Then a file with the absolute path.
 - Then a file as a resource in the ServletContext (allowing for files embedded in a .war file).
 - If none of those find the file, null will be returned.
 

Throws:
javax.servlet.ServletException

reloadIfNecessary

protected void reloadIfNecessary()
                          throws javax.servlet.ServletException
Throws:
javax.servlet.ServletException

deployNewFilter

protected void deployNewFilter(javax.servlet.Filter newFilter)
                        throws javax.servlet.ServletException
Throws:
javax.servlet.ServletException


Copyright © 2011. All Rights Reserved.