/** * 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 */ public MessageHeaderAccessor(@Nullable Message<?> message) { this.headers = new MutableMessageHeaders(message != null ? message.getHeaders() : null); }
/** * Set the value for the given header name. * <p>If the provided value is {@code null}, the header will be removed. */ public void setHeader(String name, @Nullable Object value) { if (isReadOnly(name)) { throw new IllegalArgumentException("'" + name + "' header is read-only"); } verifyType(name, value); if (value != null) { // Modify header if necessary if (!ObjectUtils.nullSafeEquals(value, getHeader(name))) { this.modified = true; this.headers.getRawHeaders().put(name, value); } } else { // Remove header if available if (this.headers.containsKey(name)) { this.modified = true; this.headers.getRawHeaders().remove(name); } } }
/** * 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 */ @Nullable public Object getHeader(String headerName) { return this.headers.get(headerName); }
/** * A variation of {@link #getAccessor(org.springframework.messaging.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 */ @SuppressWarnings("unchecked") @Nullable public static <T extends MessageHeaderAccessor> T getAccessor( MessageHeaders messageHeaders, @Nullable Class<T> requiredType) { if (messageHeaders instanceof MutableMessageHeaders) { MutableMessageHeaders mutableHeaders = (MutableMessageHeaders) messageHeaders; MessageHeaderAccessor headerAccessor = mutableHeaders.getAccessor(); if (requiredType == null || requiredType.isInstance(headerAccessor)) { return (T) headerAccessor; } } return null; }
/** * Set the value for the given header name. * <p>If the provided value is {@code null}, the header will be removed. */ public void setHeader(String name, @Nullable Object value) { if (isReadOnly(name)) { throw new IllegalArgumentException("'" + name + "' header is read-only"); } verifyType(name, value); if (value != null) { // Modify header if necessary if (!ObjectUtils.nullSafeEquals(value, getHeader(name))) { this.modified = true; this.headers.getRawHeaders().put(name, value); } } else { // Remove header if available if (this.headers.containsKey(name)) { this.modified = true; this.headers.getRawHeaders().remove(name); } } }
/** * Set the value for the given header name. * <p>If the provided value is {@code null}, the header will be removed. */ public void setHeader(String name, @Nullable Object value) { if (isReadOnly(name)) { throw new IllegalArgumentException("'" + name + "' header is read-only"); } verifyType(name, value); if (value != null) { // Modify header if necessary if (!ObjectUtils.nullSafeEquals(value, getHeader(name))) { this.modified = true; this.headers.getRawHeaders().put(name, value); } } else { // Remove header if available if (this.headers.containsKey(name)) { this.modified = true; this.headers.getRawHeaders().remove(name); } } }
/** * A variation of {@link #getAccessor(org.springframework.messaging.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 */ @SuppressWarnings("unchecked") @Nullable public static <T extends MessageHeaderAccessor> T getAccessor( MessageHeaders messageHeaders, @Nullable Class<T> requiredType) { if (messageHeaders instanceof MutableMessageHeaders) { MutableMessageHeaders mutableHeaders = (MutableMessageHeaders) messageHeaders; MessageHeaderAccessor headerAccessor = mutableHeaders.getAccessor(); if (requiredType == null || requiredType.isInstance(headerAccessor)) { return (T) headerAccessor; } } return null; }
/** * 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 */ public void setLeaveMutable(boolean leaveMutable) { Assert.state(this.headers.isMutable(), "Already immutable"); this.leaveMutable = leaveMutable; }
public void setImmutable() { if (!this.mutable) { return; } if (getId() == null) { IdGenerator idGenerator = (MessageHeaderAccessor.this.idGenerator != null ? MessageHeaderAccessor.this.idGenerator : MessageHeaders.getIdGenerator()); UUID id = idGenerator.generateId(); if (id != MessageHeaders.ID_VALUE_NONE) { getRawHeaders().put(ID, id); } } if (getTimestamp() == null) { if (MessageHeaderAccessor.this.enableTimestamp) { getRawHeaders().put(TIMESTAMP, System.currentTimeMillis()); } } this.mutable = false; }
/** * 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 */ public void setLeaveMutable(boolean leaveMutable) { Assert.state(this.headers.isMutable(), "Already immutable"); this.leaveMutable = leaveMutable; }
public void setImmutable() { if (!this.mutable) { return; } if (getId() == null) { IdGenerator idGenerator = (MessageHeaderAccessor.this.idGenerator != null ? MessageHeaderAccessor.this.idGenerator : MessageHeaders.getIdGenerator()); UUID id = idGenerator.generateId(); if (id != MessageHeaders.ID_VALUE_NONE) { getRawHeaders().put(ID, id); } } if (getTimestamp() == null) { if (MessageHeaderAccessor.this.enableTimestamp) { getRawHeaders().put(TIMESTAMP, System.currentTimeMillis()); } } this.mutable = false; }
/** * 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 */ public static MessageHeaderAccessor getMutableAccessor(Message<?> message) { if (message.getHeaders() instanceof MutableMessageHeaders) { MutableMessageHeaders mutableHeaders = (MutableMessageHeaders) message.getHeaders(); MessageHeaderAccessor accessor = mutableHeaders.getAccessor(); return (accessor.isMutable() ? accessor : accessor.createAccessor(message)); } return new MessageHeaderAccessor(message); }
/** * 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 */ public static MessageHeaderAccessor getMutableAccessor(Message<?> message) { if (message.getHeaders() instanceof MutableMessageHeaders) { MutableMessageHeaders mutableHeaders = (MutableMessageHeaders) message.getHeaders(); MessageHeaderAccessor accessor = mutableHeaders.getAccessor(); return (accessor.isMutable() ? accessor : accessor.createAccessor(message)); } return new MessageHeaderAccessor(message); }
/** * Return a concise message for logging purposes. * @param payload the payload that corresponds to the headers. * @return the message */ public String getShortLogMessage(Object payload) { return "headers=" + this.headers.toString() + getShortPayloadLogMessage(payload); }
/** * 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(boolean)} * is used, this method is necessary to indicate explicitly when the * {@code MessageHeaders} instance should no longer be modified. * @since 4.1 */ public void setImmutable() { this.headers.setImmutable(); }
/** * 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 */ public MessageHeaderAccessor(@Nullable Message<?> message) { this.headers = new MutableMessageHeaders(message != null ? message.getHeaders() : null); }
/** * 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 */ @Nullable public Object getHeader(String headerName) { return this.headers.get(headerName); }
/** * Return a more detailed message for logging purposes. * @param payload the payload that corresponds to the headers. * @return the message */ public String getDetailedLogMessage(@Nullable Object payload) { return "headers=" + this.headers.toString() + getDetailedPayloadLogMessage(payload); }
/** * 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(boolean)} * is used, this method is necessary to indicate explicitly when the * {@code MessageHeaders} instance should no longer be modified. * @since 4.1 */ public void setImmutable() { this.headers.setImmutable(); }
/** * Return a more detailed message for logging purposes. * @param payload the payload that corresponds to the headers. * @return the message */ public String getDetailedLogMessage(@Nullable Object payload) { return "headers=" + this.headers.toString() + getDetailedPayloadLogMessage(payload); }