Class WsSession
java.lang.Object
org.apache.tomcat.websocket.WsSession
- All Implemented Interfaces:
Session, Closeable, AutoCloseable
-
Constructor Summary
ConstructorsConstructorDescriptionWsSession(ClientEndpointHolder clientEndpointHolder, WsRemoteEndpointImplBase wsRemoteEndpoint, WsWebSocketContainer wsWebSocketContainer, List<Extension> negotiatedExtensions, String subProtocol, Map<String, String> pathParameters, boolean secure, ClientEndpointConfig clientEndpointConfig) Creates a new WebSocket session for communication between the provided client and remote end points.WsSession(WsRemoteEndpointImplBase wsRemoteEndpoint, WsWebSocketContainer wsWebSocketContainer, URI requestUri, Map<String, List<String>> requestParameterMap, String queryString, Principal userPrincipal, String httpSessionId, List<Extension> negotiatedExtensions, String subProtocol, Map<String, String> pathParameters, boolean secure, ServerEndpointConfig serverEndpointConfig) Creates a new WebSocket session for communication between the provided server and remote end points. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddMessageHandler(MessageHandler listener) Registers aMessageHandlerfor incoming messages.<T> voidaddMessageHandler(Class<T> clazz, MessageHandler.Partial<T> handler) Registers aMessageHandlerfor partial incoming messages.<T> voidaddMessageHandler(Class<T> clazz, MessageHandler.Whole<T> handler) Registers aMessageHandlerfor whole incoming messages.protected static voidappendCloseReasonWithTruncation(ByteBuffer msg, String reason) Use protected so unit tests can access this method directly.protected voidChecks if the session close timeout has expired and closes the connection if so.protected voidChecks if this session has expired based on idle timeout settings.voidclose()Close the connection to the remote end point using the codeCloseReason.CloseCodes.NORMAL_CLOSUREand an empty reason phrase.voidclose(CloseReason closeReason) Close the connection to the remote end point using the specified code and reason phrase.voiddoClose(CloseReason closeReasonMessage, CloseReason closeReasonLocal) WebSocket 1.0.voiddoClose(CloseReason closeReasonMessage, CloseReason closeReasonLocal, boolean closeSocket) WebSocket 1.0.Returns the asynchronous remote endpoint for this session, used for non-blocking message sending.Returns the synchronous remote endpoint for this session, used for blocking message sending.protected MessageHandlerReturns the binary message handler for this session.Get the container that created this session.Returns the HTTP session ID associated with this WebSocket session.getId()Provides a unique identifier for the session.Returns the instance manager for this session.getLocal()Returns the local endpoint for this session.intGet the current maximum buffer size for binary messages.longGet the idle timeout for this session.intGet the maximum buffer size for text messages.Returns the set of message handlers currently registered on this session.Returns the list of extensions negotiated for this session.Returns the sub-protocol negotiated for this session, ornullif no sub-protocol was negotiated.Obtain the set of open sessions associated with the same local endpoint as this session.Returns the path parameters extracted from the request URI using the endpoint's URI template.protected MessageHandler.Whole<PongMessage> Returns the pong message handler for this session.Returns the version of the WebSocket protocol in use for this session.Returns the query string from the request that initiated this WebSocket session.Returns the query parameters from the request that initiated this WebSocket session.Returns the URI of the request that initiated this WebSocket session.protected longReturns the session close timeout in milliseconds.protected MessageHandlerReturns the text message handler for this session.Returns the principal of the user associated with this session, ornullif the user is not authenticated.Returns the user principal for this session.Returns a map of user properties associated with this session.booleanisClosed()Checks if the session is closed.booleanisOpen()Returns whether this session is currently open.booleanisSecure()Returns whether this session is using a secure connection (wss://).voidonClose(CloseReason closeReason) Called when a close message is received.voidremoveMessageHandler(MessageHandler listener) Removes the specified message handler from this session.voidresume()Resumes the reading of the incoming messages.voidsetMaxBinaryMessageBufferSize(int max) Set the current maximum buffer size for binary messages.voidsetMaxIdleTimeout(long timeout) Set the idle timeout for this session.voidsetMaxTextMessageBufferSize(int max) Set the maximum buffer size for text messages.voidsuspend()Suspends the reading of the incoming messages.protected voidUpdates the last active read timestamp to the current time.protected voidUpdates the last active write timestamp to the current time.
-
Constructor Details
-
WsSession
public WsSession(ClientEndpointHolder clientEndpointHolder, WsRemoteEndpointImplBase wsRemoteEndpoint, WsWebSocketContainer wsWebSocketContainer, List<Extension> negotiatedExtensions, String subProtocol, Map<String, String> pathParameters, boolean secure, ClientEndpointConfig clientEndpointConfig) throws DeploymentExceptionCreates a new WebSocket session for communication between the provided client and remote end points. The result ofThread.getContextClassLoader()at the time this constructor is called will be used when callingEndpoint.onClose(Session, CloseReason).- Parameters:
clientEndpointHolder- The end point managed by this codewsRemoteEndpoint- The other / remote end pointwsWebSocketContainer- The container that created this sessionnegotiatedExtensions- The agreed extensions to use for this sessionsubProtocol- The agreed sub-protocol to use for this sessionpathParameters- The path parameters associated with the request that initiated this session ornullif this is a client sessionsecure- Was this session initiated over a secure connection?clientEndpointConfig- The configuration information for the client end point- Throws:
DeploymentException- if an invalid encode is specified
-
WsSession
public WsSession(WsRemoteEndpointImplBase wsRemoteEndpoint, WsWebSocketContainer wsWebSocketContainer, URI requestUri, Map<String, List<String>> requestParameterMap, String queryString, Principal userPrincipal, String httpSessionId, List<Extension> negotiatedExtensions, String subProtocol, Map<String, String> pathParameters, boolean secure, ServerEndpointConfig serverEndpointConfig) throws DeploymentExceptionCreates a new WebSocket session for communication between the provided server and remote end points. The result ofThread.getContextClassLoader()at the time this constructor is called will be used when callingEndpoint.onClose(Session, CloseReason).- Parameters:
wsRemoteEndpoint- The other / remote end pointwsWebSocketContainer- The container that created this sessionrequestUri- The URI used to connect to this end point ornullif this is a client sessionrequestParameterMap- The parameters associated with the request that initiated this session ornullif this is a client sessionqueryString- The query string associated with the request that initiated this session ornullif this is a client sessionuserPrincipal- The principal associated with the request that initiated this session ornullif this is a client sessionhttpSessionId- The HTTP session ID associated with the request that initiated this session ornullif this is a client sessionnegotiatedExtensions- The agreed extensions to use for this sessionsubProtocol- The agreed sub-protocol to use for this sessionpathParameters- The path parameters associated with the request that initiated this session ornullif this is a client sessionsecure- Was this session initiated over a secure connection?serverEndpointConfig- The configuration information for the server end point- Throws:
DeploymentException- if an invalid encode is specified
-
-
Method Details
-
getInstanceManager
Returns the instance manager for this session.- Returns:
- the instance manager
-
getContainer
Description copied from interface:jakarta.websocket.SessionGet the container that created this session.- Specified by:
getContainerin interfaceSession- Returns:
- the container that created this session.
-
addMessageHandler
Description copied from interface:jakarta.websocket.SessionRegisters aMessageHandlerfor incoming messages. Only oneMessageHandlermay be registered for each message type (text, binary, pong). The message type will be derived at runtime from the providedMessageHandlerinstance. It is not always possible to do this so it is better to useSession.addMessageHandler(Class, jakarta.websocket.MessageHandler.Partial)orSession.addMessageHandler(Class, jakarta.websocket.MessageHandler.Whole).- Specified by:
addMessageHandlerin interfaceSession- Parameters:
listener- The message handler for a incoming message
-
addMessageHandler
public <T> void addMessageHandler(Class<T> clazz, MessageHandler.Partial<T> handler) throws IllegalStateException Description copied from interface:jakarta.websocket.SessionRegisters aMessageHandlerfor partial incoming messages. Only oneMessageHandlermay be registered for each message type (text or binary, pong messages are never presented as partial messages).- Specified by:
addMessageHandlerin interfaceSession- Type Parameters:
T- The type of message that the given handler is intended for- Parameters:
clazz- The Class that implements Thandler- The message handler for a incoming message- Throws:
IllegalStateException- If a message handler has already been registered for the associated message type
-
addMessageHandler
public <T> void addMessageHandler(Class<T> clazz, MessageHandler.Whole<T> handler) throws IllegalStateException Description copied from interface:jakarta.websocket.SessionRegisters aMessageHandlerfor whole incoming messages. Only oneMessageHandlermay be registered for each message type (text, binary, pong).- Specified by:
addMessageHandlerin interfaceSession- Type Parameters:
T- The type of message that the given handler is intended for- Parameters:
clazz- The Class that implements Thandler- The message handler for a incoming message- Throws:
IllegalStateException- If a message handler has already been registered for the associated message type
-
getMessageHandlers
Description copied from interface:jakarta.websocket.SessionReturns the set of message handlers currently registered on this session.- Specified by:
getMessageHandlersin interfaceSession- Returns:
- The set of registered message handlers
-
removeMessageHandler
Description copied from interface:jakarta.websocket.SessionRemoves the specified message handler from this session.- Specified by:
removeMessageHandlerin interfaceSession- Parameters:
listener- The message handler to remove
-
getProtocolVersion
Description copied from interface:jakarta.websocket.SessionReturns the version of the WebSocket protocol in use for this session.- Specified by:
getProtocolVersionin interfaceSession- Returns:
- The protocol version
-
getNegotiatedSubprotocol
Description copied from interface:jakarta.websocket.SessionReturns the sub-protocol negotiated for this session, ornullif no sub-protocol was negotiated.- Specified by:
getNegotiatedSubprotocolin interfaceSession- Returns:
- The negotiated sub-protocol
-
getNegotiatedExtensions
Description copied from interface:jakarta.websocket.SessionReturns the list of extensions negotiated for this session.- Specified by:
getNegotiatedExtensionsin interfaceSession- Returns:
- The list of negotiated extensions
-
isSecure
public boolean isSecure()Description copied from interface:jakarta.websocket.SessionReturns whether this session is using a secure connection (wss://). -
isOpen
public boolean isOpen()Description copied from interface:jakarta.websocket.SessionReturns whether this session is currently open. -
isClosed
public boolean isClosed()Checks if the session is closed.- Returns:
- true if the session is closed
-
getMaxIdleTimeout
public long getMaxIdleTimeout()Description copied from interface:jakarta.websocket.SessionGet the idle timeout for this session.- Specified by:
getMaxIdleTimeoutin interfaceSession- Returns:
- The current idle timeout for this session in milliseconds. Zero or negative values indicate an infinite timeout.
-
setMaxIdleTimeout
public void setMaxIdleTimeout(long timeout) Description copied from interface:jakarta.websocket.SessionSet the idle timeout for this session.- Specified by:
setMaxIdleTimeoutin interfaceSession- Parameters:
timeout- The new idle timeout for this session in milliseconds. Zero or negative values indicate an infinite timeout.
-
setMaxBinaryMessageBufferSize
public void setMaxBinaryMessageBufferSize(int max) Description copied from interface:jakarta.websocket.SessionSet the current maximum buffer size for binary messages.- Specified by:
setMaxBinaryMessageBufferSizein interfaceSession- Parameters:
max- The new maximum buffer size in bytes
-
getMaxBinaryMessageBufferSize
public int getMaxBinaryMessageBufferSize()Description copied from interface:jakarta.websocket.SessionGet the current maximum buffer size for binary messages.- Specified by:
getMaxBinaryMessageBufferSizein interfaceSession- Returns:
- The current maximum buffer size in bytes
-
setMaxTextMessageBufferSize
public void setMaxTextMessageBufferSize(int max) Description copied from interface:jakarta.websocket.SessionSet the maximum buffer size for text messages.- Specified by:
setMaxTextMessageBufferSizein interfaceSession- Parameters:
max- The new maximum buffer size in characters.
-
getMaxTextMessageBufferSize
public int getMaxTextMessageBufferSize()Description copied from interface:jakarta.websocket.SessionGet the maximum buffer size for text messages.- Specified by:
getMaxTextMessageBufferSizein interfaceSession- Returns:
- The maximum buffer size in characters.
-
getOpenSessions
Description copied from interface:jakarta.websocket.SessionObtain the set of open sessions associated with the same local endpoint as this session.- Specified by:
getOpenSessionsin interfaceSession- Returns:
- The set of currently open sessions for the local endpoint that this session is associated with.
-
getAsyncRemote
Description copied from interface:jakarta.websocket.SessionReturns the asynchronous remote endpoint for this session, used for non-blocking message sending.- Specified by:
getAsyncRemotein interfaceSession- Returns:
- The asynchronous remote endpoint
-
getBasicRemote
Description copied from interface:jakarta.websocket.SessionReturns the synchronous remote endpoint for this session, used for blocking message sending.- Specified by:
getBasicRemotein interfaceSession- Returns:
- The synchronous remote endpoint
-
close
Description copied from interface:jakarta.websocket.SessionClose the connection to the remote end point using the codeCloseReason.CloseCodes.NORMAL_CLOSUREand an empty reason phrase.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceSession- Throws:
IOException- if an I/O error occurs while the WebSocket session is being closed.
-
close
Description copied from interface:jakarta.websocket.SessionClose the connection to the remote end point using the specified code and reason phrase.- Specified by:
closein interfaceSession- Parameters:
closeReason- The reason the WebSocket session is being closed.- Throws:
IOException- if an I/O error occurs while the WebSocket session is being closed.
-
doClose
WebSocket 1.0. Section 2.1.5. Need internal close method as spec requires that the local endpoint receives a 1006 on timeout.- Parameters:
closeReasonMessage- The close reason to pass to the remote endpointcloseReasonLocal- The close reason to pass to the local endpoint
-
doClose
public void doClose(CloseReason closeReasonMessage, CloseReason closeReasonLocal, boolean closeSocket) WebSocket 1.0. Section 2.1.5. Need internal close method as spec requires that the local endpoint receives a 1006 on timeout.- Parameters:
closeReasonMessage- The close reason to pass to the remote endpointcloseReasonLocal- The close reason to pass to the local endpointcloseSocket- Should the socket be closed immediately rather than waiting for the server to respond
-
onClose
Called when a close message is received. Should only ever happen once. Also called after a protocol error when the ProtocolHandler needs to force the closing of the connection.- Parameters:
closeReason- The reason contained within the received close message.
-
getSessionCloseTimeout
protected long getSessionCloseTimeout()Returns the session close timeout in milliseconds.- Returns:
- the session close timeout
-
checkCloseTimeout
protected void checkCloseTimeout()Checks if the session close timeout has expired and closes the connection if so. -
appendCloseReasonWithTruncation
Use protected so unit tests can access this method directly.- Parameters:
msg- The messagereason- The reason
-
getRequestURI
Description copied from interface:jakarta.websocket.SessionReturns the URI of the request that initiated this WebSocket session.- Specified by:
getRequestURIin interfaceSession- Returns:
- The request URI
-
getRequestParameterMap
Description copied from interface:jakarta.websocket.SessionReturns the query parameters from the request that initiated this WebSocket session.- Specified by:
getRequestParameterMapin interfaceSession- Returns:
- The query parameter map
-
getQueryString
Description copied from interface:jakarta.websocket.SessionReturns the query string from the request that initiated this WebSocket session.- Specified by:
getQueryStringin interfaceSession- Returns:
- The query string
-
getUserPrincipal
Description copied from interface:jakarta.websocket.SessionReturns the principal of the user associated with this session, ornullif the user is not authenticated.- Specified by:
getUserPrincipalin interfaceSession- Returns:
- The user principal
-
getUserPrincipalInternal
Returns the user principal for this session.- Returns:
- the user principal
-
getPathParameters
Description copied from interface:jakarta.websocket.SessionReturns the path parameters extracted from the request URI using the endpoint's URI template.- Specified by:
getPathParametersin interfaceSession- Returns:
- The path parameter map
-
getId
Description copied from interface:jakarta.websocket.SessionProvides a unique identifier for the session. This identifier should not be relied upon to be generated from a secure random source. -
getUserProperties
Description copied from interface:jakarta.websocket.SessionReturns a map of user properties associated with this session.- Specified by:
getUserPropertiesin interfaceSession- Returns:
- The user properties map
-
getLocal
-
getHttpSessionId
Returns the HTTP session ID associated with this WebSocket session.- Returns:
- the HTTP session ID, or null if not associated
-
getTextMessageHandler
Returns the text message handler for this session.- Returns:
- the text message handler
-
getBinaryMessageHandler
Returns the binary message handler for this session.- Returns:
- the binary message handler
-
getPongMessageHandler
Returns the pong message handler for this session.- Returns:
- the pong message handler
-
updateLastActiveRead
protected void updateLastActiveRead()Updates the last active read timestamp to the current time. -
updateLastActiveWrite
protected void updateLastActiveWrite()Updates the last active write timestamp to the current time. -
checkExpiration
protected void checkExpiration()Checks if this session has expired based on idle timeout settings. -
suspend
public void suspend()Suspends the reading of the incoming messages. -
resume
public void resume()Resumes the reading of the incoming messages.
-