Skip to content

Commit a171ffd

Browse files
committed
The EmbeddedJsonHeadersMessageMapper cleanups
1 parent a96b5f0 commit a171ffd

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

spring-integration-core/src/main/java/org/springframework/integration/support/json/EmbeddedJsonHeadersMessageMapper.java

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2022 the original author or authors.
2+
* Copyright 2017-2024 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.
@@ -42,15 +42,16 @@
4242
/**
4343
* For outbound messages, uses a message-aware Jackson object mapper to render the message
4444
* as JSON. For messages with {@code byte[]} payloads, if rendered as JSON, Jackson
45-
* performs Base64 conversion on the bytes. If the {@link #setRawBytes(boolean) rawBytes}
46-
* property is true (default), the result has the form
47-
* <headersLen><headers><payloadLen><payload>; with the headers
45+
* performs Base64 conversion on the bytes. If payload is {@code byte[]} and
46+
* the {@link #setRawBytes(boolean) rawBytes} property is true (default), the result has the form
47+
* {@code [headersLen][headers][payloadLen][payload]}; with the headers
4848
* rendered in JSON and the payload unchanged.
49+
* Otherwise, message is fully serialized and deserialized with Jackson object mapper.
4950
* <p>
5051
* By default, all headers are included; you can provide simple patterns to specify a
5152
* subset of headers.
5253
* <p>
53-
* If neither expected format is detected, or an error occurs during conversion, the
54+
* If neither expected format is detected, nor an error occurs during conversion, the
5455
* payload of the message is the original {@code byte[]}.
5556
* <p>
5657
* <b>IMPORTANT</b>
@@ -63,7 +64,9 @@
6364
* "org.springframework.messaging.support",
6465
* "org.springframework.integration.support",
6566
* "org.springframework.integration.message",
66-
* "org.springframework.integration.store"
67+
* "org.springframework.integration.store",
68+
* "org.springframework.integration.history",
69+
* "org.springframework.integration.handler"
6770
* </pre>
6871
* <p>
6972
* To add more packages, create an object mapper using
@@ -133,19 +136,19 @@ public EmbeddedJsonHeadersMessageMapper(ObjectMapper objectMapper, String... hea
133136
/**
134137
* For messages with {@code byte[]} payloads, if rendered as JSON, Jackson performs
135138
* Base64 conversion on the bytes. If this property is true (default), the result has
136-
* the form &lt;headersLen&gt;&lt;headers&gt;&lt;payloadLen&gt;&lt;payload&gt;; with
137-
* the headers rendered in JSON and the payload unchanged. Set to false to render
138-
* the bytes as base64.
139+
* the form {@code [headersLen][headers][payloadLen][payload]}; with
140+
* the headers rendered in JSON and the payload unchanged.
141+
* Set to {@code false} to render the bytes as base64.
139142
* @param rawBytes false to encode as base64.
140143
*/
141144
public void setRawBytes(boolean rawBytes) {
142145
this.rawBytes = rawBytes;
143146
}
144147

145148
/**
146-
* Set to true to make the header name pattern match case sensitive.
149+
* Set to true to make the header name pattern match case-sensitive.
147150
* Default false.
148-
* @param caseSensitive true to make case sensitive.
151+
* @param caseSensitive true to make case-sensitive.
149152
*/
150153
public void setCaseSensitive(boolean caseSensitive) {
151154
this.caseSensitive = caseSensitive;
@@ -162,8 +165,8 @@ public byte[] fromMessage(Message<?> message) {
162165
? message.getHeaders()
163166
: pruneHeaders(message.getHeaders());
164167

165-
if (this.rawBytes && message.getPayload() instanceof byte[]) {
166-
return fromBytesPayload((byte[]) message.getPayload(), headersToEncode);
168+
if (this.rawBytes && message.getPayload() instanceof byte[] bytes) {
169+
return fromBytesPayload(bytes, headersToEncode);
167170
}
168171
else {
169172
Message<?> messageToEncode = message;
@@ -182,8 +185,8 @@ public byte[] fromMessage(Message<?> message) {
182185
try {
183186
return this.objectMapper.writeValueAsBytes(messageToEncode);
184187
}
185-
catch (JsonProcessingException e) {
186-
throw new UncheckedIOException(e);
188+
catch (JsonProcessingException ex) {
189+
throw new UncheckedIOException(ex);
187190
}
188191
}
189192
}
@@ -212,8 +215,8 @@ private byte[] fromBytesPayload(byte[] payload, Map<String, Object> headersToEnc
212215
buffer.put(payload);
213216
return buffer.array();
214217
}
215-
catch (JsonProcessingException e) {
216-
throw new UncheckedIOException(e);
218+
catch (JsonProcessingException ex) {
219+
throw new UncheckedIOException(ex);
217220
}
218221
}
219222

@@ -223,15 +226,15 @@ public Message<?> toMessage(byte[] bytes, @Nullable Map<String, Object> headers)
223226
try {
224227
message = decodeNativeFormat(bytes, headers);
225228
}
226-
catch (@SuppressWarnings("unused") Exception e) {
227-
this.logger.debug("Failed to decode native format", e);
229+
catch (@SuppressWarnings("unused") Exception ex) {
230+
this.logger.debug("Failed to decode native format", ex);
228231
}
229232
if (message == null) {
230233
try {
231234
message = (Message<?>) this.objectMapper.readValue(bytes, Object.class);
232235
}
233-
catch (Exception e) {
234-
this.logger.debug("Failed to decode JSON", e);
236+
catch (Exception ex) {
237+
this.logger.debug("Failed to decode JSON", ex);
235238
}
236239
}
237240
if (message != null) {

spring-integration-core/src/test/java/org/springframework/integration/support/json/EmbeddedJsonHeadersMessageMapperTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2022 the original author or authors.
2+
* Copyright 2017-2024 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.
@@ -22,7 +22,7 @@
2222

2323
import com.fasterxml.jackson.core.type.TypeReference;
2424
import com.fasterxml.jackson.databind.ObjectMapper;
25-
import org.junit.Test;
25+
import org.junit.jupiter.api.Test;
2626

2727
import org.springframework.messaging.Message;
2828
import org.springframework.messaging.MessageHeaders;
@@ -57,7 +57,7 @@ public void testEmbedSome() throws Exception {
5757

5858
ObjectMapper objectMapper = new ObjectMapper();
5959
Map<String, Object> encodedMessageToCheck =
60-
objectMapper.readValue(encodedMessage, new TypeReference<Map<String, Object>>() {
60+
objectMapper.readValue(encodedMessage, new TypeReference<>() {
6161

6262
});
6363

@@ -140,7 +140,7 @@ public void testDontMapIdButOthers() throws Exception {
140140

141141
ObjectMapper objectMapper = new ObjectMapper();
142142
Map<String, Object> encodedMessageToCheck =
143-
objectMapper.readValue(encodedMessage, new TypeReference<Map<String, Object>>() {
143+
objectMapper.readValue(encodedMessage, new TypeReference<>() {
144144

145145
});
146146

0 commit comments

Comments
 (0)