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 ) {
0 commit comments