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.config.annotation; 018 019import java.util.ArrayList; 020import java.util.Collections; 021import java.util.Comparator; 022import java.util.List; 023 024import org.springframework.core.OrderComparator; 025import org.springframework.web.context.request.WebRequestInterceptor; 026import org.springframework.web.servlet.HandlerInterceptor; 027import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter; 028 029/** 030 * Helps with configuring a list of mapped interceptors. 031 * 032 * @author Rossen Stoyanchev 033 * @author Keith Donald 034 * @since 3.1 035 */ 036public class InterceptorRegistry { 037 038 private final List<InterceptorRegistration> registrations = new ArrayList<InterceptorRegistration>(); 039 040 041 /** 042 * Adds the provided {@link HandlerInterceptor}. 043 * @param interceptor the interceptor to add 044 * @return An {@link InterceptorRegistration} that allows you optionally configure the 045 * registered interceptor further for example adding URL patterns it should apply to. 046 */ 047 public InterceptorRegistration addInterceptor(HandlerInterceptor interceptor) { 048 InterceptorRegistration registration = new InterceptorRegistration(interceptor); 049 this.registrations.add(registration); 050 return registration; 051 } 052 053 /** 054 * Adds the provided {@link WebRequestInterceptor}. 055 * @param interceptor the interceptor to add 056 * @return An {@link InterceptorRegistration} that allows you optionally configure the 057 * registered interceptor further for example adding URL patterns it should apply to. 058 */ 059 public InterceptorRegistration addWebRequestInterceptor(WebRequestInterceptor interceptor) { 060 WebRequestHandlerInterceptorAdapter adapted = new WebRequestHandlerInterceptorAdapter(interceptor); 061 InterceptorRegistration registration = new InterceptorRegistration(adapted); 062 this.registrations.add(registration); 063 return registration; 064 } 065 066 /** 067 * Return all registered interceptors. 068 */ 069 protected List<Object> getInterceptors() { 070 Collections.sort(this.registrations, INTERCEPTOR_ORDER_COMPARATOR); 071 List<Object> result = new ArrayList<Object>(this.registrations.size()); 072 for (InterceptorRegistration registration : this.registrations) { 073 result.add(registration.getInterceptor()); 074 } 075 return result; 076 } 077 078 079 private static final Comparator<Object> INTERCEPTOR_ORDER_COMPARATOR = 080 OrderComparator.INSTANCE.withSourceProvider(new OrderComparator.OrderSourceProvider() { 081 @Override 082 public Object getOrderSource(final Object object) { 083 if (object instanceof InterceptorRegistration) { 084 return ((InterceptorRegistration) object).toOrdered(); 085 } 086 return null; 087 } 088 }); 089 090 091}