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:
parent
6d9136013e
commit
259bcd60fb
|
@ -23,6 +23,7 @@ import org.springframework.http.HttpHeaders;
|
|||
import org.springframework.http.InvalidMediaTypeException;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
import org.springframework.web.HttpMediaTypeNotAcceptableException;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
|
||||
|
@ -51,7 +52,7 @@ public class HeaderContentNegotiationStrategy implements ContentNegotiationStrat
|
|||
List<String> headerValues = Arrays.asList(headerValueArray);
|
||||
try {
|
||||
List<MediaType> mediaTypes = MediaType.parseMediaTypes(headerValues);
|
||||
MediaType.sortBySpecificityAndQuality(mediaTypes);
|
||||
MimeTypeUtils.sortBySpecificity(mediaTypes);
|
||||
return !CollectionUtils.isEmpty(mediaTypes) ? mediaTypes : MEDIA_TYPE_ALL_LIST;
|
||||
}
|
||||
catch (InvalidMediaTypeException ex) {
|
||||
|
|
|
@ -61,6 +61,7 @@ import org.springframework.http.converter.xml.SourceHttpMessageConverter;
|
|||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
import org.springframework.web.util.DefaultUriBuilderFactory;
|
||||
import org.springframework.web.util.DefaultUriBuilderFactory.EncodingMode;
|
||||
import org.springframework.web.util.UriTemplateHandler;
|
||||
|
@ -878,8 +879,8 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
|
|||
.filter(converter -> canReadResponse(this.responseType, converter))
|
||||
.flatMap((HttpMessageConverter<?> converter) -> getSupportedMediaTypes(this.responseType, converter))
|
||||
.distinct()
|
||||
.sorted(MediaType.SPECIFICITY_COMPARATOR)
|
||||
.collect(Collectors.toList());
|
||||
MimeTypeUtils.sortBySpecificity(allSupportedMediaTypes);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Accept=" + allSupportedMediaTypes);
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ import java.util.List;
|
|||
import org.springframework.http.InvalidMediaTypeException;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
import org.springframework.web.server.NotAcceptableStatusException;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
|
@ -36,7 +37,7 @@ public class HeaderContentTypeResolver implements RequestedContentTypeResolver {
|
|||
public List<MediaType> resolveMediaTypes(ServerWebExchange exchange) throws NotAcceptableStatusException {
|
||||
try {
|
||||
List<MediaType> mediaTypes = exchange.getRequest().getHeaders().getAccept();
|
||||
MediaType.sortBySpecificityAndQuality(mediaTypes);
|
||||
MimeTypeUtils.sortBySpecificity(mediaTypes);
|
||||
return (!CollectionUtils.isEmpty(mediaTypes) ? mediaTypes : MEDIA_TYPE_ALL_LIST);
|
||||
}
|
||||
catch (InvalidMediaTypeException ex) {
|
||||
|
|
|
@ -51,6 +51,7 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
|
|||
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.reactive.CorsUtils;
|
||||
import org.springframework.web.reactive.function.BodyExtractor;
|
||||
|
@ -630,7 +631,7 @@ public abstract class RequestPredicates {
|
|||
acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
|
||||
}
|
||||
else {
|
||||
MediaType.sortBySpecificityAndQuality(acceptedMediaTypes);
|
||||
MimeTypeUtils.sortBySpecificity(acceptedMediaTypes);
|
||||
}
|
||||
return acceptedMediaTypes;
|
||||
}
|
||||
|
|
|
@ -18,7 +18,6 @@ package org.springframework.web.reactive.result;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
@ -33,6 +32,7 @@ import org.springframework.core.ReactiveAdapterRegistry;
|
|||
import org.springframework.http.MediaType;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
import org.springframework.web.reactive.HandlerMapping;
|
||||
import org.springframework.web.reactive.HandlerResult;
|
||||
import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
|
||||
|
@ -141,7 +141,7 @@ public abstract class HandlerResultHandlerSupport implements Ordered {
|
|||
}
|
||||
|
||||
List<MediaType> result = new ArrayList<>(compatibleMediaTypes);
|
||||
MediaType.sortBySpecificityAndQuality(result);
|
||||
MimeTypeUtils.sortBySpecificity(result);
|
||||
|
||||
MediaType selected = null;
|
||||
for (MediaType mediaType : result) {
|
||||
|
@ -183,8 +183,12 @@ public abstract class HandlerResultHandlerSupport implements Ordered {
|
|||
|
||||
private MediaType selectMoreSpecificMediaType(MediaType acceptable, MediaType producible) {
|
||||
producible = producible.copyQualityValue(acceptable);
|
||||
Comparator<MediaType> comparator = MediaType.SPECIFICITY_COMPARATOR;
|
||||
return (comparator.compare(acceptable, producible) <= 0 ? acceptable : producible);
|
||||
if (acceptable.isLessSpecific(producible)) {
|
||||
return producible;
|
||||
}
|
||||
else {
|
||||
return acceptable;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -78,10 +78,19 @@ abstract class AbstractMediaTypeExpression implements Comparable<AbstractMediaTy
|
|||
protected abstract boolean matchMediaType(ServerWebExchange exchange)
|
||||
throws NotAcceptableStatusException, UnsupportedMediaTypeStatusException;
|
||||
|
||||
|
||||
@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
|
||||
|
|
|
@ -25,6 +25,7 @@ import java.util.Set;
|
|||
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.accept.ContentNegotiationManager;
|
||||
|
@ -232,13 +233,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(ServerWebExchange)} and each instance
|
||||
|
|
|
@ -206,10 +206,10 @@ public class ProducesRequestConditionTests {
|
|||
MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "text/plain"));
|
||||
|
||||
int result = condition1.compareTo(condition2, exchange);
|
||||
assertThat(result < 0).as("Invalid comparison result: " + result).isTrue();
|
||||
assertThat(result).as("Invalid comparison result: " + result).isGreaterThan(0);
|
||||
|
||||
result = condition2.compareTo(condition1, exchange);
|
||||
assertThat(result > 0).as("Invalid comparison result: " + result).isTrue();
|
||||
assertThat(result).as("Invalid comparison result: " + result).isLessThan(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue