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:
    SockJsSession, WebSocketSession
  Direct Known Subclasses:
    PollingSockJsSession, StreamingSockJsSession

      public abstract class AbstractHttpSockJsSession
      extends AbstractSockJsSession

  An abstract base class for use with HTTP transport based 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`

  - ### 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()` Only called if the connection is currently active
    `String` | `getAcceptedProtocol()` Return the selected sub-protocol to use.
    `int` | `getBinaryMessageSizeLimit()` Get the configured maximum size for an incoming binary message.
    `List<WebSocketExtension>` | `getExtensions()` Return the negotiated extensions or `null` if none was specified or negotiated successfully.
    `HttpHeaders` | `getHandshakeHeaders()` Return the headers used in the handshake request.
    `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.
    `protected ServerHttpResponse` | `getResponse()` Return response for the current request, or `null` if between requests.
    `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 HTTP request, i.e.
    `void` | `handleSuccessiveRequest(ServerHttpRequest request, ServerHttpResponse response, SockJsFrameFormat frameFormat)` Handle all HTTP requests part of the same SockJS session except for the very first, initial request.
    `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` | `startAsyncRequest()`
    `protected void` | `writeFrameInternal(SockJsFrame frame)`
    `protected void` | `writePrelude(ServerHttpRequest request, ServerHttpResponse response)`

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

      `cancelHeartbeat, close, close, delegateConnectionClosed, delegateConnectionEstablished, delegateError, delegateMessages, disableHeartbeat, getAttributes, getId, 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.

    - #### getPrincipal

          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

          public InetSocketAddress getLocalAddress()

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

    - #### getRemoteAddress

          public InetSocketAddress getRemoteAddress()

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

    - #### setAcceptedProtocol

          public 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.
      Parameters:
        `protocol` - the sub-protocol to set

    - #### getAcceptedProtocol

          public String getAcceptedProtocol()

      Return the selected sub-protocol to use.

    - #### getResponse

          protected ServerHttpResponse getResponse()

      Return response for the current request, or `null` if between requests.

    - #### 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 will be 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`
      Return the negotiated extensions or `null` if none was specified or negotiated successfully.

    - #### handleInitialRequest

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

      Handle the first HTTP request, i.e. the one that starts a SockJS session. Write a prelude to the response (if needed), send the SockJS "open" frame to indicate to the client the session is opened, and invoke the delegate WebSocketHandler to provide it with the newly opened session.

      The "xhr" and "jsonp" (polling-based) transports completes the initial request as soon as the open frame is sent. Following that the client should start a successive polling request within the same SockJS session.

      The "xhr_streaming", "eventsource", and "htmlfile" transports are streaming based and will leave the initial request open in order to stream one or more messages. However, even streaming based transports eventually recycle the long running request, after a certain number of bytes have been streamed (128K by default), and allow the client to start a successive request within the same SockJS session.

      Parameters:
        `request` - the current request
        `response` - the current response
        `frameFormat` - the transport-specific SocksJS frame format to use
      Throws:
        `SockJsException`
      See Also:
        `handleSuccessiveRequest(org.springframework.http.server.ServerHttpRequest, org.springframework.http.server.ServerHttpResponse, org.springframework.web.socket.sockjs.frame.SockJsFrameFormat)`

    - #### writePrelude

          protected void writePrelude(ServerHttpRequest request,
                                      ServerHttpResponse response)
                               throws IOException

      Throws:
        `IOException`

    - #### handleSuccessiveRequest

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

      Handle all HTTP requests part of the same SockJS session except for the very first, initial request. Write a prelude (if needed) and keep the request open and ready to send a message from the server to the client.

      The "xhr" and "jsonp" (polling-based) transports completes the request when the next message is sent, which could be an array of messages cached during the time between successive requests, or it could be a heartbeat message sent if no other messages were sent (by default within 25 seconds).

      The "xhr_streaming", "eventsource", and "htmlfile" transports are streaming based and will leave the request open longer in order to stream messages over a period of time. However, even streaming based transports eventually recycle the long running request, after a certain number of bytes have been streamed (128K by default), and allow the client to start a successive request within the same SockJS session.

      Parameters:
        `request` - the current request
        `response` - the current response
        `frameFormat` - the transport-specific SocksJS frame format to use
      Throws:
        `SockJsException`
      See Also:
        `handleInitialRequest(org.springframework.http.server.ServerHttpRequest, org.springframework.http.server.ServerHttpResponse, org.springframework.web.socket.sockjs.frame.SockJsFrameFormat)`

    - #### startAsyncRequest

          protected void startAsyncRequest()
                                    throws SockJsException

      Throws:
        `SockJsException`

    - #### sendMessageInternal

          protected final void sendMessageInternal(String message)
                                            throws SockJsTransportFailureException

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

    - #### flushCache

          protected abstract void flushCache()
                                      throws SockJsTransportFailureException

      Only called if the connection is currently active
      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`