HandlerExecutionChain prevents re-adding the interceptors array to the list (and declares varargs now)
Issue: SPR-12566
This commit is contained in:
parent
d55af2b445
commit
6b3023c2aa
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2014 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -21,6 +21,7 @@ import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler execution chain, consisting of handler object and any handler interceptors.
|
* Handler execution chain, consisting of handler object and any handler interceptors.
|
||||||
|
@ -45,7 +46,7 @@ public class HandlerExecutionChain {
|
||||||
* @param handler the handler object to execute
|
* @param handler the handler object to execute
|
||||||
*/
|
*/
|
||||||
public HandlerExecutionChain(Object handler) {
|
public HandlerExecutionChain(Object handler) {
|
||||||
this(handler, null);
|
this(handler, (HandlerInterceptor[]) null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -54,7 +55,7 @@ public class HandlerExecutionChain {
|
||||||
* @param interceptors the array of interceptors to apply
|
* @param interceptors the array of interceptors to apply
|
||||||
* (in the given order) before the handler itself executes
|
* (in the given order) before the handler itself executes
|
||||||
*/
|
*/
|
||||||
public HandlerExecutionChain(Object handler, HandlerInterceptor[] interceptors) {
|
public HandlerExecutionChain(Object handler, HandlerInterceptor... interceptors) {
|
||||||
if (handler instanceof HandlerExecutionChain) {
|
if (handler instanceof HandlerExecutionChain) {
|
||||||
HandlerExecutionChain originalChain = (HandlerExecutionChain) handler;
|
HandlerExecutionChain originalChain = (HandlerExecutionChain) handler;
|
||||||
this.handler = originalChain.getHandler();
|
this.handler = originalChain.getHandler();
|
||||||
|
@ -78,25 +79,25 @@ public class HandlerExecutionChain {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addInterceptor(HandlerInterceptor interceptor) {
|
public void addInterceptor(HandlerInterceptor interceptor) {
|
||||||
initInterceptorList();
|
initInterceptorList().add(interceptor);
|
||||||
this.interceptorList.add(interceptor);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addInterceptors(HandlerInterceptor[] interceptors) {
|
public void addInterceptors(HandlerInterceptor... interceptors) {
|
||||||
if (interceptors != null) {
|
if (!ObjectUtils.isEmpty(interceptors)) {
|
||||||
initInterceptorList();
|
initInterceptorList().addAll(Arrays.asList(interceptors));
|
||||||
this.interceptorList.addAll(Arrays.asList(interceptors));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initInterceptorList() {
|
private List<HandlerInterceptor> initInterceptorList() {
|
||||||
if (this.interceptorList == null) {
|
if (this.interceptorList == null) {
|
||||||
this.interceptorList = new ArrayList<HandlerInterceptor>();
|
this.interceptorList = new ArrayList<HandlerInterceptor>();
|
||||||
|
if (this.interceptors != null) {
|
||||||
|
// An interceptor array specified through the constructor
|
||||||
|
this.interceptorList.addAll(Arrays.asList(this.interceptors));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (this.interceptors != null) {
|
this.interceptors = null;
|
||||||
this.interceptorList.addAll(Arrays.asList(this.interceptors));
|
return this.interceptorList;
|
||||||
this.interceptors = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2014 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -26,6 +26,7 @@ import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler execution chain, consisting of handler object and any handler interceptors.
|
* Handler execution chain, consisting of handler object and any handler interceptors.
|
||||||
|
@ -53,7 +54,7 @@ public class HandlerExecutionChain {
|
||||||
* @param handler the handler object to execute
|
* @param handler the handler object to execute
|
||||||
*/
|
*/
|
||||||
public HandlerExecutionChain(Object handler) {
|
public HandlerExecutionChain(Object handler) {
|
||||||
this(handler, null);
|
this(handler, (HandlerInterceptor[]) null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -62,7 +63,7 @@ public class HandlerExecutionChain {
|
||||||
* @param interceptors the array of interceptors to apply
|
* @param interceptors the array of interceptors to apply
|
||||||
* (in the given order) before the handler itself executes
|
* (in the given order) before the handler itself executes
|
||||||
*/
|
*/
|
||||||
public HandlerExecutionChain(Object handler, HandlerInterceptor[] interceptors) {
|
public HandlerExecutionChain(Object handler, HandlerInterceptor... interceptors) {
|
||||||
if (handler instanceof HandlerExecutionChain) {
|
if (handler instanceof HandlerExecutionChain) {
|
||||||
HandlerExecutionChain originalChain = (HandlerExecutionChain) handler;
|
HandlerExecutionChain originalChain = (HandlerExecutionChain) handler;
|
||||||
this.handler = originalChain.getHandler();
|
this.handler = originalChain.getHandler();
|
||||||
|
@ -76,6 +77,7 @@ public class HandlerExecutionChain {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the handler object to execute.
|
* Return the handler object to execute.
|
||||||
* @return the handler object
|
* @return the handler object
|
||||||
|
@ -85,25 +87,25 @@ public class HandlerExecutionChain {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addInterceptor(HandlerInterceptor interceptor) {
|
public void addInterceptor(HandlerInterceptor interceptor) {
|
||||||
initInterceptorList();
|
initInterceptorList().add(interceptor);
|
||||||
this.interceptorList.add(interceptor);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addInterceptors(HandlerInterceptor[] interceptors) {
|
public void addInterceptors(HandlerInterceptor... interceptors) {
|
||||||
if (interceptors != null) {
|
if (!ObjectUtils.isEmpty(interceptors)) {
|
||||||
initInterceptorList();
|
initInterceptorList().addAll(Arrays.asList(interceptors));
|
||||||
this.interceptorList.addAll(Arrays.asList(interceptors));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initInterceptorList() {
|
private List<HandlerInterceptor> initInterceptorList() {
|
||||||
if (this.interceptorList == null) {
|
if (this.interceptorList == null) {
|
||||||
this.interceptorList = new ArrayList<HandlerInterceptor>();
|
this.interceptorList = new ArrayList<HandlerInterceptor>();
|
||||||
|
if (this.interceptors != null) {
|
||||||
|
// An interceptor array specified through the constructor
|
||||||
|
this.interceptorList.addAll(Arrays.asList(this.interceptors));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (this.interceptors != null) {
|
this.interceptors = null;
|
||||||
this.interceptorList.addAll(Arrays.asList(this.interceptors));
|
return this.interceptorList;
|
||||||
this.interceptors = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -117,6 +119,7 @@ public class HandlerExecutionChain {
|
||||||
return this.interceptors;
|
return this.interceptors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apply preHandle methods of registered interceptors.
|
* Apply preHandle methods of registered interceptors.
|
||||||
* @return {@code true} if the execution chain should proceed with the
|
* @return {@code true} if the execution chain should proceed with the
|
||||||
|
@ -124,9 +127,10 @@ public class HandlerExecutionChain {
|
||||||
* that this interceptor has already dealt with the response itself.
|
* that this interceptor has already dealt with the response itself.
|
||||||
*/
|
*/
|
||||||
boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||||
if (getInterceptors() != null) {
|
HandlerInterceptor[] interceptors = getInterceptors();
|
||||||
for (int i = 0; i < getInterceptors().length; i++) {
|
if (!ObjectUtils.isEmpty(interceptors)) {
|
||||||
HandlerInterceptor interceptor = getInterceptors()[i];
|
for (int i = 0; i < interceptors.length; i++) {
|
||||||
|
HandlerInterceptor interceptor = interceptors[i];
|
||||||
if (!interceptor.preHandle(request, response, this.handler)) {
|
if (!interceptor.preHandle(request, response, this.handler)) {
|
||||||
triggerAfterCompletion(request, response, null);
|
triggerAfterCompletion(request, response, null);
|
||||||
return false;
|
return false;
|
||||||
|
@ -141,12 +145,12 @@ public class HandlerExecutionChain {
|
||||||
* Apply postHandle methods of registered interceptors.
|
* Apply postHandle methods of registered interceptors.
|
||||||
*/
|
*/
|
||||||
void applyPostHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView mv) throws Exception {
|
void applyPostHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView mv) throws Exception {
|
||||||
if (getInterceptors() == null) {
|
HandlerInterceptor[] interceptors = getInterceptors();
|
||||||
return;
|
if (!ObjectUtils.isEmpty(interceptors)) {
|
||||||
}
|
for (int i = interceptors.length - 1; i >= 0; i--) {
|
||||||
for (int i = getInterceptors().length - 1; i >= 0; i--) {
|
HandlerInterceptor interceptor = interceptors[i];
|
||||||
HandlerInterceptor interceptor = getInterceptors()[i];
|
interceptor.postHandle(request, response, this.handler, mv);
|
||||||
interceptor.postHandle(request, response, this.handler, mv);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,16 +162,16 @@ public class HandlerExecutionChain {
|
||||||
void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, Exception ex)
|
void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, Exception ex)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
|
|
||||||
if (getInterceptors() == null) {
|
HandlerInterceptor[] interceptors = getInterceptors();
|
||||||
return;
|
if (!ObjectUtils.isEmpty(interceptors)) {
|
||||||
}
|
for (int i = this.interceptorIndex; i >= 0; i--) {
|
||||||
for (int i = this.interceptorIndex; i >= 0; i--) {
|
HandlerInterceptor interceptor = interceptors[i];
|
||||||
HandlerInterceptor interceptor = getInterceptors()[i];
|
try {
|
||||||
try {
|
interceptor.afterCompletion(request, response, this.handler, ex);
|
||||||
interceptor.afterCompletion(request, response, this.handler, ex);
|
}
|
||||||
}
|
catch (Throwable ex2) {
|
||||||
catch (Throwable ex2) {
|
logger.error("HandlerInterceptor.afterCompletion threw exception", ex2);
|
||||||
logger.error("HandlerInterceptor.afterCompletion threw exception", ex2);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -176,22 +180,23 @@ public class HandlerExecutionChain {
|
||||||
* Apply afterConcurrentHandlerStarted callback on mapped AsyncHandlerInterceptors.
|
* Apply afterConcurrentHandlerStarted callback on mapped AsyncHandlerInterceptors.
|
||||||
*/
|
*/
|
||||||
void applyAfterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response) {
|
void applyAfterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response) {
|
||||||
if (getInterceptors() == null) {
|
HandlerInterceptor[] interceptors = getInterceptors();
|
||||||
return;
|
if (!ObjectUtils.isEmpty(interceptors)) {
|
||||||
}
|
for (int i = interceptors.length - 1; i >= 0; i--) {
|
||||||
for (int i = getInterceptors().length - 1; i >= 0; i--) {
|
if (interceptors[i] instanceof AsyncHandlerInterceptor) {
|
||||||
if (interceptors[i] instanceof AsyncHandlerInterceptor) {
|
try {
|
||||||
try {
|
AsyncHandlerInterceptor asyncInterceptor = (AsyncHandlerInterceptor) interceptors[i];
|
||||||
AsyncHandlerInterceptor asyncInterceptor = (AsyncHandlerInterceptor) this.interceptors[i];
|
asyncInterceptor.afterConcurrentHandlingStarted(request, response, this.handler);
|
||||||
asyncInterceptor.afterConcurrentHandlingStarted(request, response, this.handler);
|
}
|
||||||
}
|
catch (Throwable ex) {
|
||||||
catch (Throwable ex) {
|
logger.error("Interceptor [" + interceptors[i] + "] failed in afterConcurrentHandlingStarted", ex);
|
||||||
logger.error("Interceptor [" + interceptors[i] + "] failed in afterConcurrentHandlingStarted", ex);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delegates to the handler's {@code toString()}.
|
* Delegates to the handler's {@code toString()}.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2013 the original author or authors.
|
* Copyright 2002-2014 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -46,6 +46,7 @@ public class HandlerExecutionChainTests {
|
||||||
|
|
||||||
private AsyncHandlerInterceptor interceptor3;
|
private AsyncHandlerInterceptor interceptor3;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
this.request = new MockHttpServletRequest();
|
this.request = new MockHttpServletRequest();
|
||||||
|
@ -60,9 +61,12 @@ public class HandlerExecutionChainTests {
|
||||||
|
|
||||||
this.chain.addInterceptor(this.interceptor1);
|
this.chain.addInterceptor(this.interceptor1);
|
||||||
this.chain.addInterceptor(this.interceptor2);
|
this.chain.addInterceptor(this.interceptor2);
|
||||||
|
assertEquals(2, this.chain.getInterceptors().length);
|
||||||
this.chain.addInterceptor(this.interceptor3);
|
this.chain.addInterceptor(this.interceptor3);
|
||||||
|
assertEquals(3, this.chain.getInterceptors().length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void successScenario() throws Exception {
|
public void successScenario() throws Exception {
|
||||||
ModelAndView mav = new ModelAndView();
|
ModelAndView mav = new ModelAndView();
|
||||||
|
|
Loading…
Reference in New Issue