Class NativeMessageHeaderAccessor
- java.lang.Object
- org.springframework.messaging.support.MessageHeaderAccessor
- org.springframework.messaging.support.NativeMessageHeaderAccessor
- Direct Known Subclasses:
JmsMessageHeaderAccessor
,SimpMessageHeaderAccessor
public class NativeMessageHeaderAccessor extends MessageHeaderAccessor
An extension ofMessageHeaderAccessor
that also stores and provides read/write access to message headers from an external source -- e.g. a SpringMessage
created to represent a STOMP message received from a STOMP client or message broker. Native message headers are kept in aMap<String, List<String>>
under the keyNATIVE_HEADERS
.This class is not intended for direct use but is rather expected to be used indirectly through protocol-specific sub-classes such as
StompHeaderAccessor
. Such sub-classes may provide factory methods to translate message headers from an external messaging source (e.g. STOMP) to SpringMessage
headers and reversely to translate SpringMessage
headers to a message to send to an external source.- Since:
- 4.0
- Author:
- Rossen Stoyanchev
Field Summary
Fields Modifier and Type Field Description static String
NATIVE_HEADERS
Fields inherited from class org.springframework.messaging.support.MessageHeaderAccessor
DEFAULT_CHARSET
Constructor Summary
Constructors Modifier Constructor Description protected
NativeMessageHeaderAccessor()
Protected constructor to create a new instance.protected
NativeMessageHeaderAccessor(Map<String,List<String>> nativeHeaders)
Protected constructor to create an instance with the given native headers.protected
NativeMessageHeaderAccessor(Message<?> message)
Protected constructor that copies headers from another message.
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addNativeHeader(String name, String value)
Add the specified native header value to existing values.void
addNativeHeaders(MultiValueMap<String,String> headers)
Add the specified native headers to existing values.boolean
containsNativeHeader(String headerName)
Whether the native header map contains the give header name.String
getFirstNativeHeader(String headerName)
Return the first value for the specified native header, if present.static String
getFirstNativeHeader(String headerName, Map<String,Object> headers)
Return the first value for the specified native header, ornull
if none.List<String>
getNativeHeader(String headerName)
Return all values for the specified native header, if present.List<String>
removeNativeHeader(String headerName)
Remove the specified native header value replacing existing values.void
setImmutable()
By default whenMessageHeaderAccessor.getMessageHeaders()
is called,"this"
MessageHeaderAccessor
instance can no longer be used to modify the underlying message headers.void
setNativeHeader(String name, String value)
Set the specified native header value replacing existing values.Map<String,List<String>>
toNativeHeaderMap()
Return a copy of the native headers sub-map, or an empty map.Methods inherited from class org.springframework.messaging.support.MessageHeaderAccessor
copyHeaders, copyHeadersIfAbsent, createAccessor, getAccessor, getAccessor, getContentType, getDetailedLogMessage, getDetailedPayloadLogMessage, getErrorChannel, getHeader, getId, getMessageHeaders, getMutableAccessor, getReplyChannel, getShortLogMessage, getShortPayloadLogMessage, getTimestamp, isModified, isMutable, isReadableContentType, isReadOnly, removeHeader, removeHeaders, setContentType, setErrorChannel, setErrorChannelName, setHeader, setHeaderIfAbsent, setLeaveMutable, setModified, setReplyChannel, setReplyChannelName, toMap, toMessageHeaders, toString, verifyType
Field Detail
NATIVE_HEADERS
public static final String NATIVE_HEADERS
- See Also:
- Constant Field Values
Constructor Detail
NativeMessageHeaderAccessor
protected NativeMessageHeaderAccessor()
Protected constructor to create a new instance.
NativeMessageHeaderAccessor
protected NativeMessageHeaderAccessor(Map<String,List<String>> nativeHeaders)
Protected constructor to create an instance with the given native headers.- Parameters:
nativeHeaders
- native headers to create the message with (may benull
)
NativeMessageHeaderAccessor
protected NativeMessageHeaderAccessor(Message<?> message)
Protected constructor that copies headers from another message.
Method Detail
toNativeHeaderMap
public Map<String,List<String>> toNativeHeaderMap()
Return a copy of the native headers sub-map, or an empty map.
setImmutable
public void setImmutable()
Description copied from class:MessageHeaderAccessor
By default whenMessageHeaderAccessor.getMessageHeaders()
is called,"this"
MessageHeaderAccessor
instance can no longer be used to modify the underlying message headers. However ifMessageHeaderAccessor.setLeaveMutable(boolean)
is used, this method is necessary to indicate explicitly when theMessageHeaders
instance should no longer be modified.- Overrides:
setImmutable
in classMessageHeaderAccessor
containsNativeHeader
public boolean containsNativeHeader(String headerName)
Whether the native header map contains the give header name.- Parameters:
headerName
- the name of the header
getNativeHeader
public List<String> getNativeHeader(String headerName)
Return all values for the specified native header, if present.- Parameters:
headerName
- the name of the header- Returns:
- the associated values, or
null
if none
getFirstNativeHeader
public String getFirstNativeHeader(String headerName)
Return the first value for the specified native header, if present.- Parameters:
headerName
- the name of the header- Returns:
- the associated value, or
null
if none
setNativeHeader
public void setNativeHeader(String name, String value)
Set the specified native header value replacing existing values.In order for this to work, the accessor must be
mutable
. SeeMessageHeaderAccessor
for details.
addNativeHeader
public void addNativeHeader(String name, String value)
Add the specified native header value to existing values.In order for this to work, the accessor must be
mutable
. SeeMessageHeaderAccessor
for details.- Parameters:
name
- the name of the headervalue
- the header value to set
addNativeHeaders
public void addNativeHeaders(MultiValueMap<String,String> headers)
Add the specified native headers to existing values.- Parameters:
headers
- the headers to set
removeNativeHeader
public List<String> removeNativeHeader(String headerName)
Remove the specified native header value replacing existing values.In order for this to work, the accessor must be
mutable
. SeeMessageHeaderAccessor
for details.- Parameters:
headerName
- the name of the header- Returns:
- the associated values, or
null
if the header was not present
getFirstNativeHeader
public static String getFirstNativeHeader(String headerName, Map<String,Object> headers)
Return the first value for the specified native header, ornull
if none.- Parameters:
headerName
- the name of the headerheaders
- the headers map to introspect- Returns:
- the associated value, or
null
if none