Skip to content

Commit c3571d4

Browse files
committed
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
1 parent 527850a commit c3571d4

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,10 +483,20 @@ public void customize(Connector connector) {
483483
if (handler instanceof AbstractHttp11Protocol) {
484484
@SuppressWarnings("rawtypes")
485485
AbstractHttp11Protocol protocol = (AbstractHttp11Protocol) handler;
486-
protocol.setCompression(Tomcat.this.compression);
486+
protocol.setCompression(coerceCompression(Tomcat.this.compression));
487487
protocol.setCompressableMimeTypes(Tomcat.this.compressableMimeTypes);
488488
}
489489
}
490+
491+
private String coerceCompression(String compression) {
492+
if (Boolean.toString(true).equals(compression)) {
493+
return "on";
494+
}
495+
else if (Boolean.toString(false).equals(compression)) {
496+
return "off";
497+
}
498+
return compression;
499+
}
490500
});
491501

492502
if (this.accessLogEnabled) {

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer;
3232
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
3333

34+
import static org.hamcrest.Matchers.equalTo;
35+
import static org.hamcrest.Matchers.is;
3436
import static org.hamcrest.core.IsInstanceOf.instanceOf;
3537
import static org.junit.Assert.assertEquals;
3638
import static org.junit.Assert.assertFalse;
@@ -197,9 +199,26 @@ public void customTomcatRemoteIpValve() throws Exception {
197199

198200
@Test
199201
public void customTomcatCompression() throws Exception {
202+
assertThat("on", is(equalTo(configureCompression("on"))));
203+
}
204+
205+
@Test
206+
public void disableTomcatCompressionWithYaml() throws Exception {
207+
// YAML interprets "off" as false, check that it's mapped back to off
208+
assertThat("off", is(equalTo(configureCompression("false"))));
209+
}
210+
211+
@Test
212+
public void enableTomcatCompressionWithYaml() throws Exception {
213+
// YAML interprets "on" as true, check that it's mapped back to on
214+
assertThat("on", is(equalTo(configureCompression("true"))));
215+
}
216+
217+
@Test
218+
public void customTomcatCompressableMimeTypes() throws Exception {
200219
Map<String, String> map = new HashMap<String, String>();
201220
map.put("server.port", "0");
202-
map.put("server.tomcat.compression", "on");
221+
map.put("server.tomcat.compressableMimeTypes", "application/foo");
203222
bindProperties(map);
204223

205224
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
@@ -211,18 +230,23 @@ public void customTomcatCompression() throws Exception {
211230
try {
212231
AbstractHttp11Protocol<?> protocol = (AbstractHttp11Protocol<?>) container
213232
.getTomcat().getConnector().getProtocolHandler();
214-
assertEquals("on", protocol.getCompression());
233+
assertEquals("application/foo", protocol.getCompressableMimeTypes());
215234
}
216235
finally {
217236
container.stop();
218237
}
219238
}
220239

221-
@Test
222-
public void customTomcatCompressableMimeTypes() throws Exception {
240+
private void bindProperties(Map<String, String> map) {
241+
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues(
242+
map));
243+
}
244+
245+
private String configureCompression(String compression) {
223246
Map<String, String> map = new HashMap<String, String>();
224247
map.put("server.port", "0");
225-
map.put("server.tomcat.compressableMimeTypes", "application/foo");
248+
// YAML interprets "on" as true
249+
map.put("server.tomcat.compression", compression);
226250
bindProperties(map);
227251

228252
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
@@ -234,16 +258,11 @@ public void customTomcatCompressableMimeTypes() throws Exception {
234258
try {
235259
AbstractHttp11Protocol<?> protocol = (AbstractHttp11Protocol<?>) container
236260
.getTomcat().getConnector().getProtocolHandler();
237-
assertEquals("application/foo", protocol.getCompressableMimeTypes());
261+
return protocol.getCompression();
238262
}
239263
finally {
240264
container.stop();
241265
}
242266
}
243267

244-
private void bindProperties(Map<String, String> map) {
245-
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues(
246-
map));
247-
}
248-
249268
}

0 commit comments

Comments
 (0)