Skip to content

Commit 1d2ebde

Browse files
committed
More optimal RequestMethod condition lookup
See gh-22644
1 parent 86c7347 commit 1d2ebde

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

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

+15-14
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@
4242
public final class RequestMethodsRequestCondition extends AbstractRequestCondition<RequestMethodsRequestCondition> {
4343

4444
/** Per HTTP method cache to return ready instances from getMatchingCondition. */
45-
private static final Map<String, RequestMethodsRequestCondition> requestMethodConditionCache;
45+
private static final Map<HttpMethod, RequestMethodsRequestCondition> requestMethodConditionCache;
4646

4747
static {
4848
requestMethodConditionCache = new HashMap<>(RequestMethod.values().length);
4949
for (RequestMethod method : RequestMethod.values()) {
50-
requestMethodConditionCache.put(method.name(), new RequestMethodsRequestCondition(method));
50+
requestMethodConditionCache.put(
51+
HttpMethod.valueOf(method.name()), new RequestMethodsRequestCondition(method));
5152
}
5253
}
5354

@@ -123,7 +124,7 @@ public RequestMethodsRequestCondition getMatchingCondition(ServerWebExchange exc
123124
}
124125
return this;
125126
}
126-
return matchRequestMethod(exchange.getRequest().getMethodValue());
127+
return matchRequestMethod(exchange.getRequest().getMethod());
127128
}
128129

129130
/**
@@ -137,20 +138,20 @@ private RequestMethodsRequestCondition matchPreFlight(ServerHttpRequest request)
137138
return this;
138139
}
139140
HttpMethod expectedMethod = request.getHeaders().getAccessControlRequestMethod();
140-
return expectedMethod != null ? matchRequestMethod(expectedMethod.name()) : null;
141+
return expectedMethod != null ? matchRequestMethod(expectedMethod) : null;
141142
}
142143

143144
@Nullable
144-
private RequestMethodsRequestCondition matchRequestMethod(@Nullable String httpMethod) {
145-
if (httpMethod != null) {
146-
for (RequestMethod method : getMethods()) {
147-
if (httpMethod.matches(method.name())) {
148-
return requestMethodConditionCache.get(method.name());
149-
}
150-
}
151-
if (HttpMethod.HEAD.matches(httpMethod) && getMethods().contains(RequestMethod.GET)) {
152-
return requestMethodConditionCache.get(HttpMethod.GET.name());
153-
}
145+
private RequestMethodsRequestCondition matchRequestMethod(@Nullable HttpMethod httpMethod) {
146+
if (httpMethod == null) {
147+
return null;
148+
}
149+
RequestMethod requestMethod = RequestMethod.valueOf(httpMethod.name());
150+
if (getMethods().contains(requestMethod)) {
151+
return requestMethodConditionCache.get(httpMethod);
152+
}
153+
if (requestMethod.equals(RequestMethod.HEAD) && getMethods().contains(RequestMethod.GET)) {
154+
return requestMethodConditionCache.get(HttpMethod.GET);
154155
}
155156
return null;
156157
}

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

+9-7
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,19 @@ private RequestMethodsRequestCondition matchPreFlight(HttpServletRequest request
142142

143143
@Nullable
144144
private RequestMethodsRequestCondition matchRequestMethod(String httpMethodValue) {
145-
HttpMethod httpMethod = HttpMethod.resolve(httpMethodValue);
146-
if (httpMethod != null) {
147-
for (RequestMethod method : getMethods()) {
148-
if (httpMethod.matches(method.name())) {
149-
return requestMethodConditionCache.get(method.name());
150-
}
145+
RequestMethod requestMethod;
146+
try {
147+
requestMethod = RequestMethod.valueOf(httpMethodValue);
148+
if (getMethods().contains(requestMethod)) {
149+
return requestMethodConditionCache.get(httpMethodValue);
151150
}
152-
if (httpMethod == HttpMethod.HEAD && getMethods().contains(RequestMethod.GET)) {
151+
if (requestMethod.equals(RequestMethod.HEAD) && getMethods().contains(RequestMethod.GET)) {
153152
return requestMethodConditionCache.get(HttpMethod.GET.name());
154153
}
155154
}
155+
catch (IllegalArgumentException ex) {
156+
// Custom request method
157+
}
156158
return null;
157159
}
158160

0 commit comments

Comments
 (0)