org.springframework.messaging.simp

## Class SimpMessageHeaderAccessor

- java.lang.Object
- - org.springframework.messaging.support.MessageHeaderAccessor
  - - org.springframework.messaging.support.NativeMessageHeaderAccessor
    - - org.springframework.messaging.simp.SimpMessageHeaderAccessor

Direct Known Subclasses:
  StompHeaderAccessor

    public class SimpMessageHeaderAccessor
    extends NativeMessageHeaderAccessor

A base class for working with message headers in simple messaging protocols that support basic messaging patterns. Provides uniform access to specific values common across protocols such as a destination, message type (e.g. publish, subscribe, etc), session id, and others.

Use one of the static factory method in this class, then call getters and setters, and at the end if necessary call `MessageHeaderAccessor.toMap()` to obtain the updated headers.

Since:
  4.0
Author:
  Rossen Stoyanchev

### Field Summary

Fields
`static String` `CONNECT_MESSAGE_HEADER`
`static String` `DESTINATION_HEADER`
`static String` `DISCONNECT_MESSAGE_HEADER`
`static String` `HEART_BEAT_HEADER`
`static String` `IGNORE_ERROR` A header that indicates to the broker that the sender will ignore errors.
`static String` `MESSAGE_TYPE_HEADER`
`static String` `ORIGINAL_DESTINATION` A header for internal use with "user" destinations where we need to restore the destination prior to sending messages to clients.
`static String` `SESSION_ATTRIBUTES`
`static String` `SESSION_ID_HEADER`
`static String` `SUBSCRIPTION_ID_HEADER`
`static String` `USER_HEADER`

### Fields inherited from class org.springframework.messaging.support.NativeMessageHeaderAccessor

`NATIVE_HEADERS`

### Fields inherited from class org.springframework.messaging.support.MessageHeaderAccessor

`DEFAULT_CHARSET`

### Constructor Summary

Constructors
`protected` `SimpMessageHeaderAccessor(Message<?> message)` A constructor for accessing and modifying existing message headers.
`protected` `SimpMessageHeaderAccessor(SimpMessageType messageType, Map<String,List<String>> externalSourceHeaders)` A constructor for creating new message headers.

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods
`static SimpMessageHeaderAccessor` `create()` Create an instance with `SimpMessageType` `MESSAGE`.
`static SimpMessageHeaderAccessor` `create(SimpMessageType messageType)` Create an instance with the given `SimpMessageType`.
`protected MessageHeaderAccessor` `createAccessor(Message<?> message)` Build a 'nested' accessor for the given message.
`String` `getDestination()`
`static String` `getDestination(Map<String,Object> headers)`
`String` `getDetailedLogMessage(Object payload)` Return a more detailed message for logging purposes.
`static long[]` `getHeartbeat(Map<String,Object> headers)`
`SimpMessageType` `getMessageType()`
`static SimpMessageType` `getMessageType(Map<String,Object> headers)`
`Map<String,Object>` `getSessionAttributes()` Return the attributes associated with the current session.
`static Map<String,Object>` `getSessionAttributes(Map<String,Object> headers)`
`String` `getSessionId()` Return the id of the current session.
`static String` `getSessionId(Map<String,Object> headers)`
`String` `getShortLogMessage(Object payload)` Return a concise message for logging purposes.
`String` `getSubscriptionId()`
`static String` `getSubscriptionId(Map<String,Object> headers)`
`Principal` `getUser()` Return the user associated with the current session.
`static Principal` `getUser(Map<String,Object> headers)`
`void` `setDestination(String destination)`
`void` `setMessageTypeIfNotSet(SimpMessageType messageType)`
`void` `setSessionAttributes(Map<String,Object> attributes)` A static alternative for access to the session attributes header.
`void` `setSessionId(String sessionId)`
`void` `setSubscriptionId(String subscriptionId)`
`void` `setUser(Principal principal)`
`void` `setUserChangeCallback(Consumer<Principal> callback)` Provide a callback to be invoked if and when `setUser(Principal)` is called.
`static SimpMessageHeaderAccessor` `wrap(Message<?> message)` Create an instance from the payload and headers of the given Message.

### Methods inherited from class org.springframework.messaging.support.NativeMessageHeaderAccessor

`addNativeHeader, addNativeHeaders, containsNativeHeader, copyHeaders, copyHeadersIfAbsent, getFirstNativeHeader, getFirstNativeHeader, getNativeHeader, getNativeHeaders, removeNativeHeader, setImmutable, setNativeHeader, setNativeHeaderValues, toNativeHeaderMap`

### Methods inherited from class org.springframework.messaging.support.MessageHeaderAccessor

`getAccessor, getAccessor, getAccessor, getContentType, getDetailedPayloadLogMessage, getErrorChannel, getHeader, getId, getMessageHeaders, getMutableAccessor, getReplyChannel, getShortPayloadLogMessage, getTimestamp, isModified, isMutable, isReadableContentType, isReadOnly, removeHeader, removeHeaders, setContentType, setErrorChannel, setErrorChannelName, setHeader, setHeaderIfAbsent, setLeaveMutable, setModified, setReplyChannel, setReplyChannelName, toMap, toMessageHeaders, toString, verifyType`

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

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

### Field Detail

#### DESTINATION_HEADER

    public static final String DESTINATION_HEADER

See Also:
  Constant Field Values

#### MESSAGE_TYPE_HEADER

    public static final String MESSAGE_TYPE_HEADER

See Also:
  Constant Field Values

#### SESSION_ID_HEADER

    public static final String SESSION_ID_HEADER

See Also:
  Constant Field Values

#### SESSION_ATTRIBUTES

    public static final String SESSION_ATTRIBUTES

See Also:
  Constant Field Values

#### SUBSCRIPTION_ID_HEADER

    public static final String SUBSCRIPTION_ID_HEADER

See Also:
  Constant Field Values

#### USER_HEADER

    public static final String USER_HEADER

See Also:
  Constant Field Values

#### CONNECT_MESSAGE_HEADER

    public static final String CONNECT_MESSAGE_HEADER

See Also:
  Constant Field Values

#### DISCONNECT_MESSAGE_HEADER

    public static final String DISCONNECT_MESSAGE_HEADER

See Also:
  Constant Field Values

#### HEART_BEAT_HEADER

    public static final String HEART_BEAT_HEADER

See Also:
  Constant Field Values

#### ORIGINAL_DESTINATION

    public static final String ORIGINAL_DESTINATION

A header for internal use with "user" destinations where we need to restore the destination prior to sending messages to clients.

See Also:
  Constant Field Values

#### IGNORE_ERROR

    public static final String IGNORE_ERROR

A header that indicates to the broker that the sender will ignore errors. The header is simply checked for presence or absence.

See Also:
  Constant Field Values

### Constructor Detail

#### SimpMessageHeaderAccessor

    protected SimpMessageHeaderAccessor(SimpMessageType messageType,
                                        @Nullable
                                        Map<String,List<String>> externalSourceHeaders)

A constructor for creating new message headers. This constructor is protected. See factory methods in this and sub-classes.

#### SimpMessageHeaderAccessor

    protected SimpMessageHeaderAccessor(Message<?> message)

A constructor for accessing and modifying existing message headers. This constructor is protected. See factory methods in this and sub-classes.

### Method Detail

#### createAccessor

    protected MessageHeaderAccessor createAccessor(Message<?> message)

Description copied from class: `MessageHeaderAccessor`
Build a 'nested' accessor for the given message.

Overrides:
  `createAccessor` in class `MessageHeaderAccessor`
Parameters:
  `message` - the message to build a new accessor for
Returns:
  the nested accessor (typically a specific subclass)

#### setMessageTypeIfNotSet

    public void setMessageTypeIfNotSet(SimpMessageType messageType)

#### getMessageType

    @Nullable
    public SimpMessageType getMessageType()

#### setDestination

    public void setDestination(@Nullable
                               String destination)

#### getDestination

    @Nullable
    public String getDestination()

#### setSubscriptionId

    public void setSubscriptionId(@Nullable
                                  String subscriptionId)

#### getSubscriptionId

    @Nullable
    public String getSubscriptionId()

#### setSessionId

    public void setSessionId(@Nullable
                             String sessionId)

#### getSessionId

    @Nullable
    public String getSessionId()

Return the id of the current session.

#### setSessionAttributes

    public void setSessionAttributes(@Nullable
                                     Map<String,Object> attributes)

A static alternative for access to the session attributes header.

#### getSessionAttributes

    @Nullable
    public Map<String,Object> getSessionAttributes()

Return the attributes associated with the current session.

#### setUser

    public void setUser(@Nullable
                        Principal principal)

#### getUser

    @Nullable
    public Principal getUser()

Return the user associated with the current session.

#### setUserChangeCallback

    public void setUserChangeCallback(Consumer<Principal> callback)

Provide a callback to be invoked if and when `setUser(Principal)` is called. This is used internally on the inbound channel to detect token-based authentications through an interceptor.

Parameters:
  `callback` - the callback to invoke
Since:
  5.1.9

#### getShortLogMessage

    public String getShortLogMessage(Object payload)

Description copied from class: `MessageHeaderAccessor`
Return a concise message for logging purposes.

Overrides:
  `getShortLogMessage` in class `MessageHeaderAccessor`
Parameters:
  `payload` - the payload that corresponds to the headers.
Returns:
  the message

#### getDetailedLogMessage

    public String getDetailedLogMessage(@Nullable
                                        Object payload)

Description copied from class: `MessageHeaderAccessor`
Return a more detailed message for logging purposes.

Overrides:
  `getDetailedLogMessage` in class `MessageHeaderAccessor`
Parameters:
  `payload` - the payload that corresponds to the headers.
Returns:
  the message

#### create

    public static SimpMessageHeaderAccessor create()

Create an instance with `SimpMessageType` `MESSAGE`.

#### create

    public static SimpMessageHeaderAccessor create(SimpMessageType messageType)

Create an instance with the given `SimpMessageType`.

#### wrap

    public static SimpMessageHeaderAccessor wrap(Message<?> message)

Create an instance from the payload and headers of the given Message.

#### getMessageType

    @Nullable
    public static SimpMessageType getMessageType(Map<String,Object> headers)

#### getDestination

    @Nullable
    public static String getDestination(Map<String,Object> headers)

#### getSubscriptionId

    @Nullable
    public static String getSubscriptionId(Map<String,Object> headers)

#### getSessionId

    @Nullable
    public static String getSessionId(Map<String,Object> headers)

#### getSessionAttributes

    @Nullable
    public static Map<String,Object> getSessionAttributes(Map<String,Object> headers)

#### getUser

    @Nullable
    public static Principal getUser(Map<String,Object> headers)

#### getHeartbeat

    @Nullable
    public static long[] getHeartbeat(Map<String,Object> headers)