org.springframework.web.socket.sockjs.transport.session

## Class AbstractHttpSockJsSession

- java.lang.Object
- - org.springframework.web.socket.sockjs.transport.session.AbstractSockJsSession
  - - org.springframework.web.socket.sockjs.transport.session.AbstractHttpSockJsSession

- All Implemented Interfaces:
    Closeable, AutoCloseable, SockJsSession, WebSocketSession
  Direct Known Subclasses:
    PollingSockJsSession, StreamingSockJsSession

      public abstract class AbstractHttpSockJsSession
      extends AbstractSockJsSession

  An abstract base class for use with HTTP transport SockJS sessions.
  Since:
    4.0
  Author:
    Rossen Stoyanchev

- - ### Field Summary

    - ### Fields inherited from class org.springframework.web.socket.sockjs.transport.session.AbstractSockJsSession

      `DISCONNECTED_CLIENT_LOG_CATEGORY, disconnectedClientLogger, logger, responseLock`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `AbstractHttpSockJsSession(String id, SockJsServiceConfig config, WebSocketHandler wsHandler, Map<String,Object> attributes)`

  - ### Method Summary

    All Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `disconnect(CloseStatus status)` Actually close the underlying WebSocket session or in the case of HTTP transports complete the underlying request.
    `protected abstract void` | `flushCache()` Called when the connection is active and ready to write to the response.
    `String` | `getAcceptedProtocol()` Return the selected sub-protocol to use.
    `int` | `getBinaryMessageSizeLimit()` Get the configured maximum size for an incoming binary message.
    `List<WebSocketExtension>` | `getExtensions()` Determine the negotiated extensions.
    `HttpHeaders` | `getHandshakeHeaders()` Return the headers used in the handshake request (never `null`).
    `InetSocketAddress` | `getLocalAddress()` Return the address on which the request was received.
    `protected Queue<String>` | `getMessageCache()` Return the SockJS buffer for messages stored transparently between polling requests.
    `Principal` | `getPrincipal()` Return a `Principal` instance containing the name of the authenticated user.
    `InetSocketAddress` | `getRemoteAddress()` Return the address of the remote client.
    `int` | `getTextMessageSizeLimit()` Get the configured maximum size for an incoming text message.
    `URI` | `getUri()` Return the URI used to open the WebSocket connection.
    `void` | `handleInitialRequest(ServerHttpRequest request, ServerHttpResponse response, SockJsFrameFormat frameFormat)` Handle the first request for receiving messages on a SockJS HTTP transport based session.
    `protected abstract void` | `handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response, boolean initialRequest)` Invoked when a SockJS transport request is received.
    `void` | `handleSuccessiveRequest(ServerHttpRequest request, ServerHttpResponse response, SockJsFrameFormat frameFormat)` Handle all requests, except the first one, to receive messages on a SockJS HTTP transport based session.
    `boolean` | `isActive()` Polling and Streaming sessions periodically close the current HTTP request and wait for the next request to come through.
    `protected void` | `resetRequest()`
    `protected void` | `sendMessageInternal(String message)`
    `void` | `setAcceptedProtocol(String protocol)` Unlike WebSocket where sub-protocol negotiation is part of the initial handshake, in HTTP transports the same negotiation must be emulated and the selected protocol set through this setter.
    `void` | `setBinaryMessageSizeLimit(int messageSizeLimit)` Configure the maximum size for an incoming binary message.
    `void` | `setTextMessageSizeLimit(int messageSizeLimit)` Configure the maximum size for an incoming text message.
    `protected void` | `writeFrameInternal(SockJsFrame frame)`

    - ### Methods inherited from class org.springframework.web.socket.sockjs.transport.session.AbstractSockJsSession

      `cancelHeartbeat, close, close, delegateConnectionClosed, delegateConnectionEstablished, delegateError, delegateMessages, disableHeartbeat, getAttributes, getId, getMessageCodec, getSockJsServiceConfig, getTimeSinceLastActive, isClosed, isNew, isOpen, scheduleHeartbeat, sendHeartbeat, sendMessage, toString, tryCloseWithSockJsTransportError, updateLastActiveTime, writeFrame`

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

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

- - ### Constructor Detail

    - #### AbstractHttpSockJsSession

          public AbstractHttpSockJsSession(String id,
                                           SockJsServiceConfig config,
                                           WebSocketHandler wsHandler,
                                           Map<String,Object> attributes)

  - ### Method Detail

    - #### getUri

          public URI getUri()

      Description copied from interface: `WebSocketSession`
      Return the URI used to open the WebSocket connection.

    - #### getHandshakeHeaders

          public HttpHeaders getHandshakeHeaders()

      Description copied from interface: `WebSocketSession`
      Return the headers used in the handshake request (never `null`).

    - #### getPrincipal

          @Nullable
          public Principal getPrincipal()

      Description copied from interface: `WebSocketSession`
      Return a `Principal` instance containing the name of the authenticated user.

      If the user has not been authenticated, the method returns `null`.

    - #### getLocalAddress

          @Nullable
          public InetSocketAddress getLocalAddress()

      Description copied from interface: `WebSocketSession`
      Return the address on which the request was received.

    - #### getRemoteAddress

          @Nullable
          public InetSocketAddress getRemoteAddress()

      Description copied from interface: `WebSocketSession`
      Return the address of the remote client.

    - #### setAcceptedProtocol

          public void setAcceptedProtocol(@Nullable
                                          String protocol)

      Unlike WebSocket where sub-protocol negotiation is part of the initial handshake, in HTTP transports the same negotiation must be emulated and the selected protocol set through this setter.
      Parameters:
        `protocol` - the sub-protocol to set

    - #### getAcceptedProtocol

          @Nullable
          public String getAcceptedProtocol()

      Return the selected sub-protocol to use.
      Returns:
        the protocol identifier, or `null` if no protocol was specified or negotiated successfully

    - #### getMessageCache

          protected Queue<String> getMessageCache()

      Return the SockJS buffer for messages stored transparently between polling requests. If the polling request takes longer than 5 seconds, the session is closed.
      See Also:
        `TransportHandlingSockJsService`

    - #### isActive

          public boolean isActive()

      Description copied from class: `AbstractSockJsSession`
      Polling and Streaming sessions periodically close the current HTTP request and wait for the next request to come through. During this "downtime" the session is still open but inactive and unable to send messages and therefore has to buffer them temporarily. A WebSocket session by contrast is stateful and remain active until closed.
      Specified by:
        `isActive` in class `AbstractSockJsSession`

    - #### setTextMessageSizeLimit

          public void setTextMessageSizeLimit(int messageSizeLimit)

      Description copied from interface: `WebSocketSession`
      Configure the maximum size for an incoming text message.

    - #### getTextMessageSizeLimit

          public int getTextMessageSizeLimit()

      Description copied from interface: `WebSocketSession`
      Get the configured maximum size for an incoming text message.

    - #### setBinaryMessageSizeLimit

          public void setBinaryMessageSizeLimit(int messageSizeLimit)

      Description copied from interface: `WebSocketSession`
      Configure the maximum size for an incoming binary message.

    - #### getBinaryMessageSizeLimit

          public int getBinaryMessageSizeLimit()

      Description copied from interface: `WebSocketSession`
      Get the configured maximum size for an incoming binary message.

    - #### getExtensions

          public List<WebSocketExtension> getExtensions()

      Description copied from interface: `WebSocketSession`
      Determine the negotiated extensions.
      Returns:
        the list of extensions, or an empty list if no extension was specified or negotiated successfully

    - #### handleInitialRequest

          public void handleInitialRequest(ServerHttpRequest request,
                                           ServerHttpResponse response,
                                           SockJsFrameFormat frameFormat)
                                    throws SockJsException

      Handle the first request for receiving messages on a SockJS HTTP transport based session.

      Long polling-based transports (e.g. "xhr", "jsonp") complete the request after writing the open frame. Streaming-based transports ("xhr_streaming", "eventsource", and "htmlfile") leave the response open longer for further streaming of message frames but will also close it eventually after some amount of data has been sent.

      Parameters:
        `request` - the current request
        `response` - the current response
        `frameFormat` - the transport-specific SocksJS frame format to use
      Throws:
        `SockJsException`

    - #### handleSuccessiveRequest

          public void handleSuccessiveRequest(ServerHttpRequest request,
                                              ServerHttpResponse response,
                                              SockJsFrameFormat frameFormat)
                                       throws SockJsException

      Handle all requests, except the first one, to receive messages on a SockJS HTTP transport based session.

      Long polling-based transports (e.g. "xhr", "jsonp") complete the request after writing any buffered message frames (or the next one). Streaming-based transports ("xhr_streaming", "eventsource", and "htmlfile") leave the response open longer for further streaming of message frames but will also close it eventually after some amount of data has been sent.

      Parameters:
        `request` - the current request
        `response` - the current response
        `frameFormat` - the transport-specific SocksJS frame format to use
      Throws:
        `SockJsException`

    - #### handleRequestInternal

          protected abstract void handleRequestInternal(ServerHttpRequest request,
                                                        ServerHttpResponse response,
                                                        boolean initialRequest)
                                                 throws IOException

      Invoked when a SockJS transport request is received.
      Parameters:
        `request` - the current request
        `response` - the current response
        `initialRequest` - whether it is the first request for the session
      Throws:
        `IOException`

    - #### sendMessageInternal

          protected final void sendMessageInternal(String message)
                                            throws SockJsTransportFailureException

      Specified by:
        `sendMessageInternal` in class `AbstractSockJsSession`
      Throws:
        `SockJsTransportFailureException`

    - #### flushCache

          protected abstract void flushCache()
                                      throws SockJsTransportFailureException

      Called when the connection is active and ready to write to the response. Subclasses should only call this method from a method where the "responseLock" is acquired.
      Throws:
        `SockJsTransportFailureException`

    - #### disconnect

          protected void disconnect(CloseStatus status)

      Description copied from class: `AbstractSockJsSession`
      Actually close the underlying WebSocket session or in the case of HTTP transports complete the underlying request.
      Specified by:
        `disconnect` in class `AbstractSockJsSession`

    - #### resetRequest

          protected void resetRequest()

    - #### writeFrameInternal

          protected void writeFrameInternal(SockJsFrame frame)
                                     throws IOException

      Specified by:
        `writeFrameInternal` in class `AbstractSockJsSession`
      Throws:
        `IOException`