32
32
import jakarta .websocket .ClientEndpointConfig ;
33
33
import jakarta .websocket .Endpoint ;
34
34
import jakarta .websocket .EndpointConfig ;
35
+ import jakarta .websocket .HandshakeResponse ;
35
36
import jakarta .websocket .Session ;
36
37
import java .io .DataInputStream ;
37
38
import java .io .File ;
64
65
import org .glassfish .tyrus .client .ClientManager ;
65
66
import org .glassfish .tyrus .client .ClientProperties ;
66
67
import org .glassfish .tyrus .client .SslEngineConfigurator ;
68
+ import org .glassfish .tyrus .client .exception .DeploymentHandshakeException ;
67
69
import org .glassfish .tyrus .container .jdk .client .JdkClientContainer ;
68
70
69
71
/**
@@ -340,13 +342,19 @@ public void onOpen(Session session, EndpointConfig config) {}
340
342
}
341
343
342
344
class Authenticator extends ClientEndpointConfig .Configurator {
345
+ HandshakeResponse hr ;
343
346
@ Override
344
347
public void beforeRequest (Map <String , List <String >> headers ) {
345
348
if (factory .authorization != null ) {
346
349
headers .put ("Authorization" , List .of (factory .authorization ));
347
350
}
348
351
}
352
+ @ Override
353
+ public void afterResponse (HandshakeResponse hr ) {
354
+ this .hr = hr ;
355
+ }
349
356
}
357
+ var authenticator = new Authenticator ();
350
358
351
359
ClientManager client = ClientManager .createClient (JdkClientContainer .class .getName ()); // ~ ContainerProvider.getWebSocketContainer()
352
360
client .getProperties ().put (ClientProperties .REDIRECT_ENABLED , true ); // https://tyrus-project.github.io/documentation/1.13.1/index/tyrus-proprietary-config.html#d0e1775
@@ -357,7 +365,21 @@ public void beforeRequest(Map<String, List<String>> headers) {
357
365
sslEngineConfigurator .setHostnameVerifier ((s , sslSession ) -> true );
358
366
client .getProperties ().put (ClientProperties .SSL_ENGINE_CONFIGURATOR , sslEngineConfigurator );
359
367
}
360
- Session session = client .connectToServer (new CLIEndpoint (), ClientEndpointConfig .Builder .create ().configurator (new Authenticator ()).build (), URI .create (url .replaceFirst ("^http" , "ws" ) + "cli/ws" ));
368
+ Session session ;
369
+ try {
370
+ session = client .connectToServer (new CLIEndpoint (), ClientEndpointConfig .Builder .create ().configurator (authenticator ).build (), URI .create (url .replaceFirst ("^http" , "ws" ) + "cli/ws" ));
371
+ } catch (DeploymentHandshakeException x ) {
372
+ System .err .println ("CLI handshake failed with status code " + x .getHttpStatusCode ());
373
+ if (authenticator .hr != null ) {
374
+ for (var entry : authenticator .hr .getHeaders ().entrySet ()) {
375
+ // org.glassfish.tyrus.core.Utils.parseHeaderValue improperly splits values like Date at commas, so undo that:
376
+ System .err .println (entry .getKey () + ": " + String .join (", " , entry .getValue ()));
377
+ }
378
+ // UpgradeResponse.getReasonPhrase is useless since Jetty generates it from the code,
379
+ // and the body is not accessible at all.
380
+ }
381
+ return 15 ; // compare CLICommand.main
382
+ }
361
383
PlainCLIProtocol .Output out = new PlainCLIProtocol .Output () {
362
384
@ Override
363
385
public void send (byte [] data ) throws IOException {
0 commit comments