public class DefaultExceptionHandler extends java.lang.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) { ... }
Modifier and Type | Class and Description |
---|---|
protected static class |
DefaultExceptionHandler.HandlerProxy
Inner class that ties a class and method together an invokable object.
|
Constructor and Description |
---|
DefaultExceptionHandler() |
Modifier and Type | Method and Description |
---|---|
protected void |
addHandler(java.lang.Class<?> handlerClass)
Adds a class to the set of configured delegate handlers.
|
protected void |
addHandler(java.lang.Object handler)
Adds an object instance to the set of configured handles.
|
protected Configuration |
getConfiguration()
Provides subclasses with access to the configuration.
|
protected java.lang.String |
getFileUploadExceededExceptionPath(javax.servlet.http.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,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
FileUploadLimitExceededException is notoriously difficult to handle for several
reasons:
The exception is thrown during construction of the StripesRequestWrapper . |
void |
handle(java.lang.Throwable throwable,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.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 java.lang.Throwable |
unwrap(java.lang.Throwable throwable)
Unwraps the throwable passed in.
|
public void handle(java.lang.Throwable throwable, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
handle
in interface ExceptionHandler
throwable
- the exception being handledrequest
- the current request being processedresponse
- the response paired with the current requestjavax.servlet.ServletException
- if the exception passed in cannot be handledjava.io.IOException
protected Resolution handle(FileUploadLimitExceededException exception, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws FileUploadLimitExceededException
FileUploadLimitExceededException
is notoriously difficult to handle for several
reasons:
StripesRequestWrapper
. Many
Stripes components rely on the presence of this wrapper, yet it cannot be created normally.ExecutionContext
,
ActionBeanContext
, or ActionBean
associated with the request yet._sourcePage
parameter that indicates the page from which
the request was submitted.
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.
exception
- The exception that needs to be handledrequest
- The servlet requestresponse
- The servlet responseResolution
to forward to the path returned by
getFileUploadExceededExceptionPath(HttpServletRequest)
FileUploadLimitExceededException
- If
getFileUploadExceededExceptionPath(HttpServletRequest)
returns null or
this method is unable for any other reason to forward to the error pageprotected java.lang.String getFileUploadExceededExceptionPath(javax.servlet.http.HttpServletRequest request)
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.request
- The request that generated the exceptionpublic void init(Configuration configuration) throws java.lang.Exception
init
in interface ConfigurableComponent
configuration
- the Configuration object being used by Stripesjava.lang.Exception
- should be thrown if the component cannot be configured well enough to use.protected void addHandler(java.lang.Class<?> handlerClass) throws java.lang.Exception
handlerClass
- the class being configuredjava.lang.Exception
- if the handler class cannot be instantiatedprotected void addHandler(java.lang.Object handler) throws java.lang.Exception
handler
- the handler instance being configuredjava.lang.Exception
protected Configuration getConfiguration()
protected java.lang.Throwable unwrap(java.lang.Throwable throwable)
throwable
- a throwable? Copyright 2005-2006, Stripes Development Team.