Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected Endpoint createEndpoint(String uri, String remaining, Map<String, Obje
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) {
Expand All @@ -111,6 +111,10 @@ protected Endpoint createEndpoint(String uri, String remaining, Map<String, Obje
return endpoint;
}

protected VertxWebsocketEndpoint createEndpointInstance(String uri, VertxWebsocketConfiguration configuration) {
return new VertxWebsocketEndpoint(uri, this, configuration);
}

@Override
protected void doInit() throws Exception {
if (vertx == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
*/
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;

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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
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)
Expand Down Expand Up @@ -129,31 +132,18 @@ protected WebSocket getWebSocket() throws Exception {
}

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()) {
Expand All @@ -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;
}
Expand All @@ -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<VertxWebsocketHostKey, VertxWebsocketHost> getVertxHostRegistry() {
return getComponent().getVertxHostRegistry();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}