|
20 | 20 | import java.io.OutputStream; |
21 | 21 | import java.net.URI; |
22 | 22 | import java.util.List; |
23 | | -import java.util.ListIterator; |
24 | 23 |
|
25 | 24 | import org.springframework.http.HttpHeaders; |
26 | 25 | import org.springframework.http.HttpMethod; |
@@ -69,39 +68,23 @@ public URI getURI() { |
69 | 68 |
|
70 | 69 | @Override |
71 | 70 | protected final ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException { |
72 | | - ClientHttpRequestExecution requestExecution = new DelegatingRequestExecution(this.requestFactory); |
73 | | - ListIterator<ClientHttpRequestInterceptor> iterator = this.interceptors.listIterator(this.interceptors.size()); |
74 | | - while (iterator.hasPrevious()) { |
75 | | - ClientHttpRequestInterceptor interceptor = iterator.previous(); |
76 | | - requestExecution = new InterceptingRequestExecution(interceptor, requestExecution); |
77 | | - } |
78 | | - return requestExecution.execute(this, bufferedOutput); |
| 71 | + return getExecution().execute(this, bufferedOutput); |
79 | 72 | } |
80 | 73 |
|
81 | | - |
82 | | - private static class InterceptingRequestExecution implements ClientHttpRequestExecution { |
83 | | - |
84 | | - private final ClientHttpRequestInterceptor interceptor; |
85 | | - |
86 | | - private final ClientHttpRequestExecution nextExecution; |
87 | | - |
88 | | - public InterceptingRequestExecution(ClientHttpRequestInterceptor interceptor, ClientHttpRequestExecution nextExecution) { |
89 | | - this.interceptor = interceptor; |
90 | | - this.nextExecution = nextExecution; |
91 | | - } |
92 | | - |
93 | | - @Override |
94 | | - public ClientHttpResponse execute(HttpRequest request, byte[] body) throws IOException { |
95 | | - return this.interceptor.intercept(request, body, this.nextExecution); |
96 | | - } |
97 | | - |
| 74 | + private ClientHttpRequestExecution getExecution() { |
| 75 | + ClientHttpRequestExecution execution = new EndOfChainRequestExecution(this.requestFactory); |
| 76 | + return this.interceptors.stream() |
| 77 | + .reduce(ClientHttpRequestInterceptor::andThen) |
| 78 | + .map(interceptor -> interceptor.apply(execution)) |
| 79 | + .orElse(execution); |
98 | 80 | } |
99 | 81 |
|
100 | | - private static class DelegatingRequestExecution implements ClientHttpRequestExecution { |
| 82 | + |
| 83 | + private static class EndOfChainRequestExecution implements ClientHttpRequestExecution { |
101 | 84 |
|
102 | 85 | private final ClientHttpRequestFactory requestFactory; |
103 | 86 |
|
104 | | - public DelegatingRequestExecution(ClientHttpRequestFactory requestFactory) { |
| 87 | + public EndOfChainRequestExecution(ClientHttpRequestFactory requestFactory) { |
105 | 88 | this.requestFactory = requestFactory; |
106 | 89 | } |
107 | 90 |
|
|
0 commit comments