Skip to content

Commit

Permalink
ProducesRequestCondition caches accepted media types
Browse files Browse the repository at this point in the history
Closes gh-22644
  • Loading branch information
rstoyanchev committed Apr 3, 2019
1 parent 254f06e commit 72119ac
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public abstract class MimeTypeUtils {


private static final ConcurrentLruCache<String, MimeType> cachedMimeTypes =
new ConcurrentLruCache<>(32, MimeTypeUtils::parseMimeTypeInternal);
new ConcurrentLruCache<>(64, MimeTypeUtils::parseMimeTypeInternal);

@Nullable
private static volatile Random random;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro

private static final ProducesRequestCondition EMPTY_CONDITION = new ProducesRequestCondition();

private static final String MEDIA_TYPES_ATTRIBUTE = ProducesRequestCondition.class.getName() + ".MEDIA_TYPES";


private final List<ProduceMediaTypeExpression> mediaTypeAllList =
Collections.singletonList(new ProduceMediaTypeExpression(MediaType.ALL_VALUE));
Expand Down Expand Up @@ -262,7 +264,12 @@ public int compareTo(ProducesRequestCondition other, ServerWebExchange exchange)
}

private List<MediaType> getAcceptedMediaTypes(ServerWebExchange exchange) throws NotAcceptableStatusException {
return this.contentTypeResolver.resolveMediaTypes(exchange);
List<MediaType> result = exchange.getAttribute(MEDIA_TYPES_ATTRIBUTE);
if (result == null) {
result = this.contentTypeResolver.resolveMediaTypes(exchange);
exchange.getAttributes().put(MEDIA_TYPES_ATTRIBUTE, result);
}
return result;
}

private int indexOfEqualMediaType(MediaType mediaType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ public ConsumesRequestCondition getMatchingCondition(HttpServletRequest request)
return this;
}

// Common media types are cached at the level of MimeTypeUtils

MediaType contentType;
try {
contentType = (StringUtils.hasLength(request.getContentType()) ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro
private static final List<ProduceMediaTypeExpression> MEDIA_TYPE_ALL_LIST =
Collections.singletonList(new ProduceMediaTypeExpression(MediaType.ALL_VALUE));

private static final String MEDIA_TYPES_ATTRIBUTE = ProducesRequestCondition.class.getName() + ".MEDIA_TYPES";


private final List<ProduceMediaTypeExpression> expressions;

Expand Down Expand Up @@ -266,8 +268,16 @@ public int compareTo(ProducesRequestCondition other, HttpServletRequest request)
}
}

private List<MediaType> getAcceptedMediaTypes(HttpServletRequest request) throws HttpMediaTypeNotAcceptableException {
return this.contentNegotiationManager.resolveMediaTypes(new ServletWebRequest(request));
@SuppressWarnings("unchecked")
private List<MediaType> getAcceptedMediaTypes(HttpServletRequest request)
throws HttpMediaTypeNotAcceptableException {

List<MediaType> result = (List<MediaType>) request.getAttribute(MEDIA_TYPES_ATTRIBUTE);
if (result == null) {
result = this.contentNegotiationManager.resolveMediaTypes(new ServletWebRequest(request));
request.setAttribute(MEDIA_TYPES_ATTRIBUTE, result);
}
return result;
}

private int indexOfEqualMediaType(MediaType mediaType) {
Expand Down

0 comments on commit 72119ac

Please sign in to comment.