public class DefaultHandshakeHandler extends Object implements HandshakeHandler
HandshakeHandler
implementation. Performs initial validation of the
WebSocket handshake request -- possibly rejecting it through the appropriate HTTP
status code -- while also allowing sub-classes 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 Tomcat 7 and 8, Jetty 9, and
Glassfish 4.
Constructor and Description |
---|
DefaultHandshakeHandler()
Default constructor that auto-detects and instantiates a
RequestUpgradeStrategy suitable for the runtime container. |
DefaultHandshakeHandler(RequestUpgradeStrategy upgradeStrategy)
A constructor that accepts a runtime specific
RequestUpgradeStrategy . |
Modifier and Type | Method and Description |
---|---|
boolean |
doHandshake(ServerHttpRequest request,
ServerHttpResponse response,
WebSocketHandler wsHandler,
Map<String,Object> attributes)
Initiate the handshake.
|
protected List<WebSocketExtension> |
filterRequestedExtensions(ServerHttpRequest request,
List<WebSocketExtension> requested,
List<WebSocketExtension> supported)
Filter the list of requested WebSocket extensions.
|
String[] |
getSupportedProtocols()
Return the list of supported sub-protocols.
|
protected String[] |
getSupportedVerions() |
protected void |
handleInvalidConnectHeader(ServerHttpRequest request,
ServerHttpResponse response) |
protected void |
handleInvalidUpgradeHeader(ServerHttpRequest request,
ServerHttpResponse response) |
protected void |
handleWebSocketVersionNotSupported(ServerHttpRequest request,
ServerHttpResponse response) |
protected boolean |
isValidOrigin(ServerHttpRequest request) |
protected boolean |
isWebSocketVersionSupported(WebSocketHttpHeaders httpHeaders) |
protected String |
selectProtocol(List<String> requestedProtocols) |
void |
setSupportedProtocols(String... protocols)
Use this property to configure a list of sub-protocols that are supported.
|
protected Log logger
public DefaultHandshakeHandler()
RequestUpgradeStrategy
suitable for the runtime container.IllegalStateException
- if no RequestUpgradeStrategy
can be found.public DefaultHandshakeHandler(RequestUpgradeStrategy upgradeStrategy)
RequestUpgradeStrategy
.upgradeStrategy
- the upgrade strategypublic void setSupportedProtocols(String... protocols)
public String[] getSupportedProtocols()
public final boolean doHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String,Object> attributes) throws HandshakeFailureException
HandshakeHandler
doHandshake
in interface HandshakeHandler
request
- the current requestresponse
- the current responsewsHandler
- the handler to process WebSocket messages; see
PerConnectionWebSocketHandler
for providing a handler with
per-connection lifecycle.attributes
- handshake request specific attributes to be set on the WebSocket
session via HandshakeInterceptor
and thus made available to the
WebSocketHandler
;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.protected void handleInvalidUpgradeHeader(ServerHttpRequest request, ServerHttpResponse response) throws IOException
IOException
protected void handleInvalidConnectHeader(ServerHttpRequest request, ServerHttpResponse response) throws IOException
IOException
protected boolean isWebSocketVersionSupported(WebSocketHttpHeaders httpHeaders)
protected String[] getSupportedVerions()
protected void handleWebSocketVersionNotSupported(ServerHttpRequest request, ServerHttpResponse response)
protected boolean isValidOrigin(ServerHttpRequest request)
protected List<WebSocketExtension> filterRequestedExtensions(ServerHttpRequest request, List<WebSocketExtension> requested, List<WebSocketExtension> supported)
By default all request extensions are returned. The WebSocket server will further compare the requested extensions against the list of supported extensions and return only the ones that are both requested and supported.
request
- the current requestrequested
- the list of extensions requested by the clientsupported
- the list of extensions supported by the server