Consistent HttpMethod resolution against underlying HttpServletRequest

master
Juergen Hoeller 9 years ago
parent 90409cbe98
commit 11b4e3be2c
  1. 2
      spring-web/src/main/java/org/springframework/web/context/request/ServletWebRequest.java
  2. 13
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessor.java
  3. 2
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterTests.java
  4. 10
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandlerTests.java
  5. 3
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethodTests.java

@ -106,7 +106,7 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
* @since 4.0.2 * @since 4.0.2
*/ */
public HttpMethod getHttpMethod() { public HttpMethod getHttpMethod() {
return HttpMethod.valueOf(getRequest().getMethod().trim().toUpperCase()); return HttpMethod.valueOf(getRequest().getMethod());
} }
@Override @Override

@ -57,7 +57,6 @@ import org.springframework.web.method.support.ModelAndViewContainer;
*/ */
public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodProcessor { public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodProcessor {
/** /**
* Basic constructor with converters only. Suitable for resolving * Basic constructor with converters only. Suitable for resolving
* {@code HttpEntity}. For handling {@code ResponseEntity} consider also * {@code HttpEntity}. For handling {@code ResponseEntity} consider also
@ -169,11 +168,12 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro
if (!entityHeaders.isEmpty()) { if (!entityHeaders.isEmpty()) {
outputMessage.getHeaders().putAll(entityHeaders); outputMessage.getHeaders().putAll(entityHeaders);
} }
Object body = responseEntity.getBody(); Object body = responseEntity.getBody();
if (responseEntity instanceof ResponseEntity) { if (responseEntity instanceof ResponseEntity) {
outputMessage.setStatusCode(((ResponseEntity<?>) responseEntity).getStatusCode()); outputMessage.setStatusCode(((ResponseEntity<?>) responseEntity).getStatusCode());
if ("GET".equals(inputMessage.getServletRequest().getMethod()) if (inputMessage.getMethod() == HttpMethod.GET &&
&& isResourceNotModified(inputMessage, outputMessage)) { isResourceNotModified(inputMessage, outputMessage)) {
outputMessage.setStatusCode(HttpStatus.NOT_MODIFIED); outputMessage.setStatusCode(HttpStatus.NOT_MODIFIED);
// Ensure headers are flushed, no body should be written. // Ensure headers are flushed, no body should be written.
outputMessage.flush(); outputMessage.flush();
@ -190,7 +190,6 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro
} }
private boolean isResourceNotModified(ServletServerHttpRequest inputMessage, ServletServerHttpResponse outputMessage) { private boolean isResourceNotModified(ServletServerHttpRequest inputMessage, ServletServerHttpResponse outputMessage) {
List<String> ifNoneMatch = inputMessage.getHeaders().getIfNoneMatch(); List<String> ifNoneMatch = inputMessage.getHeaders().getIfNoneMatch();
long ifModifiedSince = inputMessage.getHeaders().getIfModifiedSince(); long ifModifiedSince = inputMessage.getHeaders().getIfModifiedSince();
String eTag = addEtagPadding(outputMessage.getHeaders().getETag()); String eTag = addEtagPadding(outputMessage.getHeaders().getETag());
@ -212,10 +211,10 @@ public class HttpEntityMethodProcessor extends AbstractMessageConverterMethodPro
private boolean isETagNotModified(List<String> ifNoneMatch, String etag) { private boolean isETagNotModified(List<String> ifNoneMatch, String etag) {
if (StringUtils.hasLength(etag)) { if (StringUtils.hasLength(etag)) {
for (String clientETag : ifNoneMatch) { 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) && if (StringUtils.hasLength(clientETag) &&
(clientETag.replaceFirst("^W/", "").equals(etag.replaceFirst("^W/", "")) (clientETag.replaceFirst("^W/", "").equals(etag.replaceFirst("^W/", "")) ||
|| clientETag.equals("*"))) { clientETag.equals("*"))) {
return true; return true;
} }
} }

@ -96,7 +96,7 @@ public class RequestMappingHandlerAdapterTests {
this.webAppContext = new StaticWebApplicationContext(); this.webAppContext = new StaticWebApplicationContext();
this.handlerAdapter = new RequestMappingHandlerAdapter(); this.handlerAdapter = new RequestMappingHandlerAdapter();
this.handlerAdapter.setApplicationContext(this.webAppContext); this.handlerAdapter.setApplicationContext(this.webAppContext);
this.request = new MockHttpServletRequest(); this.request = new MockHttpServletRequest("GET", "/");
this.response = new MockHttpServletResponse(); this.response = new MockHttpServletResponse();
} }

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.web.servlet.mvc.method.annotation; package org.springframework.web.servlet.mvc.method.annotation;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.ConversionNotSupportedException; import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException; 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 org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.mockito.Mockito;
/** /**
* Test fixture for {@link ResponseEntityExceptionHandler}. * Test fixture for {@link ResponseEntityExceptionHandler}.
@ -71,14 +71,14 @@ public class ResponseEntityExceptionHandlerTests {
private WebRequest request; private WebRequest request;
private HttpServletRequest servletRequest; private MockHttpServletRequest servletRequest;
private MockHttpServletResponse servletResponse; private MockHttpServletResponse servletResponse;
@Before @Before
public void setup() { public void setup() {
this.servletRequest = new MockHttpServletRequest(); this.servletRequest = new MockHttpServletRequest("GET", "/");
this.servletResponse = new MockHttpServletResponse(); this.servletResponse = new MockHttpServletResponse();
this.request = new ServletWebRequest(this.servletRequest, this.servletResponse); this.request = new ServletWebRequest(this.servletRequest, this.servletResponse);

@ -22,7 +22,6 @@ import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.junit.Test; import org.junit.Test;
@ -66,7 +65,7 @@ public class ServletInvocableHandlerMethodTests {
private final ModelAndViewContainer mavContainer = new ModelAndViewContainer(); 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(); private final MockHttpServletResponse response = new MockHttpServletResponse();

Loading…
Cancel
Save