Skip to content

Commit 4261f34

Browse files
committed
Consistent and lenient HttpMethod resolution across all web modules
Issue: SPR-13776
1 parent 34b596c commit 4261f34

29 files changed

+149
-109
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -128,7 +128,7 @@ public String getMultipartContentType(String paramOrFileName) {
128128

129129
@Override
130130
public HttpMethod getRequestMethod() {
131-
return HttpMethod.valueOf(getMethod());
131+
return HttpMethod.resolve(getMethod());
132132
}
133133

134134
@Override

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -470,15 +470,18 @@ public void setAccessControlAllowMethods(List<HttpMethod> allowedMethods) {
470470
}
471471

472472
/**
473-
* Returns the value of the {@code Access-Control-Allow-Methods} response header.
473+
* Return the value of the {@code Access-Control-Allow-Methods} response header.
474474
*/
475475
public List<HttpMethod> getAccessControlAllowMethods() {
476476
List<HttpMethod> result = new ArrayList<HttpMethod>();
477477
String value = getFirst(ACCESS_CONTROL_ALLOW_METHODS);
478478
if (value != null) {
479479
String[] tokens = value.split(",\\s*");
480480
for (String token : tokens) {
481-
result.add(HttpMethod.valueOf(token));
481+
HttpMethod resolved = HttpMethod.resolve(token);
482+
if (resolved != null) {
483+
result.add(resolved);
484+
}
482485
}
483486
}
484487
return result;
@@ -492,7 +495,7 @@ public void setAccessControlAllowOrigin(String allowedOrigin) {
492495
}
493496

494497
/**
495-
* Returns the value of the {@code Access-Control-Allow-Origin} response header.
498+
* Return the value of the {@code Access-Control-Allow-Origin} response header.
496499
*/
497500
public String getAccessControlAllowOrigin() {
498501
return getFirst(ACCESS_CONTROL_ALLOW_ORIGIN);
@@ -550,11 +553,10 @@ public void setAccessControlRequestMethod(HttpMethod requestedMethod) {
550553
}
551554

552555
/**
553-
* Returns the value of the {@code Access-Control-Request-Method} request header.
556+
* Return the value of the {@code Access-Control-Request-Method} request header.
554557
*/
555558
public HttpMethod getAccessControlRequestMethod() {
556-
String value = getFirst(ACCESS_CONTROL_REQUEST_METHOD);
557-
return (value != null ? HttpMethod.valueOf(value) : null);
559+
return HttpMethod.resolve(getFirst(ACCESS_CONTROL_REQUEST_METHOD));
558560
}
559561

560562
/**
@@ -615,12 +617,15 @@ public void setAllow(Set<HttpMethod> allowedMethods) {
615617
public Set<HttpMethod> getAllow() {
616618
String value = getFirst(ALLOW);
617619
if (!StringUtils.isEmpty(value)) {
618-
List<HttpMethod> allowedMethod = new ArrayList<HttpMethod>(5);
620+
List<HttpMethod> result = new LinkedList<HttpMethod>();
619621
String[] tokens = value.split(",\\s*");
620622
for (String token : tokens) {
621-
allowedMethod.add(HttpMethod.valueOf(token));
623+
HttpMethod resolved = HttpMethod.resolve(token);
624+
if (resolved != null) {
625+
result.add(resolved);
626+
}
622627
}
623-
return EnumSet.copyOf(allowedMethod);
628+
return EnumSet.copyOf(result);
624629
}
625630
else {
626631
return EnumSet.noneOf(HttpMethod.class);
@@ -635,7 +640,7 @@ public void setCacheControl(String cacheControl) {
635640
}
636641

637642
/**
638-
* Returns the value of the {@code Cache-Control} header.
643+
* Return the value of the {@code Cache-Control} header.
639644
*/
640645
public String getCacheControl() {
641646
return getFirst(CACHE_CONTROL);
@@ -656,7 +661,7 @@ public void setConnection(List<String> connection) {
656661
}
657662

658663
/**
659-
* Returns the value of the {@code Connection} header.
664+
* Return the value of the {@code Connection} header.
660665
*/
661666
public List<String> getConnection() {
662667
return getFirstValueAsList(CONNECTION);
@@ -920,7 +925,7 @@ public void setRange(List<HttpRange> ranges) {
920925
}
921926

922927
/**
923-
* Returns the value of the {@code Range} header.
928+
* Return the value of the {@code Range} header.
924929
* <p>Returns an empty list when the range is unknown.
925930
*/
926931
public List<HttpRange> getRange() {
@@ -936,7 +941,7 @@ public void setUpgrade(String upgrade) {
936941
}
937942

938943
/**
939-
* Returns the value of the {@code Upgrade} header.
944+
* Return the value of the {@code Upgrade} header.
940945
*/
941946
public String getUpgrade() {
942947
return getFirst(UPGRADE);

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,16 +16,52 @@
1616

1717
package org.springframework.http;
1818

19+
import java.util.HashMap;
20+
import java.util.Map;
21+
1922
/**
2023
* Java 5 enumeration of HTTP request methods. Intended for use
2124
* with {@link org.springframework.http.client.ClientHttpRequest}
2225
* and {@link org.springframework.web.client.RestTemplate}.
2326
*
2427
* @author Arjen Poutsma
28+
* @author Juergen Hoeller
2529
* @since 3.0
2630
*/
2731
public enum HttpMethod {
2832

29-
GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE
33+
GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE;
34+
35+
36+
private static final Map<String, HttpMethod> mappings = new HashMap<String, HttpMethod>(8);
37+
38+
static {
39+
for (HttpMethod httpMethod : values()) {
40+
mappings.put(httpMethod.name(), httpMethod);
41+
}
42+
}
43+
44+
45+
/**
46+
* Resolve the given method value to an {@code HttpMethod}.
47+
* @param method the method value as a String
48+
* @return the corresponding {@code HttpMethod}, or {@code null} if not found
49+
* @since 4.2.4
50+
*/
51+
public static HttpMethod resolve(String method) {
52+
return (method != null ? mappings.get(method) : null);
53+
}
54+
55+
56+
/**
57+
* Determine whether this {@code HttpMethod} matches the given
58+
* method value.
59+
* @param method the method value as a String
60+
* @return {@code true} if it matches, {@code false} otherwise
61+
* @since 4.2.4
62+
*/
63+
public boolean matches(String method) {
64+
return name().equals(method);
65+
}
3066

3167
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,8 +19,8 @@
1919
import java.net.URI;
2020

2121
/**
22-
* Represents an HTTP request message, consisting of {@linkplain #getMethod() method}
23-
* and {@linkplain #getURI() uri}.
22+
* Represents an HTTP request message, consisting of
23+
* {@linkplain #getMethod() method} and {@linkplain #getURI() uri}.
2424
*
2525
* @author Arjen Poutsma
2626
* @since 3.1
@@ -29,13 +29,14 @@ public interface HttpRequest extends HttpMessage {
2929

3030
/**
3131
* Return the HTTP method of the request.
32-
* @return the HTTP method as an HttpMethod enum value
32+
* @return the HTTP method as an HttpMethod enum value, or {@code null}
33+
* if not resolvable (e.g. in case of a non-standard HTTP method)
3334
*/
3435
HttpMethod getMethod();
3536

3637
/**
3738
* Return the URI of the request.
38-
* @return the URI of the request
39+
* @return the URI of the request (never {@code null})
3940
*/
4041
URI getURI();
4142

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.nio.charset.Charset;
2121
import java.util.Arrays;
2222

23-
import org.springframework.util.Assert;
2423
import org.springframework.util.MultiValueMap;
2524
import org.springframework.util.ObjectUtils;
2625

@@ -104,8 +103,6 @@ public RequestEntity(MultiValueMap<String, String> headers, HttpMethod method, U
104103
*/
105104
public RequestEntity(T body, MultiValueMap<String, String> headers, HttpMethod method, URI url) {
106105
super(body, headers);
107-
Assert.notNull(method, "'method' is required");
108-
Assert.notNull(url, "'url' is required");
109106
this.method = method;
110107
this.url = url;
111108
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ final class HttpComponentsAsyncClientHttpRequest extends AbstractBufferingAsyncC
6868

6969
@Override
7070
public HttpMethod getMethod() {
71-
return HttpMethod.valueOf(this.httpRequest.getMethod());
71+
return HttpMethod.resolve(this.httpRequest.getMethod());
7272
}
7373

7474
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ final class HttpComponentsClientHttpRequest extends AbstractBufferingClientHttpR
6666

6767
@Override
6868
public HttpMethod getMethod() {
69-
return HttpMethod.valueOf(this.httpRequest.getMethod());
69+
return HttpMethod.resolve(this.httpRequest.getMethod());
7070
}
7171

7272
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ final class HttpComponentsStreamingClientHttpRequest extends AbstractClientHttpR
6565

6666
@Override
6767
public HttpMethod getMethod() {
68-
return HttpMethod.valueOf(this.httpRequest.getMethod());
68+
return HttpMethod.resolve(this.httpRequest.getMethod());
6969
}
7070

7171
@Override

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,7 +57,7 @@ final class SimpleBufferingAsyncClientHttpRequest extends AbstractBufferingAsync
5757

5858
@Override
5959
public HttpMethod getMethod() {
60-
return HttpMethod.valueOf(this.connection.getRequestMethod());
60+
return HttpMethod.resolve(this.connection.getRequestMethod());
6161
}
6262

6363
@Override
@@ -78,8 +78,8 @@ protected ListenableFuture<ClientHttpResponse> executeInternal(
7878
@Override
7979
public ClientHttpResponse call() throws Exception {
8080
SimpleBufferingClientHttpRequest.addHeaders(connection, headers);
81-
// JDK < 1.8 doesn't support getOutputStream with HTTP DELETE
82-
if (HttpMethod.DELETE.equals(getMethod()) && bufferedOutput.length == 0) {
81+
// JDK <1.8 doesn't support getOutputStream with HTTP DELETE
82+
if (HttpMethod.DELETE == getMethod() && bufferedOutput.length == 0) {
8383
connection.setDoOutput(false);
8484
}
8585
if (connection.getDoOutput() && outputStreaming) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ final class SimpleBufferingClientHttpRequest extends AbstractBufferingClientHttp
5252

5353
@Override
5454
public HttpMethod getMethod() {
55-
return HttpMethod.valueOf(this.connection.getRequestMethod());
55+
return HttpMethod.resolve(this.connection.getRequestMethod());
5656
}
5757

5858
@Override
@@ -69,8 +69,8 @@ public URI getURI() {
6969
protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
7070
addHeaders(this.connection, headers);
7171

72-
// JDK < 1.8 doesn't support getOutputStream with HTTP DELETE
73-
if (HttpMethod.DELETE.equals(getMethod()) && bufferedOutput.length == 0) {
72+
// JDK <1.8 doesn't support getOutputStream with HTTP DELETE
73+
if (HttpMethod.DELETE == getMethod() && bufferedOutput.length == 0) {
7474
this.connection.setDoOutput(false);
7575
}
7676

0 commit comments

Comments
 (0)