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

@@ -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<String, String>, Serializable
* Return the value of the {@code Access-Control-Allow-Methods} response header.
*/
public List<HttpMethod> getAccessControlAllowMethods() {
List<HttpMethod> result = new ArrayList<>();
String value = getFirst(ACCESS_CONTROL_ALLOW_METHODS);
if (value != null) {
String[] tokens = StringUtils.tokenizeToStringArray(value, ",");
List<HttpMethod> 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<String, String>, 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<String, String>, Serializable
String value = getFirst(ALLOW);
if (StringUtils.hasLength(value)) {
String[] tokens = StringUtils.tokenizeToStringArray(value, ",");
List<HttpMethod> result = new ArrayList<>(tokens.length);
Set<HttpMethod> 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();
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -57,6 +57,7 @@ class OkHttp3ClientHttpRequest extends AbstractBufferingClientHttpRequest {
}
@Override
@Deprecated
public String getMethodValue() {
return this.method.name();
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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<HttpMethod> supportedMethods = new ArrayList<>(this.supportedMethods.length);
Set<HttpMethod> 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;
}
}

View File

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

View File

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

View File

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

View File

@@ -52,7 +52,6 @@ public interface MultipartHttpServletRequest extends HttpServletRequest, Multipa
/**
* Return this request's method as a convenient HttpMethod instance.
*/
@Nullable
HttpMethod getRequestMethod();
/**

View File

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