Merge branch '5.2.x'
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -19,6 +19,7 @@ package org.springframework.http.client;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
@@ -66,6 +67,9 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
|
||||
|
||||
private boolean bufferRequestBody = true;
|
||||
|
||||
@Nullable
|
||||
private BiFunction<HttpMethod, URI, HttpContext> httpContextFactory;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new instance of the {@code HttpComponentsClientHttpRequestFactory}
|
||||
@@ -157,6 +161,19 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
|
||||
this.bufferRequestBody = bufferRequestBody;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure a factory to pre-create the {@link HttpContext} for each request.
|
||||
* <p>This may be useful for example in mutual TLS authentication where a
|
||||
* different {@code RestTemplate} for each client certificate such that
|
||||
* all calls made through a given {@code RestTemplate} instance as associated
|
||||
* for the same client identity. {@link HttpClientContext#setUserToken(Object)}
|
||||
* can be used to specify a fixed user token for all requests.
|
||||
* @param httpContextFactory the context factory to use
|
||||
* @since 5.2.7
|
||||
*/
|
||||
public void setHttpContextFactory(BiFunction<HttpMethod, URI, HttpContext> httpContextFactory) {
|
||||
this.httpContextFactory = httpContextFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
|
||||
@@ -296,7 +313,7 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
|
||||
*/
|
||||
@Nullable
|
||||
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
|
||||
return null;
|
||||
return (this.httpContextFactory != null ? this.httpContextFactory.apply(httpMethod, uri) : null);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -35,11 +35,14 @@ import org.springframework.util.ObjectUtils;
|
||||
/**
|
||||
* Spring's default implementation of the {@link ResponseErrorHandler} interface.
|
||||
*
|
||||
* <p>This error handler checks for the status code on the {@link ClientHttpResponse}:
|
||||
* Any code with series {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR}
|
||||
* or {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR} is considered to be
|
||||
* an error; this behavior can be changed by overriding the {@link #hasError(HttpStatus)}
|
||||
* method. Unknown status codes will be ignored by {@link #hasError(ClientHttpResponse)}.
|
||||
* <p>This error handler checks for the status code on the
|
||||
* {@link ClientHttpResponse}. Any code in the 4xx or 5xx series is considered
|
||||
* to be an error. This behavior can be changed by overriding
|
||||
* {@link #hasError(HttpStatus)}. Unknown status codes will be ignored by
|
||||
* {@link #hasError(ClientHttpResponse)}.
|
||||
*
|
||||
* <p>See {@link #handleError(ClientHttpResponse)} for more details on specific
|
||||
* exception types.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Rossen Stoyanchev
|
||||
@@ -93,8 +96,18 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
|
||||
}
|
||||
|
||||
/**
|
||||
* Delegates to {@link #handleError(ClientHttpResponse, HttpStatus)} with the
|
||||
* response status code.
|
||||
* Handle the error in the given response with the given resolved status code.
|
||||
* <p>The default implementation throws:
|
||||
* <ul>
|
||||
* <li>{@link HttpClientErrorException} if the status code is in the 4xx
|
||||
* series, or one of its sub-classes such as
|
||||
* {@link HttpClientErrorException.BadRequest} and others.
|
||||
* <li>{@link HttpServerErrorException} if the status code is in the 5xx
|
||||
* series, or one of its sub-classes such as
|
||||
* {@link HttpServerErrorException.InternalServerError} and others.
|
||||
* <li>{@link UnknownHttpStatusCodeException} for error status codes not in the
|
||||
* {@link HttpStatus} enum range.
|
||||
* </ul>
|
||||
* @throws UnknownHttpStatusCodeException in case of an unresolvable status code
|
||||
* @see #handleError(ClientHttpResponse, HttpStatus)
|
||||
*/
|
||||
@@ -148,12 +161,13 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the error in the given response with the given resolved status code.
|
||||
* <p>The default implementation throws an {@link HttpClientErrorException}
|
||||
* if the status code is {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR
|
||||
* CLIENT_ERROR}, an {@link HttpServerErrorException} if it is
|
||||
* {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR SERVER_ERROR},
|
||||
* or an {@link UnknownHttpStatusCodeException} in other cases.
|
||||
* Handle the error based on the resolved status code.
|
||||
*
|
||||
* <p>The default implementation delegates to
|
||||
* {@link HttpClientErrorException#create} for errors in the 4xx range, to
|
||||
* {@link HttpServerErrorException#create} for errors in the 5xx range,
|
||||
* or otherwise raises {@link UnknownHttpStatusCodeException}.
|
||||
*
|
||||
* @since 5.0
|
||||
* @see HttpClientErrorException#create
|
||||
* @see HttpServerErrorException#create
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -41,7 +41,14 @@ import org.springframework.util.StringUtils;
|
||||
public class DefaultUriBuilderFactory implements UriBuilderFactory {
|
||||
|
||||
/**
|
||||
* Enum to represent multiple URI encoding strategies.
|
||||
* Enum to represent multiple URI encoding strategies. The following are
|
||||
* available:
|
||||
* <ul>
|
||||
* <li>{@link #TEMPLATE_AND_VALUES}
|
||||
* <li>{@link #VALUES_ONLY}
|
||||
* <li>{@link #URI_COMPONENT}
|
||||
* <li>{@link #NONE}
|
||||
* </ul>
|
||||
* @see #setEncodingMode
|
||||
*/
|
||||
public enum EncodingMode {
|
||||
@@ -130,16 +137,13 @@ public class DefaultUriBuilderFactory implements UriBuilderFactory {
|
||||
|
||||
|
||||
/**
|
||||
* Set the encoding mode to use.
|
||||
* Set the {@link EncodingMode encoding mode} to use.
|
||||
* <p>By default this is set to {@link EncodingMode#TEMPLATE_AND_VALUES
|
||||
* EncodingMode.TEMPLATE_AND_VALUES}.
|
||||
* <p><strong>Note:</strong> In 5.1 the default was changed from
|
||||
* {@link EncodingMode#URI_COMPONENT EncodingMode.URI_COMPONENT}.
|
||||
* Consequently the {@code WebClient}, which relies on the built-in default
|
||||
* has also been switched to the new default. The {@code RestTemplate}
|
||||
* however sets this explicitly to {@link EncodingMode#URI_COMPONENT
|
||||
* EncodingMode.URI_COMPONENT} explicitly for historic and backwards
|
||||
* compatibility reasons.
|
||||
* <p><strong>Note:</strong> Prior to 5.1 the default was
|
||||
* {@link EncodingMode#URI_COMPONENT EncodingMode.URI_COMPONENT}
|
||||
* therefore the {@code WebClient} {@code RestTemplate} have switched their
|
||||
* default behavior.
|
||||
* @param encodingMode the encoding mode to use
|
||||
*/
|
||||
public void setEncodingMode(EncodingMode encodingMode) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -77,22 +77,69 @@ public interface UriBuilder {
|
||||
UriBuilder port(@Nullable String port);
|
||||
|
||||
/**
|
||||
* Append the given path to the existing path of this builder.
|
||||
* The given path may contain URI template variables.
|
||||
* Append to the path of this builder.
|
||||
* <p>The given value is appended as-is without any checks for slashes other
|
||||
* than to clean up duplicates. For example:
|
||||
* <pre class="code">
|
||||
*
|
||||
* builder.path("/first-").path("value/").path("/{id}").build("123")
|
||||
*
|
||||
* // Results is "/first-value/123"
|
||||
* </pre>
|
||||
* <p>By contrast {@link #pathSegment(String...)} builds the path from
|
||||
* individual path segments and in that case slashes are inserted transparently.
|
||||
* In some cases you may use a combination of both {@code pathSegment} and
|
||||
* {@code path}. For example:
|
||||
* <pre class="code">
|
||||
*
|
||||
* builder.pathSegment("first-value", "second-value").path("/")
|
||||
*
|
||||
* // Results is "/first-value/second-value/"
|
||||
*
|
||||
* </pre>
|
||||
* <p>If a URI variable value contains slashes, whether those are encoded or
|
||||
* not depends on the configured encoding mode. See
|
||||
* {@link UriComponentsBuilder#encode()}, or if using
|
||||
* {@code UriComponentsBuilder} via {@link DefaultUriBuilderFactory}
|
||||
* (e.g. {@code WebClient} or {@code RestTemplate}) see its
|
||||
* {@link DefaultUriBuilderFactory#setEncodingMode encodingMode} property.
|
||||
* Also see the <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#web-uri-encoding">
|
||||
* URI Encoding</a> section of the reference docs.
|
||||
* @param path the URI path
|
||||
*/
|
||||
UriBuilder path(String path);
|
||||
|
||||
/**
|
||||
* Set the path of this builder overriding the existing path values.
|
||||
* Override the existing path.
|
||||
* @param path the URI path, or {@code null} for an empty path
|
||||
*/
|
||||
UriBuilder replacePath(@Nullable String path);
|
||||
|
||||
/**
|
||||
* Append path segments to the existing path. Each path segment may contain
|
||||
* URI template variables and should not contain any slashes.
|
||||
* Use {@code path("/")} subsequently to ensure a trailing slash.
|
||||
* Append to the path using path segments. For example:
|
||||
* <pre class="code">
|
||||
*
|
||||
* builder.pathSegment("first-value", "second-value", "{id}").build("123")
|
||||
*
|
||||
* // Results is "/first-value/second-value/123"
|
||||
*
|
||||
* </pre>
|
||||
* <p>If slashes are present in a path segment, they are encoded:
|
||||
* <pre class="code">
|
||||
*
|
||||
* builder.pathSegment("ba/z", "{id}").build("a/b")
|
||||
*
|
||||
* // Results is "/ba%2Fz/a%2Fb"
|
||||
*
|
||||
* </pre>
|
||||
* To insert a trailing slash, use the {@link #path} builder method:
|
||||
* <pre class="code">
|
||||
*
|
||||
* builder.pathSegment("first-value", "second-value").path("/")
|
||||
*
|
||||
* // Results is "/first-value/second-value/"
|
||||
*
|
||||
* </pre>
|
||||
* @param pathSegments the URI path segments
|
||||
*/
|
||||
UriBuilder pathSegment(String... pathSegments) throws IllegalArgumentException;
|
||||
|
||||
Reference in New Issue
Block a user