Top Description Inners Fields Constructors Methods
org.springframework.web.socket.messaging

public Class StompSubProtocolHandler

extends Object
implements SubProtocolHandler, ApplicationEventPublisherAware
Class Inheritance
All Implemented Interfaces
org.springframework.context.ApplicationEventPublisherAware, org.springframework.beans.factory.Aware, org.springframework.web.socket.messaging.SubProtocolHandler
Imports
java.io.IOException, java.nio.ByteBuffer, java.security.Principal, java.util.Arrays, .List, .Map, .Set, java.util.concurrent.ConcurrentHashMap, java.util.concurrent.atomic.AtomicInteger, org.apache.commons.logging.Log, .LogFactory, org.springframework.context.ApplicationEvent, .ApplicationEventPublisher, .ApplicationEventPublisherAware, org.springframework.lang.Nullable, org.springframework.messaging.Message, .MessageChannel, org.springframework.messaging.simp.SimpAttributes, .SimpAttributesContextHolder, .SimpMessageHeaderAccessor, .SimpMessageType, org.springframework.messaging.simp.broker.OrderedMessageChannelDecorator, org.springframework.messaging.simp.stomp.BufferingStompDecoder, .StompCommand, .StompDecoder, .StompEncoder, .StompHeaderAccessor, org.springframework.messaging.support.AbstractMessageChannel, .ChannelInterceptor, .ImmutableMessageChannelInterceptor, .MessageBuilder, .MessageHeaderAccessor, .MessageHeaderInitializer, org.springframework.util.Assert, .MimeTypeUtils, org.springframework.web.socket.BinaryMessage, .CloseStatus, .TextMessage, .WebSocketMessage, .WebSocketSession, org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator, .SessionLimitExceededException, .WebSocketSessionDecorator, org.springframework.web.socket.sockjs.transport.SockJsSession

A SubProtocolHandler for STOMP that supports versions 1.0, 1.1, and 1.2 of the STOMP specification.
Authors
Rossen Stoyanchev, Andy Wilkinson
Since
4.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
public static interface
StompSubProtocolHandler.Stats

Contract for access to session counters.

Field Summary

Modifier and TypeField and Description
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.

private final Map<String, BufferingStompDecoder>
private static final byte[]
private StompSubProtocolErrorHandler
private ApplicationEventPublisher
private MessageHeaderInitializer
private Boolean
private static final Log
private int
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.

private Map<String, MessageChannel>
private final StompSubProtocolHandler.DefaultStats
private final Map<String, Principal>
private StompDecoder
private StompEncoder
private static final String[]

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
afterSessionEnded(WebSocketSession
the client session
session
,
CloseStatus
the reason why the session was closed
closeStatus
,
MessageChannel
a channel
outputChannel
)

Implements org.springframework.web.socket.messaging.SubProtocolHandler.afterSessionEnded.

Invoked after a WebSocketSession has ended.

public void
afterSessionStarted(WebSocketSession
the client session
session
,
MessageChannel
a channel
outputChannel
)

Implements org.springframework.web.socket.messaging.SubProtocolHandler.afterSessionStarted.

Invoked after a WebSocketSession has started.

private StompHeaderAccessor
private StompHeaderAccessor
convertConnectAcktoStompConnected(StompHeaderAccessor connectAckHeaders)

The simple broker produces SimpMessageType.CONNECT_ACK that's not STOMP specific and needs to be turned into a STOMP CONNECTED frame.

private Message<byte[]>
private boolean
private String
public StompSubProtocolErrorHandler
getErrorHandler()

Return the configured error handler.

public MessageHeaderInitializer
getHeaderInitializer()

Return the configured header initializer.

public int
getMessageSizeLimit()

Get the configured message buffer size limit in bytes.

public StompSubProtocolHandler.Stats
getStats()

Return a structured object with internal state and counters.

public String
getStatsInfo()

Return a String describing internal state and counters.

private StompHeaderAccessor
public List<String>
getSupportedProtocols()

Implements org.springframework.web.socket.messaging.SubProtocolHandler.getSupportedProtocols.

Return the list of sub-protocols supported by this handler (never null).

private Principal
private void
handleError(WebSocketSession session, Throwable ex, Message<byte[]> clientMessage)

public void
handleMessageFromClient(WebSocketSession
the client session
session
,
WebSocketMessage<?>
the client message
webSocketMessage
,
MessageChannel
an output channel to send messages to
targetChannel
)

Implements org.springframework.web.socket.messaging.SubProtocolHandler.handleMessageFromClient.

Handle incoming WebSocket messages from clients.

public void
handleMessageToClient(WebSocketSession
the client session
session
,
Message<?>
the client message
message
)

Implements org.springframework.web.socket.messaging.SubProtocolHandler.handleMessageToClient.

Handle STOMP messages going back out to WebSocket clients.

public boolean
isPreserveReceiveOrder()

Whether the handler is configured to handle inbound messages in the order in which they were received.

private void
public String
resolveSessionId(Message<?>
the message to resolve the session id from
message
)

Implements org.springframework.web.socket.messaging.SubProtocolHandler.resolveSessionId.

Resolve the session id from the given message or return null.

private void
sendErrorMessage(WebSocketSession session, Throwable error)

Invoked when no errorHandler is configured to send an ERROR frame to the client.

private void
sendToClient(WebSocketSession session, StompHeaderAccessor stompAccessor, byte[] payload)

public void
setApplicationEventPublisher(ApplicationEventPublisher
event publisher to be used by this object
applicationEventPublisher
)

Implements org.springframework.context.ApplicationEventPublisherAware.setApplicationEventPublisher.

Set the ApplicationEventPublisher that this object runs in.

Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method.

public void
setDecoder(StompDecoder decoder)

Configure a StompDecoder for decoding STOMP frames.

public void
setEncoder(StompEncoder encoder)

Configure a StompEncoder for encoding STOMP frames.

public void
setErrorHandler(StompSubProtocolErrorHandler
the error handler
errorHandler
)

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

public void
setHeaderInitializer(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.

public void
setMessageSizeLimit(int messageSizeLimit)

Configure the maximum size allowed for an incoming STOMP message.

public void
setPreserveReceiveOrder(boolean
whether to publish in order
preserveReceiveOrder
)

Whether client messages must be handled in the order received.

protected StompHeaderAccessor
toMutableAccessor(StompHeaderAccessor headerAccessor, Message<?> message)

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

CONNECTED_USER_HEADERback to summary
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.

decodersback to summary
private final Map<String, BufferingStompDecoder> decoders
EMPTY_PAYLOADback to summary
private static final byte[] EMPTY_PAYLOAD
errorHandlerback to summary
private StompSubProtocolErrorHandler errorHandler
Annotations
@Nullable
eventPublisherback to summary
private ApplicationEventPublisher eventPublisher
Annotations
@Nullable
headerInitializerback to summary
private MessageHeaderInitializer headerInitializer
Annotations
@Nullable
immutableMessageInterceptorPresentback to summary
private Boolean immutableMessageInterceptorPresent
Annotations
@Nullable
loggerback to summary
private static final Log logger
messageSizeLimitback to summary
private int messageSizeLimit
MINIMUM_WEBSOCKET_MESSAGE_SIZEback to summary
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.

orderedHandlingMessageChannelsback to summary
private Map<String, MessageChannel> orderedHandlingMessageChannels
Annotations
@Nullable
statsback to summary
private final StompSubProtocolHandler.DefaultStats stats
stompAuthenticationsback to summary
private final Map<String, Principal> stompAuthentications
stompDecoderback to summary
private StompDecoder stompDecoder
stompEncoderback to summary
private StompEncoder stompEncoder
SUPPORTED_VERSIONSback to summary
private static final String[] SUPPORTED_VERSIONS

Constructor Detail

StompSubProtocolHandlerback to summary
public StompSubProtocolHandler()

Method Detail

afterSessionEndedback to summary
public void afterSessionEnded(WebSocketSession session, CloseStatus closeStatus, MessageChannel outputChannel)

Implements org.springframework.web.socket.messaging.SubProtocolHandler.afterSessionEnded.

Doc from org.springframework.web.socket.messaging.SubProtocolHandler.afterSessionEnded.

Invoked after a WebSocketSession has ended.

Parameters
session:WebSocketSession

the client session

closeStatus:CloseStatus

the reason why the session was closed

outputChannel:MessageChannel

a channel

Annotations
@Override
afterSessionStartedback to summary
public void afterSessionStarted(WebSocketSession session, MessageChannel outputChannel)

Implements org.springframework.web.socket.messaging.SubProtocolHandler.afterSessionStarted.

Doc from org.springframework.web.socket.messaging.SubProtocolHandler.afterSessionStarted.

Invoked after a WebSocketSession has started.

Parameters
session:WebSocketSession

the client session

outputChannel:MessageChannel

a channel

Annotations
@Override
afterStompSessionConnectedback to summary
private StompHeaderAccessor afterStompSessionConnected(Message<?> message, StompHeaderAccessor accessor, WebSocketSession session)
convertConnectAcktoStompConnectedback to summary
private StompHeaderAccessor convertConnectAcktoStompConnected(StompHeaderAccessor connectAckHeaders)

The simple broker produces SimpMessageType.CONNECT_ACK that's not STOMP specific and needs to be turned into a STOMP CONNECTED frame.

createDisconnectMessageback to summary
private Message<byte[]> createDisconnectMessage(WebSocketSession session)
detectImmutableMessageInterceptorback to summary
private boolean detectImmutableMessageInterceptor(MessageChannel channel)
getDisconnectReceiptback to summary
private String getDisconnectReceipt(SimpMessageHeaderAccessor simpHeaders)
Annotations
@Nullable
getErrorHandlerback to summary
public StompSubProtocolErrorHandler getErrorHandler()

Return the configured error handler.

Annotations
@Nullable
getHeaderInitializerback to summary
public MessageHeaderInitializer getHeaderInitializer()

Return the configured header initializer.

Annotations
@Nullable
getMessageSizeLimitback to summary
public int getMessageSizeLimit()

Get the configured message buffer size limit in bytes.

Since
4.0.3
getStatsback to summary
public StompSubProtocolHandler.Stats getStats()

Return a structured object with internal state and counters.

Since
5.2
getStatsInfoback to summary
public String getStatsInfo()

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

getStompHeaderAccessorback to summary
private StompHeaderAccessor getStompHeaderAccessor(Message<?> message)
getSupportedProtocolsback to summary
public List<String> getSupportedProtocols()

Implements org.springframework.web.socket.messaging.SubProtocolHandler.getSupportedProtocols.

Doc from org.springframework.web.socket.messaging.SubProtocolHandler.getSupportedProtocols.

Return the list of sub-protocols supported by this handler (never null).

Annotations
@Override
getUserback to summary
private Principal getUser(WebSocketSession session)
Annotations
@Nullable
handleErrorback to summary
private void handleError(WebSocketSession session, Throwable ex, Message<byte[]> clientMessage)
handleMessageFromClientback to summary
public void handleMessageFromClient(WebSocketSession session, WebSocketMessage<?> webSocketMessage, MessageChannel targetChannel)

Implements org.springframework.web.socket.messaging.SubProtocolHandler.handleMessageFromClient.

Handle incoming WebSocket messages from clients.

Parameters
session:WebSocketSession

Doc from org.springframework.web.socket.messaging.SubProtocolHandler.handleMessageFromClient.

the client session

webSocketMessage:WebSocketMessage<?>

Doc from org.springframework.web.socket.messaging.SubProtocolHandler.handleMessageFromClient.

the client message

targetChannel:MessageChannel

Doc from org.springframework.web.socket.messaging.SubProtocolHandler.handleMessageFromClient.

an output channel to send messages to

Annotations
@Override
handleMessageToClientback to summary
public void handleMessageToClient(WebSocketSession session, Message<?> message)

Implements org.springframework.web.socket.messaging.SubProtocolHandler.handleMessageToClient.

Handle STOMP messages going back out to WebSocket clients.

Parameters
session:WebSocketSession

Doc from org.springframework.web.socket.messaging.SubProtocolHandler.handleMessageToClient.

the client session

message:Message<?>

Doc from org.springframework.web.socket.messaging.SubProtocolHandler.handleMessageToClient.

the client message

Annotations
@Override
@SuppressWarnings:unchecked
isPreserveReceiveOrderback to summary
public boolean isPreserveReceiveOrder()

Whether the handler is configured to handle inbound messages in the order in which they were received.

Since
6.1
publishEventback to summary
private void publishEvent(ApplicationEventPublisher publisher, ApplicationEvent event)
resolveSessionIdback to summary
public String resolveSessionId(Message<?> message)

Implements org.springframework.web.socket.messaging.SubProtocolHandler.resolveSessionId.

Doc from org.springframework.web.socket.messaging.SubProtocolHandler.resolveSessionId.

Resolve the session id from the given message or return null.

Parameters
message:Message<?>

the message to resolve the session id from

Annotations
@Override
@Nullable
sendErrorMessageback to summary
private void sendErrorMessage(WebSocketSession session, Throwable error)

Invoked when no errorHandler is configured to send an ERROR frame to the client.

sendToClientback to summary
private void sendToClient(WebSocketSession session, StompHeaderAccessor stompAccessor, byte[] payload)
setApplicationEventPublisherback to summary
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher)

Implements org.springframework.context.ApplicationEventPublisherAware.setApplicationEventPublisher.

Doc from org.springframework.context.ApplicationEventPublisherAware.setApplicationEventPublisher.

Set the ApplicationEventPublisher that this object runs in.

Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method. Invoked before ApplicationContextAware's setApplicationContext.

Parameters
applicationEventPublisher:ApplicationEventPublisher

event publisher to be used by this object

Annotations
@Override
setDecoderback to summary
public void setDecoder(StompDecoder decoder)

Configure a StompDecoder for decoding STOMP frames.

Since
4.3.5
setEncoderback to summary
public void setEncoder(StompEncoder encoder)

Configure a StompEncoder for encoding STOMP frames.

Since
4.3.5
setErrorHandlerback to summary
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:StompSubProtocolErrorHandler

the error handler

setHeaderInitializerback to summary
public void setHeaderInitializer(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.

setMessageSizeLimitback to summary
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
setPreserveReceiveOrderback to summary
public void setPreserveReceiveOrder(boolean preserveReceiveOrder)

Whether client messages must be handled in the order received.

By default messages sent to the "clientInboundChannel" may not be handled in the same order because the channel is backed by a ThreadPoolExecutor that in turn does not guarantee processing in order.

When this flag is set to true messages within the same session will be sent to the "clientInboundChannel" one at a time to preserve the order in which they were received.

Parameters
preserveReceiveOrder:boolean

whether to publish in order

Since
6.1
toMutableAccessorback to summary
protected StompHeaderAccessor toMutableAccessor(StompHeaderAccessor headerAccessor, Message<?> message)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
org.springframework.web.socket.messaging back to summary

private Class StompSubProtocolHandler.DefaultStats

extends Object
implements Stats
Class Inheritance
  • java.lang.Object
  • org.springframework.web.socket.messaging.StompSubProtocolHandler.DefaultStats
All Implemented Interfaces
org.springframework.web.socket.messaging.StompSubProtocolHandler.Stats

Field Summary

Modifier and TypeField and Description
private final AtomicInteger
private final AtomicInteger
private final AtomicInteger

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public int
public int
public int
public void
public void
public void
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

connectback to summary
private final AtomicInteger connect
connectedback to summary
private final AtomicInteger connected
disconnectback to summary
private final AtomicInteger disconnect

Constructor Detail

DefaultStatsback to summary
private DefaultStats()

Method Detail

getTotalConnectback to summary
public int getTotalConnect()

Implements org.springframework.web.socket.messaging.StompSubProtocolHandler.Stats.getTotalConnect.

Doc from org.springframework.web.socket.messaging.StompSubProtocolHandler.Stats.getTotalConnect.

The number of CONNECT frames processed.

Annotations
@Override
getTotalConnectedback to summary
public int getTotalConnected()

Implements org.springframework.web.socket.messaging.StompSubProtocolHandler.Stats.getTotalConnected.

Doc from org.springframework.web.socket.messaging.StompSubProtocolHandler.Stats.getTotalConnected.

The number of CONNECTED frames processed.

Annotations
@Override
getTotalDisconnectback to summary
public int getTotalDisconnect()

Implements org.springframework.web.socket.messaging.StompSubProtocolHandler.Stats.getTotalDisconnect.

Doc from org.springframework.web.socket.messaging.StompSubProtocolHandler.Stats.getTotalDisconnect.

The number of DISCONNECT frames processed.

Annotations
@Override
incrementConnectCountback to summary
public void incrementConnectCount()
incrementConnectedCountback to summary
public void incrementConnectedCount()
incrementDisconnectCountback to summary
public void incrementDisconnectCount()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
org.springframework.web.socket.messaging back to summary

public Interface StompSubProtocolHandler.Stats

Known Direct Implementers
org.springframework.web.socket.messaging.StompSubProtocolHandler.DefaultStats

Contract for access to session counters.
Since
5.2

Method Summary

Modifier and TypeMethod and Description
public int
getTotalConnect()

The number of CONNECT frames processed.

public int
getTotalConnected()

The number of CONNECTED frames processed.

public int
getTotalDisconnect()

The number of DISCONNECT frames processed.

Method Detail

getTotalConnectback to summary
public int getTotalConnect()

The number of CONNECT frames processed.

getTotalConnectedback to summary
public int getTotalConnected()

The number of CONNECTED frames processed.

getTotalDisconnectback to summary
public int getTotalDisconnect()

The number of DISCONNECT frames processed.