From caed04473e331aafb651b2e376663105164ae67e Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sun, 18 Feb 2018 22:01:22 +0100 Subject: [PATCH] Consistent HttpMethod identity comparisons (cherry picked from commit 0de36d2) --- ...SimpleBufferingAsyncClientHttpRequest.java | 4 +-- .../SimpleBufferingClientHttpRequest.java | 4 +-- .../filter/HttpPutFormContentFilterTests.java | 8 +++-- .../web/servlet/FrameworkServlet.java | 4 +-- .../RequestMappingInfoHandlerMapping.java | 10 +++--- .../servlet/support/WebContentGenerator.java | 18 +++++------ .../RequestMethodsRequestConditionTests.java | 18 +++-------- ...RequestMappingInfoHandlerMappingTests.java | 32 +++++++------------ .../sockjs/support/AbstractSockJsService.java | 11 +++---- .../TransportHandlingSockJsService.java | 4 +-- 10 files changed, 47 insertions(+), 66 deletions(-) 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 bd43996365..e4f8da58aa 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-2016 the original author or authors. + * Copyright 2002-2018 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. @@ -79,7 +79,7 @@ final class SimpleBufferingAsyncClientHttpRequest extends AbstractBufferingAsync public ClientHttpResponse call() throws Exception { SimpleBufferingClientHttpRequest.addHeaders(connection, headers); // JDK <1.8 doesn't support getOutputStream with HTTP DELETE - if (HttpMethod.DELETE == getMethod() && bufferedOutput.length == 0) { + if (getMethod() == HttpMethod.DELETE && 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 9a992f332d..7b353a8404 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 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. @@ -69,7 +69,7 @@ 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 == getMethod() && bufferedOutput.length == 0) { + if (getMethod() == HttpMethod.DELETE && bufferedOutput.length == 0) { this.connection.setDoOutput(false); } if (this.connection.getDoOutput() && this.outputStreaming) { diff --git a/spring-web/src/test/java/org/springframework/web/filter/HttpPutFormContentFilterTests.java b/spring-web/src/test/java/org/springframework/web/filter/HttpPutFormContentFilterTests.java index 07758e96fb..4f900b6b5a 100644 --- a/spring-web/src/test/java/org/springframework/web/filter/HttpPutFormContentFilterTests.java +++ b/spring-web/src/test/java/org/springframework/web/filter/HttpPutFormContentFilterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -46,6 +46,7 @@ public class HttpPutFormContentFilterTests { private MockFilterChain filterChain; + @Before public void setup() { filter = new HttpPutFormContentFilter(); @@ -56,6 +57,7 @@ public class HttpPutFormContentFilterTests { filterChain = new MockFilterChain(); } + @Test public void wrapPutAndPatchOnly() throws Exception { request.setContent("foo=bar".getBytes("ISO-8859-1")); @@ -63,7 +65,7 @@ public class HttpPutFormContentFilterTests { request.setMethod(method.name()); filterChain = new MockFilterChain(); filter.doFilter(request, response, filterChain); - if (method.equals(HttpMethod.PUT) || method.equals(HttpMethod.PATCH)) { + if (method == HttpMethod.PUT || method == HttpMethod.PATCH) { assertNotSame("Should wrap HTTP method " + method, request, filterChain.getRequest()); } else { @@ -204,7 +206,7 @@ public class HttpPutFormContentFilterTests { assertArrayEquals(new String[] {"value4"}, parameters.get("name4")); } - @Test // SPR-15835 + @Test // SPR-15835 public void hiddenHttpMethodFilterFollowedByHttpPutFormContentFilter() throws Exception { request.addParameter("_method", "PUT"); request.addParameter("hiddenField", "testHidden"); 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 b3cb3c3b6c..24634b273e 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -839,7 +839,7 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic throws ServletException, IOException { HttpMethod httpMethod = HttpMethod.resolve(request.getMethod()); - if (HttpMethod.PATCH == httpMethod || httpMethod == null) { + if (httpMethod == HttpMethod.PATCH || httpMethod == null) { processRequest(request, response); } else { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java index 2c57650d1d..448fb8ea4e 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 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. @@ -439,16 +439,16 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe Set result = new LinkedHashSet(declaredMethods.size()); if (declaredMethods.isEmpty()) { for (HttpMethod method : HttpMethod.values()) { - if (!HttpMethod.TRACE.equals(method)) { + if (method != HttpMethod.TRACE) { result.add(method); } } } else { - boolean hasHead = declaredMethods.contains("HEAD"); for (String method : declaredMethods) { - result.add(HttpMethod.valueOf(method)); - if (!hasHead && "GET".equals(method)) { + HttpMethod httpMethod = HttpMethod.valueOf(method); + result.add(httpMethod); + if (httpMethod == HttpMethod.GET) { result.add(HttpMethod.HEAD); } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/WebContentGenerator.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/WebContentGenerator.java index 477014076e..a16d52a58a 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/WebContentGenerator.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/WebContentGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 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. @@ -173,7 +173,7 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport { if (this.supportedMethods == null) { allowedMethods = new ArrayList(HttpMethod.values().length - 1); for (HttpMethod method : HttpMethod.values()) { - if (!HttpMethod.TRACE.equals(method)) { + if (method != HttpMethod.TRACE) { allowedMethods.add(method.name()); } } @@ -190,13 +190,13 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport { } /** - * Return the "Allow" header value to use in response to an HTTP OPTIONS - * request based on the configured {@link #setSupportedMethods supported - * methods} also automatically adding "OPTIONS" to the list even if not - * present as a supported method. This means sub-classes don't have to - * explicitly list "OPTIONS" as a supported method as long as HTTP OPTIONS - * requests are handled before making a call to - * {@link #checkRequest(HttpServletRequest)}. + * Return the "Allow" header value to use in response to an HTTP OPTIONS request + * based on the configured {@link #setSupportedMethods supported methods} also + * automatically adding "OPTIONS" to the list even if not present as a supported + * method. This means subclasses don't have to explicitly list "OPTIONS" as a + * supported method as long as HTTP OPTIONS requests are handled before making a + * call to {@link #checkRequest(HttpServletRequest)}. + * @since 4.3 */ protected String getAllowHeader() { return this.allowHeader; diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestConditionTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestConditionTests.java index 70558fe776..1a9ade0565 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestConditionTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/RequestMethodsRequestConditionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 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,7 +17,6 @@ package org.springframework.web.servlet.mvc.condition; import java.util.Collections; - import javax.servlet.DispatcherType; import javax.servlet.http.HttpServletRequest; @@ -27,17 +26,8 @@ import org.springframework.http.HttpHeaders; import org.springframework.mock.web.test.MockHttpServletRequest; import org.springframework.web.bind.annotation.RequestMethod; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.springframework.web.bind.annotation.RequestMethod.DELETE; -import static org.springframework.web.bind.annotation.RequestMethod.GET; -import static org.springframework.web.bind.annotation.RequestMethod.HEAD; -import static org.springframework.web.bind.annotation.RequestMethod.OPTIONS; -import static org.springframework.web.bind.annotation.RequestMethod.POST; -import static org.springframework.web.bind.annotation.RequestMethod.PUT; +import static org.junit.Assert.*; +import static org.springframework.web.bind.annotation.RequestMethod.*; /** * @author Arjen Poutsma @@ -63,7 +53,7 @@ public class RequestMethodsRequestConditionTests { public void getMatchingConditionWithEmptyConditions() { RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(); for (RequestMethod method : RequestMethod.values()) { - if (!OPTIONS.equals(method)) { + if (method != OPTIONS) { HttpServletRequest request = new MockHttpServletRequest(method.name(), ""); assertNotNull(condition.getMatchingCondition(request)); } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java index e179252010..ecd6502aa3 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 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,9 +16,6 @@ package org.springframework.web.servlet.mvc.method; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; - import java.lang.reflect.Method; import java.util.Arrays; import java.util.Collections; @@ -26,7 +23,6 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; - import javax.servlet.http.HttpServletRequest; import org.junit.Before; @@ -63,6 +59,8 @@ import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition; import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition; import org.springframework.web.util.UrlPathHelper; +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; /** * Test fixture with {@link RequestMappingInfoHandlerMapping}. @@ -84,7 +82,7 @@ public class RequestMappingInfoHandlerMappingTests { @Before - public void setUp() throws Exception { + public void setup() throws Exception { TestController testController = new TestController(); this.fooMethod = new HandlerMethod(testController, "foo"); @@ -157,9 +155,7 @@ public class RequestMappingInfoHandlerMappingTests { } } - // SPR-9603 - - @Test(expected=HttpMediaTypeNotAcceptableException.class) + @Test(expected = HttpMediaTypeNotAcceptableException.class) // SPR-9603 public void getHandlerRequestMethodMatchFalsePositive() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/users"); request.addHeader("Accept", "application/xml"); @@ -167,9 +163,7 @@ public class RequestMappingInfoHandlerMappingTests { this.handlerMapping.getHandler(request); } - // SPR-8462 - - @Test + @Test // SPR-8462 public void getHandlerMediaTypeNotSupported() throws Exception { testHttpMediaTypeNotSupportedException("/person/1"); testHttpMediaTypeNotSupportedException("/person/1/"); @@ -197,18 +191,14 @@ public class RequestMappingInfoHandlerMappingTests { } } - // SPR-8462 - - @Test + @Test // SPR-8462 public void getHandlerMediaTypeNotAccepted() throws Exception { testHttpMediaTypeNotAcceptableException("/persons"); testHttpMediaTypeNotAcceptableException("/persons/"); testHttpMediaTypeNotAcceptableException("/persons.json"); } - // SPR-12854 - - @Test + @Test // SPR-12854 public void getHandlerUnsatisfiedServletRequestParameterException() throws Exception { try { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/params"); @@ -275,10 +265,8 @@ public class RequestMappingInfoHandlerMappingTests { assertEquals("2", uriVariables.get("path2")); } - // SPR-9098 - @SuppressWarnings("unchecked") - @Test + @Test // SPR-9098 public void handleMatchUriTemplateVariablesDecode() { RequestMappingInfo key = RequestMappingInfo.paths("/{group}/{identifier}").build(); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/group/a%2Fb"); @@ -502,6 +490,7 @@ public class RequestMappingInfoHandlerMappingTests { } } + @SuppressWarnings("unused") @Controller private static class UserController { @@ -515,6 +504,7 @@ public class RequestMappingInfoHandlerMappingTests { } } + private static class TestRequestMappingInfoHandlerMapping extends RequestMappingInfoHandlerMapping { public void registerHandler(Object handler) { 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 e56169b744..6f4d4e0e87 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 @@ -23,7 +23,6 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Date; -import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Random; @@ -515,7 +514,7 @@ public abstract class AbstractSockJsService implements SockJsService, CorsConfig protected void sendMethodNotAllowed(ServerHttpResponse response, HttpMethod... httpMethods) { logger.warn("Sending Method Not Allowed (405)"); response.setStatusCode(HttpStatus.METHOD_NOT_ALLOWED); - response.getHeaders().setAllow(new HashSet(Arrays.asList(httpMethods))); + response.getHeaders().setAllow(new LinkedHashSet(Arrays.asList(httpMethods))); } @@ -545,7 +544,7 @@ public abstract class AbstractSockJsService implements SockJsService, CorsConfig @Override public void handle(ServerHttpRequest request, ServerHttpResponse response) throws IOException { - if (HttpMethod.GET == request.getMethod()) { + if (request.getMethod() == HttpMethod.GET) { addNoCacheHeaders(response); if (checkOrigin(request, response)) { response.getHeaders().setContentType(new MediaType("application", "json", UTF8_CHARSET)); @@ -555,14 +554,14 @@ public abstract class AbstractSockJsService implements SockJsService, CorsConfig } } - else if (HttpMethod.OPTIONS == request.getMethod()) { + else if (request.getMethod() == HttpMethod.OPTIONS) { if (checkOrigin(request, response)) { addCacheHeaders(response); response.setStatusCode(HttpStatus.NO_CONTENT); } } else { - sendMethodNotAllowed(response, HttpMethod.OPTIONS, HttpMethod.GET); + sendMethodNotAllowed(response, HttpMethod.GET, HttpMethod.OPTIONS); } } }; @@ -590,7 +589,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 (request.getMethod() != HttpMethod.GET) { sendMethodNotAllowed(response, HttpMethod.GET); return; } 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 1055b8d377..465762f4ef 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -250,7 +250,7 @@ public class TransportHandlingSockJsService extends AbstractSockJsService implem try { HttpMethod supportedMethod = transportType.getHttpMethod(); if (supportedMethod != request.getMethod()) { - if (HttpMethod.OPTIONS == request.getMethod() && transportType.supportsCors()) { + if (request.getMethod() == HttpMethod.OPTIONS && transportType.supportsCors()) { if (checkOrigin(request, response, HttpMethod.OPTIONS, supportedMethod)) { response.setStatusCode(HttpStatus.NO_CONTENT); addCacheHeaders(response);