mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Improve YAML-based configuration of Tomcat compression
Tomcat uses the strings “on” and “off” to enable and disable compression. YAML interprets on as true and off as false, leaving ServerProperties.Tomcat.compression configured with “true” and “false” respectively. One solution is to use “on” rather than on and “off” rather than off in the YAML file but users may not realise that they need to do so. This commit updates the connector customiser that configures compression to map “true” to “on” and “false” to “off”. Closes gh-2737
This commit is contained in:
parent
527850ae55
commit
c3571d416a
@ -483,10 +483,20 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
|
||||
if (handler instanceof AbstractHttp11Protocol) {
|
||||
@SuppressWarnings("rawtypes")
|
||||
AbstractHttp11Protocol protocol = (AbstractHttp11Protocol) handler;
|
||||
protocol.setCompression(Tomcat.this.compression);
|
||||
protocol.setCompression(coerceCompression(Tomcat.this.compression));
|
||||
protocol.setCompressableMimeTypes(Tomcat.this.compressableMimeTypes);
|
||||
}
|
||||
}
|
||||
|
||||
private String coerceCompression(String compression) {
|
||||
if (Boolean.toString(true).equals(compression)) {
|
||||
return "on";
|
||||
}
|
||||
else if (Boolean.toString(false).equals(compression)) {
|
||||
return "off";
|
||||
}
|
||||
return compression;
|
||||
}
|
||||
});
|
||||
|
||||
if (this.accessLogEnabled) {
|
||||
|
@ -31,6 +31,8 @@ import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletCont
|
||||
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer;
|
||||
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.core.IsInstanceOf.instanceOf;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
@ -197,25 +199,19 @@ public class ServerPropertiesTests {
|
||||
|
||||
@Test
|
||||
public void customTomcatCompression() throws Exception {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
map.put("server.port", "0");
|
||||
map.put("server.tomcat.compression", "on");
|
||||
bindProperties(map);
|
||||
assertThat("on", is(equalTo(configureCompression("on"))));
|
||||
}
|
||||
|
||||
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
|
||||
this.properties.customize(factory);
|
||||
@Test
|
||||
public void disableTomcatCompressionWithYaml() throws Exception {
|
||||
// YAML interprets "off" as false, check that it's mapped back to off
|
||||
assertThat("off", is(equalTo(configureCompression("false"))));
|
||||
}
|
||||
|
||||
TomcatEmbeddedServletContainer container = (TomcatEmbeddedServletContainer) factory
|
||||
.getEmbeddedServletContainer();
|
||||
|
||||
try {
|
||||
AbstractHttp11Protocol<?> protocol = (AbstractHttp11Protocol<?>) container
|
||||
.getTomcat().getConnector().getProtocolHandler();
|
||||
assertEquals("on", protocol.getCompression());
|
||||
}
|
||||
finally {
|
||||
container.stop();
|
||||
}
|
||||
@Test
|
||||
public void enableTomcatCompressionWithYaml() throws Exception {
|
||||
// YAML interprets "on" as true, check that it's mapped back to on
|
||||
assertThat("on", is(equalTo(configureCompression("true"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -246,4 +242,27 @@ public class ServerPropertiesTests {
|
||||
map));
|
||||
}
|
||||
|
||||
private String configureCompression(String compression) {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
map.put("server.port", "0");
|
||||
// YAML interprets "on" as true
|
||||
map.put("server.tomcat.compression", compression);
|
||||
bindProperties(map);
|
||||
|
||||
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
|
||||
this.properties.customize(factory);
|
||||
|
||||
TomcatEmbeddedServletContainer container = (TomcatEmbeddedServletContainer) factory
|
||||
.getEmbeddedServletContainer();
|
||||
|
||||
try {
|
||||
AbstractHttp11Protocol<?> protocol = (AbstractHttp11Protocol<?>) container
|
||||
.getTomcat().getConnector().getProtocolHandler();
|
||||
return protocol.getCompression();
|
||||
}
|
||||
finally {
|
||||
container.stop();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user