5
5
6
6
import info .unterrainer .oauthtokenmanager .OauthTokenManager ;
7
7
import io .javalin .Javalin ;
8
+ import io .javalin .websocket .WsExceptionHandler ;
8
9
import io .javalin .websocket .WsHandler ;
9
10
import lombok .extern .slf4j .Slf4j ;
10
11
@@ -19,27 +20,47 @@ public class WebsocketServer {
19
20
private boolean isOauthEnabled = false ;
20
21
21
22
public WebsocketServer () {
22
- wss = Javalin . create ( );
23
+ this ( null );
23
24
}
24
25
25
- public WebsocketServer (String keycloakHost , String keycloakRealm ) {
26
- this .keycloakHost = keycloakHost ;
27
- this .realm = keycloakRealm ;
28
-
26
+ public WebsocketServer (WsExceptionHandler <Exception > exceptionHandler ) {
29
27
try {
30
- tokenManager = new OauthTokenManager (this .keycloakHost , this .realm );
31
- tokenManager .initPublicKey ();
32
28
wss = Javalin .create ();
33
29
wss .exception (Exception .class , (e , ctx ) -> {
34
30
log .error ("Uncaught exception in Websocket-Server: {}" , e );
35
31
});
32
+ if (exceptionHandler != null )
33
+ wss .wsException (Exception .class , exceptionHandler );
36
34
wss .wsException (Exception .class , (e , ctx ) -> {
37
35
log .error ("Uncaught websocket-exception in Websocket-Server: {}" , e );
38
36
});
37
+ } catch (Exception e ) {
38
+ log .error ("Error initializing Websocket-Server." , e );
39
+ }
40
+ }
41
+
42
+ public WebsocketServer (String keycloakHost , String keycloakRealm ) {
43
+ this (keycloakHost , keycloakRealm , null );
44
+ }
45
+
46
+ public WebsocketServer (String keycloakHost , String keycloakRealm , WsExceptionHandler <Exception > exceptionHandler ) {
47
+ this (exceptionHandler );
48
+ if (keycloakHost == null || keycloakHost .isEmpty ()) {
49
+ throw new IllegalArgumentException ("Keycloak host must not be null or empty." );
50
+ }
51
+ if (keycloakRealm == null || keycloakRealm .isEmpty ()) {
52
+ throw new IllegalArgumentException ("Keycloak realm must not be null or empty." );
53
+ }
54
+ this .keycloakHost = keycloakHost ;
55
+ this .realm = keycloakRealm ;
56
+
57
+ try {
58
+ tokenManager = new OauthTokenManager (this .keycloakHost , this .realm );
59
+ tokenManager .initPublicKey ();
39
60
isOauthEnabled = true ;
40
61
} catch (Exception e ) {
41
- // Exceptions will terminate a request later on, but should not terminate the
42
- // main-thread here.
62
+ log . error ( "Error initializing OauthTokenManager." , e );
63
+ return ;
43
64
}
44
65
}
45
66
0 commit comments