diff --git a/extensions/websockets-next/deployment/src/main/java/io/quarkus/websockets/next/deployment/devui/WebSocketServerDevUIProcessor.java b/extensions/websockets-next/deployment/src/main/java/io/quarkus/websockets/next/deployment/devui/WebSocketServerDevUIProcessor.java index 5f89d5cfd0482..2785efe1fbdd4 100644 --- a/extensions/websockets-next/deployment/src/main/java/io/quarkus/websockets/next/deployment/devui/WebSocketServerDevUIProcessor.java +++ b/extensions/websockets-next/deployment/src/main/java/io/quarkus/websockets/next/deployment/devui/WebSocketServerDevUIProcessor.java @@ -40,7 +40,7 @@ public void pages(List endpoints, List root + .addClass(MyBean.class)); + + @Test + public void testConnectorIsInjected() { + assertEquals("1", RestAssured.get("mybeantest").then().statusCode(200).extract().body().asString()); + } + + @Singleton + public static class MyBean { + + @Inject + BasicWebSocketConnector connector; + + void addRoute(@Observes Router router) { + router.get("/mybeantest").handler(rc -> { + rc.end(connector != null ? "1" : "0"); + }); + } + + } + +} diff --git a/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/BasicWebSocketConnectorImpl.java b/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/BasicWebSocketConnectorImpl.java index 620369dddf0f4..6442502058725 100644 --- a/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/BasicWebSocketConnectorImpl.java +++ b/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/BasicWebSocketConnectorImpl.java @@ -116,10 +116,7 @@ public Uni connect() { // TODO would it make sense to share clients? WebSocketClient client = vertx.createWebSocketClient(populateClientOptions()); - WebSocketConnectOptions connectOptions = new WebSocketConnectOptions() - .setSsl(baseUri.getScheme().equals("https")) - .setHost(baseUri.getHost()) - .setPort(baseUri.getPort()); + WebSocketConnectOptions connectOptions = newConnectOptions(baseUri); StringBuilder requestUri = new StringBuilder(); String mergedPath = mergePath(baseUri.getPath(), replacePathParameters(path)); requestUri.append(mergedPath); diff --git a/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectorBase.java b/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectorBase.java index ee098d6a43d16..1556899636c9f 100644 --- a/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectorBase.java +++ b/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectorBase.java @@ -21,6 +21,7 @@ import io.vertx.core.Vertx; import io.vertx.core.buffer.Buffer; import io.vertx.core.http.WebSocketClientOptions; +import io.vertx.core.http.WebSocketConnectOptions; import io.vertx.core.net.SSLOptions; abstract class WebSocketConnectorBase> { @@ -191,4 +192,21 @@ protected WebSocketClientOptions populateClientOptions() { } return clientOptions; } + + protected WebSocketConnectOptions newConnectOptions(URI serverEndpointUri) { + WebSocketConnectOptions connectOptions = new WebSocketConnectOptions() + .setSsl(isHttps(serverEndpointUri)) + .setHost(serverEndpointUri.getHost()); + if (serverEndpointUri.getPort() != -1) { + connectOptions.setPort(serverEndpointUri.getPort()); + } else if (isHttps(serverEndpointUri)) { + // If port is undefined and https is used then use 443 by default + connectOptions.setPort(443); + } + return connectOptions; + } + + protected boolean isHttps(URI uri) { + return "https".equals(uri.getScheme()); + } } diff --git a/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectorImpl.java b/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectorImpl.java index cb04b5cbb61ad..686f132c71038 100644 --- a/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectorImpl.java +++ b/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectorImpl.java @@ -72,10 +72,7 @@ public Uni connect() { throw new WebSocketClientException(e); } - WebSocketConnectOptions connectOptions = new WebSocketConnectOptions() - .setSsl(serverEndpointUri.getScheme().equals("https")) - .setHost(serverEndpointUri.getHost()) - .setPort(serverEndpointUri.getPort()); + WebSocketConnectOptions connectOptions = newConnectOptions(serverEndpointUri); StringBuilder uri = new StringBuilder(); if (serverEndpointUri.getPath() != null) { uri.append(serverEndpointUri.getRawPath()); diff --git a/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/devui/WebSocketNextJsonRPCService.java b/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/devui/WebSocketNextJsonRPCService.java index 5878b1e6950fd..64916971e4c3e 100644 --- a/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/devui/WebSocketNextJsonRPCService.java +++ b/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/devui/WebSocketNextJsonRPCService.java @@ -12,6 +12,7 @@ import java.util.concurrent.ConcurrentMap; import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.Instance; import org.jboss.logging.Logger; @@ -52,16 +53,18 @@ public class WebSocketNextJsonRPCService implements ConnectionListener { private final WebSocketsServerRuntimeConfig.DevMode devModeConfig; - WebSocketNextJsonRPCService(ConnectionManager connectionManager, Vertx vertx, HttpConfiguration httpConfig, + WebSocketNextJsonRPCService(Instance connectionManager, Vertx vertx, HttpConfiguration httpConfig, WebSocketsServerRuntimeConfig config) { this.connectionStatus = BroadcastProcessor.create(); this.connectionMessages = BroadcastProcessor.create(); - this.connectionManager = connectionManager; + this.connectionManager = connectionManager.isResolvable() ? connectionManager.get() : null; this.vertx = vertx; this.httpConfig = httpConfig; this.devModeConfig = config.devMode(); this.sockets = new ConcurrentHashMap<>(); - connectionManager.addListener(this); + if (this.connectionManager != null) { + this.connectionManager.addListener(this); + } } public Multi connectionStatus() { @@ -74,14 +77,16 @@ public Multi connectionMessages() { public JsonObject getConnections(List endpoints) { JsonObject json = new JsonObject(); - for (String endpoint : endpoints) { - List connections = new ArrayList<>(connectionManager.getConnections(endpoint)); - connections.sort(Comparator.comparing(WebSocketConnection::creationTime)); - JsonArray array = new JsonArray(); - for (WebSocketConnection c : connections) { - array.add(toJsonObject(endpoint, c)); + if (connectionManager != null) { + for (String endpoint : endpoints) { + List connections = new ArrayList<>(connectionManager.getConnections(endpoint)); + connections.sort(Comparator.comparing(WebSocketConnection::creationTime)); + JsonArray array = new JsonArray(); + for (WebSocketConnection c : connections) { + array.add(toJsonObject(endpoint, c)); + } + json.put(endpoint, array); } - json.put(endpoint, array); } json.put("connectionMessagesLimit", devModeConfig.connectionMessagesLimit()); return json; @@ -103,6 +108,9 @@ public JsonArray getMessages(String connectionKey) { } public Uni openDevConnection(String path, String endpointPath) { + if (connectionManager == null) { + return failureUni(); + } if (isInvalidPath(path, endpointPath)) { LOG.errorf("Invalid path %s; original endpoint path %s", path, endpointPath); return failureUni(); @@ -179,6 +187,9 @@ private static String normalize(String path) { } public Uni closeDevConnection(String connectionKey) { + if (connectionManager == null) { + return failureUni(); + } DevWebSocket socket = sockets.remove(connectionKey); if (socket != null) { Uni uni = Uni.createFrom().completionStage(() -> socket.socket.close().toCompletionStage());