Top Description Fields Constructors Methods
org.springframework.web.socket.sockjs.transport.handler

public Class WebSocketTransportHandler

extends AbstractTransportHandler
implements SockJsSessionFactory, HandshakeHandler, Lifecycle, ServletContextAware
Class Inheritance
All Implemented Interfaces
org.springframework.web.context.ServletContextAware, org.springframework.beans.factory.Aware, org.springframework.context.Lifecycle, org.springframework.web.socket.server.HandshakeHandler, org.springframework.web.socket.sockjs.transport.SockJsSessionFactory
Imports
java.util.Map, jakarta.servlet.ServletContext, org.springframework.context.Lifecycle, org.springframework.http.server.ServerHttpRequest, .ServerHttpResponse, org.springframework.util.Assert, org.springframework.web.context.ServletContextAware, org.springframework.web.socket.CloseStatus, .WebSocketHandler, org.springframework.web.socket.server.HandshakeFailureException, .HandshakeHandler, org.springframework.web.socket.sockjs.SockJsException, .SockJsTransportFailureException, org.springframework.web.socket.sockjs.transport.SockJsSession, .SockJsSessionFactory, .TransportHandler, .TransportType, org.springframework.web.socket.sockjs.transport.session.AbstractSockJsSession, .WebSocketServerSockJsSession

WebSocket-based TransportHandler. Uses SockJsWebSocketHandler and WebSocketServerSockJsSession to add SockJS processing.

Also implements HandshakeHandler to support raw WebSocket communication at SockJS URL "/websocket".

Author
Rossen Stoyanchev
Since
4.0

Field Summary

Modifier and TypeField and Description
private final HandshakeHandler
private volatile boolean
Inherited from org.springframework.web.socket.sockjs.transport.handler.AbstractTransportHandler:
logger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
checkSessionType(SockJsSession session)

Implements org.springframework.web.socket.sockjs.transport.TransportHandler.checkSessionType.

Check whether the type of the given session matches the transport type of this TransportHandler where session id and the transport type are extracted from the SockJS URL.

public AbstractSockJsSession
createSession(String
the ID of the session
id
,
WebSocketHandler
the underlying WebSocketHandler
handler
,
Map<String, Object>
handshake request specific attributes
attrs
)

Implements org.springframework.web.socket.sockjs.transport.SockJsSessionFactory.createSession.

Create a new SockJS session.

public boolean
doHandshake(ServerHttpRequest
the current request
request
,
ServerHttpResponse
the current response
response
,
WebSocketHandler
the handler to process WebSocket messages; see PerConnectionWebSocketHandler for providing a handler with per-connection lifecycle.
handler
,
Map<String, Object>
the attributes from the HTTP handshake to associate with the WebSocket session; the provided attributes are copied, the original map is not used.
attributes
)

Implements org.springframework.web.socket.server.HandshakeHandler.doHandshake.

Initiate the handshake.

public HandshakeHandler
public TransportType
getTransportType()

Implements org.springframework.web.socket.sockjs.transport.TransportHandler.getTransportType.

Return the transport type supported by this handler.

public void
handleRequest(ServerHttpRequest
the current request
request
,
ServerHttpResponse
the current response
response
,
WebSocketHandler
the target WebSocketHandler (never null)
wsHandler
,
SockJsSession
the SockJS session (never null)
wsSession
)

Implements org.springframework.web.socket.sockjs.transport.TransportHandler.handleRequest.

Handle the given request and delegate messages to the provided WebSocketHandler.

public boolean
isRunning()

Implements org.springframework.context.Lifecycle.isRunning.

Check whether this component is currently running.

public void
setServletContext(ServletContext
the ServletContext object to be used by this object
servletContext
)

Implements org.springframework.web.context.ServletContextAware.setServletContext.

Set the ServletContext that this object runs in.

Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method.

public void
start()

Implements org.springframework.context.Lifecycle.start.

Start this component.

public void
stop()

Implements org.springframework.context.Lifecycle.stop.

Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method.

Inherited from org.springframework.web.socket.sockjs.transport.handler.AbstractTransportHandler:
getServiceConfiginitialize

Field Detail

handshakeHandlerback to summary
private final HandshakeHandler handshakeHandler
runningback to summary
private volatile boolean running

Constructor Detail

WebSocketTransportHandlerback to summary
public WebSocketTransportHandler(HandshakeHandler handshakeHandler)

Method Detail

checkSessionTypeback to summary
public boolean checkSessionType(SockJsSession session)

Implements org.springframework.web.socket.sockjs.transport.TransportHandler.checkSessionType.

Doc from org.springframework.web.socket.sockjs.transport.TransportHandler.checkSessionType.

Check whether the type of the given session matches the transport type of this TransportHandler where session id and the transport type are extracted from the SockJS URL.

Returns:boolean

true if the session matches (and would therefore get accepted by handleRequest), or false otherwise

Annotations
@Override
createSessionback to summary
public AbstractSockJsSession createSession(String id, WebSocketHandler handler, Map<String, Object> attrs)

Implements org.springframework.web.socket.sockjs.transport.SockJsSessionFactory.createSession.

Doc from org.springframework.web.socket.sockjs.transport.SockJsSessionFactory.createSession.

Create a new SockJS session.

Parameters
id:String

the ID of the session

handler:WebSocketHandler

the underlying WebSocketHandler

attrs:Map<String, Object>

handshake request specific attributes

Returns:AbstractSockJsSession

a new session, never null

Annotations
@Override
doHandshakeback to summary
public boolean doHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler handler, Map<String, Object> attributes) throws HandshakeFailureException

Implements org.springframework.web.socket.server.HandshakeHandler.doHandshake.

Doc from org.springframework.web.socket.server.HandshakeHandler.doHandshake.

Initiate the handshake.

Parameters
request:ServerHttpRequest

the current request

response:ServerHttpResponse

the current response

handler:WebSocketHandler

the handler to process WebSocket messages; see PerConnectionWebSocketHandler for providing a handler with per-connection lifecycle.

attributes:Map<String, Object>

the attributes from the HTTP handshake to associate with the WebSocket session; the provided attributes are copied, the original map is not used.

Returns:boolean

whether the handshake negotiation was successful or not. In either case the response status, headers, and body will have been updated to reflect the result of the negotiation

Annotations
@Override
Exceptions
HandshakeFailureException:
thrown when handshake processing failed to complete due to an internal, unrecoverable error, i.e. a server error as opposed to a failure to successfully negotiate the handshake.
getHandshakeHandlerback to summary
public HandshakeHandler getHandshakeHandler()
getTransportTypeback to summary
public TransportType getTransportType()

Implements org.springframework.web.socket.sockjs.transport.TransportHandler.getTransportType.

Doc from org.springframework.web.socket.sockjs.transport.TransportHandler.getTransportType.

Return the transport type supported by this handler.

Annotations
@Override
handleRequestback to summary
public void handleRequest(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException

Implements org.springframework.web.socket.sockjs.transport.TransportHandler.handleRequest.

Doc from org.springframework.web.socket.sockjs.transport.TransportHandler.handleRequest.

Handle the given request and delegate messages to the provided WebSocketHandler.

Parameters
request:ServerHttpRequest

the current request

response:ServerHttpResponse

the current response

wsHandler:WebSocketHandler

the target WebSocketHandler (never null)

wsSession:SockJsSession

the SockJS session (never null)

Annotations
@Override
Exceptions
SockJsException:
raised when request processing fails as explained in SockJsService
isRunningback to summary
public boolean isRunning()

Implements org.springframework.context.Lifecycle.isRunning.

Doc from org.springframework.context.Lifecycle.isRunning.

Check whether this component is currently running.

In the case of a container, this will return true only if all components that apply are currently running.

Returns:boolean

whether the component is currently running

Annotations
@Override
setServletContextback to summary
public void setServletContext(ServletContext servletContext)

Implements org.springframework.web.context.ServletContextAware.setServletContext.

Doc from org.springframework.web.context.ServletContextAware.setServletContext.

Set the ServletContext that this object runs in.

Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method. Invoked after ApplicationContextAware's setApplicationContext.

Parameters
servletContext:ServletContext

the ServletContext object to be used by this object

Annotations
@Override
startback to summary
public void start()

Implements org.springframework.context.Lifecycle.start.

Doc from org.springframework.context.Lifecycle.start.

Start this component.

Should not throw an exception if the component is already running.

In the case of a container, this will propagate the start signal to all components that apply.

Annotations
@Override
stopback to summary
public void stop()

Implements org.springframework.context.Lifecycle.stop.

Doc from org.springframework.context.Lifecycle.stop.

Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method. Consider implementing SmartLifecycle and its stop(Runnable) variant when asynchronous stop behavior is necessary.

Note that this stop notification is not guaranteed to come before destruction: On regular shutdown, Lifecycle beans will first receive a stop notification before the general destruction callbacks are being propagated; however, on hot refresh during a context's lifetime or on aborted refresh attempts, a given bean's destroy method will be called without any consideration of stop signals upfront.

Should not throw an exception if the component is not running (not started yet).

In the case of a container, this will propagate the stop signal to all components that apply.

Annotations
@Override