5555public abstract class AbstractJackson2MessageConverter extends AbstractMessageConverter
5656 implements BeanClassLoaderAware , SmartMessageConverter {
5757
58- protected final Log log = LogFactory .getLog (getClass ()); // NOSONAR protected
59-
6058 /**
6159 * The charset used when converting {@link String} to/from {@code byte[]}.
6260 */
6361 public static final Charset DEFAULT_CHARSET = StandardCharsets .UTF_8 ;
6462
65- protected final ObjectMapper objectMapper ; // NOSONAR protected
63+ protected final Log log = LogFactory .getLog (getClass ());
64+
65+ protected final ObjectMapper objectMapper ;
6666
6767 /**
6868 * The supported content type; only the subtype is checked when decoding, e.g.
@@ -119,7 +119,6 @@ protected AbstractJackson2MessageConverter(ObjectMapper objectMapper, MimeType c
119119 ((DefaultJackson2JavaTypeMapper ) this .javaTypeMapper ).setTrustedPackages (trustedPackages );
120120 }
121121
122-
123122 /**
124123 * Get the supported content type; only the subtype is checked when decoding, e.g.
125124 * */json, */xml. If this contains a charset parameter, when encoding, the
@@ -133,7 +132,6 @@ protected MimeType getSupportedContentType() {
133132 return this .supportedContentType ;
134133 }
135134
136-
137135 /**
138136 * Set the supported content type; only the subtype is checked when decoding, e.g.
139137 * */json, */xml. If this contains a charset parameter, when encoding, the
@@ -174,8 +172,10 @@ public void setClassMapper(ClassMapper classMapper) {
174172 * @param defaultCharset The default charset.
175173 */
176174 public void setDefaultCharset (@ Nullable String defaultCharset ) {
177- this .defaultCharset = (defaultCharset != null ) ? Charset .forName (defaultCharset )
178- : DEFAULT_CHARSET ;
175+ this .defaultCharset =
176+ defaultCharset != null
177+ ? Charset .forName (defaultCharset )
178+ : DEFAULT_CHARSET ;
179179 this .charsetIsUtf8 = this .defaultCharset .equals (StandardCharsets .UTF_8 );
180180 }
181181
@@ -311,7 +311,6 @@ public Object fromMessage(Message message, @Nullable Object conversionHint) thro
311311 Object content = null ;
312312 MessageProperties properties = message .getMessageProperties ();
313313 String contentType = properties .getContentType ();
314- // NOSONAR Boolean complexity
315314 if (this .assumeSupportedContentType && contentType .equals (MessageProperties .DEFAULT_CONTENT_TYPE )
316315 || contentType .contains (this .supportedContentType .getSubtype ())) {
317316
@@ -335,7 +334,7 @@ public Object fromMessage(Message message, @Nullable Object conversionHint) thro
335334 return content ;
336335 }
337336
338- private String determineEncoding (MessageProperties properties , @ Nullable String contentType ) {
337+ private @ Nullable String determineEncoding (MessageProperties properties , @ Nullable String contentType ) {
339338 String encoding = properties .getContentEncoding ();
340339 if (encoding == null && contentType != null ) {
341340 try {
@@ -346,29 +345,23 @@ private String determineEncoding(MessageProperties properties, @Nullable String
346345 // Ignore
347346 }
348347 }
349- if (encoding == null ) {
350- encoding = this .supportedCTCharset != null ? this .supportedCTCharset : getDefaultCharset ();
351- }
352348 return encoding ;
353349 }
354350
355351 private Object doFromMessage (Message message , @ Nullable Object conversionHint , MessageProperties properties ,
356- String encoding ) {
352+ @ Nullable String encoding ) {
357353
358- Object content = null ;
359354 try {
360- content = convertContent (message , conversionHint , properties , encoding );
355+ return convertContent (message , conversionHint , properties , encoding );
361356 }
362- catch (IOException e ) {
363- throw new MessageConversionException (
364- "Failed to convert Message content" , e );
357+ catch (IOException ex ) {
358+ throw new MessageConversionException ("Failed to convert Message content" , ex );
365359 }
366- return content ;
367360 }
368361
369362 @ SuppressWarnings ("NullAway" ) // Dataflow analysis limitation
370- private Object convertContent (Message message , @ Nullable Object conversionHint , MessageProperties properties , String encoding )
371- throws IOException {
363+ private Object convertContent (Message message , @ Nullable Object conversionHint , MessageProperties properties ,
364+ @ Nullable String encoding ) throws IOException {
372365
373366 Object content = null ;
374367 JavaType inferredType = this .javaTypeMapper .getInferredType (properties );
@@ -383,20 +376,15 @@ else if (inferredType != null && this.alwaysConvertToInferredType) {
383376 if (content == null ) {
384377 if (conversionHint instanceof ParameterizedTypeReference <?> parameterizedTypeReference ) {
385378 content = convertBytesToObject (message .getBody (), encoding ,
386- this .objectMapper .getTypeFactory ().constructType (
387- parameterizedTypeReference .getType ()));
379+ this .objectMapper .getTypeFactory ().constructType (parameterizedTypeReference .getType ()));
388380 }
389381 else if (getClassMapper () == null ) {
390- JavaType targetJavaType = getJavaTypeMapper ()
391- .toJavaType (message .getMessageProperties ());
392- content = convertBytesToObject (message .getBody (),
393- encoding , targetJavaType );
382+ JavaType targetJavaType = getJavaTypeMapper ().toJavaType (message .getMessageProperties ());
383+ content = convertBytesToObject (message .getBody (), encoding , targetJavaType );
394384 }
395385 else {
396- Class <?> targetClass = getClassMapper ().toClass (// NOSONAR never null
397- message .getMessageProperties ());
398- content = convertBytesToObject (message .getBody (),
399- encoding , targetClass );
386+ Class <?> targetClass = getClassMapper ().toClass (message .getMessageProperties ());
387+ content = convertBytesToObject (message .getBody (), encoding , targetClass );
400388 }
401389 }
402390 return content ;
@@ -406,30 +394,36 @@ else if (getClassMapper() == null) {
406394 * Unfortunately, mapper.canDeserialize() always returns true (adds an AbstractDeserializer
407395 * to the cache); so all we can do is try a conversion.
408396 */
409- private @ Nullable Object tryConvertType (Message message , String encoding , JavaType inferredType ) {
397+ private @ Nullable Object tryConvertType (Message message , @ Nullable String encoding , JavaType inferredType ) {
410398 try {
411399 return convertBytesToObject (message .getBody (), encoding , inferredType );
412400 }
413- catch (Exception e ) {
414- this .log .trace ("Cannot create possibly abstract container contents; falling back to headers" , e );
401+ catch (Exception ex ) {
402+ this .log .trace ("Cannot create possibly abstract container contents; falling back to headers" , ex );
415403 return null ;
416404 }
417405 }
418406
419- private Object convertBytesToObject (byte [] body , String encoding , JavaType targetJavaType ) throws IOException {
420- if (this .supportedCTCharset != null ) { // Jackson will determine encoding
421- return this .objectMapper .readValue (body , targetJavaType );
422- }
423- String contentAsString = new String (body , encoding );
424- return this .objectMapper .readValue (contentAsString , targetJavaType );
407+ private Object convertBytesToObject (byte [] body , @ Nullable String encoding , Class <?> targetClass )
408+ throws IOException {
409+
410+ return convertBytesToObject (body , encoding , this .objectMapper .constructType (targetClass ));
425411 }
426412
427- private Object convertBytesToObject (byte [] body , String encoding , Class <?> targetClass ) throws IOException {
428- if (this .supportedCTCharset != null ) { // Jackson will determine encoding
429- return this .objectMapper .readValue (body , this .objectMapper .constructType (targetClass ));
413+ private Object convertBytesToObject (byte [] body , @ Nullable String encoding , JavaType targetJavaType )
414+ throws IOException {
415+
416+ String encodingToUse = encoding ;
417+
418+ if (encodingToUse == null ) {
419+ if (this .charsetIsUtf8 & this .supportedCTCharset == null ) {
420+ return this .objectMapper .readValue (body , targetJavaType );
421+ }
422+ encodingToUse = getDefaultCharset ();
430423 }
431- String contentAsString = new String (body , encoding );
432- return this .objectMapper .readValue (contentAsString , this .objectMapper .constructType (targetClass ));
424+
425+ String contentAsString = new String (body , encodingToUse );
426+ return this .objectMapper .readValue (contentAsString , targetJavaType );
433427 }
434428
435429 @ Override
@@ -449,8 +443,7 @@ protected Message createMessage(Object objectToConvert, MessageProperties messag
449443 bytes = this .objectMapper .writeValueAsBytes (objectToConvert );
450444 }
451445 else {
452- String jsonString = this .objectMapper
453- .writeValueAsString (objectToConvert );
446+ String jsonString = this .objectMapper .writeValueAsString (objectToConvert );
454447 String encoding = this .supportedCTCharset != null ? this .supportedCTCharset : getDefaultCharset ();
455448 bytes = jsonString .getBytes (encoding );
456449 }
@@ -465,16 +458,17 @@ protected Message createMessage(Object objectToConvert, MessageProperties messag
465458 messageProperties .setContentLength (bytes .length );
466459
467460 if (getClassMapper () == null ) {
468- JavaType type = this . objectMapper . constructType (
469- genericType == null ? objectToConvert .getClass () : genericType );
461+ JavaType type =
462+ this . objectMapper . constructType ( genericType == null ? objectToConvert .getClass () : genericType );
470463 if (genericType != null && !type .isContainerType ()
471464 && Modifier .isAbstract (type .getRawClass ().getModifiers ())) {
465+
472466 type = this .objectMapper .constructType (objectToConvert .getClass ());
473467 }
474468 getJavaTypeMapper ().fromJavaType (type , messageProperties );
475469 }
476470 else {
477- getClassMapper ().fromClass (objectToConvert .getClass (), messageProperties ); // NOSONAR never null
471+ getClassMapper ().fromClass (objectToConvert .getClass (), messageProperties );
478472 }
479473
480474 return new Message (bytes , messageProperties );
0 commit comments