Browse Source

Polish contribution

See gh-47229
pull/47268/head
Stéphane Nicoll 6 months ago
parent
commit
aebecb3007
  1. 6
      module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatServerProperties.java
  2. 6
      module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizer.java
  3. 14
      module/spring-boot-tomcat/src/test/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizerTests.java

6
module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatServerProperties.java

@ -697,7 +697,7 @@ public class TomcatServerProperties { @@ -697,7 +697,7 @@ public class TomcatServerProperties {
/**
* Maximum size of the static resource cache.
*/
private @Nullable DataSize cacheMaxSize;
private DataSize cacheMaxSize = DataSize.ofMegabytes(10);
/**
* Time-to-live of the static resource cache.
@ -712,11 +712,11 @@ public class TomcatServerProperties { @@ -712,11 +712,11 @@ public class TomcatServerProperties {
this.allowCaching = allowCaching;
}
public @Nullable DataSize getCacheMaxSize() {
public DataSize getCacheMaxSize() {
return this.cacheMaxSize;
}
public void setCacheMaxSize(@Nullable DataSize cacheMaxSize) {
public void setCacheMaxSize(DataSize cacheMaxSize) {
this.cacheMaxSize = cacheMaxSize;
}

6
module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizer.java

@ -380,14 +380,12 @@ public class TomcatWebServerFactoryCustomizer @@ -380,14 +380,12 @@ public class TomcatWebServerFactoryCustomizer
factory.addContextCustomizers((context) -> context.addLifecycleListener((event) -> {
if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) {
context.getResources().setCachingAllowed(resource.isAllowCaching());
long cacheMaxSize = resource.getCacheMaxSize().toKilobytes();
context.getResources().setCacheMaxSize(cacheMaxSize);
if (resource.getCacheTtl() != null) {
long ttl = resource.getCacheTtl().toMillis();
context.getResources().setCacheTtl(ttl);
}
if (resource.getCacheMaxSize() != null) {
long cacheMaxSize = resource.getCacheMaxSize().toKilobytes();
context.getResources().setCacheMaxSize(cacheMaxSize);
}
}
}));
}

14
module/spring-boot-tomcat/src/test/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizerTests.java

@ -322,6 +322,18 @@ class TomcatWebServerFactoryCustomizerTests { @@ -322,6 +322,18 @@ class TomcatWebServerFactoryCustomizerTests {
assertThat(remoteIpValve.getTrustedProxies()).isEqualTo("proxy1|proxy2");
}
@Test
void resourceCacheMatchesDefault() {
TomcatServerProperties properties = new TomcatServerProperties();
customizeAndRunServer((server) -> {
Tomcat tomcat = server.getTomcat();
Context context = (Context) tomcat.getHost().findChildren()[0];
assertThat(properties.getResource().isAllowCaching()).isEqualTo(context.getResources().isCachingAllowed());
assertThat(properties.getResource().getCacheMaxSize())
.isEqualTo(DataSize.ofKilobytes(context.getResources().getCacheMaxSize()));
});
}
@Test
void customStaticResourceAllowCaching() {
bind("server.tomcat.resource.allow-caching=false");
@ -334,7 +346,7 @@ class TomcatWebServerFactoryCustomizerTests { @@ -334,7 +346,7 @@ class TomcatWebServerFactoryCustomizerTests {
@Test
void customStaticResourceCacheMaxSize() {
bind("server.tomcat.resource.cache-max-size=4096KB");
bind("server.tomcat.resource.cache-max-size=4MB");
customizeAndRunServer((server) -> {
Tomcat tomcat = server.getTomcat();
Context context = (Context) tomcat.getHost().findChildren()[0];

Loading…
Cancel
Save