You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by bu...@apache.org on 2019/12/17 11:53:49 UTC

[cxf] branch master updated: cxf-core: use AtomicInteger in AttachmentUtil

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

buhhunyx pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/master by this push:
     new 441c222  cxf-core: use AtomicInteger in AttachmentUtil
441c222 is described below

commit 441c22291e9c9dbc223c33af87e441c733c8870a
Author: Alexey Markevich <bu...@gmail.com>
AuthorDate: Tue Dec 17 14:53:08 2019 +0300

    cxf-core: use AtomicInteger in AttachmentUtil
---
 .../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 a9997ea..3a082b0 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;
@@ -64,7 +65,7 @@ import org.apache.cxf.message.MessageUtils;
 public final class AttachmentUtil {
     public static final String BODY_ATTACHMENT_ID = "root.message@cxf.apache.org";
 
-    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();
@@ -195,9 +196,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();
@@ -210,7 +209,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);
+    }
+
+}