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

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

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

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

@ -276,7 +276,6 @@ public class RelaxedDataBinder extends DataBinder {
if (path.name(++index) == null) { if (path.name(++index) == null) {
return path.toString(); return path.toString();
} }
String name = path.prefix(index); String name = path.prefix(index);
TypeDescriptor descriptor = wrapper.getPropertyTypeDescriptor(name); TypeDescriptor descriptor = wrapper.getPropertyTypeDescriptor(name);
if (descriptor == null || descriptor.isMap()) { 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 { enum Variation {
NONE { NONE {
@Override @Override
public String apply(String value) { public String apply(String value) {
return value; return value;
} }
}, },
LOWERCASE { LOWERCASE {
@Override @Override
public String apply(String value) { public String apply(String value) {
return value.toLowerCase(); return value.toLowerCase();
} }
}, },
UPPERCASE { UPPERCASE {
@Override @Override
public String apply(String value) { public String apply(String value) {
return value.toUpperCase(); return value.toUpperCase();
} }
}; };
public abstract String apply(String value); public abstract String apply(String value);
@ -109,34 +115,43 @@ public final class RelaxedNames implements Iterable<String> {
enum Manipulation { enum Manipulation {
NONE { NONE {
@Override @Override
public String apply(String value) { public String apply(String value) {
return value; return value;
} }
}, },
HYPHEN_TO_UNDERSCORE { HYPHEN_TO_UNDERSCORE {
@Override @Override
public String apply(String value) { public String apply(String value) {
return value.replace("-", "_"); return value.replace("-", "_");
} }
}, },
UNDERSCORE_TO_PERIOD { UNDERSCORE_TO_PERIOD {
@Override @Override
public String apply(String value) { public String apply(String value) {
return value.replace("_", "."); return value.replace("_", ".");
} }
}, },
PERIOD_TO_UNDERSCORE { PERIOD_TO_UNDERSCORE {
@Override @Override
public String apply(String value) { public String apply(String value) {
return value.replace(".", "_"); return value.replace(".", "_");
} }
}, },
CAMELCASE_TO_UNDERSCORE { CAMELCASE_TO_UNDERSCORE {
@Override @Override
public String apply(String value) { public String apply(String value) {
Matcher matcher = CAMEL_CASE_PATTERN.matcher(value); Matcher matcher = CAMEL_CASE_PATTERN.matcher(value);
@ -148,9 +163,11 @@ public final class RelaxedNames implements Iterable<String> {
matcher.appendTail(result); matcher.appendTail(result);
return result.toString(); return result.toString();
} }
}, },
CAMELCASE_TO_HYPHEN { CAMELCASE_TO_HYPHEN {
@Override @Override
public String apply(String value) { public String apply(String value) {
Matcher matcher = CAMEL_CASE_PATTERN.matcher(value); Matcher matcher = CAMEL_CASE_PATTERN.matcher(value);
@ -162,20 +179,25 @@ public final class RelaxedNames implements Iterable<String> {
matcher.appendTail(result); matcher.appendTail(result);
return result.toString(); return result.toString();
} }
}, },
SEPARATED_TO_CAMELCASE { SEPARATED_TO_CAMELCASE {
@Override @Override
public String apply(String value) { public String apply(String value) {
return separatedToCamelCase(value, false); return separatedToCamelCase(value, false);
} }
}, },
CASE_INSENSITIVE_SEPARATED_TO_CAMELCASE { CASE_INSENSITIVE_SEPARATED_TO_CAMELCASE {
@Override @Override
public String apply(String value) { public String apply(String value) {
return separatedToCamelCase(value, true); return separatedToCamelCase(value, true);
} }
}; };
public abstract String apply(String value); 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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>(); typeMap = new HashMap<String, Property>();
this.properties.put(type, typeMap); this.properties.put(type, typeMap);
} }
try { try {
typeMap.put(alias, this.propertyUtils.getProperty(type, name)); typeMap.put(alias, this.propertyUtils.getProperty(type, name));
} }

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

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

@ -27,6 +27,7 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -194,15 +195,8 @@ public class TomcatEmbeddedServletContainerFactory
context.setParentClassLoader( context.setParentClassLoader(
this.resourceLoader != null ? this.resourceLoader.getClassLoader() this.resourceLoader != null ? this.resourceLoader.getClassLoader()
: ClassUtils.getDefaultClassLoader()); : ClassUtils.getDefaultClassLoader());
// override defaults, see org.apache.catalina.util.CharsetMapperDefault.properties resetDefaultLocaleMapping(context);
context.addLocaleEncodingMappingParameter(Locale.ENGLISH.toString(), addLocaleMappings(context);
DEFAULT_CHARSET.displayName());
context.addLocaleEncodingMappingParameter(Locale.FRENCH.toString(),
DEFAULT_CHARSET.displayName());
for (Locale locale : getLocaleCharsetMappings().keySet()) {
context.addLocaleEncodingMappingParameter(locale.toString(),
getLocaleCharsetMappings().get(locale).toString());
}
try { try {
context.setUseRelativeRedirects(false); context.setUseRelativeRedirects(false);
} }
@ -228,6 +222,27 @@ public class TomcatEmbeddedServletContainerFactory
postProcessContext(context); 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) { private void addDefaultServlet(Context context) {
Wrapper defaultServlet = context.createWrapper(); Wrapper defaultServlet = context.createWrapper();
defaultServlet.setName("default"); defaultServlet.setName("default");

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

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

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

Loading…
Cancel
Save