|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2018 the original author or authors. |
| 2 | + * Copyright 2002-2019 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
21 | 21 | import java.lang.reflect.Method;
|
22 | 22 | import java.lang.reflect.Type;
|
23 | 23 | import java.util.concurrent.Callable;
|
| 24 | +import javax.servlet.http.HttpServletRequest; |
24 | 25 | import javax.servlet.http.HttpServletResponse;
|
25 | 26 |
|
26 | 27 | import org.springframework.core.MethodParameter;
|
|
33 | 34 | import org.springframework.web.bind.annotation.ResponseBody;
|
34 | 35 | import org.springframework.web.bind.annotation.ResponseStatus;
|
35 | 36 | import org.springframework.web.context.request.ServletWebRequest;
|
| 37 | +import org.springframework.web.filter.ShallowEtagHeaderFilter; |
36 | 38 | import org.springframework.web.method.HandlerMethod;
|
37 | 39 | import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
|
38 | 40 | import org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite;
|
@@ -104,6 +106,7 @@ public void invokeAndHandle(ServletWebRequest webRequest, ModelAndViewContainer
|
104 | 106 |
|
105 | 107 | if (returnValue == null) {
|
106 | 108 | if (isRequestNotModified(webRequest) || getResponseStatus() != null || mavContainer.isRequestHandled()) {
|
| 109 | + disableContentCachingIfNecessary(webRequest); |
107 | 110 | mavContainer.setRequestHandled(true);
|
108 | 111 | return;
|
109 | 112 | }
|
@@ -160,6 +163,18 @@ private boolean isRequestNotModified(ServletWebRequest webRequest) {
|
160 | 163 | return webRequest.isNotModified();
|
161 | 164 | }
|
162 | 165 |
|
| 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 | + |
163 | 178 | private String formatErrorForReturnValue(@Nullable Object returnValue) {
|
164 | 179 | return "Error handling return value=[" + returnValue + "]" +
|
165 | 180 | (returnValue != null ? ", type=" + returnValue.getClass().getName() : "") +
|
|
0 commit comments