Skip to content

Commit f93cb2f

Browse files
committed
Support ordered interceptors in RestTemplate
This commit sorts `ClientHttpRequestInterceptor`s when those are set in `InterceptingHttpAccessor` (which `RestTemplate` extends from). Interceptors can now be annotated with `@Order` or implements `Ordered` to reflect their order of execution for each request. Issue: SPR-13971
1 parent ab7107c commit f93cb2f

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

spring-web/src/main/java/org/springframework/http/client/support/InterceptingHttpAccessor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22+
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
2223
import org.springframework.http.client.ClientHttpRequestFactory;
2324
import org.springframework.http.client.ClientHttpRequestInterceptor;
2425
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
@@ -40,6 +41,7 @@ public abstract class InterceptingHttpAccessor extends HttpAccessor {
4041
* Sets the request interceptors that this accessor should use.
4142
*/
4243
public void setInterceptors(List<ClientHttpRequestInterceptor> interceptors) {
44+
AnnotationAwareOrderComparator.sort(interceptors);
4345
this.interceptors = interceptors;
4446
}
4547

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.client.support;
18+
19+
import static org.junit.Assert.*;
20+
21+
import java.io.IOException;
22+
import java.util.Arrays;
23+
import java.util.List;
24+
25+
import org.hamcrest.Matchers;
26+
import org.junit.Test;
27+
28+
import org.springframework.core.Ordered;
29+
import org.springframework.core.annotation.Order;
30+
import org.springframework.http.HttpRequest;
31+
import org.springframework.http.client.ClientHttpRequestExecution;
32+
import org.springframework.http.client.ClientHttpRequestInterceptor;
33+
import org.springframework.http.client.ClientHttpResponse;
34+
35+
/**
36+
* Tests for {@link InterceptingHttpAccessor}.
37+
*
38+
* @author Brian Clozel
39+
*/
40+
public class InterceptingHttpAccessorTests {
41+
42+
@Test
43+
public void getInterceptors() throws Exception {
44+
TestInterceptingHttpAccessor accessor = new TestInterceptingHttpAccessor();
45+
List<ClientHttpRequestInterceptor> interceptors = Arrays.asList(
46+
new SecondClientHttpRequestInterceptor(),
47+
new ThirdClientHttpRequestInterceptor(),
48+
new FirstClientHttpRequestInterceptor()
49+
50+
);
51+
accessor.setInterceptors(interceptors);
52+
53+
assertThat(accessor.getInterceptors().get(0), Matchers.instanceOf(FirstClientHttpRequestInterceptor.class));
54+
assertThat(accessor.getInterceptors().get(1), Matchers.instanceOf(SecondClientHttpRequestInterceptor.class));
55+
assertThat(accessor.getInterceptors().get(2), Matchers.instanceOf(ThirdClientHttpRequestInterceptor.class));
56+
}
57+
58+
private class TestInterceptingHttpAccessor extends InterceptingHttpAccessor {
59+
}
60+
61+
@Order(1)
62+
private class FirstClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
63+
@Override
64+
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
65+
ClientHttpRequestExecution execution) throws IOException {
66+
return null;
67+
}
68+
}
69+
70+
private class SecondClientHttpRequestInterceptor implements ClientHttpRequestInterceptor, Ordered {
71+
@Override
72+
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
73+
ClientHttpRequestExecution execution) throws IOException {
74+
return null;
75+
}
76+
77+
@Override
78+
public int getOrder() {
79+
return 2;
80+
}
81+
}
82+
83+
private class ThirdClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
84+
@Override
85+
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
86+
ClientHttpRequestExecution execution) throws IOException {
87+
return null;
88+
}
89+
}
90+
91+
}

0 commit comments

Comments
 (0)