More accurate checks for presence of MediaType.ALL

Typically a straight up equals as well as Collections#contains
checks for MediaType.ALL is susceptible to the presence of
media type parameters.

This commits adds equalsTypeAndSubtype as well as an
isPresentIn(Collection<MimeType>) methods to MimeType to faciliate
with checks for MediaType.ALL.

Issue: SPR-17550
This commit is contained in:
Rossen Stoyanchev
2019-01-02 14:32:50 -05:00
parent 1cb9f2c7b2
commit 4b24bcb799
10 changed files with 111 additions and 38 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
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;
@@ -47,7 +48,8 @@ import org.springframework.web.server.ServerWebExchange;
*/
public abstract class HandlerResultHandlerSupport implements Ordered {
private static final MediaType MEDIA_TYPE_APPLICATION_ALL = new MediaType("application");
private static final List<MediaType> ALL_APPLICATION_MEDIA_TYPES =
Arrays.asList(MediaType.ALL, new MediaType("application"));
protected final Log logger = LogFactory.getLog(getClass());
@@ -147,7 +149,7 @@ public abstract class HandlerResultHandlerSupport implements Ordered {
selected = mediaType;
break;
}
else if (mediaType.equals(MediaType.ALL) || mediaType.equals(MEDIA_TYPE_APPLICATION_ALL)) {
else if (mediaType.isPresentIn(ALL_APPLICATION_MEDIA_TYPES)) {
selected = MediaType.APPLICATION_OCTET_STREAM;
break;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@ import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
import org.springframework.web.server.NotAcceptableStatusException;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.UnsupportedMediaTypeStatusException;
/**
* A logical disjunction (' || ') request condition to match a request's 'Accept' header
@@ -48,6 +49,8 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro
private static final ProducesRequestCondition PRE_FLIGHT_MATCH = new ProducesRequestCondition();
private static final ProducesRequestCondition EMPTY_CONDITION = new ProducesRequestCondition();
private final List<ProduceMediaTypeExpression> mediaTypeAllList =
Collections.singletonList(new ProduceMediaTypeExpression(MediaType.ALL_VALUE));
@@ -192,7 +195,20 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro
}
Set<ProduceMediaTypeExpression> result = new LinkedHashSet<>(this.expressions);
result.removeIf(expression -> !expression.match(exchange));
return (!result.isEmpty() ? new ProducesRequestCondition(result, this.contentTypeResolver) : null);
if (!result.isEmpty()) {
return new ProducesRequestCondition(result, this.contentTypeResolver);
}
else {
try {
if (MediaType.ALL.isPresentIn(getAcceptedMediaTypes(exchange))) {
return EMPTY_CONDITION;
}
}
catch (NotAcceptableStatusException | UnsupportedMediaTypeStatusException ex) {
// Ignore
}
}
return null;
}
/**