pull/6486/head
Phillip Webb 8 years ago
parent 618e6d6bf4
commit 162b9e84a3

@ -67,7 +67,8 @@ public class HttpEncodingAutoConfiguration {
return new LocaleCharsetMappingsCustomizer(this.properties);
}
private static class LocaleCharsetMappingsCustomizer implements EmbeddedServletContainerCustomizer, Ordered {
private static class LocaleCharsetMappingsCustomizer
implements EmbeddedServletContainerCustomizer, Ordered {
private final HttpEncodingProperties properties;
@ -86,6 +87,7 @@ public class HttpEncodingAutoConfiguration {
public int getOrder() {
return 0;
}
}
}

@ -145,8 +145,8 @@ public class HttpEncodingAutoConfigurationTests {
@Test
public void noLocaleCharsetMapping() {
load(EmptyConfiguration.class);
Map<String, EmbeddedServletContainerCustomizer> beans =
this.context.getBeansOfType(EmbeddedServletContainerCustomizer.class);
Map<String, EmbeddedServletContainerCustomizer> beans = this.context
.getBeansOfType(EmbeddedServletContainerCustomizer.class);
assertThat(beans.size()).isEqualTo(1);
assertThat(this.context.getBean(MockEmbeddedServletContainerFactory.class)
.getLocaleCharsetMappings().size()).isEqualTo(0);
@ -156,15 +156,17 @@ public class HttpEncodingAutoConfigurationTests {
public void customLocaleCharsetMappings() {
load(EmptyConfiguration.class, "spring.http.encoding.mapping.en:UTF-8",
"spring.http.encoding.mapping.fr_FR:UTF-8");
Map<String, EmbeddedServletContainerCustomizer> beans =
this.context.getBeansOfType(EmbeddedServletContainerCustomizer.class);
Map<String, EmbeddedServletContainerCustomizer> beans = this.context
.getBeansOfType(EmbeddedServletContainerCustomizer.class);
assertThat(beans.size()).isEqualTo(1);
assertThat(this.context.getBean(MockEmbeddedServletContainerFactory.class)
.getLocaleCharsetMappings().size()).isEqualTo(2);
assertThat(this.context.getBean(MockEmbeddedServletContainerFactory.class)
.getLocaleCharsetMappings().get(Locale.ENGLISH)).isEqualTo(Charset.forName("UTF-8"));
.getLocaleCharsetMappings().get(Locale.ENGLISH))
.isEqualTo(Charset.forName("UTF-8"));
assertThat(this.context.getBean(MockEmbeddedServletContainerFactory.class)
.getLocaleCharsetMappings().get(Locale.FRANCE)).isEqualTo(Charset.forName("UTF-8"));
.getLocaleCharsetMappings().get(Locale.FRANCE))
.isEqualTo(Charset.forName("UTF-8"));
}
private void assertCharacterEncodingFilter(CharacterEncodingFilter actual,
@ -233,10 +235,10 @@ public class HttpEncodingAutoConfigurationTests {
}
@Bean
public EmbeddedServletContainerCustomizerBeanPostProcessor
embeddedServletContainerCustomizerBeanPostProcessor() {
public EmbeddedServletContainerCustomizerBeanPostProcessor embeddedServletContainerCustomizerBeanPostProcessor() {
return new EmbeddedServletContainerCustomizerBeanPostProcessor();
}
}
}

@ -33,6 +33,7 @@ import org.springframework.boot.test.json.JacksonTester;
* auto-configuration of JSON testers.
*
* @author Phillip Webb
* @since 1.4.0
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)

@ -276,7 +276,6 @@ public class RelaxedDataBinder extends DataBinder {
if (path.name(++index) == null) {
return path.toString();
}
String name = path.prefix(index);
TypeDescriptor descriptor = wrapper.getPropertyTypeDescriptor(name);
if (descriptor == null || descriptor.isMap()) {

@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 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.
@ -79,24 +79,30 @@ public final class RelaxedNames implements Iterable<String> {
enum Variation {
NONE {
@Override
public String apply(String value) {
return value;
}
},
LOWERCASE {
@Override
public String apply(String value) {
return value.toLowerCase();
}
},
UPPERCASE {
@Override
public String apply(String value) {
return value.toUpperCase();
}
};
public abstract String apply(String value);
@ -109,34 +115,43 @@ public final class RelaxedNames implements Iterable<String> {
enum Manipulation {
NONE {
@Override
public String apply(String value) {
return value;
}
},
HYPHEN_TO_UNDERSCORE {
@Override
public String apply(String value) {
return value.replace("-", "_");
}
},
UNDERSCORE_TO_PERIOD {
@Override
public String apply(String value) {
return value.replace("_", ".");
}
},
PERIOD_TO_UNDERSCORE {
@Override
public String apply(String value) {
return value.replace(".", "_");
}
},
CAMELCASE_TO_UNDERSCORE {
@Override
public String apply(String value) {
Matcher matcher = CAMEL_CASE_PATTERN.matcher(value);
@ -148,9 +163,11 @@ public final class RelaxedNames implements Iterable<String> {
matcher.appendTail(result);
return result.toString();
}
},
CAMELCASE_TO_HYPHEN {
@Override
public String apply(String value) {
Matcher matcher = CAMEL_CASE_PATTERN.matcher(value);
@ -162,20 +179,25 @@ public final class RelaxedNames implements Iterable<String> {
matcher.appendTail(result);
return result.toString();
}
},
SEPARATED_TO_CAMELCASE {
@Override
public String apply(String value) {
return separatedToCamelCase(value, false);
}
},
CASE_INSENSITIVE_SEPARATED_TO_CAMELCASE {
@Override
public String apply(String value) {
return separatedToCamelCase(value, true);
}
};
public abstract String apply(String value);

@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2016 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.
@ -69,7 +69,6 @@ public class YamlJavaBeanPropertyConstructor extends Constructor {
typeMap = new HashMap<String, Property>();
this.properties.put(type, typeMap);
}
try {
typeMap.put(alias, this.propertyUtils.getProperty(type, name));
}

@ -342,6 +342,7 @@ public abstract class AbstractConfigurableEmbeddedServletContainer
return this.localeCharsetMappings;
}
@Override
public void setLocaleCharsetMappings(Map<Locale, Charset> localeCharsetMappings) {
Assert.notNull(localeCharsetMappings, "localeCharsetMappings must not be null");
this.localeCharsetMappings = localeCharsetMappings;

@ -20,12 +20,14 @@ import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
@ -355,10 +357,7 @@ public class JettyEmbeddedServletContainerFactory
addJspServlet(context);
context.addBean(new JasperInitializer(context), true);
}
for (Locale locale : getLocaleCharsetMappings().keySet()) {
context.addLocaleEncoding(locale.toString(),
getLocaleCharsetMappings().get(locale).toString());
}
addLocaleMappings(context);
ServletContextInitializer[] initializersToUse = mergeInitializers(initializers);
Configuration[] configurations = getWebAppContextConfigurations(context,
initializersToUse);
@ -367,6 +366,14 @@ public class JettyEmbeddedServletContainerFactory
postProcessWebAppContext(context);
}
private void addLocaleMappings(WebAppContext context) {
for (Map.Entry<Locale, Charset> entry : getLocaleCharsetMappings().entrySet()) {
Locale locale = entry.getKey();
Charset charset = entry.getValue();
context.addLocaleEncoding(locale.toString(), charset.toString());
}
}
private void configureSession(WebAppContext context) {
SessionManager sessionManager = context.getSessionHandler().getSessionManager();
int sessionTimeout = (getSessionTimeout() > 0 ? getSessionTimeout() : -1);

@ -27,6 +27,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@ -194,15 +195,8 @@ public class TomcatEmbeddedServletContainerFactory
context.setParentClassLoader(
this.resourceLoader != null ? this.resourceLoader.getClassLoader()
: ClassUtils.getDefaultClassLoader());
// override defaults, see org.apache.catalina.util.CharsetMapperDefault.properties
context.addLocaleEncodingMappingParameter(Locale.ENGLISH.toString(),
DEFAULT_CHARSET.displayName());
context.addLocaleEncodingMappingParameter(Locale.FRENCH.toString(),
DEFAULT_CHARSET.displayName());
for (Locale locale : getLocaleCharsetMappings().keySet()) {
context.addLocaleEncodingMappingParameter(locale.toString(),
getLocaleCharsetMappings().get(locale).toString());
}
resetDefaultLocaleMapping(context);
addLocaleMappings(context);
try {
context.setUseRelativeRedirects(false);
}
@ -228,6 +222,27 @@ public class TomcatEmbeddedServletContainerFactory
postProcessContext(context);
}
/**
* Override Tomcat's default locale mappings to align with other containers. See
* {@code org.apache.catalina.util.CharsetMapperDefault.properties}.
* @param context the context to reset
*/
private void resetDefaultLocaleMapping(TomcatEmbeddedContext context) {
context.addLocaleEncodingMappingParameter(Locale.ENGLISH.toString(),
DEFAULT_CHARSET.displayName());
context.addLocaleEncodingMappingParameter(Locale.FRENCH.toString(),
DEFAULT_CHARSET.displayName());
}
private void addLocaleMappings(TomcatEmbeddedContext context) {
for (Map.Entry<Locale, Charset> entry : getLocaleCharsetMappings().entrySet()) {
Locale locale = entry.getKey();
Charset charset = entry.getValue();
context.addLocaleEncodingMappingParameter(locale.toString(),
charset.toString());
}
}
private void addDefaultServlet(Context context) {
Wrapper defaultServlet = context.createWrapper();
defaultServlet.setName("default");

@ -19,6 +19,7 @@ package org.springframework.boot.context.embedded.undertow;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
@ -28,6 +29,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.net.ssl.KeyManager;
@ -383,10 +385,7 @@ public class UndertowEmbeddedServletContainerFactory
File dir = getValidSessionStoreDir();
deployment.setSessionPersistenceManager(new FileSessionPersistence(dir));
}
for (Locale locale : getLocaleCharsetMappings().keySet()) {
deployment.addLocaleCharsetMapping(locale.toString(),
getLocaleCharsetMappings().get(locale).toString());
}
addLocaleMappings(deployment);
DeploymentManager manager = Servlets.newContainer().addDeployment(deployment);
manager.deploy();
SessionManager sessionManager = manager.getDeployment().getSessionManager();
@ -435,6 +434,14 @@ public class UndertowEmbeddedServletContainerFactory
OptionMap.builder().set(Options.THREAD_DAEMON, true).getMap());
}
private void addLocaleMappings(DeploymentInfo deployment) {
for (Map.Entry<Locale, Charset> entry : getLocaleCharsetMappings().entrySet()) {
Locale locale = entry.getKey();
Charset charset = entry.getValue();
deployment.addLocaleCharsetMapping(locale.toString(), charset.toString());
}
}
private void registerServletContainerInitializerToDriveServletContextInitializers(
DeploymentInfo deployment, ServletContextInitializer... initializers) {
ServletContextInitializer[] mergedInitializers = mergeInitializers(initializers);

@ -335,4 +335,5 @@ public class JettyEmbeddedServletContainerFactoryTests
String charsetName = context.getLocaleEncoding(locale);
return (charsetName != null) ? Charset.forName(charsetName) : null;
}
}

@ -277,4 +277,5 @@ public class UndertowEmbeddedServletContainerFactoryTests
String charsetName = info.getLocaleCharsetMapping().get(locale.toString());
return (charsetName != null) ? Charset.forName(charsetName) : null;
}
}

Loading…
Cancel
Save