polishing

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3195 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Juergen Hoeller 2010-03-30 21:12:40 +00:00
parent d957f8c74c
commit 6132e318b1
4 changed files with 66 additions and 66 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -33,8 +33,10 @@ import org.springframework.web.portlet.ModelAndView;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
/** /**
* Abstract base class for {@link HandlerExceptionResolver} implementations. <p>Provides a set of mapped handlers that * Abstract base class for {@link HandlerExceptionResolver} implementations.
* the resolver should map to, and the {@link Ordered} implementation. *
* <p>Provides a set of mapped handlers that the resolver should map to,
* and the {@link Ordered} implementation.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 3.0 * @since 3.0
@ -54,6 +56,7 @@ public abstract class AbstractHandlerExceptionResolver implements HandlerExcepti
private boolean renderWhenMinimized = false; private boolean renderWhenMinimized = false;
public void setOrder(int order) { public void setOrder(int order) {
this.order = order; this.order = order;
} }
@ -115,14 +118,13 @@ public abstract class AbstractHandlerExceptionResolver implements HandlerExcepti
this.renderWhenMinimized = renderWhenMinimized; this.renderWhenMinimized = renderWhenMinimized;
} }
/** /**
* Checks whether this resolver is supposed to apply (i.e. the handler * Checks whether this resolver is supposed to apply (i.e. the handler
* matches in case of "mappedHandlers" having been specified), then * matches in case of "mappedHandlers" having been specified), then
* delegates to the {@link #doResolveException} template method. * delegates to the {@link #doResolveException} template method.
*/ */
public ModelAndView resolveException( public ModelAndView resolveException(RenderRequest request, RenderResponse response, Object handler, Exception ex) {
RenderRequest request, RenderResponse response, Object handler, Exception ex) {
if (shouldApplyTo(request, handler)) { if (shouldApplyTo(request, handler)) {
return doResolveException(request, response, handler, ex); return doResolveException(request, response, handler, ex);
} }
@ -131,9 +133,7 @@ public abstract class AbstractHandlerExceptionResolver implements HandlerExcepti
} }
} }
public ModelAndView resolveException( public ModelAndView resolveException(ResourceRequest request, ResourceResponse response, Object handler, Exception ex) {
ResourceRequest request, ResourceResponse response, Object handler, Exception ex) {
if (shouldApplyTo(request, handler)) { if (shouldApplyTo(request, handler)) {
return doResolveException(request, response, handler, ex); return doResolveException(request, response, handler, ex);
} }
@ -205,14 +205,14 @@ public abstract class AbstractHandlerExceptionResolver implements HandlerExcepti
return "Handler execution resulted in exception"; return "Handler execution resulted in exception";
} }
/** /**
* Actually resolve the given exception that got thrown during on handler execution, * Actually resolve the given exception that got thrown during on handler execution,
* returning a ModelAndView that represents a specific error page if appropriate. * returning a ModelAndView that represents a specific error page if appropriate.
* * <p>Must be overridden in subclasses, in order to apply specific exception checks.
* <p>Must be overridden in subclasses, in order to apply specific exception checks. Note that this template method * Note that this template method will be invoked <i>after</i> checking whether this
* will be invoked <i>after</i> checking whether this resolved applies ("mappedHandlers" etc), so an implementation * resolved applies ("mappedHandlers" etc), so an implementation may simply proceed
* may simply proceed with its actual exception handling. * with its actual exception handling.
* @param request current portlet request * @param request current portlet request
* @param response current portlet response * @param response current portlet response
* @param handler the executed handler, or null if none chosen at the time of * @param handler the executed handler, or null if none chosen at the time of
@ -220,9 +220,7 @@ public abstract class AbstractHandlerExceptionResolver implements HandlerExcepti
* @param ex the exception that got thrown during handler execution * @param ex the exception that got thrown during handler execution
* @return a corresponding ModelAndView to forward to, or null for default processing * @return a corresponding ModelAndView to forward to, or null for default processing
*/ */
protected abstract ModelAndView doResolveException(PortletRequest request, protected abstract ModelAndView doResolveException(PortletRequest request, MimeResponse response,
MimeResponse response, Object handler, Exception ex);
Object handler,
Exception ex);
} }

View File

@ -245,10 +245,9 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
Class paramType = methodParameter.getParameterType(); Class paramType = methodParameter.getParameterType();
Object value = resolveStandardArgument(paramType, webRequest, thrownException); Object value = resolveStandardArgument(paramType, webRequest, thrownException);
if (value != WebArgumentResolver.UNRESOLVED && !ClassUtils.isAssignableValue(paramType, value)) { if (value != WebArgumentResolver.UNRESOLVED && !ClassUtils.isAssignableValue(paramType, value)) {
throw new IllegalStateException( throw new IllegalStateException("Standard argument type [" + paramType.getName() +
"Standard argument type [" + paramType.getName() + "] resolved to incompatible value of type [" + "] resolved to incompatible value of type [" + (value != null ? value.getClass() : null) +
(value != null ? value.getClass() : null) + "]. Consider declaring the argument type in a less specific fashion.");
"]. Consider declaring the argument type in a less specific fashion.");
} }
return value; return value;
} }
@ -266,7 +265,10 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
protected Object resolveStandardArgument(Class parameterType, NativeWebRequest webRequest, protected Object resolveStandardArgument(Class parameterType, NativeWebRequest webRequest,
Exception thrownException) throws Exception { Exception thrownException) throws Exception {
if (WebRequest.class.isAssignableFrom(parameterType)) { if (parameterType.isInstance(thrownException)) {
return thrownException;
}
else if (WebRequest.class.isAssignableFrom(parameterType)) {
return webRequest; return webRequest;
} }
@ -330,9 +332,6 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
} }
return ((EventRequest) request).getEvent(); return ((EventRequest) request).getEvent();
} }
else if (parameterType.isInstance(thrownException)) {
return thrownException;
}
else { else {
return WebArgumentResolver.UNRESOLVED; return WebArgumentResolver.UNRESOLVED;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,8 +28,10 @@ import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
/** /**
* Abstract base class for {@link HandlerExceptionResolver} implementations. <p>Provides a set of mapped handlers that * Abstract base class for {@link HandlerExceptionResolver} implementations.
* the resolver should map to, and the {@link Ordered} implementation. *
* <p>Provides a set of mapped handlers that the resolver should map to,
* and the {@link Ordered} implementation.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 3.0 * @since 3.0
@ -47,6 +49,7 @@ public abstract class AbstractHandlerExceptionResolver implements HandlerExcepti
private Log warnLogger; private Log warnLogger;
public void setOrder(int order) { public void setOrder(int order) {
this.order = order; this.order = order;
} }
@ -82,7 +85,6 @@ public abstract class AbstractHandlerExceptionResolver implements HandlerExcepti
* Commons Logging, getting interpreted as log category according to the logger's configuration. <p>Default is no warn * Commons Logging, getting interpreted as log category according to the logger's configuration. <p>Default is no warn
* logging. Specify this setting to activate warn logging into a specific category. Alternatively, override the {@link * logging. Specify this setting to activate warn logging into a specific category. Alternatively, override the {@link
* #logException} method for custom logging. * #logException} method for custom logging.
*
* @see org.apache.commons.logging.LogFactory#getLog(String) * @see org.apache.commons.logging.LogFactory#getLog(String)
* @see org.apache.log4j.Logger#getLogger(String) * @see org.apache.log4j.Logger#getLogger(String)
* @see java.util.logging.Logger#getLogger(String) * @see java.util.logging.Logger#getLogger(String)
@ -91,14 +93,14 @@ public abstract class AbstractHandlerExceptionResolver implements HandlerExcepti
this.warnLogger = LogFactory.getLog(loggerName); this.warnLogger = LogFactory.getLog(loggerName);
} }
/** /**
* Checks whether this resolver is supposed to apply (i.e. the handler matches in case of "mappedHandlers" having been * Checks whether this resolver is supposed to apply (i.e. the handler matches
* specified), then delegates to the {@link #doResolveException} template method. * in case of "mappedHandlers" having been specified), then delegates to the
* {@link #doResolveException} template method.
*/ */
public ModelAndView resolveException(HttpServletRequest request, public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
HttpServletResponse response, Object handler, Exception ex) {
Object handler,
Exception ex) {
if (shouldApplyTo(request, handler)) { if (shouldApplyTo(request, handler)) {
// Log exception, both at debug log level and at warn level, if desired. // Log exception, both at debug log level and at warn level, if desired.
@ -114,13 +116,14 @@ public abstract class AbstractHandlerExceptionResolver implements HandlerExcepti
} }
/** /**
* Check whether this resolver is supposed to apply to the given handler. <p>The default implementation checks against * Check whether this resolver is supposed to apply to the given handler.
* the specified mapped handlers and handler classes, if any. * <p>The default implementation checks against the specified mapped handlers
* * and handler classes, if any.
* @param request current HTTP request * @param request current HTTP request
* @param handler the executed handler, or <code>null</code> if none chosen at the time of the exception (for example, * @param handler the executed handler, or <code>null</code> if none chosen
* if multipart resolution failed) * at the time of the exception (for example, if multipart resolution failed)
* @return whether this resolved should proceed with resolving the exception for the given request and handler * @return whether this resolved should proceed with resolving the exception
* for the given request and handler
* @see #setMappedHandlers * @see #setMappedHandlers
* @see #setMappedHandlerClasses * @see #setMappedHandlerClasses
*/ */
@ -142,10 +145,10 @@ public abstract class AbstractHandlerExceptionResolver implements HandlerExcepti
} }
/** /**
* Log the given exception at warn level, provided that warn logging has been activated through the {@link * Log the given exception at warn level, provided that warn logging has been
* #setWarnLogCategory "warnLogCategory"} property. <p>Calls {@link #buildLogMessage} in order to determine the * activated through the {@link #setWarnLogCategory "warnLogCategory"} property.
* concrete message to log. Always passes the full exception to the logger. * <p>Calls {@link #buildLogMessage} in order to determine the concrete message to log.
* * Always passes the full exception to the logger.
* @param ex the exception that got thrown during handler execution * @param ex the exception that got thrown during handler execution
* @param request current HTTP request (useful for obtaining metadata) * @param request current HTTP request (useful for obtaining metadata)
* @see #setWarnLogCategory * @see #setWarnLogCategory
@ -160,7 +163,6 @@ public abstract class AbstractHandlerExceptionResolver implements HandlerExcepti
/** /**
* Build a log message for the given exception, occured during processing the given request. * Build a log message for the given exception, occured during processing the given request.
*
* @param ex the exception that got thrown during handler execution * @param ex the exception that got thrown during handler execution
* @param request current HTTP request (useful for obtaining metadata) * @param request current HTTP request (useful for obtaining metadata)
* @return the log message to use * @return the log message to use
@ -170,21 +172,20 @@ public abstract class AbstractHandlerExceptionResolver implements HandlerExcepti
} }
/** /**
* Actually resolve the given exception that got thrown during on handler execution, returning a ModelAndView that * Actually resolve the given exception that got thrown during on handler execution,
* represents a specific error page if appropriate. <p>May be overridden in subclasses, in order to apply specific * returning a ModelAndView that represents a specific error page if appropriate.
* exception checks. Note that this template method will be invoked <i>after</i> checking whether this resolved applies * <p>May be overridden in subclasses, in order to apply specific exception checks.
* ("mappedHandlers" etc), so an implementation may simply proceed with its actual exception handling. * Note that this template method will be invoked <i>after</i> checking whether this
* * resolved applies ("mappedHandlers" etc), so an implementation may simply proceed
* with its actual exception handling.
* @param request current HTTP request * @param request current HTTP request
* @param response current HTTP response * @param response current HTTP response
* @param handler the executed handler, or <code>null</code> if none chosen at the time of the exception (for example, * @param handler the executed handler, or <code>null</code> if none chosen at the time
* if multipart resolution failed) * of the exception (for example, if multipart resolution failed)
* @param ex the exception that got thrown during handler execution * @param ex the exception that got thrown during handler execution
* @return a corresponding ModelAndView to forward to, or <code>null</code> for default processing * @return a corresponding ModelAndView to forward to, or <code>null</code> for default processing
*/ */
protected abstract ModelAndView doResolveException(HttpServletRequest request, protected abstract ModelAndView doResolveException(HttpServletRequest request,
HttpServletResponse response, HttpServletResponse response, Object handler, Exception ex);
Object handler,
Exception ex);
} }

View File

@ -82,21 +82,23 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
private WebArgumentResolver[] customArgumentResolvers; private WebArgumentResolver[] customArgumentResolvers;
private HttpMessageConverter<?>[] messageConverters = private HttpMessageConverter<?>[] messageConverters =
new HttpMessageConverter[]{new ByteArrayHttpMessageConverter(), new StringHttpMessageConverter(), new HttpMessageConverter[] {new ByteArrayHttpMessageConverter(), new StringHttpMessageConverter(),
new FormHttpMessageConverter(), new SourceHttpMessageConverter()}; new FormHttpMessageConverter(), new SourceHttpMessageConverter()};
/** /**
* Set a custom ArgumentResolvers to use for special method parameter types. Such a custom ArgumentResolver will kick * Set a custom ArgumentResolvers to use for special method parameter types.
* in first, having a chance to resolve an argument value before the standard argument handling kicks in. * <p>Such a custom ArgumentResolver will kick in first, having a chance to resolve
* an argument value before the standard argument handling kicks in.
*/ */
public void setCustomArgumentResolver(WebArgumentResolver argumentResolver) { public void setCustomArgumentResolver(WebArgumentResolver argumentResolver) {
this.customArgumentResolvers = new WebArgumentResolver[]{argumentResolver}; this.customArgumentResolvers = new WebArgumentResolver[]{argumentResolver};
} }
/** /**
* Set one or more custom ArgumentResolvers to use for special method parameter types. Any such custom ArgumentResolver * Set one or more custom ArgumentResolvers to use for special method parameter types.
* will kick in first, having a chance to resolve an argument value before the standard argument handling kicks in. * <p>Any such custom ArgumentResolver will kick in first, having a chance to resolve
* an argument value before the standard argument handling kicks in.
*/ */
public void setCustomArgumentResolvers(WebArgumentResolver[] argumentResolvers) { public void setCustomArgumentResolvers(WebArgumentResolver[] argumentResolvers) {
this.customArgumentResolvers = argumentResolvers; this.customArgumentResolvers = argumentResolvers;
@ -288,7 +290,10 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
protected Object resolveStandardArgument(Class parameterType, NativeWebRequest webRequest, protected Object resolveStandardArgument(Class parameterType, NativeWebRequest webRequest,
Exception thrownException) throws Exception { Exception thrownException) throws Exception {
if (WebRequest.class.isAssignableFrom(parameterType)) { if (parameterType.isInstance(thrownException)) {
return thrownException;
}
else if (WebRequest.class.isAssignableFrom(parameterType)) {
return webRequest; return webRequest;
} }
@ -322,9 +327,6 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
else if (Writer.class.isAssignableFrom(parameterType)) { else if (Writer.class.isAssignableFrom(parameterType)) {
return response.getWriter(); return response.getWriter();
} }
else if (parameterType.isInstance(thrownException)) {
return thrownException;
}
else { else {
return WebArgumentResolver.UNRESOLVED; return WebArgumentResolver.UNRESOLVED;