org.springframework.web.socket.messaging

## Class StompSubProtocolHandler

- java.lang.Object
- - org.springframework.web.socket.messaging.StompSubProtocolHandler

All Implemented Interfaces:
  org.springframework.beans.factory.Aware, org.springframework.context.ApplicationEventPublisherAware, SubProtocolHandler

    public class StompSubProtocolHandler
    extends Object
    implements SubProtocolHandler, org.springframework.context.ApplicationEventPublisherAware

A `SubProtocolHandler` for STOMP that supports versions 1.0, 1.1, and 1.2 of the STOMP specification.

Since:
  4.0
Author:
  Rossen Stoyanchev, Andy Wilkinson

### Nested Class Summary

Nested Classes
`static interface` `StompSubProtocolHandler.Stats` Contract for access to session counters.

### Field Summary

Fields
`static String` `CONNECTED_USER_HEADER` The name of the header set on the CONNECTED frame indicating the name of the user authenticated on the WebSocket session.
`static int` `MINIMUM_WEBSOCKET_MESSAGE_SIZE` This handler supports assembling large STOMP messages split into multiple WebSocket messages and STOMP clients (like stomp.js) indeed split large STOMP messages at 16K boundaries.

### Constructor Summary

Constructors
Constructor and Description
`StompSubProtocolHandler()`

### Method Summary

All Methods Instance Methods Concrete Methods
`void` `afterSessionEnded(WebSocketSession session, CloseStatus closeStatus, org.springframework.messaging.MessageChannel outputChannel)` Invoked after a `WebSocketSession` has ended.
`void` `afterSessionStarted(WebSocketSession session, org.springframework.messaging.MessageChannel outputChannel)` Invoked after a `WebSocketSession` has started.
`StompSubProtocolErrorHandler` `getErrorHandler()` Return the configured error handler.
`org.springframework.messaging.support.MessageHeaderInitializer` `getHeaderInitializer()` Return the configured header initializer.
`int` `getMessageSizeLimit()` Get the configured message buffer size limit in bytes.
`StompSubProtocolHandler.Stats` `getStats()` Return a structured object with internal state and counters.
`String` `getStatsInfo()` Return a String describing internal state and counters.
`List<String>` `getSupportedProtocols()` Return the list of sub-protocols supported by this handler (never `null`).
`void` `handleMessageFromClient(WebSocketSession session, WebSocketMessage<?> webSocketMessage, org.springframework.messaging.MessageChannel outputChannel)` Handle incoming WebSocket messages from clients.
`void` `handleMessageToClient(WebSocketSession session, org.springframework.messaging.Message<?> message)` Handle STOMP messages going back out to WebSocket clients.
`String` `resolveSessionId(org.springframework.messaging.Message<?> message)` Resolve the session id from the given message or return `null`.
`void` `setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher applicationEventPublisher)`
`void` `setDecoder(org.springframework.messaging.simp.stomp.StompDecoder decoder)` Configure a `StompDecoder` for decoding STOMP frames.
`void` `setEncoder(org.springframework.messaging.simp.stomp.StompEncoder encoder)` Configure a `StompEncoder` for encoding STOMP frames.
`void` `setErrorHandler(StompSubProtocolErrorHandler errorHandler)` Configure a handler for error messages sent to clients which allows customizing the error messages or preventing them from being sent.
`void` `setHeaderInitializer(org.springframework.messaging.support.MessageHeaderInitializer headerInitializer)` Configure a `MessageHeaderInitializer` to apply to the headers of all messages created from decoded STOMP frames and other messages sent to the client inbound channel.
`void` `setMessageSizeLimit(int messageSizeLimit)` Configure the maximum size allowed for an incoming STOMP message.
`protected org.springframework.messaging.simp.stomp.StompHeaderAccessor` `toMutableAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor headerAccessor, org.springframework.messaging.Message<?> message)`
`String` `toString()`

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

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

### Field Detail

#### MINIMUM_WEBSOCKET_MESSAGE_SIZE

    public static final int MINIMUM_WEBSOCKET_MESSAGE_SIZE

This handler supports assembling large STOMP messages split into multiple WebSocket messages and STOMP clients (like stomp.js) indeed split large STOMP messages at 16K boundaries. Therefore the WebSocket server input message buffer size must allow 16K at least plus a little extra for SockJS framing.

See Also:
  Constant Field Values

#### CONNECTED_USER_HEADER

    public static final String CONNECTED_USER_HEADER

The name of the header set on the CONNECTED frame indicating the name of the user authenticated on the WebSocket session.

See Also:
  Constant Field Values

### Constructor Detail

#### StompSubProtocolHandler

    public StompSubProtocolHandler()

### Method Detail

#### setErrorHandler

    public void setErrorHandler(StompSubProtocolErrorHandler errorHandler)

Configure a handler for error messages sent to clients which allows customizing the error messages or preventing them from being sent.

By default this isn't configured in which case an ERROR frame is sent with a message header reflecting the error.

Parameters:
  `errorHandler` - the error handler

#### getErrorHandler

    @Nullable
    public StompSubProtocolErrorHandler getErrorHandler()

Return the configured error handler.

#### setMessageSizeLimit

    public void setMessageSizeLimit(int messageSizeLimit)

Configure the maximum size allowed for an incoming STOMP message. Since a STOMP message can be received in multiple WebSocket messages, buffering may be required and therefore it is necessary to know the maximum allowed message size.

By default this property is set to 64K.

Since:
  4.0.3

#### getMessageSizeLimit

    public int getMessageSizeLimit()

Get the configured message buffer size limit in bytes.

Since:
  4.0.3

#### setEncoder

    public void setEncoder(org.springframework.messaging.simp.stomp.StompEncoder encoder)

Configure a `StompEncoder` for encoding STOMP frames.

Since:
  4.3.5

#### setDecoder

    public void setDecoder(org.springframework.messaging.simp.stomp.StompDecoder decoder)

Configure a `StompDecoder` for decoding STOMP frames.

Since:
  4.3.5

#### setHeaderInitializer

    public void setHeaderInitializer(@Nullable
                                     org.springframework.messaging.support.MessageHeaderInitializer headerInitializer)

Configure a `MessageHeaderInitializer` to apply to the headers of all messages created from decoded STOMP frames and other messages sent to the client inbound channel.

By default this property is not set.

#### getHeaderInitializer

    @Nullable
    public org.springframework.messaging.support.MessageHeaderInitializer getHeaderInitializer()

Return the configured header initializer.

#### getSupportedProtocols

    public List<String> getSupportedProtocols()

Description copied from interface: `SubProtocolHandler`
Return the list of sub-protocols supported by this handler (never `null`).

Specified by:
  `getSupportedProtocols` in interface `SubProtocolHandler`

#### setApplicationEventPublisher

    public void setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher applicationEventPublisher)

Specified by:
  `setApplicationEventPublisher` in interface `org.springframework.context.ApplicationEventPublisherAware`

#### getStatsInfo

    public String getStatsInfo()

Return a String describing internal state and counters. Effectively `toString()` on `getStats()`.

#### getStats

    public StompSubProtocolHandler.Stats getStats()

Return a structured object with internal state and counters.

Since:
  5.2

#### handleMessageFromClient

    public void handleMessageFromClient(WebSocketSession session,
                                        WebSocketMessage<?> webSocketMessage,
                                        org.springframework.messaging.MessageChannel outputChannel)

Handle incoming WebSocket messages from clients.

Specified by:
  `handleMessageFromClient` in interface `SubProtocolHandler`
Parameters:
  `session` - the client session
  `webSocketMessage` - the client message
  `outputChannel` - an output channel to send messages to

#### handleMessageToClient

    public void handleMessageToClient(WebSocketSession session,
                                      org.springframework.messaging.Message<?> message)

Handle STOMP messages going back out to WebSocket clients.

Specified by:
  `handleMessageToClient` in interface `SubProtocolHandler`
Parameters:
  `session` - the client session
  `message` - the client message

#### toMutableAccessor

    protected org.springframework.messaging.simp.stomp.StompHeaderAccessor toMutableAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor headerAccessor,
                                                                                             org.springframework.messaging.Message<?> message)

#### resolveSessionId

    @Nullable
    public String resolveSessionId(org.springframework.messaging.Message<?> message)

Description copied from interface: `SubProtocolHandler`
Resolve the session id from the given message or return `null`.

Specified by:
  `resolveSessionId` in interface `SubProtocolHandler`
Parameters:
  `message` - the message to resolve the session id from

#### afterSessionStarted

    public void afterSessionStarted(WebSocketSession session,
                                    org.springframework.messaging.MessageChannel outputChannel)

Description copied from interface: `SubProtocolHandler`
Invoked after a `WebSocketSession` has started.

Specified by:
  `afterSessionStarted` in interface `SubProtocolHandler`
Parameters:
  `session` - the client session
  `outputChannel` - a channel

#### afterSessionEnded

    public void afterSessionEnded(WebSocketSession session,
                                  CloseStatus closeStatus,
                                  org.springframework.messaging.MessageChannel outputChannel)

Description copied from interface: `SubProtocolHandler`
Invoked after a `WebSocketSession` has ended.

Specified by:
  `afterSessionEnded` in interface `SubProtocolHandler`
Parameters:
  `session` - the client session
  `closeStatus` - the reason why the session was closed
  `outputChannel` - a channel

#### toString

    public String toString()

Overrides:
  `toString` in class `Object`