net.sourceforge.stripes.exception
Class DefaultExceptionHandler

java.lang.Object
  extended by net.sourceforge.stripes.exception.DefaultExceptionHandler
All Implemented Interfaces:
ConfigurableComponent, ExceptionHandler
Direct Known Subclasses:
DelegatingExceptionHandler

public class DefaultExceptionHandler
extends Object
implements ExceptionHandler

Default ExceptionHandler implementation that makes it easy for users to extend and add custom handling for different types of exception. When extending this class methods can be added that meet the following requirements:

When an exception is caught the exception handler attempts to find a method that can handle that type of exception. If none is found the exception's super-types are iterated through and methods looked for which match the super-types. If a matching method is found it will be invoked. Otherwise the exception will simply be rethrown by the exception handler - though first it will be wrapped in a StripesServletException if necessary in order to make it acceptable to the container.

The following are examples of method signatures that might be added by subclasses:

 public Resolution handle(FileUploadLimitExceededException ex, HttpServletRequest req, HttpServletResponse resp) { ... }
 public void handle(MySecurityException ex, HttpServletRequest req, HttpServletResponse resp) { ... }
 public void catchAll(Throwable t, HttpServletRequest req, HttpServletResponse resp) { ... }
 

Since:
Stripes 1.3
Author:
Tim Fennell

Nested Class Summary
protected static class DefaultExceptionHandler.HandlerProxy
          Inner class that ties a class and method together an invokable object.
 
Constructor Summary
DefaultExceptionHandler()
           
 
Method Summary
protected  void addHandler(Class<?> handlerClass)
          Adds a class to the set of configured delegate handlers.
protected  void addHandler(Object handler)
          Adds an object instance to the set of configured handles.
protected  Configuration getConfiguration()
          Provides subclasses with access to the configuration.
protected  String getFileUploadExceededExceptionPath(HttpServletRequest request)
          Get the path to which the Resolution returned by handle(FileUploadLimitExceededException, HttpServletRequest, HttpServletResponse) should forward to report the error.
protected  Resolution handle(FileUploadLimitExceededException exception, HttpServletRequest request, HttpServletResponse response)
           FileUploadLimitExceededException is notoriously difficult to handle for several reasons: The exception is thrown during construction of the StripesRequestWrapper.
 void handle(Throwable throwable, HttpServletRequest request, HttpServletResponse response)
          Implementation of the ExceptionHandler interface that attempts to find a method that is capable of handing the exception.
 void init(Configuration configuration)
          Stores the configuration and examines the handler for usable delegate methods.
protected  Throwable unwrap(Throwable throwable)
          Unwraps the throwable passed in.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultExceptionHandler

public DefaultExceptionHandler()
Method Detail

handle

public void handle(Throwable throwable,
                   HttpServletRequest request,
                   HttpServletResponse response)
            throws ServletException,
                   IOException
Implementation of the ExceptionHandler interface that attempts to find a method that is capable of handing the exception. If it finds one then it is delegated to, and if it returns a resolution it will be executed. Otherwise rethrows any unhandled exceptions, wrapped in a StripesServletException if necessary.

Specified by:
handle in interface ExceptionHandler
Parameters:
throwable - the exception being handled
request - the current request being processed
response - the response paired with the current request
Throws:
ServletException - if the exception passed in cannot be handled
IOException

handle

protected Resolution handle(FileUploadLimitExceededException exception,
                            HttpServletRequest request,
                            HttpServletResponse response)
                     throws FileUploadLimitExceededException

FileUploadLimitExceededException is notoriously difficult to handle for several reasons:

This exception handler makes an attempt to handle the exception as gracefully as possible. It relies on the HTTP Referer header to determine where the request was submitted from. It uses introspection to guess the field name of the FileBean field that exceeded the POST limit. It instantiates an ActionBean and ActionBeanContext and adds a validation error to report the field name, maximum POST size, and actual POST size. Finally, it forwards to the referer.

While this is a best effort, it won't be ideal for all situations. If this method is unable to handle the exception properly for any reason, it rethrows the exception. Subclasses can call this method in a try block, providing additional processing in the catch block.

A simple way to provide a single, global error page for this type of exception is to override getFileUploadExceededExceptionPath(HttpServletRequest) to return the path to your global error page.

Parameters:
exception - The exception that needs to be handled
request - The servlet request
response - The servlet response
Returns:
A Resolution to forward to the path returned by getFileUploadExceededExceptionPath(HttpServletRequest)
Throws:
FileUploadLimitExceededException - If getFileUploadExceededExceptionPath(HttpServletRequest) returns null or this method is unable for any other reason to forward to the error page

getFileUploadExceededExceptionPath

protected String getFileUploadExceededExceptionPath(HttpServletRequest request)
Get the path to which the Resolution returned by handle(FileUploadLimitExceededException, HttpServletRequest, HttpServletResponse) should forward to report the error. The default implementation attempts to determine this from the HTTP Referer header. If it is unable to do so, it returns null. Subclasses may override this method to return whatever they wish. The return value must be relative to the application context root.

Parameters:
request - The request that generated the exception
Returns:
The context-relative path from which the request was submitted

init

public void init(Configuration configuration)
          throws Exception
Stores the configuration and examines the handler for usable delegate methods.

Specified by:
init in interface ConfigurableComponent
Parameters:
configuration - the Configuration object being used by Stripes
Throws:
Exception - should be thrown if the component cannot be configured well enough to use.

addHandler

protected void addHandler(Class<?> handlerClass)
                   throws Exception
Adds a class to the set of configured delegate handlers. Examines all the methods on the class looking for public non-abstract methods with a signature matching that described in the class level javadoc. Each method is wrapped in a HandlerProxy and stored in a cache by the exception type it takes.

Parameters:
handlerClass - the class being configured
Throws:
Exception - if the handler class cannot be instantiated

addHandler

protected void addHandler(Object handler)
                   throws Exception
Adds an object instance to the set of configured handles. Examines all the methods on the class looking for public non-abstract methods with a signature matching that described in the class level javadoc. Each method is wrapped in a HandlerProxy and stored in a cache by the exception type it takes.

Parameters:
handler - the handler instance being configured
Throws:
Exception

getConfiguration

protected Configuration getConfiguration()
Provides subclasses with access to the configuration.


unwrap

protected Throwable unwrap(Throwable throwable)
Unwraps the throwable passed in. If the throwable is a ServletException and has a root case, the root cause is returned, otherwise the throwable is returned as is.

Parameters:
throwable - a throwable
Returns:
another thowable, either the root cause of the one passed in


? Copyright 2005-2006, Stripes Development Team.