Fixed issue in parsing of invalid MediaTypes from java.net.HttpUrlConnection (*; q=.2)
This commit is contained in:
parent
bc8941084a
commit
332607ad6c
|
@ -106,7 +106,7 @@ public final class MediaType implements Comparable<MediaType> {
|
||||||
Assert.hasText(subtype, "'subtype' must not be empty");
|
Assert.hasText(subtype, "'subtype' must not be empty");
|
||||||
this.type = type.toLowerCase(Locale.ENGLISH);
|
this.type = type.toLowerCase(Locale.ENGLISH);
|
||||||
this.subtype = subtype.toLowerCase(Locale.ENGLISH);
|
this.subtype = subtype.toLowerCase(Locale.ENGLISH);
|
||||||
if (parameters != null) {
|
if (!CollectionUtils.isEmpty(parameters)) {
|
||||||
this.parameters = CollectionFactory.createLinkedCaseInsensitiveMapIfPossible(parameters.size());
|
this.parameters = CollectionFactory.createLinkedCaseInsensitiveMapIfPossible(parameters.size());
|
||||||
this.parameters.putAll(parameters);
|
this.parameters.putAll(parameters);
|
||||||
}
|
}
|
||||||
|
@ -124,28 +124,33 @@ public final class MediaType implements Comparable<MediaType> {
|
||||||
*/
|
*/
|
||||||
public static MediaType parseMediaType(String mediaType) {
|
public static MediaType parseMediaType(String mediaType) {
|
||||||
Assert.hasLength(mediaType, "'mediaType' must not be empty");
|
Assert.hasLength(mediaType, "'mediaType' must not be empty");
|
||||||
mediaType = mediaType.trim();
|
String[] parts = StringUtils.tokenizeToStringArray(mediaType, ";");
|
||||||
int subTypeIdx = mediaType.indexOf('/');
|
|
||||||
if (subTypeIdx == -1) {
|
|
||||||
throw new IllegalArgumentException("mediaType " + mediaType + " contains no /");
|
|
||||||
}
|
|
||||||
String type = mediaType.substring(0, subTypeIdx);
|
|
||||||
String subtype;
|
|
||||||
Map<String, String> parameters;
|
Map<String, String> parameters;
|
||||||
int paramIdx = mediaType.indexOf(';', subTypeIdx + 1);
|
if (parts.length <= 1) {
|
||||||
if (paramIdx == -1) {
|
|
||||||
subtype = mediaType.substring(subTypeIdx + 1).trim();
|
|
||||||
parameters = null;
|
parameters = null;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
subtype = mediaType.substring(subTypeIdx + 1, paramIdx).trim();
|
parameters = new LinkedHashMap<String, String>(parts.length - 1);
|
||||||
String[] tokens = StringUtils.tokenizeToStringArray(mediaType.substring(paramIdx), "; ");
|
}
|
||||||
parameters = new LinkedHashMap<String, String>(tokens.length);
|
for (int i = 1; i < parts.length; i++) {
|
||||||
for (String token : tokens) {
|
String part = parts[i];
|
||||||
int eqPos = token.indexOf('=');
|
int idx = part.indexOf('=');
|
||||||
parameters.put(token.substring(0, eqPos), token.substring(eqPos + 1));
|
if (idx != -1) {
|
||||||
|
String name = part.substring(0, idx);
|
||||||
|
String value = part.substring(idx + 1, part.length());
|
||||||
|
parameters.put(name, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
String fullType = parts[0].trim();
|
||||||
|
|
||||||
|
// java.net.HttpURLConnection returns a *; q=.2 Accept header
|
||||||
|
if (WILDCARD_TYPE.equals(fullType)) {
|
||||||
|
fullType = "*/*";
|
||||||
|
}
|
||||||
|
int idx = fullType.indexOf('/');
|
||||||
|
String type = fullType.substring(0, idx);
|
||||||
|
String subtype = fullType.substring(idx + 1, fullType.length());
|
||||||
return new MediaType(type, subtype, parameters);
|
return new MediaType(type, subtype, parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,85 +28,94 @@ import org.junit.Test;
|
||||||
*/
|
*/
|
||||||
public class MediaTypeTest {
|
public class MediaTypeTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void includes() throws Exception {
|
public void includes() throws Exception {
|
||||||
MediaType type1 = new MediaType("text", "plain");
|
MediaType type1 = new MediaType("text", "plain");
|
||||||
MediaType type2 = new MediaType("text", "plain");
|
MediaType type2 = new MediaType("text", "plain");
|
||||||
assertTrue("Equal types is not inclusive", type1.includes(type2));
|
assertTrue("Equal types is not inclusive", type1.includes(type2));
|
||||||
type1 = new MediaType("text");
|
type1 = new MediaType("text");
|
||||||
assertTrue("All subtypes is not inclusive", type1.includes(type2));
|
assertTrue("All subtypes is not inclusive", type1.includes(type2));
|
||||||
type1 = MediaType.ALL;
|
type1 = MediaType.ALL;
|
||||||
assertTrue("All types is not inclusive", type1.includes(type2));
|
assertTrue("All types is not inclusive", type1.includes(type2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testToString() throws Exception {
|
public void testToString() throws Exception {
|
||||||
MediaType mediaType = new MediaType("text", "plain", Collections.singletonMap("q", "0.7"));
|
MediaType mediaType = new MediaType("text", "plain", Collections.singletonMap("q", "0.7"));
|
||||||
String result = mediaType.toString();
|
String result = mediaType.toString();
|
||||||
assertEquals("Invalid toString() returned", "text/plain;q=0.7", result);
|
assertEquals("Invalid toString() returned", "text/plain;q=0.7", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void parseMediaType() throws Exception {
|
public void parseMediaType() throws Exception {
|
||||||
String s = "audio/*; q=0.2";
|
String s = "audio/*; q=0.2";
|
||||||
MediaType mediaType = MediaType.parseMediaType(s);
|
MediaType mediaType = MediaType.parseMediaType(s);
|
||||||
assertEquals("Invalid type", "audio", mediaType.getType());
|
assertEquals("Invalid type", "audio", mediaType.getType());
|
||||||
assertEquals("Invalid subtype", "*", mediaType.getSubtype());
|
assertEquals("Invalid subtype", "*", mediaType.getSubtype());
|
||||||
assertEquals("Invalid quality factor", 0.2D, mediaType.getQualityValue(), 0D);
|
assertEquals("Invalid quality factor", 0.2D, mediaType.getQualityValue(), 0D);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void parseMediaTypes() throws Exception {
|
public void parseURLConnectionMediaType() throws Exception {
|
||||||
String s = "text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c";
|
String s = "*; q=.2";
|
||||||
List<MediaType> mediaTypes = MediaType.parseMediaTypes(s);
|
MediaType mediaType = MediaType.parseMediaType(s);
|
||||||
assertNotNull("No media types returned", mediaTypes);
|
assertEquals("Invalid type", "*", mediaType.getType());
|
||||||
assertEquals("Invalid amount of media types", 4, mediaTypes.size());
|
assertEquals("Invalid subtype", "*", mediaType.getSubtype());
|
||||||
}
|
assertEquals("Invalid quality factor", 0.2D, mediaType.getQualityValue(), 0D);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void compareTo() throws Exception {
|
public void parseMediaTypes() throws Exception {
|
||||||
MediaType audioBasic = new MediaType("audio", "basic");
|
String s = "text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c";
|
||||||
MediaType audio = new MediaType("audio");
|
List<MediaType> mediaTypes = MediaType.parseMediaTypes(s);
|
||||||
MediaType audio03 = new MediaType("audio", "*", Collections.singletonMap("q", "0.3"));
|
assertNotNull("No media types returned", mediaTypes);
|
||||||
MediaType audio07 = new MediaType("audio", "*", Collections.singletonMap("q", "0.7"));
|
assertEquals("Invalid amount of media types", 4, mediaTypes.size());
|
||||||
MediaType all = MediaType.ALL;
|
}
|
||||||
|
|
||||||
// equal
|
@Test
|
||||||
assertEquals("Invalid comparison result", 0, audioBasic.compareTo(audioBasic));
|
public void compareTo() throws Exception {
|
||||||
assertEquals("Invalid comparison result", 0, audio.compareTo(audio));
|
MediaType audioBasic = new MediaType("audio", "basic");
|
||||||
assertEquals("Invalid comparison result", 0, audio07.compareTo(audio07));
|
MediaType audio = new MediaType("audio");
|
||||||
|
MediaType audio03 = new MediaType("audio", "*", Collections.singletonMap("q", "0.3"));
|
||||||
|
MediaType audio07 = new MediaType("audio", "*", Collections.singletonMap("q", "0.7"));
|
||||||
|
MediaType all = MediaType.ALL;
|
||||||
|
|
||||||
// specific to unspecific
|
// equal
|
||||||
assertTrue("Invalid comparison result", audioBasic.compareTo(audio) < 0);
|
assertEquals("Invalid comparison result", 0, audioBasic.compareTo(audioBasic));
|
||||||
assertTrue("Invalid comparison result", audioBasic.compareTo(all) < 0);
|
assertEquals("Invalid comparison result", 0, audio.compareTo(audio));
|
||||||
assertTrue("Invalid comparison result", audio.compareTo(all) < 0);
|
assertEquals("Invalid comparison result", 0, audio07.compareTo(audio07));
|
||||||
|
|
||||||
// unspecific to specific
|
// specific to unspecific
|
||||||
assertTrue("Invalid comparison result", audio.compareTo(audioBasic) > 0);
|
assertTrue("Invalid comparison result", audioBasic.compareTo(audio) < 0);
|
||||||
assertTrue("Invalid comparison result", all.compareTo(audioBasic) > 0);
|
assertTrue("Invalid comparison result", audioBasic.compareTo(all) < 0);
|
||||||
assertTrue("Invalid comparison result", all.compareTo(audio) > 0);
|
assertTrue("Invalid comparison result", audio.compareTo(all) < 0);
|
||||||
|
|
||||||
// qualifiers
|
// unspecific to specific
|
||||||
assertTrue("Invalid comparison result", audio.compareTo(audio07) < 0);
|
assertTrue("Invalid comparison result", audio.compareTo(audioBasic) > 0);
|
||||||
assertTrue("Invalid comparison result", audio07.compareTo(audio03) < 0);
|
assertTrue("Invalid comparison result", all.compareTo(audioBasic) > 0);
|
||||||
assertTrue("Invalid comparison result", audio03.compareTo(all) > 0);
|
assertTrue("Invalid comparison result", all.compareTo(audio) > 0);
|
||||||
|
|
||||||
// sort
|
// qualifiers
|
||||||
List<MediaType> expected = new ArrayList<MediaType>();
|
assertTrue("Invalid comparison result", audio.compareTo(audio07) < 0);
|
||||||
expected.add(audioBasic);
|
assertTrue("Invalid comparison result", audio07.compareTo(audio03) < 0);
|
||||||
expected.add(audio);
|
assertTrue("Invalid comparison result", audio03.compareTo(all) > 0);
|
||||||
expected.add(all);
|
|
||||||
expected.add(audio07);
|
|
||||||
expected.add(audio03);
|
|
||||||
|
|
||||||
List<MediaType> result = new ArrayList<MediaType>(expected);
|
// sort
|
||||||
for (int i = 0; i < 10; i++) {
|
List<MediaType> expected = new ArrayList<MediaType>();
|
||||||
Collections.shuffle(result);
|
expected.add(audioBasic);
|
||||||
Collections.sort(result);
|
expected.add(audio);
|
||||||
|
expected.add(all);
|
||||||
|
expected.add(audio07);
|
||||||
|
expected.add(audio03);
|
||||||
|
|
||||||
for (int j = 0; j < result.size(); j++) {
|
List<MediaType> result = new ArrayList<MediaType>(expected);
|
||||||
assertEquals("Invalid media type at " + j, expected.get(j), result.get(j));
|
for (int i = 0; i < 10; i++) {
|
||||||
}
|
Collections.shuffle(result);
|
||||||
}
|
Collections.sort(result);
|
||||||
}
|
|
||||||
|
for (int j = 0; j < result.size(); j++) {
|
||||||
|
assertEquals("Invalid media type at " + j, expected.get(j), result.get(j));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue