MessageHeaderAccessor defensively checks id, timestamp and contentType values

Issue: SPR-12730
(cherry picked from commit dbd353b)
This commit is contained in:
Juergen Hoeller 2015-02-18 22:28:27 +01:00
parent d8269dd9fe
commit 55a14eb684
2 changed files with 35 additions and 22 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -70,16 +70,8 @@ import org.springframework.util.IdGenerator;
*/ */
public class MessageHeaders implements Map<String, Object>, Serializable { public class MessageHeaders implements Map<String, Object>, Serializable {
private static final long serialVersionUID = 7035068984263400920L;
private static final Log logger = LogFactory.getLog(MessageHeaders.class);
public static final UUID ID_VALUE_NONE = new UUID(0,0); public static final UUID ID_VALUE_NONE = new UUID(0,0);
private static volatile IdGenerator idGenerator = null;
private static final IdGenerator defaultIdGenerator = new AlternativeJdkIdGenerator();
/** /**
* The key for the Message ID. This is an automatically generated UUID and * The key for the Message ID. This is an automatically generated UUID and
* should never be explicitly set in the header map <b>except</b> in the * should never be explicitly set in the header map <b>except</b> in the
@ -90,11 +82,20 @@ public class MessageHeaders implements Map<String, Object>, Serializable {
public static final String TIMESTAMP = "timestamp"; public static final String TIMESTAMP = "timestamp";
public static final String CONTENT_TYPE = "contentType";
public static final String REPLY_CHANNEL = "replyChannel"; public static final String REPLY_CHANNEL = "replyChannel";
public static final String ERROR_CHANNEL = "errorChannel"; public static final String ERROR_CHANNEL = "errorChannel";
public static final String CONTENT_TYPE = "contentType";
private static final long serialVersionUID = 7035068984263400920L;
private static final Log logger = LogFactory.getLog(MessageHeaders.class);
private static final IdGenerator defaultIdGenerator = new AlternativeJdkIdGenerator();
private static volatile IdGenerator idGenerator = null;
private final Map<String, Object> headers; private final Map<String, Object> headers;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -418,11 +418,31 @@ public class MessageHeaderAccessor {
// Specific header accessors // Specific header accessors
public UUID getId() { public UUID getId() {
return (UUID) getHeader(MessageHeaders.ID); Object value = getHeader(MessageHeaders.ID);
if (value == null) {
return null;
}
return (value instanceof UUID ? (UUID) value : UUID.fromString(value.toString()));
} }
public Long getTimestamp() { public Long getTimestamp() {
return (Long) getHeader(MessageHeaders.TIMESTAMP); Object value = getHeader(MessageHeaders.TIMESTAMP);
if (value == null) {
return null;
}
return (value instanceof Long ? (Long) value : Long.parseLong(value.toString()));
}
public void setContentType(MimeType contentType) {
setHeader(MessageHeaders.CONTENT_TYPE, contentType);
}
public MimeType getContentType() {
Object value = getHeader(MessageHeaders.CONTENT_TYPE);
if (value == null) {
return null;
}
return (value instanceof MimeType ? (MimeType) value : MimeType.valueOf(value.toString()));
} }
public void setReplyChannelName(String replyChannelName) { public void setReplyChannelName(String replyChannelName) {
@ -449,14 +469,6 @@ public class MessageHeaderAccessor {
return getHeader(MessageHeaders.ERROR_CHANNEL); return getHeader(MessageHeaders.ERROR_CHANNEL);
} }
public void setContentType(MimeType contentType) {
setHeader(MessageHeaders.CONTENT_TYPE, contentType);
}
public MimeType getContentType() {
return (MimeType) getHeader(MessageHeaders.CONTENT_TYPE);
}
// Log message stuff // Log message stuff
@ -508,7 +520,7 @@ public class MessageHeaderAccessor {
protected String getDetailedPayloadLogMessage(Object payload) { protected String getDetailedPayloadLogMessage(Object payload) {
if (payload instanceof String) { if (payload instanceof String) {
return " payload=" + ((String) payload); return " payload=" + payload;
} }
else if (payload instanceof byte[]) { else if (payload instanceof byte[]) {
byte[] bytes = (byte[]) payload; byte[] bytes = (byte[]) payload;