This is an automated email from the ASF dual-hosted git repository. gnodet pushed a commit to branch fix/as2-issues in repository https://gitbox.apache.org/repos/asf/camel.git
commit c72b6129f4b16d92cdef3c4d385f4542d5cb5fd8 Author: Guillaume Nodet <[email protected]> AuthorDate: Mon Mar 9 11:04:27 2026 +0100 CAMEL-23066: Use UUID for globally unique AS2 Message-IDs Replace System.nanoTime() + SecureRandom with UUID.randomUUID() for Message-ID generation. UUID v4 provides 122 random bits (vs 64 from a single SecureRandom long) and is the standard Java approach for globally unique identifiers. Co-Authored-By: Claude Opus 4.6 <[email protected]> --- .../camel/component/as2/api/util/AS2Utils.java | 7 +-- .../camel/component/as2/api/util/AS2UtilsTest.java | 51 ++++++++++++++++++++++ 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/util/AS2Utils.java b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/util/AS2Utils.java index a4fb208dc73c..7f3c18c98a87 100644 --- a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/util/AS2Utils.java +++ b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/util/AS2Utils.java @@ -21,8 +21,8 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; import java.nio.charset.StandardCharsets; -import java.security.SecureRandom; import java.util.Iterator; +import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -55,8 +55,6 @@ public final class AS2Utils { public static final Pattern AS_NAME_PATTERN = Pattern.compile(AS2_NAME); - private static SecureRandom generator = new SecureRandom(); - private AS2Utils() { } @@ -88,8 +86,7 @@ public final class AS2Utils { * @return The generated message id. */ public static String createMessageId(String fqdn) { - /* Wall Clock Time in Nanoseconds */ /* 64 Bit Random Number */ /* Fully Qualified Domain Name */ - return "<" + Long.toString(System.nanoTime(), 36) + "." + Long.toString(generator.nextLong(), 36) + "@" + fqdn + ">"; + return "<" + UUID.randomUUID() + "@" + fqdn + ">"; } /** diff --git a/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/util/AS2UtilsTest.java b/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/util/AS2UtilsTest.java new file mode 100644 index 000000000000..9e4970e362b1 --- /dev/null +++ b/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/util/AS2UtilsTest.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.util; + +import java.util.HashSet; +import java.util.Set; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class AS2UtilsTest { + + @Test + void createMessageIdShouldBeUnique() { + Set<String> ids = new HashSet<>(); + for (int i = 0; i < 1000; i++) { + String id = AS2Utils.createMessageId("example.com"); + assertTrue(ids.add(id), "Duplicate message ID: " + id); + } + } + + @Test + void createMessageIdShouldContainFqdn() { + String id = AS2Utils.createMessageId("test.example.org"); + assertTrue(id.contains("@test.example.org>"), "Message ID should contain FQDN"); + assertTrue(id.startsWith("<"), "Message ID should start with <"); + assertTrue(id.endsWith(">"), "Message ID should end with >"); + } + + @Test + void createMessageIdShouldBeRfc2822Format() { + String id = AS2Utils.createMessageId("server.example.com"); + // RFC 2822 Message-ID format: <unique-part@fqdn> + assertTrue(id.matches("<[^@]+@server\\.example\\.com>"), "Message ID should match RFC 2822 format: " + id); + } +}
