Skip to content

Commit 1a97a26

Browse files
committed
Disable response caching when controller resolves ETag
See gh-22797
1 parent 13c746a commit 1a97a26

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -48,6 +48,7 @@
4848
import org.springframework.web.bind.support.WebDataBinderFactory;
4949
import org.springframework.web.context.request.NativeWebRequest;
5050
import org.springframework.web.context.request.ServletWebRequest;
51+
import org.springframework.web.filter.ShallowEtagHeaderFilter;
5152
import org.springframework.web.method.support.ModelAndViewContainer;
5253
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
5354
import org.springframework.web.servlet.support.RequestContextUtils;
@@ -207,6 +208,7 @@ public void handleReturnValue(@Nullable Object returnValue, MethodParameter retu
207208
&& isResourceNotModified(inputMessage, outputMessage)) {
208209
// Ensure headers are flushed, no body should be written.
209210
outputMessage.flush();
211+
ShallowEtagHeaderFilter.disableContentCaching(inputMessage.getServletRequest());
210212
// Skip call to converters, as they may update the body.
211213
return;
212214
}

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethod.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -21,6 +21,7 @@
2121
import java.lang.reflect.Method;
2222
import java.lang.reflect.Type;
2323
import java.util.concurrent.Callable;
24+
import javax.servlet.http.HttpServletRequest;
2425
import javax.servlet.http.HttpServletResponse;
2526

2627
import org.springframework.core.MethodParameter;
@@ -33,6 +34,7 @@
3334
import org.springframework.web.bind.annotation.ResponseBody;
3435
import org.springframework.web.bind.annotation.ResponseStatus;
3536
import org.springframework.web.context.request.ServletWebRequest;
37+
import org.springframework.web.filter.ShallowEtagHeaderFilter;
3638
import org.springframework.web.method.HandlerMethod;
3739
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
3840
import org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite;
@@ -104,6 +106,7 @@ public void invokeAndHandle(ServletWebRequest webRequest, ModelAndViewContainer
104106

105107
if (returnValue == null) {
106108
if (isRequestNotModified(webRequest) || getResponseStatus() != null || mavContainer.isRequestHandled()) {
109+
disableContentCachingIfNecessary(webRequest);
107110
mavContainer.setRequestHandled(true);
108111
return;
109112
}
@@ -160,6 +163,18 @@ private boolean isRequestNotModified(ServletWebRequest webRequest) {
160163
return webRequest.isNotModified();
161164
}
162165

166+
private void disableContentCachingIfNecessary(ServletWebRequest webRequest) {
167+
if (!isRequestNotModified(webRequest)) {
168+
HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
169+
Assert.notNull(response, "Expected HttpServletResponse");
170+
if (StringUtils.hasText(response.getHeader("ETag"))) {
171+
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
172+
Assert.notNull(request, "Expected HttpServletRequest");
173+
ShallowEtagHeaderFilter.disableContentCaching(request);
174+
}
175+
}
176+
}
177+
163178
private String formatErrorForReturnValue(@Nullable Object returnValue) {
164179
return "Error handling return value=[" + returnValue + "]" +
165180
(returnValue != null ? ", type=" + returnValue.getClass().getName() : "") +

0 commit comments

Comments
 (0)