|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2016 the original author or authors. |
| 2 | + * Copyright 2002-2017 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
23 | 23 | import org.springframework.http.client.ClientHttpRequestFactory;
|
24 | 24 | import org.springframework.http.client.ClientHttpRequestInterceptor;
|
25 | 25 | import org.springframework.http.client.InterceptingClientHttpRequestFactory;
|
| 26 | +import org.springframework.lang.Nullable; |
26 | 27 | import org.springframework.util.CollectionUtils;
|
27 | 28 |
|
28 | 29 | /**
|
29 |
| - * Base class for {@link org.springframework.web.client.RestTemplate} and other HTTP accessing gateway helpers, adding |
30 |
| - * interceptor-related properties to {@link HttpAccessor}'s common properties. |
| 30 | + * Base class for {@link org.springframework.web.client.RestTemplate} |
| 31 | + * and other HTTP accessing gateway helpers, adding interceptor-related |
| 32 | + * properties to {@link HttpAccessor}'s common properties. |
31 | 33 | *
|
32 |
| - * <p>Not intended to be used directly. See {@link org.springframework.web.client.RestTemplate}. |
| 34 | + * <p>Not intended to be used directly. |
| 35 | + * See {@link org.springframework.web.client.RestTemplate} for an entry point. |
33 | 36 | *
|
34 | 37 | * @author Arjen Poutsma
|
| 38 | + * @author Juergen Hoeller |
| 39 | + * @since 3.0 |
| 40 | + * @see ClientHttpRequestInterceptor |
| 41 | + * @see InterceptingClientHttpRequestFactory |
| 42 | + * @see org.springframework.web.client.RestTemplate |
35 | 43 | */
|
36 | 44 | public abstract class InterceptingHttpAccessor extends HttpAccessor {
|
37 | 45 |
|
38 |
| - private List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(); |
| 46 | + private final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(); |
| 47 | + |
| 48 | + @Nullable |
| 49 | + private volatile ClientHttpRequestFactory interceptingRequestFactory; |
| 50 | + |
39 | 51 |
|
40 | 52 | /**
|
41 |
| - * Sets the request interceptors that this accessor should use. |
| 53 | + * Set the request interceptors that this accessor should use. |
| 54 | + * <p>The interceptors will get sorted according to their order |
| 55 | + * once the {@link ClientHttpRequestFactory} will be built. |
| 56 | + * @see #getRequestFactory() |
| 57 | + * @see AnnotationAwareOrderComparator |
42 | 58 | */
|
43 | 59 | public void setInterceptors(List<ClientHttpRequestInterceptor> interceptors) {
|
44 |
| - AnnotationAwareOrderComparator.sort(interceptors); |
45 |
| - this.interceptors = interceptors; |
| 60 | + // Take getInterceptors() List as-is when passed in here |
| 61 | + if (this.interceptors != interceptors) { |
| 62 | + this.interceptors.clear(); |
| 63 | + this.interceptors.addAll(interceptors); |
| 64 | + AnnotationAwareOrderComparator.sort(this.interceptors); |
| 65 | + } |
46 | 66 | }
|
47 | 67 |
|
48 | 68 | /**
|
49 |
| - * Return the request interceptor that this accessor uses. |
| 69 | + * Return the request interceptors that this accessor uses. |
| 70 | + * <p>The returned {@link List} is active and may get appended to. |
50 | 71 | */
|
51 | 72 | public List<ClientHttpRequestInterceptor> getInterceptors() {
|
52 |
| - return interceptors; |
| 73 | + return this.interceptors; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * {@inheritDoc} |
| 78 | + */ |
| 79 | + @Override |
| 80 | + public void setRequestFactory(ClientHttpRequestFactory requestFactory) { |
| 81 | + super.setRequestFactory(requestFactory); |
| 82 | + this.interceptingRequestFactory = null; |
53 | 83 | }
|
54 | 84 |
|
| 85 | + /** |
| 86 | + * Overridden to expose an {@link InterceptingClientHttpRequestFactory} |
| 87 | + * if necessary. |
| 88 | + * @see #getInterceptors() |
| 89 | + */ |
55 | 90 | @Override
|
56 | 91 | public ClientHttpRequestFactory getRequestFactory() {
|
57 |
| - ClientHttpRequestFactory delegate = super.getRequestFactory(); |
58 |
| - if (!CollectionUtils.isEmpty(getInterceptors())) { |
59 |
| - return new InterceptingClientHttpRequestFactory(delegate, getInterceptors()); |
| 92 | + List<ClientHttpRequestInterceptor> interceptors = getInterceptors(); |
| 93 | + if (!CollectionUtils.isEmpty(interceptors)) { |
| 94 | + ClientHttpRequestFactory factory = this.interceptingRequestFactory; |
| 95 | + if (factory == null) { |
| 96 | + factory = new InterceptingClientHttpRequestFactory(super.getRequestFactory(), interceptors); |
| 97 | + this.interceptingRequestFactory = factory; |
| 98 | + } |
| 99 | + return factory; |
60 | 100 | }
|
61 | 101 | else {
|
62 |
| - return delegate; |
| 102 | + return super.getRequestFactory(); |
63 | 103 | }
|
64 | 104 | }
|
65 | 105 |
|
|
0 commit comments