Skip to content

Commit 0344524

Browse files
authored
GH-1152: Use contentEncoding in Message.toString()
Resolves #1152 * Add check for null properties.
1 parent 9e4ba4f commit 0344524

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

spring-amqp/src/main/java/org/springframework/amqp/core/Message.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -46,11 +46,13 @@ public class Message implements Serializable {
4646

4747
private static final long serialVersionUID = -7177590352110605597L;
4848

49-
private static final String ENCODING = Charset.defaultCharset().name();
49+
private static final String DEFAULT_ENCODING = Charset.defaultCharset().name();
5050

5151
private static final Set<String> whiteListPatterns = // NOSONAR lower case static
5252
new LinkedHashSet<>(Arrays.asList("java.util.*", "java.lang.*"));
5353

54+
private static String bodyEncoding = DEFAULT_ENCODING;
55+
5456
private final MessageProperties messageProperties;
5557

5658
private final byte[] body;
@@ -77,6 +79,17 @@ public static void addWhiteListPatterns(String... patterns) {
7779
whiteListPatterns.addAll(Arrays.asList(patterns));
7880
}
7981

82+
/**
83+
* Set the encoding to use in {@link #toString()} when converting the body if
84+
* there is no {@link MessageProperties#getContentEncoding() contentEncoding} message property present.
85+
* @param encoding the encoding to use.
86+
* @since 2.2.4
87+
*/
88+
public static void setDefaultEncoding(String encoding) {
89+
Assert.notNull(encoding, "'encoding' cannot be null");
90+
bodyEncoding = encoding;
91+
}
92+
8093
public byte[] getBody() {
8194
return this.body; //NOSONAR
8295
}
@@ -102,16 +115,21 @@ private String getBodyContentAsString() {
102115
return null;
103116
}
104117
try {
105-
String contentType = (this.messageProperties != null) ? this.messageProperties.getContentType() : null;
118+
boolean nullProps = this.messageProperties == null;
119+
String contentType = nullProps ? null : this.messageProperties.getContentType();
106120
if (MessageProperties.CONTENT_TYPE_SERIALIZED_OBJECT.equals(contentType)) {
107121
return SerializationUtils.deserialize(new ByteArrayInputStream(this.body), whiteListPatterns,
108122
ClassUtils.getDefaultClassLoader()).toString();
109123
}
124+
String encoding = nullProps ? null : this.messageProperties.getContentEncoding();
125+
if (encoding == null) {
126+
encoding = bodyEncoding;
127+
}
110128
if (MessageProperties.CONTENT_TYPE_TEXT_PLAIN.equals(contentType)
111129
|| MessageProperties.CONTENT_TYPE_JSON.equals(contentType)
112130
|| MessageProperties.CONTENT_TYPE_JSON_ALT.equals(contentType)
113131
|| MessageProperties.CONTENT_TYPE_XML.equals(contentType)) {
114-
return new String(this.body, ENCODING);
132+
return new String(this.body, encoding);
115133
}
116134
}
117135
catch (Exception e) {

spring-amqp/src/test/java/org/springframework/amqp/core/MessageTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -23,6 +23,7 @@
2323
import java.io.ObjectInputStream;
2424
import java.io.ObjectOutputStream;
2525
import java.io.Serializable;
26+
import java.nio.charset.StandardCharsets;
2627
import java.util.Collections;
2728
import java.util.Date;
2829

@@ -44,6 +45,15 @@ public void toStringForEmptyMessageBody() {
4445
assertThat(message.toString()).isNotNull();
4546
}
4647

48+
@Test
49+
public void properEncoding() {
50+
Message message = new Message("ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP".getBytes(StandardCharsets.UTF_16),
51+
new MessageProperties());
52+
message.getMessageProperties().setContentType(MessageProperties.CONTENT_TYPE_JSON);
53+
message.getMessageProperties().setContentEncoding("UTF-16");
54+
assertThat(message.toString()).contains("ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP");
55+
}
56+
4757
@Test
4858
public void toStringForNullMessageProperties() {
4959
Message message = new Message(new byte[0], null);

0 commit comments

Comments
 (0)