Skip to content

Commit e01c92a

Browse files
committed
Polish
1 parent 64c5e8b commit e01c92a

File tree

5 files changed

+110
-109
lines changed

5 files changed

+110
-109
lines changed

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

Lines changed: 58 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.time.Duration;
2020
import java.time.temporal.ChronoUnit;
2121
import java.util.concurrent.TimeUnit;
22+
import java.util.function.Consumer;
2223

2324
import org.springframework.boot.context.properties.ConfigurationProperties;
2425
import org.springframework.boot.context.properties.bind.convert.DefaultDurationUnit;
@@ -48,16 +49,16 @@ public class ResourceProperties {
4849
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
4950

5051
/**
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.
52+
* Cache period for the resources served by the resource handler. If a duration suffix
53+
* is not specified, seconds will be used. Can be overridden by the 'cache-control'
54+
* property.
5455
*/
5556
@DefaultDurationUnit(ChronoUnit.SECONDS)
5657
private Duration cachePeriod;
5758

5859
/**
59-
* Cache control HTTP headers, only allows valid directive combinations.
60-
* Overrides the {@code cache-period} property.
60+
* Cache control HTTP headers, only allows valid directive combinations. Overrides the
61+
* 'cache-period' property.
6162
*/
6263
private CacheControlProperties cacheControl = new CacheControlProperties();
6364

@@ -145,9 +146,8 @@ public static class Chain {
145146
/**
146147
* Return whether the resource chain is enabled. Return {@code null} if no
147148
* specific settings are present.
148-
*
149-
* @return whether the resource chain is enabled or {@code null} if no specified settings are
150-
* present.
149+
* @return whether the resource chain is enabled or {@code null} if no specified
150+
* settings are present.
151151
*/
152152
public Boolean getEnabled() {
153153
return getEnabled(getStrategy().getFixed().isEnabled(),
@@ -225,7 +225,7 @@ public static class Content {
225225
/**
226226
* Comma-separated list of patterns to apply to the Version Strategy.
227227
*/
228-
private String[] paths = new String[]{"/**"};
228+
private String[] paths = new String[] { "/**" };
229229

230230
public boolean isEnabled() {
231231
return this.enabled;
@@ -258,7 +258,7 @@ public static class Fixed {
258258
/**
259259
* Comma-separated list of patterns to apply to the Version Strategy.
260260
*/
261-
private String[] paths = new String[]{"/**"};
261+
private String[] paths = new String[] { "/**" };
262262

263263
/**
264264
* Version string to use for the Version Strategy.
@@ -297,15 +297,15 @@ public void setVersion(String version) {
297297
public static class CacheControlProperties {
298298

299299
/**
300-
* Maximum time the response should be cached,
301-
* in seconds if no duration suffix is not specified.
300+
* Maximum time the response should be cached, in seconds if no duration suffix is
301+
* not specified.
302302
*/
303303
@DefaultDurationUnit(ChronoUnit.SECONDS)
304304
private Duration maxAge;
305305

306306
/**
307-
* Indicate that the cached response can be reused only
308-
* if re-validated with the server.
307+
* Indicate that the cached response can be reused only if re-validated with the
308+
* server.
309309
*/
310310
private Boolean noCache;
311311

@@ -315,14 +315,14 @@ public static class CacheControlProperties {
315315
private Boolean noStore;
316316

317317
/**
318-
* Indicate that once it has become stale, a cache must not use
319-
* the response without re-validating it with the server.
318+
* Indicate that once it has become stale, a cache must not use the response
319+
* without re-validating it with the server.
320320
*/
321321
private Boolean mustRevalidate;
322322

323323
/**
324-
* Indicate intermediaries (caches and others) that they should
325-
* not transform the response content.
324+
* Indicate intermediaries (caches and others) that they should not transform the
325+
* response content.
326326
*/
327327
private Boolean noTransform;
328328

@@ -332,34 +332,34 @@ public static class CacheControlProperties {
332332
private Boolean cachePublic;
333333

334334
/**
335-
* Indicate that the response message is intended for a single user
336-
* and must not be stored by a shared cache.
335+
* Indicate that the response message is intended for a single user and must not
336+
* be stored by a shared cache.
337337
*/
338338
private Boolean cachePrivate;
339339

340340
/**
341-
* Same meaning as the "must-revalidate" directive,
342-
* except that it does not apply to private caches.
341+
* Same meaning as the "must-revalidate" directive, except that it does not apply
342+
* to private caches.
343343
*/
344344
private Boolean proxyRevalidate;
345345

346346
/**
347-
* Maximum time the response can be served after it becomes stale,
348-
* in seconds if no duration suffix is not specified.
347+
* Maximum time the response can be served after it becomes stale, in seconds if
348+
* no duration suffix is not specified.
349349
*/
350350
@DefaultDurationUnit(ChronoUnit.SECONDS)
351351
private Duration staleWhileRevalidate;
352352

353353
/**
354-
* Maximum time the response may be used when errors are encountered,
355-
* in seconds if no duration suffix is not specified.
354+
* Maximum time the response may be used when errors are encountered, in seconds
355+
* if no duration suffix is not specified.
356356
*/
357357
@DefaultDurationUnit(ChronoUnit.SECONDS)
358358
private Duration staleIfError;
359359

360360
/**
361-
* Maximum time the response should be cached by shared caches,
362-
* in seconds if no duration suffix is not specified.
361+
* Maximum time the response should be cached by shared caches, in seconds if no
362+
* duration suffix is not specified.
363363
*/
364364
@DefaultDurationUnit(ChronoUnit.SECONDS)
365365
private Duration sMaxAge;
@@ -453,44 +453,43 @@ public void setsMaxAge(Duration sMaxAge) {
453453
}
454454

455455
public CacheControl toHttpCacheControl() {
456-
CacheControl cc;
457-
if (Boolean.TRUE.equals(this.noStore)) {
458-
cc = CacheControl.noStore();
459-
}
460-
else if (Boolean.TRUE.equals(this.noCache)) {
461-
cc = CacheControl.noCache();
462-
}
463-
else if (this.maxAge != null) {
464-
cc = CacheControl.maxAge(this.maxAge.getSeconds(), TimeUnit.SECONDS);
465-
}
466-
else {
467-
cc = CacheControl.empty();
468-
}
469-
if (Boolean.TRUE.equals(this.mustRevalidate)) {
470-
cc.mustRevalidate();
471-
}
472-
if (Boolean.TRUE.equals(this.noTransform)) {
473-
cc.noTransform();
456+
CacheControl cacheControl = createCacheControl();
457+
callIfTrue(this.mustRevalidate, cacheControl, CacheControl::mustRevalidate);
458+
callIfTrue(this.noTransform, cacheControl, CacheControl::noTransform);
459+
callIfTrue(this.cachePublic, cacheControl, CacheControl::cachePublic);
460+
callIfTrue(this.cachePrivate, cacheControl, CacheControl::cachePrivate);
461+
callIfTrue(this.proxyRevalidate, cacheControl, CacheControl::proxyRevalidate);
462+
if (this.staleWhileRevalidate != null) {
463+
cacheControl.staleWhileRevalidate(this.staleWhileRevalidate.getSeconds(),
464+
TimeUnit.SECONDS);
474465
}
475-
if (Boolean.TRUE.equals(this.cachePublic)) {
476-
cc.cachePublic();
466+
if (this.staleIfError != null) {
467+
cacheControl.staleIfError(this.staleIfError.getSeconds(),
468+
TimeUnit.SECONDS);
477469
}
478-
if (Boolean.TRUE.equals(this.cachePrivate)) {
479-
cc.cachePrivate();
470+
if (this.sMaxAge != null) {
471+
cacheControl.sMaxAge(this.sMaxAge.getSeconds(), TimeUnit.SECONDS);
480472
}
481-
if (Boolean.TRUE.equals(this.proxyRevalidate)) {
482-
cc.proxyRevalidate();
473+
return cacheControl;
474+
}
475+
476+
private CacheControl createCacheControl() {
477+
if (Boolean.TRUE.equals(this.noStore)) {
478+
return CacheControl.noStore();
483479
}
484-
if (this.staleWhileRevalidate != null) {
485-
cc.staleWhileRevalidate(this.staleWhileRevalidate.getSeconds(), TimeUnit.SECONDS);
480+
if (Boolean.TRUE.equals(this.noCache)) {
481+
return CacheControl.noCache();
486482
}
487-
if (this.staleIfError != null) {
488-
cc.staleIfError(this.staleIfError.getSeconds(), TimeUnit.SECONDS);
483+
if (this.maxAge != null) {
484+
return CacheControl.maxAge(this.maxAge.getSeconds(), TimeUnit.SECONDS);
489485
}
490-
if (this.sMaxAge != null) {
491-
cc.sMaxAge(this.sMaxAge.getSeconds(), TimeUnit.SECONDS);
486+
return CacheControl.empty();
487+
}
488+
489+
private <T> void callIfTrue(Boolean property, T instance, Consumer<T> call) {
490+
if (Boolean.TRUE.equals(property)) {
491+
call.accept(instance);
492492
}
493-
return cc;
494493
}
495494

496495
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,23 +309,24 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
309309
return;
310310
}
311311
Duration cachePeriod = this.resourceProperties.getCachePeriod();
312-
CacheControl cacheControl = this.resourceProperties.getCacheControl().toHttpCacheControl();
312+
CacheControl cacheControl = this.resourceProperties.getCacheControl()
313+
.toHttpCacheControl();
313314
if (!registry.hasMappingForPattern("/webjars/**")) {
314315
customizeResourceHandlerRegistration(
315316
registry.addResourceHandler("/webjars/**")
316317
.addResourceLocations(
317318
"classpath:/META-INF/resources/webjars/")
318-
.setCachePeriod(getSeconds(cachePeriod))
319-
.setCacheControl(cacheControl));
319+
.setCachePeriod(getSeconds(cachePeriod))
320+
.setCacheControl(cacheControl));
320321
}
321322
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
322323
if (!registry.hasMappingForPattern(staticPathPattern)) {
323324
customizeResourceHandlerRegistration(
324325
registry.addResourceHandler(staticPathPattern)
325326
.addResourceLocations(getResourceLocations(
326327
this.resourceProperties.getStaticLocations()))
327-
.setCachePeriod(getSeconds(cachePeriod))
328-
.setCacheControl(cacheControl));
328+
.setCachePeriod(getSeconds(cachePeriod))
329+
.setCacheControl(cacheControl));
329330
}
330331
}
331332

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/ReactiveSessionAutoConfigurationRedisTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public void defaultConfig() {
5757
this.contextRunner.withPropertyValues("spring.session.store-type=redis")
5858
.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class,
5959
RedisReactiveAutoConfiguration.class))
60-
.run(validateSpringSessionUsesRedis("spring:session:", RedisFlushMode.ON_SAVE));
60+
.run(validateSpringSessionUsesRedis("spring:session:",
61+
RedisFlushMode.ON_SAVE));
6162
}
6263

6364
@Test
@@ -67,7 +68,8 @@ public void defaultConfigWithUniqueStoreImplementation() {
6768
ReactiveMongoOperationsSessionRepository.class))
6869
.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class,
6970
RedisReactiveAutoConfiguration.class))
70-
.run(validateSpringSessionUsesRedis("spring:session:", RedisFlushMode.ON_SAVE));
71+
.run(validateSpringSessionUsesRedis("spring:session:",
72+
RedisFlushMode.ON_SAVE));
7173
}
7274

7375
@Test

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ResourcePropertiesTests.java

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.springframework.boot.autoconfigure.web.ResourceProperties.CacheControlProperties;
2626
import org.springframework.boot.testsupport.assertj.Matched;
27+
import org.springframework.http.CacheControl;
2728

2829
import static org.assertj.core.api.Assertions.assertThat;
2930
import static org.hamcrest.CoreMatchers.endsWith;
@@ -78,42 +79,42 @@ public void customStaticLocationsAreNormalizedToEndWithTrailingSlash() {
7879

7980
@Test
8081
public void emptyCacheControl() {
81-
CacheControlProperties cacheControl = new CacheControlProperties();
82-
this.properties.setCacheControl(cacheControl);
83-
assertThat(this.properties.getCacheControl().toHttpCacheControl().getHeaderValue()).isNull();
82+
CacheControlProperties cacheControlProperties = new CacheControlProperties();
83+
this.properties.setCacheControl(cacheControlProperties);
84+
CacheControl cacheControl = this.properties.getCacheControl()
85+
.toHttpCacheControl();
86+
assertThat(cacheControl.getHeaderValue()).isNull();
8487
}
8588

8689
@Test
8790
public void cacheControlAllPropertiesSet() {
88-
CacheControlProperties cacheControl = new CacheControlProperties();
89-
cacheControl.setMaxAge(Duration.ofSeconds(4));
90-
cacheControl.setCachePrivate(true);
91-
cacheControl.setCachePublic(true);
92-
cacheControl.setMustRevalidate(true);
93-
cacheControl.setNoTransform(true);
94-
cacheControl.setProxyRevalidate(true);
95-
cacheControl.setsMaxAge(Duration.ofSeconds(5));
96-
cacheControl.setStaleIfError(Duration.ofSeconds(6));
97-
cacheControl.setStaleWhileRevalidate(Duration.ofSeconds(7));
98-
this.properties.setCacheControl(cacheControl);
99-
assertThat(this.properties.getCacheControl().toHttpCacheControl().getHeaderValue()).isEqualTo(
100-
"max-age=4, must-revalidate, no-transform, public, private, proxy-revalidate," +
101-
" s-maxage=5, stale-if-error=6, stale-while-revalidate=7");
91+
CacheControlProperties cacheControlProperties = new CacheControlProperties();
92+
cacheControlProperties.setMaxAge(Duration.ofSeconds(4));
93+
cacheControlProperties.setCachePrivate(true);
94+
cacheControlProperties.setCachePublic(true);
95+
cacheControlProperties.setMustRevalidate(true);
96+
cacheControlProperties.setNoTransform(true);
97+
cacheControlProperties.setProxyRevalidate(true);
98+
cacheControlProperties.setsMaxAge(Duration.ofSeconds(5));
99+
cacheControlProperties.setStaleIfError(Duration.ofSeconds(6));
100+
cacheControlProperties.setStaleWhileRevalidate(Duration.ofSeconds(7));
101+
this.properties.setCacheControl(cacheControlProperties);
102+
CacheControl cacheControl = this.properties.getCacheControl()
103+
.toHttpCacheControl();
104+
assertThat(cacheControl.getHeaderValue()).isEqualTo(
105+
"max-age=4, must-revalidate, no-transform, public, private, proxy-revalidate,"
106+
+ " s-maxage=5, stale-if-error=6, stale-while-revalidate=7");
102107
}
103108

104109
@Test
105110
public void invalidCacheControlCombination() {
106-
CacheControlProperties cacheControl = new CacheControlProperties();
107-
cacheControl.setMaxAge(Duration.ofSeconds(4));
108-
cacheControl.setNoStore(true);
109-
this.properties.setCacheControl(cacheControl);
110-
assertThat(this.properties.getCacheControl().toHttpCacheControl().getHeaderValue()).isEqualTo("no-store");
111-
}
112-
113-
@Test
114-
public void cacheControlNoPropertiesSet() {
115-
this.properties.setCacheControl(new CacheControlProperties());
116-
assertThat(this.properties.getCacheControl().toHttpCacheControl().getHeaderValue()).isNull();
111+
CacheControlProperties cacheControlProperties = new CacheControlProperties();
112+
cacheControlProperties.setMaxAge(Duration.ofSeconds(4));
113+
cacheControlProperties.setNoStore(true);
114+
this.properties.setCacheControl(cacheControlProperties);
115+
CacheControl cacheControl = this.properties.getCacheControl()
116+
.toHttpCacheControl();
117+
assertThat(cacheControl.getHeaderValue()).isEqualTo("no-store");
117118
}
118119

119120
}

0 commit comments

Comments
 (0)