Skip to content

Commit a492327

Browse files
committed
add advanced error-handling
1 parent c34f85a commit a492327

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<modelVersion>4.0.0</modelVersion>
1212
<artifactId>websocket-server</artifactId>
13-
<version>1.0.7</version>
13+
<version>1.0.8</version>
1414
<name>WebsocketServer</name>
1515
<packaging>jar</packaging>
1616

src/main/java/info/unterrainer/websocketserver/WebsocketServer.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import info.unterrainer.oauthtokenmanager.OauthTokenManager;
77
import io.javalin.Javalin;
8+
import io.javalin.websocket.WsExceptionHandler;
89
import io.javalin.websocket.WsHandler;
910
import lombok.extern.slf4j.Slf4j;
1011

@@ -19,27 +20,47 @@ public class WebsocketServer {
1920
private boolean isOauthEnabled = false;
2021

2122
public WebsocketServer() {
22-
wss = Javalin.create();
23+
this(null);
2324
}
2425

25-
public WebsocketServer(String keycloakHost, String keycloakRealm) {
26-
this.keycloakHost = keycloakHost;
27-
this.realm = keycloakRealm;
28-
26+
public WebsocketServer(WsExceptionHandler<Exception> exceptionHandler) {
2927
try {
30-
tokenManager = new OauthTokenManager(this.keycloakHost, this.realm);
31-
tokenManager.initPublicKey();
3228
wss = Javalin.create();
3329
wss.exception(Exception.class, (e, ctx) -> {
3430
log.error("Uncaught exception in Websocket-Server: {}", e);
3531
});
32+
if (exceptionHandler != null)
33+
wss.wsException(Exception.class, exceptionHandler);
3634
wss.wsException(Exception.class, (e, ctx) -> {
3735
log.error("Uncaught websocket-exception in Websocket-Server: {}", e);
3836
});
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();
3960
isOauthEnabled = true;
4061
} 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;
4364
}
4465
}
4566

src/main/java/info/unterrainer/websocketserver/WsOauthHandlerBase.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void onConnect(WsConnectContext ctx) throws Exception {
6868
clientsConnected.add(ctx);
6969
} catch (Exception e) {
7070
log.debug("Token validation failed for client [{}]. Disconnecting.", ctx.session.getRemoteAddress(), e);
71-
ctx.session.close();
71+
ctx.session.close(1000, "Unauthorized access with invalid token");
7272
return;
7373
}
7474
}
@@ -83,7 +83,7 @@ public void onMessage(WsMessageContext ctx) throws Exception {
8383
log.warn("Invalid message from quarantined client [{}]. Disconnecting.",
8484
ctx.session.getRemoteAddress());
8585
removeClient(ctx.session);
86-
ctx.session.close();
86+
ctx.session.close(1000, "Unauthorized access from quarantined client");
8787
return;
8888
}
8989
try {
@@ -94,8 +94,8 @@ public void onMessage(WsMessageContext ctx) throws Exception {
9494
clientsQuarantined.removeIf(c -> c.session.equals(ctx.session));
9595
clientsConnected.add(client);
9696
} catch (Exception e) {
97-
ctx.session.close();
9897
log.debug("Token validation failed for client [{}]. Disconnecting.", ctx.session.getRemoteAddress(), e);
98+
ctx.session.close(1000, "Unauthorized access with invalid token");
9999
return;
100100
}
101101
}
@@ -107,7 +107,7 @@ public void onBinaryMessage(WsBinaryMessageContext ctx) throws Exception {
107107
if (isQuarantined(ctx.session)) {
108108
log.warn("Invalid Message from quarantined client [{}]. Disconnecting.", ctx.session.getRemoteAddress());
109109
removeClient(ctx.session);
110-
ctx.session.close();
110+
ctx.session.close(1000, "Unauthorized access from quarantined client");
111111
return;
112112
}
113113
}

0 commit comments

Comments
 (0)