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

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

View File

@@ -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 <T> Mono<T> insertCheckpoint(Mono<T> 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);

View File

@@ -150,6 +150,7 @@ public abstract class ExchangeFunctions {
}
@Override
@Deprecated
public String getMethodValue() {
return request.method().name();
}

View File

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

View File

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

View File

@@ -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<DataBuffer> body;
public BuiltServerHttpRequest(String id, String method, URI uri, HttpHeaders headers,
public BuiltServerHttpRequest(String id, HttpMethod method, URI uri, HttpHeaders headers,
MultiValueMap<String, HttpCookie> cookies, Flux<DataBuffer> 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;

View File

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

View File

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

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 reactor.core.publisher.Mono;
@@ -42,7 +41,7 @@ import org.springframework.web.reactive.function.BodyInserters;
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;
@@ -56,20 +55,19 @@ class ResourceHandlerFunction implements HandlerFunction<ServerResponse> {
@Override
public Mono<ServerResponse> 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)

View File

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

View File

@@ -85,6 +85,7 @@ public class ServerRequestWrapper implements ServerRequest {
}
@Override
@Deprecated
public String methodName() {
return this.delegate.methodName();
}

View File

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

View File

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

View File

@@ -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<HttpMethod> methods = helper.getAllowedMethods();
if (HttpMethod.OPTIONS.matches(httpMethod)) {
if (HttpMethod.OPTIONS.equals(httpMethod)) {
Set<MediaType> 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<HttpMethod> 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<HttpMethod> initAllowedHttpMethods(Set<HttpMethod> 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 {

View File

@@ -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<HttpMethod> SUPPORTED_METHODS =
EnumSet.of(HttpMethod.POST, HttpMethod.PUT, HttpMethod.PATCH);
Set.of(HttpMethod.POST, HttpMethod.PUT, HttpMethod.PATCH);
private final List<HttpMessageReader<?>> 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<DataBuffer> body = request.getBody().doOnNext(buffer -> {
DataBufferUtils.release(buffer);
// Body not empty, back toy 415..

View File

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

View File

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

View File

@@ -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<Object, Ser
@Override
public boolean test(Object handler, ServerWebExchange exchange) {
if (handler instanceof WebSocketHandler) {
String method = exchange.getRequest().getMethodValue();
HttpMethod method = exchange.getRequest().getMethod();
String header = exchange.getRequest().getHeaders().getUpgrade();
return (method.equals("GET") && header != null && header.equalsIgnoreCase("websocket"));
return (HttpMethod.GET.equals(method) && header != null && header.equalsIgnoreCase("websocket"));
}
return true;
}

View File

@@ -20,7 +20,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;
@@ -100,7 +99,7 @@ public class DefaultEntityResponseBuilderTests {
public void allow() {
String body = "foo";
Mono<EntityResponse<String>> result = EntityResponse.fromObject(body).allow(HttpMethod.GET).build();
Set<HttpMethod> expected = EnumSet.of(HttpMethod.GET);
Set<HttpMethod> expected = Set.of(HttpMethod.GET);
StepVerifier.create(result)
.expectNextMatches(response -> expected.equals(response.headers().getAllow()))
.expectComplete()

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;
@@ -198,7 +197,7 @@ public class DefaultServerResponseBuilderTests {
@Test
public void allow() {
Mono<ServerResponse> result = ServerResponse.ok().allow(HttpMethod.GET).build();
Set<HttpMethod> expected = EnumSet.of(HttpMethod.GET);
Set<HttpMethod> expected = Set.of(HttpMethod.GET);
StepVerifier.create(result)
.expectNextMatches(response -> expected.equals(response.headers().getAllow()))
.expectComplete()

View File

@@ -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<ServerResponse> responseMono = this.handlerFunction.handle(request);
Mono<Void> 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();
}

View File

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