From cc818c0d62faa5c58228df8c22074e5ab73a4cc9 Mon Sep 17 00:00:00 2001 From: James Netherton Date: Wed, 5 Apr 2023 08:40:11 +0100 Subject: [PATCH] CAMEL-19238: Set default port for vertx-websocket wss scheme --- .../websocket/VertxWebsocketComponent.java | 6 ++- .../websocket/VertxWebsocketConstants.java | 3 ++ .../websocket/VertxWebsocketEndpoint.java | 48 +++++++++++-------- ...rtxWebsocketDefaultPortAssignmentTest.java | 40 ++++++++++++++++ 4 files changed, 77 insertions(+), 20 deletions(-) create mode 100644 components/camel-vertx/camel-vertx-websocket/src/test/java/org/apache/camel/component/vertx/websocket/VertxWebsocketDefaultPortAssignmentTest.java diff --git a/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketComponent.java b/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketComponent.java index 3a487874117da..540cb5857157f 100644 --- a/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketComponent.java +++ b/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketComponent.java @@ -101,7 +101,7 @@ protected Endpoint createEndpoint(String uri, String remaining, Map 0) { - connectOptions.setPort(websocketURI.getPort()); - } - - String subProtocols = configuration.getClientSubProtocols(); - if (ObjectHelper.isNotEmpty(subProtocols)) { - connectOptions.setSubProtocols(Arrays.asList(subProtocols.split(","))); + VertxHelper.setupSSLOptions(getCamelContext(), sslContextParameters, clientOptions); } + WebSocketConnectOptions connectOptions = getWebSocketConnectOptions(clientOptions); CompletableFuture future = new CompletableFuture<>(); client.webSocket(connectOptions, result -> { if (!result.failed()) { @@ -164,7 +154,7 @@ protected WebSocket getWebSocket() throws Exception { future.completeExceptionally(result.cause()); } }); - webSocket = future.get(options.getConnectTimeout(), TimeUnit.MILLISECONDS); + webSocket = future.get(clientOptions.getConnectTimeout(), TimeUnit.MILLISECONDS); } return webSocket; } @@ -173,6 +163,26 @@ protected WebSocket getWebSocket(Exchange exchange) throws Exception { return getWebSocket().exceptionHandler(event -> exchange.setException(event.getCause())); } + protected WebSocketConnectOptions getWebSocketConnectOptions(HttpClientOptions options) { + URI websocketURI = configuration.getWebsocketURI(); + WebSocketConnectOptions connectOptions = new WebSocketConnectOptions(); + connectOptions.setHost(websocketURI.getHost()); + connectOptions.setURI(URISupport.pathAndQueryOf(websocketURI)); + connectOptions.setSsl(options.isSsl() || websocketURI.getScheme().length() == 3); + + if (websocketURI.getPort() > 0) { + connectOptions.setPort(websocketURI.getPort()); + } else { + connectOptions.setPort(connectOptions.isSsl() ? DEFAULT_VERTX_CLIENT_WSS_PORT : DEFAULT_VERTX_CLIENT_WS_PORT); + } + + String subProtocols = configuration.getClientSubProtocols(); + if (ObjectHelper.isNotEmpty(subProtocols)) { + connectOptions.setSubProtocols(Arrays.asList(subProtocols.split(","))); + } + return connectOptions; + } + protected Map getVertxHostRegistry() { return getComponent().getVertxHostRegistry(); } diff --git a/components/camel-vertx/camel-vertx-websocket/src/test/java/org/apache/camel/component/vertx/websocket/VertxWebsocketDefaultPortAssignmentTest.java b/components/camel-vertx/camel-vertx-websocket/src/test/java/org/apache/camel/component/vertx/websocket/VertxWebsocketDefaultPortAssignmentTest.java new file mode 100644 index 0000000000000..ae4be2cb79092 --- /dev/null +++ b/components/camel-vertx/camel-vertx-websocket/src/test/java/org/apache/camel/component/vertx/websocket/VertxWebsocketDefaultPortAssignmentTest.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.vertx.websocket; + +import io.vertx.core.http.HttpClientOptions; +import io.vertx.core.http.WebSocketConnectOptions; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.apache.camel.component.vertx.websocket.VertxWebsocketConstants.DEFAULT_VERTX_CLIENT_WSS_PORT; +import static org.apache.camel.component.vertx.websocket.VertxWebsocketConstants.DEFAULT_VERTX_CLIENT_WS_PORT; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class VertxWebsocketDefaultPortAssignmentTest extends VertxWebSocketTestSupport { + + @ParameterizedTest + @ValueSource(strings = { "", "ws:", "wss:" }) + void testDefaultPortAssignment(String wsScheme) { + String uri = "vertx-websocket:" + wsScheme + "localhost/test"; + VertxWebsocketEndpoint endpoint = context.getEndpoint(uri, VertxWebsocketEndpoint.class); + WebSocketConnectOptions connectOptions = endpoint.getWebSocketConnectOptions(new HttpClientOptions()); + + int expectedPort = wsScheme.startsWith("wss") ? DEFAULT_VERTX_CLIENT_WSS_PORT : DEFAULT_VERTX_CLIENT_WS_PORT; + assertEquals(expectedPort, connectOptions.getPort()); + } +}