INT-1613 setHeader now throws an Exception for read-only headers (ID and TIMESTAMP)

This commit is contained in:
Mark Fisher
2010-11-13 08:27:22 -05:00
parent 060ea89b87
commit 4e3f5bef6e
3 changed files with 26 additions and 25 deletions

View File

@@ -88,6 +88,7 @@ public final class MessageBuilder<T> {
* Set the value for the given header name. If the provided value is <code>null</code>, the header will be removed.
*/
public MessageBuilder<T> setHeader(String headerName, Object headerValue) {
Assert.isTrue(!this.isReadOnly(headerName), "The '" + headerName + "' header is read-only.");
if (StringUtils.hasLength(headerName) && !headerName.equals(MessageHeaders.ID)
&& !headerName.equals(MessageHeaders.TIMESTAMP)) {
this.verifyType(headerName, headerValue);
@@ -135,7 +136,9 @@ public final class MessageBuilder<T> {
public MessageBuilder<T> copyHeaders(Map<String, ?> headersToCopy) {
Set<String> keys = headersToCopy.keySet();
for (String key : keys) {
this.setHeader(key, headersToCopy.get(key));
if (!this.isReadOnly(key)) {
this.setHeader(key, headersToCopy.get(key));
}
}
return this;
}
@@ -146,7 +149,9 @@ public final class MessageBuilder<T> {
public MessageBuilder<T> copyHeadersIfAbsent(Map<String, Object> headersToCopy) {
Set<String> keys = headersToCopy.keySet();
for (String key : keys) {
this.setHeaderIfAbsent(key, headersToCopy.get(key));
if (!this.isReadOnly(key)) {
this.setHeaderIfAbsent(key, headersToCopy.get(key));
}
}
return this;
}
@@ -251,6 +256,10 @@ public final class MessageBuilder<T> {
return new GenericMessage<T>(this.payload, this.headers);
}
private boolean isReadOnly(String headerName) {
return MessageHeaders.ID.equals(headerName) || MessageHeaders.TIMESTAMP.equals(headerName);
}
private void verifyType(String headerName, Object headerValue) {
if (headerName != null && headerValue != null) {
if (MessageHeaders.ID.equals(headerName)) {