SPR-7000 - AnnotationMethodHandlerAdapter gives priority to media type order over quality when selecting a method
This commit is contained in:
@@ -648,6 +648,35 @@ public class MediaType implements Comparable<MediaType> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the given list of {@link MediaType} objects by quality value.
|
||||
*
|
||||
* <p>Given two media types:
|
||||
* <ol>
|
||||
* <li>if the two media types have different {@linkplain #getQualityValue() quality value}, then the media type
|
||||
* with the highest quality value is ordered before the other.</li>
|
||||
* <li>if either media type has a {@linkplain #isWildcardType() wildcard type}, then the media type without the
|
||||
* wildcard is ordered before the other.</li>
|
||||
* <li>if the two media types have different {@linkplain #getType() types}, then they are considered equal and
|
||||
* remain their current order.</li>
|
||||
* <li>if either media type has a {@linkplain #isWildcardSubtype() wildcard subtype}, then the media type without
|
||||
* the wildcard is sorted before the other.</li>
|
||||
* <li>if the two media types have different {@linkplain #getSubtype() subtypes}, then they are considered equal
|
||||
* and remain their current order.</li>
|
||||
* <li>if the two media types have a different amount of {@linkplain #getParameter(String) parameters}, then the
|
||||
* media type with the most parameters is ordered before the other.</li>
|
||||
* </ol>
|
||||
*
|
||||
* @param mediaTypes the list of media types to be sorted
|
||||
* @see #getQualityValue()
|
||||
*/
|
||||
public static void sortByQualityValue(List<MediaType> mediaTypes) {
|
||||
Assert.notNull(mediaTypes, "'mediaTypes' must not be null");
|
||||
if (mediaTypes.size() > 1) {
|
||||
Collections.sort(mediaTypes, QUALITY_VALUE_COMPARATOR);
|
||||
}
|
||||
}
|
||||
|
||||
static final Comparator<MediaType> SPECIFICITY_COMPARATOR = new Comparator<MediaType>() {
|
||||
|
||||
public int compare(MediaType mediaType1, MediaType mediaType2) {
|
||||
@@ -686,4 +715,39 @@ public class MediaType implements Comparable<MediaType> {
|
||||
}
|
||||
};
|
||||
|
||||
static final Comparator<MediaType> QUALITY_VALUE_COMPARATOR = new Comparator<MediaType>() {
|
||||
|
||||
public int compare(MediaType mediaType1, MediaType mediaType2) {
|
||||
double quality1 = mediaType1.getQualityValue();
|
||||
double quality2 = mediaType2.getQualityValue();
|
||||
int qualityComparison = Double.compare(quality2, quality1);
|
||||
if (qualityComparison != 0) {
|
||||
return qualityComparison; // audio/*;q=0.7 < audio/*;q=0.3
|
||||
}
|
||||
else if (mediaType1.isWildcardType() && !mediaType2.isWildcardType()) { // */* < audio/*
|
||||
return 1;
|
||||
}
|
||||
else if (mediaType2.isWildcardType() && !mediaType1.isWildcardType()) { // audio/* > */*
|
||||
return -1;
|
||||
}
|
||||
else if (!mediaType1.getType().equals(mediaType2.getType())) { // audio/basic == text/html
|
||||
return 0;
|
||||
}
|
||||
else { // mediaType1.getType().equals(mediaType2.getType())
|
||||
if (mediaType1.isWildcardSubtype() && !mediaType2.isWildcardSubtype()) { // audio/* < audio/basic
|
||||
return 1;
|
||||
}
|
||||
else if (mediaType2.isWildcardSubtype() && !mediaType1.isWildcardSubtype()) { // audio/basic > audio/*
|
||||
return -1;
|
||||
}
|
||||
else if (!mediaType1.getSubtype().equals(mediaType2.getSubtype())) { // audio/basic == audio/wave
|
||||
return 0;
|
||||
} else {
|
||||
int paramsSize1 = mediaType1.parameters.size();
|
||||
int paramsSize2 = mediaType2.parameters.size();
|
||||
return (paramsSize2 < paramsSize1 ? -1 : (paramsSize2 == paramsSize1 ? 0 : 1)); // audio/basic;level=1 < audio/basic
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -373,4 +373,108 @@ public class MediaTypeTests {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void qualityComparator() throws Exception {
|
||||
MediaType audioBasic = new MediaType("audio", "basic");
|
||||
MediaType audioWave = new MediaType("audio", "wave");
|
||||
MediaType audio = new MediaType("audio");
|
||||
MediaType audio03 = new MediaType("audio", "*", 0.3);
|
||||
MediaType audio07 = new MediaType("audio", "*", 0.7);
|
||||
MediaType audioBasicLevel = new MediaType("audio", "basic", Collections.singletonMap("level", "1"));
|
||||
MediaType textHtml = new MediaType("text", "html");
|
||||
MediaType all = MediaType.ALL;
|
||||
|
||||
Comparator<MediaType> comp = MediaType.QUALITY_VALUE_COMPARATOR;
|
||||
|
||||
// equal
|
||||
assertEquals("Invalid comparison result", 0, comp.compare(audioBasic,audioBasic));
|
||||
assertEquals("Invalid comparison result", 0, comp.compare(audio, audio));
|
||||
assertEquals("Invalid comparison result", 0, comp.compare(audio07, audio07));
|
||||
assertEquals("Invalid comparison result", 0, comp.compare(audio03, audio03));
|
||||
assertEquals("Invalid comparison result", 0, comp.compare(audioBasicLevel, audioBasicLevel));
|
||||
|
||||
// specific to unspecific
|
||||
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);
|
||||
|
||||
// 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);
|
||||
|
||||
// qualifiers
|
||||
assertTrue("Invalid comparison result", comp.compare(audio, audio07) < 0);
|
||||
assertTrue("Invalid comparison result", comp.compare(audio07, audio) > 0);
|
||||
assertTrue("Invalid comparison result", comp.compare(audio07, audio03) < 0);
|
||||
assertTrue("Invalid comparison result", comp.compare(audio03, audio07) > 0);
|
||||
assertTrue("Invalid comparison result", comp.compare(audio03, all) > 0);
|
||||
assertTrue("Invalid comparison result", comp.compare(all, audio03) < 0);
|
||||
|
||||
// other parameters
|
||||
assertTrue("Invalid comparison result", comp.compare(audioBasic, audioBasicLevel) > 0);
|
||||
assertTrue("Invalid comparison result", comp.compare(audioBasicLevel, audioBasic) < 0);
|
||||
|
||||
// different types
|
||||
assertEquals("Invalid comparison result", 0, comp.compare(audioBasic, textHtml));
|
||||
assertEquals("Invalid comparison result", 0, comp.compare(textHtml, audioBasic));
|
||||
|
||||
// different subtypes
|
||||
assertEquals("Invalid comparison result", 0, comp.compare(audioBasic, audioWave));
|
||||
assertEquals("Invalid comparison result", 0, comp.compare(audioWave, audioBasic));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sortByQualityRelated() {
|
||||
MediaType audioBasic = new MediaType("audio", "basic");
|
||||
MediaType audio = new MediaType("audio");
|
||||
MediaType audio03 = new MediaType("audio", "*", 0.3);
|
||||
MediaType audio07 = new MediaType("audio", "*", 0.7);
|
||||
MediaType audioBasicLevel = new MediaType("audio", "basic", Collections.singletonMap("level", "1"));
|
||||
MediaType all = MediaType.ALL;
|
||||
|
||||
List<MediaType> expected = new ArrayList<MediaType>();
|
||||
expected.add(audioBasicLevel);
|
||||
expected.add(audioBasic);
|
||||
expected.add(audio);
|
||||
expected.add(all);
|
||||
expected.add(audio07);
|
||||
expected.add(audio03);
|
||||
|
||||
List<MediaType> result = new ArrayList<MediaType>(expected);
|
||||
Random rnd = new Random();
|
||||
// shuffle & sort 10 times
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Collections.shuffle(result, rnd);
|
||||
MediaType.sortByQualityValue(result);
|
||||
|
||||
for (int j = 0; j < result.size(); j++) {
|
||||
assertSame("Invalid media type at " + j, expected.get(j), result.get(j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sortByQualityUnrelated() {
|
||||
MediaType audioBasic = new MediaType("audio", "basic");
|
||||
MediaType audioWave = new MediaType("audio", "wave");
|
||||
MediaType textHtml = new MediaType("text", "html");
|
||||
|
||||
List<MediaType> expected = new ArrayList<MediaType>();
|
||||
expected.add(textHtml);
|
||||
expected.add(audioBasic);
|
||||
expected.add(audioWave);
|
||||
|
||||
List<MediaType> result = new ArrayList<MediaType>(expected);
|
||||
MediaType.sortBySpecificity(result);
|
||||
|
||||
for (int i = 0; i < result.size(); i++) {
|
||||
assertSame("Invalid media type at " + i, expected.get(i), result.get(i));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user