Recognize wildcards in media types with a suffix
The "includes" and "isCompatibleWith" methods of MediaType take into account media types with suffices (e.g. application/soap+xml) including wildcards with suffices (e.g. application/*+xml). However before this change, the isWildcardSubtype() method returned true only for subtype "*". Now a media type such as application/*+xml is also recognized as having a wildcard subtype. Issue: SPR-9841
This commit is contained in:
@@ -264,6 +264,28 @@ public class RequestResponseBodyMethodProcessorTests {
|
||||
fail("Expected exception");
|
||||
}
|
||||
|
||||
// SPR-9841
|
||||
|
||||
@Test
|
||||
public void handleReturnValueMediaTypeSuffix() throws Exception {
|
||||
String body = "Foo";
|
||||
MediaType accepted = MediaType.APPLICATION_XHTML_XML;
|
||||
List<MediaType> supported = Collections.singletonList(MediaType.valueOf("application/*+xml"));
|
||||
|
||||
servletRequest.addHeader("Accept", accepted);
|
||||
|
||||
expect(messageConverter.canWrite(String.class, null)).andReturn(true);
|
||||
expect(messageConverter.getSupportedMediaTypes()).andReturn(supported);
|
||||
expect(messageConverter.canWrite(String.class, accepted)).andReturn(true);
|
||||
messageConverter.write(eq(body), eq(accepted), isA(HttpOutputMessage.class));
|
||||
replay(messageConverter);
|
||||
|
||||
processor.handleReturnValue(body, returnTypeStringProduces, mavContainer, webRequest);
|
||||
|
||||
assertTrue(mavContainer.isRequestHandled());
|
||||
verify(messageConverter);
|
||||
}
|
||||
|
||||
// SPR-9160
|
||||
|
||||
@Test
|
||||
|
||||
@@ -413,11 +413,12 @@ public class MediaType implements Comparable<MediaType> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether the {@linkplain #getSubtype() subtype} is the wildcard character <code>*</code> or not.
|
||||
* Indicates whether the {@linkplain #getSubtype() subtype} is the wildcard character <code>*</code>
|
||||
* or the wildcard character followed by a sufiix (e.g. <code>*+xml</code>), or not.
|
||||
* @return whether the subtype is <code>*</code>
|
||||
*/
|
||||
public boolean isWildcardSubtype() {
|
||||
return WILDCARD_TYPE.equals(subtype);
|
||||
return WILDCARD_TYPE.equals(subtype) || subtype.startsWith("*+");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -313,6 +313,7 @@ public class MediaTypeTests {
|
||||
MediaType audio07 = new MediaType("audio", "*", 0.7);
|
||||
MediaType audioBasicLevel = new MediaType("audio", "basic", Collections.singletonMap("level", "1"));
|
||||
MediaType textHtml = new MediaType("text", "html");
|
||||
MediaType allXml = new MediaType("application", "*+xml");
|
||||
MediaType all = MediaType.ALL;
|
||||
|
||||
Comparator<MediaType> comp = MediaType.SPECIFICITY_COMPARATOR;
|
||||
@@ -328,9 +329,11 @@ public class MediaTypeTests {
|
||||
assertTrue("Invalid comparison result", comp.compare(audioBasic, audio) < 0);
|
||||
assertTrue("Invalid comparison result", comp.compare(audioBasic, all) < 0);
|
||||
assertTrue("Invalid comparison result", comp.compare(audio, all) < 0);
|
||||
assertTrue("Invalid comparison result", comp.compare(MediaType.APPLICATION_XHTML_XML, allXml) < 0);
|
||||
|
||||
// unspecific to specific
|
||||
assertTrue("Invalid comparison result", comp.compare(audio, audioBasic) > 0);
|
||||
assertTrue("Invalid comparison result", comp.compare(allXml, MediaType.APPLICATION_XHTML_XML) > 0);
|
||||
assertTrue("Invalid comparison result", comp.compare(all, audioBasic) > 0);
|
||||
assertTrue("Invalid comparison result", comp.compare(all, audio) > 0);
|
||||
|
||||
@@ -414,6 +417,7 @@ public class MediaTypeTests {
|
||||
MediaType audio07 = new MediaType("audio", "*", 0.7);
|
||||
MediaType audioBasicLevel = new MediaType("audio", "basic", Collections.singletonMap("level", "1"));
|
||||
MediaType textHtml = new MediaType("text", "html");
|
||||
MediaType allXml = new MediaType("application", "*+xml");
|
||||
MediaType all = MediaType.ALL;
|
||||
|
||||
Comparator<MediaType> comp = MediaType.QUALITY_VALUE_COMPARATOR;
|
||||
@@ -429,11 +433,13 @@ public class MediaTypeTests {
|
||||
assertTrue("Invalid comparison result", comp.compare(audioBasic, audio) < 0);
|
||||
assertTrue("Invalid comparison result", comp.compare(audioBasic, all) < 0);
|
||||
assertTrue("Invalid comparison result", comp.compare(audio, all) < 0);
|
||||
assertTrue("Invalid comparison result", comp.compare(MediaType.APPLICATION_XHTML_XML, allXml) < 0);
|
||||
|
||||
// unspecific to specific
|
||||
assertTrue("Invalid comparison result", comp.compare(audio, audioBasic) > 0);
|
||||
assertTrue("Invalid comparison result", comp.compare(all, audioBasic) > 0);
|
||||
assertTrue("Invalid comparison result", comp.compare(all, audio) > 0);
|
||||
assertTrue("Invalid comparison result", comp.compare(allXml, MediaType.APPLICATION_XHTML_XML) > 0);
|
||||
|
||||
// qualifiers
|
||||
assertTrue("Invalid comparison result", comp.compare(audio, audio07) < 0);
|
||||
|
||||
Reference in New Issue
Block a user