com.lowagie.servlets
Class OutputFilterBase

java.lang.Object
  extended bycom.lowagie.servlets.OutputFilterBase
All Implemented Interfaces:
javax.servlet.Filter
Direct Known Subclasses:
ITextOutputFilter

public abstract class OutputFilterBase
extends Object
implements javax.servlet.Filter

Base filter to enable filtering on the output of a resource, instead of the input. This filter calls the filter chain before calling the

perform()

method, making the output of the filter chain available to said method.

Author:
Marcelo Vanzin

Field Summary
static String EXCEPTION_KEY
          Where the exception caught when executing perform() is stored in the request (so that it can be used on customized error pages).
protected  javax.servlet.FilterConfig fConfig
           
private static String FILTER_APPLIED
           
static String PREVENT_FILTER
          Key where to store an object in the request if you do not want the output filter applied on this request.
 
Constructor Summary
OutputFilterBase()
           
 
Method Summary
 void destroy()
           
 void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)
          Prepares the filter before calling the filter chain.
protected  void dump(BufferedResponse data, javax.servlet.ServletResponse res)
          Dumps the contents of the passed BufferedResponse to the ServletResponse.
protected  void dump(InputStream in, javax.servlet.ServletResponse res)
          Dumps the data from the InputStream passed to the ServletResponse's OutputStream.
 javax.servlet.FilterConfig getFilterConfig()
           
 void init(javax.servlet.FilterConfig filterConfig)
           
protected abstract  void perform(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, InputStream data)
          This method should contain the logic to manipulate the data coming from the resources called by the filter chain and send the data to the response object received.
protected  javax.servlet.ServletRequest prepareRequest(javax.servlet.ServletRequest request)
          This method is called before executing the filter chain.
protected  BufferedResponse prepareResponse(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response)
          This method is called before executing the filter chain, and should return a BufferedResponse objeto to be sent to the chain.
protected  void sendError(javax.servlet.ServletRequest req, javax.servlet.http.HttpServletResponse res, Throwable t)
          Stores the throwable in the EXCEPTION_KEY attribute of the request and sends an "Internal Server Error" to the response with the message from the error.
 void setFilterConfig(javax.servlet.FilterConfig filterConfig)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FILTER_APPLIED

private static final String FILTER_APPLIED
See Also:
Constant Field Values

PREVENT_FILTER

public static final String PREVENT_FILTER
Key where to store an object in the request if you do not want the output filter applied on this request.

See Also:
Constant Field Values

EXCEPTION_KEY

public static final String EXCEPTION_KEY
Where the exception caught when executing perform() is stored in the request (so that it can be used on customized error pages).

See Also:
Constant Field Values

fConfig

protected javax.servlet.FilterConfig fConfig
Constructor Detail

OutputFilterBase

public OutputFilterBase()
Method Detail

doFilter

public final void doFilter(javax.servlet.ServletRequest request,
                           javax.servlet.ServletResponse response,
                           javax.servlet.FilterChain chain)
                    throws IOException,
                           javax.servlet.ServletException

Prepares the filter before calling the filter chain. This enables the implementing class to provide customized ServletRequest and ServletResponse objects to the chain.

After calling the chain, if a object is found under the "PREVENT_FILTER" key in the ServletRequest objeto, the output filter is not executed (that is, the perform() method is not called). If this situation is detected, the buffer received from the chain is copied to the original ServletResponse object.

Also, if the response has been committed (e.g., by calling sendError() on the response), the filter is not applied.

If an exception occurs during the execution of the perform() method, a Internal Server Error is sent to the response with the exception message. The exception is stored under the "EXCEPTION_KEY" key of the request.

Specified by:
doFilter in interface javax.servlet.Filter
Parameters:
request - The request from the filter chain (or from the browser, if this is the first filter).
response - The response from the filter chain (or browser).
chain - The filter chain.
Throws:
IOException
javax.servlet.ServletException

prepareRequest

protected javax.servlet.ServletRequest prepareRequest(javax.servlet.ServletRequest request)

This method is called before executing the filter chain. It should return a ServletRequest object to be provided to the chain. The default implementation just returns the request object passed.

Parameters:
request - The request received from the filter chain.
Returns:
A ServletRequest to be sent to the filter chain.

prepareResponse

protected BufferedResponse prepareResponse(javax.servlet.ServletRequest request,
                                           javax.servlet.ServletResponse response)
                                    throws IOException

This method is called before executing the filter chain, and should return a BufferedResponse objeto to be sent to the chain. This implementation uses the default BufferedResponse implementation, that stores data received from the resources called from the chain in a memory buffer.

Parameters:
request - The request from the filter chain.
response - The response from the filter chain.
Returns:
An instance of BufferedResponse that wraps the original response object.
Throws:
IOException

perform

protected abstract void perform(javax.servlet.ServletRequest request,
                                javax.servlet.ServletResponse response,
                                InputStream data)
                         throws Exception

This method should contain the logic to manipulate the data coming from the resources called by the filter chain and send the data to the response object received.

Parameters:
request - The original request received from the chain.
response - The original response received from the chain.
data - An InputStream containing the buffer that stores the data from the called resources.
Throws:
Exception

dump

protected void dump(BufferedResponse data,
                    javax.servlet.ServletResponse res)
             throws IOException

Dumps the contents of the passed BufferedResponse to the ServletResponse. Before dumping the data itself, sets the original content type and the original content length.

Parameters:
data - A BufferedResponse object.
res - A ServletResponse where to dump the data to.
Throws:
IOException

dump

protected void dump(InputStream in,
                    javax.servlet.ServletResponse res)
             throws IOException

Dumps the data from the InputStream passed to the ServletResponse's OutputStream.

Parameters:
in - An input stream.
res - A ServletResponse where to dump the data to.
Throws:
IOException

sendError

protected void sendError(javax.servlet.ServletRequest req,
                         javax.servlet.http.HttpServletResponse res,
                         Throwable t)
                  throws IOException

Stores the throwable in the EXCEPTION_KEY attribute of the request and sends an "Internal Server Error" to the response with the message from the error.

Throws:
IOException

init

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

destroy

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

getFilterConfig

public javax.servlet.FilterConfig getFilterConfig()

setFilterConfig

public void setFilterConfig(javax.servlet.FilterConfig filterConfig)