diff --git a/org.springframework.web/src/main/java/org/springframework/http/MediaType.java b/org.springframework.web/src/main/java/org/springframework/http/MediaType.java index 465f21e6aa1..24d36aee1a0 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/MediaType.java +++ b/org.springframework.web/src/main/java/org/springframework/http/MediaType.java @@ -264,7 +264,7 @@ public class MediaType implements Comparable { String attribute = entry.getKey(); String value = entry.getValue(); checkParameters(attribute, value); - m.put(attribute, value); + m.put(attribute, unquote(value)); } this.parameters = Collections.unmodifiableMap(m); } @@ -293,10 +293,12 @@ public class MediaType implements Comparable { Assert.hasLength(value, "parameter value must not be empty"); checkToken(attribute); if (PARAM_QUALITY_FACTOR.equals(attribute)) { + value = unquote(value); double d = Double.parseDouble(value); Assert.isTrue(d >= 0D && d <= 1D, "Invalid quality value \"" + value + "\": should be between 0.0 and 1.0"); } else if (PARAM_CHARSET.equals(attribute)) { + value = unquote(value); Charset.forName(value); } else if (!isQuotedString(value)) { @@ -308,6 +310,13 @@ public class MediaType implements Comparable { return s.length() > 1 && s.startsWith("\"") && s.endsWith("\"") ; } + private String unquote(String s) { + if (s == null) { + return null; + } + return isQuotedString(s) ? s.substring(1, s.length() - 1) : s; + } + /** Return the primary type. */ public String getType() { return this.type; diff --git a/org.springframework.web/src/test/java/org/springframework/http/MediaTypeTests.java b/org.springframework.web/src/test/java/org/springframework/http/MediaTypeTests.java index d518ddb615a..3b177b30688 100644 --- a/org.springframework.web/src/test/java/org/springframework/http/MediaTypeTests.java +++ b/org.springframework.web/src/test/java/org/springframework/http/MediaTypeTests.java @@ -180,6 +180,15 @@ public class MediaTypeTests { assertEquals("Invalid charset", Charset.forName("ISO-8859-1"), mediaType.getCharSet()); } + @Test + public void parseQuotedCharset() { + String s = "application/xml;charset=\"utf-8\""; + MediaType mediaType = MediaType.parseMediaType(s); + assertEquals("Invalid type", "application", mediaType.getType()); + assertEquals("Invalid subtype", "xml", mediaType.getSubtype()); + assertEquals("Invalid charset", Charset.forName("UTF-8"), mediaType.getCharSet()); + } + @Test public void parseURLConnectionMediaType() throws Exception { String s = "*; q=.2";