27
27
28
28
import org .springframework .http .HttpMethod ;
29
29
import org .springframework .http .HttpStatus ;
30
+ import org .springframework .util .ClassUtils ;
30
31
import org .springframework .util .CollectionUtils ;
31
32
import org .springframework .util .ObjectUtils ;
32
33
import org .springframework .util .StringUtils ;
@@ -54,6 +55,10 @@ public class ServletWebRequest extends ServletRequestAttributes implements Nativ
54
55
55
56
private static final String METHOD_HEAD = "HEAD" ;
56
57
58
+ /** Checking for Servlet 3.0+ HttpServletResponse.getStatus() */
59
+ private static final boolean responseGetStatusAvailable =
60
+ ClassUtils .hasMethod (HttpServletResponse .class , "getStatus" );
61
+
57
62
58
63
private boolean notModified = false ;
59
64
@@ -173,7 +178,7 @@ public boolean isSecure() {
173
178
public boolean checkNotModified (long lastModifiedTimestamp ) {
174
179
HttpServletResponse response = getResponse ();
175
180
if (lastModifiedTimestamp >= 0 && !this .notModified ) {
176
- if (response == null || HttpStatus . valueOf (response . getStatus ()). is2xxSuccessful ( )) {
181
+ if (isCompatibleWithConditionalRequests (response )) {
177
182
this .notModified = isTimestampNotModified (lastModifiedTimestamp );
178
183
if (response != null ) {
179
184
if (this .notModified && supportsNotModifiedStatus ()) {
@@ -188,6 +193,12 @@ public boolean checkNotModified(long lastModifiedTimestamp) {
188
193
return this .notModified ;
189
194
}
190
195
196
+ private boolean isCompatibleWithConditionalRequests (HttpServletResponse response ) {
197
+ return response == null
198
+ || !responseGetStatusAvailable
199
+ || HttpStatus .valueOf (response .getStatus ()).is2xxSuccessful ();
200
+ }
201
+
191
202
@ SuppressWarnings ("deprecation" )
192
203
private boolean isTimestampNotModified (long lastModifiedTimestamp ) {
193
204
long ifModifiedSince = -1 ;
@@ -215,7 +226,7 @@ private boolean isTimestampNotModified(long lastModifiedTimestamp) {
215
226
public boolean checkNotModified (String etag ) {
216
227
HttpServletResponse response = getResponse ();
217
228
if (StringUtils .hasLength (etag ) && !this .notModified ) {
218
- if (response == null || HttpStatus . valueOf (response . getStatus ()). is2xxSuccessful ( )) {
229
+ if (isCompatibleWithConditionalRequests (response )) {
219
230
etag = addEtagPadding (etag );
220
231
this .notModified = isETagNotModified (etag );
221
232
if (response != null ) {
@@ -265,7 +276,7 @@ private boolean supportsNotModifiedStatus() {
265
276
public boolean checkNotModified (String etag , long lastModifiedTimestamp ) {
266
277
HttpServletResponse response = getResponse ();
267
278
if (StringUtils .hasLength (etag ) && !this .notModified ) {
268
- if (response == null || HttpStatus . valueOf (response . getStatus ()). is2xxSuccessful ( )) {
279
+ if (isCompatibleWithConditionalRequests (response )) {
269
280
etag = addEtagPadding (etag );
270
281
this .notModified = isETagNotModified (etag ) && isTimestampNotModified (lastModifiedTimestamp );
271
282
if (response != null ) {
0 commit comments