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