From 1d2ebdeb8c81d5735ce5bb8a08aa7fa460e5379d Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 22 Aug 2019 13:39:04 +0300 Subject: [PATCH] More optimal RequestMethod condition lookup See gh-22644 --- .../RequestMethodsRequestCondition.java | 29 ++++++++++--------- .../RequestMethodsRequestCondition.java | 16 +++++----- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/RequestMethodsRequestCondition.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/RequestMethodsRequestCondition.java index 636347061a..ece8e22477 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/RequestMethodsRequestCondition.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/RequestMethodsRequestCondition.java @@ -42,12 +42,13 @@ import org.springframework.web.server.ServerWebExchange; public final class RequestMethodsRequestCondition extends AbstractRequestCondition { /** Per HTTP method cache to return ready instances from getMatchingCondition. */ - private static final Map requestMethodConditionCache; + private static final Map requestMethodConditionCache; static { requestMethodConditionCache = new HashMap<>(RequestMethod.values().length); for (RequestMethod method : RequestMethod.values()) { - requestMethodConditionCache.put(method.name(), new RequestMethodsRequestCondition(method)); + requestMethodConditionCache.put( + HttpMethod.valueOf(method.name()), new RequestMethodsRequestCondition(method)); } } @@ -123,7 +124,7 @@ public final class RequestMethodsRequestCondition extends AbstractRequestConditi } return this; } - return matchRequestMethod(exchange.getRequest().getMethodValue()); + return matchRequestMethod(exchange.getRequest().getMethod()); } /** @@ -137,20 +138,20 @@ public final class RequestMethodsRequestCondition extends AbstractRequestConditi return this; } HttpMethod expectedMethod = request.getHeaders().getAccessControlRequestMethod(); - return expectedMethod != null ? matchRequestMethod(expectedMethod.name()) : null; + return expectedMethod != null ? matchRequestMethod(expectedMethod) : null; } @Nullable - private RequestMethodsRequestCondition matchRequestMethod(@Nullable String httpMethod) { - if (httpMethod != null) { - for (RequestMethod method : getMethods()) { - if (httpMethod.matches(method.name())) { - return requestMethodConditionCache.get(method.name()); - } - } - if (HttpMethod.HEAD.matches(httpMethod) && getMethods().contains(RequestMethod.GET)) { - return requestMethodConditionCache.get(HttpMethod.GET.name()); - } + private RequestMethodsRequestCondition matchRequestMethod(@Nullable HttpMethod httpMethod) { + if (httpMethod == null) { + return null; + } + RequestMethod requestMethod = RequestMethod.valueOf(httpMethod.name()); + if (getMethods().contains(requestMethod)) { + return requestMethodConditionCache.get(httpMethod); + } + if (requestMethod.equals(RequestMethod.HEAD) && getMethods().contains(RequestMethod.GET)) { + return requestMethodConditionCache.get(HttpMethod.GET); } return null; } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestCondition.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestCondition.java index 2ab9721e67..95b991c35f 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestCondition.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestCondition.java @@ -142,17 +142,19 @@ public final class RequestMethodsRequestCondition extends AbstractRequestConditi @Nullable private RequestMethodsRequestCondition matchRequestMethod(String httpMethodValue) { - HttpMethod httpMethod = HttpMethod.resolve(httpMethodValue); - if (httpMethod != null) { - for (RequestMethod method : getMethods()) { - if (httpMethod.matches(method.name())) { - return requestMethodConditionCache.get(method.name()); - } + RequestMethod requestMethod; + try { + requestMethod = RequestMethod.valueOf(httpMethodValue); + if (getMethods().contains(requestMethod)) { + return requestMethodConditionCache.get(httpMethodValue); } - if (httpMethod == HttpMethod.HEAD && getMethods().contains(RequestMethod.GET)) { + if (requestMethod.equals(RequestMethod.HEAD) && getMethods().contains(RequestMethod.GET)) { return requestMethodConditionCache.get(HttpMethod.GET.name()); } } + catch (IllegalArgumentException ex) { + // Custom request method + } return null; }