|
|
|
@ -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. |
|
|
|
@ -23,43 +23,83 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator;
@@ -23,43 +23,83 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
|
|
|
|
import org.springframework.http.client.ClientHttpRequestFactory; |
|
|
|
|
import org.springframework.http.client.ClientHttpRequestInterceptor; |
|
|
|
|
import org.springframework.http.client.InterceptingClientHttpRequestFactory; |
|
|
|
|
import org.springframework.lang.Nullable; |
|
|
|
|
import org.springframework.util.CollectionUtils; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Base class for {@link org.springframework.web.client.RestTemplate} and other HTTP accessing gateway helpers, adding |
|
|
|
|
* interceptor-related properties to {@link HttpAccessor}'s common properties. |
|
|
|
|
* Base class for {@link org.springframework.web.client.RestTemplate} |
|
|
|
|
* and other HTTP accessing gateway helpers, adding interceptor-related |
|
|
|
|
* properties to {@link HttpAccessor}'s common properties. |
|
|
|
|
* |
|
|
|
|
* <p>Not intended to be used directly. See {@link org.springframework.web.client.RestTemplate}. |
|
|
|
|
* <p>Not intended to be used directly. |
|
|
|
|
* See {@link org.springframework.web.client.RestTemplate} for an entry point. |
|
|
|
|
* |
|
|
|
|
* @author Arjen Poutsma |
|
|
|
|
* @author Juergen Hoeller |
|
|
|
|
* @since 3.0 |
|
|
|
|
* @see ClientHttpRequestInterceptor |
|
|
|
|
* @see InterceptingClientHttpRequestFactory |
|
|
|
|
* @see org.springframework.web.client.RestTemplate |
|
|
|
|
*/ |
|
|
|
|
public abstract class InterceptingHttpAccessor extends HttpAccessor { |
|
|
|
|
|
|
|
|
|
private List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(); |
|
|
|
|
private final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
|
private volatile ClientHttpRequestFactory interceptingRequestFactory; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Sets the request interceptors that this accessor should use. |
|
|
|
|
* Set the request interceptors that this accessor should use. |
|
|
|
|
* <p>The interceptors will get sorted according to their order |
|
|
|
|
* once the {@link ClientHttpRequestFactory} will be built. |
|
|
|
|
* @see #getRequestFactory() |
|
|
|
|
* @see AnnotationAwareOrderComparator |
|
|
|
|
*/ |
|
|
|
|
public void setInterceptors(List<ClientHttpRequestInterceptor> interceptors) { |
|
|
|
|
AnnotationAwareOrderComparator.sort(interceptors); |
|
|
|
|
this.interceptors = interceptors; |
|
|
|
|
// Take getInterceptors() List as-is when passed in here
|
|
|
|
|
if (this.interceptors != interceptors) { |
|
|
|
|
this.interceptors.clear(); |
|
|
|
|
this.interceptors.addAll(interceptors); |
|
|
|
|
AnnotationAwareOrderComparator.sort(this.interceptors); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Return the request interceptor that this accessor uses. |
|
|
|
|
* Return the request interceptors that this accessor uses. |
|
|
|
|
* <p>The returned {@link List} is active and may get appended to. |
|
|
|
|
*/ |
|
|
|
|
public List<ClientHttpRequestInterceptor> getInterceptors() { |
|
|
|
|
return interceptors; |
|
|
|
|
return this.interceptors; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* {@inheritDoc} |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public void setRequestFactory(ClientHttpRequestFactory requestFactory) { |
|
|
|
|
super.setRequestFactory(requestFactory); |
|
|
|
|
this.interceptingRequestFactory = null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Overridden to expose an {@link InterceptingClientHttpRequestFactory} |
|
|
|
|
* if necessary. |
|
|
|
|
* @see #getInterceptors() |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public ClientHttpRequestFactory getRequestFactory() { |
|
|
|
|
ClientHttpRequestFactory delegate = super.getRequestFactory(); |
|
|
|
|
if (!CollectionUtils.isEmpty(getInterceptors())) { |
|
|
|
|
return new InterceptingClientHttpRequestFactory(delegate, getInterceptors()); |
|
|
|
|
List<ClientHttpRequestInterceptor> interceptors = getInterceptors(); |
|
|
|
|
if (!CollectionUtils.isEmpty(interceptors)) { |
|
|
|
|
ClientHttpRequestFactory factory = this.interceptingRequestFactory; |
|
|
|
|
if (factory == null) { |
|
|
|
|
factory = new InterceptingClientHttpRequestFactory(super.getRequestFactory(), interceptors); |
|
|
|
|
this.interceptingRequestFactory = factory; |
|
|
|
|
} |
|
|
|
|
return factory; |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
return delegate; |
|
|
|
|
return super.getRequestFactory(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|