wink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r997522 - in /incubator/wink/trunk/wink-common/src: main/java/org/apache/wink/common/internal/providers/header/MediaTypeHeaderDelegate.java test/java/org/apache/wink/common/internal/providers/header/MediaTypeHeaderDelegateTest.java
Date Wed, 15 Sep 2010 21:59:34 GMT
Author: bluk
Date: Wed Sep 15 21:59:34 2010
New Revision: 997522

URL: http://svn.apache.org/viewvc?rev=997522&view=rev
Log:
Improve the tolerance of malformed media types

Thanks to Steve Miller via Eliezer Baram for reporting the issue.

See [WINK-315]

Modified:
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/header/MediaTypeHeaderDelegate.java
    incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/providers/header/MediaTypeHeaderDelegateTest.java

Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/header/MediaTypeHeaderDelegate.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/header/MediaTypeHeaderDelegate.java?rev=997522&r1=997521&r2=997522&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/header/MediaTypeHeaderDelegate.java
(original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/header/MediaTypeHeaderDelegate.java
Wed Sep 15 21:59:34 2010
@@ -36,9 +36,9 @@ public class MediaTypeHeaderDelegate imp
     private static final Logger                               logger    =
                                                                             LoggerFactory
                                                                                 .getLogger(MediaTypeHeaderDelegate.class);
-    private static final Pattern                              EQUALS    = Pattern.compile("=");
//$NON-NLS-1$
-    private static final Pattern                              SEMICOLON = Pattern.compile(";");
//$NON-NLS-1$
-    private static final Pattern                              SLASH     = Pattern.compile("/");
//$NON-NLS-1$
+    private static final Pattern                              EQUALS    = Pattern.compile("=");
                          //$NON-NLS-1$
+    private static final Pattern                              SEMICOLON = Pattern.compile(";");
                          //$NON-NLS-1$
+    private static final Pattern                              SLASH     = Pattern.compile("/");
                          //$NON-NLS-1$
     private static final SoftConcurrentMap<String, MediaType> cache     =
                                                                             new SoftConcurrentMap<String,
MediaType>();
 
@@ -77,8 +77,12 @@ public class MediaTypeHeaderDelegate imp
             if (all.length > 1) {
                 paramsMap = new LinkedHashMap<String, String>();
                 for (int i = 1; i < all.length; i++) {
-                    String[] param = EQUALS.split(all[i]);
-                    paramsMap.put(param[0].trim(), param[1].trim());
+                    if (all[i] != null && all[i].trim().length() > 0) {
+                        String[] param = EQUALS.split(all[i]);
+                        if(param.length == 2) {
+                            paramsMap.put(param[0].trim(), param[1].trim());
+                        }
+                    }
                 }
             }
         } catch (ArrayIndexOutOfBoundsException e) {

Modified: incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/providers/header/MediaTypeHeaderDelegateTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/providers/header/MediaTypeHeaderDelegateTest.java?rev=997522&r1=997521&r2=997522&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/providers/header/MediaTypeHeaderDelegateTest.java
(original)
+++ incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/providers/header/MediaTypeHeaderDelegateTest.java
Wed Sep 15 21:59:34 2010
@@ -104,4 +104,46 @@ public class MediaTypeHeaderDelegateTest
             // success
         }
     }
+
+    @Test
+    public void testToleranceOfMalformedMediaTypes() {
+        MediaType mt = MediaType.valueOf("text/html;;charset=utf-8");
+        assertEquals("text", mt.getType());
+        assertEquals("html", mt.getSubtype());
+        assertEquals(1, mt.getParameters().size());
+        assertEquals("utf-8", mt.getParameters().get("charset"));
+
+        mt = MediaType.valueOf("text/html; charset: UTF-8");
+        assertEquals("text", mt.getType());
+        assertEquals("html", mt.getSubtype());
+        assertEquals(0, mt.getParameters().size());
+
+        mt = MediaType.valueOf("text/html; charset=");
+        assertEquals("text", mt.getType());
+        assertEquals("html", mt.getSubtype());
+        assertEquals(0, mt.getParameters().size());
+
+        mt = MediaType.valueOf("text/html; $str_charset; charset=ISO-8859-1");
+        assertEquals("text", mt.getType());
+        assertEquals("html", mt.getSubtype());
+        assertEquals(1, mt.getParameters().size());
+        assertEquals("ISO-8859-1", mt.getParameters().get("charset"));
+
+        mt = MediaType.valueOf("text/html; UTF-8;charset=ISO-8859-1");
+        assertEquals("text", mt.getType());
+        assertEquals("html", mt.getSubtype());
+        assertEquals(1, mt.getParameters().size());
+        assertEquals("ISO-8859-1", mt.getParameters().get("charset"));
+
+        mt = MediaType.valueOf("text/html; utf-8");
+        assertEquals("text", mt.getType());
+        assertEquals("html", mt.getSubtype());
+        assertEquals(0, mt.getParameters().size());
+
+        mt = MediaType.valueOf("text/html; UTF-8;charset=UTF-8");
+        assertEquals("text", mt.getType());
+        assertEquals("html", mt.getSubtype());
+        assertEquals(1, mt.getParameters().size());
+        assertEquals("UTF-8", mt.getParameters().get("charset"));
+    }
 }



Mime
View raw message