Skip to content

Commit b7753a1

Browse files
committed
Polish
1 parent e2bc90b commit b7753a1

File tree

5 files changed

+141
-168
lines changed

5 files changed

+141
-168
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java

Lines changed: 100 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,10 @@
1919
import java.time.Duration;
2020
import java.time.temporal.ChronoUnit;
2121
import java.util.concurrent.TimeUnit;
22-
import java.util.function.BiFunction;
23-
import java.util.function.Supplier;
24-
25-
import javax.annotation.PostConstruct;
2622

2723
import org.springframework.boot.context.properties.ConfigurationProperties;
28-
import org.springframework.boot.context.properties.NestedConfigurationProperty;
2924
import org.springframework.boot.context.properties.bind.convert.DurationUnit;
3025
import org.springframework.http.CacheControl;
31-
import org.springframework.util.Assert;
3226

3327
/**
3428
* Properties used to configure resource handling.
@@ -45,7 +39,7 @@ public class ResourceProperties {
4539

4640
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
4741
"classpath:/META-INF/resources/", "classpath:/resources/",
48-
"classpath:/static/", "classpath:/public/"};
42+
"classpath:/static/", "classpath:/public/" };
4943

5044
/**
5145
* Locations of static resources. Defaults to classpath:[/META-INF/resources/,
@@ -54,17 +48,18 @@ public class ResourceProperties {
5448
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
5549

5650
/**
57-
* Cache period for the resources served by the resource handler. If a duration suffix
58-
* is not specified, seconds will be used.
51+
* Cache period for the resources served by the resource handler.
52+
* If a duration suffix is not specified, seconds will be used.
53+
* Can be overridden by the {@code cache-control} property.
5954
*/
6055
@DurationUnit(ChronoUnit.SECONDS)
6156
private Duration cachePeriod;
6257

6358
/**
64-
* Cache control headers. Either {@link #cachePeriod} or {@link #cacheControl} can be set.
59+
* Cache control HTTP headers, only allows valid directive combinations.
60+
* Overrides the {@code cache-period} property.
6561
*/
66-
@NestedConfigurationProperty
67-
private CacheControlProperties cacheControl;
62+
private CacheControlProperties cacheControl = new CacheControlProperties();
6863

6964
/**
7065
* Enable default resource handling.
@@ -106,7 +101,6 @@ public void setCacheControl(CacheControlProperties cacheControl) {
106101
this.cacheControl = cacheControl;
107102
}
108103

109-
110104
public boolean isAddMappings() {
111105
return this.addMappings;
112106
}
@@ -119,36 +113,6 @@ public Chain getChain() {
119113
return this.chain;
120114
}
121115

122-
public CacheControl createCacheControl() {
123-
if (this.cachePeriod != null) {
124-
return CacheControl.maxAge(this.cachePeriod.getSeconds(), TimeUnit.SECONDS);
125-
}
126-
if (this.cacheControl != null) {
127-
return this.cacheControl.transformToHttpSpringCacheControl();
128-
}
129-
return null;
130-
}
131-
132-
@PostConstruct
133-
public void checkIncompatibleCacheOptions() {
134-
Assert.state(this.cachePeriod == null || this.cacheControl == null,
135-
"Only one of cache-period or cache-control may be set.");
136-
if (this.cacheControl != null) {
137-
if (this.cacheControl.getMaxAge() != null) {
138-
Assert.state(!Boolean.TRUE.equals(this.cacheControl.getNoCache()), "no-cache may not be set if max-age is set.");
139-
Assert.state(!Boolean.TRUE.equals(this.cacheControl.getNoStore()), "no-store may not be set if max-age is set.");
140-
}
141-
if (this.cacheControl.getNoCache() != null) {
142-
Assert.state(this.cacheControl.getMaxAge() == null, "max-age may not be set if no-cache is set.");
143-
Assert.state(!Boolean.TRUE.equals(this.cacheControl.getNoStore()), "no-store may not be set if no-cache is set.");
144-
}
145-
if (this.cacheControl.getNoStore() != null) {
146-
Assert.state(this.cacheControl.getMaxAge() == null, "max-age may not be set if no-store is set.");
147-
Assert.state(!Boolean.TRUE.equals(this.cacheControl.getNoCache()), "no-cache may not be set if no-store is set.");
148-
}
149-
}
150-
}
151-
152116
/**
153117
* Configuration for the Spring Resource Handling chain.
154118
*/
@@ -328,38 +292,83 @@ public void setVersion(String version) {
328292
}
329293

330294
/**
331-
* Configuration for the Cache Control header.
295+
* Configuration for the Cache Control HTTP header.
332296
*/
333-
334297
public static class CacheControlProperties {
335298

336-
private Long maxAge;
299+
/**
300+
* Maximum time the response should be cached,
301+
* in seconds if no duration suffix is not specified.
302+
*/
303+
@DurationUnit(ChronoUnit.SECONDS)
304+
private Duration maxAge;
337305

306+
/**
307+
* Indicate that the cached response can be reused only
308+
* if re-validated with the server.
309+
*/
338310
private Boolean noCache;
339311

312+
/**
313+
* Indicate to not cache the response in any case.
314+
*/
340315
private Boolean noStore;
341316

317+
/**
318+
* Indicate that once it has become stale, a cache must not use
319+
* the response without re-validating it with the server.
320+
*/
342321
private Boolean mustRevalidate;
343322

323+
/**
324+
* Indicate intermediaries (caches and others) that they should
325+
* not transform the response content.
326+
*/
344327
private Boolean noTransform;
345328

329+
/**
330+
* Indicate that any cache may store the response.
331+
*/
346332
private Boolean cachePublic;
347333

334+
/**
335+
* Indicate that the response message is intended for a single user
336+
* and must not be stored by a shared cache.
337+
*/
348338
private Boolean cachePrivate;
349339

340+
/**
341+
* Same meaning as the "must-revalidate" directive,
342+
* except that it does not apply to private caches.
343+
*/
350344
private Boolean proxyRevalidate;
351345

352-
private Long staleWhileRevalidate;
346+
/**
347+
* Maximum time the response can be served after it becomes stale,
348+
* in seconds if no duration suffix is not specified.
349+
*/
350+
@DurationUnit(ChronoUnit.SECONDS)
351+
private Duration staleWhileRevalidate;
353352

354-
private Long staleIfError;
353+
/**
354+
* Maximum time the response may be used when errors are encountered,
355+
* in seconds if no duration suffix is not specified.
356+
*/
357+
@DurationUnit(ChronoUnit.SECONDS)
358+
private Duration staleIfError;
355359

356-
private Long sMaxAge;
360+
/**
361+
* Maximum time the response should be cached by shared caches,
362+
* in seconds if no duration suffix is not specified.
363+
*/
364+
@DurationUnit(ChronoUnit.SECONDS)
365+
private Duration sMaxAge;
357366

358-
public Long getMaxAge() {
367+
public Duration getMaxAge() {
359368
return this.maxAge;
360369
}
361370

362-
public void setMaxAge(Long maxAge) {
371+
public void setMaxAge(Duration maxAge) {
363372
this.maxAge = maxAge;
364373
}
365374

@@ -419,91 +428,71 @@ public void setProxyRevalidate(Boolean proxyRevalidate) {
419428
this.proxyRevalidate = proxyRevalidate;
420429
}
421430

422-
public Long getStaleWhileRevalidate() {
431+
public Duration getStaleWhileRevalidate() {
423432
return this.staleWhileRevalidate;
424433
}
425434

426-
public void setStaleWhileRevalidate(Long staleWhileRevalidate) {
435+
public void setStaleWhileRevalidate(Duration staleWhileRevalidate) {
427436
this.staleWhileRevalidate = staleWhileRevalidate;
428437
}
429438

430-
public Long getStaleIfError() {
439+
public Duration getStaleIfError() {
431440
return this.staleIfError;
432441
}
433442

434-
public void setStaleIfError(Long staleIfError) {
443+
public void setStaleIfError(Duration staleIfError) {
435444
this.staleIfError = staleIfError;
436445
}
437446

438-
public Long getsMaxAge() {
447+
public Duration getsMaxAge() {
439448
return this.sMaxAge;
440449
}
441450

442-
public void setsMaxAge(Long sMaxAge) {
451+
public void setsMaxAge(Duration sMaxAge) {
443452
this.sMaxAge = sMaxAge;
444453
}
445454

446-
CacheControl transformToHttpSpringCacheControl() {
447-
CacheControl httpSpringCacheControl = initCacheControl();
448-
httpSpringCacheControl = setFlags(httpSpringCacheControl);
449-
httpSpringCacheControl = setTimes(httpSpringCacheControl);
450-
return httpSpringCacheControl;
451-
}
452-
453-
private CacheControl initCacheControl() {
454-
if (this.maxAge != null) {
455-
return CacheControl.maxAge(this.maxAge, TimeUnit.SECONDS);
455+
public CacheControl toHttpCacheControl() {
456+
CacheControl cc;
457+
if (Boolean.TRUE.equals(this.noStore)) {
458+
cc = CacheControl.noStore();
456459
}
457-
if (Boolean.TRUE.equals(this.noCache)) {
458-
return CacheControl.noCache();
460+
else if (Boolean.TRUE.equals(this.noCache)) {
461+
cc = CacheControl.noCache();
459462
}
460-
if (Boolean.TRUE.equals(this.noStore)) {
461-
return CacheControl.noStore();
463+
else if (this.maxAge != null) {
464+
cc = CacheControl.maxAge(this.maxAge.getSeconds(), TimeUnit.SECONDS);
462465
}
463-
return CacheControl.empty();
464-
}
465-
466-
private CacheControl setFlags(CacheControl cacheControl) {
467-
cacheControl = setBoolean(this.mustRevalidate, cacheControl::mustRevalidate,
468-
cacheControl);
469-
cacheControl = setBoolean(this.noTransform, cacheControl::noTransform,
470-
cacheControl);
471-
cacheControl = setBoolean(this.cachePublic, cacheControl::cachePublic,
472-
cacheControl);
473-
cacheControl = setBoolean(this.cachePrivate, cacheControl::cachePrivate,
474-
cacheControl);
475-
cacheControl = setBoolean(this.proxyRevalidate, cacheControl::proxyRevalidate,
476-
cacheControl);
477-
return cacheControl;
478-
}
479-
480-
private static CacheControl setBoolean(Boolean value,
481-
Supplier<CacheControl> setter, CacheControl cacheControl) {
482-
if (Boolean.TRUE.equals(value)) {
483-
return setter.get();
466+
else {
467+
cc = CacheControl.empty();
484468
}
485-
return cacheControl;
486-
}
487-
488-
private CacheControl setTimes(CacheControl cacheControl) {
489-
cacheControl = setLong(this.staleWhileRevalidate,
490-
cacheControl::staleWhileRevalidate, cacheControl);
491-
cacheControl = setLong(this.staleIfError, cacheControl::staleIfError,
492-
cacheControl);
493-
cacheControl = setLong(this.sMaxAge, cacheControl::sMaxAge, cacheControl);
494-
return cacheControl;
495-
}
496-
497-
private static CacheControl setLong(Long value,
498-
BiFunction<Long, TimeUnit, CacheControl> setter,
499-
CacheControl cacheControl) {
500-
if (value != null) {
501-
return setter.apply(value, TimeUnit.SECONDS);
469+
if (Boolean.TRUE.equals(this.mustRevalidate)) {
470+
cc.mustRevalidate();
471+
}
472+
if (Boolean.TRUE.equals(this.noTransform)) {
473+
cc.noTransform();
474+
}
475+
if (Boolean.TRUE.equals(this.cachePublic)) {
476+
cc.cachePublic();
502477
}
503-
return cacheControl;
478+
if (Boolean.TRUE.equals(this.cachePrivate)) {
479+
cc.cachePrivate();
480+
}
481+
if (Boolean.TRUE.equals(this.proxyRevalidate)) {
482+
cc.proxyRevalidate();
483+
}
484+
if (this.staleWhileRevalidate != null) {
485+
cc.staleWhileRevalidate(this.staleWhileRevalidate.getSeconds(), TimeUnit.SECONDS);
486+
}
487+
if (this.staleIfError != null) {
488+
cc.staleIfError(this.staleIfError.getSeconds(), TimeUnit.SECONDS);
489+
}
490+
if (this.sMaxAge != null) {
491+
cc.sMaxAge(this.sMaxAge.getSeconds(), TimeUnit.SECONDS);
492+
}
493+
return cc;
504494
}
505495

506496
}
507497

508-
509498
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,23 +309,23 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
309309
return;
310310
}
311311
Duration cachePeriod = this.resourceProperties.getCachePeriod();
312-
CacheControl cacheControl = this.resourceProperties.createCacheControl();
312+
CacheControl cacheControl = this.resourceProperties.getCacheControl().toHttpCacheControl();
313313
if (!registry.hasMappingForPattern("/webjars/**")) {
314314
customizeResourceHandlerRegistration(
315315
registry.addResourceHandler("/webjars/**")
316316
.addResourceLocations(
317317
"classpath:/META-INF/resources/webjars/")
318-
.setCachePeriod(getSeconds(cachePeriod))
319-
.setCacheControl(cacheControl));
318+
.setCachePeriod(getSeconds(cachePeriod))
319+
.setCacheControl(cacheControl));
320320
}
321321
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
322322
if (!registry.hasMappingForPattern(staticPathPattern)) {
323323
customizeResourceHandlerRegistration(
324324
registry.addResourceHandler(staticPathPattern)
325325
.addResourceLocations(getResourceLocations(
326326
this.resourceProperties.getStaticLocations()))
327-
.setCachePeriod(getSeconds(cachePeriod))
328-
.setCacheControl(cacheControl));
327+
.setCachePeriod(getSeconds(cachePeriod))
328+
.setCacheControl(cacheControl));
329329
}
330330
}
331331

0 commit comments

Comments
 (0)