This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 352393f9574 [CAMEL-22202] Fixes bug with edi content serialization
(#18481)
352393f9574 is described below
commit 352393f9574359323f586b602220f7c16be4d98e
Author: Jürgen Link <[email protected]>
AuthorDate: Thu Jun 26 16:20:17 2025 +0200
[CAMEL-22202] Fixes bug with edi content serialization (#18481)
* Add unit test for serializing different line endings with an
ApplicationEntity instance
* Do not modify line endings when serializing ediContent
* Format code properly
* Format imports properly
* Remove bogus replacement of LF -> CRLF in unit test, update expected
digest
---------
Co-authored-by: jlink <[email protected]>
---
.../as2/api/entity/ApplicationEntity.java | 2 +-
.../as2/api/entity/ApplicationEntityTest.java | 27 ++++++++++++++++++++++
.../camel/component/as2/api/util/MicUtilsTest.java | 9 +++-----
3 files changed, 31 insertions(+), 7 deletions(-)
diff --git
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/ApplicationEntity.java
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/ApplicationEntity.java
index 52f008521fa..e75fceb3d70 100644
---
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/ApplicationEntity.java
+++
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/entity/ApplicationEntity.java
@@ -68,7 +68,7 @@ public abstract class ApplicationEntity extends MimeEntity {
public void writeTo(OutputStream outstream) throws IOException {
NoCloseOutputStream ncos = new NoCloseOutputStream(outstream);
try (CanonicalOutputStream canonicalOutstream = new
CanonicalOutputStream(ncos, StandardCharsets.US_ASCII.name());
- OutputStream transferEncodedStream =
EntityUtils.encode(canonicalOutstream, getContentTransferEncodingValue())) {
+ OutputStream transferEncodedStream = EntityUtils.encode(ncos,
getContentTransferEncodingValue())) {
// Write out mime part headers if this is not the main body of
message.
if (!isMainBody()) {
diff --git
a/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/entity/ApplicationEntityTest.java
b/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/entity/ApplicationEntityTest.java
index 3a410244750..5374a6f8701 100644
---
a/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/entity/ApplicationEntityTest.java
+++
b/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/entity/ApplicationEntityTest.java
@@ -18,10 +18,15 @@ package org.apache.camel.component.as2.api.entity;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
+import java.util.stream.Stream;
import org.apache.hc.core5.http.ContentType;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -47,4 +52,26 @@ public class ApplicationEntityTest {
assertEquals(messageBytes.length, actualBytes.length,
"The byte length should match the length of the UTF-8 encoded
message.");
}
+
+ @ParameterizedTest(name = "writing content {1}")
+ @MethodSource("test_write_line_endings")
+ void test_write_carriage_return(String content, String description) throws
IOException {
+
+ ContentType contentType = ContentType.create("text/plain",
StandardCharsets.UTF_8);
+ ApplicationEntity applicationEntity = new
ApplicationEntity(content.getBytes(), contentType, "binary", true, null) {
+ @Override
+ public void close() throws IOException {
+ }
+ };
+ OutputStream outputStream = new ByteArrayOutputStream();
+ applicationEntity.writeTo(outputStream);
+ assertArrayEquals(outputStream.toString().getBytes(),
content.getBytes());
+ }
+
+ private static Stream<Arguments> test_write_line_endings() {
+ return Stream.of(
+ Arguments.of("\r", "CR"),
+ Arguments.of("\n", "LF"),
+ Arguments.of("\r\n", "CRLF"));
+ }
}
diff --git
a/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/util/MicUtilsTest.java
b/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/util/MicUtilsTest.java
index 51d088ee15d..23f67a8f4c0 100644
---
a/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/util/MicUtilsTest.java
+++
b/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/util/MicUtilsTest.java
@@ -104,7 +104,7 @@ public class MicUtilsTest {
+
"UNZ+1+00000000000778'";
private static final String EXPECTED_MESSAGE_DIGEST_ALGORITHM = "sha1";
- private static final String EXPECTED_ENCODED_MESSAGE_DIGEST =
"XUt+ug5GEDD0X9+Nv8DGYZZThOQ=";
+ private static final String EXPECTED_ENCODED_MESSAGE_DIGEST =
"0mGTGdBjQtu8VQ52506Coi0xHbc=";
@BeforeEach
public void setUp() {
@@ -162,8 +162,7 @@ public class MicUtilsTest {
// calculate the MIC of the EDI message directly for comparison
String expectedDigest = new ReceivedContentMic(
"sha1", MicUtils.createMic(
- // the entity parser appends 'CR' and 'LF' for each
line
- EDI_MESSAGE_WITH_NON_ASCII.replaceAll("\n",
"\r\n").getBytes(StandardCharsets.UTF_8), "sha1"))
+
EDI_MESSAGE_WITH_NON_ASCII.getBytes(StandardCharsets.UTF_8), "sha1"))
.getEncodedMessageDigest();
assertEquals(expectedDigest,
receivedContentMic.getEncodedMessageDigest(), "Unexpected encoded message
digest value");
@@ -200,9 +199,7 @@ public class MicUtilsTest {
private String getMicContent(String content, String algorithm) {
return new String(
Base64.getEncoder().encode(
- createMic(content
- .replaceAll("\\n", "\r\n")
- .getBytes(StandardCharsets.US_ASCII),
algorithm)),
+ createMic(content.getBytes(StandardCharsets.US_ASCII),
algorithm)),
StandardCharsets.US_ASCII);
}
}