Skip to content

Commit 3bbfee5

Browse files
poutsmawilkinsona
authored andcommitted
Support JDK HttpClient in ClientHttpRequestFactories
See gh-36118
1 parent 46aeeab commit 3bbfee5

File tree

4 files changed

+92
-3
lines changed

4 files changed

+92
-3
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/ClientHttpRequestFactories.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.springframework.http.client.AbstractClientHttpRequestFactoryWrapper;
5050
import org.springframework.http.client.ClientHttpRequestFactory;
5151
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
52+
import org.springframework.http.client.JdkClientHttpRequestFactory;
5253
import org.springframework.http.client.JettyClientHttpRequestFactory;
5354
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
5455
import org.springframework.http.client.SimpleClientHttpRequestFactory;
@@ -79,6 +80,10 @@ public final class ClientHttpRequestFactories {
7980

8081
private static final boolean JETTY_CLIENT_PRESENT = ClassUtils.isPresent(JETTY_CLIENT_CLASS, null);
8182

83+
static final String JDK_CLIENT_CLASS = "java.net.http.HttpClient";
84+
85+
private static final boolean JDK_CLIENT_PRESENT = ClassUtils.isPresent(JDK_CLIENT_CLASS, null);
86+
8287
private ClientHttpRequestFactories() {
8388
}
8489

@@ -99,6 +104,9 @@ public static ClientHttpRequestFactory get(ClientHttpRequestFactorySettings sett
99104
if (JETTY_CLIENT_PRESENT) {
100105
return Jetty.get(settings);
101106
}
107+
if (JDK_CLIENT_PRESENT) {
108+
return Jdk.get(settings);
109+
}
102110
return Simple.get(settings);
103111
}
104112

@@ -126,6 +134,9 @@ public static <T extends ClientHttpRequestFactory> T get(Class<T> requestFactory
126134
if (requestFactoryType == JettyClientHttpRequestFactory.class) {
127135
return (T) Jetty.get(settings);
128136
}
137+
if (requestFactoryType == JdkClientHttpRequestFactory.class) {
138+
return (T) Jdk.get(settings);
139+
}
129140
if (requestFactoryType == SimpleClientHttpRequestFactory.class) {
130141
return (T) Simple.get(settings);
131142
}
@@ -254,6 +265,32 @@ private static JettyClientHttpRequestFactory createRequestFactory(SslBundle sslB
254265

255266
}
256267

268+
/**
269+
* Support for {@link JdkClientHttpRequestFactory}.
270+
*/
271+
static class Jdk {
272+
273+
static JdkClientHttpRequestFactory get(ClientHttpRequestFactorySettings settings) {
274+
java.net.http.HttpClient httpClient = createHttpClient(settings.connectTimeout(), settings.sslBundle());
275+
JdkClientHttpRequestFactory requestFactory = new JdkClientHttpRequestFactory(httpClient);
276+
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
277+
map.from(settings::readTimeout).asInt(Duration::toMillis).to(requestFactory::setReadTimeout);
278+
return requestFactory;
279+
}
280+
281+
private static java.net.http.HttpClient createHttpClient(Duration connectTimeout, SslBundle sslBundle) {
282+
java.net.http.HttpClient.Builder builder = java.net.http.HttpClient.newBuilder();
283+
if (connectTimeout != null) {
284+
builder.connectTimeout(connectTimeout);
285+
}
286+
if (sslBundle != null) {
287+
builder.sslContext(sslBundle.createSslContext());
288+
}
289+
return builder.build();
290+
}
291+
292+
}
293+
257294
/**
258295
* Support for {@link SimpleClientHttpRequestFactory}.
259296
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.web.client;
18+
19+
import java.net.http.HttpClient;
20+
import java.time.Duration;
21+
22+
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
23+
import org.springframework.http.client.JdkClientHttpRequestFactory;
24+
import org.springframework.test.util.ReflectionTestUtils;
25+
26+
/**
27+
* Tests for {@link ClientHttpRequestFactories} when JDK HttpClient is the
28+
* predominant HTTP client.
29+
*
30+
* @author Andy Wilkinson
31+
*/
32+
@ClassPathExclusions({ "httpclient5-*.jar", "okhttp-*.jar" })
33+
class ClientHttpRequestFactoriesJdkClientTests
34+
extends AbstractClientHttpRequestFactoriesTests<JdkClientHttpRequestFactory> {
35+
36+
ClientHttpRequestFactoriesJdkClientTests() {
37+
super(JdkClientHttpRequestFactory.class);
38+
}
39+
40+
@Override
41+
protected long connectTimeout(JdkClientHttpRequestFactory requestFactory) {
42+
HttpClient httpClient = (HttpClient) ReflectionTestUtils.getField(requestFactory, "httpClient");
43+
return httpClient.connectTimeout().map(Duration::toMillis).orElse(-1L);
44+
}
45+
46+
@Override
47+
@SuppressWarnings("unchecked")
48+
protected long readTimeout(JdkClientHttpRequestFactory requestFactory) {
49+
return ((Duration) ReflectionTestUtils.getField(requestFactory, "readTimeout")).toMillis();
50+
}
51+
52+
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/client/ClientHttpRequestFactoriesSimpleTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,7 @@
2626
*
2727
* @author Andy Wilkinson
2828
*/
29-
@ClassPathExclusions({ "httpclient5-*.jar", "okhttp-*.jar", "jetty-client-*.jar" })
29+
@ClassPathExclusions(files = {"httpclient5-*.jar", "jetty-client-*.jar", "okhttp-*.jar"}, packages = "java.net.http")
3030
class ClientHttpRequestFactoriesSimpleTests
3131
extends AbstractClientHttpRequestFactoriesTests<SimpleClientHttpRequestFactory> {
3232

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/webservices/client/HttpWebServiceMessageSenderBuilderSimpleIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*
3535
* @author Stephane Nicoll
3636
*/
37-
@ClassPathExclusions(files = { "httpclient5-*.jar", "jetty-client-*.jar", "okhttp*.jar" })
37+
@ClassPathExclusions(files = { "httpclient5-*.jar", "jetty-client-*.jar", "okhttp*.jar" }, packages = "java.net.http")
3838
class HttpWebServiceMessageSenderBuilderSimpleIntegrationTests {
3939

4040
private final HttpWebServiceMessageSenderBuilder builder = new HttpWebServiceMessageSenderBuilder();

0 commit comments

Comments
 (0)