Class WebSocketSessionDecorator
- java.lang.Object
- org.springframework.web.socket.handler.WebSocketSessionDecorator
- All Implemented Interfaces:
Closeable
,AutoCloseable
,WebSocketSession
- Direct Known Subclasses:
ConcurrentWebSocketSessionDecorator
public class WebSocketSessionDecorator extends Object implements WebSocketSession
Wraps anotherWebSocketSession
instance and delegates to it.Also provides a
getDelegate()
method to return the decorated session as well as agetLastSession()
method to go through all nested delegates and return the "last" session.- Since:
- 4.0.3
- Author:
- Rossen Stoyanchev
Constructor Summary
Constructors Constructor Description WebSocketSessionDecorator(WebSocketSession session)
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Close the WebSocket connection with status 1000, i.e.void
close(CloseStatus status)
Close the WebSocket connection with the given close status.String
getAcceptedProtocol()
Return the negotiated sub-protocol.Map<String,Object>
getAttributes()
Return the map with attributes associated with the WebSocket session.int
getBinaryMessageSizeLimit()
Get the configured maximum size for an incoming binary message.WebSocketSession
getDelegate()
List<WebSocketExtension>
getExtensions()
Determine the negotiated extensions.HttpHeaders
getHandshakeHeaders()
Return the headers used in the handshake request (nevernull
).String
getId()
Return a unique session identifier.WebSocketSession
getLastSession()
InetSocketAddress
getLocalAddress()
Return the address on which the request was received.Principal
getPrincipal()
Return aPrincipal
instance containing the name of the authenticated user.InetSocketAddress
getRemoteAddress()
Return the address of the remote client.int
getTextMessageSizeLimit()
Get the configured maximum size for an incoming text message.URI
getUri()
Return the URI used to open the WebSocket connection.boolean
isOpen()
Return whether the connection is still open.void
sendMessage(WebSocketMessage<?> message)
Send a WebSocket message: eitherTextMessage
orBinaryMessage
.void
setBinaryMessageSizeLimit(int messageSizeLimit)
Configure the maximum size for an incoming binary message.void
setTextMessageSizeLimit(int messageSizeLimit)
Configure the maximum size for an incoming text message.String
toString()
static WebSocketSession
unwrap(WebSocketSession session)
Constructor Detail
WebSocketSessionDecorator
public WebSocketSessionDecorator(WebSocketSession session)
Method Detail
getDelegate
public WebSocketSession getDelegate()
getLastSession
public WebSocketSession getLastSession()
unwrap
public static WebSocketSession unwrap(WebSocketSession session)
getId
public String getId()
Description copied from interface:WebSocketSession
Return a unique session identifier.- Specified by:
getId
in interfaceWebSocketSession
getUri
@Nullable public URI getUri()
Description copied from interface:WebSocketSession
Return the URI used to open the WebSocket connection.- Specified by:
getUri
in interfaceWebSocketSession
getHandshakeHeaders
public HttpHeaders getHandshakeHeaders()
Description copied from interface:WebSocketSession
Return the headers used in the handshake request (nevernull
).- Specified by:
getHandshakeHeaders
in interfaceWebSocketSession
getAttributes
public Map<String,Object> getAttributes()
Description copied from interface:WebSocketSession
Return the map with attributes associated with the WebSocket session.On the server side the map can be populated initially through a
HandshakeInterceptor
. On the client side the map can be populated viaWebSocketClient
handshake methods.- Specified by:
getAttributes
in interfaceWebSocketSession
- Returns:
- a Map with the session attributes (never
null
)
getPrincipal
public Principal getPrincipal()
Description copied from interface:WebSocketSession
Return aPrincipal
instance containing the name of the authenticated user.If the user has not been authenticated, the method returns
null
.- Specified by:
getPrincipal
in interfaceWebSocketSession
getLocalAddress
public InetSocketAddress getLocalAddress()
Description copied from interface:WebSocketSession
Return the address on which the request was received.- Specified by:
getLocalAddress
in interfaceWebSocketSession
getRemoteAddress
public InetSocketAddress getRemoteAddress()
Description copied from interface:WebSocketSession
Return the address of the remote client.- Specified by:
getRemoteAddress
in interfaceWebSocketSession
getAcceptedProtocol
public String getAcceptedProtocol()
Description copied from interface:WebSocketSession
Return the negotiated sub-protocol.- Specified by:
getAcceptedProtocol
in interfaceWebSocketSession
- Returns:
- the protocol identifier, or
null
if no protocol was specified or negotiated successfully
getExtensions
public List<WebSocketExtension> getExtensions()
Description copied from interface:WebSocketSession
Determine the negotiated extensions.- Specified by:
getExtensions
in interfaceWebSocketSession
- Returns:
- the list of extensions, or an empty list if no extension was specified or negotiated successfully
setTextMessageSizeLimit
public void setTextMessageSizeLimit(int messageSizeLimit)
Description copied from interface:WebSocketSession
Configure the maximum size for an incoming text message.- Specified by:
setTextMessageSizeLimit
in interfaceWebSocketSession
getTextMessageSizeLimit
public int getTextMessageSizeLimit()
Description copied from interface:WebSocketSession
Get the configured maximum size for an incoming text message.- Specified by:
getTextMessageSizeLimit
in interfaceWebSocketSession
setBinaryMessageSizeLimit
public void setBinaryMessageSizeLimit(int messageSizeLimit)
Description copied from interface:WebSocketSession
Configure the maximum size for an incoming binary message.- Specified by:
setBinaryMessageSizeLimit
in interfaceWebSocketSession
getBinaryMessageSizeLimit
public int getBinaryMessageSizeLimit()
Description copied from interface:WebSocketSession
Get the configured maximum size for an incoming binary message.- Specified by:
getBinaryMessageSizeLimit
in interfaceWebSocketSession
isOpen
public boolean isOpen()
Description copied from interface:WebSocketSession
Return whether the connection is still open.- Specified by:
isOpen
in interfaceWebSocketSession
sendMessage
public void sendMessage(WebSocketMessage<?> message) throws IOException
Description copied from interface:WebSocketSession
Send a WebSocket message: eitherTextMessage
orBinaryMessage
.Note: The underlying standard WebSocket session (JSR-356) does not allow concurrent sending. Therefore sending must be synchronized. To ensure that, one option is to wrap the
WebSocketSession
with theConcurrentWebSocketSessionDecorator
.- Specified by:
sendMessage
in interfaceWebSocketSession
- Throws:
IOException
- See Also:
ConcurrentWebSocketSessionDecorator
close
public void close() throws IOException
Description copied from interface:WebSocketSession
Close the WebSocket connection with status 1000, i.e. equivalent to:session.close(CloseStatus.NORMAL);
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceWebSocketSession
- Throws:
IOException
close
public void close(CloseStatus status) throws IOException
Description copied from interface:WebSocketSession
Close the WebSocket connection with the given close status.- Specified by:
close
in interfaceWebSocketSession
- Throws:
IOException