SPR-7135 - org.springframework.http.MediaType#checkParameters fails to process a Content-Type like application/xml;charset="utf-8"

This commit is contained in:
Arjen Poutsma 2010-04-27 08:26:51 +00:00
parent 8ce4037af2
commit c2707150b1
2 changed files with 19 additions and 1 deletions

View File

@ -264,7 +264,7 @@ public class MediaType implements Comparable<MediaType> {
String attribute = entry.getKey(); String attribute = entry.getKey();
String value = entry.getValue(); String value = entry.getValue();
checkParameters(attribute, value); checkParameters(attribute, value);
m.put(attribute, value); m.put(attribute, unquote(value));
} }
this.parameters = Collections.unmodifiableMap(m); this.parameters = Collections.unmodifiableMap(m);
} }
@ -293,10 +293,12 @@ public class MediaType implements Comparable<MediaType> {
Assert.hasLength(value, "parameter value must not be empty"); Assert.hasLength(value, "parameter value must not be empty");
checkToken(attribute); checkToken(attribute);
if (PARAM_QUALITY_FACTOR.equals(attribute)) { if (PARAM_QUALITY_FACTOR.equals(attribute)) {
value = unquote(value);
double d = Double.parseDouble(value); double d = Double.parseDouble(value);
Assert.isTrue(d >= 0D && d <= 1D, "Invalid quality value \"" + value + "\": should be between 0.0 and 1.0"); Assert.isTrue(d >= 0D && d <= 1D, "Invalid quality value \"" + value + "\": should be between 0.0 and 1.0");
} }
else if (PARAM_CHARSET.equals(attribute)) { else if (PARAM_CHARSET.equals(attribute)) {
value = unquote(value);
Charset.forName(value); Charset.forName(value);
} }
else if (!isQuotedString(value)) { else if (!isQuotedString(value)) {
@ -308,6 +310,13 @@ public class MediaType implements Comparable<MediaType> {
return s.length() > 1 && s.startsWith("\"") && s.endsWith("\"") ; 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. */ /** Return the primary type. */
public String getType() { public String getType() {
return this.type; return this.type;

View File

@ -180,6 +180,15 @@ public class MediaTypeTests {
assertEquals("Invalid charset", Charset.forName("ISO-8859-1"), mediaType.getCharSet()); 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 @Test
public void parseURLConnectionMediaType() throws Exception { public void parseURLConnectionMediaType() throws Exception {
String s = "*; q=.2"; String s = "*; q=.2";