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 651ac27602d CAMEL-21403L: AS2 Component - Incorrect message length calculation when generating MIC for EDI messages with multi-byte characters 651ac27602d is described below commit 651ac27602df3766d0c291edc633da49988ece35 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sat Nov 9 08:44:02 2024 +0100 CAMEL-21403L: AS2 Component - Incorrect message length calculation when generating MIC for EDI messages with multi-byte characters --- .../as2/api/entity/ApplicationEntity.java | 2 +- .../as2/api/entity/ApplicationEntityTest.java | 51 ++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) 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 e8c081a3901..9a650e83d40 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 @@ -64,7 +64,7 @@ public abstract class ApplicationEntity extends MimeEntity { canonicalOutstream.writeln(); // ensure empty line between headers and body; RFC2046 - 5.1.1 } - transferEncodedStream.write(ediMessage.getBytes(getCharset()), 0, ediMessage.length()); + transferEncodedStream.write(ediMessage.getBytes(getCharset())); } catch (Exception e) { throw new IOException("Failed to write to output stream", e); } 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 new file mode 100644 index 00000000000..0ff19f305b8 --- /dev/null +++ b/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/entity/ApplicationEntityTest.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.as2.api.entity; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +import org.apache.hc.core5.http.ContentType; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ApplicationEntityTest { + + @Test + void checkWriteToWithMultiByteCharacter() throws IOException { + String message = "Test message with special char รณ"; + + ContentType contentType = ContentType.create("text/plain", StandardCharsets.UTF_8); + ApplicationEntity applicationEntity = new ApplicationEntity(message, contentType, "binary", true, null) { + @Override + public void close() throws IOException { + } + }; + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + applicationEntity.writeTo(outputStream); + + byte[] expectedBytes = message.getBytes(StandardCharsets.UTF_8); + byte[] actualBytes = outputStream.toByteArray(); + + assertArrayEquals(expectedBytes, actualBytes, "The output bytes should match the expected UTF-8 encoded bytes."); + assertEquals(expectedBytes.length, actualBytes.length, + "The byte length should match the length of the UTF-8 encoded message."); + } +}