mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-05 00:56:58 +08:00
Polish "Support Jetty in ClientHttpRequestFactories"
See gh-36116
This commit is contained in:
parent
7ceece3d3d
commit
c3e2c9d684
@ -25,6 +25,7 @@ In order of preference, the following clients are supported:
|
||||
|
||||
. Apache HttpClient
|
||||
. OkHttp
|
||||
. Jetty HttpClient
|
||||
. Simple JDK client (`HttpURLConnection`)
|
||||
|
||||
If multiple clients are available on the classpath, the most preferred client will be used.
|
||||
|
@ -241,14 +241,12 @@ public final class ClientHttpRequestFactories {
|
||||
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));
|
||||
org.eclipse.jetty.client.HttpClient httpClient = new org.eclipse.jetty.client.HttpClient(
|
||||
new HttpClientTransportDynamic(connector));
|
||||
return new JettyClientHttpRequestFactory(httpClient);
|
||||
}
|
||||
return new JettyClientHttpRequestFactory();
|
||||
|
@ -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.
|
||||
@ -28,6 +28,7 @@ import org.springframework.aot.hint.TypeReference;
|
||||
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;
|
||||
@ -59,6 +60,10 @@ class ClientHttpRequestFactoriesRuntimeHints implements RuntimeHintsRegistrar {
|
||||
typeHint.onReachableType(TypeReference.of(ClientHttpRequestFactories.OKHTTP_CLIENT_CLASS));
|
||||
registerReflectionHints(hints, OkHttp3ClientHttpRequestFactory.class);
|
||||
});
|
||||
hints.registerTypeIfPresent(classLoader, ClientHttpRequestFactories.JETTY_CLIENT_CLASS, (typeHint) -> {
|
||||
typeHint.onReachableType(TypeReference.of(ClientHttpRequestFactories.JETTY_CLIENT_CLASS));
|
||||
registerReflectionHints(hints, JettyClientHttpRequestFactory.class, long.class);
|
||||
});
|
||||
hints.registerType(SimpleClientHttpRequestFactory.class, (typeHint) -> {
|
||||
typeHint.onReachableType(HttpURLConnection.class);
|
||||
registerReflectionHints(hints, SimpleClientHttpRequestFactory.class);
|
||||
@ -67,8 +72,13 @@ class ClientHttpRequestFactoriesRuntimeHints implements RuntimeHintsRegistrar {
|
||||
|
||||
private void registerReflectionHints(ReflectionHints hints,
|
||||
Class<? extends ClientHttpRequestFactory> requestFactoryType) {
|
||||
registerReflectionHints(hints, requestFactoryType, int.class);
|
||||
}
|
||||
|
||||
private void registerReflectionHints(ReflectionHints hints,
|
||||
Class<? extends ClientHttpRequestFactory> requestFactoryType, Class<?> readTimeoutType) {
|
||||
registerMethod(hints, requestFactoryType, "setConnectTimeout", int.class);
|
||||
registerMethod(hints, requestFactoryType, "setReadTimeout", int.class);
|
||||
registerMethod(hints, requestFactoryType, "setReadTimeout", readTimeoutType);
|
||||
}
|
||||
|
||||
private void registerMethod(ReflectionHints hints, Class<? extends ClientHttpRequestFactory> requestFactoryType,
|
||||
|
@ -23,8 +23,7 @@ import org.springframework.http.client.JettyClientHttpRequestFactory;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
/**
|
||||
* Tests for {@link ClientHttpRequestFactories} when Jetty is the
|
||||
* predominant HTTP client.
|
||||
* Tests for {@link ClientHttpRequestFactories} when Jetty is the predominant HTTP client.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
@ -38,14 +37,12 @@ class ClientHttpRequestFactoriesJettyTests
|
||||
|
||||
@Override
|
||||
protected long connectTimeout(JettyClientHttpRequestFactory requestFactory) {
|
||||
HttpClient client = (HttpClient) ReflectionTestUtils.getField(requestFactory, "httpClient");
|
||||
return client.getConnectTimeout();
|
||||
return ((HttpClient) ReflectionTestUtils.getField(requestFactory, "httpClient")).getConnectTimeout();
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected long readTimeout(JettyClientHttpRequestFactory requestFactory) {
|
||||
return (int) ReflectionTestUtils.getField(requestFactory, "readTimeout");
|
||||
return (long) ReflectionTestUtils.getField(requestFactory, "readTimeout");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ import org.springframework.aot.hint.predicate.ReflectionHintsPredicates;
|
||||
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
|
||||
import org.springframework.http.client.AbstractClientHttpRequestFactoryWrapper;
|
||||
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.ReflectionUtils;
|
||||
@ -73,6 +74,17 @@ class ClientHttpRequestFactoriesRuntimeHintsTests {
|
||||
assertThat(hints.reflection().getTypeHint(OkHttp3ClientHttpRequestFactory.class).methods()).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRegisterJettyClientHints() {
|
||||
RuntimeHints hints = new RuntimeHints();
|
||||
new ClientHttpRequestFactoriesRuntimeHints().registerHints(hints, getClass().getClassLoader());
|
||||
ReflectionHintsPredicates reflection = RuntimeHintsPredicates.reflection();
|
||||
assertThat(reflection.onMethod(method(JettyClientHttpRequestFactory.class, "setConnectTimeout", int.class)))
|
||||
.accepts(hints);
|
||||
assertThat(reflection.onMethod(method(JettyClientHttpRequestFactory.class, "setReadTimeout", long.class)))
|
||||
.accepts(hints);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRegisterSimpleHttpHints() {
|
||||
RuntimeHints hints = new RuntimeHints();
|
||||
|
@ -34,7 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@ClassPathExclusions({ "httpclient5-*.jar", "okhttp*.jar" })
|
||||
@ClassPathExclusions(files = { "httpclient5-*.jar", "jetty-client-*.jar", "okhttp*.jar" })
|
||||
class HttpWebServiceMessageSenderBuilderSimpleIntegrationTests {
|
||||
|
||||
private final HttpWebServiceMessageSenderBuilder builder = new HttpWebServiceMessageSenderBuilder();
|
||||
|
@ -16,10 +16,6 @@
|
||||
|
||||
package smoketest.jetty;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import smoketest.jetty.util.RandomStringUtil;
|
||||
@ -35,7 +31,6 @@ import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@ -65,16 +60,14 @@ class SampleJettyApplicationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCompression() throws Exception {
|
||||
HttpHeaders requestHeaders = new HttpHeaders();
|
||||
requestHeaders.set("Accept-Encoding", "gzip");
|
||||
HttpEntity<?> requestEntity = new HttpEntity<>(requestHeaders);
|
||||
ResponseEntity<byte[]> entity = this.restTemplate.exchange("/", HttpMethod.GET, requestEntity, byte[].class);
|
||||
void testCompression() {
|
||||
// Jetty HttpClient sends Accept-Encoding: gzip by default
|
||||
ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(entity.getBody()).isNotNull();
|
||||
try (GZIPInputStream inflater = new GZIPInputStream(new ByteArrayInputStream(entity.getBody()))) {
|
||||
assertThat(StreamUtils.copyToString(inflater, StandardCharsets.UTF_8)).isEqualTo("Hello World");
|
||||
}
|
||||
assertThat(entity.getBody()).isEqualTo("Hello World");
|
||||
// Jetty HttpClient decodes gzip reponses automatically
|
||||
// Check that we received a gzip-encoded response
|
||||
assertThat(entity.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING)).isEqualTo("gzip");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
Reference in New Issue
Block a user