Browse Source

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
pull/739/head
Rossen Stoyanchev 10 years ago
parent
commit
b4fa1c24cc
  1. 12
      spring-websocket/src/main/java/org/springframework/web/socket/WebSocketSession.java
  2. 11
      spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/WebSocketTransportHandler.java

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

@ -1,5 +1,5 @@ @@ -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 { @@ -52,10 +52,12 @@ public interface WebSocketSession extends Closeable {
/**
* Return the map with attributes associated with the WebSocket session.
* <p>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.
* <p>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<String, Object> getAttributes();

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

@ -1,5 +1,5 @@ @@ -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 @@ @@ -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 @@ -67,10 +66,8 @@ public class WebSocketTransportHandler extends AbstractTransportHandler
}
@Override
public AbstractSockJsSession createSession(
String sessionId, WebSocketHandler handler, Map<String, Object> attributes) {
return new WebSocketServerSockJsSession(sessionId, getServiceConfig(), handler, attributes);
public AbstractSockJsSession createSession(String id, WebSocketHandler handler, Map<String, Object> attrs) {
return new WebSocketServerSockJsSession(id, getServiceConfig(), handler, attrs);
}
@Override
@ -80,7 +77,7 @@ public class WebSocketTransportHandler extends AbstractTransportHandler @@ -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.<String, Object>emptyMap());
this.handshakeHandler.doHandshake(request, response, wsHandler, sockJsSession.getAttributes());
}
catch (Throwable ex) {
sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);

Loading…
Cancel
Save