Change deprecated MimeType specificity usages
This commit changes all code that uses now deprecated methods in MimeType and MediaType. See gh-27580
This commit is contained in:
committed by
Arjen Poutsma
parent
6d9136013e
commit
259bcd60fb
@@ -57,6 +57,7 @@ import org.springframework.http.server.ServletServerHttpRequest;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.web.HttpMediaTypeNotSupportedException;
|
||||
@@ -203,10 +204,12 @@ class DefaultServerRequest implements ServerRequest {
|
||||
}
|
||||
|
||||
private List<MediaType> getSupportedMediaTypes(Class<?> bodyClass) {
|
||||
return this.messageConverters.stream()
|
||||
.flatMap(converter -> converter.getSupportedMediaTypes(bodyClass).stream())
|
||||
.sorted(MediaType.SPECIFICITY_COMPARATOR)
|
||||
.collect(Collectors.toList());
|
||||
List<MediaType> result = new ArrayList<>(this.messageConverters.size());
|
||||
for (HttpMessageConverter<?> converter : this.messageConverters) {
|
||||
result.addAll(converter.getSupportedMediaTypes(bodyClass));
|
||||
}
|
||||
MimeTypeUtils.sortBySpecificity(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -53,6 +53,7 @@ import org.springframework.http.server.RequestPath;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.cors.CorsUtils;
|
||||
import org.springframework.web.util.UriBuilder;
|
||||
@@ -628,7 +629,7 @@ public abstract class RequestPredicates {
|
||||
acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
|
||||
}
|
||||
else {
|
||||
MediaType.sortBySpecificityAndQuality(acceptedMediaTypes);
|
||||
MimeTypeUtils.sortBySpecificity(acceptedMediaTypes);
|
||||
}
|
||||
return acceptedMediaTypes;
|
||||
}
|
||||
|
||||
@@ -65,7 +65,17 @@ abstract class AbstractMediaTypeExpression implements MediaTypeExpression, Compa
|
||||
|
||||
@Override
|
||||
public int compareTo(AbstractMediaTypeExpression other) {
|
||||
return MediaType.SPECIFICITY_COMPARATOR.compare(this.getMediaType(), other.getMediaType());
|
||||
MediaType mediaType1 = this.getMediaType();
|
||||
MediaType mediaType2 = other.getMediaType();
|
||||
if (mediaType1.isMoreSpecific(mediaType2)) {
|
||||
return -1;
|
||||
}
|
||||
else if (mediaType1.isLessSpecific(mediaType2)) {
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -27,6 +27,7 @@ import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.MimeType;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.HttpMediaTypeException;
|
||||
@@ -238,13 +239,14 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro
|
||||
* Compares this and another "produces" condition as follows:
|
||||
* <ol>
|
||||
* <li>Sort 'Accept' header media types by quality value via
|
||||
* {@link MediaType#sortByQualityValue(List)} and iterate the list.
|
||||
* {@link org.springframework.util.MimeTypeUtils#sortBySpecificity(List)}
|
||||
* and iterate the list.
|
||||
* <li>Get the first index of matching media types in each "produces"
|
||||
* condition first matching with {@link MediaType#equals(Object)} and
|
||||
* then with {@link MediaType#includes(MediaType)}.
|
||||
* <li>If a lower index is found, the condition at that index wins.
|
||||
* <li>If both indexes are equal, the media types at the index are
|
||||
* compared further with {@link MediaType#SPECIFICITY_COMPARATOR}.
|
||||
* compared further with {@link MediaType#isMoreSpecific(MimeType)}.
|
||||
* </ol>
|
||||
* <p>It is assumed that both instances have been obtained via
|
||||
* {@link #getMatchingCondition(HttpServletRequest)} and each instance
|
||||
|
||||
@@ -48,6 +48,7 @@ import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.http.server.ServletServerHttpRequest;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
import org.springframework.util.StreamUtils;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.annotation.ValidationAnnotationUtils;
|
||||
@@ -263,7 +264,7 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements
|
||||
|
||||
/**
|
||||
* Return the media types supported by all provided message converters sorted
|
||||
* by specificity via {@link MediaType#sortBySpecificity(List)}.
|
||||
* by specificity via {@link MimeTypeUtils#sortBySpecificity(List)}.
|
||||
* @since 5.3.4
|
||||
*/
|
||||
protected List<MediaType> getSupportedMediaTypes(Class<?> clazz) {
|
||||
@@ -272,7 +273,7 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements
|
||||
mediaTypeSet.addAll(converter.getSupportedMediaTypes(clazz));
|
||||
}
|
||||
List<MediaType> result = new ArrayList<>(mediaTypeSet);
|
||||
MediaType.sortBySpecificity(result);
|
||||
MimeTypeUtils.sortBySpecificity(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -53,6 +53,7 @@ import org.springframework.http.server.ServletServerHttpResponse;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.HttpMediaTypeNotAcceptableException;
|
||||
import org.springframework.web.accept.ContentNegotiationManager;
|
||||
@@ -251,7 +252,7 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
|
||||
return;
|
||||
}
|
||||
|
||||
MediaType.sortBySpecificityAndQuality(mediaTypesToUse);
|
||||
MimeTypeUtils.sortBySpecificity(mediaTypesToUse);
|
||||
|
||||
for (MediaType mediaType : mediaTypesToUse) {
|
||||
if (mediaType.isConcrete()) {
|
||||
@@ -400,7 +401,12 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
|
||||
*/
|
||||
private MediaType getMostSpecificMediaType(MediaType acceptType, MediaType produceType) {
|
||||
MediaType produceTypeToUse = produceType.copyQualityValue(acceptType);
|
||||
return (MediaType.SPECIFICITY_COMPARATOR.compare(acceptType, produceTypeToUse) <= 0 ? acceptType : produceTypeToUse);
|
||||
if (acceptType.isLessSpecific(produceTypeToUse)) {
|
||||
return produceTypeToUse;
|
||||
}
|
||||
else {
|
||||
return acceptType;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -37,6 +37,7 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.HttpMediaTypeNotAcceptableException;
|
||||
import org.springframework.web.accept.ContentNegotiationManager;
|
||||
@@ -268,7 +269,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
|
||||
}
|
||||
}
|
||||
List<MediaType> selectedMediaTypes = new ArrayList<>(compatibleMediaTypes);
|
||||
MediaType.sortBySpecificityAndQuality(selectedMediaTypes);
|
||||
MimeTypeUtils.sortBySpecificity(selectedMediaTypes);
|
||||
return selectedMediaTypes;
|
||||
}
|
||||
catch (HttpMediaTypeNotAcceptableException ex) {
|
||||
@@ -297,7 +298,12 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
|
||||
*/
|
||||
private MediaType getMostSpecificMediaType(MediaType acceptType, MediaType produceType) {
|
||||
produceType = produceType.copyQualityValue(acceptType);
|
||||
return (MediaType.SPECIFICITY_COMPARATOR.compare(acceptType, produceType) < 0 ? acceptType : produceType);
|
||||
if (acceptType.isLessSpecific(produceType)) {
|
||||
return produceType;
|
||||
}
|
||||
else {
|
||||
return acceptType;
|
||||
}
|
||||
}
|
||||
|
||||
private List<View> getCandidateViews(String viewName, Locale locale, List<MediaType> requestedMediaTypes)
|
||||
|
||||
@@ -221,10 +221,10 @@ public class ProducesRequestConditionTests {
|
||||
HttpServletRequest request = createRequest("text/plain");
|
||||
|
||||
int result = condition1.compareTo(condition2, request);
|
||||
assertThat(result < 0).as("Invalid comparison result: " + result).isTrue();
|
||||
assertThat(result).as("Invalid comparison result: " + result).isGreaterThan(0);
|
||||
|
||||
result = condition2.compareTo(condition1, request);
|
||||
assertThat(result > 0).as("Invalid comparison result: " + result).isTrue();
|
||||
assertThat(result).as("Invalid comparison result: " + result).isLessThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user