|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2018 the original author or authors. |
| 2 | + * Copyright 2002-2020 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.
|
|
19 | 19 | import java.io.Closeable;
|
20 | 20 | import java.io.IOException;
|
21 | 21 | import java.net.URI;
|
| 22 | +import java.util.function.BiFunction; |
22 | 23 |
|
23 | 24 | import org.apache.http.client.HttpClient;
|
24 | 25 | import org.apache.http.client.config.RequestConfig;
|
@@ -66,6 +67,9 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest
|
66 | 67 |
|
67 | 68 | private boolean bufferRequestBody = true;
|
68 | 69 |
|
| 70 | + @Nullable |
| 71 | + private BiFunction<HttpMethod, URI, HttpContext> httpContextFactory; |
| 72 | + |
69 | 73 |
|
70 | 74 | /**
|
71 | 75 | * Create a new instance of the {@code HttpComponentsClientHttpRequestFactory}
|
@@ -157,6 +161,19 @@ public void setBufferRequestBody(boolean bufferRequestBody) {
|
157 | 161 | this.bufferRequestBody = bufferRequestBody;
|
158 | 162 | }
|
159 | 163 |
|
| 164 | + /** |
| 165 | + * Configure a factory to pre-create the {@link HttpContext} for each request. |
| 166 | + * <p>This may be useful for example in mutual TLS authentication where a |
| 167 | + * different {@code RestTemplate} for each client certificate such that |
| 168 | + * all calls made through a given {@code RestTemplate} instance as associated |
| 169 | + * for the same client identity. {@link HttpClientContext#setUserToken(Object)} |
| 170 | + * can be used to specify a fixed user token for all requests. |
| 171 | + * @param httpContextFactory the context factory to use |
| 172 | + * @since 5.2.7 |
| 173 | + */ |
| 174 | + public void setHttpContextFactory(BiFunction<HttpMethod, URI, HttpContext> httpContextFactory) { |
| 175 | + this.httpContextFactory = httpContextFactory; |
| 176 | + } |
160 | 177 |
|
161 | 178 | @Override
|
162 | 179 | public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
|
@@ -296,7 +313,7 @@ protected void postProcessHttpRequest(HttpUriRequest request) {
|
296 | 313 | */
|
297 | 314 | @Nullable
|
298 | 315 | protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
|
299 |
| - return null; |
| 316 | + return (this.httpContextFactory != null ? this.httpContextFactory.apply(httpMethod, uri) : null); |
300 | 317 | }
|
301 | 318 |
|
302 | 319 |
|
|
0 commit comments