diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index 69a50539abc..6565b7f76cb 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -307,6 +307,19 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord */ private String uriEncoding; + /** + * Controls response compression. Acceptable values are "off" to disable + * compression, "on" to enable compression of responses over 2048 bytes, "force" + * to force response compression, or an integer value to enable compression of + * responses with content length that is at least that value. + */ + private String compression = "off"; + + /** + * A comma-separated list of MIME types for which compression is used. + */ + private String compressableMimeTypes = "text/html,text/xml,text/plain"; + public int getMaxThreads() { return this.maxThreads; } @@ -355,6 +368,22 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord this.accessLogPattern = accessLogPattern; } + public String getCompressableMimeTypes() { + return this.compressableMimeTypes; + } + + public void setCompressableMimeTypes(String compressableMimeTypes) { + this.compressableMimeTypes = compressableMimeTypes; + } + + public String getCompression() { + return this.compression; + } + + public void setCompression(String compression) { + this.compression = compression; + } + public String getInternalProxies() { return this.internalProxies; } @@ -447,6 +476,19 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord }); } + factory.addConnectorCustomizers(new TomcatConnectorCustomizer() { + @Override + public void customize(Connector connector) { + ProtocolHandler handler = connector.getProtocolHandler(); + if (handler instanceof AbstractHttp11Protocol) { + @SuppressWarnings("rawtypes") + AbstractHttp11Protocol protocol = (AbstractHttp11Protocol) handler; + protocol.setCompression(Tomcat.this.compression); + protocol.setCompressableMimeTypes(Tomcat.this.compressableMimeTypes); + } + } + }); + if (this.accessLogEnabled) { AccessLogValve valve = new AccessLogValve(); String accessLogPattern = getAccessLogPattern(); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java index 66f75e86049..bd2a13d0659 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,10 +23,12 @@ import java.util.Map; import org.apache.catalina.Valve; import org.apache.catalina.valves.RemoteIpValve; +import org.apache.coyote.http11.AbstractHttp11Protocol; import org.junit.Test; import org.springframework.beans.MutablePropertyValues; import org.springframework.boot.bind.RelaxedDataBinder; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; +import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import static org.hamcrest.core.IsInstanceOf.instanceOf; @@ -193,6 +195,52 @@ public class ServerPropertiesTests { assertEquals("192.168.0.1", remoteIpValve.getInternalProxies()); } + @Test + public void customTomcatCompression() throws Exception { + Map map = new HashMap(); + map.put("server.port", "0"); + map.put("server.tomcat.compression", "on"); + bindProperties(map); + + TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); + this.properties.customize(factory); + + TomcatEmbeddedServletContainer container = (TomcatEmbeddedServletContainer) factory + .getEmbeddedServletContainer(); + + try { + AbstractHttp11Protocol protocol = (AbstractHttp11Protocol) container + .getTomcat().getConnector().getProtocolHandler(); + assertEquals("on", protocol.getCompression()); + } + finally { + container.stop(); + } + } + + @Test + public void customTomcatCompressableMimeTypes() throws Exception { + Map map = new HashMap(); + map.put("server.port", "0"); + map.put("server.tomcat.compressableMimeTypes", "application/foo"); + bindProperties(map); + + TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); + this.properties.customize(factory); + + TomcatEmbeddedServletContainer container = (TomcatEmbeddedServletContainer) factory + .getEmbeddedServletContainer(); + + try { + AbstractHttp11Protocol protocol = (AbstractHttp11Protocol) container + .getTomcat().getConnector().getProtocolHandler(); + assertEquals("application/foo", protocol.getCompressableMimeTypes()); + } + finally { + container.stop(); + } + } + private void bindProperties(Map map) { new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues( map));