Skip to content

Commit 9af4f5c

Browse files
committed
Remove deprecated API in WebContentGenerator
Closes gh-31492
1 parent 92d1ebe commit 9af4f5c

File tree

2 files changed

+8
-297
lines changed

2 files changed

+8
-297
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/support/WebContentGenerator.java

Lines changed: 8 additions & 240 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,6 @@
5050
* headers can be controlled via the {@link #setCacheSeconds "cacheSeconds"}
5151
* and {@link #setCacheControl "cacheControl"} properties.
5252
*
53-
* <p><b>NOTE:</b> As of Spring 4.2, this generator's default behavior changed when
54-
* using only {@link #setCacheSeconds}, sending HTTP response headers that are in line
55-
* with current browsers and proxies implementations (i.e. no HTTP 1.0 headers anymore)
56-
* Reverting to the previous behavior can be easily done by using one of the newly
57-
* deprecated methods {@link #setUseExpiresHeader}, {@link #setUseCacheControlHeader},
58-
* {@link #setUseCacheControlNoStore} or {@link #setAlwaysMustRevalidate}.
59-
*
6053
* @author Rod Johnson
6154
* @author Juergen Hoeller
6255
* @author Brian Clozel
@@ -76,10 +69,6 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
7669
/** HTTP method "POST". */
7770
public static final String METHOD_POST = "POST";
7871

79-
private static final String HEADER_PRAGMA = "Pragma";
80-
81-
private static final String HEADER_EXPIRES = "Expires";
82-
8372
protected static final String HEADER_CACHE_CONTROL = "Cache-Control";
8473

8574

@@ -101,20 +90,6 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
10190
private String[] varyByRequestHeaders;
10291

10392

104-
// deprecated fields
105-
106-
/** Use HTTP 1.0 expires header? */
107-
private boolean useExpiresHeader = false;
108-
109-
/** Use HTTP 1.1 cache-control header? */
110-
private boolean useCacheControlHeader = true;
111-
112-
/** Use HTTP 1.1 cache-control header value "no-store"? */
113-
private boolean useCacheControlNoStore = true;
114-
115-
private boolean alwaysMustRevalidate = false;
116-
117-
11893
/**
11994
* Create a new WebContentGenerator which supports
12095
* HTTP methods GET, HEAD and POST by default.
@@ -284,90 +259,6 @@ public final String[] getVaryByRequestHeaders() {
284259
return this.varyByRequestHeaders;
285260
}
286261

287-
/**
288-
* Set whether to use the HTTP 1.0 expires header. Default is "false",
289-
* as of 4.2.
290-
* <p>Note: Cache headers will only get applied if caching is enabled
291-
* (or explicitly prevented) for the current request.
292-
* @deprecated as of 4.2, since going forward, the HTTP 1.1 cache-control
293-
* header will be required, with the HTTP 1.0 headers disappearing
294-
*/
295-
@Deprecated
296-
public final void setUseExpiresHeader(boolean useExpiresHeader) {
297-
this.useExpiresHeader = useExpiresHeader;
298-
}
299-
300-
/**
301-
* Return whether the HTTP 1.0 expires header is used.
302-
* @deprecated as of 4.2, in favor of {@link #getCacheControl()}
303-
*/
304-
@Deprecated
305-
public final boolean isUseExpiresHeader() {
306-
return this.useExpiresHeader;
307-
}
308-
309-
/**
310-
* Set whether to use the HTTP 1.1 cache-control header. Default is "true".
311-
* <p>Note: Cache headers will only get applied if caching is enabled
312-
* (or explicitly prevented) for the current request.
313-
* @deprecated as of 4.2, since going forward, the HTTP 1.1 cache-control
314-
* header will be required, with the HTTP 1.0 headers disappearing
315-
*/
316-
@Deprecated
317-
public final void setUseCacheControlHeader(boolean useCacheControlHeader) {
318-
this.useCacheControlHeader = useCacheControlHeader;
319-
}
320-
321-
/**
322-
* Return whether the HTTP 1.1 cache-control header is used.
323-
* @deprecated as of 4.2, in favor of {@link #getCacheControl()}
324-
*/
325-
@Deprecated
326-
public final boolean isUseCacheControlHeader() {
327-
return this.useCacheControlHeader;
328-
}
329-
330-
/**
331-
* Set whether to use the HTTP 1.1 cache-control header value "no-store"
332-
* when preventing caching. Default is "true".
333-
* @deprecated as of 4.2, in favor of {@link #setCacheControl}
334-
*/
335-
@Deprecated
336-
public final void setUseCacheControlNoStore(boolean useCacheControlNoStore) {
337-
this.useCacheControlNoStore = useCacheControlNoStore;
338-
}
339-
340-
/**
341-
* Return whether the HTTP 1.1 cache-control header value "no-store" is used.
342-
* @deprecated as of 4.2, in favor of {@link #getCacheControl()}
343-
*/
344-
@Deprecated
345-
public final boolean isUseCacheControlNoStore() {
346-
return this.useCacheControlNoStore;
347-
}
348-
349-
/**
350-
* An option to add 'must-revalidate' to every Cache-Control header.
351-
* This may be useful with annotated controller methods, which can
352-
* programmatically do a last-modified calculation as described in
353-
* {@link org.springframework.web.context.request.WebRequest#checkNotModified(long)}.
354-
* <p>Default is "false".
355-
* @deprecated as of 4.2, in favor of {@link #setCacheControl}
356-
*/
357-
@Deprecated
358-
public final void setAlwaysMustRevalidate(boolean mustRevalidate) {
359-
this.alwaysMustRevalidate = mustRevalidate;
360-
}
361-
362-
/**
363-
* Return whether 'must-revalidate' is added to every Cache-Control header.
364-
* @deprecated as of 4.2, in favor of {@link #getCacheControl()}
365-
*/
366-
@Deprecated
367-
public final boolean isAlwaysMustRevalidate() {
368-
return this.alwaysMustRevalidate;
369-
}
370-
371262

372263
/**
373264
* Check the given request for supported methods and a required session, if any.
@@ -425,15 +316,6 @@ protected final void applyCacheControl(HttpServletResponse response, CacheContro
425316
if (ccValue != null) {
426317
// Set computed HTTP 1.1 Cache-Control header
427318
response.setHeader(HEADER_CACHE_CONTROL, ccValue);
428-
429-
if (response.containsHeader(HEADER_PRAGMA)) {
430-
// Reset HTTP 1.0 Pragma header if present
431-
response.setHeader(HEADER_PRAGMA, "");
432-
}
433-
if (response.containsHeader(HEADER_EXPIRES)) {
434-
// Reset HTTP 1.0 Expires header if present
435-
response.setHeader(HEADER_EXPIRES, "");
436-
}
437319
}
438320
}
439321

@@ -446,33 +328,18 @@ protected final void applyCacheControl(HttpServletResponse response, CacheContro
446328
* @param cacheSeconds positive number of seconds into the future that the
447329
* response should be cacheable for, 0 to prevent caching
448330
*/
449-
@SuppressWarnings("deprecation")
450331
protected final void applyCacheSeconds(HttpServletResponse response, int cacheSeconds) {
451-
if (this.useExpiresHeader || !this.useCacheControlHeader) {
452-
// Deprecated HTTP 1.0 cache behavior, as in previous Spring versions
453-
if (cacheSeconds > 0) {
454-
cacheForSeconds(response, cacheSeconds);
455-
}
456-
else if (cacheSeconds == 0) {
457-
preventCaching(response);
458-
}
332+
CacheControl cControl;
333+
if (cacheSeconds > 0) {
334+
cControl = CacheControl.maxAge(cacheSeconds, TimeUnit.SECONDS);
335+
}
336+
else if (cacheSeconds == 0) {
337+
cControl = CacheControl.noStore();
459338
}
460339
else {
461-
CacheControl cControl;
462-
if (cacheSeconds > 0) {
463-
cControl = CacheControl.maxAge(cacheSeconds, TimeUnit.SECONDS);
464-
if (this.alwaysMustRevalidate) {
465-
cControl = cControl.mustRevalidate();
466-
}
467-
}
468-
else if (cacheSeconds == 0) {
469-
cControl = (this.useCacheControlNoStore ? CacheControl.noStore() : CacheControl.noCache());
470-
}
471-
else {
472-
cControl = CacheControl.empty();
473-
}
474-
applyCacheControl(response, cControl);
340+
cControl = CacheControl.empty();
475341
}
342+
applyCacheControl(response, cControl);
476343
}
477344

478345

@@ -493,105 +360,6 @@ protected final void checkAndPrepare(
493360
applyCacheSeconds(response, cacheSeconds);
494361
}
495362

496-
/**
497-
* Apply the given cache seconds and generate respective HTTP headers.
498-
* <p>That is, allow caching for the given number of seconds in the
499-
* case of a positive value, prevent caching if given a 0 value, else
500-
* do nothing (i.e. leave caching to the client).
501-
* @param response the current HTTP response
502-
* @param cacheSeconds the (positive) number of seconds into the future
503-
* that the response should be cacheable for; 0 to prevent caching; and
504-
* a negative value to leave caching to the client.
505-
* @param mustRevalidate whether the client should revalidate the resource
506-
* (typically only necessary for controllers with last-modified support)
507-
* @deprecated as of 4.2, in favor of {@link #applyCacheControl}
508-
*/
509-
@Deprecated
510-
protected final void applyCacheSeconds(HttpServletResponse response, int cacheSeconds, boolean mustRevalidate) {
511-
if (cacheSeconds > 0) {
512-
cacheForSeconds(response, cacheSeconds, mustRevalidate);
513-
}
514-
else if (cacheSeconds == 0) {
515-
preventCaching(response);
516-
}
517-
}
518-
519-
/**
520-
* Set HTTP headers to allow caching for the given number of seconds.
521-
* Does not tell the browser to revalidate the resource.
522-
* @param response current HTTP response
523-
* @param seconds number of seconds into the future that the response
524-
* should be cacheable for
525-
* @deprecated as of 4.2, in favor of {@link #applyCacheControl}
526-
*/
527-
@Deprecated
528-
protected final void cacheForSeconds(HttpServletResponse response, int seconds) {
529-
cacheForSeconds(response, seconds, false);
530-
}
531-
532-
/**
533-
* Set HTTP headers to allow caching for the given number of seconds.
534-
* Tells the browser to revalidate the resource if mustRevalidate is
535-
* {@code true}.
536-
* @param response the current HTTP response
537-
* @param seconds number of seconds into the future that the response
538-
* should be cacheable for
539-
* @param mustRevalidate whether the client should revalidate the resource
540-
* (typically only necessary for controllers with last-modified support)
541-
* @deprecated as of 4.2, in favor of {@link #applyCacheControl}
542-
*/
543-
@Deprecated
544-
protected final void cacheForSeconds(HttpServletResponse response, int seconds, boolean mustRevalidate) {
545-
if (this.useExpiresHeader) {
546-
// HTTP 1.0 header
547-
response.setDateHeader(HEADER_EXPIRES, System.currentTimeMillis() + seconds * 1000L);
548-
}
549-
else if (response.containsHeader(HEADER_EXPIRES)) {
550-
// Reset HTTP 1.0 Expires header if present
551-
response.setHeader(HEADER_EXPIRES, "");
552-
}
553-
554-
if (this.useCacheControlHeader) {
555-
// HTTP 1.1 header
556-
String headerValue = "max-age=" + seconds;
557-
if (mustRevalidate || this.alwaysMustRevalidate) {
558-
headerValue += ", must-revalidate";
559-
}
560-
response.setHeader(HEADER_CACHE_CONTROL, headerValue);
561-
}
562-
563-
if (response.containsHeader(HEADER_PRAGMA)) {
564-
// Reset HTTP 1.0 Pragma header if present
565-
response.setHeader(HEADER_PRAGMA, "");
566-
}
567-
}
568-
569-
/**
570-
* Prevent the response from being cached.
571-
* Only called in HTTP 1.0 compatibility mode.
572-
* <p>See {@code https://www.mnot.net/cache_docs}.
573-
* @deprecated as of 4.2, in favor of {@link #applyCacheControl}
574-
*/
575-
@Deprecated
576-
protected final void preventCaching(HttpServletResponse response) {
577-
response.setHeader(HEADER_PRAGMA, "no-cache");
578-
579-
if (this.useExpiresHeader) {
580-
// HTTP 1.0 Expires header
581-
response.setDateHeader(HEADER_EXPIRES, 1L);
582-
}
583-
584-
if (this.useCacheControlHeader) {
585-
// HTTP 1.1 Cache-Control header: "no-cache" is the standard value,
586-
// "no-store" is necessary to prevent caching on Firefox.
587-
response.setHeader(HEADER_CACHE_CONTROL, "no-cache");
588-
if (this.useCacheControlNoStore) {
589-
response.addHeader(HEADER_CACHE_CONTROL, "no-store");
590-
}
591-
}
592-
}
593-
594-
595363
private Collection<String> getVaryRequestHeadersToAdd(HttpServletResponse response, String[] varyByRequestHeaders) {
596364
if (!response.containsHeader(HttpHeaders.VARY)) {
597365
return Arrays.asList(varyByRequestHeaders);

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/WebContentInterceptorTests.java

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -103,63 +103,6 @@ void emptyCacheConfiguration(Function<String, MockHttpServletRequest> requestFac
103103
assertThat(cacheControlHeaders).isEmpty();
104104
}
105105

106-
@PathPatternsParameterizedTest // SPR-13252, SPR-14053
107-
void cachingConfigAndPragmaHeader(Function<String, MockHttpServletRequest> requestFactory) throws Exception {
108-
response.setHeader("Pragma", "no-cache");
109-
response.setHeader("Expires", "0");
110-
111-
interceptor.setCacheSeconds(10);
112-
interceptor.preHandle(requestFactory.apply("/"), response, handler);
113-
114-
assertThat(response.getHeader("Pragma")).isEmpty();
115-
assertThat(response.getHeader("Expires")).isEmpty();
116-
}
117-
118-
@SuppressWarnings("deprecation")
119-
@PathPatternsParameterizedTest // SPR-13252, SPR-14053
120-
void http10CachingConfigAndPragmaHeader(Function<String, MockHttpServletRequest> requestFactory) throws Exception {
121-
response.setHeader("Pragma", "no-cache");
122-
response.setHeader("Expires", "0");
123-
124-
interceptor.setCacheSeconds(10);
125-
interceptor.setAlwaysMustRevalidate(true);
126-
interceptor.preHandle(requestFactory.apply("/"), response, handler);
127-
128-
assertThat(response.getHeader("Pragma")).isEmpty();
129-
assertThat(response.getHeader("Expires")).isEmpty();
130-
}
131-
132-
@SuppressWarnings("deprecation")
133-
@PathPatternsParameterizedTest
134-
void http10CachingConfigAndSpecificMapping(Function<String, MockHttpServletRequest> requestFactory) throws Exception {
135-
interceptor.setCacheSeconds(0);
136-
interceptor.setUseExpiresHeader(true);
137-
interceptor.setAlwaysMustRevalidate(true);
138-
Properties mappings = new Properties();
139-
mappings.setProperty("/*/*.cache.html", "10");
140-
interceptor.setCacheMappings(mappings);
141-
142-
MockHttpServletRequest request = requestFactory.apply("/foo/page.html");
143-
MockHttpServletResponse response = new MockHttpServletResponse();
144-
interceptor.preHandle(request, response, handler);
145-
146-
Iterable<String> expiresHeaders = response.getHeaders("Expires");
147-
assertThat(expiresHeaders).hasSize(1);
148-
Iterable<String> cacheControlHeaders = response.getHeaders("Cache-Control");
149-
assertThat(cacheControlHeaders).containsExactly("no-cache", "no-store");
150-
Iterable<String> pragmaHeaders = response.getHeaders("Pragma");
151-
assertThat(pragmaHeaders).containsExactly("no-cache");
152-
153-
request = requestFactory.apply("/foo/page.cache.html");
154-
response = new MockHttpServletResponse();
155-
interceptor.preHandle(request, response, handler);
156-
157-
expiresHeaders = response.getHeaders("Expires");
158-
assertThat(expiresHeaders).hasSize(1);
159-
cacheControlHeaders = response.getHeaders("Cache-Control");
160-
assertThat(cacheControlHeaders).containsExactly("max-age=10, must-revalidate");
161-
}
162-
163106
@Test
164107
void throwsExceptionWithNullPathMatcher() {
165108
assertThatIllegalArgumentException()

0 commit comments

Comments
 (0)