001/*
002 * Copyright 2002-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.servlet.handler;
018
019import javax.servlet.http.HttpServletRequest;
020import javax.servlet.http.HttpServletResponse;
021
022import org.springframework.web.method.HandlerMethod;
023import org.springframework.web.servlet.ModelAndView;
024
025/**
026 * Abstract base class for
027 * {@link org.springframework.web.servlet.HandlerExceptionResolver HandlerExceptionResolver}
028 * implementations that support handling exceptions from handlers of type {@link HandlerMethod}.
029 *
030 * @author Rossen Stoyanchev
031 * @since 3.1
032 */
033public abstract class AbstractHandlerMethodExceptionResolver extends AbstractHandlerExceptionResolver {
034
035        /**
036         * Checks if the handler is a {@link HandlerMethod} and then delegates to the
037         * base class implementation of {@code #shouldApplyTo(HttpServletRequest, Object)}
038         * passing the bean of the {@code HandlerMethod}. Otherwise returns {@code false}.
039         */
040        @Override
041        protected boolean shouldApplyTo(HttpServletRequest request, Object handler) {
042                if (handler == null) {
043                        return super.shouldApplyTo(request, handler);
044                }
045                else if (handler instanceof HandlerMethod) {
046                        HandlerMethod handlerMethod = (HandlerMethod) handler;
047                        handler = handlerMethod.getBean();
048                        return super.shouldApplyTo(request, handler);
049                }
050                else {
051                        return false;
052                }
053        }
054
055        @Override
056        protected final ModelAndView doResolveException(
057                        HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
058
059                return doResolveHandlerMethodException(request, response, (HandlerMethod) handler, ex);
060        }
061
062        /**
063         * Actually resolve the given exception that got thrown during on handler execution,
064         * returning a ModelAndView that represents a specific error page if appropriate.
065         * <p>May be overridden in subclasses, in order to apply specific exception checks.
066         * Note that this template method will be invoked <i>after</i> checking whether this
067         * resolved applies ("mappedHandlers" etc), so an implementation may simply proceed
068         * with its actual exception handling.
069         * @param request current HTTP request
070         * @param response current HTTP response
071         * @param handlerMethod the executed handler method, or {@code null} if none chosen at the time
072         * of the exception (for example, if multipart resolution failed)
073         * @param ex the exception that got thrown during handler execution
074         * @return a corresponding ModelAndView to forward to, or {@code null} for default processing
075         */
076        protected abstract ModelAndView doResolveHandlerMethodException(
077                        HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod, Exception ex);
078
079}