|
|
|
@ -40,6 +40,7 @@ import org.apache.commons.logging.LogFactory; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.core.ParameterizedTypeReference; |
|
|
|
import org.springframework.core.ParameterizedTypeReference; |
|
|
|
import org.springframework.core.ResolvableType; |
|
|
|
import org.springframework.core.ResolvableType; |
|
|
|
|
|
|
|
import org.springframework.http.HttpCookie; |
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
import org.springframework.http.HttpMethod; |
|
|
|
import org.springframework.http.HttpMethod; |
|
|
|
import org.springframework.http.HttpRequest; |
|
|
|
import org.springframework.http.HttpRequest; |
|
|
|
@ -64,6 +65,8 @@ import org.springframework.http.converter.SmartHttpMessageConverter; |
|
|
|
import org.springframework.lang.Nullable; |
|
|
|
import org.springframework.lang.Nullable; |
|
|
|
import org.springframework.util.Assert; |
|
|
|
import org.springframework.util.Assert; |
|
|
|
import org.springframework.util.CollectionUtils; |
|
|
|
import org.springframework.util.CollectionUtils; |
|
|
|
|
|
|
|
import org.springframework.util.LinkedMultiValueMap; |
|
|
|
|
|
|
|
import org.springframework.util.MultiValueMap; |
|
|
|
import org.springframework.web.util.UriBuilder; |
|
|
|
import org.springframework.web.util.UriBuilder; |
|
|
|
import org.springframework.web.util.UriBuilderFactory; |
|
|
|
import org.springframework.web.util.UriBuilderFactory; |
|
|
|
|
|
|
|
|
|
|
|
@ -103,6 +106,9 @@ final class DefaultRestClient implements RestClient { |
|
|
|
@Nullable |
|
|
|
@Nullable |
|
|
|
private final HttpHeaders defaultHeaders; |
|
|
|
private final HttpHeaders defaultHeaders; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
|
|
|
|
private final MultiValueMap<String, String> defaultCookies; |
|
|
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
@Nullable |
|
|
|
private final Consumer<RequestHeadersSpec<?>> defaultRequest; |
|
|
|
private final Consumer<RequestHeadersSpec<?>> defaultRequest; |
|
|
|
|
|
|
|
|
|
|
|
@ -123,6 +129,7 @@ final class DefaultRestClient implements RestClient { |
|
|
|
@Nullable List<ClientHttpRequestInitializer> initializers, |
|
|
|
@Nullable List<ClientHttpRequestInitializer> initializers, |
|
|
|
UriBuilderFactory uriBuilderFactory, |
|
|
|
UriBuilderFactory uriBuilderFactory, |
|
|
|
@Nullable HttpHeaders defaultHeaders, |
|
|
|
@Nullable HttpHeaders defaultHeaders, |
|
|
|
|
|
|
|
@Nullable MultiValueMap<String, String> defaultCookies, |
|
|
|
@Nullable Consumer<RequestHeadersSpec<?>> defaultRequest, |
|
|
|
@Nullable Consumer<RequestHeadersSpec<?>> defaultRequest, |
|
|
|
@Nullable List<StatusHandler> statusHandlers, |
|
|
|
@Nullable List<StatusHandler> statusHandlers, |
|
|
|
List<HttpMessageConverter<?>> messageConverters, |
|
|
|
List<HttpMessageConverter<?>> messageConverters, |
|
|
|
@ -135,6 +142,7 @@ final class DefaultRestClient implements RestClient { |
|
|
|
this.interceptors = interceptors; |
|
|
|
this.interceptors = interceptors; |
|
|
|
this.uriBuilderFactory = uriBuilderFactory; |
|
|
|
this.uriBuilderFactory = uriBuilderFactory; |
|
|
|
this.defaultHeaders = defaultHeaders; |
|
|
|
this.defaultHeaders = defaultHeaders; |
|
|
|
|
|
|
|
this.defaultCookies = defaultCookies; |
|
|
|
this.defaultRequest = defaultRequest; |
|
|
|
this.defaultRequest = defaultRequest; |
|
|
|
this.defaultStatusHandlers = (statusHandlers != null ? new ArrayList<>(statusHandlers) : new ArrayList<>()); |
|
|
|
this.defaultStatusHandlers = (statusHandlers != null ? new ArrayList<>(statusHandlers) : new ArrayList<>()); |
|
|
|
this.messageConverters = messageConverters; |
|
|
|
this.messageConverters = messageConverters; |
|
|
|
@ -293,6 +301,8 @@ final class DefaultRestClient implements RestClient { |
|
|
|
|
|
|
|
|
|
|
|
private class DefaultRequestBodyUriSpec implements RequestBodyUriSpec { |
|
|
|
private class DefaultRequestBodyUriSpec implements RequestBodyUriSpec { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final String COOKIE_DELIMITER = "; "; |
|
|
|
|
|
|
|
|
|
|
|
private final HttpMethod httpMethod; |
|
|
|
private final HttpMethod httpMethod; |
|
|
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
@Nullable |
|
|
|
@ -301,6 +311,9 @@ final class DefaultRestClient implements RestClient { |
|
|
|
@Nullable |
|
|
|
@Nullable |
|
|
|
private HttpHeaders headers; |
|
|
|
private HttpHeaders headers; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
|
|
|
|
private MultiValueMap<String, String> cookies; |
|
|
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
@Nullable |
|
|
|
private InternalBody body; |
|
|
|
private InternalBody body; |
|
|
|
|
|
|
|
|
|
|
|
@ -356,6 +369,13 @@ final class DefaultRestClient implements RestClient { |
|
|
|
return this.headers; |
|
|
|
return this.headers; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private MultiValueMap<String, String> getCookies() { |
|
|
|
|
|
|
|
if (this.cookies == null) { |
|
|
|
|
|
|
|
this.cookies = new LinkedMultiValueMap<>(3); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return this.cookies; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public DefaultRequestBodyUriSpec header(String headerName, String... headerValues) { |
|
|
|
public DefaultRequestBodyUriSpec header(String headerName, String... headerValues) { |
|
|
|
for (String headerValue : headerValues) { |
|
|
|
for (String headerValue : headerValues) { |
|
|
|
@ -382,6 +402,18 @@ final class DefaultRestClient implements RestClient { |
|
|
|
return this; |
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public DefaultRequestBodyUriSpec cookie(String name, String value) { |
|
|
|
|
|
|
|
getCookies().add(name, value); |
|
|
|
|
|
|
|
return this; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public DefaultRequestBodyUriSpec cookies(Consumer<MultiValueMap<String, String>> cookiesConsumer) { |
|
|
|
|
|
|
|
cookiesConsumer.accept(getCookies()); |
|
|
|
|
|
|
|
return this; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public DefaultRequestBodyUriSpec contentType(MediaType contentType) { |
|
|
|
public DefaultRequestBodyUriSpec contentType(MediaType contentType) { |
|
|
|
getHeaders().setContentType(contentType); |
|
|
|
getHeaders().setContentType(contentType); |
|
|
|
@ -525,6 +557,12 @@ final class DefaultRestClient implements RestClient { |
|
|
|
try { |
|
|
|
try { |
|
|
|
uri = initUri(); |
|
|
|
uri = initUri(); |
|
|
|
HttpHeaders headers = initHeaders(); |
|
|
|
HttpHeaders headers = initHeaders(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MultiValueMap<String, String> cookies = initCookies(); |
|
|
|
|
|
|
|
if (!CollectionUtils.isEmpty(cookies)) { |
|
|
|
|
|
|
|
headers.put(HttpHeaders.COOKIE, List.of(cookiesToHeaderValue(cookies))); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ClientHttpRequest clientRequest = createRequest(uri); |
|
|
|
ClientHttpRequest clientRequest = createRequest(uri); |
|
|
|
clientRequest.getHeaders().addAll(headers); |
|
|
|
clientRequest.getHeaders().addAll(headers); |
|
|
|
Map<String, Object> attributes = getAttributes(); |
|
|
|
Map<String, Object> attributes = getAttributes(); |
|
|
|
@ -599,6 +637,28 @@ final class DefaultRestClient implements RestClient { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private MultiValueMap<String, String> initCookies() { |
|
|
|
|
|
|
|
MultiValueMap<String, String> mergedCookies = new LinkedMultiValueMap<>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!CollectionUtils.isEmpty(defaultCookies)) { |
|
|
|
|
|
|
|
mergedCookies.putAll(defaultCookies); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!CollectionUtils.isEmpty(this.cookies)) { |
|
|
|
|
|
|
|
mergedCookies.putAll(this.cookies); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return mergedCookies; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String cookiesToHeaderValue(MultiValueMap<String, String> cookies) { |
|
|
|
|
|
|
|
List<String> flatCookies = new ArrayList<>(); |
|
|
|
|
|
|
|
cookies.forEach((name, cookieValues) -> cookieValues.forEach(value -> |
|
|
|
|
|
|
|
flatCookies.add(new HttpCookie(name, value).toString()) |
|
|
|
|
|
|
|
)); |
|
|
|
|
|
|
|
return String.join(COOKIE_DELIMITER, flatCookies); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private ClientHttpRequest createRequest(URI uri) throws IOException { |
|
|
|
private ClientHttpRequest createRequest(URI uri) throws IOException { |
|
|
|
ClientHttpRequestFactory factory; |
|
|
|
ClientHttpRequestFactory factory; |
|
|
|
if (DefaultRestClient.this.interceptors != null) { |
|
|
|
if (DefaultRestClient.this.interceptors != null) { |
|
|
|
|