Skip to content

Commit 5fb2060

Browse files
committed
Polish "Add configuration for Tomcat's cachingAllowed property"
Closes gh-13614
1 parent 200ac6d commit 5fb2060

File tree

4 files changed

+33
-70
lines changed

4 files changed

+33
-70
lines changed

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

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
* @author Aurélien Leboulanger
5353
* @author Brian Clozel
5454
* @author Olivier Lamy
55-
* @author Rob Tompkins
5655
*/
5756
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
5857
public class ServerProperties {
@@ -266,11 +265,6 @@ public static class Tomcat {
266265
*/
267266
private final Accesslog accesslog = new Accesslog();
268267

269-
/**
270-
* Web resource configuration.
271-
*/
272-
private final WebResource webResource = new WebResource();
273-
274268
/**
275269
* Regular expression matching trusted IP addresses.
276270
*/
@@ -405,10 +399,6 @@ public Accesslog getAccesslog() {
405399
return this.accesslog;
406400
}
407401

408-
public WebResource getWebResource() {
409-
return this.webResource;
410-
}
411-
412402
public Duration getBackgroundProcessorDelay() {
413403
return this.backgroundProcessorDelay;
414404
}
@@ -525,26 +515,6 @@ public Resource getResource() {
525515
return this.resource;
526516
}
527517

528-
/**
529-
* Tomcat web resource properties.
530-
*/
531-
public static class WebResource {
532-
533-
/**
534-
* Whether tomcat WebResource caching is permitted for this web application.
535-
*/
536-
private Boolean useCaching = Boolean.TRUE;
537-
538-
public Boolean getUseCaching() {
539-
return this.useCaching;
540-
}
541-
542-
public void setUseCaching(Boolean useCaching) {
543-
this.useCaching = useCaching;
544-
}
545-
546-
}
547-
548518
/**
549519
* Tomcat access log properties.
550520
*/
@@ -690,11 +660,24 @@ public void setBuffered(boolean buffered) {
690660
*/
691661
public static class Resource {
692662

663+
/**
664+
* Whether static resource caching is permitted for this web application.
665+
*/
666+
private boolean allowCaching = true;
667+
693668
/**
694669
* Time-to-live of the static resource cache.
695670
*/
696671
private Duration cacheTtl;
697672

673+
public boolean isAllowCaching() {
674+
return this.allowCaching;
675+
}
676+
677+
public void setAllowCaching(boolean allowCaching) {
678+
this.allowCaching = allowCaching;
679+
}
680+
698681
public Duration getCacheTtl() {
699682
return this.cacheTtl;
700683
}

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

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919
import java.time.Duration;
2020

2121
import org.apache.catalina.Lifecycle;
22-
import org.apache.catalina.WebResourceRoot;
2322
import org.apache.catalina.valves.AccessLogValve;
2423
import org.apache.catalina.valves.ErrorReportValve;
2524
import org.apache.catalina.valves.RemoteIpValve;
26-
import org.apache.catalina.webresources.StandardRoot;
2725
import org.apache.coyote.AbstractProtocol;
2826
import org.apache.coyote.ProtocolHandler;
2927
import org.apache.coyote.http11.AbstractHttp11Protocol;
@@ -48,7 +46,6 @@
4846
* @author Yulin Qin
4947
* @author Stephane Nicoll
5048
* @author Phillip Webb
51-
* @author Rob Tompkins
5249
* @since 2.0.0
5350
*/
5451
public class TomcatWebServerFactoryCustomizer implements
@@ -73,8 +70,6 @@ public int getOrder() {
7370
public void customize(ConfigurableTomcatWebServerFactory factory) {
7471
ServerProperties properties = this.serverProperties;
7572
ServerProperties.Tomcat tomcatProperties = properties.getTomcat();
76-
ServerProperties.Tomcat.WebResource tomcatWebResourceProperties = tomcatProperties
77-
.getWebResource();
7873
PropertyMapper propertyMapper = PropertyMapper.get();
7974
propertyMapper.from(tomcatProperties::getBasedir).whenNonNull()
8075
.to(factory::setBaseDirectory);
@@ -106,9 +101,6 @@ public void customize(ConfigurableTomcatWebServerFactory factory) {
106101
.to((maxConnections) -> customizeMaxConnections(factory, maxConnections));
107102
propertyMapper.from(tomcatProperties::getAcceptCount).when(this::isPositive)
108103
.to((acceptCount) -> customizeAcceptCount(factory, acceptCount));
109-
propertyMapper.from(tomcatWebResourceProperties::getUseCaching).whenFalse()
110-
.to((isWebResourceCachingAllowed) -> customizeWebResourceCaching(factory,
111-
isWebResourceCachingAllowed));
112104
customizeStaticResources(factory);
113105
customizeErrorReportValve(properties.getError(), factory);
114106
}
@@ -134,18 +126,6 @@ private void customizeAcceptCount(ConfigurableTomcatWebServerFactory factory,
134126
});
135127
}
136128

137-
private void customizeWebResourceCaching(ConfigurableTomcatWebServerFactory factory,
138-
Boolean useWebResourceCaching) {
139-
factory.addContextCustomizers((context) -> {
140-
WebResourceRoot webResourceRoot = context.getResources();
141-
if (webResourceRoot == null) {
142-
webResourceRoot = new StandardRoot(context);
143-
}
144-
webResourceRoot.setCachingAllowed(useWebResourceCaching);
145-
context.setResources(webResourceRoot);
146-
});
147-
}
148-
149129
private void customizeMaxConnections(ConfigurableTomcatWebServerFactory factory,
150130
int maxConnections) {
151131
factory.addConnectorCustomizers((connector) -> {
@@ -261,14 +241,14 @@ private void customizeAccessLog(ConfigurableTomcatWebServerFactory factory) {
261241
private void customizeStaticResources(ConfigurableTomcatWebServerFactory factory) {
262242
ServerProperties.Tomcat.Resource resource = this.serverProperties.getTomcat()
263243
.getResource();
264-
if (resource.getCacheTtl() == null) {
265-
return;
266-
}
267244
factory.addContextCustomizers((context) -> {
268245
context.addLifecycleListener((event) -> {
269246
if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) {
270-
long ttl = resource.getCacheTtl().toMillis();
271-
context.getResources().setCacheTtl(ttl);
247+
context.getResources().setCachingAllowed(resource.isAllowCaching());
248+
if (resource.getCacheTtl() != null) {
249+
long ttl = resource.getCacheTtl().toMillis();
250+
context.getResources().setCacheTtl(ttl);
251+
}
272252
}
273253
});
274254
});

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,6 @@ public void customMaxConnections() {
9999
.isEqualTo(5));
100100
}
101101

102-
@Test
103-
public void turnOffWebResourceCaching() {
104-
bind("server.tomcat.webresource.use-caching=false");
105-
customizeAndRunServer((server) -> {
106-
Mapper mapper = server.getTomcat().getService().getMapper();
107-
Object contextObjectToContextVersionMap = ReflectionTestUtils.getField(mapper,
108-
"contextObjectToContextVersionMap");
109-
Object tomcatEmbeddedContext = ((Map<Context, Object>) contextObjectToContextVersionMap)
110-
.values().toArray()[0];
111-
assertThat(((StandardRoot) ReflectionTestUtils.getField(tomcatEmbeddedContext,
112-
"resources")).isCachingAllowed()).isFalse();
113-
});
114-
}
115-
116102
@Test
117103
public void customMaxHttpPostSize() {
118104
bind("server.tomcat.max-http-post-size=10000");
@@ -140,6 +126,20 @@ public void customRemoteIpValve() {
140126
assertThat(remoteIpValve.getInternalProxies()).isEqualTo("192.168.0.1");
141127
}
142128

129+
@Test
130+
public void customStaticResourceAllowCaching() {
131+
bind("server.tomcat.resource.allow-caching=false");
132+
customizeAndRunServer((server) -> {
133+
Mapper mapper = server.getTomcat().getService().getMapper();
134+
Object contextObjectToContextVersionMap = ReflectionTestUtils.getField(mapper,
135+
"contextObjectToContextVersionMap");
136+
Object tomcatEmbeddedContext = ((Map<Context, Object>) contextObjectToContextVersionMap)
137+
.values().toArray()[0];
138+
assertThat(((StandardRoot) ReflectionTestUtils.getField(tomcatEmbeddedContext,
139+
"resources")).isCachingAllowed()).isFalse();
140+
});
141+
}
142+
143143
@Test
144144
public void customStaticResourceCacheTtl() {
145145
bind("server.tomcat.resource.cache-ttl=10000");

spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,10 @@ content into your application. Rather, pick only the properties that you need.
259259
server.tomcat.protocol-header-https-value=https # Value of the protocol header indicating whether the incoming request uses SSL.
260260
server.tomcat.redirect-context-root= # Whether requests to the context root should be redirected by appending a / to the path.
261261
server.tomcat.remote-ip-header= # Name of the HTTP header from which the remote IP is extracted. For instance, `X-FORWARDED-FOR`.
262+
server.tomcat.resource.allow-caching= # Whether static resource caching is permitted for this web application.
262263
server.tomcat.resource.cache-ttl= # Time-to-live of the static resource cache.
263264
server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI.
264265
server.tomcat.use-relative-redirects= # Whether HTTP 1.1 and later location headers generated by a call to sendRedirect will use relative or absolute redirects.
265-
server.tomcat.webresource.use-caching= # Whether tomcat WebResource caching is permitted for this web application.
266266
server.undertow.accesslog.dir= # Undertow access log directory.
267267
server.undertow.accesslog.enabled=false # Whether to enable the access log.
268268
server.undertow.accesslog.pattern=common # Format pattern for access logs.

0 commit comments

Comments
 (0)