001/*
002 * Copyright 2012-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.web;
018
019import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
020import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
021import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
022import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
023import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
024import org.springframework.boot.autoconfigure.web.HttpEncodingProperties.Type;
025import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
026import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
027import org.springframework.boot.context.properties.EnableConfigurationProperties;
028import org.springframework.boot.web.filter.OrderedCharacterEncodingFilter;
029import org.springframework.context.annotation.Bean;
030import org.springframework.context.annotation.Configuration;
031import org.springframework.core.Ordered;
032import org.springframework.web.filter.CharacterEncodingFilter;
033
034/**
035 * {@link EnableAutoConfiguration Auto-configuration} for configuring the encoding to use
036 * in web applications.
037 *
038 * @author Stephane Nicoll
039 * @author Brian Clozel
040 * @since 1.2.0
041 */
042@Configuration
043@EnableConfigurationProperties(HttpEncodingProperties.class)
044@ConditionalOnWebApplication
045@ConditionalOnClass(CharacterEncodingFilter.class)
046@ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true)
047public class HttpEncodingAutoConfiguration {
048
049        private final HttpEncodingProperties properties;
050
051        public HttpEncodingAutoConfiguration(HttpEncodingProperties properties) {
052                this.properties = properties;
053        }
054
055        @Bean
056        @ConditionalOnMissingBean(CharacterEncodingFilter.class)
057        public CharacterEncodingFilter characterEncodingFilter() {
058                CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
059                filter.setEncoding(this.properties.getCharset().name());
060                filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST));
061                filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE));
062                return filter;
063        }
064
065        @Bean
066        public LocaleCharsetMappingsCustomizer localeCharsetMappingsCustomizer() {
067                return new LocaleCharsetMappingsCustomizer(this.properties);
068        }
069
070        private static class LocaleCharsetMappingsCustomizer
071                        implements EmbeddedServletContainerCustomizer, Ordered {
072
073                private final HttpEncodingProperties properties;
074
075                LocaleCharsetMappingsCustomizer(HttpEncodingProperties properties) {
076                        this.properties = properties;
077                }
078
079                @Override
080                public void customize(ConfigurableEmbeddedServletContainer container) {
081                        if (this.properties.getMapping() != null) {
082                                container.setLocaleCharsetMappings(this.properties.getMapping());
083                        }
084                }
085
086                @Override
087                public int getOrder() {
088                        return 0;
089                }
090
091        }
092
093}