Fix Gzip filter properties

Fix `excludeAgentPatterns`, `excludePaths` and `excludePathPatterns`
properties. Introduce `excludedMimeTypes` property.

Fixes gh-3042
This commit is contained in:
Stephane Nicoll 2015-05-26 15:00:54 +02:00
parent 676b7d713a
commit 160f2d341f
3 changed files with 48 additions and 28 deletions

View File

@ -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<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.
@ -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<MimeType> getExcludedMimeTypes() {
return excludedMimeTypes;
}
public void setExcludedMimeTypes(List<MimeType> 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() {

View File

@ -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"));

View File

@ -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