diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpEncodingAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpEncodingAutoConfiguration.java new file mode 100644 index 0000000000..1064e6c0a9 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpEncodingAutoConfiguration.java @@ -0,0 +1,57 @@ +/* + * Copyright 2012-2014 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.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.web.filter.CharacterEncodingFilter; + +/** + * {@link EnableAutoConfiguration Auto-configuration} for configuring the + * encoding to use in web applications. + * + * @author Stephane Nicoll + * @since 1.2.0 + */ +@Configuration +@EnableConfigurationProperties(HttpEncodingProperties.class) +@ConditionalOnClass(CharacterEncodingFilter.class) +@ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true) +public class HttpEncodingAutoConfiguration { + + @Autowired + private HttpEncodingProperties httpEncodingProperties; + + @ConditionalOnMissingBean + @Bean + @Order(Ordered.HIGHEST_PRECEDENCE) + public CharacterEncodingFilter characterEncodingFilter() { + CharacterEncodingFilter filter = new CharacterEncodingFilter(); + filter.setEncoding(httpEncodingProperties.getCharset().name()); + filter.setForceEncoding(httpEncodingProperties.isForce()); + return filter; + } + +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpEncodingProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpEncodingProperties.java new file mode 100644 index 0000000000..d16e612f55 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpEncodingProperties.java @@ -0,0 +1,60 @@ +/* + * Copyright 2012-2014 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.nio.charset.Charset; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Configuration properties for http encoding. + * + * @author Stephane Nicoll + * @since 1.2.0 + */ +@ConfigurationProperties(prefix = "spring.http.encoding") +public class HttpEncodingProperties { + + public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); + + /** + * The Charset to use. + */ + private Charset charset = DEFAULT_CHARSET; + + /** + * To force the encoding to the configured charset typically on HTTP requests and responses. + */ + private boolean force = true; + + public Charset getCharset() { + return charset; + } + + public void setCharset(Charset charset) { + this.charset = charset; + } + + public boolean isForce() { + return force; + } + + public void setForce(boolean force) { + this.force = force; + } + +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfiguration.java index 9e03aa64aa..6c35b56cc2 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfiguration.java @@ -16,7 +16,6 @@ package org.springframework.boot.autoconfigure.web; -import java.nio.charset.Charset; import java.util.Collections; import java.util.List; @@ -55,7 +54,6 @@ import com.google.gson.Gson; @ConditionalOnClass(HttpMessageConverter.class) public class HttpMessageConvertersAutoConfiguration { - private static final Charset UTF_8 = Charset.forName("UTF-8"); @Autowired(required = false) private final List> converters = Collections.emptyList(); @@ -125,12 +123,16 @@ public class HttpMessageConvertersAutoConfiguration { @Configuration @ConditionalOnClass(StringHttpMessageConverter.class) + @EnableConfigurationProperties(HttpEncodingProperties.class) protected static class StringHttpMessageConverterConfiguration { + @Autowired + private HttpEncodingProperties httpEncodingProperties; + @Bean @ConditionalOnMissingBean public StringHttpMessageConverter stringHttpMessageConverter() { - return new StringHttpMessageConverter(UTF_8); + return new StringHttpMessageConverter(httpEncodingProperties.getCharset()); } } diff --git a/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index da9f04f279..97c2ffac69 100644 --- a/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -53,6 +53,12 @@ "description": "Automatically enable Solr repositories.", "defaultValue": true }, + { + "name": "spring.http.encoding.enabled", + "dataType": "java.lang.Boolean", + "description": "Enable http encoding support.", + "defaultValue": true + }, { "name": "spring.jmx.enabled", "dataType": "java.lang.Boolean", diff --git a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories index 9b88304c1a..c3459b716a 100644 --- a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -60,6 +60,7 @@ org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfigura org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration,\ org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration,\ org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration,\ +org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration,\ org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration,\ org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration,\ org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration,\ diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/HttpEncodingAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/HttpEncodingAutoConfigurationTests.java new file mode 100644 index 0000000000..9e29910175 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/HttpEncodingAutoConfigurationTests.java @@ -0,0 +1,119 @@ +/* + * Copyright 2012-2014 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.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import org.springframework.beans.DirectFieldAccessor; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.boot.test.EnvironmentTestUtils; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.filter.CharacterEncodingFilter; + +import static org.junit.Assert.assertEquals; + +/** + * Tests for {@link HttpEncodingAutoConfiguration} + * + * @author Stephane Nicoll + */ +public class HttpEncodingAutoConfigurationTests { + + @Rule + public final ExpectedException thrown = ExpectedException.none(); + + private AnnotationConfigApplicationContext context; + + @After + public void close() { + if (this.context != null) { + this.context.close(); + } + } + + @Test + public void defaultConfiguration() { + load(EmptyConfiguration.class); + CharacterEncodingFilter filter = context.getBean(CharacterEncodingFilter.class); + assertCharacterEncodingFilter(filter, "UTF-8", true); + } + + @Test + public void disableConfiguration() { + load(EmptyConfiguration.class, "spring.http.encoding.enabled:false"); + + thrown.expect(NoSuchBeanDefinitionException.class); + context.getBean(CharacterEncodingFilter.class); + } + + @Test + public void customConfiguration() { + load(EmptyConfiguration.class, "spring.http.encoding.charset:ISO-8859-15", "spring.http.encoding.force:false"); + CharacterEncodingFilter filter = context.getBean(CharacterEncodingFilter.class); + assertCharacterEncodingFilter(filter, "ISO-8859-15", false); + } + + @Test + public void customFilterConfiguration() { + load(FilterConfiguration.class, "spring.http.encoding.charset:ISO-8859-15", "spring.http.encoding.force:false"); + CharacterEncodingFilter filter = context.getBean(CharacterEncodingFilter.class); + assertCharacterEncodingFilter(filter, "US-ASCII", false); + } + + private void assertCharacterEncodingFilter(CharacterEncodingFilter actual, String encoding, boolean forceEncoding) { + DirectFieldAccessor accessor = new DirectFieldAccessor(actual); + assertEquals("Wrong encoding", encoding, accessor.getPropertyValue("encoding")); + assertEquals("Wrong forceEncoding flag", forceEncoding, accessor.getPropertyValue("forceEncoding")); + } + + private void load(Class config, String... environment) { + this.context = doLoad(new Class[] {config}, environment); + } + + private AnnotationConfigApplicationContext doLoad(Class[] configs, + String... environment) { + AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(applicationContext, environment); + applicationContext.register(configs); + applicationContext.register(HttpEncodingAutoConfiguration.class); + applicationContext.refresh(); + return applicationContext; + } + + + @Configuration + static class EmptyConfiguration { + } + + @Configuration + static class FilterConfiguration { + + @Bean + public CharacterEncodingFilter myCharacterEncodingFilter() { + CharacterEncodingFilter filter = new CharacterEncodingFilter(); + filter.setEncoding("US-ASCII"); + filter.setForceEncoding(false); + return filter; + } + } + +} 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 f3c2d87f53..3e1bbbae77 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -93,6 +93,10 @@ content into your application; rather pick only the properties that you need. spring.resources.cache-period= # cache timeouts in headers sent to browser spring.resources.add-mappings=true # if default mappings should be added + # HTTP encoding ({sc-spring-boot-autoconfigure}/web/HttpEncodingProperties.{sc-ext}[HttpEncodingProperties]) + spring.http.encoding=UTF-8 # the encoding of HTTP requests/responses + spring.http.force=true # force the configured encoding + # JACKSON ({sc-spring-boot-autoconfigure}}/jackson/JacksonProperties.{sc-ext}[JacksonProperties]) spring.jackson.date-format= # Date format string (e.g. yyyy-MM-dd HH:mm:ss), or a fully-qualified date format class name (e.g. com.fasterxml.jackson.databind.util.ISO8601DateFormat) spring.jackson.property-naming-strategy= # One of the constants on Jackson's PropertyNamingStrategy (e.g. CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES) or the fully-qualified class name of a PropertyNamingStrategy subclass diff --git a/spring-boot-samples/spring-boot-sample-web-static/src/test/java/sample/ui/SampleWebStaticApplicationTests.java b/spring-boot-samples/spring-boot-sample-web-static/src/test/java/sample/ui/SampleWebStaticApplicationTests.java index 851e415733..fed68bab27 100644 --- a/spring-boot-samples/spring-boot-sample-web-static/src/test/java/sample/ui/SampleWebStaticApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-web-static/src/test/java/sample/ui/SampleWebStaticApplicationTests.java @@ -64,7 +64,7 @@ public class SampleWebStaticApplicationTests { assertEquals(HttpStatus.OK, entity.getStatusCode()); assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body")); assertEquals("Wrong content type:\n" + entity.getHeaders().getContentType(), - MediaType.valueOf("text/css"), entity.getHeaders().getContentType()); + MediaType.valueOf("text/css;charset=UTF-8"), entity.getHeaders().getContentType()); } }