Skip to content

Commit 57510f8

Browse files
committed
Replace Ok Http Factory with the new Jdk Http Factory
1 parent 2b7ac1a commit 57510f8

File tree

2 files changed

+50
-32
lines changed

2 files changed

+50
-32
lines changed

headless-services/spring-boot-language-server/pom.xml

-5
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,6 @@
158158
</exclusions>
159159
</dependency>
160160

161-
<dependency>
162-
<groupId>com.squareup.okhttp3</groupId>
163-
<artifactId>okhttp</artifactId>
164-
</dependency>
165-
166161
<!-- Test harness -->
167162
<dependency>
168163
<groupId>org.springframework.boot</groupId>

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/app/RestTemplateFactory.java

+50-27
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,27 @@
1111
package org.springframework.ide.vscode.boot.app;
1212

1313
import java.io.IOException;
14+
import java.net.Authenticator;
1415
import java.net.InetSocketAddress;
1516
import java.net.MalformedURLException;
17+
import java.net.PasswordAuthentication;
1618
import java.net.Proxy;
19+
import java.net.ProxySelector;
20+
import java.net.SocketAddress;
21+
import java.net.URI;
1722
import java.net.URL;
23+
import java.net.http.HttpClient;
24+
import java.net.http.HttpClient.Builder;
25+
import java.util.Collections;
26+
import java.util.List;
1827
import java.util.Set;
1928

2029
import org.slf4j.Logger;
2130
import org.slf4j.LoggerFactory;
22-
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
31+
import org.springframework.http.client.JdkClientHttpRequestFactory;
2332
import org.springframework.stereotype.Component;
2433
import org.springframework.web.client.RestTemplate;
2534

26-
import okhttp3.Authenticator;
27-
import okhttp3.Credentials;
28-
import okhttp3.OkHttpClient;
29-
import okhttp3.Request;
30-
import okhttp3.Response;
31-
import okhttp3.Route;
32-
3335
@Component
3436
public class RestTemplateFactory {
3537

@@ -42,27 +44,49 @@ public RestTemplateFactory(BootJavaConfig config) {
4244
}
4345

4446
public RestTemplate createRestTemplate(String host) {
45-
String proxyUrl = config.getRawSettings().getString("http", "proxy");
46-
if (proxyUrl == null || proxyUrl.isBlank()) {
47-
proxyUrl = getProxyUrlFromEnv();
47+
String proxyUrlStr = config.getRawSettings().getString("http", "proxy");
48+
if (proxyUrlStr == null || proxyUrlStr.isBlank()) {
49+
proxyUrlStr = getProxyUrlFromEnv();
4850
}
49-
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
50-
if (proxyUrl != null && !proxyUrl.isBlank()) {
51+
52+
Builder clientBuilder = HttpClient.newBuilder();
53+
if (proxyUrlStr != null && !proxyUrlStr.isBlank()) {
5154
Set<String> exclusions = config.getRawSettings().getStringSet("http", "proxy-exclusions");
5255
if (!"localhost".equals(host) && !"127.0.0.1".equals(host) && !exclusions.contains(host)) {
5356
try {
54-
URL url = new URL(proxyUrl);
55-
if (url.getProtocol().startsWith("http")) {
56-
clientBuilder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(url.getHost(), url.getPort() < 0 ? "https".equals(url.getProtocol()) ? 443 : 80 : url.getPort() )));
57-
} else if (url.getProtocol().startsWith("sock")) {
58-
clientBuilder.proxy(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(url.getHost(), url.getPort())));
59-
}
57+
URL proxyUrl = new URL(proxyUrlStr);
58+
clientBuilder.proxy(new ProxySelector() {
59+
60+
@Override
61+
public List<Proxy> select(URI uri) {
62+
try {
63+
URL url = uri.toURL();
64+
if (url.getProtocol().startsWith("http")) {
65+
return List.of(new Proxy(Proxy.Type.HTTP,
66+
new InetSocketAddress(proxyUrl.getHost(),
67+
proxyUrl.getPort() < 0
68+
? "https".equals(proxyUrl.getProtocol()) ? 443 : 80
69+
: proxyUrl.getPort())));
70+
} else if (url.getProtocol().startsWith("sock")) {
71+
return List.of(new Proxy(Proxy.Type.SOCKS,
72+
new InetSocketAddress(proxyUrl.getHost(), proxyUrl.getPort())));
73+
}
74+
} catch (MalformedURLException e) {
75+
log.error("", e);
76+
}
77+
return Collections.emptyList();
78+
}
79+
80+
@Override
81+
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
82+
}
83+
});
6084
String username = null, password = null;
61-
if (url.getUserInfo() == null || url.getUserInfo().isBlank()) {
85+
if (proxyUrl.getUserInfo() == null || proxyUrl.getUserInfo().isBlank()) {
6286
username = config.getRawSettings().getString("http", "proxy-user");
6387
password = config.getRawSettings().getString("http", "proxy-password");
6488
} else {
65-
String userInfo = url.getUserInfo();
89+
String userInfo = proxyUrl.getUserInfo();
6690
int idx = userInfo.indexOf(':');
6791
if (idx > 0 && idx < userInfo.length()) {
6892
username = userInfo.substring(0, idx);
@@ -72,21 +96,20 @@ public RestTemplate createRestTemplate(String host) {
7296
if (username != null && password != null && !username.isEmpty()) {
7397
final String user = username;
7498
final String pass = password;
75-
clientBuilder.proxyAuthenticator(new Authenticator() {
99+
clientBuilder.authenticator(new Authenticator() {
76100
@Override
77-
public Request authenticate(Route route, Response response) throws IOException {
78-
String credential = Credentials.basic(user, pass);
79-
return response.request().newBuilder().header("Proxy-Authorization", credential)
80-
.build();
101+
protected PasswordAuthentication getPasswordAuthentication() {
102+
return new PasswordAuthentication(user, pass.toCharArray());
81103
}
104+
82105
});
83106
}
84107
} catch (MalformedURLException e) {
85108
log.error("", e);
86109
}
87110
}
88111
}
89-
return new RestTemplate(new OkHttp3ClientHttpRequestFactory(clientBuilder.build()));
112+
return new RestTemplate(new JdkClientHttpRequestFactory(clientBuilder.build()));
90113
}
91114

92115
private static String getProxyUrlFromEnv() {

0 commit comments

Comments
 (0)