Skip to content

Commit e2dc278

Browse files
committed
Use default SslContextFactory for JettyClient
Update the auto-configured Jetty `HttpClient` so that a default `SslContextFactory` is used. Prior to this commit connecting to https URLs would cause a `NullPointerException`. Fixed gh-16810
1 parent f665910 commit e2dc278

File tree

2 files changed

+79
-3
lines changed

2 files changed

+79
-3
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/function/client/ClientHttpConnectorConfiguration.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,9 @@
1818

1919
import java.util.function.Function;
2020

21+
import org.eclipse.jetty.client.HttpClient;
22+
import org.eclipse.jetty.util.ssl.SslContextFactory;
23+
2124
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2225
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2326
import org.springframework.context.annotation.Bean;
@@ -73,8 +76,12 @@ public JettyResourceFactory jettyClientResourceFactory() {
7376
@Bean
7477
public JettyClientHttpConnector jettyClientHttpConnector(
7578
JettyResourceFactory jettyResourceFactory) {
76-
return new JettyClientHttpConnector(jettyResourceFactory, (httpClient) -> {
77-
});
79+
SslContextFactory sslContextFactory = new SslContextFactory.Client();
80+
HttpClient httpClient = new HttpClient(sslContextFactory);
81+
httpClient.setExecutor(jettyResourceFactory.getExecutor());
82+
httpClient.setByteBufferPool(jettyResourceFactory.getByteBufferPool());
83+
httpClient.setScheduler(jettyResourceFactory.getScheduler());
84+
return new JettyClientHttpConnector(httpClient);
7885
}
7986

8087
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2012-2019 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.autoconfigure.web.reactive.function.client;
18+
19+
import java.util.concurrent.Executor;
20+
21+
import org.eclipse.jetty.client.HttpClient;
22+
import org.eclipse.jetty.io.ByteBufferPool;
23+
import org.eclipse.jetty.util.thread.Scheduler;
24+
import org.junit.Test;
25+
26+
import org.springframework.http.client.reactive.JettyClientHttpConnector;
27+
import org.springframework.http.client.reactive.JettyResourceFactory;
28+
import org.springframework.test.util.ReflectionTestUtils;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
import static org.mockito.Mockito.mock;
32+
33+
/**
34+
* Tests for {@link ClientHttpConnectorConfiguration}.
35+
*
36+
* @author Phillip Webb
37+
*/
38+
public class ClientHttpConnectorConfigurationTests {
39+
40+
@Test
41+
public void jettyClientHttpConnectorAppliesJettyResourceFactory() {
42+
Executor executor = mock(Executor.class);
43+
ByteBufferPool byteBufferPool = mock(ByteBufferPool.class);
44+
Scheduler scheduler = mock(Scheduler.class);
45+
JettyResourceFactory jettyResourceFactory = new JettyResourceFactory();
46+
jettyResourceFactory.setExecutor(executor);
47+
jettyResourceFactory.setByteBufferPool(byteBufferPool);
48+
jettyResourceFactory.setScheduler(scheduler);
49+
JettyClientHttpConnector connector = new ClientHttpConnectorConfiguration.JettyClient()
50+
.jettyClientHttpConnector(jettyResourceFactory);
51+
HttpClient httpClient = (HttpClient) ReflectionTestUtils.getField(connector,
52+
"httpClient");
53+
assertThat(httpClient.getExecutor()).isSameAs(executor);
54+
assertThat(httpClient.getByteBufferPool()).isSameAs(byteBufferPool);
55+
assertThat(httpClient.getScheduler()).isSameAs(scheduler);
56+
}
57+
58+
@Test
59+
public void JettyResourceFactoryHasSslContextFactory() {
60+
// gh-16810
61+
JettyResourceFactory jettyResourceFactory = new JettyResourceFactory();
62+
JettyClientHttpConnector connector = new ClientHttpConnectorConfiguration.JettyClient()
63+
.jettyClientHttpConnector(jettyResourceFactory);
64+
HttpClient httpClient = (HttpClient) ReflectionTestUtils.getField(connector,
65+
"httpClient");
66+
assertThat(httpClient.getSslContextFactory()).isNotNull();
67+
}
68+
69+
}

0 commit comments

Comments
 (0)