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 fa7886216aef0aeed74348eae6d7f420218448ce..20cd7543143f89445d587c9dbe7b8a165fdc4252 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 @@ -404,7 +404,6 @@ public class MediaType implements Comparable { int otherPlusIdx = other.subtype.indexOf('+'); if (thisPlusIdx != -1 && otherPlusIdx != -1) { String thisSubtypeNoSuffix = this.subtype.substring(0, thisPlusIdx); - String thisSubtypeSuffix = this.subtype.substring(thisPlusIdx + 1); String otherSubtypeSuffix = other.subtype.substring(otherPlusIdx + 1); if (thisSubtypeSuffix.equals(otherSubtypeSuffix) && WILDCARD_TYPE.equals(thisSubtypeNoSuffix)) { @@ -575,6 +574,9 @@ public class MediaType implements Comparable { } String type = fullType.substring(0, subIndex); String subtype = fullType.substring(subIndex + 1, fullType.length()); + if (WILDCARD_TYPE.equals(type) && !WILDCARD_TYPE.equals(subtype)) { + throw new IllegalArgumentException("A wildcard type is legal only in '*/*' (all media types)."); + } Map parameters = null; if (parts.length > 1) { 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 dab8d8ed78aa90f671518f88d4e3c2889c4ff1e6..aa7e8faffcc346b4b26fa8a442dd240f3bd793ba 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,12 +23,13 @@ import java.util.Comparator; import java.util.List; import java.util.Random; -import static org.junit.Assert.*; import org.junit.Test; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.support.ConversionServiceFactory; +import static org.junit.Assert.*; + /** * @author Arjen Poutsma * @author Juergen Hoeller @@ -127,6 +128,11 @@ public class MediaTypeTests { MediaType.parseMediaType("audio/"); } + @Test(expected = IllegalArgumentException.class) + public void parseMediaTypeTypeRange() { + MediaType.parseMediaType("*/json"); + } + @Test(expected = IllegalArgumentException.class) public void parseMediaTypeIllegalType() { MediaType.parseMediaType("audio(/basic"); @@ -496,4 +502,11 @@ public class MediaTypeTests { assertEquals(mediaType, conversionService.convert("application/xml", MediaType.class)); } + @Test + public void isConcrete() { + assertTrue("text/plain not concrete", MediaType.TEXT_PLAIN.isConcrete()); + assertFalse("*/* concrete", MediaType.ALL.isConcrete()); + assertFalse("text/* concrete", new MediaType("text", "*").isConcrete()); + } + }