You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cxf.apache.org by GitBox <gi...@apache.org> on 2018/12/17 17:49:36 UTC

[GitHub] amarkevich closed pull request #483: HttpHeaderHelper: String.replace instead of Pattern.matcher().replaceAll

amarkevich closed pull request #483: HttpHeaderHelper: String.replace instead of Pattern.matcher().replaceAll
URL: https://github.com/apache/cxf/pull/483
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/core/src/main/java/org/apache/cxf/helpers/HttpHeaderHelper.java b/core/src/main/java/org/apache/cxf/helpers/HttpHeaderHelper.java
index 0ed3e52605e..7d613d54004 100644
--- a/core/src/main/java/org/apache/cxf/helpers/HttpHeaderHelper.java
+++ b/core/src/main/java/org/apache/cxf/helpers/HttpHeaderHelper.java
@@ -21,12 +21,12 @@
 
 import java.nio.charset.Charset;
 import java.nio.charset.IllegalCharsetNameException;
+import java.nio.charset.StandardCharsets;
 import java.nio.charset.UnsupportedCharsetException;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.regex.Pattern;
 
 public final class HttpHeaderHelper {
     public static final String ACCEPT_ENCODING = "Accept-Encoding";
@@ -41,11 +41,10 @@
     public static final String CONNECTION = "Connection";
     public static final String CLOSE = "close";
     public static final String AUTHORIZATION = "Authorization";
-    private static final String ISO88591 = Charset.forName("ISO-8859-1").name();
+    static final String ISO88591 = StandardCharsets.ISO_8859_1.name();
 
     private static Map<String, String> internalHeaders = new HashMap<>();
     private static ConcurrentHashMap<String, String> encodings = new ConcurrentHashMap<>();
-    private static Pattern charsetPattern = Pattern.compile("\"|'");
 
     static {
         internalHeaders.put("Accept-Encoding", "accept-encoding");
@@ -112,8 +111,8 @@ public static String mapCharset(String enc, String deflt) {
         }
         // Charsets can be quoted. But it's quite certain that they can't have escaped quoted or
         // anything like that.
-        enc = charsetPattern.matcher(enc).replaceAll("").trim();
-        if ("".equals(enc)) {
+        enc = enc.replace('"', ' ').replace('\'', ' ').trim();
+        if (enc.isEmpty()) {
             return deflt;
         }
         String newenc = encodings.get(enc);
diff --git a/core/src/test/java/org/apache/cxf/common/util/Base64UtilityTest.java b/core/src/test/java/org/apache/cxf/common/util/Base64UtilityTest.java
index aa5c7fd0cb6..8fc81074598 100644
--- a/core/src/test/java/org/apache/cxf/common/util/Base64UtilityTest.java
+++ b/core/src/test/java/org/apache/cxf/common/util/Base64UtilityTest.java
@@ -30,17 +30,6 @@
 
 public class Base64UtilityTest extends Assert {
 
-    public Base64UtilityTest() {
-        super();
-    }
-
-    void assertEquals(byte[] b1, byte[] b2) {
-        assertEquals(b1.length, b2.length);
-        for (int x = 0; x < b1.length; x++) {
-            assertEquals(b1[x], b2[x]);
-        }
-    }
-
     @Test
     public void testEncodeMultipleChunks() throws Exception {
         final String text = "The true sign of intelligence is not knowledge but imagination.";
@@ -83,7 +72,7 @@ public void testEncodeDecodeChunk() throws Exception {
         encodedChars = Base64Utility.encodeChunk(bytes, 0, bytes.length);
         assertNotNull(encodedChars);
         byte[] bytesDecoded = Base64Utility.decodeChunk(encodedChars, 0, encodedChars.length);
-        assertEquals(bytes, bytesDecoded);
+        assertArrayEquals(bytes, bytesDecoded);
 
         //require padding
         bytes = new byte[99];
@@ -93,7 +82,7 @@ public void testEncodeDecodeChunk() throws Exception {
         encodedChars = Base64Utility.encodeChunk(bytes, 0, bytes.length);
         assertNotNull(encodedChars);
         bytesDecoded = Base64Utility.decodeChunk(encodedChars, 0, encodedChars.length);
-        assertEquals(bytes, bytesDecoded);
+        assertArrayEquals(bytes, bytesDecoded);
 
         //require padding
         bytes = new byte[98];
@@ -103,7 +92,7 @@ public void testEncodeDecodeChunk() throws Exception {
         encodedChars = Base64Utility.encodeChunk(bytes, 0, bytes.length);
         assertNotNull(encodedChars);
         bytesDecoded = Base64Utility.decodeChunk(encodedChars, 0, encodedChars.length);
-        assertEquals(bytes, bytesDecoded);
+        assertArrayEquals(bytes, bytesDecoded);
 
         //require padding
         bytes = new byte[97];
@@ -113,7 +102,7 @@ public void testEncodeDecodeChunk() throws Exception {
         encodedChars = Base64Utility.encodeChunk(bytes, 0, bytes.length);
         assertNotNull(encodedChars);
         bytesDecoded = Base64Utility.decodeChunk(encodedChars, 0, encodedChars.length);
-        assertEquals(bytes, bytesDecoded);
+        assertArrayEquals(bytes, bytesDecoded);
 
 
         bytesDecoded = Base64Utility.decodeChunk(new char[3], 0, 3);
@@ -129,15 +118,10 @@ public void testEncodeDecodeString() throws Exception {
         assertEquals(in, encoded);
     }
 
-    @Test
+    @Test(expected = Base64Exception.class)
     public void testDecodeInvalidString() throws Exception {
-        try {
-            String in = "QWxhZGRpbjpcGVuIHNlc2FtZQ==";
-            Base64Utility.decode(in);
-            fail("This test should be fail");
-        } catch (Base64Exception e) {
-            //nothing to do
-        }
+        String in = "QWxhZGRpbjpcGVuIHNlc2FtZQ==";
+        Base64Utility.decode(in);
     }
 
     @Test
@@ -154,7 +138,7 @@ public void testEncodeDecodeStreams() throws Exception {
                              0,
                              encodedString.length(),
                              bout2);
-        assertEquals(bytes, bout2.toByteArray());
+        assertArrayEquals(bytes, bout2.toByteArray());
 
 
         String in = "QWxhZGRpbjpvcGVuIHNlc2FtZQ==";
diff --git a/core/src/test/java/org/apache/cxf/helpers/HttpHeaderHelperTest.java b/core/src/test/java/org/apache/cxf/helpers/HttpHeaderHelperTest.java
index 09112defdca..4d8c570698a 100644
--- a/core/src/test/java/org/apache/cxf/helpers/HttpHeaderHelperTest.java
+++ b/core/src/test/java/org/apache/cxf/helpers/HttpHeaderHelperTest.java
@@ -19,11 +19,12 @@
 
 package org.apache.cxf.helpers;
 
-import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
 
 /**
  *
@@ -32,25 +33,50 @@
 
     @Test
     public void testMapCharset() {
-        String cs = HttpHeaderHelper.mapCharset("utf-8");
-        assertEquals(Charset.forName("utf-8").name(), cs);
+        final String charset = StandardCharsets.UTF_8.name();
+
+        String cs = HttpHeaderHelper.mapCharset(charset);
+        assertEquals(charset, cs);
+
+        cs = HttpHeaderHelper.mapCharset("\"" + charset + "\"");
+        assertEquals(charset, cs);
+
+        cs = HttpHeaderHelper.mapCharset("'" + charset + "'");
+        assertEquals(charset, cs);
+
         cs = HttpHeaderHelper.mapCharset(null);
-        assertEquals("ISO-8859-1", cs);
-        cs = HttpHeaderHelper.mapCharset("\"utf-8\"");
-        assertEquals(Charset.forName("utf-8").name(), cs);
-        cs = HttpHeaderHelper.mapCharset("'utf-8'");
-        assertEquals(Charset.forName("utf-8").name(), cs);
+        assertEquals(HttpHeaderHelper.ISO88591, cs);
+
+        cs = HttpHeaderHelper.mapCharset("''");
+        assertEquals(HttpHeaderHelper.ISO88591, cs);
+
+        cs = HttpHeaderHelper.mapCharset("wrong-charset-name");
+        assertNull(cs);
     }
 
     @Test
     public void testEmptyCharset() {
         String cs = HttpHeaderHelper.mapCharset(HttpHeaderHelper.findCharset("foo/bar; charset="));
-        assertEquals("ISO-8859-1", cs);
+        assertEquals(HttpHeaderHelper.ISO88591, cs);
     }
+
     @Test
     public void testEmptyCharset2() {
         String cs = HttpHeaderHelper.mapCharset(HttpHeaderHelper.findCharset("foo/bar; charset=;"));
-        assertEquals("ISO-8859-1", cs);
+        assertEquals(HttpHeaderHelper.ISO88591, cs);
+    }
+
+    @Test
+    public void testNoCharset() {
+        String cs = HttpHeaderHelper.mapCharset(HttpHeaderHelper.findCharset("foo/bar"));
+        assertEquals(HttpHeaderHelper.ISO88591, cs);
+    }
+
+    @Test
+    public void testFindCharset() {
+        final String charset = StandardCharsets.UTF_8.name();
+        String cs = HttpHeaderHelper.findCharset("foo/bar; charset=" + charset);
+        assertEquals(charset, cs);
     }
 
 }
diff --git a/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSMessageUtils.java b/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSMessageUtils.java
index e30dec649ba..c2c47f1689b 100644
--- a/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSMessageUtils.java
+++ b/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSMessageUtils.java
@@ -128,18 +128,7 @@ private static void populateIncomingContext(JMSMessageHeadersType messageHeaders
 
 
     static String getEncoding(String ct) throws UnsupportedEncodingException {
-        String contentType = ct.toLowerCase();
-        String enc = null;
-
-        String[] tokens = contentType.split(";");
-        for (String token : tokens) {
-            int index = token.indexOf("charset=");
-            if (index >= 0) {
-                enc = token.substring(index + 8);
-                break;
-            }
-        }
-
+        String enc = HttpHeaderHelper.findCharset(ct);
         String normalizedEncoding = HttpHeaderHelper.mapCharset(enc, StandardCharsets.UTF_8.name());
         if (normalizedEncoding == null) {
             String m = new org.apache.cxf.common.i18n.Message("INVALID_ENCODING_MSG", LOG, new Object[] {
diff --git a/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSMessageUtilTest.java b/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSMessageUtilTest.java
index c9a11aed13e..fc0c2bb44d2 100644
--- a/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSMessageUtilTest.java
+++ b/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/JMSMessageUtilTest.java
@@ -19,7 +19,6 @@
 
 package org.apache.cxf.transport.jms;
 
-import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.nio.charset.StandardCharsets;
 
@@ -29,18 +28,17 @@
 public class JMSMessageUtilTest extends Assert {
 
     @Test
-    public void testGetEncoding() throws IOException {
-        assertEquals("Get the wrong encoding", JMSMessageUtils.getEncoding("text/xml; charset=utf-8"),
-                     StandardCharsets.UTF_8.name());
-        assertEquals("Get the wrong encoding", JMSMessageUtils.getEncoding("text/xml"),
-                     StandardCharsets.UTF_8.name());
-        assertEquals("Get the wrong encoding", JMSMessageUtils.getEncoding("text/xml; charset=GBK"), "GBK");
+    public void testGetEncoding() throws Exception {
+        assertEquals("Get the wrong encoding", StandardCharsets.UTF_8.name(),
+                JMSMessageUtils.getEncoding("text/xml; charset=utf-8"));
+        assertEquals("Get the wrong encoding", StandardCharsets.UTF_8.name(),
+                JMSMessageUtils.getEncoding("text/xml"));
+        assertEquals("Get the wrong encoding", "GBK", JMSMessageUtils.getEncoding("text/xml; charset=GBK"));
         try {
             JMSMessageUtils.getEncoding("text/xml; charset=asci");
             fail("Expect the exception here");
-        } catch (Exception ex) {
-            assertTrue("we should get the UnsupportedEncodingException here",
-                       ex instanceof UnsupportedEncodingException);
+        } catch (UnsupportedEncodingException ex) {
+            // we should get the UnsupportedEncodingException here
         }
     }
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services