WebSocketConnectionManager accepts prepared URI

Closes gh-28524
This commit is contained in:
rstoyanchev 2023-02-13 17:10:19 +00:00
parent ccbb4bdd27
commit 9fcafd2650
2 changed files with 29 additions and 3 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -49,9 +49,20 @@ public abstract class ConnectionManagerSupport implements SmartLifecycle {
private final Object lifecycleMonitor = new Object();
/**
* Constructor with a URI template and variables.
*/
public ConnectionManagerSupport(String uriTemplate, Object... uriVariables) {
this.uri = UriComponentsBuilder.fromUriString(uriTemplate).buildAndExpand(
uriVariables).encode().toUri();
this.uri = UriComponentsBuilder.fromUriString(uriTemplate).buildAndExpand(uriVariables).encode().toUri();
}
/**
* Constructor with a prepared {@link URI}.
* @param uri the url to connect to
* @since 6.0.5
*/
public ConnectionManagerSupport(URI uri) {
this.uri = uri;
}

View File

@ -16,6 +16,7 @@
package org.springframework.web.socket.client;
import java.net.URI;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@ -48,6 +49,9 @@ public class WebSocketConnectionManager extends ConnectionManagerSupport {
private final WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
/**
* Constructor with the client to use and a handler to handle messages with.
*/
public WebSocketConnectionManager(WebSocketClient client,
WebSocketHandler webSocketHandler, String uriTemplate, Object... uriVariables) {
@ -56,6 +60,17 @@ public class WebSocketConnectionManager extends ConnectionManagerSupport {
this.webSocketHandler = decorateWebSocketHandler(webSocketHandler);
}
/**
* Variant of {@link #WebSocketConnectionManager(WebSocketClient, WebSocketHandler, String, Object...)}
* with a prepared {@link URI}.
* @since 6.0.5
*/
public WebSocketConnectionManager(WebSocketClient client, WebSocketHandler webSocketHandler, URI uri) {
super(uri);
this.client = client;
this.webSocketHandler = decorateWebSocketHandler(webSocketHandler);
}
/**
* Set the sub-protocols to use. If configured, specified sub-protocols will be