|
22 | 22 | import java.util.ArrayList;
|
23 | 23 | import java.util.Arrays;
|
24 | 24 | import java.util.Collections;
|
| 25 | +import java.util.EnumSet; |
25 | 26 | import java.util.HashSet;
|
26 | 27 | import java.util.LinkedHashMap;
|
27 | 28 | import java.util.List;
|
@@ -79,14 +80,26 @@ public static RequestPredicate all() {
|
79 | 80 |
|
80 | 81 |
|
81 | 82 | /**
|
82 |
| - * Return a {@code RequestPredicate} that tests against the given HTTP method. |
83 |
| - * @param httpMethod the HTTP method to match to |
| 83 | + * Return a {@code RequestPredicate} that matches if the request's HTTP method is equal to the |
| 84 | + * given method |
| 85 | + * @param httpMethod the HTTP method to match against |
84 | 86 | * @return a predicate that tests against the given HTTP method
|
85 | 87 | */
|
86 | 88 | public static RequestPredicate method(HttpMethod httpMethod) {
|
87 | 89 | return new HttpMethodPredicate(httpMethod);
|
88 | 90 | }
|
89 | 91 |
|
| 92 | + /** |
| 93 | + * Return a {@code RequestPredicate} that matches if the request's HTTP method is equal to one |
| 94 | + * the of the given methods. |
| 95 | + * @param httpMethods the HTTP methods to match against |
| 96 | + * @return a predicate that tests against the given HTTP methods |
| 97 | + * @since 5.1 |
| 98 | + */ |
| 99 | + public static RequestPredicate methods(HttpMethod... httpMethods) { |
| 100 | + return new HttpMethodPredicate(httpMethods); |
| 101 | + } |
| 102 | + |
90 | 103 | /**
|
91 | 104 | * Return a {@code RequestPredicate} that tests the request path against the given path pattern.
|
92 | 105 | * @param pattern the pattern to match to
|
@@ -336,23 +349,34 @@ private static void traceMatch(String prefix, Object desired, @Nullable Object a
|
336 | 349 |
|
337 | 350 | private static class HttpMethodPredicate implements RequestPredicate {
|
338 | 351 |
|
339 |
| - private final HttpMethod httpMethod; |
| 352 | + private final Set<HttpMethod> httpMethods; |
340 | 353 |
|
341 | 354 | public HttpMethodPredicate(HttpMethod httpMethod) {
|
342 | 355 | Assert.notNull(httpMethod, "HttpMethod must not be null");
|
343 |
| - this.httpMethod = httpMethod; |
| 356 | + this.httpMethods = EnumSet.of(httpMethod); |
| 357 | + } |
| 358 | + |
| 359 | + public HttpMethodPredicate(HttpMethod... httpMethods) { |
| 360 | + Assert.notEmpty(httpMethods, "HttpMethods must not be empty"); |
| 361 | + |
| 362 | + this.httpMethods = EnumSet.copyOf(Arrays.asList(httpMethods)); |
344 | 363 | }
|
345 | 364 |
|
346 | 365 | @Override
|
347 | 366 | public boolean test(ServerRequest request) {
|
348 |
| - boolean match = this.httpMethod == request.method(); |
349 |
| - traceMatch("Method", this.httpMethod, request.method(), match); |
| 367 | + boolean match = this.httpMethods.contains(request.method()); |
| 368 | + traceMatch("Method", this.httpMethods, request.method(), match); |
350 | 369 | return match;
|
351 | 370 | }
|
352 | 371 |
|
353 | 372 | @Override
|
354 | 373 | public String toString() {
|
355 |
| - return this.httpMethod.toString(); |
| 374 | + if (this.httpMethods.size() == 1) { |
| 375 | + return this.httpMethods.iterator().next().toString(); |
| 376 | + } |
| 377 | + else { |
| 378 | + return this.httpMethods.toString(); |
| 379 | + } |
356 | 380 | }
|
357 | 381 | }
|
358 | 382 |
|
|
0 commit comments