From 7ceece3d3d1a6a77672d00b32e3f166d81f06b66 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Thu, 29 Jun 2023 13:24:43 +0200 Subject: [PATCH] Support Jetty in ClientHttpRequestFactories This commit introduces support for the JettyClientHttpRequestFactory in ClientHttpRequestFactories. See gh-36116 --- spring-boot-project/spring-boot/build.gradle | 1 + .../client/ClientHttpRequestFactories.java | 46 +++++++++++++++++ .../ClientHttpRequestFactoriesJettyTests.java | 51 +++++++++++++++++++ ...ClientHttpRequestFactoriesSimpleTests.java | 2 +- 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/client/ClientHttpRequestFactoriesJettyTests.java diff --git a/spring-boot-project/spring-boot/build.gradle b/spring-boot-project/spring-boot/build.gradle index 3a862fe98b..6b86fd02ad 100644 --- a/spring-boot-project/spring-boot/build.gradle +++ b/spring-boot-project/spring-boot/build.gradle @@ -67,6 +67,7 @@ dependencies { optional("org.eclipse.jetty.http2:http2-server") { exclude(group: "org.eclipse.jetty.toolchain", module: "jetty-jakarta-servlet-api") } + optional("org.eclipse.jetty:jetty-client") optional("org.flywaydb:flyway-core") optional("org.hamcrest:hamcrest-library") optional("org.hibernate.orm:hibernate-core") diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/ClientHttpRequestFactories.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/ClientHttpRequestFactories.java index 5b85a914c0..384e2f2b89 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/ClientHttpRequestFactories.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/ClientHttpRequestFactories.java @@ -26,6 +26,7 @@ import java.util.concurrent.TimeUnit; import java.util.function.Supplier; import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; @@ -38,6 +39,9 @@ import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuil import org.apache.hc.client5.http.ssl.DefaultHostnameVerifier; import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory; import org.apache.hc.core5.http.io.SocketConfig; +import org.eclipse.jetty.client.dynamic.HttpClientTransportDynamic; +import org.eclipse.jetty.io.ClientConnector; +import org.eclipse.jetty.util.ssl.SslContextFactory; import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.boot.ssl.SslBundle; @@ -45,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.JettyClientHttpRequestFactory; import org.springframework.http.client.OkHttp3ClientHttpRequestFactory; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.util.Assert; @@ -70,6 +75,10 @@ public final class ClientHttpRequestFactories { private static final boolean OKHTTP_CLIENT_PRESENT = ClassUtils.isPresent(OKHTTP_CLIENT_CLASS, null); + static final String JETTY_CLIENT_CLASS = "org.eclipse.jetty.client.HttpClient"; + + private static final boolean JETTY_CLIENT_PRESENT = ClassUtils.isPresent(JETTY_CLIENT_CLASS, null); + private ClientHttpRequestFactories() { } @@ -87,6 +96,9 @@ public final class ClientHttpRequestFactories { if (OKHTTP_CLIENT_PRESENT) { return OkHttp.get(settings); } + if (JETTY_CLIENT_PRESENT) { + return Jetty.get(settings); + } return Simple.get(settings); } @@ -111,6 +123,9 @@ public final class ClientHttpRequestFactories { if (requestFactoryType == OkHttp3ClientHttpRequestFactory.class) { return (T) OkHttp.get(settings); } + if (requestFactoryType == JettyClientHttpRequestFactory.class) { + return (T) Jetty.get(settings); + } if (requestFactoryType == SimpleClientHttpRequestFactory.class) { return (T) Simple.get(settings); } @@ -210,6 +225,37 @@ public final class ClientHttpRequestFactories { } + /** + * Support for {@link JettyClientHttpRequestFactory}. + */ + static class Jetty { + + static JettyClientHttpRequestFactory get(ClientHttpRequestFactorySettings settings) { + JettyClientHttpRequestFactory requestFactory = createRequestFactory(settings.sslBundle()); + PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); + map.from(settings::connectTimeout).asInt(Duration::toMillis).to(requestFactory::setConnectTimeout); + map.from(settings::readTimeout).asInt(Duration::toMillis).to(requestFactory::setReadTimeout); + return requestFactory; + } + + private static JettyClientHttpRequestFactory createRequestFactory(SslBundle sslBundle) { + if (sslBundle != null) { + SSLContext sslContext = sslBundle.createSslContext(); + + SslContextFactory.Client sslContextFactory = new SslContextFactory.Client(); + sslContextFactory.setSslContext(sslContext); + + ClientConnector connector = new ClientConnector(); + connector.setSslContextFactory(sslContextFactory); + org.eclipse.jetty.client.HttpClient httpClient = + new org.eclipse.jetty.client.HttpClient(new HttpClientTransportDynamic(connector)); + return new JettyClientHttpRequestFactory(httpClient); + } + return new JettyClientHttpRequestFactory(); + } + + } + /** * Support for {@link SimpleClientHttpRequestFactory}. */ diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/client/ClientHttpRequestFactoriesJettyTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/client/ClientHttpRequestFactoriesJettyTests.java new file mode 100644 index 0000000000..10cb8a609f --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/client/ClientHttpRequestFactoriesJettyTests.java @@ -0,0 +1,51 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.web.client; + +import org.eclipse.jetty.client.HttpClient; + +import org.springframework.boot.testsupport.classpath.ClassPathExclusions; +import org.springframework.http.client.JettyClientHttpRequestFactory; +import org.springframework.test.util.ReflectionTestUtils; + +/** + * Tests for {@link ClientHttpRequestFactories} when Jetty is the + * predominant HTTP client. + * + * @author Arjen Poutsma + */ +@ClassPathExclusions({ "httpclient5-*.jar", "okhttp-*.jar" }) +class ClientHttpRequestFactoriesJettyTests + extends AbstractClientHttpRequestFactoriesTests { + + ClientHttpRequestFactoriesJettyTests() { + super(JettyClientHttpRequestFactory.class); + } + + @Override + protected long connectTimeout(JettyClientHttpRequestFactory requestFactory) { + HttpClient client = (HttpClient) ReflectionTestUtils.getField(requestFactory, "httpClient"); + return client.getConnectTimeout(); + } + + @Override + @SuppressWarnings("unchecked") + protected long readTimeout(JettyClientHttpRequestFactory requestFactory) { + return (int) ReflectionTestUtils.getField(requestFactory, "readTimeout"); + } + +} diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/client/ClientHttpRequestFactoriesSimpleTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/client/ClientHttpRequestFactoriesSimpleTests.java index a9e75aa649..f00882bc7d 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/client/ClientHttpRequestFactoriesSimpleTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/client/ClientHttpRequestFactoriesSimpleTests.java @@ -26,7 +26,7 @@ import org.springframework.test.util.ReflectionTestUtils; * * @author Andy Wilkinson */ -@ClassPathExclusions({ "httpclient5-*.jar", "okhttp-*.jar" }) +@ClassPathExclusions({ "httpclient5-*.jar", "okhttp-*.jar", "jetty-client-*.jar" }) class ClientHttpRequestFactoriesSimpleTests extends AbstractClientHttpRequestFactoriesTests {