Browse Source

Polish

pull/1462/merge
Rossen Stoyanchev 8 years ago
parent
commit
782c595cf7
  1. 30
      spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistration.java
  2. 13
      spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistry.java
  3. 25
      spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/InterceptorRegistryTests.java

30
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistration.java

@ -34,7 +34,7 @@ import org.springframework.web.servlet.handler.MappedInterceptor;
* @author Keith Donald * @author Keith Donald
* @since 3.1 * @since 3.1
*/ */
public class InterceptorRegistration implements Ordered { public class InterceptorRegistration {
private final HandlerInterceptor interceptor; private final HandlerInterceptor interceptor;
@ -42,9 +42,10 @@ public class InterceptorRegistration implements Ordered {
private final List<String> excludePatterns = new ArrayList<>(); private final List<String> excludePatterns = new ArrayList<>();
private int order = 0;
private PathMatcher pathMatcher; private PathMatcher pathMatcher;
private int order = 0;
/** /**
* Creates an {@link InterceptorRegistration} instance. * Creates an {@link InterceptorRegistration} instance.
@ -70,6 +71,18 @@ public class InterceptorRegistration implements Ordered {
return this; return this;
} }
/**
* An order position to be used, default is 0.
*/
public InterceptorRegistration order(int order){
this.order = order;
return this;
}
protected int getOrder() {
return this.order;
}
/** /**
* A PathMatcher implementation to use with this interceptor. This is an optional, * A PathMatcher implementation to use with this interceptor. This is an optional,
* advanced property required only if using custom PathMatcher implementations * advanced property required only if using custom PathMatcher implementations
@ -101,17 +114,4 @@ public class InterceptorRegistration implements Ordered {
return mappedInterceptor; 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;
}
} }

13
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistry.java

@ -17,10 +17,12 @@
package org.springframework.web.servlet.config.annotation; package org.springframework.web.servlet.config.annotation;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.springframework.core.OrderComparator; import org.springframework.core.OrderComparator;
import org.springframework.core.Ordered;
import org.springframework.web.context.request.WebRequestInterceptor; import org.springframework.web.context.request.WebRequestInterceptor;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter; import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter;
@ -67,9 +69,18 @@ public class InterceptorRegistry {
*/ */
protected List<Object> getInterceptors() { protected List<Object> getInterceptors() {
return this.registrations.stream() return this.registrations.stream()
.sorted(OrderComparator.INSTANCE) .sorted(INTERCEPTOR_ORDER_COMPARATOR)
.map(InterceptorRegistration::getInterceptor) .map(InterceptorRegistration::getInterceptor)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
private static final Comparator<Object> INTERCEPTOR_ORDER_COMPARATOR =
OrderComparator.INSTANCE.withSourceProvider(object -> {
if (object instanceof InterceptorRegistration) {
return (Ordered) ((InterceptorRegistration) object)::getOrder;
}
return null;
});
} }

25
spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/InterceptorRegistryTests.java

@ -40,7 +40,10 @@ import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapt
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.theme.ThemeChangeInterceptor; import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/** /**
* Test fixture with a {@link InterceptorRegistry}, two {@link HandlerInterceptor}s and two * Test fixture with a {@link InterceptorRegistry}, two {@link HandlerInterceptor}s and two
@ -198,25 +201,25 @@ public class InterceptorRegistryTests {
@Test @Test
public void orderedInterceptors() throws Exception { public void orderedInterceptors() throws Exception {
registry.addInterceptor(interceptor1).order(Ordered.LOWEST_PRECEDENCE); this.registry.addInterceptor(this.interceptor1).order(Ordered.LOWEST_PRECEDENCE);
registry.addInterceptor(interceptor2).order(Ordered.HIGHEST_PRECEDENCE); this.registry.addInterceptor(this.interceptor2).order(Ordered.HIGHEST_PRECEDENCE);
List<Object> interceptors = registry.getInterceptors(); List<Object> interceptors = this.registry.getInterceptors();
assertEquals(2, interceptors.size()); assertEquals(2, interceptors.size());
assertSame(interceptor2, interceptors.get(0)); assertSame(this.interceptor2, interceptors.get(0));
assertSame(interceptor1, interceptors.get(1)); assertSame(this.interceptor1, interceptors.get(1));
} }
@Test @Test
public void nonOrderedInterceptors() throws Exception { public void nonOrderedInterceptors() throws Exception {
registry.addInterceptor(interceptor1).order(0); this.registry.addInterceptor(this.interceptor1).order(0);
registry.addInterceptor(interceptor2).order(0); this.registry.addInterceptor(this.interceptor2).order(0);
List<Object> interceptors = registry.getInterceptors(); List<Object> interceptors = this.registry.getInterceptors();
assertEquals(2, interceptors.size()); assertEquals(2, interceptors.size());
assertSame(interceptor1, interceptors.get(0)); assertSame(this.interceptor1, interceptors.get(0));
assertSame(interceptor2, interceptors.get(1)); assertSame(this.interceptor2, interceptors.get(1));
} }
} }

Loading…
Cancel
Save