Skip to content

Commit 4ac4c1b

Browse files
committed
Revert "Merge pull request #33366 from kashike"
This reverts commit a1ec766, reversing changes made to e27192e. See gh-33366
1 parent bb4a96f commit 4ac4c1b

File tree

1 file changed

+76
-102
lines changed

1 file changed

+76
-102
lines changed

spring-web/src/main/java/org/springframework/http/CacheControl.java

Lines changed: 76 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -23,8 +23,7 @@
2323
import org.springframework.util.StringUtils;
2424

2525
/**
26-
* A builder for creating "Cache-Control" HTTP response headers. As of Spring
27-
* Framework 6.2, this class is immutable.
26+
* A builder for creating "Cache-Control" HTTP response headers.
2827
*
2928
* <p>Adding Cache-Control directives to HTTP responses can significantly improve the client
3029
* experience when interacting with a web application. This builder creates opinionated
@@ -52,74 +51,55 @@
5251
*/
5352
public class CacheControl {
5453

55-
private static final CacheControl EMPTY = new CacheControl();
56-
5754
@Nullable
58-
private final Duration maxAge;
55+
private Duration maxAge;
5956

60-
private final boolean noCache;
57+
private boolean noCache = false;
6158

62-
private final boolean noStore;
59+
private boolean noStore = false;
6360

64-
private final boolean mustRevalidate;
61+
private boolean mustRevalidate = false;
6562

66-
private final boolean noTransform;
63+
private boolean noTransform = false;
6764

68-
private final boolean cachePublic;
65+
private boolean cachePublic = false;
6966

70-
private final boolean cachePrivate;
67+
private boolean cachePrivate = false;
7168

72-
private final boolean proxyRevalidate;
69+
private boolean proxyRevalidate = false;
7370

7471
@Nullable
75-
private final Duration staleWhileRevalidate;
72+
private Duration staleWhileRevalidate;
7673

7774
@Nullable
78-
private final Duration staleIfError;
75+
private Duration staleIfError;
7976

8077
@Nullable
81-
private final Duration sMaxAge;
78+
private Duration sMaxAge;
79+
80+
private boolean immutable = false;
8281

83-
private final boolean immutable;
8482

8583
/**
8684
* Create an empty CacheControl instance.
8785
* @see #empty()
8886
*/
8987
protected CacheControl() {
90-
this(null, false, false, false, false, false, false, false, null, null, null, false);
9188
}
9289

93-
private CacheControl(@Nullable Duration maxAge, boolean noCache, boolean noStore,
94-
boolean mustRevalidate, boolean noTransform, boolean cachePublic,
95-
boolean cachePrivate, boolean proxyRevalidate, @Nullable Duration staleWhileRevalidate,
96-
@Nullable Duration staleIfError, @Nullable Duration sMaxAge, boolean immutable) {
97-
this.maxAge = maxAge;
98-
this.noCache = noCache;
99-
this.noStore = noStore;
100-
this.mustRevalidate = mustRevalidate;
101-
this.noTransform = noTransform;
102-
this.cachePublic = cachePublic;
103-
this.cachePrivate = cachePrivate;
104-
this.proxyRevalidate = proxyRevalidate;
105-
this.staleWhileRevalidate = staleWhileRevalidate;
106-
this.staleIfError = staleIfError;
107-
this.sMaxAge = sMaxAge;
108-
this.immutable = immutable;
109-
}
11090

11191
/**
11292
* Return an empty directive.
11393
* <p>This is well suited for using other optional directives without "max-age",
11494
* "no-cache" or "no-store".
115-
* @return an empty directive
95+
* @return {@code this}, to facilitate method chaining
11696
*/
11797
public static CacheControl empty() {
118-
return EMPTY;
98+
return new CacheControl();
11999
}
120100

121101
/**
122-
* Return a "max-age=" directive.
102+
* Add a "max-age=" directive.
123103
* <p>This directive is well suited for publicly caching resources, knowing that
124104
* they won't change within the configured amount of time. Additional directives
125105
* can be also used, in case resources shouldn't be cached ({@link #cachePrivate()})
@@ -129,7 +109,7 @@ public static CacheControl empty() {
129109
* directive should be set ({@link #mustRevalidate()}
130110
* @param maxAge the maximum time the response should be cached
131111
* @param unit the time unit of the {@code maxAge} argument
132-
* @return a CacheControl instance with a "max-age" directive
112+
* @return {@code this}, to facilitate method chaining
133113
* @see #maxAge(Duration)
134114
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.8">rfc7234 section 5.2.2.8</a>
135115
*/
@@ -138,7 +118,7 @@ public static CacheControl maxAge(long maxAge, TimeUnit unit) {
138118
}
139119

140120
/**
141-
* Return a "max-age=" directive.
121+
* Add a "max-age=" directive.
142122
* <p>This directive is well suited for publicly caching resources, knowing that
143123
* they won't change within the configured amount of time. Additional directives
144124
* can be also used, in case resources shouldn't be cached ({@link #cachePrivate()})
@@ -147,120 +127,118 @@ public static CacheControl maxAge(long maxAge, TimeUnit unit) {
147127
* become stale (i.e. the "max-age" delay is passed), the "must-revalidate"
148128
* directive should be set ({@link #mustRevalidate()}
149129
* @param maxAge the maximum time the response should be cached
150-
* @return a CacheControl instance with a "max-age" directive
130+
* @return {@code this}, to facilitate method chaining
151131
* @since 5.2
152132
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.8">rfc7234 section 5.2.2.8</a>
153133
*/
154134
public static CacheControl maxAge(Duration maxAge) {
155-
return new CacheControl(maxAge, false, false, false, false, false, false, false,
156-
null, null, null, false);
135+
CacheControl cc = new CacheControl();
136+
cc.maxAge = maxAge;
137+
return cc;
157138
}
158139

159140
/**
160-
* Return a "no-cache" directive.
141+
* Add a "no-cache" directive.
161142
* <p>This directive is well suited for telling caches that the response
162143
* can be reused only if the client revalidates it with the server.
163144
* This directive won't disable cache altogether and may result with clients
164145
* sending conditional requests (with "ETag", "If-Modified-Since" headers)
165146
* and the server responding with "304 - Not Modified" status.
166147
* <p>In order to disable caching and minimize requests/responses exchanges,
167148
* the {@link #noStore()} directive should be used instead of {@code #noCache()}.
168-
* @return a CacheControl instance with a "no-cache" directive
149+
* @return {@code this}, to facilitate method chaining
169150
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.2">rfc7234 section 5.2.2.2</a>
170151
*/
171152
public static CacheControl noCache() {
172-
return new CacheControl(null, true, false, false, false, false, false, false,
173-
null, null, null, false);
153+
CacheControl cc = new CacheControl();
154+
cc.noCache = true;
155+
return cc;
174156
}
175157

176158
/**
177-
* Return a "no-store" directive.
159+
* Add a "no-store" directive.
178160
* <p>This directive is well suited for preventing caches (browsers and proxies)
179161
* to cache the content of responses.
180-
* @return a CacheControl instance with a "no-store" directive
162+
* @return {@code this}, to facilitate method chaining
181163
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.3">rfc7234 section 5.2.2.3</a>
182164
*/
183165
public static CacheControl noStore() {
184-
return new CacheControl(null, false, true, false, false, false, false, false,
185-
null, null, null, false);
166+
CacheControl cc = new CacheControl();
167+
cc.noStore = true;
168+
return cc;
186169
}
187170

188171

189172
/**
190-
* Return a new instance with an additional "must-revalidate" directive.
173+
* Add a "must-revalidate" directive.
191174
* <p>This directive indicates that once it has become stale, a cache MUST NOT
192175
* use the response to satisfy subsequent requests without successful validation
193176
* on the origin server.
194-
* @return a new CacheControl instance with an additional "must-revalidate" directive
177+
* @return {@code this}, to facilitate method chaining
195178
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.1">rfc7234 section 5.2.2.1</a>
196179
*/
197180
public CacheControl mustRevalidate() {
198-
return new CacheControl(this.maxAge, this.noCache, this.noStore, true, this.noTransform,
199-
this.cachePublic, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate,
200-
this.staleIfError, this.sMaxAge, this.immutable);
181+
this.mustRevalidate = true;
182+
return this;
201183
}
202184

203185
/**
204-
* Return a new instance with an additional "no-transform" directive.
186+
* Add a "no-transform" directive.
205187
* <p>This directive indicates that intermediaries (caches and others) should
206188
* not transform the response content. This can be useful to force caches and
207189
* CDNs not to automatically gzip or optimize the response content.
208-
* @return a new CacheControl instance with an additional "no-transform" directive
190+
* @return {@code this}, to facilitate method chaining
209191
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.4">rfc7234 section 5.2.2.4</a>
210192
*/
211193
public CacheControl noTransform() {
212-
return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, true,
213-
this.cachePublic, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate,
214-
this.staleIfError, this.sMaxAge, this.immutable);
194+
this.noTransform = true;
195+
return this;
215196
}
216197

217198
/**
218-
* Return a new instance with an additional "public" directive.
199+
* Add a "public" directive.
219200
* <p>This directive indicates that any cache MAY store the response,
220201
* even if the response would normally be non-cacheable or cacheable
221202
* only within a private cache.
222-
* @return a new CacheControl instance with an additional "public" directive
203+
* @return {@code this}, to facilitate method chaining
223204
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.5">rfc7234 section 5.2.2.5</a>
224205
*/
225206
public CacheControl cachePublic() {
226-
return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform,
227-
true, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate,
228-
this.staleIfError, this.sMaxAge, this.immutable);
207+
this.cachePublic = true;
208+
return this;
229209
}
230210

231211
/**
232-
* Return a new instance with an additional "private" directive.
212+
* Add a "private" directive.
233213
* <p>This directive indicates that the response message is intended
234214
* for a single user and MUST NOT be stored by a shared cache.
235-
* @return a new CacheControl instance with an additional "private" directive
215+
* @return {@code this}, to facilitate method chaining
236216
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.6">rfc7234 section 5.2.2.6</a>
237217
*/
238218
public CacheControl cachePrivate() {
239-
return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform,
240-
this.cachePublic, true, this.proxyRevalidate, this.staleWhileRevalidate,
241-
this.staleIfError, this.sMaxAge, this.immutable);
219+
this.cachePrivate = true;
220+
return this;
242221
}
243222

244223
/**
245-
* Return a new instance with an additional "proxy-revalidate" directive.
224+
* Add a "proxy-revalidate" directive.
246225
* <p>This directive has the same meaning as the "must-revalidate" directive,
247226
* except that it does not apply to private caches (i.e. browsers, HTTP clients).
248-
* @return a new CacheControl instance with an additional "proxy-revalidate" directive
227+
* @return {@code this}, to facilitate method chaining
249228
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.7">rfc7234 section 5.2.2.7</a>
250229
*/
251230
public CacheControl proxyRevalidate() {
252-
return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform,
253-
this.cachePublic, this.cachePrivate, true, this.staleWhileRevalidate,
254-
this.staleIfError, this.sMaxAge, this.immutable);
231+
this.proxyRevalidate = true;
232+
return this;
255233
}
256234

257235
/**
258-
* Return a new instance with an additional "s-maxage" directive.
236+
* Add an "s-maxage" directive.
259237
* <p>This directive indicates that, in shared caches, the maximum age specified
260238
* by this directive overrides the maximum age specified by other directives.
261239
* @param sMaxAge the maximum time the response should be cached
262240
* @param unit the time unit of the {@code sMaxAge} argument
263-
* @return a new CacheControl instance with an additional "s-maxage" directive
241+
* @return {@code this}, to facilitate method chaining
264242
* @see #sMaxAge(Duration)
265243
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.9">rfc7234 section 5.2.2.9</a>
266244
*/
@@ -269,30 +247,29 @@ public CacheControl sMaxAge(long sMaxAge, TimeUnit unit) {
269247
}
270248

271249
/**
272-
* Return a new instance with an additional "s-maxage" directive.
250+
* Add an "s-maxage" directive.
273251
* <p>This directive indicates that, in shared caches, the maximum age specified
274252
* by this directive overrides the maximum age specified by other directives.
275253
* @param sMaxAge the maximum time the response should be cached
276-
* @return a new CacheControl instance with an additional "s-maxage" directive
254+
* @return {@code this}, to facilitate method chaining
277255
* @since 5.2
278256
* @see <a href="https://tools.ietf.org/html/rfc7234#section-5.2.2.9">rfc7234 section 5.2.2.9</a>
279257
*/
280258
public CacheControl sMaxAge(Duration sMaxAge) {
281-
return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform,
282-
this.cachePublic, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate,
283-
this.staleIfError, sMaxAge, this.immutable);
259+
this.sMaxAge = sMaxAge;
260+
return this;
284261
}
285262

286263
/**
287-
* Return a new instance with an additional "stale-while-revalidate" directive.
264+
* Add a "stale-while-revalidate" directive.
288265
* <p>This directive indicates that caches MAY serve the response in which it
289266
* appears after it becomes stale, up to the indicated number of seconds.
290267
* If a cached response is served stale due to the presence of this extension,
291268
* the cache SHOULD attempt to revalidate it while still serving stale responses
292269
* (i.e. without blocking).
293270
* @param staleWhileRevalidate the maximum time the response should be used while being revalidated
294271
* @param unit the time unit of the {@code staleWhileRevalidate} argument
295-
* @return a new CacheControl instance with an additional "stale-while-revalidate" directive
272+
* @return {@code this}, to facilitate method chaining
296273
* @see #staleWhileRevalidate(Duration)
297274
* @see <a href="https://tools.ietf.org/html/rfc5861#section-3">rfc5861 section 3</a>
298275
*/
@@ -301,30 +278,29 @@ public CacheControl staleWhileRevalidate(long staleWhileRevalidate, TimeUnit uni
301278
}
302279

303280
/**
304-
* Return a new instance with an additional "stale-while-revalidate" directive.
281+
* Add a "stale-while-revalidate" directive.
305282
* <p>This directive indicates that caches MAY serve the response in which it
306283
* appears after it becomes stale, up to the indicated number of seconds.
307284
* If a cached response is served stale due to the presence of this extension,
308285
* the cache SHOULD attempt to revalidate it while still serving stale responses
309286
* (i.e. without blocking).
310287
* @param staleWhileRevalidate the maximum time the response should be used while being revalidated
311-
* @return a new CacheControl instance with an additional "stale-while-revalidate" directive
288+
* @return {@code this}, to facilitate method chaining
312289
* @since 5.2
313290
* @see <a href="https://tools.ietf.org/html/rfc5861#section-3">rfc5861 section 3</a>
314291
*/
315292
public CacheControl staleWhileRevalidate(Duration staleWhileRevalidate) {
316-
return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform,
317-
this.cachePublic, this.cachePrivate, this.proxyRevalidate, staleWhileRevalidate,
318-
this.staleIfError, this.sMaxAge, this.immutable);
293+
this.staleWhileRevalidate = staleWhileRevalidate;
294+
return this;
319295
}
320296

321297
/**
322-
* Return a new instance with an additional "stale-if-error" directive.
298+
* Add a "stale-if-error" directive.
323299
* <p>This directive indicates that when an error is encountered, a cached stale response
324300
* MAY be used to satisfy the request, regardless of other freshness information.
325301
* @param staleIfError the maximum time the response should be used when errors are encountered
326302
* @param unit the time unit of the {@code staleIfError} argument
327-
* @return a new CacheControl instance with an additional "stale-if-error" directive
303+
* @return {@code this}, to facilitate method chaining
328304
* @see #staleIfError(Duration)
329305
* @see <a href="https://tools.ietf.org/html/rfc5861#section-4">rfc5861 section 4</a>
330306
*/
@@ -333,34 +309,32 @@ public CacheControl staleIfError(long staleIfError, TimeUnit unit) {
333309
}
334310

335311
/**
336-
* Return a new instance with an additional "stale-if-error" directive.
312+
* Add a "stale-if-error" directive.
337313
* <p>This directive indicates that when an error is encountered, a cached stale response
338314
* MAY be used to satisfy the request, regardless of other freshness information.
339315
* @param staleIfError the maximum time the response should be used when errors are encountered
340-
* @return a new CacheControl instance with an additional "stale-if-error" directive
316+
* @return {@code this}, to facilitate method chaining
341317
* @since 5.2
342318
* @see <a href="https://tools.ietf.org/html/rfc5861#section-4">rfc5861 section 4</a>
343319
*/
344320
public CacheControl staleIfError(Duration staleIfError) {
345-
return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform,
346-
this.cachePublic, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate,
347-
staleIfError, this.sMaxAge, this.immutable);
321+
this.staleIfError = staleIfError;
322+
return this;
348323
}
349324

350325
/**
351-
* Return a new instance with an additional "immutable" directive.
326+
* Add an "immutable" directive.
352327
* <p>This directive indicates that the origin server will not update the
353328
* representation of that resource during the freshness lifetime of the response.
354329
* Adding a {@link #maxAge(Duration) max-age} directive is strongly advised
355330
* to enforce the actual freshness lifetime.
356-
* @return a new CacheControl instance with an additional "immutable" directive
331+
* @return {@code this}, to facilitate method chaining
357332
* @since 6.0.5
358333
* @see <a href="https://tools.ietf.org/html/rfc8246">rfc8246</a>
359334
*/
360335
public CacheControl immutable() {
361-
return new CacheControl(this.maxAge, this.noCache, this.noStore, this.mustRevalidate, this.noTransform,
362-
this.cachePublic, this.cachePrivate, this.proxyRevalidate, this.staleWhileRevalidate,
363-
this.staleIfError, this.sMaxAge, true);
336+
this.immutable = true;
337+
return this;
364338
}
365339

366340
/**

0 commit comments

Comments
 (0)