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