Skip to content

Commit 7ceece3

Browse files
poutsmawilkinsona
authored andcommitted
Support Jetty in ClientHttpRequestFactories
This commit introduces support for the JettyClientHttpRequestFactory in ClientHttpRequestFactories. See gh-36116
1 parent 2cbd916 commit 7ceece3

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed

spring-boot-project/spring-boot/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ dependencies {
6767
optional("org.eclipse.jetty.http2:http2-server") {
6868
exclude(group: "org.eclipse.jetty.toolchain", module: "jetty-jakarta-servlet-api")
6969
}
70+
optional("org.eclipse.jetty:jetty-client")
7071
optional("org.flywaydb:flyway-core")
7172
optional("org.hamcrest:hamcrest-library")
7273
optional("org.hibernate.orm:hibernate-core")

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.function.Supplier;
2727

2828
import javax.net.ssl.HttpsURLConnection;
29+
import javax.net.ssl.SSLContext;
2930
import javax.net.ssl.SSLSocketFactory;
3031
import javax.net.ssl.TrustManager;
3132
import javax.net.ssl.X509TrustManager;
@@ -38,13 +39,17 @@
3839
import org.apache.hc.client5.http.ssl.DefaultHostnameVerifier;
3940
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
4041
import org.apache.hc.core5.http.io.SocketConfig;
42+
import org.eclipse.jetty.client.dynamic.HttpClientTransportDynamic;
43+
import org.eclipse.jetty.io.ClientConnector;
44+
import org.eclipse.jetty.util.ssl.SslContextFactory;
4145

4246
import org.springframework.boot.context.properties.PropertyMapper;
4347
import org.springframework.boot.ssl.SslBundle;
4448
import org.springframework.boot.ssl.SslOptions;
4549
import org.springframework.http.client.AbstractClientHttpRequestFactoryWrapper;
4650
import org.springframework.http.client.ClientHttpRequestFactory;
4751
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
52+
import org.springframework.http.client.JettyClientHttpRequestFactory;
4853
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
4954
import org.springframework.http.client.SimpleClientHttpRequestFactory;
5055
import org.springframework.util.Assert;
@@ -70,6 +75,10 @@ public final class ClientHttpRequestFactories {
7075

7176
private static final boolean OKHTTP_CLIENT_PRESENT = ClassUtils.isPresent(OKHTTP_CLIENT_CLASS, null);
7277

78+
static final String JETTY_CLIENT_CLASS = "org.eclipse.jetty.client.HttpClient";
79+
80+
private static final boolean JETTY_CLIENT_PRESENT = ClassUtils.isPresent(JETTY_CLIENT_CLASS, null);
81+
7382
private ClientHttpRequestFactories() {
7483
}
7584

@@ -87,6 +96,9 @@ public static ClientHttpRequestFactory get(ClientHttpRequestFactorySettings sett
8796
if (OKHTTP_CLIENT_PRESENT) {
8897
return OkHttp.get(settings);
8998
}
99+
if (JETTY_CLIENT_PRESENT) {
100+
return Jetty.get(settings);
101+
}
90102
return Simple.get(settings);
91103
}
92104

@@ -111,6 +123,9 @@ public static <T extends ClientHttpRequestFactory> T get(Class<T> requestFactory
111123
if (requestFactoryType == OkHttp3ClientHttpRequestFactory.class) {
112124
return (T) OkHttp.get(settings);
113125
}
126+
if (requestFactoryType == JettyClientHttpRequestFactory.class) {
127+
return (T) Jetty.get(settings);
128+
}
114129
if (requestFactoryType == SimpleClientHttpRequestFactory.class) {
115130
return (T) Simple.get(settings);
116131
}
@@ -210,6 +225,37 @@ private static OkHttp3ClientHttpRequestFactory createRequestFactory(SslBundle ss
210225

211226
}
212227

228+
/**
229+
* Support for {@link JettyClientHttpRequestFactory}.
230+
*/
231+
static class Jetty {
232+
233+
static JettyClientHttpRequestFactory get(ClientHttpRequestFactorySettings settings) {
234+
JettyClientHttpRequestFactory requestFactory = createRequestFactory(settings.sslBundle());
235+
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
236+
map.from(settings::connectTimeout).asInt(Duration::toMillis).to(requestFactory::setConnectTimeout);
237+
map.from(settings::readTimeout).asInt(Duration::toMillis).to(requestFactory::setReadTimeout);
238+
return requestFactory;
239+
}
240+
241+
private static JettyClientHttpRequestFactory createRequestFactory(SslBundle sslBundle) {
242+
if (sslBundle != null) {
243+
SSLContext sslContext = sslBundle.createSslContext();
244+
245+
SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
246+
sslContextFactory.setSslContext(sslContext);
247+
248+
ClientConnector connector = new ClientConnector();
249+
connector.setSslContextFactory(sslContextFactory);
250+
org.eclipse.jetty.client.HttpClient httpClient =
251+
new org.eclipse.jetty.client.HttpClient(new HttpClientTransportDynamic(connector));
252+
return new JettyClientHttpRequestFactory(httpClient);
253+
}
254+
return new JettyClientHttpRequestFactory();
255+
}
256+
257+
}
258+
213259
/**
214260
* Support for {@link SimpleClientHttpRequestFactory}.
215261
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 org.eclipse.jetty.client.HttpClient;
20+
21+
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
22+
import org.springframework.http.client.JettyClientHttpRequestFactory;
23+
import org.springframework.test.util.ReflectionTestUtils;
24+
25+
/**
26+
* Tests for {@link ClientHttpRequestFactories} when Jetty is the
27+
* predominant HTTP client.
28+
*
29+
* @author Arjen Poutsma
30+
*/
31+
@ClassPathExclusions({ "httpclient5-*.jar", "okhttp-*.jar" })
32+
class ClientHttpRequestFactoriesJettyTests
33+
extends AbstractClientHttpRequestFactoriesTests<JettyClientHttpRequestFactory> {
34+
35+
ClientHttpRequestFactoriesJettyTests() {
36+
super(JettyClientHttpRequestFactory.class);
37+
}
38+
39+
@Override
40+
protected long connectTimeout(JettyClientHttpRequestFactory requestFactory) {
41+
HttpClient client = (HttpClient) ReflectionTestUtils.getField(requestFactory, "httpClient");
42+
return client.getConnectTimeout();
43+
}
44+
45+
@Override
46+
@SuppressWarnings("unchecked")
47+
protected long readTimeout(JettyClientHttpRequestFactory requestFactory) {
48+
return (int) ReflectionTestUtils.getField(requestFactory, "readTimeout");
49+
}
50+
51+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
* @author Andy Wilkinson
2828
*/
29-
@ClassPathExclusions({ "httpclient5-*.jar", "okhttp-*.jar" })
29+
@ClassPathExclusions({ "httpclient5-*.jar", "okhttp-*.jar", "jetty-client-*.jar" })
3030
class ClientHttpRequestFactoriesSimpleTests
3131
extends AbstractClientHttpRequestFactoriesTests<SimpleClientHttpRequestFactory> {
3232

0 commit comments

Comments
 (0)