Skip to content

Minor fixes in spring-websocket #29408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
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 @@ -33,7 +33,7 @@
/**
* WebSocket {@link ConnectionManagerSupport connection manager} that connects
* to the server via {@link WebSocketContainer} and handles the session with an
* {@link javax.websocket.ClientEndpoint @ClientEndpoint} endpoint.
* {@link jakarta.websocket.ClientEndpoint @ClientEndpoint} endpoint.
*
* @author Rossen Stoyanchev
* @since 4.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public SockJsServiceRegistration setTaskScheduler(TaskScheduler scheduler) {
* server. Since the iframe needs to load the SockJS javascript client library,
* this property allows specifying where to load it from.
* <p>By default this is set to point to
* "https://cdn.jsdelivr.net/sockjs/0.3.4/sockjs.min.js". However it can
* "<a href="https://cdn.jsdelivr.net/sockjs/1.0.0/sockjs.min.js">sockjs.min.js</a>". However, it can
* also be set to point to a URL served by the application.
* <p>Note that it's possible to specify a relative URL in which case the URL
* must be relative to the iframe URL. For example assuming a SockJS endpoint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public CloseStatus getCloseStatus() {

@Override
public String toString() {
return "SessionDisconnectEvent[sessionId=" + this.sessionId + ", " + this.status.toString() + "]";
return "SessionDisconnectEvent[sessionId=" + this.sessionId + ", " + this.status + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public String encode(String... messages) {
}

/**
* Apply standard JSON string quoting (see https://www.json.org/).
* Apply standard JSON string quoting (see <a href="https://www.json.org/">json.org</a>).
*/
protected abstract char[] applyJsonQuoting(String content);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public String getName() {
* server. Since the iframe needs to load the SockJS javascript client library,
* this property allows specifying where to load it from.
* <p>By default this is set to point to
* "https://cdn.jsdelivr.net/sockjs/1.0.0/sockjs.min.js".
* "<a href="https://cdn.jsdelivr.net/sockjs/1.0.0/sockjs.min.js">sockjs.min.js</a>".
* However, it can also be set to point to a URL served by the application.
* <p>Note that it's possible to specify a relative URL in which case the URL
* must be relative to the iframe URL. For example assuming a SockJS endpoint
Expand Down Expand Up @@ -614,22 +614,23 @@ else if (request.getMethod() == HttpMethod.OPTIONS) {
private class IframeHandler implements SockJsRequestHandler {

private static final String IFRAME_CONTENT =
"<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
" <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\" />\n" +
" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n" +
" <script>\n" +
" document.domain = document.domain;\n" +
" _sockjs_onload = function(){SockJS.bootstrap_iframe();};\n" +
" </script>\n" +
" <script src=\"%s\"></script>\n" +
"</head>\n" +
"<body>\n" +
" <h2>Don't panic!</h2>\n" +
" <p>This is a SockJS hidden iframe. It's used for cross domain magic.</p>\n" +
"</body>\n" +
"</html>";
"""
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script>
document.domain = document.domain;
_sockjs_onload = function(){SockJS.bootstrap_iframe();};
</script>
<script src="%s"></script>
</head>
<body>
<h2>Don't panic!</h2>
<p>This is a SockJS hidden iframe. It's used for cross domain magic.</p>
</body>
</html>""";

@Override
public void handle(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,19 @@ public class HtmlFileTransportHandler extends AbstractHttpSendingTransportHandle

static {
StringBuilder sb = new StringBuilder(
"<!doctype html>\n" +
"<html><head>\n" +
" <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\" />\n" +
" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n" +
"</head><body><h2>Don't panic!</h2>\n" +
" <script>\n" +
" document.domain = document.domain;\n" +
" var c = parent.%s;\n" +
" c.start();\n" +
" function p(d) {c.message(d);};\n" +
" window.onload = function() {c.stop();};\n" +
" </script>"
"""
<!DOCTYPE html>
<html><head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head><body><h2>Don't panic!</h2>
<script>
document.domain = document.domain;
var c = parent.%s;
c.start();
function p(d) {c.message(d);};
window.onload = function() {c.stop();};
</script>"""
);

while (sb.length() < MINIMUM_PARTIAL_HTML_CONTENT_LENGTH) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,15 @@ public boolean equals(Object obj) {
private static class MyTypeToStringConverter implements Converter<MyType, String> {
@Override
public String convert(MyType source) {
return "_" + source.toString();
return "_" + source;
}
}


private static class MyTypeToBytesConverter implements Converter<MyType, byte[]> {
@Override
public byte[] convert(MyType source) {
return ("~" + source.toString()).getBytes();
return ("~" + source).getBytes();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.springframework.web.socket.client;

import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;

Expand Down Expand Up @@ -45,7 +44,7 @@ public class WebSocketConnectionManagerTests {

@Test
public void openConnection() throws Exception {
List<String> subprotocols = Arrays.asList("abc");
List<String> subprotocols = List.of("abc");

TestLifecycleWebSocketClient client = new TestLifecycleWebSocketClient(false);
WebSocketHandler handler = new TextWebSocketHandler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.springframework.web.socket.config.annotation;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -72,7 +71,7 @@ public void minimalRegistration() {
Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
assertThat(((WebSocketHttpRequestHandler) entry.getKey()).getWebSocketHandler()).isNotNull();
assertThat(((WebSocketHttpRequestHandler) entry.getKey()).getHandshakeInterceptors().size()).isEqualTo(1);
assertThat(entry.getValue()).isEqualTo(Arrays.asList("/foo"));
assertThat(entry.getValue()).isEqualTo(List.of("/foo"));
}

@Test
Expand Down Expand Up @@ -190,7 +189,7 @@ public void handshakeHandlerAndInterceptor() {
assertThat(mappings.size()).isEqualTo(1);

Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
assertThat(entry.getValue()).isEqualTo(Arrays.asList("/foo"));
assertThat(entry.getValue()).isEqualTo(List.of("/foo"));

WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler) entry.getKey();
assertThat(requestHandler.getWebSocketHandler()).isNotNull();
Expand All @@ -214,7 +213,7 @@ public void handshakeHandlerAndInterceptorWithAllowedOrigins() {
assertThat(mappings.size()).isEqualTo(1);

Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
assertThat(entry.getValue()).isEqualTo(Arrays.asList("/foo"));
assertThat(entry.getValue()).isEqualTo(List.of("/foo"));

WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler) entry.getKey();
assertThat(requestHandler.getWebSocketHandler()).isNotNull();
Expand All @@ -238,7 +237,7 @@ public void handshakeHandlerInterceptorWithSockJsService() {
assertThat(mappings.size()).isEqualTo(1);

Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
assertThat(entry.getValue()).isEqualTo(Arrays.asList("/foo/**"));
assertThat(entry.getValue()).isEqualTo(List.of("/foo/**"));

SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler) entry.getKey();
assertThat(requestHandler.getWebSocketHandler()).isNotNull();
Expand Down Expand Up @@ -270,7 +269,7 @@ public void handshakeHandlerInterceptorWithSockJsServiceAndAllowedOrigins() {
assertThat(mappings.size()).isEqualTo(1);

Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
assertThat(entry.getValue()).isEqualTo(Arrays.asList("/foo/**"));
assertThat(entry.getValue()).isEqualTo(List.of("/foo/**"));

SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler) entry.getKey();
assertThat(requestHandler.getWebSocketHandler()).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ public void handleMessageToClientWithConnectedFrame() {

assertThat(this.session.getSentMessages().size()).isEqualTo(1);
WebSocketMessage<?> textMessage = this.session.getSentMessages().get(0);
assertThat(textMessage.getPayload()).isEqualTo(("CONNECTED\n" + "user-name:joe\n" + "\n" + "\u0000"));
assertThat(textMessage.getPayload()).isEqualTo(("""
CONNECTED
user-name:joe

\u0000"""));
}

@Test
Expand All @@ -123,7 +127,11 @@ public void handleMessageToClientWithDestinationUserNameProvider() {

assertThat(this.session.getSentMessages().size()).isEqualTo(1);
WebSocketMessage<?> textMessage = this.session.getSentMessages().get(0);
assertThat(textMessage.getPayload()).isEqualTo(("CONNECTED\n" + "user-name:joe\n" + "\n" + "\u0000"));
assertThat(textMessage.getPayload()).isEqualTo(("""
CONNECTED
user-name:joe

\u0000"""));
}

@Test
Expand All @@ -142,8 +150,13 @@ public void handleMessageToClientWithSimpConnectAck() {

assertThat(this.session.getSentMessages().size()).isEqualTo(1);
TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
assertThat(actual.getPayload()).isEqualTo(("CONNECTED\n" + "version:1.2\n" + "heart-beat:15000,15000\n" +
"user-name:joe\n" + "\n" + "\u0000"));
assertThat(actual.getPayload()).isEqualTo(("""
CONNECTED
version:1.2
heart-beat:15000,15000
user-name:joe

\u0000"""));
}

@Test
Expand All @@ -161,8 +174,13 @@ public void handleMessageToClientWithSimpConnectAckDefaultHeartBeat() {

assertThat(this.session.getSentMessages().size()).isEqualTo(1);
TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
assertThat(actual.getPayload()).isEqualTo(("CONNECTED\n" + "version:1.0\n" + "heart-beat:0,0\n" +
"user-name:joe\n" + "\n" + "\u0000"));
assertThat(actual.getPayload()).isEqualTo(("""
CONNECTED
version:1.0
heart-beat:0,0
user-name:joe

\u0000"""));
}

@Test
Expand All @@ -178,8 +196,12 @@ public void handleMessageToClientWithSimpDisconnectAck() {

assertThat(this.session.getSentMessages().size()).isEqualTo(1);
TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
assertThat(actual.getPayload()).isEqualTo(("ERROR\n" + "message:Session closed.\n" + "content-length:0\n" +
"\n\u0000"));
assertThat(actual.getPayload()).isEqualTo(("""
ERROR
message:Session closed.
content-length:0

\u0000"""));
}

@Test
Expand All @@ -196,7 +218,11 @@ public void handleMessageToClientWithSimpDisconnectAckAndReceipt() {

assertThat(this.session.getSentMessages().size()).isEqualTo(1);
TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
assertThat(actual.getPayload()).isEqualTo(("RECEIPT\n" + "receipt-id:message-123\n" + "\n\u0000"));
assertThat(actual.getPayload()).isEqualTo(("""
RECEIPT
receipt-id:message-123

\u0000"""));
}

@Test
Expand Down Expand Up @@ -391,7 +417,11 @@ public void handleMessageFromClientWithTokenAuthentication() {
assertThat(this.session.getSentMessages()).hasSize(1);
WebSocketMessage<?> textMessage = this.session.getSentMessages().get(0);
assertThat(textMessage.getPayload())
.isEqualTo("CONNECTED\n" + "user-name:__pete__@gmail.com\n" + "\n" + "\u0000");
.isEqualTo("""
CONNECTED
user-name:__pete__@gmail.com

\u0000""");
}

@Test
Expand Down Expand Up @@ -470,7 +500,11 @@ public void eventPublicationWithExceptions() {

assertThat(this.session.getSentMessages().size()).isEqualTo(1);
textMessage = (TextMessage) this.session.getSentMessages().get(0);
assertThat(textMessage.getPayload()).isEqualTo(("CONNECTED\n" + "user-name:joe\n" + "\n" + "\u0000"));
assertThat(textMessage.getPayload()).isEqualTo(("""
CONNECTED
user-name:joe

\u0000"""));

this.protocolHandler.afterSessionEnded(this.session, CloseStatus.BAD_DATA, this.channel);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.web.socket.messaging;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -70,7 +71,7 @@ public class SubProtocolWebSocketHandlerTests {
public void setup() {
this.webSocketHandler = new SubProtocolWebSocketHandler(this.inClientChannel, this.outClientChannel);
given(stompHandler.getSupportedProtocols()).willReturn(Arrays.asList("v10.stomp", "v11.stomp", "v12.stomp"));
given(mqttHandler.getSupportedProtocols()).willReturn(Arrays.asList("MQTT"));
given(mqttHandler.getSupportedProtocols()).willReturn(List.of("MQTT"));
this.session = new TestWebSocketSession();
this.session.setId("1");
this.session.setOpen(true);
Expand Down Expand Up @@ -133,7 +134,7 @@ public void emptySubProtocol() throws Exception {

@Test
public void noSubProtocolOneHandler() throws Exception {
this.webSocketHandler.setProtocolHandlers(Arrays.asList(stompHandler));
this.webSocketHandler.setProtocolHandlers(List.of(stompHandler));
this.webSocketHandler.afterConnectionEstablished(session);

verify(this.stompHandler).afterSessionStarted(
Expand Down Expand Up @@ -164,7 +165,7 @@ public void checkSession() throws Exception {
session1.setAcceptedProtocol("v12.stomp");
session2.setAcceptedProtocol("v12.stomp");

this.webSocketHandler.setProtocolHandlers(Arrays.asList(this.stompHandler));
this.webSocketHandler.setProtocolHandlers(List.of(this.stompHandler));
this.webSocketHandler.afterConnectionEstablished(session1);
this.webSocketHandler.afterConnectionEstablished(session2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void sameOriginMatchWithEmptyAllowedOrigins() throws Exception {
public void sameOriginMatchWithAllowedOrigins() throws Exception {
this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://mydomain2.example");
this.servletRequest.setServerName("mydomain2.example");
OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(Arrays.asList("http://mydomain1.example"));
OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(List.of("http://mydomain1.example"));
assertThat(interceptor.beforeHandshake(request, response, wsHandler, attributes)).isTrue();
assertThat(HttpStatus.FORBIDDEN.value()).isNotEqualTo(servletResponse.getStatus());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ public class RestTemplateXhrTransportTests {

@Test
public void connectReceiveAndClose() throws Exception {
String body = "o\n" + "a[\"foo\"]\n" + "c[3000,\"Go away!\"]";
String body = """
o
a["foo"]
c[3000,"Go away!"]""";
ClientHttpResponse response = response(HttpStatus.OK, body);
connect(response);

Expand All @@ -91,7 +94,7 @@ public void connectReceiveAndCloseWithPrelude() throws Exception {
for (int i = 0; i < 2048; i++) {
sb.append('h');
}
String body = sb.toString() + "\n" + "o\n" + "a[\"foo\"]\n" + "c[3000,\"Go away!\"]";
String body = sb + "\n" + "o\n" + "a[\"foo\"]\n" + "c[3000,\"Go away!\"]";
ClientHttpResponse response = response(HttpStatus.OK, body);
connect(response);

Expand Down Expand Up @@ -157,7 +160,11 @@ public void errorResponseStatus() throws Exception {

@Test
public void responseClosedAfterDisconnected() throws Exception {
String body = "o\n" + "c[3000,\"Go away!\"]\n" + "a[\"foo\"]\n";
String body = """
o
c[3000,"Go away!"]
a["foo"]
""";
ClientHttpResponse response = response(HttpStatus.OK, body);
connect(response);

Expand Down
Loading