mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Support Jetty in ClientHttpRequestFactories
This commit introduces support for the JettyClientHttpRequestFactory in ClientHttpRequestFactories. See gh-36116
This commit is contained in:
parent
2cbd916cdb
commit
7ceece3d3d
@ -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")
|
||||
|
@ -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}.
|
||||
*/
|
||||
|
@ -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<JettyClientHttpRequestFactory> {
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
@ -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<SimpleClientHttpRequestFactory> {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user