Execute HandlerInterceptors in registration order
Prior to this commit, registering `HandlerInterceptor`s using the `InterceptorRegistry` would not guarantee their order of execution. In fact, `HandlerInterceptor`s would always be executed before `MappedInterceptor`s. This change makes `MappedInterceptor` implement the `HandlerInterceptor` interface, in order to register all interceptors in a single ordered list. The order of execution of interceptors is now guaranteed in the `HandlerExecutionChain` built by `AbstractHandlerMapping`. Issue: SPR-12673
This commit is contained in:
@@ -19,7 +19,6 @@ package org.springframework.web.servlet.handler;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -78,9 +77,9 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
||||
|
||||
private final List<Object> interceptors = new ArrayList<Object>();
|
||||
|
||||
private final List<HandlerInterceptor> adaptedInterceptors = new ArrayList<HandlerInterceptor>();
|
||||
private final List<HandlerInterceptor> handlerInterceptors = new ArrayList<HandlerInterceptor>();
|
||||
|
||||
private final List<MappedInterceptor> mappedInterceptors = new ArrayList<MappedInterceptor>();
|
||||
private final List<MappedInterceptor> detectedMappedInterceptors = new ArrayList<MappedInterceptor>();
|
||||
|
||||
private CorsProcessor corsProcessor = new DefaultCorsProcessor();
|
||||
|
||||
@@ -246,7 +245,7 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
||||
@Override
|
||||
protected void initApplicationContext() throws BeansException {
|
||||
extendInterceptors(this.interceptors);
|
||||
detectMappedInterceptors(this.mappedInterceptors);
|
||||
detectMappedInterceptors(this.detectedMappedInterceptors);
|
||||
initInterceptors();
|
||||
}
|
||||
|
||||
@@ -288,14 +287,11 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
||||
if (interceptor == null) {
|
||||
throw new IllegalArgumentException("Entry number " + i + " in interceptors array is null");
|
||||
}
|
||||
if (interceptor instanceof MappedInterceptor) {
|
||||
this.mappedInterceptors.add((MappedInterceptor) interceptor);
|
||||
}
|
||||
else {
|
||||
this.adaptedInterceptors.add(adaptInterceptor(interceptor));
|
||||
}
|
||||
this.handlerInterceptors.add(adaptInterceptor(interceptor));
|
||||
}
|
||||
}
|
||||
this.handlerInterceptors.addAll(this.detectedMappedInterceptors);
|
||||
this.interceptors.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -327,8 +323,14 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
||||
* @return the array of {@link HandlerInterceptor}s, or {@code null} if none
|
||||
*/
|
||||
protected final HandlerInterceptor[] getAdaptedInterceptors() {
|
||||
int count = this.adaptedInterceptors.size();
|
||||
return (count > 0 ? this.adaptedInterceptors.toArray(new HandlerInterceptor[count]) : null);
|
||||
List<HandlerInterceptor> adaptedInterceptors = new ArrayList<HandlerInterceptor>();
|
||||
for (HandlerInterceptor interceptor : this.handlerInterceptors) {
|
||||
if (!(interceptor instanceof MappedInterceptor)) {
|
||||
adaptedInterceptors.add(interceptor);
|
||||
}
|
||||
}
|
||||
int count = adaptedInterceptors.size();
|
||||
return (count > 0 ? adaptedInterceptors.toArray(new HandlerInterceptor[count]) : null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -336,8 +338,14 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
||||
* @return the array of {@link MappedInterceptor}s, or {@code null} if none
|
||||
*/
|
||||
protected final MappedInterceptor[] getMappedInterceptors() {
|
||||
int count = this.mappedInterceptors.size();
|
||||
return (count > 0 ? this.mappedInterceptors.toArray(new MappedInterceptor[count]) : null);
|
||||
List<MappedInterceptor> mappedInterceptors = new ArrayList<MappedInterceptor>();
|
||||
for (HandlerInterceptor interceptor : this.handlerInterceptors) {
|
||||
if (interceptor instanceof MappedInterceptor) {
|
||||
mappedInterceptors.add((MappedInterceptor) interceptor);
|
||||
}
|
||||
}
|
||||
int count = mappedInterceptors.size();
|
||||
return (count > 0 ? mappedInterceptors.toArray(new MappedInterceptor[count]) : null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -397,8 +405,9 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
||||
* applicable interceptors.
|
||||
* <p>The default implementation builds a standard {@link HandlerExecutionChain}
|
||||
* with the given handler, the handler mapping's common interceptors, and any
|
||||
* {@link MappedInterceptor}s matching to the current request URL. Subclasses
|
||||
* may override this in order to extend/rearrange the list of interceptors.
|
||||
* {@link MappedInterceptor}s matching to the current request URL. Interceptors
|
||||
* are added in the order they were registered. Subclasses may override this
|
||||
* in order to extend/rearrange the list of interceptors.
|
||||
* <p><b>NOTE:</b> The passed-in handler object may be a raw handler or a
|
||||
* pre-built {@link HandlerExecutionChain}. This method should handle those
|
||||
* two cases explicitly, either building a new {@link HandlerExecutionChain}
|
||||
@@ -414,15 +423,19 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
||||
protected HandlerExecutionChain getHandlerExecutionChain(Object handler, HttpServletRequest request) {
|
||||
HandlerExecutionChain chain = (handler instanceof HandlerExecutionChain ?
|
||||
(HandlerExecutionChain) handler : new HandlerExecutionChain(handler));
|
||||
chain.addInterceptors(getAdaptedInterceptors());
|
||||
|
||||
String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
|
||||
for (MappedInterceptor mappedInterceptor : this.mappedInterceptors) {
|
||||
if (mappedInterceptor.matches(lookupPath, this.pathMatcher)) {
|
||||
chain.addInterceptor(mappedInterceptor.getInterceptor());
|
||||
for (HandlerInterceptor interceptor : this.handlerInterceptors) {
|
||||
if (interceptor instanceof MappedInterceptor) {
|
||||
MappedInterceptor mappedInterceptor = (MappedInterceptor) interceptor;
|
||||
if (mappedInterceptor.matches(lookupPath, this.pathMatcher)) {
|
||||
chain.addInterceptor(mappedInterceptor.getInterceptor());
|
||||
}
|
||||
}
|
||||
else {
|
||||
chain.addInterceptor(interceptor);
|
||||
}
|
||||
}
|
||||
|
||||
return chain;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,14 +16,18 @@
|
||||
|
||||
package org.springframework.web.servlet.handler;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.web.context.request.WebRequestInterceptor;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
/**
|
||||
* Contains a {@link HandlerInterceptor} along with include (and optionally
|
||||
* exclude) path patterns to which the interceptor should apply. Also provides
|
||||
* matching logic to test if the interceptor applies to a given request path.
|
||||
* Contains and delegates calls to a {@link HandlerInterceptor} along with
|
||||
* include (and optionally exclude) path patterns to which the interceptor should apply.
|
||||
* Also provides matching logic to test if the interceptor applies to a given request path.
|
||||
*
|
||||
* <p>A MappedInterceptor can be registered directly with any
|
||||
* {@link org.springframework.web.servlet.handler.AbstractHandlerMethodMapping
|
||||
@@ -34,9 +38,10 @@ import org.springframework.web.servlet.HandlerInterceptor;
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Brian Clozel
|
||||
* @since 3.0
|
||||
*/
|
||||
public final class MappedInterceptor {
|
||||
public final class MappedInterceptor implements HandlerInterceptor {
|
||||
|
||||
private final String[] includePatterns;
|
||||
|
||||
@@ -122,6 +127,21 @@ public final class MappedInterceptor {
|
||||
return this.interceptor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
return this.interceptor.preHandle(request, response, handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
|
||||
this.interceptor.postHandle(request, response, handler, modelAndView);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||
this.interceptor.afterCompletion(request, response, handler, ex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if the interceptor applies to the given request path.
|
||||
* @param lookupPath the current request path
|
||||
|
||||
Reference in New Issue
Block a user