From 162b9e84a3d0c439bbbbca7784029fe8a79bdb40 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 26 Jul 2016 22:35:04 -0700 Subject: [PATCH] Polish --- .../web/HttpEncodingAutoConfiguration.java | 4 ++- .../HttpEncodingAutoConfigurationTests.java | 18 +++++----- .../json/AutoConfigureJsonTesters.java | 1 + .../boot/bind/RelaxedDataBinder.java | 1 - .../boot/bind/RelaxedNames.java | 24 +++++++++++++- .../bind/YamlJavaBeanPropertyConstructor.java | 3 +- ...tConfigurableEmbeddedServletContainer.java | 1 + .../JettyEmbeddedServletContainerFactory.java | 15 ++++++--- ...TomcatEmbeddedServletContainerFactory.java | 33 ++++++++++++++----- ...dertowEmbeddedServletContainerFactory.java | 15 ++++++--- ...yEmbeddedServletContainerFactoryTests.java | 1 + ...wEmbeddedServletContainerFactoryTests.java | 1 + 12 files changed, 87 insertions(+), 30 deletions(-) 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 index 0e6e639b971..58aa8fbdcf8 100644 --- 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 @@ -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; } + } } 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 index ac03ec2501c..0023e1b09f2 100644 --- 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 @@ -145,8 +145,8 @@ public class HttpEncodingAutoConfigurationTests { @Test public void noLocaleCharsetMapping() { load(EmptyConfiguration.class); - Map beans = - this.context.getBeansOfType(EmbeddedServletContainerCustomizer.class); + Map 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 beans = - this.context.getBeansOfType(EmbeddedServletContainerCustomizer.class); + Map 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(); } + } } diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/AutoConfigureJsonTesters.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/AutoConfigureJsonTesters.java index 2711787eaeb..48f851a5f11 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/AutoConfigureJsonTesters.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/AutoConfigureJsonTesters.java @@ -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) diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedDataBinder.java b/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedDataBinder.java index 44ba7ae7ee0..fc1ca4133a4 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedDataBinder.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedDataBinder.java @@ -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()) { diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedNames.java b/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedNames.java index a20cdaa312c..3e335e335ef 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedNames.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedNames.java @@ -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 { 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 { 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 { 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 { 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); diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/YamlJavaBeanPropertyConstructor.java b/spring-boot/src/main/java/org/springframework/boot/bind/YamlJavaBeanPropertyConstructor.java index f72c17803b5..63a8f588cbc 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/YamlJavaBeanPropertyConstructor.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/YamlJavaBeanPropertyConstructor.java @@ -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(); this.properties.put(type, typeMap); } - try { typeMap.put(alias, this.propertyUtils.getProperty(type, name)); } diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractConfigurableEmbeddedServletContainer.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractConfigurableEmbeddedServletContainer.java index 9b0e8b01758..790f10be16d 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractConfigurableEmbeddedServletContainer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractConfigurableEmbeddedServletContainer.java @@ -342,6 +342,7 @@ public abstract class AbstractConfigurableEmbeddedServletContainer return this.localeCharsetMappings; } + @Override public void setLocaleCharsetMappings(Map localeCharsetMappings) { Assert.notNull(localeCharsetMappings, "localeCharsetMappings must not be null"); this.localeCharsetMappings = localeCharsetMappings; diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java index a2600c17092..2845854061a 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java @@ -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 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); diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java index 4117307e259..87605aaef94 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java @@ -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 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"); diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactory.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactory.java index b4059ca9059..f28725d1fc3 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactory.java @@ -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 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); diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactoryTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactoryTests.java index b5aefc400bb..9ac8844d2f2 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactoryTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactoryTests.java @@ -335,4 +335,5 @@ public class JettyEmbeddedServletContainerFactoryTests String charsetName = context.getLocaleEncoding(locale); return (charsetName != null) ? Charset.forName(charsetName) : null; } + } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactoryTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactoryTests.java index 1f7264c070b..5c1be361aac 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactoryTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactoryTests.java @@ -277,4 +277,5 @@ public class UndertowEmbeddedServletContainerFactoryTests String charsetName = info.getLocaleCharsetMapping().get(locale.toString()); return (charsetName != null) ? Charset.forName(charsetName) : null; } + }