001/* 002 * Copyright 2002-2019 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 java.util.Collections; 020import java.util.List; 021 022import javax.servlet.http.HttpServletRequest; 023import javax.servlet.http.HttpServletResponse; 024 025import org.springframework.core.Ordered; 026import org.springframework.lang.Nullable; 027import org.springframework.web.servlet.HandlerExceptionResolver; 028import org.springframework.web.servlet.ModelAndView; 029 030/** 031 * A {@link HandlerExceptionResolver} that delegates to a list of other 032 * {@link HandlerExceptionResolver HandlerExceptionResolvers}. 033 * 034 * @author Rossen Stoyanchev 035 * @since 3.1 036 */ 037public class HandlerExceptionResolverComposite implements HandlerExceptionResolver, Ordered { 038 039 @Nullable 040 private List<HandlerExceptionResolver> resolvers; 041 042 private int order = Ordered.LOWEST_PRECEDENCE; 043 044 045 /** 046 * Set the list of exception resolvers to delegate to. 047 */ 048 public void setExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) { 049 this.resolvers = exceptionResolvers; 050 } 051 052 /** 053 * Return the list of exception resolvers to delegate to. 054 */ 055 public List<HandlerExceptionResolver> getExceptionResolvers() { 056 return (this.resolvers != null ? Collections.unmodifiableList(this.resolvers) : Collections.emptyList()); 057 } 058 059 public void setOrder(int order) { 060 this.order = order; 061 } 062 063 @Override 064 public int getOrder() { 065 return this.order; 066 } 067 068 069 /** 070 * Resolve the exception by iterating over the list of configured exception resolvers. 071 * <p>The first one to return a {@link ModelAndView} wins. Otherwise {@code null} is returned. 072 */ 073 @Override 074 @Nullable 075 public ModelAndView resolveException( 076 HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) { 077 078 if (this.resolvers != null) { 079 for (HandlerExceptionResolver handlerExceptionResolver : this.resolvers) { 080 ModelAndView mav = handlerExceptionResolver.resolveException(request, response, handler, ex); 081 if (mav != null) { 082 return mav; 083 } 084 } 085 } 086 return null; 087 } 088 089}