Commit 5fb20605 authored by Stephane Nicoll's avatar Stephane Nicoll

Polish "Add configuration for Tomcat's cachingAllowed property"

Closes gh-13614
parent 200ac6db
...@@ -52,7 +52,6 @@ import org.springframework.util.StringUtils; ...@@ -52,7 +52,6 @@ import org.springframework.util.StringUtils;
* @author Aurélien Leboulanger * @author Aurélien Leboulanger
* @author Brian Clozel * @author Brian Clozel
* @author Olivier Lamy * @author Olivier Lamy
* @author Rob Tompkins
*/ */
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true) @ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties { public class ServerProperties {
...@@ -266,11 +265,6 @@ public class ServerProperties { ...@@ -266,11 +265,6 @@ public class ServerProperties {
*/ */
private final Accesslog accesslog = new Accesslog(); private final Accesslog accesslog = new Accesslog();
/**
* Web resource configuration.
*/
private final WebResource webResource = new WebResource();
/** /**
* Regular expression matching trusted IP addresses. * Regular expression matching trusted IP addresses.
*/ */
...@@ -405,10 +399,6 @@ public class ServerProperties { ...@@ -405,10 +399,6 @@ public class ServerProperties {
return this.accesslog; return this.accesslog;
} }
public WebResource getWebResource() {
return this.webResource;
}
public Duration getBackgroundProcessorDelay() { public Duration getBackgroundProcessorDelay() {
return this.backgroundProcessorDelay; return this.backgroundProcessorDelay;
} }
...@@ -525,26 +515,6 @@ public class ServerProperties { ...@@ -525,26 +515,6 @@ public class ServerProperties {
return this.resource; return this.resource;
} }
/**
* Tomcat web resource properties.
*/
public static class WebResource {
/**
* Whether tomcat WebResource caching is permitted for this web application.
*/
private Boolean useCaching = Boolean.TRUE;
public Boolean getUseCaching() {
return this.useCaching;
}
public void setUseCaching(Boolean useCaching) {
this.useCaching = useCaching;
}
}
/** /**
* Tomcat access log properties. * Tomcat access log properties.
*/ */
...@@ -690,11 +660,24 @@ public class ServerProperties { ...@@ -690,11 +660,24 @@ public class ServerProperties {
*/ */
public static class Resource { public static class Resource {
/**
* Whether static resource caching is permitted for this web application.
*/
private boolean allowCaching = true;
/** /**
* Time-to-live of the static resource cache. * Time-to-live of the static resource cache.
*/ */
private Duration cacheTtl; private Duration cacheTtl;
public boolean isAllowCaching() {
return this.allowCaching;
}
public void setAllowCaching(boolean allowCaching) {
this.allowCaching = allowCaching;
}
public Duration getCacheTtl() { public Duration getCacheTtl() {
return this.cacheTtl; return this.cacheTtl;
} }
......
...@@ -19,11 +19,9 @@ package org.springframework.boot.autoconfigure.web.embedded; ...@@ -19,11 +19,9 @@ package org.springframework.boot.autoconfigure.web.embedded;
import java.time.Duration; import java.time.Duration;
import org.apache.catalina.Lifecycle; import org.apache.catalina.Lifecycle;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.valves.AccessLogValve; import org.apache.catalina.valves.AccessLogValve;
import org.apache.catalina.valves.ErrorReportValve; import org.apache.catalina.valves.ErrorReportValve;
import org.apache.catalina.valves.RemoteIpValve; import org.apache.catalina.valves.RemoteIpValve;
import org.apache.catalina.webresources.StandardRoot;
import org.apache.coyote.AbstractProtocol; import org.apache.coyote.AbstractProtocol;
import org.apache.coyote.ProtocolHandler; import org.apache.coyote.ProtocolHandler;
import org.apache.coyote.http11.AbstractHttp11Protocol; import org.apache.coyote.http11.AbstractHttp11Protocol;
...@@ -48,7 +46,6 @@ import org.springframework.util.StringUtils; ...@@ -48,7 +46,6 @@ import org.springframework.util.StringUtils;
* @author Yulin Qin * @author Yulin Qin
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Phillip Webb * @author Phillip Webb
* @author Rob Tompkins
* @since 2.0.0 * @since 2.0.0
*/ */
public class TomcatWebServerFactoryCustomizer implements public class TomcatWebServerFactoryCustomizer implements
...@@ -73,8 +70,6 @@ public class TomcatWebServerFactoryCustomizer implements ...@@ -73,8 +70,6 @@ public class TomcatWebServerFactoryCustomizer implements
public void customize(ConfigurableTomcatWebServerFactory factory) { public void customize(ConfigurableTomcatWebServerFactory factory) {
ServerProperties properties = this.serverProperties; ServerProperties properties = this.serverProperties;
ServerProperties.Tomcat tomcatProperties = properties.getTomcat(); ServerProperties.Tomcat tomcatProperties = properties.getTomcat();
ServerProperties.Tomcat.WebResource tomcatWebResourceProperties = tomcatProperties
.getWebResource();
PropertyMapper propertyMapper = PropertyMapper.get(); PropertyMapper propertyMapper = PropertyMapper.get();
propertyMapper.from(tomcatProperties::getBasedir).whenNonNull() propertyMapper.from(tomcatProperties::getBasedir).whenNonNull()
.to(factory::setBaseDirectory); .to(factory::setBaseDirectory);
...@@ -106,9 +101,6 @@ public class TomcatWebServerFactoryCustomizer implements ...@@ -106,9 +101,6 @@ public class TomcatWebServerFactoryCustomizer implements
.to((maxConnections) -> customizeMaxConnections(factory, maxConnections)); .to((maxConnections) -> customizeMaxConnections(factory, maxConnections));
propertyMapper.from(tomcatProperties::getAcceptCount).when(this::isPositive) propertyMapper.from(tomcatProperties::getAcceptCount).when(this::isPositive)
.to((acceptCount) -> customizeAcceptCount(factory, acceptCount)); .to((acceptCount) -> customizeAcceptCount(factory, acceptCount));
propertyMapper.from(tomcatWebResourceProperties::getUseCaching).whenFalse()
.to((isWebResourceCachingAllowed) -> customizeWebResourceCaching(factory,
isWebResourceCachingAllowed));
customizeStaticResources(factory); customizeStaticResources(factory);
customizeErrorReportValve(properties.getError(), factory); customizeErrorReportValve(properties.getError(), factory);
} }
...@@ -134,18 +126,6 @@ public class TomcatWebServerFactoryCustomizer implements ...@@ -134,18 +126,6 @@ public class TomcatWebServerFactoryCustomizer implements
}); });
} }
private void customizeWebResourceCaching(ConfigurableTomcatWebServerFactory factory,
Boolean useWebResourceCaching) {
factory.addContextCustomizers((context) -> {
WebResourceRoot webResourceRoot = context.getResources();
if (webResourceRoot == null) {
webResourceRoot = new StandardRoot(context);
}
webResourceRoot.setCachingAllowed(useWebResourceCaching);
context.setResources(webResourceRoot);
});
}
private void customizeMaxConnections(ConfigurableTomcatWebServerFactory factory, private void customizeMaxConnections(ConfigurableTomcatWebServerFactory factory,
int maxConnections) { int maxConnections) {
factory.addConnectorCustomizers((connector) -> { factory.addConnectorCustomizers((connector) -> {
...@@ -261,14 +241,14 @@ public class TomcatWebServerFactoryCustomizer implements ...@@ -261,14 +241,14 @@ public class TomcatWebServerFactoryCustomizer implements
private void customizeStaticResources(ConfigurableTomcatWebServerFactory factory) { private void customizeStaticResources(ConfigurableTomcatWebServerFactory factory) {
ServerProperties.Tomcat.Resource resource = this.serverProperties.getTomcat() ServerProperties.Tomcat.Resource resource = this.serverProperties.getTomcat()
.getResource(); .getResource();
if (resource.getCacheTtl() == null) {
return;
}
factory.addContextCustomizers((context) -> { factory.addContextCustomizers((context) -> {
context.addLifecycleListener((event) -> { context.addLifecycleListener((event) -> {
if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) { if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) {
long ttl = resource.getCacheTtl().toMillis(); context.getResources().setCachingAllowed(resource.isAllowCaching());
context.getResources().setCacheTtl(ttl); if (resource.getCacheTtl() != null) {
long ttl = resource.getCacheTtl().toMillis();
context.getResources().setCacheTtl(ttl);
}
} }
}); });
}); });
......
...@@ -99,20 +99,6 @@ public class TomcatWebServerFactoryCustomizerTests { ...@@ -99,20 +99,6 @@ public class TomcatWebServerFactoryCustomizerTests {
.isEqualTo(5)); .isEqualTo(5));
} }
@Test
public void turnOffWebResourceCaching() {
bind("server.tomcat.webresource.use-caching=false");
customizeAndRunServer((server) -> {
Mapper mapper = server.getTomcat().getService().getMapper();
Object contextObjectToContextVersionMap = ReflectionTestUtils.getField(mapper,
"contextObjectToContextVersionMap");
Object tomcatEmbeddedContext = ((Map<Context, Object>) contextObjectToContextVersionMap)
.values().toArray()[0];
assertThat(((StandardRoot) ReflectionTestUtils.getField(tomcatEmbeddedContext,
"resources")).isCachingAllowed()).isFalse();
});
}
@Test @Test
public void customMaxHttpPostSize() { public void customMaxHttpPostSize() {
bind("server.tomcat.max-http-post-size=10000"); bind("server.tomcat.max-http-post-size=10000");
...@@ -140,6 +126,20 @@ public class TomcatWebServerFactoryCustomizerTests { ...@@ -140,6 +126,20 @@ public class TomcatWebServerFactoryCustomizerTests {
assertThat(remoteIpValve.getInternalProxies()).isEqualTo("192.168.0.1"); assertThat(remoteIpValve.getInternalProxies()).isEqualTo("192.168.0.1");
} }
@Test
public void customStaticResourceAllowCaching() {
bind("server.tomcat.resource.allow-caching=false");
customizeAndRunServer((server) -> {
Mapper mapper = server.getTomcat().getService().getMapper();
Object contextObjectToContextVersionMap = ReflectionTestUtils.getField(mapper,
"contextObjectToContextVersionMap");
Object tomcatEmbeddedContext = ((Map<Context, Object>) contextObjectToContextVersionMap)
.values().toArray()[0];
assertThat(((StandardRoot) ReflectionTestUtils.getField(tomcatEmbeddedContext,
"resources")).isCachingAllowed()).isFalse();
});
}
@Test @Test
public void customStaticResourceCacheTtl() { public void customStaticResourceCacheTtl() {
bind("server.tomcat.resource.cache-ttl=10000"); bind("server.tomcat.resource.cache-ttl=10000");
......
...@@ -259,10 +259,10 @@ content into your application. Rather, pick only the properties that you need. ...@@ -259,10 +259,10 @@ content into your application. Rather, pick only the properties that you need.
server.tomcat.protocol-header-https-value=https # Value of the protocol header indicating whether the incoming request uses SSL. server.tomcat.protocol-header-https-value=https # Value of the protocol header indicating whether the incoming request uses SSL.
server.tomcat.redirect-context-root= # Whether requests to the context root should be redirected by appending a / to the path. server.tomcat.redirect-context-root= # Whether requests to the context root should be redirected by appending a / to the path.
server.tomcat.remote-ip-header= # Name of the HTTP header from which the remote IP is extracted. For instance, `X-FORWARDED-FOR`. server.tomcat.remote-ip-header= # Name of the HTTP header from which the remote IP is extracted. For instance, `X-FORWARDED-FOR`.
server.tomcat.resource.allow-caching= # Whether static resource caching is permitted for this web application.
server.tomcat.resource.cache-ttl= # Time-to-live of the static resource cache. server.tomcat.resource.cache-ttl= # Time-to-live of the static resource cache.
server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI. server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI.
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. 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.
server.tomcat.webresource.use-caching= # Whether tomcat WebResource caching is permitted for this web application.
server.undertow.accesslog.dir= # Undertow access log directory. server.undertow.accesslog.dir= # Undertow access log directory.
server.undertow.accesslog.enabled=false # Whether to enable the access log. server.undertow.accesslog.enabled=false # Whether to enable the access log.
server.undertow.accesslog.pattern=common # Format pattern for access logs. server.undertow.accesslog.pattern=common # Format pattern for access logs.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment