From 48b62e80d55b321e3025e92b20a725f7e665aa28 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 19 Mar 2014 12:25:08 -0400 Subject: [PATCH] Rename handshakeAttributes to just attributes Issue: SPR-11566 --- .../web/socket/WebSocketSession.java | 12 +++++++---- .../adapter/AbstractWebSocketSession.java | 16 +++++++-------- .../adapter/jetty/JettyWebSocketSession.java | 19 ++++++++---------- .../standard/StandardWebSocketSession.java | 20 +++++++++---------- .../client/AbstractWebSocketClient.java | 8 +++++--- .../client/jetty/JettyWebSocketClient.java | 4 ++-- .../standard/StandardWebSocketClient.java | 4 ++-- .../socket/server/HandshakeInterceptor.java | 6 +++--- .../HttpSessionHandshakeInterceptor.java | 2 +- .../TransportHandlingSockJsService.java | 4 ++-- .../session/AbstractHttpSockJsSession.java | 4 ++-- .../session/AbstractSockJsSession.java | 15 ++++++++------ .../socket/handler/TestWebSocketSession.java | 4 ++-- 13 files changed, 62 insertions(+), 56 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 b882aaa747a..e20fbd9f231 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 @@ -50,11 +50,15 @@ public interface WebSocketSession { HttpHeaders getHandshakeHeaders(); /** - * Handshake request specific attributes. - * To add attributes to a server-side WebSocket session see - * {@link org.springframework.web.socket.server.HandshakeInterceptor}. + * 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. */ - Map getHandshakeAttributes(); + Map getAttributes(); /** * Return a {@link java.security.Principal} instance containing the name of the diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/adapter/AbstractWebSocketSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/adapter/AbstractWebSocketSession.java index 23a52498576..3024319983c 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/adapter/AbstractWebSocketSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/adapter/AbstractWebSocketSession.java @@ -43,22 +43,22 @@ public abstract class AbstractWebSocketSession implements NativeWebSocketSess private T nativeSession; - private final Map handshakeAttributes; + private final Map attributes; /** - * Class constructor - * @param handshakeAttributes attributes from the HTTP handshake to make available - * through the WebSocket session + * Create a new instance and associate the given attributes with it. + * + * @param attributes attributes from the HTTP handshake to associate with the WebSocket session */ - public AbstractWebSocketSession(Map handshakeAttributes) { - this.handshakeAttributes = handshakeAttributes; + public AbstractWebSocketSession(Map attributes) { + this.attributes = attributes; } @Override - public Map getHandshakeAttributes() { - return this.handshakeAttributes; + public Map getAttributes() { + return this.attributes; } @Override diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSession.java index d2b5f0917ba..46a2bda28af 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSession.java @@ -56,24 +56,21 @@ public class JettyWebSocketSession extends AbstractWebSocketSession { /** * Create a new {@link JettyWebSocketSession} instance. * - * @param handshakeAttributes attributes from the HTTP handshake to make available - * through the WebSocket session + * @param attributes attributes from the HTTP handshake to associate with the WebSocket session */ - public JettyWebSocketSession(Map handshakeAttributes) { - this(handshakeAttributes, null); + public JettyWebSocketSession(Map attributes) { + this(attributes, null); } /** * Create a new {@link JettyWebSocketSession} instance associated with the given user. * - * @param handshakeAttributes attributes from the HTTP handshake to make available - * through the WebSocket session - * @param user the user associated with the session; can be left - * {@code null} in which case, we'll fallback on the user available via - * {@link org.eclipse.jetty.websocket.api.Session#getUpgradeRequest()} + * @param attributes attributes from the HTTP handshake to associate with the WebSocket session + * @param user the user associated with the session; if {@code null} we'll fallback on the user + * available via {@link org.eclipse.jetty.websocket.api.Session#getUpgradeRequest()} */ - public JettyWebSocketSession(Map handshakeAttributes, Principal user) { - super(handshakeAttributes); + public JettyWebSocketSession(Map attributes, Principal user) { + super(attributes); this.user = user; } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/adapter/standard/StandardWebSocketSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/adapter/standard/StandardWebSocketSession.java index 7eaf08e732e..aa962a13b5d 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/adapter/standard/StandardWebSocketSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/adapter/standard/StandardWebSocketSession.java @@ -63,32 +63,31 @@ public class StandardWebSocketSession extends AbstractWebSocketSession * Class constructor. * * @param headers the headers of the handshake request - * @param handshakeAttributes attributes from the HTTP handshake to make available - * through the WebSocket session + * @param attributes attributes from the HTTP handshake to associate with the WebSocket session * @param localAddress the address on which the request was received * @param remoteAddress the address of the remote client */ - public StandardWebSocketSession(HttpHeaders headers, Map handshakeAttributes, + public StandardWebSocketSession(HttpHeaders headers, Map attributes, InetSocketAddress localAddress, InetSocketAddress remoteAddress) { - this(headers, handshakeAttributes, localAddress, remoteAddress, null); + this(headers, attributes, localAddress, remoteAddress, null); } /** * Class constructor that associates a user with the WebSocket session. * * @param headers the headers of the handshake request - * @param handshakeAttributes attributes from the HTTP handshake to make available - * through the WebSocket session + * @param attributes attributes from the HTTP handshake to associate with the WebSocket session * @param localAddress the address on which the request was received * @param remoteAddress the address of the remote client - * @param user the user associated with the session; can be left - * {@code null} in which case, we'll fallback on the user available via + * @param user the user associated with the session; if {@code null} we'll + * fallback on the user available in the underlying WebSocket session */ - public StandardWebSocketSession(HttpHeaders headers, Map handshakeAttributes, + public StandardWebSocketSession(HttpHeaders headers, Map attributes, InetSocketAddress localAddress, InetSocketAddress remoteAddress, Principal user) { - super(handshakeAttributes); + super(attributes); + headers = (headers != null) ? headers : new HttpHeaders(); this.handshakeHeaders = HttpHeaders.readOnlyHttpHeaders(headers); this.localAddress = localAddress; @@ -96,6 +95,7 @@ public class StandardWebSocketSession extends AbstractWebSocketSession this.user = user; } + @Override public String getId() { checkNativeSessionInitialized(); diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/client/AbstractWebSocketClient.java b/spring-websocket/src/main/java/org/springframework/web/socket/client/AbstractWebSocketClient.java index 9e341b92d4a..ea08db729a8 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/client/AbstractWebSocketClient.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/client/AbstractWebSocketClient.java @@ -103,18 +103,20 @@ public abstract class AbstractWebSocketClient implements WebSocketClient { /** * Perform the actual handshake to establish a connection to the server. + * * @param webSocketHandler the client-side handler for WebSocket messages * @param headers HTTP headers to use for the handshake, with unwanted (forbidden) * headers filtered out, never {@code null} * @param uri the target URI for the handshake, never {@code null} * @param subProtocols requested sub-protocols, or an empty list * @param extensions requested WebSocket extensions, or an empty list - * @param handshakeAttributes attributes to make available via - * {@link WebSocketSession#getHandshakeAttributes()}; currently always an empty map. + * @param attributes attributes to associate with the WebSocketSession, i.e. via + * {@link WebSocketSession#getAttributes()}; currently always an empty map. + * * @return the established WebSocket session wrapped in a ListenableFuture. */ protected abstract ListenableFuture doHandshakeInternal(WebSocketHandler webSocketHandler, HttpHeaders headers, URI uri, List subProtocols, List extensions, - Map handshakeAttributes); + Map attributes); } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/client/jetty/JettyWebSocketClient.java b/spring-websocket/src/main/java/org/springframework/web/socket/client/jetty/JettyWebSocketClient.java index 29e27c938df..3bc7a965ecf 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/client/jetty/JettyWebSocketClient.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/client/jetty/JettyWebSocketClient.java @@ -172,7 +172,7 @@ public class JettyWebSocketClient extends AbstractWebSocketClient implements Sma @Override public ListenableFuture doHandshakeInternal(WebSocketHandler wsHandler, HttpHeaders headers, final URI uri, List protocols, - List extensions, Map handshakeAttributes) { + List extensions, Map attributes) { final ClientUpgradeRequest request = new ClientUpgradeRequest(); request.setSubProtocols(protocols); @@ -186,7 +186,7 @@ public class JettyWebSocketClient extends AbstractWebSocketClient implements Sma } Principal user = getUser(); - final JettyWebSocketSession wsSession = new JettyWebSocketSession(handshakeAttributes, user); + final JettyWebSocketSession wsSession = new JettyWebSocketSession(attributes, user); final JettyWebSocketHandlerAdapter listener = new JettyWebSocketHandlerAdapter(wsHandler, wsSession); Callable connectTask = new Callable() { diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/client/standard/StandardWebSocketClient.java b/spring-websocket/src/main/java/org/springframework/web/socket/client/standard/StandardWebSocketClient.java index c221e40715f..a485620e519 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/client/standard/StandardWebSocketClient.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/client/standard/StandardWebSocketClient.java @@ -104,14 +104,14 @@ public class StandardWebSocketClient extends AbstractWebSocketClient { @Override protected ListenableFuture doHandshakeInternal(WebSocketHandler webSocketHandler, HttpHeaders headers, final URI uri, List protocols, - List extensions, Map handshakeAttributes) { + List extensions, Map attributes) { int port = getPort(uri); InetSocketAddress localAddress = new InetSocketAddress(getLocalHost(), port); InetSocketAddress remoteAddress = new InetSocketAddress(uri.getHost(), port); final StandardWebSocketSession session = new StandardWebSocketSession(headers, - handshakeAttributes, localAddress, remoteAddress); + attributes, localAddress, remoteAddress); final ClientEndpointConfig.Builder configBuidler = ClientEndpointConfig.Builder.create(); configBuidler.configurator(new StandardWebSocketClientConfigurator(headers)); diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/server/HandshakeInterceptor.java b/spring-websocket/src/main/java/org/springframework/web/socket/server/HandshakeInterceptor.java index 262d81e5de9..b02dfdaddf6 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/server/HandshakeInterceptor.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/server/HandshakeInterceptor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -40,8 +40,8 @@ public interface HandshakeInterceptor { * @param request the current request * @param response the current response * @param wsHandler the target WebSocket handler - * @param attributes attributes to make available via - * {@link WebSocketSession#getHandshakeAttributes()} + * @param attributes attributes from the HTTP handshake to associate with the + * WebSocket session, i.e. via {@link WebSocketSession#getAttributes()} * @return whether to proceed with the handshake {@code true} or abort {@code false} */ boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/server/support/HttpSessionHandshakeInterceptor.java b/spring-websocket/src/main/java/org/springframework/web/socket/server/support/HttpSessionHandshakeInterceptor.java index 611f37f1cd6..dc8e9da55ae 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/server/support/HttpSessionHandshakeInterceptor.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/server/support/HttpSessionHandshakeInterceptor.java @@ -34,7 +34,7 @@ import org.springframework.web.socket.server.HandshakeInterceptor; /** * An interceptor to copy HTTP session attributes into the map of "handshake attributes" - * made available through {@link WebSocketSession#getHandshakeAttributes()}. + * made available through {@link WebSocketSession#getAttributes()}. * * @author Rossen Stoyanchev * @since 4.0 diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportHandlingSockJsService.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportHandlingSockJsService.java index 2a5b8b383e7..42eaa1bd0c1 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportHandlingSockJsService.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportHandlingSockJsService.java @@ -269,7 +269,7 @@ public class TransportHandlingSockJsService extends AbstractSockJsService implem } private SockJsSession createSockJsSession(String sessionId, SockJsSessionFactory sessionFactory, - WebSocketHandler handler, Map handshakeAttributes) { + WebSocketHandler handler, Map attributes) { synchronized (this.sessions) { SockJsSession session = this.sessions.get(sessionId); @@ -283,7 +283,7 @@ public class TransportHandlingSockJsService extends AbstractSockJsService implem if (logger.isDebugEnabled()) { logger.debug("Creating new session with session id \"" + sessionId + "\""); } - session = sessionFactory.createSession(sessionId, handler, handshakeAttributes); + session = sessionFactory.createSession(sessionId, handler, attributes); this.sessions.put(sessionId, session); return session; } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractHttpSockJsSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractHttpSockJsSession.java index 6a95305bf38..5e49a510cf1 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractHttpSockJsSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractHttpSockJsSession.java @@ -72,9 +72,9 @@ public abstract class AbstractHttpSockJsSession extends AbstractSockJsSession { public AbstractHttpSockJsSession(String id, SockJsServiceConfig config, - WebSocketHandler wsHandler, Map handshakeAttributes) { + WebSocketHandler wsHandler, Map attributes) { - super(id, config, wsHandler, handshakeAttributes); + super(id, config, wsHandler, attributes); this.messageCache = new ArrayBlockingQueue(config.getHttpMessageCacheSize()); } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java index ffa7b6f4564..9bf13d9a904 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java @@ -93,7 +93,7 @@ public abstract class AbstractSockJsSession implements SockJsSession { private final WebSocketHandler handler; - private final Map handshakeAttributes; + private final Map attributes; private State state = State.NEW; @@ -105,12 +105,15 @@ public abstract class AbstractSockJsSession implements SockJsSession { /** + * Create a new instance. + * * @param id the session ID * @param config SockJS service configuration options * @param handler the recipient of SockJS messages + * @param attributes attributes from the HTTP handshake to associate with the WebSocket session */ - public AbstractSockJsSession(String id, SockJsServiceConfig config, - WebSocketHandler handler, Map handshakeAttributes) { + public AbstractSockJsSession(String id, SockJsServiceConfig config, WebSocketHandler handler, + Map attributes) { Assert.notNull(id, "SessionId must not be null"); Assert.notNull(config, "SockJsConfig must not be null"); @@ -119,7 +122,7 @@ public abstract class AbstractSockJsSession implements SockJsSession { this.id = id; this.config = config; this.handler = handler; - this.handshakeAttributes = handshakeAttributes; + this.attributes = attributes; } @@ -133,8 +136,8 @@ public abstract class AbstractSockJsSession implements SockJsSession { } @Override - public Map getHandshakeAttributes() { - return this.handshakeAttributes; + public Map getAttributes() { + return this.attributes; } public boolean isNew() { diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/handler/TestWebSocketSession.java b/spring-websocket/src/test/java/org/springframework/web/socket/handler/TestWebSocketSession.java index d9f2031732c..7d495611909 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/handler/TestWebSocketSession.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/handler/TestWebSocketSession.java @@ -95,12 +95,12 @@ public class TestWebSocketSession implements WebSocketSession { this.headers = headers; } - public void setHandshakeAttributes(Map attributes) { + public void setAttributes(Map attributes) { this.attributes = attributes; } @Override - public Map getHandshakeAttributes() { + public Map getAttributes() { return this.attributes; }