Class AbstractHandshakeHandler
- All Implemented Interfaces:
- Lifecycle,- HandshakeHandler
- Direct Known Subclasses:
- DefaultHandshakeHandler
HandshakeHandler implementations, independent of the Servlet API.
 Performs initial validation of the WebSocket handshake request - possibly rejecting it through the appropriate HTTP status code - while also allowing its subclasses to override various parts of the negotiation process (e.g. origin validation, sub-protocol negotiation, extensions negotiation, etc).
If the negotiation succeeds, the actual upgrade is delegated to a server-specific
 RequestUpgradeStrategy, which will update
 the response as necessary and initialize the WebSocket. Currently, supported servers are
 Jetty 9.0-9.3, Tomcat 7.0.47+ and 8.x, Undertow 1.0-1.3, GlassFish 4.1+, WebLogic 12.1.3+.
- Since:
- 4.2
- Author:
- Rossen Stoyanchev, Juergen Hoeller
- See Also:
- 
Field SummaryFields
- 
Constructor SummaryConstructorsModifierConstructorDescriptionprotectedDefault constructor that auto-detects and instantiates aRequestUpgradeStrategysuitable for the runtime container.protectedAbstractHandshakeHandler(RequestUpgradeStrategy requestUpgradeStrategy) A constructor that accepts a runtime-specificRequestUpgradeStrategy.
- 
Method SummaryModifier and TypeMethodDescriptionDetermine the sub-protocols supported by the given WebSocketHandler by checking whether it is an instance ofSubProtocolCapable.protected PrincipaldetermineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) A method that can be used to associate a user with the WebSocket session in the process of being established.final booleandoHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) Initiate the handshake.protected voiddoStart()protected voiddoStop()protected List<WebSocketExtension>filterRequestedExtensions(ServerHttpRequest request, List<WebSocketExtension> requestedExtensions, List<WebSocketExtension> supportedExtensions) Filter the list of requested WebSocket extensions.Return theRequestUpgradeStrategyfor WebSocket requests.String[]Return the list of supported sub-protocols.protected String[]protected voidhandleInvalidConnectHeader(ServerHttpRequest request, ServerHttpResponse response) protected voidhandleInvalidUpgradeHeader(ServerHttpRequest request, ServerHttpResponse response) protected voidhandleWebSocketVersionNotSupported(ServerHttpRequest request, ServerHttpResponse response) booleanCheck whether this component is currently running.protected booleanisValidOrigin(ServerHttpRequest request) Return whether the requestOriginheader value is valid or not.protected booleanisWebSocketVersionSupported(WebSocketHttpHeaders httpHeaders) protected StringselectProtocol(List<String> requestedProtocols, WebSocketHandler webSocketHandler) Perform the sub-protocol negotiation based on requested and supported sub-protocols.voidsetSupportedProtocols(String... protocols) Use this property to configure the list of supported sub-protocols.voidstart()Start this component.voidstop()Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method.
- 
Field Details- 
logger
 
- 
- 
Constructor Details- 
AbstractHandshakeHandlerprotected AbstractHandshakeHandler()Default constructor that auto-detects and instantiates aRequestUpgradeStrategysuitable for the runtime container.- Throws:
- IllegalStateException- if no- RequestUpgradeStrategycan be found.
 
- 
AbstractHandshakeHandlerA constructor that accepts a runtime-specificRequestUpgradeStrategy.- Parameters:
- requestUpgradeStrategy- the upgrade strategy to use
 
 
- 
- 
Method Details- 
getRequestUpgradeStrategyReturn theRequestUpgradeStrategyfor WebSocket requests.
- 
setSupportedProtocolsUse this property to configure the list of supported sub-protocols. The first configured sub-protocol that matches a client-requested sub-protocol is accepted. If there are no matches the response will not contain a Sec-WebSocket-Protocol header.Note that if the WebSocketHandler passed in at runtime is an instance of SubProtocolCapablethen there is no need to explicitly configure this property. That is certainly the case with the built-in STOMP over WebSocket support. Therefore, this property should be configured explicitly only if the WebSocketHandler does not implementSubProtocolCapable.
- 
getSupportedProtocolsReturn the list of supported sub-protocols.
- 
startpublic void start()Description copied from interface:LifecycleStart 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. 
- 
doStartprotected void doStart()
- 
stoppublic void stop()Description copied from interface:LifecycleStop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method. Consider implementingSmartLifecycleand itsstop(Runnable)variant when asynchronous stop behavior is necessary.Note that this stop notification is not guaranteed to come before destruction: On regular shutdown, Lifecyclebeans 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. 
- 
doStopprotected void doStop()
- 
isRunningpublic boolean isRunning()Description copied from interface:LifecycleCheck whether this component is currently running.In the case of a container, this will return trueonly if all components that apply are currently running.
- 
doHandshakepublic final boolean doHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws HandshakeFailureExceptionDescription copied from interface:HandshakeHandlerInitiate the handshake.- Specified by:
- doHandshakein interface- HandshakeHandler
- Parameters:
- request- the current request
- response- the current response
- wsHandler- the handler to process WebSocket messages; see- PerConnectionWebSocketHandlerfor providing a handler with per-connection lifecycle.
- attributes- the attributes from the HTTP handshake to associate with the WebSocket session; the provided attributes are copied, the original map is not used.
- Returns:
- 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
- Throws:
- 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.
 
- 
handleInvalidUpgradeHeaderprotected void handleInvalidUpgradeHeader(ServerHttpRequest request, ServerHttpResponse response) throws IOException - Throws:
- IOException
 
- 
handleInvalidConnectHeaderprotected void handleInvalidConnectHeader(ServerHttpRequest request, ServerHttpResponse response) throws IOException - Throws:
- IOException
 
- 
isWebSocketVersionSupported
- 
getSupportedVersions
- 
handleWebSocketVersionNotSupportedprotected void handleWebSocketVersionNotSupported(ServerHttpRequest request, ServerHttpResponse response) 
- 
isValidOriginReturn whether the requestOriginheader value is valid or not. By default, all origins as considered as valid. Consider using anOriginHandshakeInterceptorfor filtering origins if needed.
- 
selectProtocol@Nullable protected String selectProtocol(List<String> requestedProtocols, WebSocketHandler webSocketHandler) Perform the sub-protocol negotiation based on requested and supported sub-protocols. For the list of supported sub-protocols, this method first checks if the target WebSocketHandler is aSubProtocolCapableand then also checks if any sub-protocols have been explicitly configured withsetSupportedProtocols(String...).- Parameters:
- requestedProtocols- the requested sub-protocols
- webSocketHandler- the WebSocketHandler that will be used
- Returns:
- the selected protocols or null
- See Also:
 
- 
determineHandlerSupportedProtocolsDetermine the sub-protocols supported by the given WebSocketHandler by checking whether it is an instance ofSubProtocolCapable.- Parameters:
- handler- the handler to check
- Returns:
- a list of supported protocols, or an empty list if none available
 
- 
filterRequestedExtensionsprotected List<WebSocketExtension> filterRequestedExtensions(ServerHttpRequest request, List<WebSocketExtension> requestedExtensions, List<WebSocketExtension> supportedExtensions) Filter the list of requested WebSocket extensions.As of 4.1, the default implementation of this method filters the list to leave only extensions that are both requested and supported. - Parameters:
- request- the current request
- requestedExtensions- the list of extensions requested by the client
- supportedExtensions- the list of extensions supported by the server
- Returns:
- the selected extensions or an empty list
 
- 
determineUser@Nullable protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) A method that can be used to associate a user with the WebSocket session in the process of being established. The default implementation callsServerHttpRequest.getPrincipal()Subclasses can provide custom logic for associating a user with a session, for example for assigning a name to anonymous users (i.e. not fully authenticated). - Parameters:
- request- the handshake request
- wsHandler- the WebSocket handler that will handle messages
- attributes- handshake attributes to pass to the WebSocket session
- Returns:
- the user for the WebSocket session, or nullif not available
 
 
-