Changes because HttpMethod changed to class

This commit contains changes made because HttpMethod changed from enum
to class.

See gh-27697
This commit is contained in:
Arjen Poutsma
2021-11-25 13:45:34 +01:00
parent 6e335e3a9f
commit 7a4207cd7b
74 changed files with 337 additions and 274 deletions

View File

@@ -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 {

View File

@@ -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<HttpMethod> SAFE_METHODS = EnumSet.of(HttpMethod.GET, HttpMethod.HEAD);
private static final Set<HttpMethod> 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;

View File

@@ -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();
}

View File

@@ -68,7 +68,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder {
private final List<HttpMessageConverter<?>> 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<String, Cookie> cookies,
Map<String, Object> attributes, MultiValueMap<String, String> params,
@Nullable InetSocketAddress remoteAddress, byte[] body, List<HttpMessageConverter<?>> 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

View File

@@ -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();
}

View File

@@ -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<ServerResponse> {
private static final Set<HttpMethod> 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<ServerResponse> {
@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();

View File

@@ -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();
/**

View File

@@ -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<HttpMethod> SUPPORTED_METHODS =
EnumSet.of(HttpMethod.POST, HttpMethod.PUT, HttpMethod.PATCH);
private static final Set<HttpMethod> SUPPORTED_METHODS = Set.of(HttpMethod.POST, HttpMethod.PUT, HttpMethod.PATCH);
private static final Object NO_VALUE = new Object();

View File

@@ -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);

View File

@@ -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<String> result =
EntityResponse.fromObject(body).allow(HttpMethod.GET).build();
Set<HttpMethod> expected = EnumSet.of(HttpMethod.GET);
Set<HttpMethod> expected = Set.of(HttpMethod.GET);
assertThat(result.headers().getAllow()).isEqualTo(expected);
}

View File

@@ -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

View File

@@ -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);
}

View File

@@ -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<Object> 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