Skip to content

Commit 8039491

Browse files
committed
Merge branch 'create-tests'
2 parents 57b11d4 + 623eb69 commit 8039491

9 files changed

Lines changed: 1490 additions & 10 deletions

File tree

pom.xml

Lines changed: 15 additions & 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.19</version>
13+
<version>1.1.1</version>
1414
<name>WebsocketServer</name>
1515
<packaging>jar</packaging>
1616

@@ -40,6 +40,13 @@
4040
<artifactId>oauth-token-manager</artifactId>
4141
<version>1.0.11</version>
4242
</dependency>
43+
<!-- Test dependencies -->
44+
<dependency>
45+
<groupId>org.eclipse.jetty.websocket</groupId>
46+
<artifactId>websocket-client</artifactId>
47+
<version>9.4.38.v20210224</version>
48+
<scope>test</scope>
49+
</dependency>
4350
</dependencies>
4451

4552
<build>
@@ -63,5 +70,12 @@
6370
</plugin>
6471
</plugins>
6572
</pluginManagement>
73+
<plugins>
74+
<plugin>
75+
<groupId>org.apache.maven.plugins</groupId>
76+
<artifactId>maven-surefire-plugin</artifactId>
77+
<version>3.2.5</version>
78+
</plugin>
79+
</plugins>
6680
</build>
6781
</project>

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,21 @@ public WebsocketServer(Javalin server, WsExceptionHandler<Exception> exceptionHa
4848
this("", server, exceptionHandler);
4949
}
5050

51+
/// There is no exception-handler-chain in Javalin, so we have to do it ourselves.
52+
/// This means, if you provide your own exception handler, it will be called first,
53+
/// then the default one will be called, which just logs the exception.
5154
public WebsocketServer(String name, Javalin server, WsExceptionHandler<Exception> exceptionHandler) {
5255
this.name = name;
5356
try {
5457
wss = server;
5558
if (wss == null)
5659
wss = Javalin.create();
5760

58-
if (exceptionHandler != null)
59-
wss.wsException(Exception.class, exceptionHandler);
60-
6161
wss.wsException(Exception.class, (e, ctx) -> {
6262
log.error("(" + name + ") Uncaught websocket-exception in Websocket-Server: {}", e);
63+
if (exceptionHandler != null) {
64+
exceptionHandler.handle(e, ctx);
65+
}
6366
});
6467
} catch (Exception e) {
6568
log.error("(" + name + ") Error initializing Websocket-Server.", e);
@@ -131,6 +134,12 @@ public WebsocketServer start(int port) {
131134
log.debug("(" + name + ") Websocket server started on port: {}", port);
132135
return this;
133136
}
137+
138+
public WebsocketServer stop() {
139+
wss.stop();
140+
log.debug("(" + name + ") Websocket server stopped.");
141+
return this;
142+
}
134143

135144
public WebsocketServer ws(String path, Consumer<WsHandler> ws) {
136145
wss.ws(path, ws, new HashSet<>());
@@ -146,8 +155,8 @@ public WebsocketServer wsOauth(String path, WsOauthHandlerBase handler) {
146155
handler.setTokenHandler(tokenManager);
147156
wss.ws(path, ws -> {
148157
ws.onConnect(handler::onConnect);
149-
ws.onMessage(handler::onMessage);
150-
ws.onBinaryMessage(handler::onBinaryMessage);
158+
ws.onMessage(handler::onMsg);
159+
ws.onBinaryMessage(handler::onBinaryMsg);
151160
ws.onClose(handler::onClose);
152161
ws.onError(handler::onError);
153162
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public abstract class WsHandlerBase {
1212

1313
public abstract void onMsg(WsMessageContext ctx) throws Exception;
1414

15-
public abstract void onBinaryMessage(WsBinaryMessageContext ctx) throws Exception;
15+
public abstract void onBinaryMsg(WsBinaryMessageContext ctx) throws Exception;
1616

1717
public abstract void onClose(WsCloseContext ctx) throws Exception;
1818

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public void removeClient(Session session) {
6969
log.debug("(" + name + ") Removing client: [{}]", session.getRemoteAddress());
7070
clientsConnected.removeIf(client -> client.session.equals(session));
7171
clientsQuarantined.removeIf(client -> client.session.equals(session));
72+
tenantIdsBySession.remove(session);
7273
}
7374

7475
public WsConnectContext getClient(Session session) {
@@ -93,6 +94,10 @@ public boolean isConnected(Session session) {
9394

9495
@Override
9596
public void onConnect(WsConnectContext ctx) throws Exception {
97+
handleConnect(ctx);
98+
}
99+
100+
protected void handleConnect(WsConnectContext ctx) throws Exception {
96101
log.debug("(" + name + ") New client tries to connect: [{}]", ctx.session.getRemoteAddress());
97102
String token = ctx.header("Authorization");
98103
if (token == null || token.isEmpty()) {
@@ -116,9 +121,10 @@ public void onConnect(WsConnectContext ctx) throws Exception {
116121

117122
@Override
118123
public void onMsg(WsMessageContext ctx) throws Exception {
124+
handleMessage(ctx);
119125
}
120126

121-
public final void onMessage(WsMessageContext ctx) throws Exception {
127+
protected void handleMessage(WsMessageContext ctx) throws Exception {
122128
log.debug("(" + name + ") Received from [{}]: [{}]", ctx.session.getRemoteAddress(), ctx.message());
123129
if (isQuarantined(ctx.session)) {
124130
log.warn(
@@ -152,7 +158,11 @@ public final void onMessage(WsMessageContext ctx) throws Exception {
152158
}
153159

154160
@Override
155-
public void onBinaryMessage(WsBinaryMessageContext ctx) throws Exception {
161+
public void onBinaryMsg(WsBinaryMessageContext ctx) throws Exception {
162+
handleBinaryMessage(ctx);
163+
}
164+
165+
protected void handleBinaryMessage(WsBinaryMessageContext ctx) throws Exception {
156166
log.debug("(" + name + ") Received binary message from [{}]: [{}] bytes", ctx.session.getRemoteAddress(),
157167
ctx.data().length);
158168
if (isQuarantined(ctx.session)) {
@@ -162,16 +172,25 @@ public void onBinaryMessage(WsBinaryMessageContext ctx) throws Exception {
162172
ctx.session.close(1000, "(" + name + ") Unauthorized access from quarantined client");
163173
return;
164174
}
175+
onBinaryMsg(ctx);
165176
}
166177

167178
@Override
168179
public void onClose(WsCloseContext ctx) throws Exception {
180+
handleClose(ctx);
181+
}
182+
183+
protected void handleClose(WsCloseContext ctx) throws Exception {
169184
log.debug("(" + name + ") Disconnected client: [{}]", ctx.session.getRemoteAddress());
170185
removeClient(ctx.session);
171186
}
172187

173188
@Override
174189
public void onError(WsErrorContext ctx) throws Exception {
190+
handleError(ctx);
191+
}
192+
193+
protected void handleError(WsErrorContext ctx) throws Exception {
175194
Throwable t = ctx.error();
176195
if (t instanceof EOFException || t instanceof IOException) {
177196
log.debug("(" + name + ") Client disconnected [{}].", ctx.session.getRemoteAddress());

src/test/java/info/unterrainer/websocketserver/WebSocketServerTest.java renamed to src/test/java/info/unterrainer/websocketserver/WebSocketServerManualTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package info.unterrainer.websocketserver;
22

3-
public class WebSocketServerTest {
3+
public class WebSocketServerManualTest {
44

55
public static void main(String[] args) {
66
WebsocketServer server = new WebsocketServer("https://keycloak.lan.elite-zettl.at", "Cms");

0 commit comments

Comments
 (0)