22
22
import java .net .Proxy ;
23
23
import java .net .SocketAddress ;
24
24
import java .net .URI ;
25
- import java .util .Arrays ;
26
25
import java .util .Collections ;
27
26
import java .util .List ;
28
27
import java .util .Map ;
@@ -50,16 +49,6 @@ private GitpodServerLauncher(
50
49
this .client = client ;
51
50
}
52
51
53
- public GitpodServerConnection listen (
54
- String apiUrl ,
55
- String origin ,
56
- String userAgent ,
57
- String clientVersion ,
58
- String token
59
- ) throws Exception {
60
- return listen (apiUrl , origin , userAgent , clientVersion , token , Collections .emptyList (), null );
61
- }
62
-
63
52
public GitpodServerConnection listen (
64
53
String apiUrl ,
65
54
String origin ,
@@ -70,77 +59,52 @@ public GitpodServerConnection listen(
70
59
SSLContext sslContext
71
60
) throws Exception {
72
61
String gitpodHost = URI .create (apiUrl ).getHost ();
73
- HttpClient httpClient ;
74
- if (sslContext == null && proxies .size () == 0 ) {
75
- GitpodServerConnectionImpl connection = new GitpodServerConnectionImpl (gitpodHost );
76
- connection .setSession (ContainerProvider .getWebSocketContainer ().connectToServer (new Endpoint () {
77
- @ Override
78
- public void onOpen (Session session , EndpointConfig config ) {
79
- session .addMessageHandler (new WebSocketMessageHandler (messageReader , jsonHandler , remoteEndpoint ));
80
- messageWriter .setSession (session );
81
- client .notifyConnect ();
82
- }
62
+ GitpodServerConnectionImpl connection = new GitpodServerConnectionImpl (gitpodHost );
83
63
84
- @ Override
85
- public void onClose (Session session , CloseReason closeReason ) {
86
- connection .complete (closeReason );
87
- }
64
+ if (!proxies .isEmpty ()) {
65
+ HttpClient httpClient ;
66
+
67
+ if (sslContext == null ) {
68
+ httpClient = new HttpClient ();
69
+ } else {
70
+ SslContextFactory ssl = new SslContextFactory .Client ();
71
+ ssl .setSslContext (sslContext );
72
+ httpClient = new HttpClient (ssl );
73
+ }
88
74
89
- @ Override
90
- public void onError (Session session , Throwable thr ) {
91
- GitpodServerConnectionImpl .LOG .log (Level .WARNING , gitpodHost + ": connection error:" , thr );
92
- connection .completeExceptionally (thr );
75
+ for (Proxy proxy : proxies ) {
76
+ if (proxy .type ().equals (Proxy .Type .DIRECT )) {
77
+ continue ;
93
78
}
94
- }, ClientEndpointConfig .Builder .create ().configurator (new ClientEndpointConfig .Configurator () {
95
- @ Override
96
- public void beforeRequest (final Map <String , List <String >> headers ) {
97
- headers .put ("Origin" , Arrays .asList (origin ));
98
- headers .put ("Authorization" , Arrays .asList ("Bearer " + token ));
99
- headers .put ("User-Agent" , Arrays .asList (userAgent ));
100
- headers .put ("X-Client-Version" , Arrays .asList (clientVersion ));
79
+ SocketAddress proxyAddress = proxy .address ();
80
+ if (!(proxyAddress instanceof InetSocketAddress )) {
81
+ GitpodServerConnectionImpl .LOG .log (Level .WARNING , gitpodHost + ": unexpected proxy:" , proxy );
82
+ continue ;
83
+ }
84
+ String hostName = ((InetSocketAddress ) proxyAddress ).getHostString ();
85
+ int port = ((InetSocketAddress ) proxyAddress ).getPort ();
86
+ if (proxy .type ().equals (Proxy .Type .HTTP )) {
87
+ httpClient .getProxyConfiguration ().getProxies ().add (new HttpProxy (hostName , port ));
88
+ } else if (proxy .type ().equals (Proxy .Type .SOCKS )) {
89
+ httpClient .getProxyConfiguration ().getProxies ().add (new Socks4Proxy (hostName , port ));
101
90
}
102
- }).build (), URI .create (apiUrl )));
103
- return connection ;
104
- }
105
- if (sslContext == null ) {
106
- httpClient = new HttpClient ();
107
- } else {
108
- SslContextFactory ssl = new SslContextFactory .Client ();
109
- ssl .setSslContext (sslContext );
110
- httpClient = new HttpClient (ssl );
111
- }
112
- for (Proxy proxy : proxies ) {
113
- if (proxy .type ().equals (Proxy .Type .DIRECT )) {
114
- continue ;
115
- }
116
- SocketAddress proxyAddress = proxy .address ();
117
- if (!(proxyAddress instanceof InetSocketAddress )) {
118
- GitpodServerConnectionImpl .LOG .log (Level .WARNING , gitpodHost + ": unexpected proxy:" , proxy );
119
- continue ;
120
- }
121
- String hostName = ((InetSocketAddress ) proxyAddress ).getHostString ();
122
- int port = ((InetSocketAddress ) proxyAddress ).getPort ();
123
- if (proxy .type ().equals (Proxy .Type .HTTP )) {
124
- httpClient .getProxyConfiguration ().getProxies ().add (new HttpProxy (hostName , port ));
125
- } else if (proxy .type ().equals (Proxy .Type .SOCKS )) {
126
- httpClient .getProxyConfiguration ().getProxies ().add (new Socks4Proxy (hostName , port ));
127
91
}
128
- }
129
- ClientContainer container = new ClientContainer (httpClient );
130
- // allow clientContainer to own httpClient (for start/stop lifecycle)
131
- container .getClient ().addManaged (httpClient );
132
- container .start ();
133
92
134
- GitpodServerConnectionImpl connection = new GitpodServerConnectionImpl (gitpodHost );
135
- connection .whenComplete ((input , exception ) -> {
136
- try {
137
- container .stop ();
138
- } catch (Throwable t ) {
139
- GitpodServerConnectionImpl .LOG .log (Level .WARNING , gitpodHost + ": failed to stop websocket container:" , t );
140
- }
141
- });
93
+ ClientContainer container = new ClientContainer (httpClient );
94
+ // allow clientContainer to own httpClient (for start/stop lifecycle)
95
+ container .getClient ().addManaged (httpClient );
96
+ container .start ();
97
+
98
+ connection .whenComplete ((input , exception ) -> {
99
+ try {
100
+ container .stop ();
101
+ } catch (Throwable t ) {
102
+ GitpodServerConnectionImpl .LOG .log (Level .WARNING , gitpodHost + ": failed to stop websocket container:" , t );
103
+ }
104
+ });
105
+ }
142
106
143
- connection .setSession (container .connectToServer (new Endpoint () {
107
+ connection .setSession (ContainerProvider . getWebSocketContainer () .connectToServer (new Endpoint () {
144
108
@ Override
145
109
public void onOpen (Session session , EndpointConfig config ) {
146
110
session .addMessageHandler (new WebSocketMessageHandler (messageReader , jsonHandler , remoteEndpoint ));
@@ -161,10 +125,10 @@ public void onError(Session session, Throwable thr) {
161
125
}, ClientEndpointConfig .Builder .create ().configurator (new ClientEndpointConfig .Configurator () {
162
126
@ Override
163
127
public void beforeRequest (final Map <String , List <String >> headers ) {
164
- headers .put ("Origin" , Arrays . asList (origin ));
165
- headers .put ("Authorization" , Arrays . asList ("Bearer " + token ));
166
- headers .put ("User-Agent" , Arrays . asList (userAgent ));
167
- headers .put ("X-Client-Version" , Arrays . asList (clientVersion ));
128
+ headers .put ("Origin" , Collections . singletonList (origin ));
129
+ headers .put ("Authorization" , Collections . singletonList ("Bearer " + token ));
130
+ headers .put ("User-Agent" , Collections . singletonList (userAgent ));
131
+ headers .put ("X-Client-Version" , Collections . singletonList (clientVersion ));
168
132
}
169
133
}).build (), URI .create (apiUrl )));
170
134
return connection ;
0 commit comments