From ac68cc35c2cf776d66c41e8544663f0db1f17166 Mon Sep 17 00:00:00 2001 From: Eko Kurniawan Khannedy Date: Sat, 3 Jun 2017 12:09:28 +0700 Subject: [PATCH] Ordered WebMvcConfigurer interceptor registrations Issue: SPR-15620 --- .../annotation/InterceptorRegistration.java | 17 ++++++++++++- .../annotation/InterceptorRegistry.java | 13 +++++----- .../annotation/InterceptorRegistryTests.java | 25 +++++++++++++++++++ 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistration.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistration.java index c91a6f80eef..5bd6ff8f9be 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistration.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistration.java @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.springframework.core.Ordered; import org.springframework.util.Assert; import org.springframework.util.PathMatcher; import org.springframework.util.StringUtils; @@ -33,7 +34,7 @@ import org.springframework.web.servlet.handler.MappedInterceptor; * @author Keith Donald * @since 3.1 */ -public class InterceptorRegistration { +public class InterceptorRegistration implements Ordered { private final HandlerInterceptor interceptor; @@ -43,6 +44,7 @@ public class InterceptorRegistration { private PathMatcher pathMatcher; + private int order = 0; /** * Creates an {@link InterceptorRegistration} instance. @@ -99,4 +101,17 @@ public class InterceptorRegistration { return mappedInterceptor; } + /** + * An order position to be used, default is 0. + */ + public InterceptorRegistration order(int order){ + this.order = order; + return this; + } + + @Override + public int getOrder() { + return order; + } + } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistry.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistry.java index 6104acde637..0e4f4357ebb 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistry.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistry.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -18,7 +18,9 @@ package org.springframework.web.servlet.config.annotation; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; +import org.springframework.core.OrderComparator; import org.springframework.web.context.request.WebRequestInterceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter; @@ -64,11 +66,10 @@ public class InterceptorRegistry { * Return all registered interceptors. */ protected List getInterceptors() { - List interceptors = new ArrayList<>(this.registrations.size()); - for (InterceptorRegistration registration : this.registrations) { - interceptors.add(registration.getInterceptor()); - } - return interceptors ; + return this.registrations.stream() + .sorted(OrderComparator.INSTANCE) + .map(InterceptorRegistration::getInterceptor) + .collect(Collectors.toList()); } } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/InterceptorRegistryTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/InterceptorRegistryTests.java index c6bc02f71d1..1c586d0aafd 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/InterceptorRegistryTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/InterceptorRegistryTests.java @@ -25,6 +25,7 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; +import org.springframework.core.Ordered; import org.springframework.lang.Nullable; import org.springframework.mock.web.test.MockHttpServletRequest; import org.springframework.mock.web.test.MockHttpServletResponse; @@ -46,6 +47,7 @@ import static org.junit.Assert.*; * {@link WebRequestInterceptor}s. * * @author Rossen Stoyanchev + * @author Eko Kurniawan Khannedy */ public class InterceptorRegistryTests { @@ -194,4 +196,27 @@ public class InterceptorRegistryTests { } } + @Test + public void orderedInterceptors() throws Exception { + registry.addInterceptor(interceptor1).order(Ordered.LOWEST_PRECEDENCE); + registry.addInterceptor(interceptor2).order(Ordered.HIGHEST_PRECEDENCE); + + List interceptors = registry.getInterceptors(); + assertEquals(2, interceptors.size()); + + assertSame(interceptor2, interceptors.get(0)); + assertSame(interceptor1, interceptors.get(1)); + } + + @Test + public void nonOrderedInterceptors() throws Exception { + registry.addInterceptor(interceptor1).order(0); + registry.addInterceptor(interceptor2).order(0); + + List interceptors = registry.getInterceptors(); + assertEquals(2, interceptors.size()); + + assertSame(interceptor1, interceptors.get(0)); + assertSame(interceptor2, interceptors.get(1)); + } }