Browse Source

Ordered WebMvcConfigurer interceptor registrations

Issue: SPR-15620
pull/1462/merge
Eko Kurniawan Khannedy 9 years ago committed by Rossen Stoyanchev
parent
commit
ac68cc35c2
  1. 17
      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

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

@ -20,6 +20,7 @@ import java.util.ArrayList; @@ -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; @@ -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 { @@ -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 { @@ -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;
}
}

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

@ -1,5 +1,5 @@ @@ -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; @@ -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 { @@ -64,11 +66,10 @@ public class InterceptorRegistry {
* Return all registered interceptors.
*/
protected List<Object> getInterceptors() {
List<Object> 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());
}
}

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

@ -25,6 +25,7 @@ import org.junit.Before; @@ -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.*; @@ -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 { @@ -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<Object> 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<Object> interceptors = registry.getInterceptors();
assertEquals(2, interceptors.size());
assertSame(interceptor1, interceptors.get(0));
assertSame(interceptor2, interceptors.get(1));
}
}

Loading…
Cancel
Save