4
4
import java .net .URI ;
5
5
import java .nio .ByteBuffer ;
6
6
import java .time .Duration ;
7
- import java .util .List ;
8
- import java .util .Map ;
9
7
import java .util .concurrent .CompletableFuture ;
10
8
import java .util .concurrent .TimeUnit ;
11
9
import java .util .function .Consumer ;
19
17
import info .unterrainer .websocketclient .exceptions .WebsocketConnectingException ;
20
18
import info .unterrainer .websocketclient .exceptions .WebsocketSendingMessageException ;
21
19
import jakarta .websocket .ClientEndpointConfig ;
22
- import jakarta .websocket .ClientEndpointConfig .Builder ;
23
20
import jakarta .websocket .CloseReason ;
24
21
import jakarta .websocket .Session ;
25
22
import lombok .Data ;
@@ -129,6 +126,7 @@ public void close() {
129
126
}
130
127
131
128
public void establish () {
129
+ String at = null ;
132
130
String accessToken = null ;
133
131
log .debug ("Establish called." );
134
132
@@ -141,26 +139,25 @@ public void establish() {
141
139
throw new WebsocketConnectingException ("Failed to create WebSocket ClientManager." , e );
142
140
}
143
141
endpoints = new WebsocketEndpoints (this );
144
- Builder c = ClientEndpointConfig .Builder .create ();
145
142
146
143
if (keycloakHost != null ) {
147
144
OauthTokenManager tokenManager = new OauthTokenManager (keycloakHost , keycloakClient );
148
145
LocalOauthTokens tokens = tokenManager .getTokensFromCredentials (keycloakClient , keycloakUser ,
149
146
keycloakPassword );
150
147
accessToken = tokens .getAccessToken ();
151
- String at = "Bearer " + accessToken ;
152
-
153
- c .configurator (new ClientEndpointConfig .Configurator () {
154
- @ Override
155
- public void beforeRequest (Map <String , List <String >> headers ) {
156
- headers .put ("Authorization" , List .of (at ));
157
- }
158
- });
148
+ at = "Bearer " + accessToken ;
159
149
}
160
- ClientEndpointConfig config = c .build ();
161
150
162
151
try {
163
- container .connectToServer (endpoints , config , URI .create (host ));
152
+ container .connectToServer (endpoints ,
153
+ ClientEndpointConfig .Builder .create ().configurator (new ClientEndpointConfig .Configurator () {
154
+ }).build (), URI .create (host ));
155
+ if (at != null ) {
156
+ Session s = awaitOpen (Duration .ofMillis (5000L ));
157
+ s .getBasicRemote ().sendText (at );
158
+ } else {
159
+ log .debug ("No access token provided, connecting without authentication." );
160
+ }
164
161
} catch (Exception e ) {
165
162
log .error ("Error connecting to WebSocket server: " , e );
166
163
throw new WebsocketConnectingException ("Failed to connect to WebSocket server at " + host , e );
0 commit comments