Replace GzipFilter and Tomcat compression with general purpose approach
Closes gh-3296
This commit is contained in:
@@ -1,53 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web;
|
||||
|
||||
import org.eclipse.jetty.servlets.GzipFilter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.embedded.FilterRegistrationBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for {@link GzipFilter}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @since 1.2.2
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(GzipFilter.class)
|
||||
@EnableConfigurationProperties(GzipFilterProperties.class)
|
||||
public class GzipFilterAutoConfiguration {
|
||||
|
||||
@Autowired
|
||||
private GzipFilterProperties properties;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(prefix = "spring.http.gzip", name = "enabled", matchIfMissing = true)
|
||||
public FilterRegistrationBean gzipFilter() {
|
||||
FilterRegistrationBean registration = new FilterRegistrationBean(new GzipFilter());
|
||||
registration.addUrlPatterns("/*");
|
||||
registration.setInitParameters(this.properties.getAsInitParameters());
|
||||
|
||||
return registration;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,242 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.eclipse.jetty.servlets.GzipFilter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.util.MimeType;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Properties for configuring {@link GzipFilter}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.2
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "spring.http.gzip")
|
||||
public class GzipFilterProperties {
|
||||
|
||||
private final Map<String, String> initParameters = new HashMap<String, String>();
|
||||
|
||||
/**
|
||||
* Size of the output buffer in bytes.
|
||||
*/
|
||||
private Integer bufferSize;
|
||||
|
||||
/**
|
||||
* Minimum content length required for compression to occur.
|
||||
*/
|
||||
private Integer minGzipSize;
|
||||
|
||||
/**
|
||||
* Level used for deflate compression (0-9).
|
||||
*/
|
||||
private Integer deflateCompressionLevel;
|
||||
|
||||
/**
|
||||
* noWrap setting for deflate compression.
|
||||
*/
|
||||
private Boolean deflateNoWrap;
|
||||
|
||||
/**
|
||||
* Comma-separated list of HTTP methods for which compression is enabled.
|
||||
*/
|
||||
private List<HttpMethod> methods;
|
||||
|
||||
/**
|
||||
* Comma-separated list of MIME types which should be compressed.
|
||||
*/
|
||||
private List<MimeType> mimeTypes;
|
||||
|
||||
/**
|
||||
* Comma-separated list of MIME types to exclude from compression.
|
||||
*/
|
||||
private List<MimeType> excludedMimeTypes;
|
||||
|
||||
/**
|
||||
* Comma-separated list of user agents to exclude from compression. String.contains is
|
||||
* used to determine a match against the request's User-Agent header.
|
||||
*/
|
||||
private String excludedAgents;
|
||||
|
||||
/**
|
||||
* Comma-separated list of regular expression patterns to control user agents excluded
|
||||
* from compression.
|
||||
*/
|
||||
private String excludeAgentPatterns;
|
||||
|
||||
/**
|
||||
* Comma-separated list of paths to exclude from compression. Uses String.startsWith
|
||||
* to determine a match against the request's path.
|
||||
*/
|
||||
private String excludePaths;
|
||||
|
||||
/**
|
||||
* Comma-separated list of regular expression patterns to control the paths that are
|
||||
* excluded from compression.
|
||||
*/
|
||||
private String excludePathPatterns;
|
||||
|
||||
/**
|
||||
* Vary header sent on responses that may be compressed.
|
||||
*/
|
||||
private String vary;
|
||||
|
||||
public GzipFilterProperties() {
|
||||
this.addInitParameter("checkGzExists", false);
|
||||
}
|
||||
|
||||
public Integer getBufferSize() {
|
||||
return this.bufferSize;
|
||||
}
|
||||
|
||||
public void setBufferSize(Integer bufferSize) {
|
||||
this.addInitParameter("bufferSize", bufferSize);
|
||||
this.bufferSize = bufferSize;
|
||||
}
|
||||
|
||||
public Integer getMinGzipSize() {
|
||||
return this.minGzipSize;
|
||||
}
|
||||
|
||||
public void setMinGzipSize(Integer minGzipSize) {
|
||||
this.addInitParameter("minGzipSize", minGzipSize);
|
||||
this.minGzipSize = minGzipSize;
|
||||
}
|
||||
|
||||
public Integer getDeflateCompressionLevel() {
|
||||
return this.deflateCompressionLevel;
|
||||
}
|
||||
|
||||
public void setDeflateCompressionLevel(Integer deflateCompressionLevel) {
|
||||
this.addInitParameter("deflateCompressionLevel", deflateCompressionLevel);
|
||||
this.deflateCompressionLevel = deflateCompressionLevel;
|
||||
}
|
||||
|
||||
public Boolean getDeflateNoWrap() {
|
||||
return this.deflateNoWrap;
|
||||
}
|
||||
|
||||
public void setDeflateNoWrap(Boolean deflateNoWrap) {
|
||||
this.addInitParameter("deflateNoWrap", deflateNoWrap);
|
||||
this.deflateNoWrap = deflateNoWrap;
|
||||
}
|
||||
|
||||
public List<HttpMethod> getMethods() {
|
||||
return this.methods;
|
||||
}
|
||||
|
||||
public void setMethods(List<HttpMethod> methods) {
|
||||
this.addInitParameter("methods",
|
||||
StringUtils.collectionToCommaDelimitedString(methods));
|
||||
this.methods = methods;
|
||||
}
|
||||
|
||||
public List<MimeType> getMimeTypes() {
|
||||
return this.mimeTypes;
|
||||
}
|
||||
|
||||
public void setMimeTypes(List<MimeType> mimeTypes) {
|
||||
this.addInitParameter("mimeTypes",
|
||||
StringUtils.collectionToCommaDelimitedString(mimeTypes));
|
||||
this.mimeTypes = mimeTypes;
|
||||
}
|
||||
|
||||
public List<MimeType> getExcludedMimeTypes() {
|
||||
return this.excludedMimeTypes;
|
||||
}
|
||||
|
||||
public void setExcludedMimeTypes(List<MimeType> excludedMimeTypes) {
|
||||
this.addInitParameter("excludedMimeTypes",
|
||||
StringUtils.collectionToCommaDelimitedString(excludedMimeTypes));
|
||||
this.excludedMimeTypes = excludedMimeTypes;
|
||||
}
|
||||
|
||||
public String getExcludedAgents() {
|
||||
return this.excludedAgents;
|
||||
}
|
||||
|
||||
public void setExcludedAgents(String excludedAgents) {
|
||||
this.addInitParameter("excludedAgents", excludedAgents);
|
||||
this.excludedAgents = excludedAgents;
|
||||
}
|
||||
|
||||
public String getExcludeAgentPatterns() {
|
||||
return this.excludeAgentPatterns;
|
||||
}
|
||||
|
||||
public void setExcludeAgentPatterns(String excludeAgentPatterns) {
|
||||
this.addInitParameter("excludeAgentPatterns", excludeAgentPatterns);
|
||||
this.excludeAgentPatterns = excludeAgentPatterns;
|
||||
}
|
||||
|
||||
public String getExcludePaths() {
|
||||
return this.excludePaths;
|
||||
}
|
||||
|
||||
public void setExcludePaths(String excludePaths) {
|
||||
this.addInitParameter("excludePaths", excludePaths);
|
||||
this.excludePaths = excludePaths;
|
||||
}
|
||||
|
||||
public String getExcludePathPatterns() {
|
||||
return this.excludePathPatterns;
|
||||
}
|
||||
|
||||
public void setExcludePathPatterns(String excludePathPatterns) {
|
||||
this.addInitParameter("excludePathPatterns", excludePathPatterns);
|
||||
this.excludePathPatterns = excludePathPatterns;
|
||||
}
|
||||
|
||||
public String getVary() {
|
||||
return this.vary;
|
||||
}
|
||||
|
||||
public void setVary(String vary) {
|
||||
this.addInitParameter("vary", vary);
|
||||
this.vary = vary;
|
||||
}
|
||||
|
||||
Map<String, String> getAsInitParameters() {
|
||||
return this.initParameters;
|
||||
}
|
||||
|
||||
private void addInitParameter(String name, Integer value) {
|
||||
if (value != null) {
|
||||
this.initParameters.put(name, value.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private void addInitParameter(String name, Boolean value) {
|
||||
if (value != null) {
|
||||
this.initParameters.put(name, value.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private void addInitParameter(String name, String value) {
|
||||
if (value != null) {
|
||||
this.initParameters.put(name, value.toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -31,8 +31,7 @@ import org.apache.catalina.valves.RemoteIpValve;
|
||||
import org.apache.coyote.AbstractProtocol;
|
||||
import org.apache.coyote.ProtocolHandler;
|
||||
import org.apache.coyote.http11.AbstractHttp11Protocol;
|
||||
import org.springframework.boot.context.embedded.AbstractConfigurableEmbeddedServletContainer;
|
||||
import org.springframework.boot.context.embedded.AbstractConfigurableEmbeddedServletContainer.CompressionProperties;
|
||||
import org.springframework.boot.context.embedded.Compression;
|
||||
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor;
|
||||
@@ -101,7 +100,8 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
|
||||
|
||||
private final Undertow undertow = new Undertow();
|
||||
|
||||
private CompressionProperties compression = new CompressionProperties();
|
||||
@NestedConfigurationProperty
|
||||
private Compression compression = new Compression();
|
||||
|
||||
@NestedConfigurationProperty
|
||||
private JspServlet jspServlet;
|
||||
@@ -124,7 +124,7 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
|
||||
return this.undertow;
|
||||
}
|
||||
|
||||
public CompressionProperties getCompression() {
|
||||
public Compression getCompression() {
|
||||
return this.compression;
|
||||
}
|
||||
|
||||
@@ -247,9 +247,8 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
|
||||
if (getJspServlet() != null) {
|
||||
container.setJspServlet(getJspServlet());
|
||||
}
|
||||
if (container instanceof AbstractConfigurableEmbeddedServletContainer) {
|
||||
((AbstractConfigurableEmbeddedServletContainer) container)
|
||||
.setCompression(getCompression());
|
||||
if (getCompression() != null) {
|
||||
container.setCompression(getCompression());
|
||||
}
|
||||
if (container instanceof TomcatEmbeddedServletContainerFactory) {
|
||||
getTomcat()
|
||||
@@ -359,19 +358,6 @@ 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";
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
@@ -420,22 +406,6 @@ 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;
|
||||
}
|
||||
@@ -496,7 +466,6 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
|
||||
if (this.maxHttpHeaderSize > 0) {
|
||||
customizeMaxHttpHeaderSize(factory);
|
||||
}
|
||||
customizeCompression(factory);
|
||||
if (this.accessLogEnabled) {
|
||||
customizeAccessLog(factory);
|
||||
}
|
||||
@@ -565,33 +534,6 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
|
||||
});
|
||||
}
|
||||
|
||||
private void customizeCompression(TomcatEmbeddedServletContainerFactory factory) {
|
||||
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(coerceCompression(Tomcat.this.compression));
|
||||
protocol.setCompressableMimeTypes(Tomcat.this.compressableMimeTypes);
|
||||
}
|
||||
}
|
||||
|
||||
private String coerceCompression(String compression) {
|
||||
if ("true".equalsIgnoreCase(compression)) {
|
||||
return "on";
|
||||
}
|
||||
if ("false".equalsIgnoreCase(compression)) {
|
||||
return "off";
|
||||
}
|
||||
return compression;
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
private void customizeAccessLog(TomcatEmbeddedServletContainerFactory factory) {
|
||||
AccessLogValve valve = new AccessLogValve();
|
||||
String accessLogPattern = getAccessLogPattern();
|
||||
|
||||
@@ -68,7 +68,6 @@ org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.web.GzipFilterAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration,\
|
||||
|
||||
@@ -1,123 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.context.embedded.FilterRegistrationBean;
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link GzipFilterAutoConfiguration}
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class GzipFilterAutoConfigurationTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filterIsMappedToSlashStar() {
|
||||
createAndRefreshContext();
|
||||
FilterRegistrationBean registrationBean = this.context.getBean("gzipFilter",
|
||||
FilterRegistrationBean.class);
|
||||
assertThat(registrationBean.getUrlPatterns(), contains("/*"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void byDefaultCheckGzExistsIsTheOnlyInitParameter() {
|
||||
createAndRefreshContext();
|
||||
FilterRegistrationBean registrationBean = this.context.getBean("gzipFilter",
|
||||
FilterRegistrationBean.class);
|
||||
assertThat(registrationBean.getInitParameters().size(), equalTo(1));
|
||||
assertThat(registrationBean.getInitParameters().get("checkGzExists"),
|
||||
equalTo("false"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customInitParameterConfiguration() {
|
||||
createAndRefreshContext("spring.http.gzip.bufferSize:1234",
|
||||
"spring.http.gzip.minGzipSize:2345",
|
||||
"spring.http.gzip.deflateCompressionLevel:5",
|
||||
"spring.http.gzip.deflateNoWrap:false",
|
||||
"spring.http.gzip.methods:GET,POST",
|
||||
"spring.http.gzip.mimeTypes:application/foo,application/bar",
|
||||
"spring.http.gzip.excludedMimeTypes:application/biz",
|
||||
"spring.http.gzip.excludedAgents:excluded-agent-1,excluded-agent-2",
|
||||
"spring.http.gzip.excludeAgentPatterns:agent-pattern-1,agent-pattern-2",
|
||||
"spring.http.gzip.excludePaths:/static/",
|
||||
"spring.http.gzip.excludePathPatterns:path-pattern",
|
||||
"spring.http.gzip.vary:vary-header-value");
|
||||
FilterRegistrationBean registrationBean = this.context.getBean("gzipFilter",
|
||||
FilterRegistrationBean.class);
|
||||
assertThat(registrationBean.getInitParameters().size(), equalTo(13));
|
||||
assertThat(registrationBean.getInitParameters().get("checkGzExists"),
|
||||
equalTo("false"));
|
||||
assertThat(registrationBean.getInitParameters().get("bufferSize"),
|
||||
equalTo("1234"));
|
||||
assertThat(registrationBean.getInitParameters().get("minGzipSize"),
|
||||
equalTo("2345"));
|
||||
assertThat(registrationBean.getInitParameters().get("deflateCompressionLevel"),
|
||||
equalTo("5"));
|
||||
assertThat(registrationBean.getInitParameters().get("deflateNoWrap"),
|
||||
equalTo("false"));
|
||||
assertThat(registrationBean.getInitParameters().get("methods"),
|
||||
equalTo("GET,POST"));
|
||||
assertThat(registrationBean.getInitParameters().get("mimeTypes"),
|
||||
equalTo("application/foo,application/bar"));
|
||||
assertThat(registrationBean.getInitParameters().get("excludedMimeTypes"),
|
||||
equalTo("application/biz"));
|
||||
assertThat(registrationBean.getInitParameters().get("excludedAgents"),
|
||||
equalTo("excluded-agent-1,excluded-agent-2"));
|
||||
assertThat(registrationBean.getInitParameters().get("excludeAgentPatterns"),
|
||||
equalTo("agent-pattern-1,agent-pattern-2"));
|
||||
assertThat(registrationBean.getInitParameters().get("excludePaths"),
|
||||
equalTo("/static/"));
|
||||
assertThat(registrationBean.getInitParameters().get("excludePathPatterns"),
|
||||
equalTo("path-pattern"));
|
||||
assertThat(registrationBean.getInitParameters().get("vary"),
|
||||
equalTo("vary-header-value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filterCanBeDisabled() {
|
||||
createAndRefreshContext("spring.http.gzip.enabled:false");
|
||||
assertThat(this.context.getBeanNamesForType(FilterRegistrationBean.class).length,
|
||||
is(equalTo(0)));
|
||||
}
|
||||
|
||||
private void createAndRefreshContext(String... pairs) {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(this.context, pairs);
|
||||
this.context.register(GzipFilterAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,21 +23,16 @@ 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.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;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -106,18 +101,6 @@ public class ServerPropertiesTests {
|
||||
.getInternalProxies());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompressionBinding() throws Exception {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
map.put("server.compression.enabled", "true");
|
||||
map.put("server.compression.mimeTypes", "foo/bar");
|
||||
map.put("server.compression.minSize", "228");
|
||||
bindProperties(map);
|
||||
assertTrue(this.properties.getCompression().isEnabled());
|
||||
assertEquals("foo/bar", this.properties.getCompression().getMimeTypes());
|
||||
assertEquals(228, this.properties.getCompression().getMinSize());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeTomcat() throws Exception {
|
||||
ConfigurableEmbeddedServletContainer factory = mock(ConfigurableEmbeddedServletContainer.class);
|
||||
@@ -243,72 +226,9 @@ public class ServerPropertiesTests {
|
||||
assertEquals("192.168.0.1", remoteIpValve.getInternalProxies());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTomcatCompression() throws Exception {
|
||||
assertThat("on", is(equalTo(configureCompression("on"))));
|
||||
}
|
||||
|
||||
@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"))));
|
||||
}
|
||||
|
||||
@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
|
||||
public void customTomcatCompressableMimeTypes() throws Exception {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
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<String, String> map) {
|
||||
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues(
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user