org.springframework.web.socket.server.support

## Class HttpSessionHandshakeInterceptor

- java.lang.Object
- - org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor

All Implemented Interfaces:
  HandshakeInterceptor

    public class HttpSessionHandshakeInterceptor
    extends Object
    implements HandshakeInterceptor

An interceptor to copy information from the HTTP session to the "handshake attributes" map to made available via`WebSocketSession.getAttributes()`.

Copies a subset or all HTTP session attributes and/or the HTTP session id under the key `HTTP_SESSION_ID_ATTR_NAME`.

Since:
  4.0
Author:
  Rossen Stoyanchev

### Field Summary

Fields
`static String` `HTTP_SESSION_ID_ATTR_NAME` The name of the attribute under which the HTTP session id is exposed when `copyHttpSessionId` is "true".

### Constructor Summary

Constructors
Constructor and Description
`HttpSessionHandshakeInterceptor()` Default constructor for copying all HTTP session attributes and the HTTP session id.
`HttpSessionHandshakeInterceptor(Collection<String> attributeNames)` Constructor for copying specific HTTP session attributes and the HTTP session id.

### Method Summary

All Methods Instance Methods Concrete Methods
`void` `afterHandshake(org.springframework.http.server.ServerHttpRequest request, org.springframework.http.server.ServerHttpResponse response, WebSocketHandler wsHandler, Exception ex)` Invoked after the handshake is done.
`boolean` `beforeHandshake(org.springframework.http.server.ServerHttpRequest request, org.springframework.http.server.ServerHttpResponse response, WebSocketHandler wsHandler, Map<String,Object> attributes)` Invoked before the handshake is processed.
`Collection<String>` `getAttributeNames()` Return the configured attribute names to copy (read-only).
`boolean` `isCopyAllAttributes()` Whether to copy all HTTP session attributes.
`boolean` `isCopyHttpSessionId()` Whether to copy the HTTP session id to the handshake attributes.
`boolean` `isCreateSession()` Whether the HTTP session is allowed to be created.
`void` `setCopyAllAttributes(boolean copyAllAttributes)` Whether to copy all attributes from the HTTP session.
`void` `setCopyHttpSessionId(boolean copyHttpSessionId)` Whether the HTTP session id should be copied to the handshake attributes under the key `HTTP_SESSION_ID_ATTR_NAME`.
`void` `setCreateSession(boolean createSession)` Whether to allow the HTTP session to be created while accessing it.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### HTTP_SESSION_ID_ATTR_NAME

    public static final String HTTP_SESSION_ID_ATTR_NAME

The name of the attribute under which the HTTP session id is exposed when `copyHttpSessionId` is "true".

See Also:
  Constant Field Values

### Constructor Detail

#### HttpSessionHandshakeInterceptor

    public HttpSessionHandshakeInterceptor()

Default constructor for copying all HTTP session attributes and the HTTP session id.

See Also:
  `setCopyAllAttributes(boolean)`, `setCopyHttpSessionId(boolean)`

#### HttpSessionHandshakeInterceptor

    public HttpSessionHandshakeInterceptor(Collection<String> attributeNames)

Constructor for copying specific HTTP session attributes and the HTTP session id.

Parameters:
  `attributeNames` - session attributes to copy
See Also:
  `setCopyAllAttributes(boolean)`, `setCopyHttpSessionId(boolean)`

### Method Detail

#### getAttributeNames

    public Collection<String> getAttributeNames()

Return the configured attribute names to copy (read-only).

#### setCopyAllAttributes

    public void setCopyAllAttributes(boolean copyAllAttributes)

Whether to copy all attributes from the HTTP session. If set to "true", any explicitly configured attribute names are ignored.

By default this is set to either "true" or "false" depending on which constructor was used (default or with attribute names respectively).

Parameters:
  `copyAllAttributes` - whether to copy all attributes

#### isCopyAllAttributes

    public boolean isCopyAllAttributes()

Whether to copy all HTTP session attributes.

#### setCopyHttpSessionId

    public void setCopyHttpSessionId(boolean copyHttpSessionId)

Whether the HTTP session id should be copied to the handshake attributes under the key `HTTP_SESSION_ID_ATTR_NAME`.

By default this is "true".

Parameters:
  `copyHttpSessionId` - whether to copy the HTTP session id.

#### isCopyHttpSessionId

    public boolean isCopyHttpSessionId()

Whether to copy the HTTP session id to the handshake attributes.

#### setCreateSession

    public void setCreateSession(boolean createSession)

Whether to allow the HTTP session to be created while accessing it.

By default set to `false`.

See Also:
  `HttpServletRequest.getSession(boolean)`

#### isCreateSession

    public boolean isCreateSession()

Whether the HTTP session is allowed to be created.

#### beforeHandshake

    public boolean beforeHandshake(org.springframework.http.server.ServerHttpRequest request,
                                   org.springframework.http.server.ServerHttpResponse response,
                                   WebSocketHandler wsHandler,
                                   Map<String,Object> attributes)
                            throws Exception

Description copied from interface: `HandshakeInterceptor`
Invoked before the handshake is processed.

Specified by:
  `beforeHandshake` in interface `HandshakeInterceptor`
Parameters:
  `request` - the current request
  `response` - the current response
  `wsHandler` - the target WebSocket handler
  `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 to proceed with the handshake (`true`) or abort (`false`)
Throws:
  `Exception`

#### afterHandshake

    public void afterHandshake(org.springframework.http.server.ServerHttpRequest request,
                               org.springframework.http.server.ServerHttpResponse response,
                               WebSocketHandler wsHandler,
                               @Nullable
                               Exception ex)

Description copied from interface: `HandshakeInterceptor`
Invoked after the handshake is done. The response status and headers indicate the results of the handshake, i.e. whether it was successful or not.

Specified by:
  `afterHandshake` in interface `HandshakeInterceptor`
Parameters:
  `request` - the current request
  `response` - the current response
  `wsHandler` - the target WebSocket handler
  `ex` - an exception raised during the handshake, or `null` if none