public class MessageHeaderAccessor
extends java.lang.Object
The method getMessageHeaders()
provides access to the underlying,
fully-prepared MessageHeaders
that can then be used as-is (i.e.
without copying) to create a single message as follows:
MessageHeaderAccessor accessor = new MessageHeaderAccessor(); accessor.setHeader("foo", "bar"); Message message = MessageBuilder.createMessage("payload", accessor.getMessageHeaders());
After the above, by default the MessageHeaderAccessor
becomes
immutable. However it is possible to leave it mutable for further initialization
in the same thread, for example:
MessageHeaderAccessor accessor = new MessageHeaderAccessor(); accessor.setHeader("foo", "bar"); accessor.setLeaveMutable(true); Message message = MessageBuilder.createMessage("payload", accessor.getMessageHeaders()); // later on in the same thread... MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message); accessor.setHeader("bar", "baz"); accessor.setImmutable();
The method toMap()
returns a copy of the underlying headers. It can
be used to prepare multiple messages from the same MessageHeaderAccessor
instance:
MessageHeaderAccessor accessor = new MessageHeaderAccessor(); MessageBuilder builder = MessageBuilder.withPayload("payload").setHeaders(accessor); accessor.setHeader("foo", "bar1"); Message message1 = builder.build(); accessor.setHeader("foo", "bar2"); Message message2 = builder.build(); accessor.setHeader("foo", "bar3"); Message message3 = builder.build();
However note that with the above style, the header accessor is shared and
cannot be re-obtained later on. Alternatively it is also possible to create
one MessageHeaderAccessor
per message:
MessageHeaderAccessor accessor1 = new MessageHeaderAccessor(); accessor.set("foo", "bar1"); Message message1 = MessageBuilder.createMessage("payload", accessor1.getMessageHeaders()); MessageHeaderAccessor accessor2 = new MessageHeaderAccessor(); accessor.set("foo", "bar2"); Message message2 = MessageBuilder.createMessage("payload", accessor2.getMessageHeaders()); MessageHeaderAccessor accessor3 = new MessageHeaderAccessor(); accessor.set("foo", "bar3"); Message message3 = MessageBuilder.createMessage("payload", accessor3.getMessageHeaders());
Note that the above examples aim to demonstrate the general idea of using header accessors. The most likely usage however is through subclasses.
Modifier and Type | Class and Description |
---|---|
private class |
MessageHeaderAccessor.MutableMessageHeaders |
Modifier and Type | Field and Description |
---|---|
static java.nio.charset.Charset |
DEFAULT_CHARSET |
private boolean |
enableTimestamp |
private MessageHeaderAccessor.MutableMessageHeaders |
headers |
private IdGenerator |
idGenerator |
private boolean |
leaveMutable |
protected Log |
logger |
private boolean |
modified |
private static MimeType[] |
READABLE_MIME_TYPES |
Constructor and Description |
---|
MessageHeaderAccessor()
A constructor to create new headers.
|
MessageHeaderAccessor(Message<?> message)
A constructor accepting the headers of an existing message to copy.
|
Modifier and Type | Method and Description |
---|---|
void |
copyHeaders(java.util.Map<java.lang.String,?> headersToCopy)
Copy the name-value pairs from the provided Map.
|
void |
copyHeadersIfAbsent(java.util.Map<java.lang.String,?> headersToCopy)
Copy the name-value pairs from the provided Map.
|
protected MessageHeaderAccessor |
createAccessor(Message<?> message)
Build a 'nested' accessor for the given message.
|
static <T extends MessageHeaderAccessor> |
getAccessor(Message<?> message,
java.lang.Class<T> requiredType)
Return the original
MessageHeaderAccessor used to create the headers
of the given Message , or null if that's not available or if
its type does not match the required type. |
static <T extends MessageHeaderAccessor> |
getAccessor(MessageHeaders messageHeaders,
java.lang.Class<T> requiredType)
A variation of
getAccessor(org.springframework.messaging.Message, Class)
with a MessageHeaders instance instead of a Message . |
MimeType |
getContentType() |
java.lang.String |
getDetailedLogMessage(java.lang.Object payload)
Return a more detailed message for logging purposes.
|
protected java.lang.String |
getDetailedPayloadLogMessage(java.lang.Object payload) |
java.lang.Object |
getErrorChannel() |
java.lang.Object |
getHeader(java.lang.String headerName)
Retrieve the value for the header with the given name.
|
java.util.UUID |
getId() |
private java.util.List<java.lang.String> |
getMatchingHeaderNames(java.lang.String pattern,
java.util.Map<java.lang.String,java.lang.Object> headers) |
MessageHeaders |
getMessageHeaders()
Return the underlying
MessageHeaders instance. |
static MessageHeaderAccessor |
getMutableAccessor(Message<?> message)
Return a mutable
MessageHeaderAccessor for the given message attempting
to match the type of accessor used to create the message headers, or otherwise
wrapping the message with a MessageHeaderAccessor instance. |
java.lang.Object |
getReplyChannel() |
java.lang.String |
getShortLogMessage(java.lang.Object payload)
Return a concise message for logging purposes.
|
protected java.lang.String |
getShortPayloadLogMessage(java.lang.Object payload) |
java.lang.Long |
getTimestamp() |
boolean |
isModified()
Check whether the underlying message headers have been marked as modified.
|
boolean |
isMutable()
Whether the underlying headers can still be modified.
|
protected boolean |
isReadableContentType() |
protected boolean |
isReadOnly(java.lang.String headerName) |
void |
removeHeader(java.lang.String headerName)
Remove the value for the given header name.
|
void |
removeHeaders(java.lang.String... headerPatterns)
Removes all headers provided via array of 'headerPatterns'.
|
void |
setContentType(MimeType contentType) |
(package private) void |
setEnableTimestamp(boolean enableTimestamp)
A package private mechanism to enables the automatic addition of the
MessageHeaders.TIMESTAMP header. |
void |
setErrorChannel(MessageChannel errorChannel) |
void |
setErrorChannelName(java.lang.String errorChannelName) |
void |
setHeader(java.lang.String name,
java.lang.Object value)
Set the value for the given header name.
|
void |
setHeaderIfAbsent(java.lang.String name,
java.lang.Object value)
Set the value for the given header name only if the header name is not
already associated with a value.
|
(package private) void |
setIdGenerator(IdGenerator idGenerator)
A package-private mechanism to configure the IdGenerator strategy to use.
|
void |
setImmutable()
By default when
getMessageHeaders() is called, "this"
MessageHeaderAccessor instance can no longer be used to modify the
underlying message headers. |
void |
setLeaveMutable(boolean leaveMutable)
By default when
getMessageHeaders() is called, "this"
MessageHeaderAccessor instance can no longer be used to modify the
underlying message headers and the returned MessageHeaders is immutable. |
protected void |
setModified(boolean modified)
Mark the underlying message headers as modified.
|
void |
setReplyChannel(MessageChannel replyChannel) |
void |
setReplyChannelName(java.lang.String replyChannelName) |
java.util.Map<java.lang.String,java.lang.Object> |
toMap()
Return a copy of the underlying header values as a plain
Map object. |
MessageHeaders |
toMessageHeaders()
Return a copy of the underlying header values as a
MessageHeaders object. |
java.lang.String |
toString() |
protected void |
verifyType(java.lang.String headerName,
java.lang.Object headerValue) |
public static final java.nio.charset.Charset DEFAULT_CHARSET
private static final MimeType[] READABLE_MIME_TYPES
protected final Log logger
private final MessageHeaderAccessor.MutableMessageHeaders headers
private boolean leaveMutable
private boolean modified
private boolean enableTimestamp
private IdGenerator idGenerator
public MessageHeaderAccessor()
public MessageHeaderAccessor(Message<?> message)
protected MessageHeaderAccessor createAccessor(Message<?> message)
message
- the message to build a new accessor forpublic void setLeaveMutable(boolean leaveMutable)
getMessageHeaders()
is called, "this"
MessageHeaderAccessor
instance can no longer be used to modify the
underlying message headers and the returned MessageHeaders
is immutable.
However when this is set to true
, the returned (underlying)
MessageHeaders
instance remains mutable. To make further modifications
continue to use the same accessor instance or re-obtain it via:
MessageHeaderAccessor.getAccessor(Message, Class)
When modifications are complete use setImmutable()
to prevent
further changes. The intended use case for this mechanism is initialization
of a Message within a single thread.
By default this is set to false
.
public void setImmutable()
getMessageHeaders()
is called, "this"
MessageHeaderAccessor
instance can no longer be used to modify the
underlying message headers. However if setLeaveMutable(boolean)
is used, this method is necessary to indicate explicitly when the
MessageHeaders
instance should no longer be modified.public boolean isMutable()
protected void setModified(boolean modified)
modified
- typically true
, or false
to reset the flagpublic boolean isModified()
true
if the flag has been set, false
otherwisevoid setEnableTimestamp(boolean enableTimestamp)
MessageHeaders.TIMESTAMP
header.
By default, this property is set to false
.
IdTimestampMessageHeaderInitializer
void setIdGenerator(IdGenerator idGenerator)
By default this property is not set in which case the default IdGenerator
in MessageHeaders
is used.
IdTimestampMessageHeaderInitializer
public MessageHeaders getMessageHeaders()
MessageHeaders
instance.
Unless setLeaveMutable(boolean)
was set to true
, after
this call, the headers are immutable and this accessor can no longer
modify them.
This method always returns the same MessageHeaders
instance if
invoked multiples times. To obtain a copy of the underlying headers, use
toMessageHeaders()
or toMap()
instead.
public MessageHeaders toMessageHeaders()
MessageHeaders
object.
This method can be invoked many times, with modifications in between where each new call returns a fresh copy of the current header values.
public java.util.Map<java.lang.String,java.lang.Object> toMap()
Map
object.
This method can be invoked many times, with modifications in between where each new call returns a fresh copy of the current header values.
public java.lang.Object getHeader(java.lang.String headerName)
headerName
- the name of the headernull
if none foundpublic void setHeader(java.lang.String name, java.lang.Object value)
If the provided value is null
, the header will be removed.
protected void verifyType(java.lang.String headerName, java.lang.Object headerValue)
public void setHeaderIfAbsent(java.lang.String name, java.lang.Object value)
public void removeHeader(java.lang.String headerName)
public void removeHeaders(java.lang.String... headerPatterns)
As the name suggests, array may contain simple matching patterns for header names. Supported pattern styles are: "xxx*", "*xxx", "*xxx*" and "xxx*yyy".
private java.util.List<java.lang.String> getMatchingHeaderNames(java.lang.String pattern, java.util.Map<java.lang.String,java.lang.Object> headers)
public void copyHeaders(java.util.Map<java.lang.String,?> headersToCopy)
This operation will overwrite any existing values. Use
copyHeadersIfAbsent(Map)
to avoid overwriting values.
public void copyHeadersIfAbsent(java.util.Map<java.lang.String,?> headersToCopy)
This operation will not overwrite any existing values.
protected boolean isReadOnly(java.lang.String headerName)
public java.util.UUID getId()
public java.lang.Long getTimestamp()
public void setContentType(MimeType contentType)
public MimeType getContentType()
public void setReplyChannelName(java.lang.String replyChannelName)
public void setReplyChannel(MessageChannel replyChannel)
public java.lang.Object getReplyChannel()
public void setErrorChannelName(java.lang.String errorChannelName)
public void setErrorChannel(MessageChannel errorChannel)
public java.lang.Object getErrorChannel()
public java.lang.String getShortLogMessage(java.lang.Object payload)
payload
- the payload that corresponds to the headers.public java.lang.String getDetailedLogMessage(java.lang.Object payload)
payload
- the payload that corresponds to the headers.protected java.lang.String getShortPayloadLogMessage(java.lang.Object payload)
protected java.lang.String getDetailedPayloadLogMessage(java.lang.Object payload)
protected boolean isReadableContentType()
public java.lang.String toString()
toString
in class java.lang.Object
public static <T extends MessageHeaderAccessor> T getAccessor(Message<?> message, java.lang.Class<T> requiredType)
MessageHeaderAccessor
used to create the headers
of the given Message
, or null
if that's not available or if
its type does not match the required type.
This is for cases where the existence of an accessor is strongly expected (to be followed up with an assertion) or will created if not provided.
null
if nonepublic static <T extends MessageHeaderAccessor> T getAccessor(MessageHeaders messageHeaders, java.lang.Class<T> requiredType)
getAccessor(org.springframework.messaging.Message, Class)
with a MessageHeaders
instance instead of a Message
.
This is for cases when a full message may not have been created yet.
null
if nonepublic static MessageHeaderAccessor getMutableAccessor(Message<?> message)
MessageHeaderAccessor
for the given message attempting
to match the type of accessor used to create the message headers, or otherwise
wrapping the message with a MessageHeaderAccessor
instance.
This is for cases where a header needs to be updated in generic code while preserving the accessor type for downstream processing.
null
)