A constructor to create new headers.
A constructor accepting the headers of an existing message to copy. @param message a message to copy the headers from, or {@code null} if none
Copy the name-value pairs from the provided Map. <p>This operation will overwrite any existing values. Use {@link #copyHeadersIfAbsent(Map)} to avoid overwriting values.
Copy the name-value pairs from the provided Map. <p>This operation will <em>not</em> overwrite any existing values.
Build a 'nested' accessor for the given message. @param message the message to build a new accessor for @return the nested accessor (typically a specific subclass)
Return a more detailed message for logging purposes. @param payload the payload that corresponds to the headers. @return the message
Retrieve the value for the header with the given name. @param headerName the name of the header @return the associated value, or {@code null} if none found
Return the underlying {@code MessageHeaders} instance. <p>Unless {@link #setLeaveMutable()} was set to {@code true}, after this call, the headers are immutable and this accessor can no longer modify them. <p>This method always returns the same {@code MessageHeaders} instance if invoked multiples times. To obtain a copy of the underlying headers, use {@link #toMessageHeaders()} or {@link #toMap()} instead. @since 4.1
Return a concise message for logging purposes. @param payload the payload that corresponds to the headers. @return the message
Check whether the underlying message headers have been marked as modified. @return {@code true} if the flag has been set, {@code false} otherwise
Whether the underlying headers can still be modified. @since 4.1
Remove the value for the given header name.
Removes all headers provided via array of 'headerPatterns'. <p>As the name suggests, array may contain simple matching patterns for header names. Supported pattern styles are: "xxx*", "*xxx", "*xxx*" and "xxx*yyy".
A package private mechanism to enables the automatic addition of the {@link hunt.stomp.MessageHeaders#TIMESTAMP} header. <p>By default, this property is set to {@code false}. @see IdTimestampMessageHeaderInitializer
Set the value for the given header name. <p>If the provided value is {@code null}, the header will be removed.
Set the value for the given header name only if the header name is not already associated with a value.
A package-private mechanism to configure the IdGenerator strategy to use. <p>By default this property is not set in which case the default IdGenerator in {@link hunt.stomp.MessageHeaders} is used. @see IdTimestampMessageHeaderInitializer
By default when {@link #getMessageHeaders()} is called, {@code "this"} {@code MessageHeaderAccessor} instance can no longer be used to modify the underlying message headers. However if {@link #setLeaveMutable()} is used, this method is necessary to indicate explicitly when the {@code MessageHeaders} instance should no longer be modified. @since 4.1
By default when {@link #getMessageHeaders()} is called, {@code "this"} {@code MessageHeaderAccessor} instance can no longer be used to modify the underlying message headers and the returned {@code MessageHeaders} is immutable. <p>However when this is set to {@code true}, the returned (underlying) {@code MessageHeaders} instance remains mutable. To make further modifications continue to use the same accessor instance or re-obtain it via:<br> {@link MessageHeaderAccessor#getAccessor(Message, Class) MessageHeaderAccessor.getAccessor(Message, Class)} <p>When modifications are complete use {@link #setImmutable()} to prevent further changes. The intended use case for this mechanism is initialization of a Message within a single thread. <p>By default this is set to {@code false}. @since 4.1
Mark the underlying message headers as modified. @param modified typically {@code true}, or {@code false} to reset the flag @since 4.1
Return a copy of the underlying header values as a plain {@link Map} object. <p>This method can be invoked many times, with modifications in between where each new call returns a fresh copy of the current header values.
Return a copy of the underlying header values as a {@link MessageHeaders} object. <p>This method can be invoked many times, with modifications in between where each new call returns a fresh copy of the current header values. @since 4.1
Return the original {@code MessageHeaderAccessor} used to create the headers of the given {@code Message}, or {@code null} if that's not available or if its type does not match the required type. <p>This is for cases where the existence of an accessor is strongly expected (followed up with an assertion) or where an accessor will be created otherwise. @param message the message to get an accessor for @param requiredType the required accessor type (or {@code null} for any) @return an accessor instance of the specified type, or {@code null} if none @since 4.1
A variation of {@link #getAccessor(hunt.stomp.Message, Class)} with a {@code MessageHeaders} instance instead of a {@code Message}. <p>This is for cases when a full message may not have been created yet. @param messageHeaders the message headers to get an accessor for @param requiredType the required accessor type (or {@code null} for any) @return an accessor instance of the specified type, or {@code null} if none @since 4.1
Return a mutable {@code 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 {@code MessageHeaderAccessor} instance. <p>This is for cases where a header needs to be updated in generic code while preserving the accessor type for downstream processing. @return an accessor of the required type (never {@code null}) @since 4.1
The default charset used for headers.
A base for classes providing strongly typed getters and setters as well as behavior around specific categories of headers (e.g. STOMP headers). Supports creating new headers, modifying existing headers (when still mutable), or copying and modifying existing headers.
<p>The method {@link #getMessageHeaders()} provides access to the underlying, fully-prepared {@link MessageHeaders} that can then be used as-is (i.e. without copying) to create a single message as follows:
<pre class="code"> MessageHeaderAccessor accessor = new MessageHeaderAccessor(); accessor.setHeader("foo", "bar"); Message message = MessageHelper.createMessage("payload", accessor.getMessageHeaders()); </pre>
<p>After the above, by default the {@code MessageHeaderAccessor} becomes immutable. However it is possible to leave it mutable for further initialization in the same thread, for example:
<pre class="code"> MessageHeaderAccessor accessor = new MessageHeaderAccessor(); accessor.setHeader("foo", "bar"); accessor.setLeaveMutable(true); Message message = MessageHelper.createMessage("payload", accessor.getMessageHeaders());
// later on in the same thread...
MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message); accessor.setHeader("bar", "baz"); accessor.setImmutable(); </pre>
<p>The method {@link #toMap()} returns a copy of the underlying headers. It can be used to prepare multiple messages from the same {@code MessageHeaderAccessor} instance: <pre class="code"> 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(); </pre>
<p>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 {@code MessageHeaderAccessor} per message:
<pre class="code"> MessageHeaderAccessor accessor1 = new MessageHeaderAccessor(); accessor.set("foo", "bar1"); Message message1 = MessageHelper.createMessage("payload", accessor1.getMessageHeaders());
MessageHeaderAccessor accessor2 = new MessageHeaderAccessor(); accessor.set("foo", "bar2"); Message message2 = MessageHelper.createMessage("payload", accessor2.getMessageHeaders());
MessageHeaderAccessor accessor3 = new MessageHeaderAccessor(); accessor.set("foo", "bar3"); Message message3 = MessageHelper.createMessage("payload", accessor3.getMessageHeaders()); </pre>
<p>Note that the above examples aim to demonstrate the general idea of using header accessors. The most likely usage however is through subclasses.
@author Rossen Stoyanchev @author Juergen Hoeller @since 4.0