You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2023/04/05 08:57:37 UTC

[camel] branch main updated: CAMEL-19238: Set default port for vertx-websocket wss scheme

This is an automated email from the ASF dual-hosted git repository.

jamesnetherton pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new f9d5075bbb0 CAMEL-19238: Set default port for vertx-websocket wss scheme
f9d5075bbb0 is described below

commit f9d5075bbb0ec594c1ed39e81e8eaf3f4ea648a9
Author: James Netherton <ja...@gmail.com>
AuthorDate: Wed Apr 5 08:40:11 2023 +0100

    CAMEL-19238: Set default port for vertx-websocket wss scheme
---
 .../vertx/websocket/VertxWebsocketComponent.java   |  6 ++-
 .../vertx/websocket/VertxWebsocketConstants.java   |  3 ++
 .../vertx/websocket/VertxWebsocketEndpoint.java    | 48 +++++++++++++---------
 .../VertxWebsocketDefaultPortAssignmentTest.java   | 40 ++++++++++++++++++
 4 files changed, 77 insertions(+), 20 deletions(-)

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 3a487874117..540cb585715 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 @@ public class VertxWebsocketComponent extends DefaultComponent implements SSLCont
         VertxWebsocketConfiguration configuration = new VertxWebsocketConfiguration();
         configuration.setWebsocketURI(websocketURI);
 
-        VertxWebsocketEndpoint endpoint = new VertxWebsocketEndpoint(uri, this, configuration);
+        VertxWebsocketEndpoint endpoint = createEndpointInstance(uri, configuration);
         setProperties(endpoint, parameters);
 
         if (configuration.getSslContextParameters() == null) {
@@ -111,6 +111,10 @@ public class VertxWebsocketComponent extends DefaultComponent implements SSLCont
         return endpoint;
     }
 
+    protected VertxWebsocketEndpoint createEndpointInstance(String uri, VertxWebsocketConfiguration configuration) {
+        return new VertxWebsocketEndpoint(uri, this, configuration);
+    }
+
     @Override
     protected void doInit() throws Exception {
         if (vertx == null) {
diff --git a/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketConstants.java b/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketConstants.java
index 98fd0692e71..9d6b6c9a16e 100644
--- a/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketConstants.java
+++ b/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketConstants.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.component.vertx.websocket;
 
+import io.vertx.core.http.HttpClientOptions;
 import io.vertx.core.net.NetServerOptions;
 import org.apache.camel.spi.Metadata;
 
@@ -23,6 +24,8 @@ public final class VertxWebsocketConstants {
 
     public static final String DEFAULT_VERTX_SERVER_HOST = NetServerOptions.DEFAULT_HOST;
     public static final int DEFAULT_VERTX_SERVER_PORT = NetServerOptions.DEFAULT_PORT;
+    public static final int DEFAULT_VERTX_CLIENT_WS_PORT = HttpClientOptions.DEFAULT_DEFAULT_PORT;
+    public static final int DEFAULT_VERTX_CLIENT_WSS_PORT = 443;
 
     @Metadata(description = "Sends the message to the client with the given connection key. You can\n" +
                             "use a comma separated list of keys to send a message to multiple clients",
diff --git a/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketEndpoint.java b/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketEndpoint.java
index 5f9dda8930a..002776107d1 100644
--- a/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketEndpoint.java
+++ b/components/camel-vertx/camel-vertx-websocket/src/main/java/org/apache/camel/component/vertx/websocket/VertxWebsocketEndpoint.java
@@ -44,6 +44,9 @@ import org.apache.camel.util.URISupport;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+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;
+
 @UriEndpoint(firstVersion = "3.5.0", scheme = "vertx-websocket", title = "Vert.x WebSocket",
              syntax = "vertx-websocket:host:port/path", category = { Category.WEBSOCKET },
              headersClass = VertxWebsocketConstants.class, lenientProperties = true)
@@ -129,31 +132,18 @@ public class VertxWebsocketEndpoint extends DefaultEndpoint {
         }
 
         if (webSocket == null || webSocket.isClosed()) {
-            HttpClientOptions options = configuration.getClientOptions();
+            HttpClientOptions clientOptions = configuration.getClientOptions();
 
-            if (options == null) {
-                options = new HttpClientOptions();
+            if (clientOptions == null) {
+                clientOptions = new HttpClientOptions();
             }
 
             SSLContextParameters sslContextParameters = configuration.getSslContextParameters();
             if (sslContextParameters != null) {
-                VertxHelper.setupSSLOptions(getCamelContext(), sslContextParameters, 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());
-            }
-
-            String subProtocols = configuration.getClientSubProtocols();
-            if (ObjectHelper.isNotEmpty(subProtocols)) {
-                connectOptions.setSubProtocols(Arrays.asList(subProtocols.split(",")));
+                VertxHelper.setupSSLOptions(getCamelContext(), sslContextParameters, clientOptions);
             }
 
+            WebSocketConnectOptions connectOptions = getWebSocketConnectOptions(clientOptions);
             CompletableFuture<WebSocket> future = new CompletableFuture<>();
             client.webSocket(connectOptions, result -> {
                 if (!result.failed()) {
@@ -164,7 +154,7 @@ public class VertxWebsocketEndpoint extends DefaultEndpoint {
                     future.completeExceptionally(result.cause());
                 }
             });
-            webSocket = future.get(options.getConnectTimeout(), TimeUnit.MILLISECONDS);
+            webSocket = future.get(clientOptions.getConnectTimeout(), TimeUnit.MILLISECONDS);
         }
         return webSocket;
     }
@@ -173,6 +163,26 @@ public class VertxWebsocketEndpoint extends DefaultEndpoint {
         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<VertxWebsocketHostKey, VertxWebsocketHost> 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 00000000000..ae4be2cb790
--- /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());
+    }
+}