Skip to content

Commit 7a4207c

Browse files
committed
Changes because HttpMethod changed to class
This commit contains changes made because HttpMethod changed from enum to class. See gh-27697
1 parent 6e335e3 commit 7a4207c

File tree

74 files changed

+337
-274
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+337
-274
lines changed

spring-test/src/main/java/org/springframework/mock/http/client/MockClientHttpRequest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public HttpMethod getMethod() {
7878
}
7979

8080
@Override
81+
@Deprecated
8182
public String getMethodValue() {
8283
return this.httpMethod.name();
8384
}

spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpRequest.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@
5555
*/
5656
public final class MockServerHttpRequest extends AbstractServerHttpRequest {
5757

58-
/**
59-
* String representation of one of {@link HttpMethod} or not empty custom method (e.g. <i>CONNECT</i>).
60-
*/
61-
private final String httpMethod;
58+
private final HttpMethod httpMethod;
6259

6360
private final MultiValueMap<String, HttpCookie> cookies;
6461

@@ -73,13 +70,12 @@ public final class MockServerHttpRequest extends AbstractServerHttpRequest {
7370

7471
private final Flux<DataBuffer> body;
7572

76-
private MockServerHttpRequest(String httpMethod,
73+
private MockServerHttpRequest(HttpMethod httpMethod,
7774
URI uri, @Nullable String contextPath, HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
7875
@Nullable InetSocketAddress localAddress, @Nullable InetSocketAddress remoteAddress,
7976
@Nullable SslInfo sslInfo, Publisher<? extends DataBuffer> body) {
8077

8178
super(uri, contextPath, headers);
82-
Assert.isTrue(StringUtils.hasText(httpMethod), "HTTP method is required.");
8379
this.httpMethod = httpMethod;
8480
this.cookies = cookies;
8581
this.localAddress = localAddress;
@@ -90,14 +86,14 @@ private MockServerHttpRequest(String httpMethod,
9086

9187

9288
@Override
93-
@Nullable
9489
public HttpMethod getMethod() {
95-
return HttpMethod.resolve(this.httpMethod);
90+
return this.httpMethod;
9691
}
9792

9893
@Override
94+
@Deprecated
9995
public String getMethodValue() {
100-
return this.httpMethod;
96+
return this.httpMethod.name();
10197
}
10298

10399
@Override
@@ -218,7 +214,7 @@ public static BaseBuilder<?> options(String urlTemplate, Object... uriVars) {
218214
public static BodyBuilder method(HttpMethod method, URI url) {
219215
Assert.notNull(method, "HTTP method is required. " +
220216
"For a custom HTTP method, please provide a String HTTP method value.");
221-
return new DefaultBodyBuilder(method.name(), url);
217+
return new DefaultBodyBuilder(method, url);
222218
}
223219

224220
/**
@@ -242,9 +238,12 @@ public static BodyBuilder method(HttpMethod method, String uri, Object... vars)
242238
* @param vars variables to expand into the template
243239
* @return the created builder
244240
* @since 5.2.7
241+
* @deprecated in favor of {@link #method(HttpMethod, String, Object...)}
245242
*/
243+
@Deprecated
246244
public static BodyBuilder method(String httpMethod, String uri, Object... vars) {
247-
return new DefaultBodyBuilder(httpMethod, toUri(uri, vars));
245+
Assert.isTrue(StringUtils.hasText(httpMethod), "HTTP method is required.");
246+
return new DefaultBodyBuilder(HttpMethod.valueOf(httpMethod), toUri(uri, vars));
248247
}
249248

250249
private static URI toUri(String uri, Object[] vars) {
@@ -427,7 +426,7 @@ public interface BodyBuilder extends BaseBuilder<BodyBuilder> {
427426

428427
private static class DefaultBodyBuilder implements BodyBuilder {
429428

430-
private final String methodValue;
429+
private final HttpMethod method;
431430

432431
private final URI url;
433432

@@ -449,8 +448,8 @@ private static class DefaultBodyBuilder implements BodyBuilder {
449448
@Nullable
450449
private SslInfo sslInfo;
451450

452-
DefaultBodyBuilder(String method, URI url) {
453-
this.methodValue = method;
451+
DefaultBodyBuilder(HttpMethod method, URI url) {
452+
this.method = method;
454453
this.url = url;
455454
}
456455

@@ -589,7 +588,7 @@ private Charset getCharset() {
589588
@Override
590589
public MockServerHttpRequest body(Publisher<? extends DataBuffer> body) {
591590
applyCookiesIfNecessary();
592-
return new MockServerHttpRequest(this.methodValue, getUrlToUse(), this.contextPath,
591+
return new MockServerHttpRequest(this.method, getUrlToUse(), this.contextPath,
593592
this.headers, this.cookies, this.localAddress, this.remoteAddress, this.sslInfo, body);
594593
}
595594

spring-test/src/main/java/org/springframework/mock/web/MockMultipartHttpServletRequest.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Iterator;
2424
import java.util.List;
2525
import java.util.Map;
26+
import java.util.Objects;
2627

2728
import jakarta.servlet.ServletContext;
2829
import jakarta.servlet.ServletException;
@@ -102,12 +103,7 @@ public MultipartFile getFile(String name) {
102103
@Override
103104
public List<MultipartFile> getFiles(String name) {
104105
List<MultipartFile> multipartFiles = this.multipartFiles.get(name);
105-
if (multipartFiles != null) {
106-
return multipartFiles;
107-
}
108-
else {
109-
return Collections.emptyList();
110-
}
106+
return Objects.requireNonNullElse(multipartFiles, Collections.emptyList());
111107
}
112108

113109
@Override
@@ -141,7 +137,9 @@ public String getMultipartContentType(String paramOrFileName) {
141137

142138
@Override
143139
public HttpMethod getRequestMethod() {
144-
return HttpMethod.resolve(getMethod());
140+
String method = getMethod();
141+
Assert.state(method != null, "Method must not be null");
142+
return HttpMethod.valueOf(method);
145143
}
146144

147145
@Override

spring-test/src/main/java/org/springframework/mock/web/reactive/function/server/MockServerRequest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public HttpMethod method() {
132132
}
133133

134134
@Override
135+
@Deprecated
135136
public String methodName() {
136137
return this.method.name();
137138
}

spring-web/src/main/java/org/springframework/http/HttpHeaders.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import java.util.Base64;
3535
import java.util.Collection;
3636
import java.util.Collections;
37-
import java.util.EnumSet;
37+
import java.util.LinkedHashSet;
3838
import java.util.List;
3939
import java.util.Locale;
4040
import java.util.Map;
@@ -589,18 +589,19 @@ public void setAccessControlAllowMethods(List<HttpMethod> allowedMethods) {
589589
* Return the value of the {@code Access-Control-Allow-Methods} response header.
590590
*/
591591
public List<HttpMethod> getAccessControlAllowMethods() {
592-
List<HttpMethod> result = new ArrayList<>();
593592
String value = getFirst(ACCESS_CONTROL_ALLOW_METHODS);
594593
if (value != null) {
595594
String[] tokens = StringUtils.tokenizeToStringArray(value, ",");
595+
List<HttpMethod> result = new ArrayList<>();
596596
for (String token : tokens) {
597-
HttpMethod resolved = HttpMethod.resolve(token);
598-
if (resolved != null) {
599-
result.add(resolved);
600-
}
597+
HttpMethod method = HttpMethod.valueOf(token);
598+
result.add(method);
601599
}
600+
return result;
601+
}
602+
else {
603+
return Collections.emptyList();
602604
}
603-
return result;
604605
}
605606

606607
/**
@@ -682,7 +683,13 @@ public void setAccessControlRequestMethod(@Nullable HttpMethod requestMethod) {
682683
*/
683684
@Nullable
684685
public HttpMethod getAccessControlRequestMethod() {
685-
return HttpMethod.resolve(getFirst(ACCESS_CONTROL_REQUEST_METHOD));
686+
String requestMethod = getFirst(ACCESS_CONTROL_REQUEST_METHOD);
687+
if (requestMethod != null) {
688+
return HttpMethod.valueOf(requestMethod);
689+
}
690+
else {
691+
return null;
692+
}
686693
}
687694

688695
/**
@@ -743,17 +750,15 @@ public Set<HttpMethod> getAllow() {
743750
String value = getFirst(ALLOW);
744751
if (StringUtils.hasLength(value)) {
745752
String[] tokens = StringUtils.tokenizeToStringArray(value, ",");
746-
List<HttpMethod> result = new ArrayList<>(tokens.length);
753+
Set<HttpMethod> result = new LinkedHashSet<>(tokens.length);
747754
for (String token : tokens) {
748-
HttpMethod resolved = HttpMethod.resolve(token);
749-
if (resolved != null) {
750-
result.add(resolved);
751-
}
755+
HttpMethod method = HttpMethod.valueOf(token);
756+
result.add(method);
752757
}
753-
return EnumSet.copyOf(result);
758+
return result;
754759
}
755760
else {
756-
return EnumSet.noneOf(HttpMethod.class);
761+
return Collections.emptySet();
757762
}
758763
}
759764

spring-web/src/main/java/org/springframework/http/HttpRequest.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
import java.net.URI;
2020

21-
import org.springframework.lang.Nullable;
22-
2321
/**
2422
* Represents an HTTP request message, consisting of
2523
* {@linkplain #getMethod() method} and {@linkplain #getURI() uri}.
@@ -31,22 +29,20 @@ public interface HttpRequest extends HttpMessage {
3129

3230
/**
3331
* Return the HTTP method of the request.
34-
* @return the HTTP method as an HttpMethod enum value, or {@code null}
35-
* if not resolvable (e.g. in case of a non-standard HTTP method)
36-
* @see #getMethodValue()
37-
* @see HttpMethod#resolve(String)
32+
* @return the HTTP method as an HttpMethod value
33+
* @see HttpMethod#valueOf(String)
3834
*/
39-
@Nullable
40-
default HttpMethod getMethod() {
41-
return HttpMethod.resolve(getMethodValue());
42-
}
35+
HttpMethod getMethod();
4336

4437
/**
4538
* Return the HTTP method of the request as a String value.
4639
* @return the HTTP method as a plain String
4740
* @since 5.0
4841
* @see #getMethod()
42+
* @deprecated in favor of {@link #getMethod()} and
43+
* {@link HttpMethod#name()}
4944
*/
45+
@Deprecated
5046
String getMethodValue();
5147

5248
/**

spring-web/src/main/java/org/springframework/http/client/BufferingClientHttpRequestWrapper.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import org.springframework.http.HttpHeaders;
2323
import org.springframework.http.HttpMethod;
24-
import org.springframework.lang.Nullable;
2524
import org.springframework.util.StreamUtils;
2625

2726
/**
@@ -41,12 +40,12 @@ final class BufferingClientHttpRequestWrapper extends AbstractBufferingClientHtt
4140

4241

4342
@Override
44-
@Nullable
4543
public HttpMethod getMethod() {
4644
return this.request.getMethod();
4745
}
4846

4947
@Override
48+
@Deprecated
5049
public String getMethodValue() {
5150
return this.request.getMethodValue();
5251
}

spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,13 @@ final class HttpComponentsClientHttpRequest extends AbstractBufferingClientHttpR
5959
this.httpContext = context;
6060
}
6161

62+
@Override
63+
public HttpMethod getMethod() {
64+
return HttpMethod.valueOf(this.httpRequest.getMethod());
65+
}
6266

6367
@Override
68+
@Deprecated
6469
public String getMethodValue() {
6570
return this.httpRequest.getMethod();
6671
}

spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -273,26 +273,31 @@ protected RequestConfig mergeRequestConfig(RequestConfig clientConfig) {
273273
* @return the Commons HttpMethodBase object
274274
*/
275275
protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
276-
switch (httpMethod) {
277-
case GET:
278-
return new HttpGet(uri);
279-
case HEAD:
280-
return new HttpHead(uri);
281-
case POST:
282-
return new HttpPost(uri);
283-
case PUT:
284-
return new HttpPut(uri);
285-
case PATCH:
286-
return new HttpPatch(uri);
287-
case DELETE:
288-
return new HttpDelete(uri);
289-
case OPTIONS:
290-
return new HttpOptions(uri);
291-
case TRACE:
292-
return new HttpTrace(uri);
293-
default:
294-
throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod);
276+
if (HttpMethod.GET.equals(httpMethod)) {
277+
return new HttpGet(uri);
295278
}
279+
else if (HttpMethod.HEAD.equals(httpMethod)) {
280+
return new HttpHead(uri);
281+
}
282+
else if (HttpMethod.POST.equals(httpMethod)) {
283+
return new HttpPost(uri);
284+
}
285+
else if (HttpMethod.PUT.equals(httpMethod)) {
286+
return new HttpPut(uri);
287+
}
288+
else if (HttpMethod.PATCH.equals(httpMethod)) {
289+
return new HttpPatch(uri);
290+
}
291+
else if (HttpMethod.DELETE.equals(httpMethod)) {
292+
return new HttpDelete(uri);
293+
}
294+
else if (HttpMethod.OPTIONS.equals(httpMethod)) {
295+
return new HttpOptions(uri);
296+
}
297+
else if (HttpMethod.TRACE.equals(httpMethod)) {
298+
return new HttpTrace(uri);
299+
}
300+
throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod);
296301
}
297302

298303
/**

spring-web/src/main/java/org/springframework/http/client/HttpComponentsStreamingClientHttpRequest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.http.protocol.HttpContext;
3232

3333
import org.springframework.http.HttpHeaders;
34+
import org.springframework.http.HttpMethod;
3435
import org.springframework.http.MediaType;
3536
import org.springframework.http.StreamingHttpOutputMessage;
3637
import org.springframework.lang.Nullable;
@@ -64,8 +65,13 @@ final class HttpComponentsStreamingClientHttpRequest extends AbstractClientHttpR
6465
this.httpContext = context;
6566
}
6667

68+
@Override
69+
public HttpMethod getMethod() {
70+
return HttpMethod.valueOf(this.httpRequest.getMethod());
71+
}
6772

6873
@Override
74+
@Deprecated
6975
public String getMethodValue() {
7076
return this.httpRequest.getMethod();
7177
}

spring-web/src/main/java/org/springframework/http/client/InterceptingClientHttpRequest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.springframework.http.HttpMethod;
2626
import org.springframework.http.HttpRequest;
2727
import org.springframework.http.StreamingHttpOutputMessage;
28-
import org.springframework.util.Assert;
2928
import org.springframework.util.StreamUtils;
3029

3130
/**
@@ -62,6 +61,7 @@ public HttpMethod getMethod() {
6261
}
6362

6463
@Override
64+
@Deprecated
6565
public String getMethodValue() {
6666
return this.method.name();
6767
}
@@ -94,7 +94,6 @@ public ClientHttpResponse execute(HttpRequest request, byte[] body) throws IOExc
9494
}
9595
else {
9696
HttpMethod method = request.getMethod();
97-
Assert.state(method != null, "No standard HTTP method");
9897
ClientHttpRequest delegate = requestFactory.createRequest(request.getURI(), method);
9998
request.getHeaders().forEach((key, value) -> delegate.getHeaders().addAll(key, value));
10099
if (body.length > 0) {

spring-web/src/main/java/org/springframework/http/client/OkHttp3ClientHttpRequest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public HttpMethod getMethod() {
5757
}
5858

5959
@Override
60+
@Deprecated
6061
public String getMethodValue() {
6162
return this.method.name();
6263
}

0 commit comments

Comments
 (0)