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,55 @@ 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
+ WebSocketContainer webSocketContainer = ContainerProvider .getWebSocketContainer ();
63
+ GitpodServerConnectionImpl connection = new GitpodServerConnectionImpl (gitpodHost );
83
64
84
- @ Override
85
- public void onClose (Session session , CloseReason closeReason ) {
86
- connection .complete (closeReason );
87
- }
65
+ if (!proxies .isEmpty ()) {
66
+ HttpClient httpClient ;
67
+
68
+ if (sslContext == null ) {
69
+ httpClient = new HttpClient ();
70
+ } else {
71
+ SslContextFactory ssl = new SslContextFactory .Client ();
72
+ ssl .setSslContext (sslContext );
73
+ httpClient = new HttpClient (ssl );
74
+ }
88
75
89
- @ Override
90
- public void onError (Session session , Throwable thr ) {
91
- GitpodServerConnectionImpl .LOG .log (Level .WARNING , gitpodHost + ": connection error:" , thr );
92
- connection .completeExceptionally (thr );
76
+ for (Proxy proxy : proxies ) {
77
+ if (proxy .type ().equals (Proxy .Type .DIRECT )) {
78
+ continue ;
93
79
}
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 ));
80
+ SocketAddress proxyAddress = proxy .address ();
81
+ if (!(proxyAddress instanceof InetSocketAddress )) {
82
+ GitpodServerConnectionImpl .LOG .log (Level .WARNING , gitpodHost + ": unexpected proxy:" , proxy );
83
+ continue ;
84
+ }
85
+ String hostName = ((InetSocketAddress ) proxyAddress ).getHostString ();
86
+ int port = ((InetSocketAddress ) proxyAddress ).getPort ();
87
+ if (proxy .type ().equals (Proxy .Type .HTTP )) {
88
+ httpClient .getProxyConfiguration ().getProxies ().add (new HttpProxy (hostName , port ));
89
+ } else if (proxy .type ().equals (Proxy .Type .SOCKS )) {
90
+ httpClient .getProxyConfiguration ().getProxies ().add (new Socks4Proxy (hostName , port ));
101
91
}
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
92
}
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
93
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
- });
94
+ ClientContainer container = new ClientContainer (httpClient );
95
+ // allow clientContainer to own httpClient (for start/stop lifecycle)
96
+ container .getClient ().addManaged (httpClient );
97
+ container .start ();
98
+
99
+ connection .whenComplete ((input , exception ) -> {
100
+ try {
101
+ container .stop ();
102
+ } catch (Throwable t ) {
103
+ GitpodServerConnectionImpl .LOG .log (Level .WARNING , gitpodHost + ": failed to stop websocket container:" , t );
104
+ }
105
+ });
106
+
107
+ webSocketContainer = container ;
108
+ }
142
109
143
- connection .setSession (container .connectToServer (new Endpoint () {
110
+ connection .setSession (webSocketContainer .connectToServer (new Endpoint () {
144
111
@ Override
145
112
public void onOpen (Session session , EndpointConfig config ) {
146
113
session .addMessageHandler (new WebSocketMessageHandler (messageReader , jsonHandler , remoteEndpoint ));
@@ -161,10 +128,10 @@ public void onError(Session session, Throwable thr) {
161
128
}, ClientEndpointConfig .Builder .create ().configurator (new ClientEndpointConfig .Configurator () {
162
129
@ Override
163
130
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 ));
131
+ headers .put ("Origin" , Collections . singletonList (origin ));
132
+ headers .put ("Authorization" , Collections . singletonList ("Bearer " + token ));
133
+ headers .put ("User-Agent" , Collections . singletonList (userAgent ));
134
+ headers .put ("X-Client-Version" , Collections . singletonList (clientVersion ));
168
135
}
169
136
}).build (), URI .create (apiUrl )));
170
137
return connection ;
0 commit comments