Merge pull request #36118 from poutsma

* gh-36118:
  Polish "Support JDK HttpClient in ClientHttpRequestFactories"
  Support JDK HttpClient in ClientHttpRequestFactories

Closes gh-36118
pull/36270/head
Andy Wilkinson 1 year ago
commit 1191282004

@ -49,6 +49,7 @@ import org.springframework.boot.ssl.SslOptions;
import org.springframework.http.client.AbstractClientHttpRequestFactoryWrapper;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.JdkClientHttpRequestFactory;
import org.springframework.http.client.JettyClientHttpRequestFactory;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
@ -126,6 +127,9 @@ public final class ClientHttpRequestFactories {
if (requestFactoryType == JettyClientHttpRequestFactory.class) {
return (T) Jetty.get(settings);
}
if (requestFactoryType == JdkClientHttpRequestFactory.class) {
return (T) Jdk.get(settings);
}
if (requestFactoryType == SimpleClientHttpRequestFactory.class) {
return (T) Simple.get(settings);
}
@ -254,6 +258,32 @@ public final class ClientHttpRequestFactories {
}
/**
* Support for {@link JdkClientHttpRequestFactory}.
*/
static class Jdk {
static JdkClientHttpRequestFactory get(ClientHttpRequestFactorySettings settings) {
java.net.http.HttpClient httpClient = createHttpClient(settings.connectTimeout(), settings.sslBundle());
JdkClientHttpRequestFactory requestFactory = new JdkClientHttpRequestFactory(httpClient);
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
map.from(settings::readTimeout).asInt(Duration::toMillis).to(requestFactory::setReadTimeout);
return requestFactory;
}
private static java.net.http.HttpClient createHttpClient(Duration connectTimeout, SslBundle sslBundle) {
java.net.http.HttpClient.Builder builder = java.net.http.HttpClient.newBuilder();
if (connectTimeout != null) {
builder.connectTimeout(connectTimeout);
}
if (sslBundle != null) {
builder.sslContext(sslBundle.createSslContext());
}
return builder.build();
}
}
/**
* Support for {@link SimpleClientHttpRequestFactory}.
*/

@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 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.
@ -26,7 +26,7 @@ import org.springframework.test.util.ReflectionTestUtils;
*
* @author Andy Wilkinson
*/
@ClassPathExclusions({ "httpclient5-*.jar", "okhttp-*.jar", "jetty-client-*.jar" })
@ClassPathExclusions({ "httpclient5-*.jar", "jetty-client-*.jar", "okhttp-*.jar" })
class ClientHttpRequestFactoriesSimpleTests
extends AbstractClientHttpRequestFactoriesTests<SimpleClientHttpRequestFactory> {

@ -27,6 +27,7 @@ import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.JdkClientHttpRequestFactory;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
@ -75,6 +76,13 @@ class ClientHttpRequestFactoriesTests {
assertThat(requestFactory).isInstanceOf(OkHttp3ClientHttpRequestFactory.class);
}
@Test
void getOfJdkFactoryReturnsJdkFactory() {
ClientHttpRequestFactory requestFactory = ClientHttpRequestFactories.get(JdkClientHttpRequestFactory.class,
ClientHttpRequestFactorySettings.DEFAULTS);
assertThat(requestFactory).isInstanceOf(JdkClientHttpRequestFactory.class);
}
@Test
void getOfUnknownTypeCreatesFactory() {
ClientHttpRequestFactory requestFactory = ClientHttpRequestFactories.get(TestClientHttpRequestFactory.class,

@ -34,7 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Stephane Nicoll
*/
@ClassPathExclusions(files = { "httpclient5-*.jar", "jetty-client-*.jar", "okhttp*.jar" })
@ClassPathExclusions({ "httpclient5-*.jar", "jetty-client-*.jar", "okhttp*.jar" })
class HttpWebServiceMessageSenderBuilderSimpleIntegrationTests {
private final HttpWebServiceMessageSenderBuilder builder = new HttpWebServiceMessageSenderBuilder();

Loading…
Cancel
Save