Browse Source

More optimal RequestMethod condition lookup

See gh-22644
pull/23506/head
Rossen Stoyanchev 6 years ago
parent
commit
1d2ebdeb8c
  1. 25
      spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/RequestMethodsRequestCondition.java
  2. 16
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestCondition.java

25
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<RequestMethodsRequestCondition> { public final class RequestMethodsRequestCondition extends AbstractRequestCondition<RequestMethodsRequestCondition> {
/** Per HTTP method cache to return ready instances from getMatchingCondition. */ /** Per HTTP method cache to return ready instances from getMatchingCondition. */
private static final Map<String, RequestMethodsRequestCondition> requestMethodConditionCache; private static final Map<HttpMethod, RequestMethodsRequestCondition> requestMethodConditionCache;
static { static {
requestMethodConditionCache = new HashMap<>(RequestMethod.values().length); requestMethodConditionCache = new HashMap<>(RequestMethod.values().length);
for (RequestMethod method : RequestMethod.values()) { 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 this;
} }
return matchRequestMethod(exchange.getRequest().getMethodValue()); return matchRequestMethod(exchange.getRequest().getMethod());
} }
/** /**
@ -137,20 +138,20 @@ public final class RequestMethodsRequestCondition extends AbstractRequestConditi
return this; return this;
} }
HttpMethod expectedMethod = request.getHeaders().getAccessControlRequestMethod(); HttpMethod expectedMethod = request.getHeaders().getAccessControlRequestMethod();
return expectedMethod != null ? matchRequestMethod(expectedMethod.name()) : null; return expectedMethod != null ? matchRequestMethod(expectedMethod) : null;
} }
@Nullable @Nullable
private RequestMethodsRequestCondition matchRequestMethod(@Nullable String httpMethod) { private RequestMethodsRequestCondition matchRequestMethod(@Nullable HttpMethod httpMethod) {
if (httpMethod != null) { if (httpMethod == null) {
for (RequestMethod method : getMethods()) { return null;
if (httpMethod.matches(method.name())) {
return requestMethodConditionCache.get(method.name());
}
} }
if (HttpMethod.HEAD.matches(httpMethod) && getMethods().contains(RequestMethod.GET)) { RequestMethod requestMethod = RequestMethod.valueOf(httpMethod.name());
return requestMethodConditionCache.get(HttpMethod.GET.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; return null;
} }

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

@ -142,17 +142,19 @@ public final class RequestMethodsRequestCondition extends AbstractRequestConditi
@Nullable @Nullable
private RequestMethodsRequestCondition matchRequestMethod(String httpMethodValue) { private RequestMethodsRequestCondition matchRequestMethod(String httpMethodValue) {
HttpMethod httpMethod = HttpMethod.resolve(httpMethodValue); RequestMethod requestMethod;
if (httpMethod != null) { try {
for (RequestMethod method : getMethods()) { requestMethod = RequestMethod.valueOf(httpMethodValue);
if (httpMethod.matches(method.name())) { if (getMethods().contains(requestMethod)) {
return requestMethodConditionCache.get(method.name()); return requestMethodConditionCache.get(httpMethodValue);
} }
} if (requestMethod.equals(RequestMethod.HEAD) && getMethods().contains(RequestMethod.GET)) {
if (httpMethod == HttpMethod.HEAD && getMethods().contains(RequestMethod.GET)) {
return requestMethodConditionCache.get(HttpMethod.GET.name()); return requestMethodConditionCache.get(HttpMethod.GET.name());
} }
} }
catch (IllegalArgumentException ex) {
// Custom request method
}
return null; return null;
} }

Loading…
Cancel
Save