This is an automated email from the ASF dual-hosted git repository.

coheigea pushed a commit to branch 3.3.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit 737c8b846f228e758a34716baecc598f4b8e8ec2
Author: Alexey Markevich <[email protected]>
AuthorDate: Tue Dec 17 14:53:08 2019 +0300

    cxf-core: use AtomicInteger in AttachmentUtil
    
    (cherry picked from commit 441c22291e9c9dbc223c33af87e441c733c8870a)
---
 .../main/java/org/apache/cxf/attachment/AttachmentUtil.java |  9 ++++-----
 .../java/org/apache/cxf/attachment/AttachmentUtilTest.java  | 13 ++++++++++++-
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java 
b/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java
index fba8cba..229867a 100644
--- a/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java
+++ b/core/src/main/java/org/apache/cxf/attachment/AttachmentUtil.java
@@ -43,6 +43,7 @@ import java.util.Map;
 import java.util.Random;
 import java.util.Set;
 import java.util.UUID;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import javax.activation.CommandInfo;
 import javax.activation.CommandMap;
@@ -65,7 +66,7 @@ import org.apache.cxf.message.MessageUtils;
 public final class AttachmentUtil {
     public static final String BODY_ATTACHMENT_ID = 
"[email protected]";
 
-    private static volatile int counter;
+    private static final AtomicInteger COUNTER = new AtomicInteger();
     private static final String ATT_UUID = UUID.randomUUID().toString();
 
     private static final Random BOUND_RANDOM = new Random();
@@ -196,9 +197,7 @@ public final class AttachmentUtil {
     public static String createContentID(String ns) throws 
UnsupportedEncodingException {
         // tend to change
         String cid = "cxf.apache.org";
-
-        String name = ATT_UUID + "-" + String.valueOf(++counter);
-        if (ns != null && (ns.length() > 0)) {
+        if (ns != null && !ns.isEmpty()) {
             try {
                 URI uri = new URI(ns);
                 String host = uri.getHost();
@@ -211,7 +210,7 @@ public final class AttachmentUtil {
                 cid = ns;
             }
         }
-        return URLEncoder.encode(name, StandardCharsets.UTF_8.name()) + "@"
+        return ATT_UUID + '-' + Integer.toString(COUNTER.incrementAndGet()) + 
"%40" // '@'
             + URLEncoder.encode(cid, StandardCharsets.UTF_8.name());
     }
 
diff --git 
a/core/src/test/java/org/apache/cxf/attachment/AttachmentUtilTest.java 
b/core/src/test/java/org/apache/cxf/attachment/AttachmentUtilTest.java
index 2d53be9..96f6717 100644
--- a/core/src/test/java/org/apache/cxf/attachment/AttachmentUtilTest.java
+++ b/core/src/test/java/org/apache/cxf/attachment/AttachmentUtilTest.java
@@ -18,9 +18,13 @@
  */
 package org.apache.cxf.attachment;
 
+import java.net.URLDecoder;
+import java.nio.charset.StandardCharsets;
+
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 public class AttachmentUtilTest {
 
@@ -115,4 +119,11 @@ public class AttachmentUtilTest {
         assertEquals("a=b.txt",
             
AttachmentUtil.getContentDispositionFileName("filename=\"a=b.txt\""));
     }
-}
\ No newline at end of file
+
+    @Test
+    public void testCreateContentID() throws Exception {
+        final String contentID = AttachmentUtil.createContentID(null);
+        assertTrue(URLDecoder.decode(contentID, 
StandardCharsets.UTF_8.name()).indexOf('@') > -1);
+    }
+
+}

Reply via email to