Skip to content

Commit f398dd0

Browse files
committed
Pass SockJS session attributes to HandshakeHandler
Before this change the WebSocketTransportHandler passed Collections.emptyMap as attributes to the HandshakeHandler because it didn't matter what attributes the underlying WebSocketSession has since it is wrapped by the SockJsSession and that's what exposed for use everywhere. This change has the WebSocketTransportHandler passing the attributes from the SockJsSession instead since it's more accurate for the underlying WebSocketSession to have access to the same map instance and it allows the HandshakeHandler to change the attributes even if it doesn't need to do that today. Issue: SPR-12716
1 parent f82c663 commit f398dd0

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/WebSocketSession.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -52,10 +52,12 @@ public interface WebSocketSession extends Closeable {
5252

5353
/**
5454
* Return the map with attributes associated with the WebSocket session.
55-
* <p>When the WebSocketSession is created, on the server side, the map can be
56-
* through a {@link org.springframework.web.socket.server.HandshakeInterceptor}.
57-
* On the client side, the map can be populated by passing attributes to the
58-
* {@link org.springframework.web.socket.client.WebSocketClient} handshake methods.
55+
* <p>On the server side the map can be populated initially through a
56+
* {@link org.springframework.web.socket.server.HandshakeInterceptor
57+
* HandshakeInterceptor}. On the client side the map can be populated via
58+
* {@link org.springframework.web.socket.client.WebSocketClient
59+
* WebSocketClient} handshake methods.
60+
* @return a Map with the session attributes, never {@code null}.
5961
*/
6062
Map<String, Object> getAttributes();
6163

spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/WebSocketTransportHandler.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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,7 +16,6 @@
1616

1717
package org.springframework.web.socket.sockjs.transport.handler;
1818

19-
import java.util.Collections;
2019
import java.util.Map;
2120

2221
import org.springframework.http.server.ServerHttpRequest;
@@ -67,10 +66,8 @@ public HandshakeHandler getHandshakeHandler() {
6766
}
6867

6968
@Override
70-
public AbstractSockJsSession createSession(
71-
String sessionId, WebSocketHandler handler, Map<String, Object> attributes) {
72-
73-
return new WebSocketServerSockJsSession(sessionId, getServiceConfig(), handler, attributes);
69+
public AbstractSockJsSession createSession(String id, WebSocketHandler handler, Map<String, Object> attrs) {
70+
return new WebSocketServerSockJsSession(id, getServiceConfig(), handler, attrs);
7471
}
7572

7673
@Override
@@ -80,7 +77,7 @@ public void handleRequest(ServerHttpRequest request, ServerHttpResponse response
8077
WebSocketServerSockJsSession sockJsSession = (WebSocketServerSockJsSession) wsSession;
8178
try {
8279
wsHandler = new SockJsWebSocketHandler(getServiceConfig(), wsHandler, sockJsSession);
83-
this.handshakeHandler.doHandshake(request, response, wsHandler, Collections.<String, Object>emptyMap());
80+
this.handshakeHandler.doHandshake(request, response, wsHandler, sockJsSession.getAttributes());
8481
}
8582
catch (Throwable ex) {
8683
sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);

0 commit comments

Comments
 (0)