Skip to content

Commit dbd353b

Browse files
committed
MessageHeaderAccessor defensively checks id, timestamp and contentType values
Issue: SPR-12730
1 parent c7a456c commit dbd353b

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

spring-messaging/src/main/java/org/springframework/messaging/MessageHeaders.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -70,16 +70,8 @@
7070
*/
7171
public class MessageHeaders implements Map<String, Object>, Serializable {
7272

73-
private static final long serialVersionUID = 7035068984263400920L;
74-
75-
private static final Log logger = LogFactory.getLog(MessageHeaders.class);
76-
7773
public static final UUID ID_VALUE_NONE = new UUID(0,0);
7874

79-
private static volatile IdGenerator idGenerator = null;
80-
81-
private static final IdGenerator defaultIdGenerator = new AlternativeJdkIdGenerator();
82-
8375
/**
8476
* The key for the Message ID. This is an automatically generated UUID and
8577
* 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 {
9082

9183
public static final String TIMESTAMP = "timestamp";
9284

85+
public static final String CONTENT_TYPE = "contentType";
86+
9387
public static final String REPLY_CHANNEL = "replyChannel";
9488

9589
public static final String ERROR_CHANNEL = "errorChannel";
9690

97-
public static final String CONTENT_TYPE = "contentType";
91+
92+
private static final long serialVersionUID = 7035068984263400920L;
93+
94+
private static final Log logger = LogFactory.getLog(MessageHeaders.class);
95+
96+
private static final IdGenerator defaultIdGenerator = new AlternativeJdkIdGenerator();
97+
98+
private static volatile IdGenerator idGenerator = null;
9899

99100

100101
private final Map<String, Object> headers;

spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -418,11 +418,31 @@ protected boolean isReadOnly(String headerName) {
418418
// Specific header accessors
419419

420420
public UUID getId() {
421-
return (UUID) getHeader(MessageHeaders.ID);
421+
Object value = getHeader(MessageHeaders.ID);
422+
if (value == null) {
423+
return null;
424+
}
425+
return (value instanceof UUID ? (UUID) value : UUID.fromString(value.toString()));
422426
}
423427

424428
public Long getTimestamp() {
425-
return (Long) getHeader(MessageHeaders.TIMESTAMP);
429+
Object value = getHeader(MessageHeaders.TIMESTAMP);
430+
if (value == null) {
431+
return null;
432+
}
433+
return (value instanceof Long ? (Long) value : Long.parseLong(value.toString()));
434+
}
435+
436+
public void setContentType(MimeType contentType) {
437+
setHeader(MessageHeaders.CONTENT_TYPE, contentType);
438+
}
439+
440+
public MimeType getContentType() {
441+
Object value = getHeader(MessageHeaders.CONTENT_TYPE);
442+
if (value == null) {
443+
return null;
444+
}
445+
return (value instanceof MimeType ? (MimeType) value : MimeType.valueOf(value.toString()));
426446
}
427447

428448
public void setReplyChannelName(String replyChannelName) {
@@ -449,14 +469,6 @@ public Object getErrorChannel() {
449469
return getHeader(MessageHeaders.ERROR_CHANNEL);
450470
}
451471

452-
public void setContentType(MimeType contentType) {
453-
setHeader(MessageHeaders.CONTENT_TYPE, contentType);
454-
}
455-
456-
public MimeType getContentType() {
457-
return (MimeType) getHeader(MessageHeaders.CONTENT_TYPE);
458-
}
459-
460472

461473
// Log message stuff
462474

@@ -508,7 +520,7 @@ else if (payload instanceof byte[]) {
508520

509521
protected String getDetailedPayloadLogMessage(Object payload) {
510522
if (payload instanceof String) {
511-
return " payload=" + ((String) payload);
523+
return " payload=" + payload;
512524
}
513525
else if (payload instanceof byte[]) {
514526
byte[] bytes = (byte[]) payload;

0 commit comments

Comments
 (0)