11
11
package org .springframework .ide .vscode .boot .app ;
12
12
13
13
import java .io .IOException ;
14
+ import java .net .Authenticator ;
14
15
import java .net .InetSocketAddress ;
15
16
import java .net .MalformedURLException ;
17
+ import java .net .PasswordAuthentication ;
16
18
import java .net .Proxy ;
19
+ import java .net .ProxySelector ;
20
+ import java .net .SocketAddress ;
21
+ import java .net .URI ;
17
22
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 ;
18
27
import java .util .Set ;
19
28
20
29
import org .slf4j .Logger ;
21
30
import org .slf4j .LoggerFactory ;
22
- import org .springframework .http .client .OkHttp3ClientHttpRequestFactory ;
31
+ import org .springframework .http .client .JdkClientHttpRequestFactory ;
23
32
import org .springframework .stereotype .Component ;
24
33
import org .springframework .web .client .RestTemplate ;
25
34
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
-
33
35
@ Component
34
36
public class RestTemplateFactory {
35
37
@@ -42,27 +44,49 @@ public RestTemplateFactory(BootJavaConfig config) {
42
44
}
43
45
44
46
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 ();
48
50
}
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 ()) {
51
54
Set <String > exclusions = config .getRawSettings ().getStringSet ("http" , "proxy-exclusions" );
52
55
if (!"localhost" .equals (host ) && !"127.0.0.1" .equals (host ) && !exclusions .contains (host )) {
53
56
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
+ });
60
84
String username = null , password = null ;
61
- if (url .getUserInfo () == null || url .getUserInfo ().isBlank ()) {
85
+ if (proxyUrl .getUserInfo () == null || proxyUrl .getUserInfo ().isBlank ()) {
62
86
username = config .getRawSettings ().getString ("http" , "proxy-user" );
63
87
password = config .getRawSettings ().getString ("http" , "proxy-password" );
64
88
} else {
65
- String userInfo = url .getUserInfo ();
89
+ String userInfo = proxyUrl .getUserInfo ();
66
90
int idx = userInfo .indexOf (':' );
67
91
if (idx > 0 && idx < userInfo .length ()) {
68
92
username = userInfo .substring (0 , idx );
@@ -72,21 +96,20 @@ public RestTemplate createRestTemplate(String host) {
72
96
if (username != null && password != null && !username .isEmpty ()) {
73
97
final String user = username ;
74
98
final String pass = password ;
75
- clientBuilder .proxyAuthenticator (new Authenticator () {
99
+ clientBuilder .authenticator (new Authenticator () {
76
100
@ 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 ());
81
103
}
104
+
82
105
});
83
106
}
84
107
} catch (MalformedURLException e ) {
85
108
log .error ("" , e );
86
109
}
87
110
}
88
111
}
89
- return new RestTemplate (new OkHttp3ClientHttpRequestFactory (clientBuilder .build ()));
112
+ return new RestTemplate (new JdkClientHttpRequestFactory (clientBuilder .build ()));
90
113
}
91
114
92
115
private static String getProxyUrlFromEnv () {
0 commit comments