Skip to content

Commit

Permalink
Change deprecated MimeType specificity usages
Browse files Browse the repository at this point in the history
This commit changes all code that uses now deprecated methods in
MimeType and MediaType.

See spring-projectsgh-27580
  • Loading branch information
poutsma committed Oct 20, 2021
1 parent 177b292 commit 6dc8cde
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;

Expand Down Expand Up @@ -51,7 +52,7 @@ public List<MediaType> resolveMediaTypes(NativeWebRequest request)
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
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;
Expand Down Expand Up @@ -888,8 +889,8 @@ public void doWithRequest(ClientHttpRequest request) throws IOException {
.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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
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;
Expand Down Expand Up @@ -630,7 +631,7 @@ private static List<MediaType> acceptedMediaTypes(ServerRequest.Headers headers)
acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
}
else {
MediaType.sortBySpecificityAndQuality(acceptedMediaTypes);
MimeTypeUtils.sortBySpecificity(acceptedMediaTypes);
}
return acceptedMediaTypes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -33,6 +32,7 @@
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;
Expand Down Expand Up @@ -141,7 +141,7 @@ protected MediaType selectMediaType(
}

List<MediaType> result = new ArrayList<>(compatibleMediaTypes);
MediaType.sortBySpecificityAndQuality(result);
MimeTypeUtils.sortBySpecificity(result);

MediaType selected = null;
for (MediaType mediaType : result) {
Expand Down Expand Up @@ -183,8 +183,12 @@ private List<MediaType> getProducibleTypes(

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;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,19 @@ public final boolean match(ServerWebExchange exchange) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;
Expand Down Expand Up @@ -232,13 +233,14 @@ private List<ProduceMediaTypeExpression> getMatchingExpressions(ServerWebExchang
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,10 @@ public void compareToMultipleExpressions() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
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;
Expand Down Expand Up @@ -203,10 +204,12 @@ private <T> T bodyInternal(Type bodyType, Class<?> bodyClass) throws ServletExce
}

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
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;
Expand Down Expand Up @@ -628,7 +629,7 @@ private static List<MediaType> acceptedMediaTypes(ServerRequest.Headers headers)
acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
}
else {
MediaType.sortBySpecificityAndQuality(acceptedMediaTypes);
MimeTypeUtils.sortBySpecificity(acceptedMediaTypes);
}
return acceptedMediaTypes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,17 @@ public boolean isNegated() {

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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;
Expand Down Expand Up @@ -238,13 +239,14 @@ private List<ProduceMediaTypeExpression> getMatchingExpressions(List<MediaType>
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
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;
Expand Down Expand Up @@ -263,7 +264,7 @@ protected boolean isBindExceptionRequired(WebDataBinder binder, MethodParameter

/**
* 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) {
Expand All @@ -272,7 +273,7 @@ protected List<MediaType> getSupportedMediaTypes(Class<?> clazz) {
mediaTypeSet.addAll(converter.getSupportedMediaTypes(clazz));
}
List<MediaType> result = new ArrayList<>(mediaTypeSet);
MediaType.sortBySpecificity(result);
MimeTypeUtils.sortBySpecificity(result);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
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;
Expand Down Expand Up @@ -251,7 +252,7 @@ protected <T> void writeWithMessageConverters(@Nullable T value, MethodParameter
return;
}

MediaType.sortBySpecificityAndQuality(mediaTypesToUse);
MimeTypeUtils.sortBySpecificity(mediaTypesToUse);

for (MediaType mediaType : mediaTypesToUse) {
if (mediaType.isConcrete()) {
Expand Down Expand Up @@ -400,7 +401,12 @@ private List<MediaType> getAcceptableMediaTypes(HttpServletRequest request)
*/
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;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
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;
Expand Down Expand Up @@ -268,7 +269,7 @@ protected List<MediaType> getMediaTypes(HttpServletRequest request) {
}
}
List<MediaType> selectedMediaTypes = new ArrayList<>(compatibleMediaTypes);
MediaType.sortBySpecificityAndQuality(selectedMediaTypes);
MimeTypeUtils.sortBySpecificity(selectedMediaTypes);
return selectedMediaTypes;
}
catch (HttpMediaTypeNotAcceptableException ex) {
Expand Down Expand Up @@ -297,7 +298,12 @@ private List<MediaType> getProducibleMediaTypes(HttpServletRequest request) {
*/
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ public void compareToMultipleExpressions() {
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
Expand Down

0 comments on commit 6dc8cde

Please sign in to comment.