|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.sourceforge.stripes.exception.DefaultExceptionHandler
public class DefaultExceptionHandler
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) { ... }
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 |
---|
public DefaultExceptionHandler()
Method Detail |
---|
public void handle(Throwable throwable, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
handle
in interface ExceptionHandler
throwable
- the exception being handledrequest
- the current request being processedresponse
- the response paired with the current request
ServletException
- if the exception passed in cannot be handled
IOException
protected Resolution handle(FileUploadLimitExceededException exception, HttpServletRequest request, 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 response
Resolution
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 String getFileUploadExceededExceptionPath(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 exception
public void init(Configuration configuration) throws Exception
init
in interface ConfigurableComponent
configuration
- the Configuration object being used by Stripes
Exception
- should be thrown if the component cannot be configured well enough to use.protected void addHandler(Class<?> handlerClass) throws Exception
handlerClass
- the class being configured
Exception
- if the handler class cannot be instantiatedprotected void addHandler(Object handler) throws Exception
handler
- the handler instance being configured
Exception
protected Configuration getConfiguration()
protected Throwable unwrap(Throwable throwable)
throwable
- a throwable
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |