From b4fa1c24cc782a90935bde9275bfb203d4c97b14 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 16 Feb 2015 14:12:12 -0500 Subject: [PATCH] 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 --- .../springframework/web/socket/WebSocketSession.java | 12 +++++++----- .../transport/handler/WebSocketTransportHandler.java | 11 ++++------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketSession.java index 56038e240c..f21edfa013 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/WebSocketSession.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -52,10 +52,12 @@ public interface WebSocketSession extends Closeable { /** * Return the map with attributes associated with the WebSocket session. - *

When the WebSocketSession is created, on the server side, the map can be - * through a {@link org.springframework.web.socket.server.HandshakeInterceptor}. - * On the client side, the map can be populated by passing attributes to the - * {@link org.springframework.web.socket.client.WebSocketClient} handshake methods. + *

On the server side the map can be populated initially through a + * {@link org.springframework.web.socket.server.HandshakeInterceptor + * HandshakeInterceptor}. On the client side the map can be populated via + * {@link org.springframework.web.socket.client.WebSocketClient + * WebSocketClient} handshake methods. + * @return a Map with the session attributes, never {@code null}. */ Map getAttributes(); diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/WebSocketTransportHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/WebSocketTransportHandler.java index df1c81617f..5a64cb0c48 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/WebSocketTransportHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/WebSocketTransportHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -16,7 +16,6 @@ package org.springframework.web.socket.sockjs.transport.handler; -import java.util.Collections; import java.util.Map; import org.springframework.http.server.ServerHttpRequest; @@ -67,10 +66,8 @@ public class WebSocketTransportHandler extends AbstractTransportHandler } @Override - public AbstractSockJsSession createSession( - String sessionId, WebSocketHandler handler, Map attributes) { - - return new WebSocketServerSockJsSession(sessionId, getServiceConfig(), handler, attributes); + public AbstractSockJsSession createSession(String id, WebSocketHandler handler, Map attrs) { + return new WebSocketServerSockJsSession(id, getServiceConfig(), handler, attrs); } @Override @@ -80,7 +77,7 @@ public class WebSocketTransportHandler extends AbstractTransportHandler WebSocketServerSockJsSession sockJsSession = (WebSocketServerSockJsSession) wsSession; try { wsHandler = new SockJsWebSocketHandler(getServiceConfig(), wsHandler, sockJsSession); - this.handshakeHandler.doHandshake(request, response, wsHandler, Collections.emptyMap()); + this.handshakeHandler.doHandshake(request, response, wsHandler, sockJsSession.getAttributes()); } catch (Throwable ex) { sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);