Skip to content

Commit

Permalink
The EmbeddedJsonHeadersMessageMapper cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
artembilan committed Nov 12, 2024
1 parent a96b5f0 commit a171ffd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2022 the original author or authors.
* Copyright 2017-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -42,15 +42,16 @@
/**
* For outbound messages, uses a message-aware Jackson object mapper to render the message
* as JSON. For messages with {@code byte[]} payloads, if rendered as JSON, Jackson
* performs Base64 conversion on the bytes. If the {@link #setRawBytes(boolean) rawBytes}
* property is true (default), the result has the form
* <headersLen><headers><payloadLen><payload>; with the headers
* performs Base64 conversion on the bytes. If payload is {@code byte[]} and
* the {@link #setRawBytes(boolean) rawBytes} property is true (default), the result has the form
* {@code [headersLen][headers][payloadLen][payload]}; with the headers
* rendered in JSON and the payload unchanged.
* Otherwise, message is fully serialized and deserialized with Jackson object mapper.
* <p>
* By default, all headers are included; you can provide simple patterns to specify a
* subset of headers.
* <p>
* If neither expected format is detected, or an error occurs during conversion, the
* If neither expected format is detected, nor an error occurs during conversion, the
* payload of the message is the original {@code byte[]}.
* <p>
* <b>IMPORTANT</b>
Expand All @@ -63,7 +64,9 @@
* "org.springframework.messaging.support",
* "org.springframework.integration.support",
* "org.springframework.integration.message",
* "org.springframework.integration.store"
* "org.springframework.integration.store",
* "org.springframework.integration.history",
* "org.springframework.integration.handler"
* </pre>
* <p>
* To add more packages, create an object mapper using
Expand Down Expand Up @@ -133,19 +136,19 @@ public EmbeddedJsonHeadersMessageMapper(ObjectMapper objectMapper, String... hea
/**
* For messages with {@code byte[]} payloads, if rendered as JSON, Jackson performs
* Base64 conversion on the bytes. If this property is true (default), the result has
* the form &lt;headersLen&gt;&lt;headers&gt;&lt;payloadLen&gt;&lt;payload&gt;; with
* the headers rendered in JSON and the payload unchanged. Set to false to render
* the bytes as base64.
* the form {@code [headersLen][headers][payloadLen][payload]}; with
* the headers rendered in JSON and the payload unchanged.
* Set to {@code false} to render the bytes as base64.
* @param rawBytes false to encode as base64.
*/
public void setRawBytes(boolean rawBytes) {
this.rawBytes = rawBytes;
}

/**
* Set to true to make the header name pattern match case sensitive.
* Set to true to make the header name pattern match case-sensitive.
* Default false.
* @param caseSensitive true to make case sensitive.
* @param caseSensitive true to make case-sensitive.
*/
public void setCaseSensitive(boolean caseSensitive) {
this.caseSensitive = caseSensitive;
Expand All @@ -162,8 +165,8 @@ public byte[] fromMessage(Message<?> message) {
? message.getHeaders()
: pruneHeaders(message.getHeaders());

if (this.rawBytes && message.getPayload() instanceof byte[]) {
return fromBytesPayload((byte[]) message.getPayload(), headersToEncode);
if (this.rawBytes && message.getPayload() instanceof byte[] bytes) {
return fromBytesPayload(bytes, headersToEncode);
}
else {
Message<?> messageToEncode = message;
Expand All @@ -182,8 +185,8 @@ public byte[] fromMessage(Message<?> message) {
try {
return this.objectMapper.writeValueAsBytes(messageToEncode);
}
catch (JsonProcessingException e) {
throw new UncheckedIOException(e);
catch (JsonProcessingException ex) {
throw new UncheckedIOException(ex);
}
}
}
Expand Down Expand Up @@ -212,8 +215,8 @@ private byte[] fromBytesPayload(byte[] payload, Map<String, Object> headersToEnc
buffer.put(payload);
return buffer.array();
}
catch (JsonProcessingException e) {
throw new UncheckedIOException(e);
catch (JsonProcessingException ex) {
throw new UncheckedIOException(ex);
}
}

Expand All @@ -223,15 +226,15 @@ public Message<?> toMessage(byte[] bytes, @Nullable Map<String, Object> headers)
try {
message = decodeNativeFormat(bytes, headers);
}
catch (@SuppressWarnings("unused") Exception e) {
this.logger.debug("Failed to decode native format", e);
catch (@SuppressWarnings("unused") Exception ex) {
this.logger.debug("Failed to decode native format", ex);
}
if (message == null) {
try {
message = (Message<?>) this.objectMapper.readValue(bytes, Object.class);
}
catch (Exception e) {
this.logger.debug("Failed to decode JSON", e);
catch (Exception ex) {
this.logger.debug("Failed to decode JSON", ex);
}
}
if (message != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2022 the original author or authors.
* Copyright 2017-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,7 +22,7 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
Expand Down Expand Up @@ -57,7 +57,7 @@ public void testEmbedSome() throws Exception {

ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> encodedMessageToCheck =
objectMapper.readValue(encodedMessage, new TypeReference<Map<String, Object>>() {
objectMapper.readValue(encodedMessage, new TypeReference<>() {

});

Expand Down Expand Up @@ -140,7 +140,7 @@ public void testDontMapIdButOthers() throws Exception {

ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> encodedMessageToCheck =
objectMapper.readValue(encodedMessage, new TypeReference<Map<String, Object>>() {
objectMapper.readValue(encodedMessage, new TypeReference<>() {

});

Expand Down

0 comments on commit a171ffd

Please sign in to comment.