Skip to content

Commit 290e9be

Browse files
committed
Change MethodNotAllowedException to use HttpMethod
Changed the MethodNotAllowedException to use HttpMethod, instead of Strings.
1 parent 4db2daa commit 290e9be

File tree

5 files changed

+53
-58
lines changed

5 files changed

+53
-58
lines changed

spring-web/src/main/java/org/springframework/web/server/MethodNotAllowedException.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import java.util.HashSet;
2222
import java.util.Set;
2323

24+
import org.springframework.http.HttpMethod;
2425
import org.springframework.http.HttpStatus;
2526
import org.springframework.util.Assert;
2627

@@ -33,14 +34,21 @@
3334
@SuppressWarnings("serial")
3435
public class MethodNotAllowedException extends ResponseStatusException {
3536

36-
private String method;
37+
private final String method;
3738

38-
private Set<String> supportedMethods;
39+
private final Set<HttpMethod> supportedMethods;
3940

4041

41-
public MethodNotAllowedException(String method, Collection<String> supportedMethods) {
42+
public MethodNotAllowedException(HttpMethod method, Collection<HttpMethod> supportedMethods) {
43+
this(method.name(), supportedMethods);
44+
}
45+
46+
public MethodNotAllowedException(String method, Collection<HttpMethod> supportedMethods) {
4247
super(HttpStatus.METHOD_NOT_ALLOWED, "Request method '" + method + "' not supported");
4348
Assert.notNull(method, "'method' is required");
49+
if (supportedMethods == null) {
50+
supportedMethods = Collections.emptySet();
51+
}
4452
this.method = method;
4553
this.supportedMethods = Collections.unmodifiableSet(new HashSet<>(supportedMethods));
4654
}
@@ -56,7 +64,7 @@ public String getHttpMethod() {
5664
/**
5765
* Return the list of supported HTTP methods.
5866
*/
59-
public Set<String> getSupportedMethods() {
67+
public Set<HttpMethod> getSupportedMethods() {
6068
return supportedMethods;
6169
}
6270
}

spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,10 +21,9 @@
2121
import java.net.URLDecoder;
2222
import java.time.Instant;
2323
import java.util.ArrayList;
24-
import java.util.Arrays;
2524
import java.util.Collections;
25+
import java.util.EnumSet;
2626
import java.util.HashMap;
27-
import java.util.LinkedHashSet;
2827
import java.util.List;
2928
import java.util.Map;
3029
import java.util.Optional;
@@ -89,14 +88,10 @@ public class ResourceWebHandler
8988
implements WebHandler, InitializingBean, SmartInitializingSingleton {
9089

9190
/** Set of supported HTTP methods */
92-
private static final Set<String> SUPPORTED_METHODS = new LinkedHashSet<>(2);
91+
private static final Set<HttpMethod> SUPPORTED_METHODS = EnumSet.of(HttpMethod.GET, HttpMethod.HEAD);
9392

9493
private static final Log logger = LogFactory.getLog(ResourceWebHandler.class);
9594

96-
static {
97-
SUPPORTED_METHODS.addAll(Arrays.asList("GET", "HEAD"));
98-
}
99-
10095

10196
private final List<Resource> locations = new ArrayList<>(4);
10297

@@ -294,9 +289,9 @@ public Mono<Void> handle(ServerWebExchange exchange) {
294289
}
295290

296291
// Supported methods and required session
297-
String httpMehtod = exchange.getRequest().getMethod().name();
298-
if (!SUPPORTED_METHODS.contains(httpMehtod)) {
299-
return Mono.error(new MethodNotAllowedException(httpMehtod, SUPPORTED_METHODS));
292+
HttpMethod httpMethod = exchange.getRequest().getMethod();
293+
if (!SUPPORTED_METHODS.contains(httpMethod)) {
294+
return Mono.error(new MethodNotAllowedException(httpMethod, SUPPORTED_METHODS));
300295
}
301296

302297
// Header phase

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMapping.java

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import java.util.ArrayList;
2121
import java.util.Collections;
2222
import java.util.Comparator;
23+
import java.util.EnumSet;
2324
import java.util.LinkedHashMap;
2425
import java.util.LinkedHashSet;
2526
import java.util.List;
@@ -218,12 +219,12 @@ protected HandlerMethod handleNoMatch(Set<RequestMappingInfo> infos, String look
218219

219220
if (helper.hasMethodsMismatch()) {
220221
HttpMethod httpMethod = request.getMethod();
221-
Set<String> methods = helper.getAllowedMethods();
222+
Set<HttpMethod> methods = helper.getAllowedMethods();
222223
if (HttpMethod.OPTIONS.matches(httpMethod.name())) {
223224
HttpOptionsHandler handler = new HttpOptionsHandler(methods);
224225
return new HandlerMethod(handler, HTTP_OPTIONS_HANDLE_METHOD);
225226
}
226-
throw new MethodNotAllowedException(httpMethod.name(), methods);
227+
throw new MethodNotAllowedException(httpMethod, methods);
227228
}
228229

229230
if (helper.hasConsumesMismatch()) {
@@ -280,42 +281,42 @@ public boolean isEmpty() {
280281
* Any partial matches for "methods"?
281282
*/
282283
public boolean hasMethodsMismatch() {
283-
return !this.partialMatches.stream().
284-
filter(PartialMatch::hasMethodsMatch).findAny().isPresent();
284+
return this.partialMatches.stream().
285+
noneMatch(PartialMatch::hasMethodsMatch);
285286
}
286287

287288
/**
288289
* Any partial matches for "methods" and "consumes"?
289290
*/
290291
public boolean hasConsumesMismatch() {
291-
return !this.partialMatches.stream().
292-
filter(PartialMatch::hasConsumesMatch).findAny().isPresent();
292+
return this.partialMatches.stream().
293+
noneMatch(PartialMatch::hasConsumesMatch);
293294
}
294295

295296
/**
296297
* Any partial matches for "methods", "consumes", and "produces"?
297298
*/
298299
public boolean hasProducesMismatch() {
299-
return !this.partialMatches.stream().
300-
filter(PartialMatch::hasProducesMatch).findAny().isPresent();
300+
return this.partialMatches.stream().
301+
noneMatch(PartialMatch::hasProducesMatch);
301302
}
302303

303304
/**
304305
* Any partial matches for "methods", "consumes", "produces", and "params"?
305306
*/
306307
public boolean hasParamsMismatch() {
307-
return !this.partialMatches.stream().
308-
filter(PartialMatch::hasParamsMatch).findAny().isPresent();
308+
return this.partialMatches.stream().
309+
noneMatch(PartialMatch::hasParamsMatch);
309310
}
310311

311312
/**
312313
* Return declared HTTP methods.
313314
*/
314-
public Set<String> getAllowedMethods() {
315+
public Set<HttpMethod> getAllowedMethods() {
315316
return this.partialMatches.stream().
316317
flatMap(m -> m.getInfo().getMethodsCondition().getMethods().stream()).
317-
map(requestMethod -> requestMethod.name()).
318-
collect(Collectors.toCollection(LinkedHashSet::new));
318+
map(requestMethod -> HttpMethod.resolve(requestMethod.name())).
319+
collect(Collectors.toSet());
319320
}
320321

321322
/**
@@ -413,29 +414,23 @@ private static class HttpOptionsHandler {
413414
private final HttpHeaders headers = new HttpHeaders();
414415

415416

416-
public HttpOptionsHandler(Set<String> declaredMethods) {
417+
public HttpOptionsHandler(Set<HttpMethod> declaredMethods) {
417418
this.headers.setAllow(initAllowedHttpMethods(declaredMethods));
418419
}
419420

420-
private static Set<HttpMethod> initAllowedHttpMethods(Set<String> declaredMethods) {
421-
Set<HttpMethod> result = new LinkedHashSet<>(declaredMethods.size());
421+
private static Set<HttpMethod> initAllowedHttpMethods(Set<HttpMethod> declaredMethods) {
422422
if (declaredMethods.isEmpty()) {
423-
for (HttpMethod method : HttpMethod.values()) {
424-
if (!HttpMethod.TRACE.equals(method)) {
425-
result.add(method);
426-
}
427-
}
423+
return EnumSet.allOf(HttpMethod.class).stream()
424+
.filter(method -> !method.equals(HttpMethod.TRACE))
425+
.collect(Collectors.toSet());
428426
}
429427
else {
430-
boolean hasHead = declaredMethods.contains("HEAD");
431-
for (String method : declaredMethods) {
432-
result.add(HttpMethod.valueOf(method));
433-
if (!hasHead && "GET".equals(method)) {
434-
result.add(HttpMethod.HEAD);
435-
}
428+
Set<HttpMethod> result = new LinkedHashSet<>(declaredMethods);
429+
if (result.contains(HttpMethod.GET)) {
430+
result.add(HttpMethod.HEAD);
436431
}
432+
return result;
437433
}
438-
return result;
439434
}
440435

441436
@SuppressWarnings("unused")

spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/support/HandshakeWebSocketService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public Mono<Void> handleRequest(ServerWebExchange exchange, WebSocketHandler han
187187
}
188188

189189
if (HttpMethod.GET != method) {
190-
return Mono.error(new MethodNotAllowedException(method.name(), Collections.singleton("GET")));
190+
return Mono.error(new MethodNotAllowedException(method, Collections.singleton(HttpMethod.GET)));
191191
}
192192

193193
if (!"WebSocket".equalsIgnoreCase(headers.getUpgrade())) {

spring-webflux/src/test/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMappingTests.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.net.URI;
2121
import java.util.Arrays;
2222
import java.util.Collections;
23+
import java.util.EnumSet;
2324
import java.util.HashSet;
2425
import java.util.Map;
2526
import java.util.Optional;
@@ -56,11 +57,7 @@
5657
import org.springframework.web.server.support.HttpRequestPathHelper;
5758

5859
import static org.hamcrest.CoreMatchers.containsString;
59-
import static org.junit.Assert.assertEquals;
60-
import static org.junit.Assert.assertNotNull;
61-
import static org.junit.Assert.assertNull;
62-
import static org.junit.Assert.assertThat;
63-
import static org.junit.Assert.assertTrue;
60+
import static org.junit.Assert.*;
6461
import static org.springframework.mock.http.server.reactive.test.MockServerHttpRequest.get;
6562
import static org.springframework.web.bind.annotation.RequestMethod.GET;
6663
import static org.springframework.web.bind.annotation.RequestMethod.HEAD;
@@ -141,7 +138,7 @@ public void getHandlerRequestMethodNotAllowed() throws Exception {
141138
Mono<Object> mono = this.handlerMapping.getHandler(exchange);
142139

143140
assertError(mono, MethodNotAllowedException.class,
144-
ex -> assertEquals(new HashSet<>(Arrays.asList("GET", "HEAD")), ex.getSupportedMethods()));
141+
ex -> assertEquals(EnumSet.of(HttpMethod.GET, HttpMethod.HEAD), ex.getSupportedMethods()));
145142
}
146143

147144
@Test // SPR-9603
@@ -192,10 +189,10 @@ public void getHandlerTestRequestParamMismatch() throws Exception {
192189

193190
@Test
194191
public void getHandlerHttpOptions() throws Exception {
195-
testHttpOptions("/foo", "GET,HEAD");
196-
testHttpOptions("/person/1", "PUT");
197-
testHttpOptions("/persons", "GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS");
198-
testHttpOptions("/something", "PUT,POST");
192+
testHttpOptions("/foo", EnumSet.of(HttpMethod.GET, HttpMethod.HEAD));
193+
testHttpOptions("/person/1", EnumSet.of(HttpMethod.PUT));
194+
testHttpOptions("/persons", EnumSet.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.POST, HttpMethod.PUT, HttpMethod.PATCH, HttpMethod.DELETE, HttpMethod.OPTIONS));
195+
testHttpOptions("/something", EnumSet.of(HttpMethod.PUT, HttpMethod.POST));
199196
}
200197

201198
@Test
@@ -349,7 +346,7 @@ private void testHttpMediaTypeNotSupportedException(String url) throws Exception
349346
ex.getSupportedMediaTypes()));
350347
}
351348

352-
private void testHttpOptions(String requestURI, String allowHeader) throws Exception {
349+
private void testHttpOptions(String requestURI, Set<HttpMethod> allowedMethods) throws Exception {
353350
ServerWebExchange exchange = MockServerHttpRequest.options(requestURI).toExchange();
354351
HandlerMethod handlerMethod = (HandlerMethod) this.handlerMapping.getHandler(exchange).block();
355352

@@ -363,7 +360,7 @@ private void testHttpOptions(String requestURI, String allowHeader) throws Excep
363360
Optional<Object> value = result.getReturnValue();
364361
assertTrue(value.isPresent());
365362
assertEquals(HttpHeaders.class, value.get().getClass());
366-
assertEquals(allowHeader, ((HttpHeaders) value.get()).getFirst("Allow"));
363+
assertEquals(allowedMethods, ((HttpHeaders) value.get()).getAllow());
367364
}
368365

369366
private void testMediaTypeNotAcceptable(String url) throws Exception {

0 commit comments

Comments
 (0)