diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsClient.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsClient.java index 39d51fc6dace..559ac54ffd77 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsClient.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsClient.java @@ -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. @@ -55,6 +55,7 @@ * * @author Rossen Stoyanchev * @author Sam Brannen + * @author Juergen Hoeller * @since 4.1 * @see https://github.com/sockjs/sockjs-client * @see org.springframework.web.socket.sockjs.client.Transport @@ -242,7 +243,7 @@ public final CompletableFuture execute( CompletableFuture connectFuture = new CompletableFuture<>(); try { - SockJsUrlInfo sockJsUrlInfo = new SockJsUrlInfo(url); + SockJsUrlInfo sockJsUrlInfo = buildSockJsUrlInfo(url); ServerInfo serverInfo = getServerInfo(sockJsUrlInfo, getHttpRequestHeaders(headers)); createRequest(sockJsUrlInfo, headers, serverInfo).connect(handler, connectFuture); } @@ -255,6 +256,18 @@ public final CompletableFuture execute( return connectFuture; } + /** + * Create a new {@link SockJsUrlInfo} for the current client execution. + *

The default implementation builds a {@code SockJsUrlInfo} which + * calculates a random server id and session id if necessary. + * @param url the target URL + * @since 6.1.3 + * @see SockJsUrlInfo#SockJsUrlInfo(URI) + */ + protected SockJsUrlInfo buildSockJsUrlInfo(URI url) { + return new SockJsUrlInfo(url); + } + @Nullable private HttpHeaders getHttpRequestHeaders(@Nullable HttpHeaders webSocketHttpHeaders) { if (getHttpHeaderNames() == null || webSocketHttpHeaders == null) { diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsUrlInfo.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsUrlInfo.java index 9448b72867cf..3d861815e334 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsUrlInfo.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsUrlInfo.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 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. @@ -32,6 +32,7 @@ * and {@link #getTransportUrl(TransportType) transport} URLs. * * @author Rossen Stoyanchev + * @author Juergen Hoeller * @since 4.1 */ public class SockJsUrlInfo { @@ -51,10 +52,28 @@ public class SockJsUrlInfo { private UUID uuid; + /** + * Construct a new {@code SockJsUrlInfo} instance, + * calculating a random server id and session id if necessary. + * @param sockJsUrl the target URL + */ public SockJsUrlInfo(URI sockJsUrl) { this.sockJsUrl = sockJsUrl; } + /** + * Construct a new {@code SockJsUrlInfo} instance. + * @param sockJsUrl the target URL + * @param serverId a pre-determined server id, if any + * @param sessionId a pre-determined session id, if any + * @since 6.1.3 + */ + public SockJsUrlInfo(URI sockJsUrl, @Nullable String serverId, @Nullable String sessionId) { + this.sockJsUrl = sockJsUrl; + this.serverId = serverId; + this.sessionId = sessionId; + } + public URI getSockJsUrl() { return this.sockJsUrl;