From 160f2d341f9e02c017f63c36348125dfb0631ae3 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 26 May 2015 15:00:54 +0200 Subject: [PATCH] Fix Gzip filter properties Fix `excludeAgentPatterns`, `excludePaths` and `excludePathPatterns` properties. Introduce `excludedMimeTypes` property. Fixes gh-3042 --- .../web/GzipFilterProperties.java | 52 ++++++++++++------- .../web/GzipFilterAutoConfigurationTests.java | 17 +++--- .../appendix-application-properties.adoc | 7 +-- 3 files changed, 48 insertions(+), 28 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/GzipFilterProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/GzipFilterProperties.java index c52912432d..1b238d6af7 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/GzipFilterProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/GzipFilterProperties.java @@ -30,6 +30,7 @@ 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") @@ -67,6 +68,11 @@ public class GzipFilterProperties { */ private List mimeTypes; + /** + * Comma-separated list of MIME types to exclude from compression. + */ + private List 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. @@ -77,19 +83,19 @@ public class GzipFilterProperties { * Comma-separated list of regular expression patterns to control user agents excluded * from compression. */ - private String excludedAgentPatterns; + 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 excludedPaths; + private String excludePaths; /** * Comma-separated list of regular expression patterns to control the paths that are * excluded from compression. */ - private String excludedPathPatterns; + private String excludePathPatterns; /** * Vary header sent on responses that may be compressed. @@ -156,6 +162,16 @@ public class GzipFilterProperties { this.mimeTypes = mimeTypes; } + public List getExcludedMimeTypes() { + return excludedMimeTypes; + } + + public void setExcludedMimeTypes(List excludedMimeTypes) { + this.addInitParameter("excludedMimeTypes", + StringUtils.collectionToCommaDelimitedString(excludedMimeTypes)); + this.excludedMimeTypes = excludedMimeTypes; + } + public String getExcludedAgents() { return this.excludedAgents; } @@ -165,31 +181,31 @@ public class GzipFilterProperties { this.excludedAgents = excludedAgents; } - public String getExcludedAgentPatterns() { - return this.excludedAgentPatterns; + public String getExcludeAgentPatterns() { + return this.excludeAgentPatterns; } - public void setExcludedAgentPatterns(String excludedAgentPatterns) { - this.addInitParameter("excludedAgentPatterns", excludedAgentPatterns); - this.excludedAgentPatterns = excludedAgentPatterns; + public void setExcludeAgentPatterns(String excludeAgentPatterns) { + this.addInitParameter("excludeAgentPatterns", excludeAgentPatterns); + this.excludeAgentPatterns = excludeAgentPatterns; } - public String getExcludedPaths() { - return this.excludedPaths; + public String getExcludePaths() { + return this.excludePaths; } - public void setExcludedPaths(String excludedPaths) { - this.addInitParameter("excludedPaths", excludedPaths); - this.excludedPaths = excludedPaths; + public void setExcludePaths(String excludePaths) { + this.addInitParameter("excludePaths", excludePaths); + this.excludePaths = excludePaths; } - public String getExcludedPathPatterns() { - return this.excludedPathPatterns; + public String getExcludePathPatterns() { + return this.excludePathPatterns; } - public void setExcludedPathPatterns(String excludedPathPatterns) { - this.addInitParameter("excludedPathPatterns", excludedPathPatterns); - this.excludedPathPatterns = excludedPathPatterns; + public void setExcludePathPatterns(String excludePathPatterns) { + this.addInitParameter("excludePathPatterns", excludePathPatterns); + this.excludePathPatterns = excludePathPatterns; } public String getVary() { diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/GzipFilterAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/GzipFilterAutoConfigurationTests.java index 727049577a..17a5d8306c 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/GzipFilterAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/GzipFilterAutoConfigurationTests.java @@ -69,14 +69,15 @@ public class GzipFilterAutoConfigurationTests { "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.excludedAgentPatterns:agent-pattern-1,agent-pattern-2", - "spring.http.gzip.excludedPaths:/static/", - "spring.http.gzip.excludedPathPatterns:path-pattern", + "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(12)); + assertThat(registrationBean.getInitParameters().size(), equalTo(13)); assertThat(registrationBean.getInitParameters().get("checkGzExists"), equalTo("false")); assertThat(registrationBean.getInitParameters().get("bufferSize"), @@ -91,13 +92,15 @@ public class GzipFilterAutoConfigurationTests { 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("excludedAgentPatterns"), + assertThat(registrationBean.getInitParameters().get("excludeAgentPatterns"), equalTo("agent-pattern-1,agent-pattern-2")); - assertThat(registrationBean.getInitParameters().get("excludedPaths"), + assertThat(registrationBean.getInitParameters().get("excludePaths"), equalTo("/static/")); - assertThat(registrationBean.getInitParameters().get("excludedPathPatterns"), + assertThat(registrationBean.getInitParameters().get("excludePathPatterns"), equalTo("path-pattern")); assertThat(registrationBean.getInitParameters().get("vary"), equalTo("vary-header-value")); diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 3f841bed85..b718a80ee5 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -126,11 +126,12 @@ content into your application; rather pick only the properties that you need. spring.http.gzip.deflate-no-wrap= # noWrap setting for deflate compression (true or false) spring.http.gzip.enabled=true # enable gzip filter support spring.http.gzip.excluded-agents= # comma-separated list of user agents to exclude from compression - spring.http.gzip.excluded-agent-patterns= # comma-separated list of regular expression patterns to control user agents excluded from compression - spring.http.gzip.excluded-paths= # comma-separated list of paths to exclude from compression - spring.http.gzip.excluded-path-patterns= # comma-separated list of regular expression patterns to control the paths that are excluded from compression + spring.http.gzip.exclude-agent-patterns= # comma-separated list of regular expression patterns to control user agents excluded from compression + spring.http.gzip.exclude-paths= # comma-separated list of paths to exclude from compression + spring.http.gzip.exclude-path-patterns= # comma-separated list of regular expression patterns to control the paths that are excluded from compression spring.http.gzip.methods= # comma-separated list of HTTP methods for which compression is enabled spring.http.gzip.mime-types= # comma-separated list of MIME types which should be compressed + spring.http.gzip.excluded-mime-types= # comma-separated list of MIME types to exclude from compression spring.http.gzip.min-gzip-size= # minimum content length required for compression to occur spring.http.gzip.vary= # Vary header to be sent on responses that may be compressed