Skip to content

Commit 2dcf527

Browse files
committed
Consistent and defensive Servlet 3.0 method calls across Spring's web abstraction
(in particular, also working in debug mode where method references may get resolved early, so ternary expressions are to be avoided)
1 parent de0f3ae commit 2dcf527

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

spring-web/src/main/java/org/springframework/http/server/ServletServerHttpResponse.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -39,6 +39,7 @@
3939
*/
4040
public class ServletServerHttpResponse implements ServerHttpResponse {
4141

42+
/** Checking for Servlet 3.0+ HttpServletResponse.getHeader(String) */
4243
private static final boolean servlet3Present =
4344
ClassUtils.hasMethod(HttpServletResponse.class, "getHeader", String.class);
4445

@@ -55,7 +56,7 @@ public class ServletServerHttpResponse implements ServerHttpResponse {
5556
* @param servletResponse the servlet response
5657
*/
5758
public ServletServerHttpResponse(HttpServletResponse servletResponse) {
58-
Assert.notNull(servletResponse, "'servletResponse' must not be null");
59+
Assert.notNull(servletResponse, "HttpServletResponse must not be null");
5960
this.servletResponse = servletResponse;
6061
this.headers = (servlet3Present ? new ServletResponseHttpHeaders() : new HttpHeaders());
6162
}

spring-web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
5757

5858

5959
/** Checking for Servlet 3.0+ HttpServletResponse.getHeader(String) */
60-
private static final boolean responseGetHeaderAvailable =
60+
private static final boolean servlet3Present =
6161
ClassUtils.hasMethod(HttpServletResponse.class, "getHeader", String.class);
6262

6363

@@ -149,9 +149,11 @@ else if (isEligibleForEtag(request, responseWrapper, statusCode, body)) {
149149
protected boolean isEligibleForEtag(HttpServletRequest request, HttpServletResponse response,
150150
int responseStatusCode, byte[] responseBody) {
151151

152-
if (responseStatusCode >= 200 && responseStatusCode < 300 &&
153-
HttpMethod.GET.name().equals(request.getMethod())) {
154-
String cacheControl = (responseGetHeaderAvailable ? response.getHeader(HEADER_CACHE_CONTROL) : null);
152+
if (responseStatusCode >= 200 && responseStatusCode < 300 && HttpMethod.GET.name().equals(request.getMethod())) {
153+
String cacheControl = null;
154+
if (servlet3Present) {
155+
cacheControl = response.getHeader(HEADER_CACHE_CONTROL);
156+
}
155157
if (cacheControl == null || !cacheControl.contains(DIRECTIVE_NO_STORE)) {
156158
return true;
157159
}

0 commit comments

Comments
 (0)