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:
Arjen Poutsma
2021-10-20 11:47:53 +02:00
committed by Arjen Poutsma
parent 6d9136013e
commit 259bcd60fb
16 changed files with 78 additions and 30 deletions

View File

@@ -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) {

View File

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

View File

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

View File

@@ -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

View File

@@ -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

View File

@@ -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