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