Moss

Java Components

 

Header Filter

This filter allows you to modify the HTTP Response header attributes, based on the request URL. This can be used to, for instance, set the content encoding, prevent browsers from caching a page, etc.

How to Use this Component

Dependencies

This component has no external dependencies.

Installation

To use this component, you must include the .jar file in the web application's classpath (either in WEB-INF/lib or in the server's lib folder). You must then add the following code to the web.xml file:

<filter>
 <filter-name>HeaderFilter</filter-name>
 <filter-class>org.homeunix.thecave.moss.jsp.header.HeaderFilter</filter-class>
 <init-param>
  <param-name>config</param-name>
  <param-value>header.xml</param-value>
 </init-param>
 <init-param>
  <param-name>refresh-interval</param-name>
  <param-value>60</param-value>
 </init-param>
</filter>

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

The filter will go through each url line in the configuration file for a given header type, and try matching the regular expression in the 'matches' atrribute to the request URL. If it matches, it will set the header to the specified content. Once a url has matched for a given header, it skips onto the next header; this allows you to set up an 'if / else if / else' structure in the config file.

Configuration

This filter has two optional init parameters: config and refresh-interval. Config determines the name of the config file, relative to WEB-INF (defaults to 'header.xml'). Refresh Interval determines the length of time (in seconds) before the config file is cached before being re-read.

The config file has the following structure:

<?xml version="1.0"?>
<headers xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
 xsi:noNamespaceSchemaLocation='http://moss.thecave.homeunix.org/schema/2009/Header'>
 <header name="Cache-Control" type="string">
  <request matches=".*\.jpg">max-age=86400</request>
  <request matches=".*\.css">max-age=172800</request>
  <request matches=".*">max-age=3600</request>
 </header>
</headers>