Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
[RFR-409] Fix Websocket in Vert.X
Browse files Browse the repository at this point in the history
  • Loading branch information
hb0 committed Jun 29, 2023
1 parent 1631ad6 commit 431c79a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
42 changes: 37 additions & 5 deletions api/src/main/java/de/cyface/api/HttpServer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2022 Cyface GmbH
* Copyright 2020-2023 Cyface GmbH
*
* This file is part of the Cyface API Library.
*
Expand Down Expand Up @@ -34,7 +34,7 @@
* A wrapper which starts the {@code HttpServer} for {@code ApiVerticle}s.
*
* @author Armin Schnabel
* @version 1.0.2
* @version 1.1.0
* @since 1.0.0
*/
@SuppressWarnings("unused") // Part of the API
Expand All @@ -59,24 +59,56 @@ public HttpServer(final int port) {
}

/**
* Starts the HTTP server provided by this application. This server runs the Cyface Collector REST-API.
* Starts the HTTP server provided by this application.
* <p>
* This server runs the Cyface Collector REST-API.
*
* @param vertx The Vertx instance to get the parameters from
* @param router The router for all the endpoints the HTTP server should serve
* @param startPromise Informs the caller about the successful or failed start of the server
*/
@SuppressWarnings("unused") // Part of the API
public void start(final Vertx vertx, final Router router, final Promise<Void> startPromise) {
Validate.notNull(router);
Validate.notNull(startPromise);
final var options = new HttpServerOptions();
options.setCompressionSupported(true);
start(vertx, router, startPromise, options);
}

/**
* Starts the HTTP server provided by this application with WebSocket support.
* <p>
* This server runs the Cyface Collector REST-API.
*
* @param vertx The Vertx instance to get the parameters from
* @param router The router for all the endpoints the HTTP server should serve
* @param startPromise Informs the caller about the successful or failed start of the server
*/
@SuppressWarnings("unused") // Part of the API
public void startWithWebSocket(final Vertx vertx, final Router router, final Promise<Void> startPromise) {
final var options = new HttpServerOptions();
options.setCompressionSupported(true);
// Make server respond with one of the sub-protocols sent by our client, in our case: ['Bearer', 'eyToken***'].
// When the server does not respond with one of both options, the websocket client won't accept the connection.
// The protocol name "Bearer" is made up, but injecting the auth token via protocol is a common workaround
// for the issue that the Javascript Websocket class does not allow to add an `Authorization` header [RFR-165].
options.setWebSocketSubProtocols(Collections.singletonList("Bearer"));
options.setRegisterWebSocketWriteHandlers(true);
start(vertx, router, startPromise, options);
}

/**
* Starts the HTTP server provided by this application.
* <p>
* This server runs the Cyface Collector REST-API.
*
* @param vertx The Vertx instance to get the parameters from
* @param router The router for all the endpoints the HTTP server should serve
* @param startPromise Informs the caller about the successful or failed start of the server
* @param options The options for the Http server.
*/
private void start(final Vertx vertx, final Router router, final Promise<Void> startPromise, final HttpServerOptions options) {
Validate.notNull(router);
Validate.notNull(startPromise);
vertx.createHttpServer(options)
.requestHandler(router)
.listen(port, serverStartup -> completeStartup(serverStartup, startPromise));
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ subprojects {
// If you increase this version, check if the `collector.GridFSStorageIT` breaks
// There is a bug up to version 4.4.1, scheduled to be fixed in 4.4.2
// https://github.com/vert-x3/vertx-mongo-client/issues/291
vertxVersion = '4.3.6'
vertxVersion = '4.4.1' // FIXME: Don't release yet as this version does not work with collector.
slf4jVersion = '2.0.7'
commonsLangVersion = '3.12.0'
cyfaceSerializationVersion = '2.3.5'
Expand Down

0 comments on commit 431c79a

Please sign in to comment.