From 7a4207cd7bfbe57217b1718111f8c56cb076a34d Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Thu, 25 Nov 2021 13:45:34 +0100 Subject: [PATCH] Changes because HttpMethod changed to class This commit contains changes made because HttpMethod changed from enum to class. See gh-27697 --- .../http/client/MockClientHttpRequest.java | 1 + .../reactive/MockServerHttpRequest.java | 29 ++++++------- .../web/MockMultipartHttpServletRequest.java | 12 +++--- .../function/server/MockServerRequest.java | 1 + .../org/springframework/http/HttpHeaders.java | 35 ++++++++------- .../org/springframework/http/HttpRequest.java | 16 +++---- .../BufferingClientHttpRequestWrapper.java | 3 +- .../HttpComponentsClientHttpRequest.java | 5 +++ ...ttpComponentsClientHttpRequestFactory.java | 43 +++++++++++-------- ...pComponentsStreamingClientHttpRequest.java | 6 +++ .../client/InterceptingClientHttpRequest.java | 3 +- .../http/client/OkHttp3ClientHttpRequest.java | 1 + .../SimpleBufferingClientHttpRequest.java | 5 +++ .../SimpleStreamingClientHttpRequest.java | 5 +++ .../HttpComponentsClientHttpRequest.java | 5 +-- .../client/support/HttpRequestWrapper.java | 3 +- .../http/server/ServletServerHttpRequest.java | 4 +- .../DefaultServerHttpRequestBuilder.java | 23 ++++++---- .../reactive/ReactorServerHttpRequest.java | 6 +++ .../reactive/ServerHttpRequestDecorator.java | 2 +- .../reactive/ServletServerHttpRequest.java | 6 +++ .../reactive/UndertowServerHttpRequest.java | 7 +++ ...ttpRequestMethodNotSupportedException.java | 14 +++--- .../context/request/ServletWebRequest.java | 3 +- .../web/cors/CorsConfiguration.java | 6 +-- .../reactive/HiddenHttpMethodFilter.java | 3 +- .../MultipartHttpServletRequest.java | 1 - .../AbstractMultipartHttpServletRequest.java | 2 +- .../http/HttpHeadersTests.java | 4 +- ...rceptingClientHttpRequestFactoryTests.java | 1 + .../reactive/ClientHttpConnectorTests.java | 6 +-- .../client/RestTemplateIntegrationTests.java | 3 +- .../web/client/RestTemplateTests.java | 3 +- .../reactive/HiddenHttpMethodFilterTests.java | 11 ----- .../web/util/UriComponentsBuilderTests.java | 7 +++ ...tParamMethodArgumentResolverKotlinTests.kt | 16 +++---- .../reactive/MockClientHttpRequest.java | 1 + .../reactive/MockServerHttpRequest.java | 29 ++++++------- .../MockMultipartHttpServletRequest.java | 8 +++- .../client/DefaultClientResponseBuilder.java | 9 +++- .../function/client/DefaultWebClient.java | 3 +- .../function/client/ExchangeFunctions.java | 1 + .../client/WebClientResponseException.java | 2 +- .../function/server/DefaultServerRequest.java | 9 +++- .../server/DefaultServerRequestBuilder.java | 20 ++++++--- .../server/DefaultServerResponseBuilder.java | 3 +- .../function/server/RequestPredicates.java | 16 +++---- .../server/ResourceHandlerFunction.java | 30 ++++++------- .../function/server/ServerRequest.java | 7 ++- .../server/support/ServerRequestWrapper.java | 1 + .../reactive/resource/ResourceWebHandler.java | 7 ++- .../RequestMethodsRequestCondition.java | 2 +- .../RequestMappingInfoHandlerMapping.java | 12 +++--- ...AbstractMessageReaderArgumentResolver.java | 5 +-- .../ResponseEntityResultHandler.java | 3 +- .../support/HandshakeWebSocketService.java | 2 +- .../WebSocketUpgradeHandlerPredicate.java | 5 ++- .../DefaultEntityResponseBuilderTests.java | 3 +- .../DefaultServerResponseBuilderTests.java | 3 +- .../server/ResourceHandlerFunctionTests.java | 6 +-- ...RequestMappingInfoHandlerMappingTests.java | 13 +++--- .../web/servlet/FrameworkServlet.java | 4 +- .../function/AbstractServerResponse.java | 5 +-- .../function/DefaultServerRequest.java | 6 +++ .../function/DefaultServerRequestBuilder.java | 22 ++++++---- .../servlet/function/RequestPredicates.java | 16 +++---- .../function/ResourceHandlerFunction.java | 24 +++++------ .../web/servlet/function/ServerRequest.java | 7 ++- ...essageConverterMethodArgumentResolver.java | 4 +- .../ServletRequestMethodArgumentResolver.java | 2 +- .../DefaultEntityResponseBuilderTests.java | 3 +- .../DefaultServerResponseBuilderTests.java | 4 +- .../ResourceHandlerFunctionTests.java | 9 ++-- .../ResponseEntityExceptionHandlerTests.java | 4 +- 74 files changed, 337 insertions(+), 274 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/mock/http/client/MockClientHttpRequest.java b/spring-test/src/main/java/org/springframework/mock/http/client/MockClientHttpRequest.java index 7630ac3a3b2..f3aafea735a 100644 --- a/spring-test/src/main/java/org/springframework/mock/http/client/MockClientHttpRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/http/client/MockClientHttpRequest.java @@ -78,6 +78,7 @@ public class MockClientHttpRequest extends MockHttpOutputMessage implements Clie } @Override + @Deprecated public String getMethodValue() { return this.httpMethod.name(); } diff --git a/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java b/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java index 29b39931af6..6d8bda48ab8 100644 --- a/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java @@ -55,10 +55,7 @@ import org.springframework.web.util.UriComponentsBuilder; */ public final class MockServerHttpRequest extends AbstractServerHttpRequest { - /** - * String representation of one of {@link HttpMethod} or not empty custom method (e.g. CONNECT). - */ - private final String httpMethod; + private final HttpMethod httpMethod; private final MultiValueMap cookies; @@ -73,13 +70,12 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { private final Flux body; - private MockServerHttpRequest(String httpMethod, + private MockServerHttpRequest(HttpMethod httpMethod, URI uri, @Nullable String contextPath, HttpHeaders headers, MultiValueMap cookies, @Nullable InetSocketAddress localAddress, @Nullable InetSocketAddress remoteAddress, @Nullable SslInfo sslInfo, Publisher body) { super(uri, contextPath, headers); - Assert.isTrue(StringUtils.hasText(httpMethod), "HTTP method is required."); this.httpMethod = httpMethod; this.cookies = cookies; this.localAddress = localAddress; @@ -90,14 +86,14 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { @Override - @Nullable public HttpMethod getMethod() { - return HttpMethod.resolve(this.httpMethod); + return this.httpMethod; } @Override + @Deprecated public String getMethodValue() { - return this.httpMethod; + return this.httpMethod.name(); } @Override @@ -218,7 +214,7 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { public static BodyBuilder method(HttpMethod method, URI url) { Assert.notNull(method, "HTTP method is required. " + "For a custom HTTP method, please provide a String HTTP method value."); - return new DefaultBodyBuilder(method.name(), url); + return new DefaultBodyBuilder(method, url); } /** @@ -242,9 +238,12 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { * @param vars variables to expand into the template * @return the created builder * @since 5.2.7 + * @deprecated in favor of {@link #method(HttpMethod, String, Object...)} */ + @Deprecated public static BodyBuilder method(String httpMethod, String uri, Object... vars) { - return new DefaultBodyBuilder(httpMethod, toUri(uri, vars)); + Assert.isTrue(StringUtils.hasText(httpMethod), "HTTP method is required."); + return new DefaultBodyBuilder(HttpMethod.valueOf(httpMethod), toUri(uri, vars)); } private static URI toUri(String uri, Object[] vars) { @@ -427,7 +426,7 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { private static class DefaultBodyBuilder implements BodyBuilder { - private final String methodValue; + private final HttpMethod method; private final URI url; @@ -449,8 +448,8 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { @Nullable private SslInfo sslInfo; - DefaultBodyBuilder(String method, URI url) { - this.methodValue = method; + DefaultBodyBuilder(HttpMethod method, URI url) { + this.method = method; this.url = url; } @@ -589,7 +588,7 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { @Override public MockServerHttpRequest body(Publisher body) { applyCookiesIfNecessary(); - return new MockServerHttpRequest(this.methodValue, getUrlToUse(), this.contextPath, + return new MockServerHttpRequest(this.method, getUrlToUse(), this.contextPath, this.headers, this.cookies, this.localAddress, this.remoteAddress, this.sslInfo, body); } diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockMultipartHttpServletRequest.java b/spring-test/src/main/java/org/springframework/mock/web/MockMultipartHttpServletRequest.java index af199e42336..9357c9b5848 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockMultipartHttpServletRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockMultipartHttpServletRequest.java @@ -23,6 +23,7 @@ import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Objects; import jakarta.servlet.ServletContext; import jakarta.servlet.ServletException; @@ -102,12 +103,7 @@ public class MockMultipartHttpServletRequest extends MockHttpServletRequest impl @Override public List getFiles(String name) { List multipartFiles = this.multipartFiles.get(name); - if (multipartFiles != null) { - return multipartFiles; - } - else { - return Collections.emptyList(); - } + return Objects.requireNonNullElse(multipartFiles, Collections.emptyList()); } @Override @@ -141,7 +137,9 @@ public class MockMultipartHttpServletRequest extends MockHttpServletRequest impl @Override public HttpMethod getRequestMethod() { - return HttpMethod.resolve(getMethod()); + String method = getMethod(); + Assert.state(method != null, "Method must not be null"); + return HttpMethod.valueOf(method); } @Override diff --git a/spring-test/src/main/java/org/springframework/mock/web/reactive/function/server/MockServerRequest.java b/spring-test/src/main/java/org/springframework/mock/web/reactive/function/server/MockServerRequest.java index 21440f4a07b..5e5ee6ac4f5 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/reactive/function/server/MockServerRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/web/reactive/function/server/MockServerRequest.java @@ -132,6 +132,7 @@ public final class MockServerRequest implements ServerRequest { } @Override + @Deprecated public String methodName() { return this.method.name(); } diff --git a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java index b009471a26f..d4a27e923e1 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java +++ b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java @@ -34,7 +34,7 @@ import java.util.ArrayList; import java.util.Base64; import java.util.Collection; import java.util.Collections; -import java.util.EnumSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.Locale; import java.util.Map; @@ -589,18 +589,19 @@ public class HttpHeaders implements MultiValueMap, Serializable * Return the value of the {@code Access-Control-Allow-Methods} response header. */ public List getAccessControlAllowMethods() { - List result = new ArrayList<>(); String value = getFirst(ACCESS_CONTROL_ALLOW_METHODS); if (value != null) { String[] tokens = StringUtils.tokenizeToStringArray(value, ","); + List result = new ArrayList<>(); for (String token : tokens) { - HttpMethod resolved = HttpMethod.resolve(token); - if (resolved != null) { - result.add(resolved); - } + HttpMethod method = HttpMethod.valueOf(token); + result.add(method); } + return result; + } + else { + return Collections.emptyList(); } - return result; } /** @@ -682,7 +683,13 @@ public class HttpHeaders implements MultiValueMap, Serializable */ @Nullable public HttpMethod getAccessControlRequestMethod() { - return HttpMethod.resolve(getFirst(ACCESS_CONTROL_REQUEST_METHOD)); + String requestMethod = getFirst(ACCESS_CONTROL_REQUEST_METHOD); + if (requestMethod != null) { + return HttpMethod.valueOf(requestMethod); + } + else { + return null; + } } /** @@ -743,17 +750,15 @@ public class HttpHeaders implements MultiValueMap, Serializable String value = getFirst(ALLOW); if (StringUtils.hasLength(value)) { String[] tokens = StringUtils.tokenizeToStringArray(value, ","); - List result = new ArrayList<>(tokens.length); + Set result = new LinkedHashSet<>(tokens.length); for (String token : tokens) { - HttpMethod resolved = HttpMethod.resolve(token); - if (resolved != null) { - result.add(resolved); - } + HttpMethod method = HttpMethod.valueOf(token); + result.add(method); } - return EnumSet.copyOf(result); + return result; } else { - return EnumSet.noneOf(HttpMethod.class); + return Collections.emptySet(); } } diff --git a/spring-web/src/main/java/org/springframework/http/HttpRequest.java b/spring-web/src/main/java/org/springframework/http/HttpRequest.java index ed82012166e..2c0c71ca185 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/HttpRequest.java @@ -18,8 +18,6 @@ package org.springframework.http; import java.net.URI; -import org.springframework.lang.Nullable; - /** * Represents an HTTP request message, consisting of * {@linkplain #getMethod() method} and {@linkplain #getURI() uri}. @@ -31,22 +29,20 @@ public interface HttpRequest extends HttpMessage { /** * Return the HTTP method of the request. - * @return the HTTP method as an HttpMethod enum value, or {@code null} - * if not resolvable (e.g. in case of a non-standard HTTP method) - * @see #getMethodValue() - * @see HttpMethod#resolve(String) + * @return the HTTP method as an HttpMethod value + * @see HttpMethod#valueOf(String) */ - @Nullable - default HttpMethod getMethod() { - return HttpMethod.resolve(getMethodValue()); - } + HttpMethod getMethod(); /** * Return the HTTP method of the request as a String value. * @return the HTTP method as a plain String * @since 5.0 * @see #getMethod() + * @deprecated in favor of {@link #getMethod()} and + * {@link HttpMethod#name()} */ + @Deprecated String getMethodValue(); /** diff --git a/spring-web/src/main/java/org/springframework/http/client/BufferingClientHttpRequestWrapper.java b/spring-web/src/main/java/org/springframework/http/client/BufferingClientHttpRequestWrapper.java index c7daa6115e2..2af781be97a 100644 --- a/spring-web/src/main/java/org/springframework/http/client/BufferingClientHttpRequestWrapper.java +++ b/spring-web/src/main/java/org/springframework/http/client/BufferingClientHttpRequestWrapper.java @@ -21,7 +21,6 @@ import java.net.URI; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; -import org.springframework.lang.Nullable; import org.springframework.util.StreamUtils; /** @@ -41,12 +40,12 @@ final class BufferingClientHttpRequestWrapper extends AbstractBufferingClientHtt @Override - @Nullable public HttpMethod getMethod() { return this.request.getMethod(); } @Override + @Deprecated public String getMethodValue() { return this.request.getMethodValue(); } diff --git a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequest.java index 5803e19f92a..36440555372 100644 --- a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequest.java @@ -59,8 +59,13 @@ final class HttpComponentsClientHttpRequest extends AbstractBufferingClientHttpR this.httpContext = context; } + @Override + public HttpMethod getMethod() { + return HttpMethod.valueOf(this.httpRequest.getMethod()); + } @Override + @Deprecated public String getMethodValue() { return this.httpRequest.getMethod(); } diff --git a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java index db8cb8cefce..dbf763ac3b8 100644 --- a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java +++ b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java @@ -273,26 +273,31 @@ public class HttpComponentsClientHttpRequestFactory implements ClientHttpRequest * @return the Commons HttpMethodBase object */ protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) { - switch (httpMethod) { - case GET: - return new HttpGet(uri); - case HEAD: - return new HttpHead(uri); - case POST: - return new HttpPost(uri); - case PUT: - return new HttpPut(uri); - case PATCH: - return new HttpPatch(uri); - case DELETE: - return new HttpDelete(uri); - case OPTIONS: - return new HttpOptions(uri); - case TRACE: - return new HttpTrace(uri); - default: - throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod); + if (HttpMethod.GET.equals(httpMethod)) { + return new HttpGet(uri); } + else if (HttpMethod.HEAD.equals(httpMethod)) { + return new HttpHead(uri); + } + else if (HttpMethod.POST.equals(httpMethod)) { + return new HttpPost(uri); + } + else if (HttpMethod.PUT.equals(httpMethod)) { + return new HttpPut(uri); + } + else if (HttpMethod.PATCH.equals(httpMethod)) { + return new HttpPatch(uri); + } + else if (HttpMethod.DELETE.equals(httpMethod)) { + return new HttpDelete(uri); + } + else if (HttpMethod.OPTIONS.equals(httpMethod)) { + return new HttpOptions(uri); + } + else if (HttpMethod.TRACE.equals(httpMethod)) { + return new HttpTrace(uri); + } + throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod); } /** diff --git a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsStreamingClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsStreamingClientHttpRequest.java index ed0cc19fa79..2dc701df6de 100644 --- a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsStreamingClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsStreamingClientHttpRequest.java @@ -31,6 +31,7 @@ import org.apache.http.message.BasicHeader; import org.apache.http.protocol.HttpContext; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.StreamingHttpOutputMessage; import org.springframework.lang.Nullable; @@ -64,8 +65,13 @@ final class HttpComponentsStreamingClientHttpRequest extends AbstractClientHttpR this.httpContext = context; } + @Override + public HttpMethod getMethod() { + return HttpMethod.valueOf(this.httpRequest.getMethod()); + } @Override + @Deprecated public String getMethodValue() { return this.httpRequest.getMethod(); } diff --git a/spring-web/src/main/java/org/springframework/http/client/InterceptingClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/InterceptingClientHttpRequest.java index 57198a5e697..aa617cccf14 100644 --- a/spring-web/src/main/java/org/springframework/http/client/InterceptingClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/InterceptingClientHttpRequest.java @@ -25,7 +25,6 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpRequest; import org.springframework.http.StreamingHttpOutputMessage; -import org.springframework.util.Assert; import org.springframework.util.StreamUtils; /** @@ -62,6 +61,7 @@ class InterceptingClientHttpRequest extends AbstractBufferingClientHttpRequest { } @Override + @Deprecated public String getMethodValue() { return this.method.name(); } @@ -94,7 +94,6 @@ class InterceptingClientHttpRequest extends AbstractBufferingClientHttpRequest { } else { HttpMethod method = request.getMethod(); - Assert.state(method != null, "No standard HTTP method"); ClientHttpRequest delegate = requestFactory.createRequest(request.getURI(), method); request.getHeaders().forEach((key, value) -> delegate.getHeaders().addAll(key, value)); if (body.length > 0) { diff --git a/spring-web/src/main/java/org/springframework/http/client/OkHttp3ClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/OkHttp3ClientHttpRequest.java index 74ff955332c..07c074fd2e6 100644 --- a/spring-web/src/main/java/org/springframework/http/client/OkHttp3ClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/OkHttp3ClientHttpRequest.java @@ -57,6 +57,7 @@ class OkHttp3ClientHttpRequest extends AbstractBufferingClientHttpRequest { } @Override + @Deprecated public String getMethodValue() { return this.method.name(); } diff --git a/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingClientHttpRequest.java index 394a8307635..d47be65d4a2 100644 --- a/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingClientHttpRequest.java @@ -47,8 +47,13 @@ final class SimpleBufferingClientHttpRequest extends AbstractBufferingClientHttp this.outputStreaming = outputStreaming; } + @Override + public HttpMethod getMethod() { + return HttpMethod.valueOf(this.connection.getRequestMethod()); + } @Override + @Deprecated public String getMethodValue() { return this.connection.getRequestMethod(); } diff --git a/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingClientHttpRequest.java index 2b751ac1c21..1a6a9fc42a4 100644 --- a/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingClientHttpRequest.java @@ -55,8 +55,13 @@ final class SimpleStreamingClientHttpRequest extends AbstractClientHttpRequest { this.outputStreaming = outputStreaming; } + @Override + public HttpMethod getMethod() { + return HttpMethod.valueOf(this.connection.getRequestMethod()); + } @Override + @Deprecated public String getMethodValue() { return this.connection.getRequestMethod(); } diff --git a/spring-web/src/main/java/org/springframework/http/client/reactive/HttpComponentsClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/reactive/HttpComponentsClientHttpRequest.java index 440fc681860..40c6af39b58 100644 --- a/spring-web/src/main/java/org/springframework/http/client/reactive/HttpComponentsClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/reactive/HttpComponentsClientHttpRequest.java @@ -40,7 +40,6 @@ import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.lang.Nullable; -import org.springframework.util.Assert; import static org.springframework.http.MediaType.ALL_VALUE; @@ -74,9 +73,7 @@ class HttpComponentsClientHttpRequest extends AbstractClientHttpRequest { @Override public HttpMethod getMethod() { - HttpMethod method = HttpMethod.resolve(this.httpRequest.getMethod()); - Assert.state(method != null, "Method must not be null"); - return method; + return HttpMethod.valueOf(this.httpRequest.getMethod()); } @Override diff --git a/spring-web/src/main/java/org/springframework/http/client/support/HttpRequestWrapper.java b/spring-web/src/main/java/org/springframework/http/client/support/HttpRequestWrapper.java index d3a72f55449..28233e85855 100644 --- a/spring-web/src/main/java/org/springframework/http/client/support/HttpRequestWrapper.java +++ b/spring-web/src/main/java/org/springframework/http/client/support/HttpRequestWrapper.java @@ -21,7 +21,6 @@ import java.net.URI; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpRequest; -import org.springframework.lang.Nullable; import org.springframework.util.Assert; /** @@ -59,7 +58,6 @@ public class HttpRequestWrapper implements HttpRequest { * Return the method of the wrapped request. */ @Override - @Nullable public HttpMethod getMethod() { return this.request.getMethod(); } @@ -68,6 +66,7 @@ public class HttpRequestWrapper implements HttpRequest { * Return the method value of the wrapped request. */ @Override + @Deprecated public String getMethodValue() { return this.request.getMethodValue(); } diff --git a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java index 478ae10542c..75bbdd0dd7a 100644 --- a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java @@ -92,12 +92,12 @@ public class ServletServerHttpRequest implements ServerHttpRequest { } @Override - @Nullable public HttpMethod getMethod() { - return HttpMethod.resolve(this.servletRequest.getMethod()); + return HttpMethod.valueOf(this.servletRequest.getMethod()); } @Override + @Deprecated public String getMethodValue() { return this.servletRequest.getMethod(); } diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java b/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java index 956808e57ae..c93b4ac286e 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java @@ -47,7 +47,7 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder { private final HttpHeaders headers; - private String httpMethodValue; + private HttpMethod httpMethod; @Nullable private String uriPath; @@ -71,7 +71,7 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder { this.uri = original.getURI(); this.headers = HttpHeaders.writableHttpHeaders(original.getHeaders()); - this.httpMethodValue = original.getMethodValue(); + this.httpMethod = original.getMethod(); this.contextPath = original.getPath().contextPath().value(); this.remoteAddress = original.getRemoteAddress(); this.body = original.getBody(); @@ -81,7 +81,8 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder { @Override public ServerHttpRequest.Builder method(HttpMethod httpMethod) { - this.httpMethodValue = httpMethod.name(); + Assert.notNull(httpMethod, "HttpMethod must not be null"); + this.httpMethod = httpMethod; return this; } @@ -132,7 +133,7 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder { @Override public ServerHttpRequest build() { return new MutatedServerHttpRequest(getUriToUse(), this.contextPath, - this.httpMethodValue, this.sslInfo, this.remoteAddress, this.headers, this.body, this.originalRequest); + this.httpMethod, this.sslInfo, this.remoteAddress, this.headers, this.body, this.originalRequest); } private URI getUriToUse() { @@ -176,7 +177,7 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder { private static class MutatedServerHttpRequest extends AbstractServerHttpRequest { - private final String methodValue; + private final HttpMethod method; @Nullable private final SslInfo sslInfo; @@ -190,11 +191,11 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder { public MutatedServerHttpRequest(URI uri, @Nullable String contextPath, - String methodValue, @Nullable SslInfo sslInfo, @Nullable InetSocketAddress remoteAddress, + HttpMethod method, @Nullable SslInfo sslInfo, @Nullable InetSocketAddress remoteAddress, HttpHeaders headers, Flux body, ServerHttpRequest originalRequest) { super(uri, contextPath, headers); - this.methodValue = methodValue; + this.method = method; this.remoteAddress = (remoteAddress != null ? remoteAddress : originalRequest.getRemoteAddress()); this.sslInfo = (sslInfo != null ? sslInfo : originalRequest.getSslInfo()); this.body = body; @@ -202,8 +203,14 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder { } @Override + public HttpMethod getMethod() { + return this.method; + } + + @Override + @Deprecated public String getMethodValue() { - return this.methodValue; + return this.method.name(); } @Override diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java index ab0fdb62d18..ad809acf011 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java @@ -36,6 +36,7 @@ import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.NettyDataBufferFactory; import org.springframework.http.HttpCookie; import org.springframework.http.HttpLogging; +import org.springframework.http.HttpMethod; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; @@ -138,8 +139,13 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest { return uri; } + @Override + public HttpMethod getMethod() { + return HttpMethod.valueOf(this.request.method().name()); + } @Override + @Deprecated public String getMethodValue() { return this.request.method().name(); } diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequestDecorator.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequestDecorator.java index df1086d21c9..d5f34dcde32 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequestDecorator.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequestDecorator.java @@ -61,12 +61,12 @@ public class ServerHttpRequestDecorator implements ServerHttpRequest { } @Override - @Nullable public HttpMethod getMethod() { return getDelegate().getMethod(); } @Override + @Deprecated public String getMethodValue() { return getDelegate().getMethodValue(); } diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java index 93be805e7ff..cd5d47c6dfe 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java @@ -41,6 +41,7 @@ import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.core.io.buffer.DefaultDataBufferFactory; import org.springframework.http.HttpCookie; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; @@ -158,8 +159,13 @@ class ServletServerHttpRequest extends AbstractServerHttpRequest { return (headers != null ? headers : headerValues); } + @Override + public HttpMethod getMethod() { + return HttpMethod.valueOf(this.request.getMethod()); + } @Override + @Deprecated public String getMethodValue() { return this.request.getMethod(); } diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java index 6de7dfb3999..f21abb16bfc 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpRequest.java @@ -35,6 +35,7 @@ import reactor.core.publisher.Flux; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.http.HttpCookie; +import org.springframework.http.HttpMethod; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.LinkedMultiValueMap; @@ -77,6 +78,12 @@ class UndertowServerHttpRequest extends AbstractServerHttpRequest { } @Override + public HttpMethod getMethod() { + return HttpMethod.valueOf(this.exchange.getRequestMethod().toString()); + } + + @Override + @Deprecated public String getMethodValue() { return this.exchange.getRequestMethod().toString(); } diff --git a/spring-web/src/main/java/org/springframework/web/HttpRequestMethodNotSupportedException.java b/spring-web/src/main/java/org/springframework/web/HttpRequestMethodNotSupportedException.java index 4e1168d84de..fec3b285266 100644 --- a/spring-web/src/main/java/org/springframework/web/HttpRequestMethodNotSupportedException.java +++ b/spring-web/src/main/java/org/springframework/web/HttpRequestMethodNotSupportedException.java @@ -16,10 +16,8 @@ package org.springframework.web; -import java.util.ArrayList; import java.util.Collection; -import java.util.EnumSet; -import java.util.List; +import java.util.LinkedHashSet; import java.util.Set; import jakarta.servlet.ServletException; @@ -117,14 +115,12 @@ public class HttpRequestMethodNotSupportedException extends ServletException { if (this.supportedMethods == null) { return null; } - List supportedMethods = new ArrayList<>(this.supportedMethods.length); + Set supportedMethods = new LinkedHashSet<>(this.supportedMethods.length); for (String value : this.supportedMethods) { - HttpMethod resolved = HttpMethod.resolve(value); - if (resolved != null) { - supportedMethods.add(resolved); - } + HttpMethod method = HttpMethod.valueOf(value); + supportedMethods.add(method); } - return EnumSet.copyOf(supportedMethods); + return supportedMethods; } } diff --git a/spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java b/spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java index 1b2917cf5ae..946a19ad60a 100644 --- a/spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java +++ b/spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java @@ -118,9 +118,8 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ * Return the HTTP method of the request. * @since 4.0.2 */ - @Nullable public HttpMethod getHttpMethod() { - return HttpMethod.resolve(getRequest().getMethod()); + return HttpMethod.valueOf(getRequest().getMethod()); } @Override diff --git a/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java b/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java index 3d69879e1ed..a0357bf720c 100644 --- a/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java +++ b/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java @@ -258,7 +258,7 @@ public class CorsConfiguration { this.resolvedMethods = null; break; } - this.resolvedMethods.add(HttpMethod.resolve(method)); + this.resolvedMethods.add(HttpMethod.valueOf(method)); } } else { @@ -302,7 +302,7 @@ public class CorsConfiguration { this.resolvedMethods = null; } else if (this.resolvedMethods != null) { - this.resolvedMethods.add(HttpMethod.resolve(method)); + this.resolvedMethods.add(HttpMethod.valueOf(method)); } } } @@ -447,7 +447,7 @@ public class CorsConfiguration { if (this.allowedMethods == null) { this.allowedMethods = DEFAULT_PERMIT_METHODS; this.resolvedMethods = DEFAULT_PERMIT_METHODS - .stream().map(HttpMethod::resolve).collect(Collectors.toList()); + .stream().map(HttpMethod::valueOf).collect(Collectors.toList()); } if (this.allowedHeaders == null) { this.allowedHeaders = DEFAULT_PERMIT_ALL; diff --git a/spring-web/src/main/java/org/springframework/web/filter/reactive/HiddenHttpMethodFilter.java b/spring-web/src/main/java/org/springframework/web/filter/reactive/HiddenHttpMethodFilter.java index 90d18497169..d06cac32bc9 100644 --- a/spring-web/src/main/java/org/springframework/web/filter/reactive/HiddenHttpMethodFilter.java +++ b/spring-web/src/main/java/org/springframework/web/filter/reactive/HiddenHttpMethodFilter.java @@ -91,8 +91,7 @@ public class HiddenHttpMethodFilter implements WebFilter { } private ServerWebExchange mapExchange(ServerWebExchange exchange, String methodParamValue) { - HttpMethod httpMethod = HttpMethod.resolve(methodParamValue.toUpperCase(Locale.ENGLISH)); - Assert.notNull(httpMethod, () -> "HttpMethod '" + methodParamValue + "' not supported"); + HttpMethod httpMethod = HttpMethod.valueOf(methodParamValue.toUpperCase(Locale.ENGLISH)); if (ALLOWED_METHODS.contains(httpMethod)) { return exchange.mutate().request(builder -> builder.method(httpMethod)).build(); } diff --git a/spring-web/src/main/java/org/springframework/web/multipart/MultipartHttpServletRequest.java b/spring-web/src/main/java/org/springframework/web/multipart/MultipartHttpServletRequest.java index 0fa6a8b726f..40f678469da 100644 --- a/spring-web/src/main/java/org/springframework/web/multipart/MultipartHttpServletRequest.java +++ b/spring-web/src/main/java/org/springframework/web/multipart/MultipartHttpServletRequest.java @@ -52,7 +52,6 @@ public interface MultipartHttpServletRequest extends HttpServletRequest, Multipa /** * Return this request's method as a convenient HttpMethod instance. */ - @Nullable HttpMethod getRequestMethod(); /** diff --git a/spring-web/src/main/java/org/springframework/web/multipart/support/AbstractMultipartHttpServletRequest.java b/spring-web/src/main/java/org/springframework/web/multipart/support/AbstractMultipartHttpServletRequest.java index fb705a9445e..a331034bf10 100644 --- a/spring-web/src/main/java/org/springframework/web/multipart/support/AbstractMultipartHttpServletRequest.java +++ b/spring-web/src/main/java/org/springframework/web/multipart/support/AbstractMultipartHttpServletRequest.java @@ -64,7 +64,7 @@ public abstract class AbstractMultipartHttpServletRequest extends HttpServletReq @Override public HttpMethod getRequestMethod() { - return HttpMethod.resolve(getRequest().getMethod()); + return HttpMethod.valueOf(getRequest().getMethod()); } @Override diff --git a/spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java b/spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java index 0bad35ed16c..f802ac500bb 100644 --- a/spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java +++ b/spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java @@ -28,8 +28,8 @@ import java.util.Arrays; import java.util.Base64; import java.util.Calendar; import java.util.Collections; -import java.util.EnumSet; import java.util.GregorianCalendar; +import java.util.LinkedHashSet; import java.util.List; import java.util.Locale; import java.util.Map.Entry; @@ -129,7 +129,7 @@ public class HttpHeadersTests { @Test void allow() { - EnumSet methods = EnumSet.of(HttpMethod.GET, HttpMethod.POST); + Set methods = new LinkedHashSet<>(Arrays.asList(HttpMethod.GET, HttpMethod.POST)); headers.setAllow(methods); assertThat(headers.getAllow()).as("Invalid Allow header").isEqualTo(methods); assertThat(headers.getFirst("Allow")).as("Invalid Allow header").isEqualTo("GET,POST"); diff --git a/spring-web/src/test/java/org/springframework/http/client/InterceptingClientHttpRequestFactoryTests.java b/spring-web/src/test/java/org/springframework/http/client/InterceptingClientHttpRequestFactoryTests.java index 82a3e79cb4c..7dbcc58f7e1 100644 --- a/spring-web/src/test/java/org/springframework/http/client/InterceptingClientHttpRequestFactoryTests.java +++ b/spring-web/src/test/java/org/springframework/http/client/InterceptingClientHttpRequestFactoryTests.java @@ -268,6 +268,7 @@ public class InterceptingClientHttpRequestFactoryTests { } @Override + @Deprecated public String getMethodValue() { return method.name(); } diff --git a/spring-web/src/test/java/org/springframework/http/client/reactive/ClientHttpConnectorTests.java b/spring-web/src/test/java/org/springframework/http/client/reactive/ClientHttpConnectorTests.java index 0f30c2391e0..f6d577d04ac 100644 --- a/spring-web/src/test/java/org/springframework/http/client/reactive/ClientHttpConnectorTests.java +++ b/spring-web/src/test/java/org/springframework/http/client/reactive/ClientHttpConnectorTests.java @@ -25,9 +25,9 @@ import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; -import java.util.EnumSet; import java.util.List; import java.util.Random; +import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.function.Consumer; @@ -62,8 +62,8 @@ public class ClientHttpConnectorTests { private static final int BUF_SIZE = 1024; - private static final EnumSet METHODS_WITH_BODY = - EnumSet.of(HttpMethod.PUT, HttpMethod.POST, HttpMethod.PATCH); + private static final Set METHODS_WITH_BODY = + Set.of(HttpMethod.PUT, HttpMethod.POST, HttpMethod.PATCH); private final MockWebServer server = new MockWebServer(); diff --git a/spring-web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java b/spring-web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java index cc684fe001b..e2a46d5919e 100644 --- a/spring-web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java +++ b/spring-web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java @@ -23,7 +23,6 @@ import java.lang.annotation.Target; import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.ArrayList; -import java.util.EnumSet; import java.util.List; import java.util.Set; import java.util.stream.Stream; @@ -275,7 +274,7 @@ class RestTemplateIntegrationTests extends AbstractMockWebServerTests { setUpClient(clientHttpRequestFactory); Set allowed = template.optionsForAllow(new URI(baseUrl + "/get")); - assertThat(allowed).as("Invalid response").isEqualTo(EnumSet.of(HttpMethod.GET, HttpMethod.OPTIONS, HttpMethod.HEAD, HttpMethod.TRACE)); + assertThat(allowed).as("Invalid response").isEqualTo(Set.of(HttpMethod.GET, HttpMethod.OPTIONS, HttpMethod.HEAD, HttpMethod.TRACE)); } @ParameterizedRestTemplateTest diff --git a/spring-web/src/test/java/org/springframework/web/client/RestTemplateTests.java b/spring-web/src/test/java/org/springframework/web/client/RestTemplateTests.java index c95c60cb28f..85144efdeda 100644 --- a/spring-web/src/test/java/org/springframework/web/client/RestTemplateTests.java +++ b/spring-web/src/test/java/org/springframework/web/client/RestTemplateTests.java @@ -21,7 +21,6 @@ import java.io.IOException; import java.net.URI; import java.util.Arrays; import java.util.Collections; -import java.util.EnumSet; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -580,7 +579,7 @@ class RestTemplateTests { mockSentRequest(OPTIONS, "https://example.com"); mockResponseStatus(HttpStatus.OK); HttpHeaders responseHeaders = new HttpHeaders(); - EnumSet expected = EnumSet.of(GET, POST); + Set expected = Set.of(GET, POST); responseHeaders.setAllow(expected); given(response.getHeaders()).willReturn(responseHeaders); diff --git a/spring-web/src/test/java/org/springframework/web/filter/reactive/HiddenHttpMethodFilterTests.java b/spring-web/src/test/java/org/springframework/web/filter/reactive/HiddenHttpMethodFilterTests.java index 4bf21aa0803..8b360081c7e 100644 --- a/spring-web/src/test/java/org/springframework/web/filter/reactive/HiddenHttpMethodFilterTests.java +++ b/spring-web/src/test/java/org/springframework/web/filter/reactive/HiddenHttpMethodFilterTests.java @@ -20,7 +20,6 @@ import java.time.Duration; import org.junit.jupiter.api.Test; import reactor.core.publisher.Mono; -import reactor.test.StepVerifier; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -75,16 +74,6 @@ public class HiddenHttpMethodFilterTests { assertThat(this.filterChain.getHttpMethod()).isEqualTo(HttpMethod.DELETE); } - @Test - public void filterWithInvalidMethodValue() { - StepVerifier.create(postForm("_method=INVALID")) - .consumeErrorWith(error -> { - assertThat(error).isInstanceOf(IllegalArgumentException.class); - assertThat(error.getMessage()).isEqualTo("HttpMethod 'INVALID' not supported"); - }) - .verify(); - } - @Test public void filterWithHttpPut() { diff --git a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java index d6d971c4ed5..6f6b5011081 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java @@ -32,6 +32,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; import org.springframework.http.HttpRequest; import org.springframework.http.server.ServletServerHttpRequest; import org.springframework.util.LinkedMultiValueMap; @@ -680,6 +681,12 @@ class UriComponentsBuilderTests { void fromHttpRequestWithEmptyScheme() { HttpRequest request = new HttpRequest() { @Override + public HttpMethod getMethod() { + return HttpMethod.GET; + } + + @Override + @Deprecated public String getMethodValue() { return "GET"; } diff --git a/spring-web/src/test/kotlin/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverKotlinTests.kt b/spring-web/src/test/kotlin/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverKotlinTests.kt index 281771bd636..e49b805a01b 100644 --- a/spring-web/src/test/kotlin/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverKotlinTests.kt +++ b/spring-web/src/test/kotlin/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverKotlinTests.kt @@ -25,10 +25,6 @@ import org.springframework.core.annotation.SynthesizingMethodParameter import org.springframework.core.convert.support.DefaultConversionService import org.springframework.http.HttpMethod import org.springframework.http.MediaType -import org.springframework.web.testfixture.servlet.MockHttpServletRequest -import org.springframework.web.testfixture.servlet.MockHttpServletResponse -import org.springframework.web.testfixture.servlet.MockMultipartFile -import org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest import org.springframework.util.ReflectionUtils import org.springframework.web.bind.MissingServletRequestParameterException import org.springframework.web.bind.annotation.RequestParam @@ -39,6 +35,10 @@ import org.springframework.web.context.request.NativeWebRequest import org.springframework.web.context.request.ServletWebRequest import org.springframework.web.multipart.MultipartFile import org.springframework.web.multipart.support.MissingServletRequestPartException +import org.springframework.web.testfixture.servlet.MockHttpServletRequest +import org.springframework.web.testfixture.servlet.MockHttpServletResponse +import org.springframework.web.testfixture.servlet.MockMultipartFile +import org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest /** * Kotlin test fixture for [RequestParamMethodArgumentResolver]. @@ -156,7 +156,7 @@ class RequestParamMethodArgumentResolverKotlinTests { @Test fun resolveNullableRequiredWithoutMultipartParameter() { - request.method = HttpMethod.POST.name + request.method = HttpMethod.POST.name() request.contentType = MediaType.MULTIPART_FORM_DATA_VALUE var result = resolver.resolveArgument(nullableMultipartParamRequired, null, webRequest, binderFactory) @@ -176,7 +176,7 @@ class RequestParamMethodArgumentResolverKotlinTests { @Test fun resolveNullableNotRequiredWithoutMultipartParameter() { - request.method = HttpMethod.POST.name + request.method = HttpMethod.POST.name() request.contentType = MediaType.MULTIPART_FORM_DATA_VALUE var result = resolver.resolveArgument(nullableMultipartParamNotRequired, null, webRequest, binderFactory) @@ -196,7 +196,7 @@ class RequestParamMethodArgumentResolverKotlinTests { @Test fun resolveNonNullableRequiredWithoutMultipartParameter() { - request.method = HttpMethod.POST.name + request.method = HttpMethod.POST.name() request.contentType = MediaType.MULTIPART_FORM_DATA_VALUE assertThatExceptionOfType(MissingServletRequestPartException::class.java).isThrownBy { @@ -217,7 +217,7 @@ class RequestParamMethodArgumentResolverKotlinTests { @Test fun resolveNonNullableNotRequiredWithoutMultipartParameter() { - request.method = HttpMethod.POST.name + request.method = HttpMethod.POST.name() request.contentType = MediaType.MULTIPART_FORM_DATA_VALUE assertThatExceptionOfType(NullPointerException::class.java).isThrownBy { diff --git a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/client/reactive/MockClientHttpRequest.java b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/client/reactive/MockClientHttpRequest.java index e05438231d2..67d08a0d089 100644 --- a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/client/reactive/MockClientHttpRequest.java +++ b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/client/reactive/MockClientHttpRequest.java @@ -96,6 +96,7 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest implements } @Override + @Deprecated public String getMethodValue() { return this.httpMethod.name(); } diff --git a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/server/reactive/MockServerHttpRequest.java b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/server/reactive/MockServerHttpRequest.java index b3ed7ea547a..5a28bd98b13 100644 --- a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/server/reactive/MockServerHttpRequest.java +++ b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/http/server/reactive/MockServerHttpRequest.java @@ -55,10 +55,7 @@ import org.springframework.web.util.UriComponentsBuilder; */ public final class MockServerHttpRequest extends AbstractServerHttpRequest { - /** - * String representation of one of {@link HttpMethod} or not empty custom method (e.g. CONNECT). - */ - private final String httpMethod; + private final HttpMethod httpMethod; private final MultiValueMap cookies; @@ -73,13 +70,12 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { private final Flux body; - private MockServerHttpRequest(String httpMethod, + private MockServerHttpRequest(HttpMethod httpMethod, URI uri, @Nullable String contextPath, HttpHeaders headers, MultiValueMap cookies, @Nullable InetSocketAddress localAddress, @Nullable InetSocketAddress remoteAddress, @Nullable SslInfo sslInfo, Publisher body) { super(uri, contextPath, headers); - Assert.isTrue(StringUtils.hasText(httpMethod), "HTTP method is required."); this.httpMethod = httpMethod; this.cookies = cookies; this.localAddress = localAddress; @@ -90,14 +86,14 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { @Override - @Nullable public HttpMethod getMethod() { - return HttpMethod.resolve(this.httpMethod); + return this.httpMethod; } @Override + @Deprecated public String getMethodValue() { - return this.httpMethod; + return this.httpMethod.name(); } @Override @@ -218,7 +214,7 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { public static BodyBuilder method(HttpMethod method, URI url) { Assert.notNull(method, "HTTP method is required. " + "For a custom HTTP method, please provide a String HTTP method value."); - return new DefaultBodyBuilder(method.name(), url); + return new DefaultBodyBuilder(method, url); } /** @@ -242,9 +238,12 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { * @param vars variables to expand into the template * @return the created builder * @since 5.2.7 + * @deprecated in favor of {@link #method(HttpMethod, String, Object...)} */ + @Deprecated public static BodyBuilder method(String httpMethod, String uri, Object... vars) { - return new DefaultBodyBuilder(httpMethod, toUri(uri, vars)); + Assert.isTrue(StringUtils.hasText(httpMethod), "HTTP method is required."); + return new DefaultBodyBuilder(HttpMethod.valueOf(httpMethod), toUri(uri, vars)); } private static URI toUri(String uri, Object[] vars) { @@ -427,7 +426,7 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { private static class DefaultBodyBuilder implements BodyBuilder { - private final String methodValue; + private final HttpMethod method; private final URI url; @@ -449,8 +448,8 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { @Nullable private SslInfo sslInfo; - DefaultBodyBuilder(String method, URI url) { - this.methodValue = method; + DefaultBodyBuilder(HttpMethod method, URI url) { + this.method = method; this.url = url; } @@ -589,7 +588,7 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest { @Override public MockServerHttpRequest body(Publisher body) { applyCookiesIfNecessary(); - return new MockServerHttpRequest(this.methodValue, getUrlToUse(), this.contextPath, + return new MockServerHttpRequest(this.method, getUrlToUse(), this.contextPath, this.headers, this.cookies, this.localAddress, this.remoteAddress, this.sslInfo, body); } diff --git a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockMultipartHttpServletRequest.java b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockMultipartHttpServletRequest.java index 88c51b9498d..10a4443fc48 100644 --- a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockMultipartHttpServletRequest.java +++ b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockMultipartHttpServletRequest.java @@ -141,7 +141,13 @@ public class MockMultipartHttpServletRequest extends MockHttpServletRequest impl @Override public HttpMethod getRequestMethod() { - return HttpMethod.resolve(getMethod()); + String method = getMethod(); + if (method != null) { + return HttpMethod.valueOf(method); + } + else { + return null; + } } @Override diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java index a63ba76bf7e..955f9834dc1 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java @@ -27,6 +27,7 @@ import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.core.io.buffer.DefaultDataBufferFactory; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; import org.springframework.http.HttpRequest; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseCookie; @@ -50,6 +51,12 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder { private final URI empty = URI.create(""); @Override + public HttpMethod getMethod() { + return HttpMethod.valueOf("UNKNOWN"); + } + + @Override + @Deprecated public String getMethodValue() { return "UNKNOWN"; } @@ -210,7 +217,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder { return new DefaultClientResponse(httpResponse, this.strategies, this.originalResponse != null ? this.originalResponse.logPrefix() : "", - this.request.getMethodValue() + " " + this.request.getURI(), + this.request.getMethod() + " " + this.request.getURI(), () -> this.request); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java index efaf758ef43..d6627ce2f6e 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java @@ -378,6 +378,7 @@ class DefaultWebClient implements WebClient { return httpMethod; } @Override + @Deprecated public String getMethodValue() { return httpMethod.name(); } @@ -667,7 +668,7 @@ class DefaultWebClient implements WebClient { } private Mono insertCheckpoint(Mono result, int statusCode, HttpRequest request) { - String httpMethod = request.getMethodValue(); + HttpMethod httpMethod = request.getMethod(); URI uri = request.getURI(); String description = statusCode + " from " + httpMethod + " " + uri + " [DefaultWebClient]"; return result.checkpoint(description); diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFunctions.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFunctions.java index b6b4d58b6c9..b85566ab482 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFunctions.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFunctions.java @@ -150,6 +150,7 @@ public abstract class ExchangeFunctions { } @Override + @Deprecated public String getMethodValue() { return request.method().name(); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java index ab211917b5f..0d5137019b6 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java @@ -73,7 +73,7 @@ public class WebClientResponseException extends WebClientException { private static String initMessage(int status, String reasonPhrase, @Nullable HttpRequest request) { return status + " " + reasonPhrase + - (request != null ? " from " + request.getMethodValue() + " " + request.getURI() : ""); + (request != null ? " from " + request.getMethod() + " " + request.getURI() : ""); } /** diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java index 7e7d8b4bf1d..1514af903ea 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java @@ -39,6 +39,7 @@ import org.springframework.core.codec.Hints; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.http.HttpCookie; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; import org.springframework.http.HttpRange; import org.springframework.http.MediaType; import org.springframework.http.codec.HttpMessageReader; @@ -108,8 +109,14 @@ class DefaultServerRequest implements ServerRequest { } @Override + public HttpMethod method() { + return request().getMethod(); + } + + @Override + @Deprecated public String methodName() { - return request().getMethodValue(); + return request().getMethod().name(); } @Override diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequestBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequestBuilder.java index 174899b8894..50f5eb5bd96 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequestBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequestBuilder.java @@ -71,7 +71,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder { private final ServerWebExchange exchange; - private String methodName; + private HttpMethod method; private URI uri; @@ -88,7 +88,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder { Assert.notNull(other, "ServerRequest must not be null"); this.messageReaders = other.messageReaders(); this.exchange = other.exchange(); - this.methodName = other.methodName(); + this.method = other.method(); this.uri = other.uri(); this.headers.addAll(other.headers().asHttpHeaders()); this.cookies.addAll(other.cookies()); @@ -99,7 +99,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder { @Override public ServerRequest.Builder method(HttpMethod method) { Assert.notNull(method, "HttpMethod must not be null"); - this.methodName = method.name(); + this.method = method; return this; } @@ -177,7 +177,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder { @Override public ServerRequest build() { ServerHttpRequest serverHttpRequest = new BuiltServerHttpRequest(this.exchange.getRequest().getId(), - this.methodName, this.uri, this.headers, this.cookies, this.body); + this.method, this.uri, this.headers, this.cookies, this.body); ServerWebExchange exchange = new DelegatingServerWebExchange( serverHttpRequest, this.attributes, this.exchange, this.messageReaders); return new DefaultServerRequest(exchange, this.messageReaders); @@ -190,7 +190,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder { private final String id; - private final String method; + private final HttpMethod method; private final URI uri; @@ -204,7 +204,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder { private final Flux body; - public BuiltServerHttpRequest(String id, String method, URI uri, HttpHeaders headers, + public BuiltServerHttpRequest(String id, HttpMethod method, URI uri, HttpHeaders headers, MultiValueMap cookies, Flux body) { this.id = id; @@ -248,10 +248,16 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder { } @Override - public String getMethodValue() { + public HttpMethod getMethod() { return this.method; } + @Override + @Deprecated + public String getMethodValue() { + return this.method.name(); + } + @Override public URI getURI() { return this.uri; diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java index 25b53f373b5..b62189c041b 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java @@ -21,7 +21,6 @@ import java.time.Instant; import java.time.ZonedDateTime; import java.util.Arrays; import java.util.Collections; -import java.util.EnumSet; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.List; @@ -297,7 +296,7 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder { */ abstract static class AbstractServerResponse implements ServerResponse { - private static final Set SAFE_METHODS = EnumSet.of(HttpMethod.GET, HttpMethod.HEAD); + private static final Set SAFE_METHODS = Set.of(HttpMethod.GET, HttpMethod.HEAD); final int statusCode; diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java index 444d7d44a85..3ae0b2f9c34 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java @@ -21,10 +21,10 @@ import java.net.URI; import java.security.Principal; import java.util.Arrays; import java.util.Collections; -import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Optional; @@ -437,12 +437,12 @@ public abstract class RequestPredicates { public HttpMethodPredicate(HttpMethod httpMethod) { Assert.notNull(httpMethod, "HttpMethod must not be null"); - this.httpMethods = EnumSet.of(httpMethod); + this.httpMethods = Collections.singleton(httpMethod); } public HttpMethodPredicate(HttpMethod... httpMethods) { Assert.notEmpty(httpMethods, "HttpMethods must not be empty"); - this.httpMethods = EnumSet.copyOf(Arrays.asList(httpMethods)); + this.httpMethods = new LinkedHashSet<>(Arrays.asList(httpMethods)); } @Override @@ -453,16 +453,15 @@ public abstract class RequestPredicates { return match; } - @Nullable private static HttpMethod method(ServerRequest request) { if (CorsUtils.isPreFlightRequest(request.exchange().getRequest())) { String accessControlRequestMethod = request.headers().firstHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD); - return HttpMethod.resolve(accessControlRequestMethod); - } - else { - return request.method(); + if (accessControlRequestMethod != null) { + return HttpMethod.valueOf(accessControlRequestMethod); + } } + return request.method(); } @Override @@ -968,6 +967,7 @@ public abstract class RequestPredicates { } @Override + @Deprecated public String methodName() { return this.request.methodName(); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ResourceHandlerFunction.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ResourceHandlerFunction.java index 95b91fb0f44..66f8c097cf3 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ResourceHandlerFunction.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ResourceHandlerFunction.java @@ -22,7 +22,6 @@ import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.net.URL; -import java.util.EnumSet; import java.util.Set; import reactor.core.publisher.Mono; @@ -42,7 +41,7 @@ import org.springframework.web.reactive.function.BodyInserters; class ResourceHandlerFunction implements HandlerFunction { private static final Set SUPPORTED_METHODS = - EnumSet.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS); + Set.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS); private final Resource resource; @@ -56,20 +55,19 @@ class ResourceHandlerFunction implements HandlerFunction { @Override public Mono handle(ServerRequest request) { HttpMethod method = request.method(); - if (method != null) { - switch (method) { - case GET: - return EntityResponse.fromObject(this.resource).build() - .map(response -> response); - case HEAD: - Resource headResource = new HeadMethodResource(this.resource); - return EntityResponse.fromObject(headResource).build() - .map(response -> response); - case OPTIONS: - return ServerResponse.ok() - .allow(SUPPORTED_METHODS) - .body(BodyInserters.empty()); - } + if (HttpMethod.GET.equals(method)) { + return EntityResponse.fromObject(this.resource).build() + .map(response -> response); + } + else if (HttpMethod.HEAD.equals(method)) { + Resource headResource = new HeadMethodResource(this.resource); + return EntityResponse.fromObject(headResource).build() + .map(response -> response); + } + else if (HttpMethod.OPTIONS.equals(method)) { + return ServerResponse.ok() + .allow(SUPPORTED_METHODS) + .body(BodyInserters.empty()); } return ServerResponse.status(HttpStatus.METHOD_NOT_ALLOWED) .allow(SUPPORTED_METHODS) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerRequest.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerRequest.java index 5b44e5a4564..9bbab1a7eab 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerRequest.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerRequest.java @@ -70,15 +70,14 @@ public interface ServerRequest { * @return the HTTP method as an HttpMethod enum value, or {@code null} * if not resolvable (e.g. in case of a non-standard HTTP method) */ - @Nullable - default HttpMethod method() { - return HttpMethod.resolve(methodName()); - } + HttpMethod method(); /** * Get the name of the HTTP method. * @return the HTTP method as a String + * @deprecated in favor of {@link #method()} */ + @Deprecated String methodName(); /** diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/support/ServerRequestWrapper.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/support/ServerRequestWrapper.java index 20127d8a634..ac7faf5743f 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/support/ServerRequestWrapper.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/support/ServerRequestWrapper.java @@ -85,6 +85,7 @@ public class ServerRequestWrapper implements ServerRequest { } @Override + @Deprecated public String methodName() { return this.delegate.methodName(); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java index 8a8ba5894d1..125593a8879 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java @@ -22,7 +22,6 @@ import java.nio.charset.StandardCharsets; import java.time.Instant; import java.util.ArrayList; import java.util.Collections; -import java.util.EnumSet; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -90,7 +89,7 @@ import org.springframework.web.server.WebHandler; */ public class ResourceWebHandler implements WebHandler, InitializingBean { - private static final Set SUPPORTED_METHODS = EnumSet.of(HttpMethod.GET, HttpMethod.HEAD); + private static final Set SUPPORTED_METHODS = Set.of(HttpMethod.GET, HttpMethod.HEAD); private static final Log logger = LogFactory.getLog(ResourceWebHandler.class); @@ -407,7 +406,7 @@ public class ResourceWebHandler implements WebHandler, InitializingBean { })) .flatMap(resource -> { try { - if (HttpMethod.OPTIONS.matches(exchange.getRequest().getMethodValue())) { + if (HttpMethod.OPTIONS.equals(exchange.getRequest().getMethod())) { exchange.getResponse().getHeaders().add("Allow", "GET,HEAD,OPTIONS"); return Mono.empty(); } @@ -416,7 +415,7 @@ public class ResourceWebHandler implements WebHandler, InitializingBean { HttpMethod httpMethod = exchange.getRequest().getMethod(); if (!SUPPORTED_METHODS.contains(httpMethod)) { return Mono.error(new MethodNotAllowedException( - exchange.getRequest().getMethodValue(), SUPPORTED_METHODS)); + exchange.getRequest().getMethod(), SUPPORTED_METHODS)); } // Header phase 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 3583f8657d9..600708a7736 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 @@ -127,7 +127,7 @@ public final class RequestMethodsRequestCondition extends AbstractRequestConditi return matchPreFlight(exchange.getRequest()); } if (getMethods().isEmpty()) { - if (RequestMethod.OPTIONS.name().equals(exchange.getRequest().getMethodValue())) { + if (HttpMethod.OPTIONS.equals(exchange.getRequest().getMethod())) { return null; // We handle OPTIONS transparently, so don't match if no explicit declarations } return this; diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMapping.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMapping.java index fe4478e646e..a581cc6e0c3 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMapping.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMapping.java @@ -20,12 +20,12 @@ import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; -import java.util.EnumSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; import reactor.core.publisher.Mono; @@ -171,9 +171,9 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe ServerHttpRequest request = exchange.getRequest(); if (helper.hasMethodsMismatch()) { - String httpMethod = request.getMethodValue(); + HttpMethod httpMethod = request.getMethod(); Set methods = helper.getAllowedMethods(); - if (HttpMethod.OPTIONS.matches(httpMethod)) { + if (HttpMethod.OPTIONS.equals(httpMethod)) { Set mediaTypes = helper.getConsumablePatchMediaTypes(); HttpOptionsHandler handler = new HttpOptionsHandler(methods, mediaTypes); return new HandlerMethod(handler, HTTP_OPTIONS_HANDLE_METHOD); @@ -269,7 +269,7 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe public Set getAllowedMethods() { return this.partialMatches.stream(). flatMap(m -> m.getInfo().getMethodsCondition().getMethods().stream()). - map(requestMethod -> HttpMethod.resolve(requestMethod.name())). + map(requestMethod -> HttpMethod.valueOf(requestMethod.name())). collect(Collectors.toSet()); } @@ -392,8 +392,8 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe private static Set initAllowedHttpMethods(Set declaredMethods) { if (declaredMethods.isEmpty()) { - return EnumSet.allOf(HttpMethod.class).stream() - .filter(method -> method != HttpMethod.TRACE) + return Stream.of(HttpMethod.values()) + .filter(method -> !HttpMethod.TRACE.equals(method)) .collect(Collectors.toSet()); } else { diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageReaderArgumentResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageReaderArgumentResolver.java index 082bd5e88bc..79dde79b078 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageReaderArgumentResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageReaderArgumentResolver.java @@ -18,7 +18,6 @@ package org.springframework.web.reactive.result.method.annotation; import java.lang.annotation.Annotation; import java.util.ArrayList; -import java.util.EnumSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -70,7 +69,7 @@ import org.springframework.web.server.UnsupportedMediaTypeStatusException; public abstract class AbstractMessageReaderArgumentResolver extends HandlerMethodArgumentResolverSupport { private static final Set SUPPORTED_METHODS = - EnumSet.of(HttpMethod.POST, HttpMethod.PUT, HttpMethod.PATCH); + Set.of(HttpMethod.POST, HttpMethod.PUT, HttpMethod.PATCH); private final List> messageReaders; @@ -203,7 +202,7 @@ public abstract class AbstractMessageReaderArgumentResolver extends HandlerMetho // No compatible reader but body may be empty.. HttpMethod method = request.getMethod(); - if (contentType == null && method != null && SUPPORTED_METHODS.contains(method)) { + if (contentType == null && SUPPORTED_METHODS.contains(method)) { Flux body = request.getBody().doOnNext(buffer -> { DataBufferUtils.release(buffer); // Body not empty, back toy 415.. diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandler.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandler.java index 26b59a4bd4b..35f8044c0ec 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandler.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandler.java @@ -17,7 +17,6 @@ package org.springframework.web.reactive.result.method.annotation; import java.time.Instant; -import java.util.EnumSet; import java.util.List; import java.util.Set; @@ -52,7 +51,7 @@ import org.springframework.web.server.ServerWebExchange; */ public class ResponseEntityResultHandler extends AbstractMessageWriterResultHandler implements HandlerResultHandler { - private static final Set SAFE_METHODS = EnumSet.of(HttpMethod.GET, HttpMethod.HEAD); + private static final Set SAFE_METHODS = Set.of(HttpMethod.GET, HttpMethod.HEAD); /** diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/support/HandshakeWebSocketService.java b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/support/HandshakeWebSocketService.java index 78d6bcb8b60..059dedffe1e 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/support/HandshakeWebSocketService.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/support/HandshakeWebSocketService.java @@ -214,7 +214,7 @@ public class HandshakeWebSocketService implements WebSocketService, Lifecycle { if (HttpMethod.GET != method) { return Mono.error(new MethodNotAllowedException( - request.getMethodValue(), Collections.singleton(HttpMethod.GET))); + request.getMethod(), Collections.singleton(HttpMethod.GET))); } if (!"WebSocket".equalsIgnoreCase(headers.getUpgrade())) { diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/support/WebSocketUpgradeHandlerPredicate.java b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/support/WebSocketUpgradeHandlerPredicate.java index 0ee33929ab1..a986d53d596 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/support/WebSocketUpgradeHandlerPredicate.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/support/WebSocketUpgradeHandlerPredicate.java @@ -18,6 +18,7 @@ package org.springframework.web.reactive.socket.server.support; import java.util.function.BiPredicate; +import org.springframework.http.HttpMethod; import org.springframework.web.reactive.socket.WebSocketHandler; import org.springframework.web.server.ServerWebExchange; @@ -35,9 +36,9 @@ public class WebSocketUpgradeHandlerPredicate implements BiPredicate> result = EntityResponse.fromObject(body).allow(HttpMethod.GET).build(); - Set expected = EnumSet.of(HttpMethod.GET); + Set expected = Set.of(HttpMethod.GET); StepVerifier.create(result) .expectNextMatches(response -> expected.equals(response.headers().getAllow())) .expectComplete() diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java index c3780d2c443..8fa8f14c4ff 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java @@ -21,7 +21,6 @@ import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.Collections; -import java.util.EnumSet; import java.util.List; import java.util.Set; @@ -198,7 +197,7 @@ public class DefaultServerResponseBuilderTests { @Test public void allow() { Mono result = ServerResponse.ok().allow(HttpMethod.GET).build(); - Set expected = EnumSet.of(HttpMethod.GET); + Set expected = Set.of(HttpMethod.GET); StepVerifier.create(result) .expectNextMatches(response -> expected.equals(response.headers().getAllow())) .expectComplete() diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/ResourceHandlerFunctionTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/ResourceHandlerFunctionTests.java index 1c60b8a5ad1..086eedaa7d9 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/ResourceHandlerFunctionTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/ResourceHandlerFunctionTests.java @@ -18,8 +18,8 @@ package org.springframework.web.reactive.function.server; import java.io.IOException; import java.nio.file.Files; -import java.util.EnumSet; import java.util.List; +import java.util.Set; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -141,7 +141,7 @@ public class ResourceHandlerFunctionTests { Mono responseMono = this.handlerFunction.handle(request); Mono result = responseMono.flatMap(response -> { assertThat(response.statusCode()).isEqualTo(HttpStatus.OK); - assertThat(response.headers().getAllow()).isEqualTo(EnumSet.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS)); + assertThat(response.headers().getAllow()).isEqualTo(Set.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS)); return response.writeTo(exchange, context); }); @@ -150,7 +150,7 @@ public class ResourceHandlerFunctionTests { .expectComplete() .verify(); assertThat(mockResponse.getStatusCode()).isEqualTo(HttpStatus.OK); - assertThat(mockResponse.getHeaders().getAllow()).isEqualTo(EnumSet.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS)); + assertThat(mockResponse.getHeaders().getAllow()).isEqualTo(Set.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS)); StepVerifier.create(mockResponse.getBody()).expectComplete().verify(); } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMappingTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMappingTests.java index cbc923abe1b..7e13ecf5440 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMappingTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMappingTests.java @@ -21,7 +21,6 @@ import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.EnumSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -141,7 +140,7 @@ public class RequestMappingInfoHandlerMappingTests { Mono mono = this.handlerMapping.getHandler(exchange); assertError(mono, MethodNotAllowedException.class, - ex -> assertThat(ex.getSupportedMethods()).isEqualTo(EnumSet.of(HttpMethod.GET, HttpMethod.HEAD))); + ex -> assertThat(ex.getSupportedMethods()).isEqualTo(Set.of(HttpMethod.GET, HttpMethod.HEAD))); } @Test // SPR-9603 @@ -194,11 +193,11 @@ public class RequestMappingInfoHandlerMappingTests { List allMethodExceptTrace = new ArrayList<>(Arrays.asList(HttpMethod.values())); allMethodExceptTrace.remove(HttpMethod.TRACE); - testHttpOptions("/foo", EnumSet.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS), null); - testHttpOptions("/person/1", EnumSet.of(HttpMethod.PUT, HttpMethod.OPTIONS), null); - testHttpOptions("/persons", EnumSet.copyOf(allMethodExceptTrace), null); - testHttpOptions("/something", EnumSet.of(HttpMethod.PUT, HttpMethod.POST), null); - testHttpOptions("/qux", EnumSet.of(HttpMethod.PATCH,HttpMethod.GET,HttpMethod.HEAD,HttpMethod.OPTIONS), + testHttpOptions("/foo", Set.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS), null); + testHttpOptions("/person/1", Set.of(HttpMethod.PUT, HttpMethod.OPTIONS), null); + testHttpOptions("/persons", Set.copyOf(allMethodExceptTrace), null); + testHttpOptions("/something", Set.of(HttpMethod.PUT, HttpMethod.POST), null); + testHttpOptions("/qux", Set.of(HttpMethod.PATCH,HttpMethod.GET,HttpMethod.HEAD,HttpMethod.OPTIONS), new MediaType("foo", "bar")); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java index 36dceaad678..3af108f45f8 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java @@ -872,8 +872,8 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - HttpMethod httpMethod = HttpMethod.resolve(request.getMethod()); - if (httpMethod == HttpMethod.PATCH || httpMethod == null) { + HttpMethod httpMethod = HttpMethod.valueOf(request.getMethod()); + if (HttpMethod.PATCH.equals(httpMethod)) { processRequest(request, response); } else { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/AbstractServerResponse.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/AbstractServerResponse.java index a35571fdf22..fc667124dbc 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/AbstractServerResponse.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/AbstractServerResponse.java @@ -18,7 +18,6 @@ package org.springframework.web.servlet.function; import java.io.IOException; import java.util.Collection; -import java.util.EnumSet; import java.util.Set; import jakarta.servlet.ServletException; @@ -44,7 +43,7 @@ import org.springframework.web.servlet.ModelAndView; */ abstract class AbstractServerResponse extends ErrorHandlingServerResponse { - private static final Set SAFE_METHODS = EnumSet.of(HttpMethod.GET, HttpMethod.HEAD); + private static final Set SAFE_METHODS = Set.of(HttpMethod.GET, HttpMethod.HEAD); final int statusCode; @@ -90,7 +89,7 @@ abstract class AbstractServerResponse extends ErrorHandlingServerResponse { long lastModified = headers().getLastModified(); ServletWebRequest servletWebRequest = new ServletWebRequest(request, response); - HttpMethod httpMethod = HttpMethod.resolve(request.getMethod()); + HttpMethod httpMethod = HttpMethod.valueOf(request.getMethod()); if (SAFE_METHODS.contains(httpMethod) && servletWebRequest.checkNotModified(headers().getETag(), lastModified)) { return null; diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultServerRequest.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultServerRequest.java index f09177c2af4..9ff4cd503ba 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultServerRequest.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultServerRequest.java @@ -48,6 +48,7 @@ import jakarta.servlet.http.Part; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; import org.springframework.http.HttpRange; import org.springframework.http.MediaType; import org.springframework.http.converter.GenericHttpMessageConverter; @@ -107,8 +108,13 @@ class DefaultServerRequest implements ServerRequest { ServletRequestPathUtils.parseAndCache(servletRequest)); } + @Override + public HttpMethod method() { + return HttpMethod.valueOf(servletRequest().getMethod()); + } @Override + @Deprecated public String methodName() { return servletRequest().getMethod(); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultServerRequestBuilder.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultServerRequestBuilder.java index 7dbca4417cb..c3e6d93d83d 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultServerRequestBuilder.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultServerRequestBuilder.java @@ -68,7 +68,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder { private final List> messageConverters; - private String methodName; + private HttpMethod method; private URI uri; @@ -90,7 +90,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder { Assert.notNull(other, "ServerRequest must not be null"); this.servletRequest = other.servletRequest(); this.messageConverters = new ArrayList<>(other.messageConverters()); - this.methodName = other.methodName(); + this.method = other.method(); this.uri = other.uri(); headers(headers -> headers.addAll(other.headers().asHttpHeaders())); cookies(cookies -> cookies.addAll(other.cookies())); @@ -102,7 +102,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder { @Override public ServerRequest.Builder method(HttpMethod method) { Assert.notNull(method, "HttpMethod must not be null"); - this.methodName = method.name(); + this.method = method; return this; } @@ -188,14 +188,14 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder { @Override public ServerRequest build() { - return new BuiltServerRequest(this.servletRequest, this.methodName, this.uri, this.headers, this.cookies, + return new BuiltServerRequest(this.servletRequest, this.method, this.uri, this.headers, this.cookies, this.attributes, this.params, this.remoteAddress, this.body, this.messageConverters); } private static class BuiltServerRequest implements ServerRequest { - private final String methodName; + private final HttpMethod method; private final URI uri; @@ -216,13 +216,13 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder { @Nullable private final InetSocketAddress remoteAddress; - public BuiltServerRequest(HttpServletRequest servletRequest, String methodName, URI uri, + public BuiltServerRequest(HttpServletRequest servletRequest, HttpMethod method, URI uri, HttpHeaders headers, MultiValueMap cookies, Map attributes, MultiValueMap params, @Nullable InetSocketAddress remoteAddress, byte[] body, List> messageConverters) { this.servletRequest = servletRequest; - this.methodName = methodName; + this.method = method; this.uri = uri; this.headers = new HttpHeaders(headers); this.cookies = new LinkedMultiValueMap<>(cookies); @@ -234,8 +234,14 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder { } @Override + public HttpMethod method() { + return this.method; + } + + @Override + @Deprecated public String methodName() { - return this.methodName; + return this.method.name(); } @Override diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RequestPredicates.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RequestPredicates.java index db160bc4b9c..653f3af9463 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RequestPredicates.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RequestPredicates.java @@ -23,10 +23,10 @@ import java.security.Principal; import java.time.Instant; import java.util.Arrays; import java.util.Collections; -import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Optional; @@ -435,12 +435,12 @@ public abstract class RequestPredicates { public HttpMethodPredicate(HttpMethod httpMethod) { Assert.notNull(httpMethod, "HttpMethod must not be null"); - this.httpMethods = EnumSet.of(httpMethod); + this.httpMethods = Set.of(httpMethod); } public HttpMethodPredicate(HttpMethod... httpMethods) { Assert.notEmpty(httpMethods, "HttpMethods must not be empty"); - this.httpMethods = EnumSet.copyOf(Arrays.asList(httpMethods)); + this.httpMethods = new LinkedHashSet<>(Arrays.asList(httpMethods)); } @Override @@ -451,16 +451,15 @@ public abstract class RequestPredicates { return match; } - @Nullable private static HttpMethod method(ServerRequest request) { if (CorsUtils.isPreFlightRequest(request.servletRequest())) { String accessControlRequestMethod = request.headers().firstHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD); - return HttpMethod.resolve(accessControlRequestMethod); - } - else { - return request.method(); + if (accessControlRequestMethod != null) { + return HttpMethod.valueOf(accessControlRequestMethod); + } } + return request.method(); } @Override @@ -966,6 +965,7 @@ public abstract class RequestPredicates { } @Override + @Deprecated public String methodName() { return this.request.methodName(); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/ResourceHandlerFunction.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/ResourceHandlerFunction.java index 55da489a21b..0012c3d0da8 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/ResourceHandlerFunction.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/ResourceHandlerFunction.java @@ -22,7 +22,6 @@ import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.net.URL; -import java.util.EnumSet; import java.util.Set; import org.springframework.core.io.Resource; @@ -39,7 +38,7 @@ import org.springframework.lang.Nullable; class ResourceHandlerFunction implements HandlerFunction { private static final Set SUPPORTED_METHODS = - EnumSet.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS); + Set.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS); private final Resource resource; @@ -53,17 +52,16 @@ class ResourceHandlerFunction implements HandlerFunction { @Override public ServerResponse handle(ServerRequest request) { HttpMethod method = request.method(); - if (method != null) { - switch (method) { - case GET: - return EntityResponse.fromObject(this.resource).build(); - case HEAD: - Resource headResource = new HeadMethodResource(this.resource); - return EntityResponse.fromObject(headResource).build(); - case OPTIONS: - return ServerResponse.ok() - .allow(SUPPORTED_METHODS).build(); - } + if (HttpMethod.GET.equals(method)) { + return EntityResponse.fromObject(this.resource).build(); + } + else if (HttpMethod.HEAD.equals(method)) { + Resource headResource = new HeadMethodResource(this.resource); + return EntityResponse.fromObject(headResource).build(); + } + else if (HttpMethod.OPTIONS.equals(method)) { + return ServerResponse.ok() + .allow(SUPPORTED_METHODS).build(); } return ServerResponse.status(HttpStatus.METHOD_NOT_ALLOWED) .allow(SUPPORTED_METHODS).build(); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/ServerRequest.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/ServerRequest.java index 741c4190f18..e8b3982c77a 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/ServerRequest.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/ServerRequest.java @@ -66,15 +66,14 @@ public interface ServerRequest { * @return the HTTP method as an HttpMethod enum value, or {@code null} * if not resolvable (e.g. in case of a non-standard HTTP method) */ - @Nullable - default HttpMethod method() { - return HttpMethod.resolve(methodName()); - } + HttpMethod method(); /** * Get the name of the HTTP method. * @return the HTTP method as a String + * @deprecated in favor of {@link #method()} */ + @Deprecated String methodName(); /** diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java index f550766908f..cdd4bf407c0 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java @@ -23,7 +23,6 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collection; -import java.util.EnumSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Optional; @@ -68,8 +67,7 @@ import org.springframework.web.method.support.HandlerMethodArgumentResolver; */ public abstract class AbstractMessageConverterMethodArgumentResolver implements HandlerMethodArgumentResolver { - private static final Set SUPPORTED_METHODS = - EnumSet.of(HttpMethod.POST, HttpMethod.PUT, HttpMethod.PATCH); + private static final Set SUPPORTED_METHODS = Set.of(HttpMethod.POST, HttpMethod.PUT, HttpMethod.PATCH); private static final Object NO_VALUE = new Object(); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletRequestMethodArgumentResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletRequestMethodArgumentResolver.java index db80a70dfd1..9da95ef5116 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletRequestMethodArgumentResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletRequestMethodArgumentResolver.java @@ -171,7 +171,7 @@ public class ServletRequestMethodArgumentResolver implements HandlerMethodArgume return userPrincipal; } else if (HttpMethod.class == paramType) { - return HttpMethod.resolve(request.getMethod()); + return HttpMethod.valueOf(request.getMethod()); } else if (Locale.class == paramType) { return RequestContextUtils.getLocale(request); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/function/DefaultEntityResponseBuilderTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/function/DefaultEntityResponseBuilderTests.java index 00969fbb85c..f57fdbded43 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/function/DefaultEntityResponseBuilderTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/function/DefaultEntityResponseBuilderTests.java @@ -21,7 +21,6 @@ import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.Collections; -import java.util.EnumSet; import java.util.List; import java.util.Set; @@ -89,7 +88,7 @@ public class DefaultEntityResponseBuilderTests { String body = "foo"; EntityResponse result = EntityResponse.fromObject(body).allow(HttpMethod.GET).build(); - Set expected = EnumSet.of(HttpMethod.GET); + Set expected = Set.of(HttpMethod.GET); assertThat(result.headers().getAllow()).isEqualTo(expected); } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/function/DefaultServerResponseBuilderTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/function/DefaultServerResponseBuilderTests.java index d70ffb3a8e8..6630814bc34 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/function/DefaultServerResponseBuilderTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/function/DefaultServerResponseBuilderTests.java @@ -22,8 +22,8 @@ import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Collections; -import java.util.EnumSet; import java.util.List; +import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; @@ -156,7 +156,7 @@ public class DefaultServerResponseBuilderTests { @Test public void allow() { ServerResponse response = ServerResponse.ok().allow(HttpMethod.GET).build(); - assertThat(response.headers().getAllow()).isEqualTo(EnumSet.of(HttpMethod.GET)); + assertThat(response.headers().getAllow()).isEqualTo(Set.of(HttpMethod.GET)); } @Test diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/function/ResourceHandlerFunctionTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/function/ResourceHandlerFunctionTests.java index a5cfa327b7d..90aab2cff2f 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/function/ResourceHandlerFunctionTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/function/ResourceHandlerFunctionTests.java @@ -21,7 +21,7 @@ import java.io.InputStream; import java.nio.file.Files; import java.util.Arrays; import java.util.Collections; -import java.util.EnumSet; +import java.util.Set; import jakarta.servlet.ServletException; import org.junit.jupiter.api.BeforeEach; @@ -35,6 +35,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.converter.ResourceHttpMessageConverter; import org.springframework.http.converter.ResourceRegionHttpMessageConverter; +import org.springframework.util.StringUtils; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.PathPatternsTestUtils; import org.springframework.web.testfixture.servlet.MockHttpServletRequest; @@ -173,14 +174,16 @@ public class ResourceHandlerFunctionTests { ServerResponse response = this.handlerFunction.handle(request); assertThat(response.statusCode()).isEqualTo(HttpStatus.OK); - assertThat(response.headers().getAllow()).isEqualTo(EnumSet.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS)); + assertThat(response.headers().getAllow()).isEqualTo(Set.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS)); MockHttpServletResponse servletResponse = new MockHttpServletResponse(); ModelAndView mav = response.writeTo(servletRequest, servletResponse, this.context); assertThat(mav).isNull(); assertThat(servletResponse.getStatus()).isEqualTo(200); - assertThat(servletResponse.getHeader("Allow")).isEqualTo("GET,HEAD,OPTIONS"); + String allowHeader = servletResponse.getHeader("Allow"); + String[] methods = StringUtils.tokenizeToStringArray(allowHeader, ","); + assertThat(methods).containsExactlyInAnyOrder("GET","HEAD","OPTIONS"); byte[] actualBytes = servletResponse.getContentAsByteArray(); assertThat(actualBytes.length).isEqualTo(0); } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandlerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandlerTests.java index dfc40e2807c..f469e8c8523 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandlerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandlerTests.java @@ -18,8 +18,8 @@ package org.springframework.web.servlet.mvc.method.annotation; import java.lang.reflect.Method; import java.util.Arrays; -import java.util.EnumSet; import java.util.List; +import java.util.Set; import jakarta.servlet.ServletException; import org.junit.jupiter.api.Test; @@ -102,7 +102,7 @@ public class ResponseEntityExceptionHandlerTests { Exception ex = new HttpRequestMethodNotSupportedException("GET", supported); ResponseEntity responseEntity = testException(ex); - assertThat(responseEntity.getHeaders().getAllow()).isEqualTo(EnumSet.of(HttpMethod.POST, HttpMethod.DELETE)); + assertThat(responseEntity.getHeaders().getAllow()).isEqualTo(Set.of(HttpMethod.POST, HttpMethod.DELETE)); } @Test