sunlabs.brazil.velocity
Class VelocityFilter

java.lang.Object
  extended by sunlabs.brazil.velocity.VelocityFilter (view source)
All Implemented Interfaces:
LogSystem, Filter, Handler

public class VelocityFilter
extends Object
implements Filter, LogSystem

A filter for processing markup that is a Velocity template. The filter will "merge" the content with a BeanShell script to produce output content.

The following server properties are used:

script
The name of a BeanShell script file.
session
The name of the propery containing the session. The default session, if none is found, is "common".

The BeanShell script is found by first looking in the filesystem. If not found, the classpath is searched. It's okay not to specify or use a BeanShell script. The content will still be processed by the Velocity engine.

The BeanShell interpreter is managed by session and is persistent during the session. The interpreter has access to the Server object, the prefix string, the Request object, and the Velocity context through the variables "server", "prefix", "request", and "context".

A new Velocity context is created each time the filter method is called. For convenience, the context is initially populated with the same objects as the BeanShell. They are referenced by the same names ("server", "prefix", "request", and "context"). However, due to the design of Velocity, the public fields of the Server and Request objects are read only. The field values can not be changed by the template code.

Each time the filter method is called, a new Velocity context is created and populated. Then the BeanShell script, if it exists, is passed through Format.subst for variable substitution and then passed to the BeanShell interpreter. Finally the content (i.e. a Velocity template) and the context are passed to the Velocity engine for processing.

Note: Velocity requires at least a 1.2 Java VM. Attempts to use Velocity with a 1.1 VM will most likely produce incorrect results. Also note that the Brazil system must be compiled with a 1.2+ compiler so that core components of Brazil are compatible with the Velocity engine.


Nested Class Summary
 class VelocityFilter.Vrequest
          A helper class for Velocity that provides read only access to the public fields of the Request object.
 class VelocityFilter.Vserver
          A helper class for Velocity that provides read only access to the public fields of the Server object.
 
Field Summary
 
Fields inherited from interface org.apache.velocity.runtime.log.LogSystem
DEBUG_ID, DEBUG_ON, ERROR_ID, INFO_ID, WARN_ID
 
Constructor Summary
VelocityFilter()
           
 
Method Summary
 byte[] filter(Request request, MimeHeaders headers, byte[] content)
          Execute the BeanShell script if it exists and then process the content as a Velocity template.
 void init(RuntimeServices rsvc)
          Velocity LogSystem interface implementation.
 boolean init(Server server, String prefix)
          Initializes the handler.
 void logVelocityMessage(int level, String message)
           
 boolean respond(Request request)
          This is the request object before the content was fetched
 boolean shouldFilter(Request request, MimeHeaders headers)
          Only filter text/* documents
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

VelocityFilter

public VelocityFilter()
Method Detail

init

public boolean init(Server server,
                    String prefix)
Description copied from interface: Handler
Initializes the handler.

Specified by:
init in interface Handler
Parameters:
server - The HTTP server that created this Handler. Typical Handlers will use Server.props to obtain run-time configuration information.
prefix - The handlers name. The string this Handler may prepend to all of the keys that it uses to extract configuration information from Server.props. This is set (by the Server and ChainHandler) to help avoid configuration parameter namespace collisions.
Returns:
true if this Handler initialized successfully, false otherwise. If false is returned, this Handler should not be used.

respond

public boolean respond(Request request)
This is the request object before the content was fetched

Specified by:
respond in interface Handler
Parameters:
request - The Request object that represents the HTTP request.
Returns:
true if the request was handled. A request was handled if a response was supplied to the client, typically by calling Request.sendResponse() or Request.sendError.

shouldFilter

public boolean shouldFilter(Request request,
                            MimeHeaders headers)
Only filter text/* documents

Specified by:
shouldFilter in interface Filter
Parameters:
request - The in-progress HTTP request.
headers - The MIME headers generated by the wrapped Handler.
Returns:
true if this filter would like to examine and possibly rewrite the content, false otherwise.

filter

public byte[] filter(Request request,
                     MimeHeaders headers,
                     byte[] content)
Execute the BeanShell script if it exists and then process the content as a Velocity template.

Specified by:
filter in interface Filter
Parameters:
request - The finished HTTP request.
headers - The MIME headers generated by the Handler.
content - The output from the Handler that this Filter may rewrite.
Returns:
The rewritten content. The Filter may return the original content unchanged. The Filter may return null to indicate that the FilterHandler should stop processing the request and should not return any content to the client.

init

public void init(RuntimeServices rsvc)
Velocity LogSystem interface implementation.

Specified by:
init in interface LogSystem

logVelocityMessage

public void logVelocityMessage(int level,
                               String message)
Specified by:
logVelocityMessage in interface LogSystem

Version Kenai-svn-r24, Generated 08/18/09
Copyright (c) 2001-2009, Sun Microsystems.