From 11b4e3be2cef95d398a2e1e432786b0b1a3b7f3c Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 24 Sep 2015 20:53:22 +0200 Subject: [PATCH] Consistent HttpMethod resolution against underlying HttpServletRequest --- .../web/context/request/ServletWebRequest.java | 2 +- .../annotation/HttpEntityMethodProcessor.java | 13 ++++++------- .../RequestMappingHandlerAdapterTests.java | 2 +- .../ResponseEntityExceptionHandlerTests.java | 10 +++++----- .../ServletInvocableHandlerMethodTests.java | 3 +-- 5 files changed, 14 insertions(+), 16 deletions(-) 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 c734e08f6c..493a1aabfc 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().trim().toUpperCase()); + return HttpMethod.valueOf(getRequest().getMethod()); } @Override 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 7446aa19c4..ee7c53ac64 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 @@ -57,7 +57,6 @@ import org.springframework.web.method.support.ModelAndViewContainer; */ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodProcessor { - /** * Basic constructor with converters only. Suitable for resolving * {@code HttpEntity}. For handling {@code ResponseEntity} consider also @@ -169,11 +168,12 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro if (!entityHeaders.isEmpty()) { outputMessage.getHeaders().putAll(entityHeaders); } + Object body = responseEntity.getBody(); if (responseEntity instanceof ResponseEntity) { outputMessage.setStatusCode(((ResponseEntity) responseEntity).getStatusCode()); - if ("GET".equals(inputMessage.getServletRequest().getMethod()) - && isResourceNotModified(inputMessage, outputMessage)) { + if (inputMessage.getMethod() == HttpMethod.GET && + isResourceNotModified(inputMessage, outputMessage)) { outputMessage.setStatusCode(HttpStatus.NOT_MODIFIED); // Ensure headers are flushed, no body should be written. outputMessage.flush(); @@ -190,7 +190,6 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro } private boolean isResourceNotModified(ServletServerHttpRequest inputMessage, ServletServerHttpResponse outputMessage) { - List ifNoneMatch = inputMessage.getHeaders().getIfNoneMatch(); long ifModifiedSince = inputMessage.getHeaders().getIfModifiedSince(); String eTag = addEtagPadding(outputMessage.getHeaders().getETag()); @@ -212,10 +211,10 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro private boolean isETagNotModified(List ifNoneMatch, String etag) { if (StringUtils.hasLength(etag)) { for (String clientETag : ifNoneMatch) { - // compare weak/strong ETags as per https://tools.ietf.org/html/rfc7232#section-2.3 + // Compare weak/strong ETags as per https://tools.ietf.org/html/rfc7232#section-2.3 if (StringUtils.hasLength(clientETag) && - (clientETag.replaceFirst("^W/", "").equals(etag.replaceFirst("^W/", "")) - || clientETag.equals("*"))) { + (clientETag.replaceFirst("^W/", "").equals(etag.replaceFirst("^W/", "")) || + clientETag.equals("*"))) { return true; } } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterTests.java index e0ad67ad63..acbb8fc73e 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterTests.java @@ -96,7 +96,7 @@ public class RequestMappingHandlerAdapterTests { this.webAppContext = new StaticWebApplicationContext(); this.handlerAdapter = new RequestMappingHandlerAdapter(); this.handlerAdapter.setApplicationContext(this.webAppContext); - this.request = new MockHttpServletRequest(); + this.request = new MockHttpServletRequest("GET", "/"); this.response = new MockHttpServletResponse(); } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandlerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandlerTests.java index a5fb558868..bc25f84fee 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandlerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandlerTests.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. @@ -13,16 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.web.servlet.mvc.method.annotation; import java.lang.reflect.Method; import java.util.Arrays; import java.util.EnumSet; import java.util.List; -import javax.servlet.http.HttpServletRequest; import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; import org.springframework.beans.ConversionNotSupportedException; import org.springframework.beans.TypeMismatchException; @@ -56,7 +57,6 @@ import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMeth import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver; import static org.junit.Assert.*; -import org.mockito.Mockito; /** * Test fixture for {@link ResponseEntityExceptionHandler}. @@ -71,14 +71,14 @@ public class ResponseEntityExceptionHandlerTests { private WebRequest request; - private HttpServletRequest servletRequest; + private MockHttpServletRequest servletRequest; private MockHttpServletResponse servletResponse; @Before public void setup() { - this.servletRequest = new MockHttpServletRequest(); + this.servletRequest = new MockHttpServletRequest("GET", "/"); this.servletResponse = new MockHttpServletResponse(); this.request = new ServletWebRequest(this.servletRequest, this.servletResponse); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethodTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethodTests.java index 157efb024f..431e4d5ccf 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethodTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethodTests.java @@ -22,7 +22,6 @@ import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; import java.util.List; - import javax.servlet.http.HttpServletResponse; import org.junit.Test; @@ -66,7 +65,7 @@ public class ServletInvocableHandlerMethodTests { private final ModelAndViewContainer mavContainer = new ModelAndViewContainer(); - private final MockHttpServletRequest request = new MockHttpServletRequest(); + private final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); private final MockHttpServletResponse response = new MockHttpServletResponse(); -- GitLab