diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockMultipartHttpServletRequest.java b/spring-test/src/main/java/org/springframework/mock/web/MockMultipartHttpServletRequest.java index ac0cb75e6402a202a58a7e12f5f029a692303ecb..7988b17b3262e3a47d5d76a31a1d8ed5d5dc8692 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockMultipartHttpServletRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockMultipartHttpServletRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -128,7 +128,7 @@ public class MockMultipartHttpServletRequest extends MockHttpServletRequest impl @Override public HttpMethod getRequestMethod() { - return HttpMethod.valueOf(getMethod()); + return HttpMethod.resolve(getMethod()); } @Override diff --git a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java index 006a631faf65785615e47a5e107585fbf2e00538..886b06a6332564e566ab4e4d5f7bfd303ea9112c 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java +++ b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java @@ -470,7 +470,7 @@ public class HttpHeaders implements MultiValueMap, Serializable } /** - * Returns the value of the {@code Access-Control-Allow-Methods} response header. + * Return the value of the {@code Access-Control-Allow-Methods} response header. */ public List getAccessControlAllowMethods() { List result = new ArrayList(); @@ -478,7 +478,10 @@ public class HttpHeaders implements MultiValueMap, Serializable if (value != null) { String[] tokens = value.split(",\\s*"); for (String token : tokens) { - result.add(HttpMethod.valueOf(token)); + HttpMethod resolved = HttpMethod.resolve(token); + if (resolved != null) { + result.add(resolved); + } } } return result; @@ -492,7 +495,7 @@ public class HttpHeaders implements MultiValueMap, Serializable } /** - * Returns the value of the {@code Access-Control-Allow-Origin} response header. + * Return the value of the {@code Access-Control-Allow-Origin} response header. */ public String getAccessControlAllowOrigin() { return getFirst(ACCESS_CONTROL_ALLOW_ORIGIN); @@ -550,11 +553,10 @@ public class HttpHeaders implements MultiValueMap, Serializable } /** - * Returns the value of the {@code Access-Control-Request-Method} request header. + * Return the value of the {@code Access-Control-Request-Method} request header. */ public HttpMethod getAccessControlRequestMethod() { - String value = getFirst(ACCESS_CONTROL_REQUEST_METHOD); - return (value != null ? HttpMethod.valueOf(value) : null); + return HttpMethod.resolve(getFirst(ACCESS_CONTROL_REQUEST_METHOD)); } /** @@ -615,12 +617,15 @@ public class HttpHeaders implements MultiValueMap, Serializable public Set getAllow() { String value = getFirst(ALLOW); if (!StringUtils.isEmpty(value)) { - List allowedMethod = new ArrayList(5); + List result = new LinkedList(); String[] tokens = value.split(",\\s*"); for (String token : tokens) { - allowedMethod.add(HttpMethod.valueOf(token)); + HttpMethod resolved = HttpMethod.resolve(token); + if (resolved != null) { + result.add(resolved); + } } - return EnumSet.copyOf(allowedMethod); + return EnumSet.copyOf(result); } else { return EnumSet.noneOf(HttpMethod.class); @@ -635,7 +640,7 @@ public class HttpHeaders implements MultiValueMap, Serializable } /** - * Returns the value of the {@code Cache-Control} header. + * Return the value of the {@code Cache-Control} header. */ public String getCacheControl() { return getFirst(CACHE_CONTROL); @@ -656,7 +661,7 @@ public class HttpHeaders implements MultiValueMap, Serializable } /** - * Returns the value of the {@code Connection} header. + * Return the value of the {@code Connection} header. */ public List getConnection() { return getFirstValueAsList(CONNECTION); @@ -920,7 +925,7 @@ public class HttpHeaders implements MultiValueMap, Serializable } /** - * Returns the value of the {@code Range} header. + * Return the value of the {@code Range} header. *

Returns an empty list when the range is unknown. */ public List getRange() { @@ -936,7 +941,7 @@ public class HttpHeaders implements MultiValueMap, Serializable } /** - * Returns the value of the {@code Upgrade} header. + * Return the value of the {@code Upgrade} header. */ public String getUpgrade() { return getFirst(UPGRADE); diff --git a/spring-web/src/main/java/org/springframework/http/HttpMethod.java b/spring-web/src/main/java/org/springframework/http/HttpMethod.java index f006c703ccf283ea378f7927dcda3bfdaaf86b02..87173fd381961038fd48048cc2f65fc46f10b3d6 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpMethod.java +++ b/spring-web/src/main/java/org/springframework/http/HttpMethod.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,16 +16,52 @@ package org.springframework.http; +import java.util.HashMap; +import java.util.Map; + /** * Java 5 enumeration of HTTP request methods. Intended for use * with {@link org.springframework.http.client.ClientHttpRequest} * and {@link org.springframework.web.client.RestTemplate}. * * @author Arjen Poutsma + * @author Juergen Hoeller * @since 3.0 */ public enum HttpMethod { - GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE + GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE; + + + private static final Map mappings = new HashMap(8); + + static { + for (HttpMethod httpMethod : values()) { + mappings.put(httpMethod.name(), httpMethod); + } + } + + + /** + * Resolve the given method value to an {@code HttpMethod}. + * @param method the method value as a String + * @return the corresponding {@code HttpMethod}, or {@code null} if not found + * @since 4.2.4 + */ + public static HttpMethod resolve(String method) { + return (method != null ? mappings.get(method) : null); + } + + + /** + * Determine whether this {@code HttpMethod} matches the given + * method value. + * @param method the method value as a String + * @return {@code true} if it matches, {@code false} otherwise + * @since 4.2.4 + */ + public boolean matches(String method) { + return name().equals(method); + } } diff --git a/spring-web/src/main/java/org/springframework/http/HttpRequest.java b/spring-web/src/main/java/org/springframework/http/HttpRequest.java index b84df266de22c83f9a977cbd54bc6fd4b0a0d9af..2f670a37e7ee901b9c52415ee70e1ecebe0c2812 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/HttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,8 +19,8 @@ package org.springframework.http; import java.net.URI; /** - * Represents an HTTP request message, consisting of {@linkplain #getMethod() method} - * and {@linkplain #getURI() uri}. + * Represents an HTTP request message, consisting of + * {@linkplain #getMethod() method} and {@linkplain #getURI() uri}. * * @author Arjen Poutsma * @since 3.1 @@ -29,13 +29,14 @@ public interface HttpRequest extends HttpMessage { /** * Return the HTTP method of the request. - * @return the HTTP method as an HttpMethod enum value + * @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) */ HttpMethod getMethod(); /** * Return the URI of the request. - * @return the URI of the request + * @return the URI of the request (never {@code null}) */ URI getURI(); diff --git a/spring-web/src/main/java/org/springframework/http/RequestEntity.java b/spring-web/src/main/java/org/springframework/http/RequestEntity.java index e7e4b5e7631b785e8c1a276d965d8327eeef320a..7e9ab88b2fe4be28cc747a0760a955ba73b98749 100644 --- a/spring-web/src/main/java/org/springframework/http/RequestEntity.java +++ b/spring-web/src/main/java/org/springframework/http/RequestEntity.java @@ -20,7 +20,6 @@ import java.net.URI; import java.nio.charset.Charset; import java.util.Arrays; -import org.springframework.util.Assert; import org.springframework.util.MultiValueMap; import org.springframework.util.ObjectUtils; @@ -104,8 +103,6 @@ public class RequestEntity extends HttpEntity { */ public RequestEntity(T body, MultiValueMap headers, HttpMethod method, URI url) { super(body, headers); - Assert.notNull(method, "'method' is required"); - Assert.notNull(url, "'url' is required"); this.method = method; this.url = url; } diff --git a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsAsyncClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsAsyncClientHttpRequest.java index 4606c07a352d0791b2a3d6d3c977c1f801ea99e9..e062ba8db464db0bd04048c21a4c28f8635403c9 100644 --- a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsAsyncClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsAsyncClientHttpRequest.java @@ -68,7 +68,7 @@ final class HttpComponentsAsyncClientHttpRequest extends AbstractBufferingAsyncC @Override public HttpMethod getMethod() { - return HttpMethod.valueOf(this.httpRequest.getMethod()); + return HttpMethod.resolve(this.httpRequest.getMethod()); } @Override diff --git a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequest.java index 996a17e07e6c058f5b7436d2b591b554a609a7f5..0bf8d9fb04bd76282d105258f9ce9bfbe66103bf 100644 --- a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequest.java @@ -66,7 +66,7 @@ final class HttpComponentsClientHttpRequest extends AbstractBufferingClientHttpR @Override public HttpMethod getMethod() { - return HttpMethod.valueOf(this.httpRequest.getMethod()); + return HttpMethod.resolve(this.httpRequest.getMethod()); } @Override diff --git a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsStreamingClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsStreamingClientHttpRequest.java index 856a9ea68464627f0eb988515577ade138ab2f14..68756d7166602f71603e669d4934d1bc94d60f0a 100644 --- a/spring-web/src/main/java/org/springframework/http/client/HttpComponentsStreamingClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/HttpComponentsStreamingClientHttpRequest.java @@ -65,7 +65,7 @@ final class HttpComponentsStreamingClientHttpRequest extends AbstractClientHttpR @Override public HttpMethod getMethod() { - return HttpMethod.valueOf(this.httpRequest.getMethod()); + return HttpMethod.resolve(this.httpRequest.getMethod()); } @Override diff --git a/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingAsyncClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingAsyncClientHttpRequest.java index 336cf4f239f11961d9df97a012ec837877e313b2..015a2e42251f53560719d54b7409305588c4da63 100644 --- a/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingAsyncClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingAsyncClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -57,7 +57,7 @@ final class SimpleBufferingAsyncClientHttpRequest extends AbstractBufferingAsync @Override public HttpMethod getMethod() { - return HttpMethod.valueOf(this.connection.getRequestMethod()); + return HttpMethod.resolve(this.connection.getRequestMethod()); } @Override @@ -78,8 +78,8 @@ final class SimpleBufferingAsyncClientHttpRequest extends AbstractBufferingAsync @Override public ClientHttpResponse call() throws Exception { SimpleBufferingClientHttpRequest.addHeaders(connection, headers); - // JDK < 1.8 doesn't support getOutputStream with HTTP DELETE - if (HttpMethod.DELETE.equals(getMethod()) && bufferedOutput.length == 0) { + // JDK <1.8 doesn't support getOutputStream with HTTP DELETE + if (HttpMethod.DELETE == getMethod() && bufferedOutput.length == 0) { connection.setDoOutput(false); } if (connection.getDoOutput() && outputStreaming) { diff --git a/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingClientHttpRequest.java index c16f9848880d4f4b27c36087d0d1123a87a1feab..24195bebdf64f6f82983731a529a5c0143fdca67 100644 --- a/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingClientHttpRequest.java @@ -52,7 +52,7 @@ final class SimpleBufferingClientHttpRequest extends AbstractBufferingClientHttp @Override public HttpMethod getMethod() { - return HttpMethod.valueOf(this.connection.getRequestMethod()); + return HttpMethod.resolve(this.connection.getRequestMethod()); } @Override @@ -69,8 +69,8 @@ final class SimpleBufferingClientHttpRequest extends AbstractBufferingClientHttp protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException { addHeaders(this.connection, headers); - // JDK < 1.8 doesn't support getOutputStream with HTTP DELETE - if (HttpMethod.DELETE.equals(getMethod()) && bufferedOutput.length == 0) { + // JDK <1.8 doesn't support getOutputStream with HTTP DELETE + if (HttpMethod.DELETE == getMethod() && bufferedOutput.length == 0) { this.connection.setDoOutput(false); } diff --git a/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingAsyncClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingAsyncClientHttpRequest.java index cf6f59690b655e91721ef3da7c7c4dea99db8879..0417eef09cd7ba3d2bcd77922eb78bfb1971be68 100644 --- a/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingAsyncClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingAsyncClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -63,7 +63,7 @@ final class SimpleStreamingAsyncClientHttpRequest extends AbstractAsyncClientHtt @Override public HttpMethod getMethod() { - return HttpMethod.valueOf(this.connection.getRequestMethod()); + return HttpMethod.resolve(this.connection.getRequestMethod()); } @Override diff --git a/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingClientHttpRequest.java index 504f567a61f8ac9007128ff496ab19843d62f3a5..5e871d007c996e9993bfc55230c5450b67b642ea 100644 --- a/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -53,7 +53,7 @@ final class SimpleStreamingClientHttpRequest extends AbstractClientHttpRequest { public HttpMethod getMethod() { - return HttpMethod.valueOf(this.connection.getRequestMethod()); + return HttpMethod.resolve(this.connection.getRequestMethod()); } @Override diff --git a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java index 489dfa294f55e2580aba88f41d69ee7b5dffe4a5..fbbe1446d650c23114c439098c2e13eb496ad7b6 100644 --- a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -55,8 +55,6 @@ public class ServletServerHttpRequest implements ServerHttpRequest { protected static final String FORM_CHARSET = "UTF-8"; - private static final String METHOD_POST = "POST"; - private final HttpServletRequest servletRequest; @@ -84,7 +82,7 @@ public class ServletServerHttpRequest implements ServerHttpRequest { @Override public HttpMethod getMethod() { - return HttpMethod.valueOf(this.servletRequest.getMethod()); + return HttpMethod.resolve(this.servletRequest.getMethod()); } @Override @@ -166,9 +164,22 @@ public class ServletServerHttpRequest implements ServerHttpRequest { } } - private boolean isFormPost(HttpServletRequest request) { - return (request.getContentType() != null && request.getContentType().contains(FORM_CONTENT_TYPE) && - METHOD_POST.equalsIgnoreCase(request.getMethod())); + @Override + public ServerHttpAsyncRequestControl getAsyncRequestControl(ServerHttpResponse response) { + if (this.asyncRequestControl == null) { + Assert.isInstanceOf(ServletServerHttpResponse.class, response); + ServletServerHttpResponse servletServerResponse = (ServletServerHttpResponse) response; + this.asyncRequestControl = new ServletServerHttpAsyncRequestControl(this, servletServerResponse); + } + return this.asyncRequestControl; + } + + + + private static boolean isFormPost(HttpServletRequest request) { + String contentType = request.getContentType(); + return (contentType != null && contentType.contains(FORM_CONTENT_TYPE) && + HttpMethod.POST.matches(request.getMethod())); } /** @@ -177,7 +188,7 @@ public class ServletServerHttpRequest implements ServerHttpRequest { * from the body, which can fail if any other code has used ServletRequest * to access a parameter thus causing the input stream to be "consumed". */ - private InputStream getBodyFromServletRequestParameters(HttpServletRequest request) throws IOException { + private static InputStream getBodyFromServletRequestParameters(HttpServletRequest request) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); Writer writer = new OutputStreamWriter(bos, FORM_CHARSET); @@ -205,14 +216,4 @@ public class ServletServerHttpRequest implements ServerHttpRequest { return new ByteArrayInputStream(bos.toByteArray()); } - @Override - public ServerHttpAsyncRequestControl getAsyncRequestControl(ServerHttpResponse response) { - if (this.asyncRequestControl == null) { - Assert.isInstanceOf(ServletServerHttpResponse.class, response); - ServletServerHttpResponse servletServerResponse = (ServletServerHttpResponse) response; - this.asyncRequestControl = new ServletServerHttpAsyncRequestControl(this, servletServerResponse); - } - return this.asyncRequestControl; - } - } diff --git a/spring-web/src/main/java/org/springframework/web/HttpRequestMethodNotSupportedException.java b/spring-web/src/main/java/org/springframework/web/HttpRequestMethodNotSupportedException.java index 44096bb5839132916494460fc9dd1b49bd54c48e..13caeda9bb3c8ef56048b69fb09520f9e100035e 100644 --- a/spring-web/src/main/java/org/springframework/web/HttpRequestMethodNotSupportedException.java +++ b/spring-web/src/main/java/org/springframework/web/HttpRequestMethodNotSupportedException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,8 +17,9 @@ package org.springframework.web; import java.util.Collection; -import java.util.Collections; -import java.util.LinkedHashSet; +import java.util.EnumSet; +import java.util.LinkedList; +import java.util.List; import java.util.Set; import javax.servlet.ServletException; @@ -105,11 +106,14 @@ public class HttpRequestMethodNotSupportedException extends ServletException { * Return the actually supported HTTP methods, if known, as {@link HttpMethod} instances. */ public Set getSupportedHttpMethods() { - Set supportedMethods = new LinkedHashSet(); + List supportedMethods = new LinkedList(); for (String value : this.supportedMethods) { - supportedMethods.add(HttpMethod.valueOf(value)); + HttpMethod resolved = HttpMethod.resolve(value); + if (resolved != null) { + supportedMethods.add(resolved); + } } - return Collections.unmodifiableSet(supportedMethods); + return EnumSet.copyOf(supportedMethods); } } diff --git a/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMethod.java b/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMethod.java index 814687ca65df5d039a79c6aa815d2af67d56b763..80bd4b1c017d91e785a261bd1abd5274346ab12e 100644 --- a/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMethod.java +++ b/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMethod.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,16 +17,14 @@ package org.springframework.web.bind.annotation; /** - * Java 5 enumeration of HTTP request methods. Intended for use - * with the {@link RequestMapping#method()} attribute of the - * {@link RequestMapping} annotation. + * Java 5 enumeration of HTTP request methods. Intended for use with the + * {@link RequestMapping#method()} attribute of the {@link RequestMapping} annotation. * *

Note that, by default, {@link org.springframework.web.servlet.DispatcherServlet} * supports GET, HEAD, POST, PUT, PATCH and DELETE only. DispatcherServlet will - * process TRACE and OPTIONS with the default HttpServlet behavior unless - * explicitly told to dispatch those request types as well: Check out - * the "dispatchOptionsRequest" and "dispatchTraceRequest" properties, - * switching them to "true" if necessary. + * process TRACE and OPTIONS with the default HttpServlet behavior unless explicitly + * told to dispatch those request types as well: Check out the "dispatchOptionsRequest" + * and "dispatchTraceRequest" properties, switching them to "true" if necessary. * * @author Juergen Hoeller * @since 2.5 diff --git a/spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java b/spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java index 381e72bcc305429559c92a3a41d8e361c0c5a3c8..6cc8e052721fd3aed6d18166869fb610a5c75c9b 100644 --- a/spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java +++ b/spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java @@ -106,7 +106,7 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ * @since 4.0.2 */ public HttpMethod getHttpMethod() { - return HttpMethod.valueOf(getRequest().getMethod()); + return HttpMethod.resolve(getRequest().getMethod()); } @Override diff --git a/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java b/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java index 4cc231e50cd3fe28e6be9be7d89b0bb10618f7e5..76daa33bbc8118f9415986f02859ed9cecad7db6 100644 --- a/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java +++ b/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java @@ -344,10 +344,13 @@ public class CorsConfiguration { List result = new ArrayList(allowedMethods.size()); boolean allowed = false; for (String method : allowedMethods) { - if (requestMethod.name().equals(method)) { + if (requestMethod.matches(method)) { allowed = true; } - result.add(HttpMethod.valueOf(method)); + HttpMethod resolved = HttpMethod.resolve(method); + if (resolved != null) { + result.add(resolved); + } } return (allowed ? result : null); } diff --git a/spring-web/src/main/java/org/springframework/web/cors/CorsUtils.java b/spring-web/src/main/java/org/springframework/web/cors/CorsUtils.java index d85ddd4cdf2a59465c84573d433dd1cf8def7a64..c46f6956cfadb057d2baa5841be271ac6a759415 100644 --- a/spring-web/src/main/java/org/springframework/web/cors/CorsUtils.java +++ b/spring-web/src/main/java/org/springframework/web/cors/CorsUtils.java @@ -28,7 +28,7 @@ import org.springframework.http.HttpMethod; * @author Sebastien Deleuze * @since 4.2 */ -public class CorsUtils { +public abstract class CorsUtils { /** * Returns {@code true} if the request is a valid CORS one. @@ -41,7 +41,7 @@ public class CorsUtils { * Returns {@code true} if the request is a valid CORS pre-flight one. */ public static boolean isPreFlightRequest(HttpServletRequest request) { - return (isCorsRequest(request) && request.getMethod().equals(HttpMethod.OPTIONS.name()) && + return (isCorsRequest(request) && HttpMethod.OPTIONS.matches(request.getMethod()) && request.getHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD) != null); } diff --git a/spring-web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java b/spring-web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java index 211cb0ebb3388965ec9a014296497d146a5fec50..b3fbe838266884dcd451519834412080967692c5 100644 --- a/spring-web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java +++ b/spring-web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java @@ -144,7 +144,7 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter { protected boolean isEligibleForEtag(HttpServletRequest request, HttpServletResponse response, int responseStatusCode, InputStream inputStream) { - if (responseStatusCode >= 200 && responseStatusCode < 300 && HttpMethod.GET.name().equals(request.getMethod())) { + if (responseStatusCode >= 200 && responseStatusCode < 300 && HttpMethod.GET.matches(request.getMethod())) { String cacheControl = null; if (servlet3Present) { cacheControl = response.getHeader(HEADER_CACHE_CONTROL); diff --git a/spring-web/src/main/java/org/springframework/web/multipart/support/AbstractMultipartHttpServletRequest.java b/spring-web/src/main/java/org/springframework/web/multipart/support/AbstractMultipartHttpServletRequest.java index decbe3859f2c032bd2ea16956d9f820210618daa..4e423e44966bd716eb698d652009e5477a1054b3 100644 --- a/spring-web/src/main/java/org/springframework/web/multipart/support/AbstractMultipartHttpServletRequest.java +++ b/spring-web/src/main/java/org/springframework/web/multipart/support/AbstractMultipartHttpServletRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -61,7 +61,7 @@ public abstract class AbstractMultipartHttpServletRequest extends HttpServletReq @Override public HttpMethod getRequestMethod() { - return HttpMethod.valueOf(getRequest().getMethod()); + return HttpMethod.resolve(getRequest().getMethod()); } @Override diff --git a/spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java b/spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java index cfca663ad68108b86cb667e95235fda64e8cba47..9b9a97cffeddbbead9aaeac5728646a8fafe9f99 100644 --- a/spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java +++ b/spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java @@ -30,6 +30,8 @@ import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; +import org.springframework.http.HttpMethod; + /** * {@link javax.servlet.http.HttpServletRequest} wrapper that caches all content read from * the {@linkplain #getInputStream() input stream} and {@linkplain #getReader() reader}, @@ -46,8 +48,6 @@ public class ContentCachingRequestWrapper extends HttpServletRequestWrapper { private static final String FORM_CONTENT_TYPE = "application/x-www-form-urlencoded"; - private static final String METHOD_POST = "POST"; - private final ByteArrayOutputStream cachedContent; @@ -125,7 +125,7 @@ public class ContentCachingRequestWrapper extends HttpServletRequestWrapper { private boolean isFormPost() { String contentType = getContentType(); return (contentType != null && contentType.contains(FORM_CONTENT_TYPE) && - METHOD_POST.equalsIgnoreCase(getMethod())); + HttpMethod.POST.matches(getMethod())); } private void writeRequestParametersToCachedContent() { diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockMultipartHttpServletRequest.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockMultipartHttpServletRequest.java index a0c87ea3ac23482c3e7cc7341c5afb59fb3ff90b..7f218695a52f5f95ee1e6f89b9ffa3a7fd5a0349 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockMultipartHttpServletRequest.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockMultipartHttpServletRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -128,7 +128,7 @@ public class MockMultipartHttpServletRequest extends MockHttpServletRequest impl @Override public HttpMethod getRequestMethod() { - return HttpMethod.valueOf(getMethod()); + return HttpMethod.resolve(getMethod()); } @Override diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java index 706b3ab6a2c70c745a5912902a19e07ef8ac80c9..0143fdc8689166c8fc0c6d182e74863e21788b7c 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java @@ -1137,9 +1137,8 @@ public class DispatcherServlet extends FrameworkServlet { "] in DispatcherServlet with name '" + getServletName() + "'"); } if (this.throwExceptionIfNoHandlerFound) { - ServletServerHttpRequest sshr = new ServletServerHttpRequest(request); - throw new NoHandlerFoundException( - sshr.getMethod().name(), sshr.getServletRequest().getRequestURI(), sshr.getHeaders()); + throw new NoHandlerFoundException(request.getMethod(), getRequestUri(request), + new ServletServerHttpRequest(request).getHeaders()); } else { response.sendError(HttpServletResponse.SC_NOT_FOUND); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java index 478495d5c1d3f8ca52ecaa37d23ee63266112d2d..e3d859fc0ae189d3e89a5b0810e8b3187512de4c 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java @@ -42,6 +42,7 @@ import org.springframework.context.i18n.SimpleLocaleContext; import org.springframework.core.GenericTypeResolver; import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.http.HttpMethod; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.ObjectUtils; @@ -831,15 +832,13 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic /** - * Override the parent class implementation in order to intercept PATCH - * requests. + * Override the parent class implementation in order to intercept PATCH requests. */ @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - String method = request.getMethod(); - if (method.equalsIgnoreCase(RequestMethod.PATCH.name())) { + if (HttpMethod.PATCH.matches(request.getMethod())) { processRequest(request, response); } else { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java index d8399ebc3d6da8b2cdb7b41cb6e769a05baca3a0..75c78a910520218e314aabd9c298e16a8baa72dc 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java @@ -22,8 +22,8 @@ import java.io.PushbackInputStream; import java.lang.annotation.Annotation; import java.lang.reflect.Type; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; +import java.util.EnumSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; @@ -63,8 +63,8 @@ import org.springframework.web.method.support.HandlerMethodArgumentResolver; */ public abstract class AbstractMessageConverterMethodArgumentResolver implements HandlerMethodArgumentResolver { - private static final List SUPPORTED_METHODS = - Arrays.asList(HttpMethod.POST, HttpMethod.PUT, HttpMethod.PATCH); + private static final Set SUPPORTED_METHODS = + EnumSet.of(HttpMethod.POST, HttpMethod.PUT, HttpMethod.PATCH); private static final Object NO_VALUE = new Object(); @@ -228,8 +228,8 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements } if (body == NO_VALUE) { - if (!SUPPORTED_METHODS.contains(httpMethod) - || (noContentType && inputMessage.getBody() == null)) { + if (httpMethod == null || !SUPPORTED_METHODS.contains(httpMethod) || + (noContentType && inputMessage.getBody() == null)) { return null; } throw new HttpMediaTypeNotSupportedException(contentType, this.allSupportedMediaTypes); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessor.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessor.java index 630a8c123e4d4e3e58401502ed073aa132cbccb1..7741b1c4db454d3f6f6f689c54c36b4010ef2572 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessor.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessor.java @@ -19,7 +19,6 @@ package org.springframework.web.servlet.mvc.method.annotation; import java.io.IOException; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; -import java.net.URI; import java.util.List; import org.springframework.core.MethodParameter; @@ -122,9 +121,8 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro Object body = readWithMessageConverters(webRequest, parameter, paramType); if (RequestEntity.class == parameter.getParameterType()) { - URI url = inputMessage.getURI(); - HttpMethod httpMethod = inputMessage.getMethod(); - return new RequestEntity(body, inputMessage.getHeaders(), httpMethod, url); + return new RequestEntity(body, inputMessage.getHeaders(), + inputMessage.getMethod(), inputMessage.getURI()); } else { return new HttpEntity(body, inputMessage.getHeaders()); @@ -172,8 +170,7 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro Object body = responseEntity.getBody(); if (responseEntity instanceof ResponseEntity) { outputMessage.setStatusCode(((ResponseEntity) responseEntity).getStatusCode()); - if (inputMessage.getMethod().equals(HttpMethod.GET) && - isResourceNotModified(inputMessage, outputMessage)) { + if (HttpMethod.GET == inputMessage.getMethod() && isResourceNotModified(inputMessage, outputMessage)) { outputMessage.setStatusCode(HttpStatus.NOT_MODIFIED); // Ensure headers are flushed, no body should be written. outputMessage.flush(); diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/server/support/AbstractHandshakeHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/server/support/AbstractHandshakeHandler.java index 0b8b99afaf3c09af9f9ee60ac53e84d52c95661d..d76ed66d221375d2d13603fa3cedd19ba41bf007 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/server/support/AbstractHandshakeHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/server/support/AbstractHandshakeHandler.java @@ -231,7 +231,7 @@ public abstract class AbstractHandshakeHandler implements HandshakeHandler, Life logger.trace("Processing request " + request.getURI() + " with headers=" + headers); } try { - if (!HttpMethod.GET.equals(request.getMethod())) { + if (HttpMethod.GET != request.getMethod()) { response.setStatusCode(HttpStatus.METHOD_NOT_ALLOWED); response.getHeaders().setAllow(Collections.singleton(HttpMethod.GET)); if (logger.isErrorEnabled()) { @@ -320,8 +320,8 @@ public abstract class AbstractHandshakeHandler implements HandshakeHandler, Life ". Supported versions: " + Arrays.toString(getSupportedVersions())); } response.setStatusCode(HttpStatus.UPGRADE_REQUIRED); - response.getHeaders().put(WebSocketHttpHeaders.SEC_WEBSOCKET_VERSION, - Arrays.asList(StringUtils.arrayToCommaDelimitedString(getSupportedVersions()))); + response.getHeaders().set(WebSocketHttpHeaders.SEC_WEBSOCKET_VERSION, + StringUtils.arrayToCommaDelimitedString(getSupportedVersions())); } /** diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/support/AbstractSockJsService.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/support/AbstractSockJsService.java index 9c00716740239505022ce47a110e575741169d54..761671febca9097d4b225f09497ebdda798ceb12 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/support/AbstractSockJsService.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/support/AbstractSockJsService.java @@ -544,7 +544,7 @@ public abstract class AbstractSockJsService implements SockJsService, CorsConfig @Override public void handle(ServerHttpRequest request, ServerHttpResponse response) throws IOException { - if (HttpMethod.GET.equals(request.getMethod())) { + if (HttpMethod.GET == request.getMethod()) { addNoCacheHeaders(response); if (checkOrigin(request, response)) { response.getHeaders().setContentType(new MediaType("application", "json", UTF8_CHARSET)); @@ -554,7 +554,7 @@ public abstract class AbstractSockJsService implements SockJsService, CorsConfig } } - else if (HttpMethod.OPTIONS.equals(request.getMethod())) { + else if (HttpMethod.OPTIONS == request.getMethod()) { if (checkOrigin(request, response)) { addCacheHeaders(response); response.setStatusCode(HttpStatus.NO_CONTENT); diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportHandlingSockJsService.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportHandlingSockJsService.java index 811c0861b65708807b92c004afd4a182cdcfdf86..81d10a0b85a7ab3b5f05222b8e19686d78bdc6db 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportHandlingSockJsService.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportHandlingSockJsService.java @@ -247,8 +247,8 @@ public class TransportHandlingSockJsService extends AbstractSockJsService implem try { HttpMethod supportedMethod = transportType.getHttpMethod(); - if (!supportedMethod.equals(request.getMethod())) { - if (HttpMethod.OPTIONS.equals(request.getMethod()) && transportType.supportsCors()) { + if (supportedMethod != request.getMethod()) { + if (HttpMethod.OPTIONS == request.getMethod() && transportType.supportsCors()) { if (checkOrigin(request, response, HttpMethod.OPTIONS, supportedMethod)) { response.setStatusCode(HttpStatus.NO_CONTENT); addCacheHeaders(response);