@@ -23,7 +23,6 @@ import java.util.function.Consumer;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.bind.convert.DefaultDurationUnit;
|
||||
import org.springframework.http.CacheControl;
|
||||
|
||||
/**
|
||||
* Properties used to configure resource handling.
|
||||
@@ -275,7 +274,7 @@ public class ResourceProperties {
|
||||
/**
|
||||
* Cache period for the resources served by the resource handler. If a duration
|
||||
* suffix is not specified, seconds will be used. Can be overridden by the
|
||||
* 'spring.resources.cache.control' properties.
|
||||
* 'spring.resources.cache.cachecontrol' properties.
|
||||
*/
|
||||
@DefaultDurationUnit(ChronoUnit.SECONDS)
|
||||
private Duration period;
|
||||
@@ -284,7 +283,7 @@ public class ResourceProperties {
|
||||
* Cache control HTTP headers, only allows valid directive combinations. Overrides
|
||||
* the 'spring.resources.cache.period' property.
|
||||
*/
|
||||
private final Control control = new Control();
|
||||
private final CacheControl cacheControl = new CacheControl();
|
||||
|
||||
public Duration getPeriod() {
|
||||
return this.period;
|
||||
@@ -294,14 +293,14 @@ public class ResourceProperties {
|
||||
this.period = period;
|
||||
}
|
||||
|
||||
public Control getControl() {
|
||||
return this.control;
|
||||
public CacheControl getCacheControl() {
|
||||
return this.cacheControl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache Control HTTP header configuration.
|
||||
*/
|
||||
public static class Control {
|
||||
public static class CacheControl {
|
||||
|
||||
/**
|
||||
* Maximum time the response should be cached, in seconds if no duration
|
||||
@@ -459,15 +458,15 @@ public class ResourceProperties {
|
||||
this.sMaxAge = sMaxAge;
|
||||
}
|
||||
|
||||
public CacheControl toHttpCacheControl() {
|
||||
CacheControl cacheControl = createCacheControl();
|
||||
public org.springframework.http.CacheControl toHttpCacheControl() {
|
||||
org.springframework.http.CacheControl cacheControl = createCacheControl();
|
||||
callIfTrue(this.mustRevalidate, cacheControl,
|
||||
CacheControl::mustRevalidate);
|
||||
callIfTrue(this.noTransform, cacheControl, CacheControl::noTransform);
|
||||
callIfTrue(this.cachePublic, cacheControl, CacheControl::cachePublic);
|
||||
callIfTrue(this.cachePrivate, cacheControl, CacheControl::cachePrivate);
|
||||
org.springframework.http.CacheControl::mustRevalidate);
|
||||
callIfTrue(this.noTransform, cacheControl, org.springframework.http.CacheControl::noTransform);
|
||||
callIfTrue(this.cachePublic, cacheControl, org.springframework.http.CacheControl::cachePublic);
|
||||
callIfTrue(this.cachePrivate, cacheControl, org.springframework.http.CacheControl::cachePrivate);
|
||||
callIfTrue(this.proxyRevalidate, cacheControl,
|
||||
CacheControl::proxyRevalidate);
|
||||
org.springframework.http.CacheControl::proxyRevalidate);
|
||||
if (this.staleWhileRevalidate != null) {
|
||||
cacheControl.staleWhileRevalidate(
|
||||
this.staleWhileRevalidate.getSeconds(), TimeUnit.SECONDS);
|
||||
@@ -482,18 +481,18 @@ public class ResourceProperties {
|
||||
return cacheControl;
|
||||
}
|
||||
|
||||
private CacheControl createCacheControl() {
|
||||
private org.springframework.http.CacheControl createCacheControl() {
|
||||
if (Boolean.TRUE.equals(this.noStore)) {
|
||||
return CacheControl.noStore();
|
||||
return org.springframework.http.CacheControl.noStore();
|
||||
}
|
||||
if (Boolean.TRUE.equals(this.noCache)) {
|
||||
return CacheControl.noCache();
|
||||
return org.springframework.http.CacheControl.noCache();
|
||||
}
|
||||
if (this.maxAge != null) {
|
||||
return CacheControl.maxAge(this.maxAge.getSeconds(),
|
||||
return org.springframework.http.CacheControl.maxAge(this.maxAge.getSeconds(),
|
||||
TimeUnit.SECONDS);
|
||||
}
|
||||
return CacheControl.empty();
|
||||
return org.springframework.http.CacheControl.empty();
|
||||
}
|
||||
|
||||
private <T> void callIfTrue(Boolean property, T instance, Consumer<T> call) {
|
||||
|
||||
@@ -307,7 +307,7 @@ public class WebMvcAutoConfiguration {
|
||||
return;
|
||||
}
|
||||
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
|
||||
CacheControl cacheControl = this.resourceProperties.getCache().getControl()
|
||||
CacheControl cacheControl = this.resourceProperties.getCache().getCacheControl()
|
||||
.toHttpCacheControl();
|
||||
if (!registry.hasMappingForPattern("/webjars/**")) {
|
||||
customizeResourceHandlerRegistration(
|
||||
|
||||
@@ -22,7 +22,6 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.web.ResourceProperties.Cache;
|
||||
import org.springframework.boot.testsupport.assertj.Matched;
|
||||
import org.springframework.http.CacheControl;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.CoreMatchers.endsWith;
|
||||
@@ -74,14 +73,14 @@ public class ResourcePropertiesTests {
|
||||
|
||||
@Test
|
||||
public void emptyCacheControl() {
|
||||
CacheControl cacheControl = this.properties.getCache().getControl()
|
||||
org.springframework.http.CacheControl cacheControl = this.properties.getCache().getCacheControl()
|
||||
.toHttpCacheControl();
|
||||
assertThat(cacheControl.getHeaderValue()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cacheControlAllPropertiesSet() {
|
||||
Cache.Control properties = this.properties.getCache().getControl();
|
||||
Cache.CacheControl properties = this.properties.getCache().getCacheControl();
|
||||
properties.setMaxAge(Duration.ofSeconds(4));
|
||||
properties.setCachePrivate(true);
|
||||
properties.setCachePublic(true);
|
||||
@@ -91,7 +90,7 @@ public class ResourcePropertiesTests {
|
||||
properties.setSMaxAge(Duration.ofSeconds(5));
|
||||
properties.setStaleIfError(Duration.ofSeconds(6));
|
||||
properties.setStaleWhileRevalidate(Duration.ofSeconds(7));
|
||||
CacheControl cacheControl = properties.toHttpCacheControl();
|
||||
org.springframework.http.CacheControl cacheControl = properties.toHttpCacheControl();
|
||||
assertThat(cacheControl.getHeaderValue()).isEqualTo(
|
||||
"max-age=4, must-revalidate, no-transform, public, private, proxy-revalidate,"
|
||||
+ " s-maxage=5, stale-if-error=6, stale-while-revalidate=7");
|
||||
@@ -99,10 +98,10 @@ public class ResourcePropertiesTests {
|
||||
|
||||
@Test
|
||||
public void invalidCacheControlCombination() {
|
||||
Cache.Control properties = this.properties.getCache().getControl();
|
||||
Cache.CacheControl properties = this.properties.getCache().getCacheControl();
|
||||
properties.setMaxAge(Duration.ofSeconds(4));
|
||||
properties.setNoStore(true);
|
||||
CacheControl cacheControl = properties.toHttpCacheControl();
|
||||
org.springframework.http.CacheControl cacheControl = properties.toHttpCacheControl();
|
||||
assertThat(cacheControl.getHeaderValue()).isEqualTo("no-store");
|
||||
}
|
||||
|
||||
|
||||
@@ -733,8 +733,8 @@ public class WebMvcAutoConfigurationTests {
|
||||
@Test
|
||||
public void cacheControl() throws Exception {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.resources.cache.control.max-age:5",
|
||||
"spring.resources.cache.control.proxy-revalidate:true")
|
||||
.withPropertyValues("spring.resources.cache.cachecontrol.max-age:5",
|
||||
"spring.resources.cache.cachecontrol.proxy-revalidate:true")
|
||||
.run((context) -> assertCacheControl(context));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user