Skip to content

Commit fe9b59e

Browse files
GH-3813: ClientWebSocketContainer URI setting (#8561)
* GH-3813: ClientWebSocketContainer URI setting Fixes #3813 Introduce a `ClientWebSocketContainer(WebSocketClient client, URI uri)` ctor to let end-user to decide what and how should be encoded the URI for WebSocket connection * Fix language in docs Co-authored-by: Gary Russell <[email protected]> --------- Co-authored-by: Gary Russell <[email protected]>
1 parent 86b4ad5 commit fe9b59e

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

spring-integration-websocket/src/main/java/org/springframework/integration/websocket/ClientWebSocketContainer.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2022 the original author or authors.
2+
* Copyright 2014-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.integration.websocket;
1818

19+
import java.net.URI;
1920
import java.util.Map;
2021
import java.util.concurrent.CompletableFuture;
2122
import java.util.concurrent.CountDownLatch;
@@ -56,21 +57,32 @@ public final class ClientWebSocketContainer extends IntegrationWebSocketContaine
5657

5758
private final IntegrationWebSocketConnectionManager connectionManager;
5859

60+
private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
61+
5962
private volatile CountDownLatch connectionLatch;
6063

6164
private volatile WebSocketSession clientSession;
6265

6366
private volatile Throwable openConnectionException;
6467

65-
private volatile int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
66-
6768
private volatile boolean connecting;
6869

6970
public ClientWebSocketContainer(WebSocketClient client, String uriTemplate, Object... uriVariables) {
7071
Assert.notNull(client, "'client' must not be null");
7172
this.connectionManager = new IntegrationWebSocketConnectionManager(client, uriTemplate, uriVariables);
7273
}
7374

75+
/**
76+
* Constructor with a prepared {@link URI}.
77+
* @param client the {@link WebSocketClient} to use.
78+
* @param uri the url to connect to
79+
* @since 6.1
80+
*/
81+
public ClientWebSocketContainer(WebSocketClient client, URI uri) {
82+
Assert.notNull(client, "'client' must not be null");
83+
this.connectionManager = new IntegrationWebSocketConnectionManager(client, uri);
84+
}
85+
7486
public void setOrigin(String origin) {
7587
this.headers.setOrigin(origin);
7688
}
@@ -204,13 +216,18 @@ private final class IntegrationWebSocketConnectionManager extends ConnectionMana
204216

205217
private final boolean syncClientLifecycle;
206218

207-
IntegrationWebSocketConnectionManager(WebSocketClient client, String uriTemplate,
208-
Object... uriVariables) {
219+
IntegrationWebSocketConnectionManager(WebSocketClient client, String uriTemplate, Object... uriVariables) {
209220
super(uriTemplate, uriVariables);
210221
this.client = client;
211222
this.syncClientLifecycle = ((client instanceof Lifecycle) && !((Lifecycle) client).isRunning());
212223
}
213224

225+
IntegrationWebSocketConnectionManager(WebSocketClient client, URI uri) {
226+
super(uri);
227+
this.client = client;
228+
this.syncClientLifecycle = ((client instanceof Lifecycle) && !((Lifecycle) client).isRunning());
229+
}
230+
214231
@Override
215232
public void startInternal() {
216233
if (this.syncClientLifecycle) {

spring-integration-websocket/src/test/java/org/springframework/integration/websocket/ClientWebSocketContainerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2022 the original author or authors.
2+
* Copyright 2014-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -93,7 +93,7 @@ protected CompletableFuture<WebSocketSession> executeInternal(WebSocketHandler w
9393
webSocketClient.setUserProperties(userProperties);
9494

9595
ClientWebSocketContainer container =
96-
new ClientWebSocketContainer(webSocketClient, server.getWsBaseUrl() + "/ws/websocket");
96+
new ClientWebSocketContainer(webSocketClient, new URI(server.getWsBaseUrl() + "/ws/websocket"));
9797

9898
TestWebSocketListener messageListener = new TestWebSocketListener();
9999
container.setMessageListener(messageListener);

src/reference/asciidoc/web-sockets.adoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ It does so under the provided `paths` and other server WebSocket options (such a
8787
This registration is achieved with an infrastructural `WebSocketIntegrationConfigurationInitializer` component, which does the same as the `@EnableWebSocket` annotation.
8888
This means that, by using `@EnableIntegration` (or any Spring Integration namespace in the application context), you can omit the `@EnableWebSocket` declaration, because the Spring Integration infrastructure detects all WebSocket endpoints.
8989

90+
Starting with version 6.1, the `ClientWebSocketContainer` can be configured with a provided `URI` instead of `uriTemplate` and `uriVariables` combination.
91+
This is useful in cases when custom encoding is required for some parts of the uri.
92+
See an `UriComponentsBuilder` API for convenience.
93+
9094
[[web-socket-inbound-adapter]]
9195
=== WebSocket Inbound Channel Adapter
9296

@@ -431,4 +435,4 @@ dynamicServerFlow.destroy();
431435
NOTE: It is important to call `.addBean(serverWebSocketContainer)` on the dynamic flow registration to add the instance of `ServerWebSocketContainer` into an `ApplicationContext` for endpoint registration.
432436
When a dynamic flow registration is destroyed, the associated `ServerWebSocketContainer` instance is destroyed, too, as well as the respective endpoint registration, including URL path mappings.
433437

434-
IMPORTANT: The dynamic Websocket endpoints can only be registered via Spring Integration mechanism: when regular Spring `@EnableWebsocket` is used, Spring Integration configuration backs off and no infrastructure for dynamic endpoints is registered.
438+
IMPORTANT: The dynamic Websocket endpoints can only be registered via Spring Integration mechanism: when regular Spring `@EnableWebsocket` is used, Spring Integration configuration backs off and no infrastructure for dynamic endpoints is registered.

src/reference/asciidoc/whats-new.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@ See <<./zip.adoc#zip,Zip Support>> for more information.
2626

2727
[[x6.1-general]]
2828
=== General Changes
29+
30+
31+
[[x6.1-web-sockets]]
32+
=== Web Sockets Changes
33+
34+
A `ClientWebSocketContainer` can now be configured with a predefined `URI` instead of a combination of `uriTemplate` and `uriVariables`.
35+
See <<./web-sockets.adoc#web-socket-overview, WebSocket Overview>> for more information.

0 commit comments

Comments
 (0)