Skip to content

Commit 72119ac

Browse files
committed
ProducesRequestCondition caches accepted media types
Closes gh-22644
1 parent 254f06e commit 72119ac

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public abstract class MimeTypeUtils {
161161

162162

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

166166
@Nullable
167167
private static volatile Random random;

spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/ProducesRequestCondition.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro
5252

5353
private static final ProducesRequestCondition EMPTY_CONDITION = new ProducesRequestCondition();
5454

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

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

264266
private List<MediaType> getAcceptedMediaTypes(ServerWebExchange exchange) throws NotAcceptableStatusException {
265-
return this.contentTypeResolver.resolveMediaTypes(exchange);
267+
List<MediaType> result = exchange.getAttribute(MEDIA_TYPES_ATTRIBUTE);
268+
if (result == null) {
269+
result = this.contentTypeResolver.resolveMediaTypes(exchange);
270+
exchange.getAttributes().put(MEDIA_TYPES_ATTRIBUTE, result);
271+
}
272+
return result;
266273
}
267274

268275
private int indexOfEqualMediaType(MediaType mediaType) {

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ConsumesRequestCondition.java

+2
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ public ConsumesRequestCondition getMatchingCondition(HttpServletRequest request)
171171
return this;
172172
}
173173

174+
// Common media types are cached at the level of MimeTypeUtils
175+
174176
MediaType contentType;
175177
try {
176178
contentType = (StringUtils.hasLength(request.getContentType()) ?

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ProducesRequestCondition.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public final class ProducesRequestCondition extends AbstractRequestCondition<Pro
5656
private static final List<ProduceMediaTypeExpression> MEDIA_TYPE_ALL_LIST =
5757
Collections.singletonList(new ProduceMediaTypeExpression(MediaType.ALL_VALUE));
5858

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

6062
private final List<ProduceMediaTypeExpression> expressions;
6163

@@ -266,8 +268,16 @@ public int compareTo(ProducesRequestCondition other, HttpServletRequest request)
266268
}
267269
}
268270

269-
private List<MediaType> getAcceptedMediaTypes(HttpServletRequest request) throws HttpMediaTypeNotAcceptableException {
270-
return this.contentNegotiationManager.resolveMediaTypes(new ServletWebRequest(request));
271+
@SuppressWarnings("unchecked")
272+
private List<MediaType> getAcceptedMediaTypes(HttpServletRequest request)
273+
throws HttpMediaTypeNotAcceptableException {
274+
275+
List<MediaType> result = (List<MediaType>) request.getAttribute(MEDIA_TYPES_ATTRIBUTE);
276+
if (result == null) {
277+
result = this.contentNegotiationManager.resolveMediaTypes(new ServletWebRequest(request));
278+
request.setAttribute(MEDIA_TYPES_ATTRIBUTE, result);
279+
}
280+
return result;
271281
}
272282

273283
private int indexOfEqualMediaType(MediaType mediaType) {

0 commit comments

Comments
 (0)