From 7d4e558f8e89abfce459c4ba00609a34cfe880a3 Mon Sep 17 00:00:00 2001 From: Huang YunKun Date: Thu, 25 Jan 2018 21:46:25 +0800 Subject: [PATCH 1/2] Use PropertyMapper to configure WebServerFactory See gh-11773 --- ...ultReactiveWebServerFactoryCustomizer.java | 44 +++++------- ...aultServletWebServerFactoryCustomizer.java | 69 +++++++------------ ...activeWebServerFactoryCustomizerTests.java | 12 ++++ ...ervletWebServerFactoryCustomizerTests.java | 22 ++++++ 4 files changed, 77 insertions(+), 70 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/DefaultReactiveWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/DefaultReactiveWebServerFactoryCustomizer.java index efc074b874c..519f0725aec 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/DefaultReactiveWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/DefaultReactiveWebServerFactoryCustomizer.java @@ -20,6 +20,7 @@ import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.embedded.jetty.JettyCustomizer; import org.springframework.boot.autoconfigure.web.embedded.tomcat.TomcatCustomizer; import org.springframework.boot.autoconfigure.web.embedded.undertow.UndertowCustomizer; +import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.boot.web.embedded.jetty.JettyReactiveWebServerFactory; import org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFactory; import org.springframework.boot.web.embedded.undertow.UndertowReactiveWebServerFactory; @@ -33,6 +34,7 @@ import org.springframework.core.env.Environment; * Default {@link WebServerFactoryCustomizer} for reactive servers. * * @author Brian Clozel + * @author Yunkun Huang * @since 2.0.0 */ public class DefaultReactiveWebServerFactoryCustomizer @@ -59,33 +61,21 @@ public class DefaultReactiveWebServerFactoryCustomizer @Override public void customize(ConfigurableReactiveWebServerFactory factory) { - if (this.serverProperties.getPort() != null) { - factory.setPort(this.serverProperties.getPort()); - } - if (this.serverProperties.getAddress() != null) { - factory.setAddress(this.serverProperties.getAddress()); - } - if (this.serverProperties.getSsl() != null) { - factory.setSsl(this.serverProperties.getSsl()); - } - if (this.serverProperties.getCompression() != null) { - factory.setCompression(this.serverProperties.getCompression()); - } - if (this.serverProperties.getHttp2() != null) { - factory.setHttp2(this.serverProperties.getHttp2()); - } - if (factory instanceof TomcatReactiveWebServerFactory) { - TomcatCustomizer.customizeTomcat(this.serverProperties, this.environment, - (TomcatReactiveWebServerFactory) factory); - } - if (factory instanceof JettyReactiveWebServerFactory) { - JettyCustomizer.customizeJetty(this.serverProperties, this.environment, - (JettyReactiveWebServerFactory) factory); - } - if (factory instanceof UndertowReactiveWebServerFactory) { - UndertowCustomizer.customizeUndertow(this.serverProperties, this.environment, - (UndertowReactiveWebServerFactory) factory); - } + PropertyMapper map = PropertyMapper.get(); + map.from(this.serverProperties::getPort).whenNonNull().to(factory::setPort); + map.from(this.serverProperties::getAddress).whenNonNull().to(factory::setAddress); + map.from(this.serverProperties::getSsl).whenNonNull().to(factory::setSsl); + map.from(this.serverProperties::getCompression).whenNonNull().to(factory::setCompression); + map.from(this.serverProperties::getHttp2).whenNonNull().to(factory::setHttp2); + map.from(() -> factory).when(configurableReactiveWebServerFactory -> factory instanceof TomcatReactiveWebServerFactory) + .to(configurableReactiveWebServerFactory -> TomcatCustomizer.customizeTomcat(this.serverProperties, this.environment, + (TomcatReactiveWebServerFactory) factory)); + map.from(() -> factory).when(configurableReactiveWebServerFactory -> factory instanceof JettyReactiveWebServerFactory) + .to(configurableReactiveWebServerFactory -> JettyCustomizer.customizeJetty(this.serverProperties, this.environment, + (JettyReactiveWebServerFactory) factory)); + map.from(() -> factory).when(configurableReactiveWebServerFactory -> factory instanceof UndertowReactiveWebServerFactory) + .to(configurableReactiveWebServerFactory -> UndertowCustomizer.customizeUndertow(this.serverProperties, this.environment, + (UndertowReactiveWebServerFactory) factory)); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/DefaultServletWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/DefaultServletWebServerFactoryCustomizer.java index 7b08c241088..4869b8d7e3d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/DefaultServletWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/DefaultServletWebServerFactoryCustomizer.java @@ -20,6 +20,7 @@ import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.embedded.jetty.JettyCustomizer; import org.springframework.boot.autoconfigure.web.embedded.tomcat.TomcatCustomizer; import org.springframework.boot.autoconfigure.web.embedded.undertow.UndertowCustomizer; +import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory; import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; @@ -37,6 +38,7 @@ import org.springframework.util.ObjectUtils; * @author Brian Clozel * @author Stephane Nicoll * @author Olivier Lamy + * @author Yunkun Huang * @since 2.0.0 */ public class DefaultServletWebServerFactoryCustomizer @@ -67,49 +69,30 @@ public class DefaultServletWebServerFactoryCustomizer @Override public void customize(ConfigurableServletWebServerFactory factory) { - if (this.serverProperties.getPort() != null) { - factory.setPort(this.serverProperties.getPort()); - } - if (this.serverProperties.getAddress() != null) { - factory.setAddress(this.serverProperties.getAddress()); - } - if (this.serverProperties.getServlet().getContextPath() != null) { - factory.setContextPath(this.serverProperties.getServlet().getContextPath()); - } - if (this.serverProperties.getDisplayName() != null) { - factory.setDisplayName(this.serverProperties.getDisplayName()); - } - factory.setSession(this.serverProperties.getServlet().getSession()); - if (this.serverProperties.getSsl() != null) { - factory.setSsl(this.serverProperties.getSsl()); - } - if (this.serverProperties.getServlet() != null) { - factory.setJsp(this.serverProperties.getServlet().getJsp()); - } - if (this.serverProperties.getCompression() != null) { - factory.setCompression(this.serverProperties.getCompression()); - } - if (this.serverProperties.getHttp2() != null) { - factory.setHttp2(this.serverProperties.getHttp2()); - } - factory.setServerHeader(this.serverProperties.getServerHeader()); - if (factory instanceof TomcatServletWebServerFactory) { - TomcatServletWebServerFactory tomcatFactory = (TomcatServletWebServerFactory) factory; - TomcatCustomizer.customizeTomcat(this.serverProperties, this.environment, - tomcatFactory); - TomcatServletCustomizer.customizeTomcat(this.serverProperties, - this.environment, tomcatFactory); - } - if (factory instanceof JettyServletWebServerFactory) { - JettyCustomizer.customizeJetty(this.serverProperties, this.environment, - (JettyServletWebServerFactory) factory); - } - if (factory instanceof UndertowServletWebServerFactory) { - UndertowCustomizer.customizeUndertow(this.serverProperties, this.environment, - (UndertowServletWebServerFactory) factory); - } - factory.setInitParameters( - this.serverProperties.getServlet().getContextParameters()); + PropertyMapper map = PropertyMapper.get(); + map.from(this.serverProperties::getPort).whenNonNull().to(factory::setPort); + map.from(this.serverProperties::getAddress).whenNonNull().to(factory::setAddress); + map.from(this.serverProperties.getServlet()::getContextPath).whenNonNull().to(factory::setContextPath); + map.from(this.serverProperties::getDisplayName).whenNonNull().to(factory::setDisplayName); + map.from(this.serverProperties.getServlet()::getSession).to(factory::setSession); + map.from(this.serverProperties::getSsl).whenNonNull().to(factory::setSsl); + map.from(this.serverProperties::getServlet).whenNonNull().as(ServerProperties.Servlet::getJsp).to(factory::setJsp); + map.from(this.serverProperties::getCompression).whenNonNull().to(factory::setCompression); + map.from(this.serverProperties::getHttp2).whenNonNull().to(factory::setHttp2); + map.from(this.serverProperties::getServerHeader).to(factory::setServerHeader); + map.from(() -> factory).when(configurableServletWebServerFactory -> factory instanceof TomcatServletWebServerFactory) + .to(configurableServletWebServerFactory -> { + TomcatServletWebServerFactory tomcatFactory = (TomcatServletWebServerFactory) factory; + TomcatCustomizer.customizeTomcat(this.serverProperties, this.environment, tomcatFactory); + TomcatServletCustomizer.customizeTomcat(this.serverProperties, this.environment, tomcatFactory); + }); + map.from(() -> factory).when(configurableServletWebServerFactory -> factory instanceof JettyServletWebServerFactory) + .to(configurableServletWebServerFactory -> JettyCustomizer.customizeJetty(this.serverProperties, this.environment, + (JettyServletWebServerFactory) factory)); + map.from(() -> factory).when(configurableServletWebServerFactory -> factory instanceof UndertowServletWebServerFactory) + .to(configurableServletWebServerFactory -> UndertowCustomizer.customizeUndertow(this.serverProperties, this.environment, + (UndertowServletWebServerFactory) factory)); + map.from(this.serverProperties.getServlet()::getContextParameters).to(factory::setInitParameters); } private static class TomcatServletCustomizer { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/DefaultReactiveWebServerFactoryCustomizerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/DefaultReactiveWebServerFactoryCustomizerTests.java index a705a019f29..19be920fd38 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/DefaultReactiveWebServerFactoryCustomizerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/DefaultReactiveWebServerFactoryCustomizerTests.java @@ -46,6 +46,7 @@ import org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFacto import org.springframework.boot.web.embedded.tomcat.TomcatWebServer; import org.springframework.boot.web.embedded.undertow.UndertowReactiveWebServerFactory; import org.springframework.boot.web.reactive.server.ConfigurableReactiveWebServerFactory; +import org.springframework.boot.web.server.Ssl; import org.springframework.http.server.reactive.HttpHandler; import org.springframework.mock.env.MockEnvironment; @@ -60,6 +61,7 @@ import static org.mockito.Mockito.verify; * Tests for {@link DefaultReactiveWebServerFactoryCustomizer}. * * @author Brian Clozel + * @author Yunkun Huang */ public class DefaultReactiveWebServerFactoryCustomizerTests { @@ -91,6 +93,16 @@ public class DefaultReactiveWebServerFactoryCustomizerTests { verify(factory).setAddress(address); } + @Test + public void testCustomizeServerSsl() { + ConfigurableReactiveWebServerFactory factory = mock( + ConfigurableReactiveWebServerFactory.class); + Ssl ssl = mock(Ssl.class); + this.properties.setSsl(ssl); + this.customizer.customize(factory); + verify(factory).setSsl(ssl); + } + @Test public void tomcatAccessLogIsDisabledByDefault() { TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory(); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/DefaultServletWebServerFactoryCustomizerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/DefaultServletWebServerFactoryCustomizerTests.java index e806b71e13c..71b148ed6e9 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/DefaultServletWebServerFactoryCustomizerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/DefaultServletWebServerFactoryCustomizerTests.java @@ -49,13 +49,16 @@ import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.embedded.tomcat.TomcatWebServer; import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory; +import org.springframework.boot.web.server.Ssl; import org.springframework.boot.web.servlet.ServletContextInitializer; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; +import org.springframework.boot.web.servlet.server.Jsp; import org.springframework.boot.web.servlet.server.Session; import org.springframework.boot.web.servlet.server.Session.Cookie; import org.springframework.mock.env.MockEnvironment; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -66,6 +69,7 @@ import static org.mockito.Mockito.verify; * Tests for {@link DefaultServletWebServerFactoryCustomizer}. * * @author Brian Clozel + * @author Yunkun Huang */ public class DefaultServletWebServerFactoryCustomizerTests { @@ -203,6 +207,24 @@ public class DefaultServletWebServerFactoryCustomizerTests { verify(factory).setDisplayName("TestName"); } + @Test + public void testCustomizeSsl() { + ConfigurableServletWebServerFactory factory = mock( + ConfigurableServletWebServerFactory.class); + Ssl ssl = mock(Ssl.class); + this.properties.setSsl(ssl); + this.customizer.customize(factory); + verify(factory).setSsl(ssl); + } + + @Test + public void testCustomizeJsp() { + ConfigurableServletWebServerFactory factory = mock( + ConfigurableServletWebServerFactory.class); + this.customizer.customize(factory); + verify(factory).setJsp(any(Jsp.class)); + } + @Test public void customizeSessionProperties() throws Exception { Map map = new HashMap<>(); From 98c667c2d5da552be15de4d805a3470e41bc1367 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 26 Jan 2018 11:19:03 +0100 Subject: [PATCH 2/2] Polish "Use PropertyMapper to configure WebServerFactory" Closes gh-11773 --- ...ultReactiveWebServerFactoryCustomizer.java | 30 ++++++------ ...aultServletWebServerFactoryCustomizer.java | 46 ++++++++++--------- 2 files changed, 40 insertions(+), 36 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/DefaultReactiveWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/DefaultReactiveWebServerFactoryCustomizer.java index 519f0725aec..bac4b73803b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/DefaultReactiveWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/DefaultReactiveWebServerFactoryCustomizer.java @@ -61,21 +61,21 @@ public class DefaultReactiveWebServerFactoryCustomizer @Override public void customize(ConfigurableReactiveWebServerFactory factory) { - PropertyMapper map = PropertyMapper.get(); - map.from(this.serverProperties::getPort).whenNonNull().to(factory::setPort); - map.from(this.serverProperties::getAddress).whenNonNull().to(factory::setAddress); - map.from(this.serverProperties::getSsl).whenNonNull().to(factory::setSsl); - map.from(this.serverProperties::getCompression).whenNonNull().to(factory::setCompression); - map.from(this.serverProperties::getHttp2).whenNonNull().to(factory::setHttp2); - map.from(() -> factory).when(configurableReactiveWebServerFactory -> factory instanceof TomcatReactiveWebServerFactory) - .to(configurableReactiveWebServerFactory -> TomcatCustomizer.customizeTomcat(this.serverProperties, this.environment, - (TomcatReactiveWebServerFactory) factory)); - map.from(() -> factory).when(configurableReactiveWebServerFactory -> factory instanceof JettyReactiveWebServerFactory) - .to(configurableReactiveWebServerFactory -> JettyCustomizer.customizeJetty(this.serverProperties, this.environment, - (JettyReactiveWebServerFactory) factory)); - map.from(() -> factory).when(configurableReactiveWebServerFactory -> factory instanceof UndertowReactiveWebServerFactory) - .to(configurableReactiveWebServerFactory -> UndertowCustomizer.customizeUndertow(this.serverProperties, this.environment, - (UndertowReactiveWebServerFactory) factory)); + PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); + map.from(this.serverProperties::getPort).to(factory::setPort); + map.from(this.serverProperties::getAddress).to(factory::setAddress); + map.from(this.serverProperties::getSsl).to(factory::setSsl); + map.from(this.serverProperties::getCompression).to(factory::setCompression); + map.from(this.serverProperties::getHttp2).to(factory::setHttp2); + map.from(() -> factory).whenInstanceOf(TomcatReactiveWebServerFactory.class) + .to(tomcatFactory -> TomcatCustomizer.customizeTomcat( + this.serverProperties, this.environment, tomcatFactory)); + map.from(() -> factory).whenInstanceOf(JettyReactiveWebServerFactory.class) + .to(jettyFactory -> JettyCustomizer.customizeJetty(this.serverProperties, + this.environment, jettyFactory)); + map.from(() -> factory).whenInstanceOf(UndertowReactiveWebServerFactory.class) + .to(undertowFactory -> UndertowCustomizer.customizeUndertow( + this.serverProperties, this.environment, undertowFactory)); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/DefaultServletWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/DefaultServletWebServerFactoryCustomizer.java index 4869b8d7e3d..8d696262296 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/DefaultServletWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/DefaultServletWebServerFactoryCustomizer.java @@ -69,30 +69,34 @@ public class DefaultServletWebServerFactoryCustomizer @Override public void customize(ConfigurableServletWebServerFactory factory) { - PropertyMapper map = PropertyMapper.get(); - map.from(this.serverProperties::getPort).whenNonNull().to(factory::setPort); - map.from(this.serverProperties::getAddress).whenNonNull().to(factory::setAddress); - map.from(this.serverProperties.getServlet()::getContextPath).whenNonNull().to(factory::setContextPath); - map.from(this.serverProperties::getDisplayName).whenNonNull().to(factory::setDisplayName); + PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); + map.from(this.serverProperties::getPort).to(factory::setPort); + map.from(this.serverProperties::getAddress).to(factory::setAddress); + map.from(this.serverProperties.getServlet()::getContextPath) + .to(factory::setContextPath); + map.from(this.serverProperties::getDisplayName).to(factory::setDisplayName); map.from(this.serverProperties.getServlet()::getSession).to(factory::setSession); - map.from(this.serverProperties::getSsl).whenNonNull().to(factory::setSsl); - map.from(this.serverProperties::getServlet).whenNonNull().as(ServerProperties.Servlet::getJsp).to(factory::setJsp); - map.from(this.serverProperties::getCompression).whenNonNull().to(factory::setCompression); - map.from(this.serverProperties::getHttp2).whenNonNull().to(factory::setHttp2); + map.from(this.serverProperties::getSsl).to(factory::setSsl); + map.from(this.serverProperties::getServlet).as(ServerProperties.Servlet::getJsp) + .to(factory::setJsp); + map.from(this.serverProperties::getCompression).to(factory::setCompression); + map.from(this.serverProperties::getHttp2).to(factory::setHttp2); map.from(this.serverProperties::getServerHeader).to(factory::setServerHeader); - map.from(() -> factory).when(configurableServletWebServerFactory -> factory instanceof TomcatServletWebServerFactory) - .to(configurableServletWebServerFactory -> { - TomcatServletWebServerFactory tomcatFactory = (TomcatServletWebServerFactory) factory; - TomcatCustomizer.customizeTomcat(this.serverProperties, this.environment, tomcatFactory); - TomcatServletCustomizer.customizeTomcat(this.serverProperties, this.environment, tomcatFactory); + map.from(() -> factory).whenInstanceOf(TomcatServletWebServerFactory.class) + .to(tomcatFactory -> { + TomcatCustomizer.customizeTomcat(this.serverProperties, + this.environment, tomcatFactory); + TomcatServletCustomizer.customizeTomcat(this.serverProperties, + this.environment, tomcatFactory); }); - map.from(() -> factory).when(configurableServletWebServerFactory -> factory instanceof JettyServletWebServerFactory) - .to(configurableServletWebServerFactory -> JettyCustomizer.customizeJetty(this.serverProperties, this.environment, - (JettyServletWebServerFactory) factory)); - map.from(() -> factory).when(configurableServletWebServerFactory -> factory instanceof UndertowServletWebServerFactory) - .to(configurableServletWebServerFactory -> UndertowCustomizer.customizeUndertow(this.serverProperties, this.environment, - (UndertowServletWebServerFactory) factory)); - map.from(this.serverProperties.getServlet()::getContextParameters).to(factory::setInitParameters); + map.from(() -> factory).whenInstanceOf(JettyServletWebServerFactory.class) + .to(jettyFactory -> JettyCustomizer.customizeJetty(this.serverProperties, + this.environment, jettyFactory)); + map.from(() -> factory).whenInstanceOf(UndertowServletWebServerFactory.class) + .to(undertowFactory -> UndertowCustomizer.customizeUndertow( + this.serverProperties, this.environment, undertowFactory)); + map.from(this.serverProperties.getServlet()::getContextParameters) + .to(factory::setInitParameters); } private static class TomcatServletCustomizer {