From 1c195f5b9ac88a3573107a0f2bfa263886fe4b45 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 26 Jan 2018 13:37:49 +0100 Subject: [PATCH] Polish "Migrate server customizer to PropertyMapper" Closes gh-11772 --- .../web/embedded/jetty/JettyCustomizer.java | 21 +++++-- .../web/embedded/tomcat/TomcatCustomizer.java | 46 ++++++++++----- .../embedded/undertow/UndertowCustomizer.java | 58 ++++++++++++------- 3 files changed, 85 insertions(+), 40 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/jetty/JettyCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/jetty/JettyCustomizer.java index 8c07581122..dcaadc6196 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/jetty/JettyCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/jetty/JettyCustomizer.java @@ -52,18 +52,27 @@ public final class JettyCustomizer { factory.setUseForwardHeaders( getOrDeduceUseForwardHeaders(serverProperties, environment)); PropertyMapper propertyMapper = PropertyMapper.get(); - propertyMapper.from(jettyProperties::getAcceptors).whenNonNull().to(factory::setAcceptors); - propertyMapper.from(jettyProperties::getSelectors).whenNonNull().to(factory::setSelectors); - propertyMapper.from(serverProperties::getMaxHttpHeaderSize).when(maxHttpHeaderSize -> maxHttpHeaderSize > 0) - .to(maxHttpHeaderSize -> customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize)); - propertyMapper.from(jettyProperties::getMaxHttpPostSize).when(maxHttpPostSize -> maxHttpPostSize > 0) + propertyMapper.from(jettyProperties::getAcceptors).whenNonNull() + .to(factory::setAcceptors); + propertyMapper.from(jettyProperties::getSelectors).whenNonNull() + .to(factory::setSelectors); + propertyMapper.from(serverProperties::getMaxHttpHeaderSize) + .when(JettyCustomizer::isPositive).to(maxHttpHeaderSize -> + customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize)); + propertyMapper.from(jettyProperties::getMaxHttpPostSize) + .when(JettyCustomizer::isPositive) .to(maxHttpPostSize -> customizeMaxHttpPostSize(factory, maxHttpPostSize)); propertyMapper.from(serverProperties::getConnectionTimeout).whenNonNull() .to(connectionTimeout -> customizeConnectionTimeout(factory, connectionTimeout)); - propertyMapper.from(jettyProperties::getAccesslog).when(ServerProperties.Jetty.Accesslog::isEnabled) + propertyMapper.from(jettyProperties::getAccesslog) + .when(ServerProperties.Jetty.Accesslog::isEnabled) .to(accesslog -> customizeAccessLog(factory, accesslog)); } + private static boolean isPositive(Integer value) { + return value > 0; + } + private static boolean getOrDeduceUseForwardHeaders(ServerProperties serverProperties, Environment environment) { if (serverProperties.isUseForwardHeaders() != null) { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/tomcat/TomcatCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/tomcat/TomcatCustomizer.java index 62cee78d3d..6840d1b5db 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/tomcat/TomcatCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/tomcat/TomcatCustomizer.java @@ -38,6 +38,7 @@ import org.springframework.util.StringUtils; * * @author Brian Clozel * @author Yulin Qin + * @author Stephane Nicoll * @since 2.0.0 */ public final class TomcatCustomizer { @@ -48,35 +49,52 @@ public final class TomcatCustomizer { public static void customizeTomcat(ServerProperties serverProperties, Environment environment, ConfigurableTomcatWebServerFactory factory) { ServerProperties.Tomcat tomcatProperties = serverProperties.getTomcat(); - PropertyMapper propertyMapper = PropertyMapper.get(); - propertyMapper.from(tomcatProperties::getBasedir).whenNonNull().to(factory::setBaseDirectory); + propertyMapper.from(tomcatProperties::getBasedir).whenNonNull() + .to(factory::setBaseDirectory); propertyMapper.from(tomcatProperties::getBackgroundProcessorDelay).whenNonNull() - .to((backgroundProcessorDelay) -> factory.setBackgroundProcessorDelay((int) backgroundProcessorDelay.getSeconds())); + .as(Duration::getSeconds).as(Long::intValue) + .to(factory::setBackgroundProcessorDelay); customizeRemoteIpValve(serverProperties, environment, factory); - propertyMapper.from(tomcatProperties::getMaxThreads).when(maxThreads -> maxThreads > 0) + propertyMapper.from(tomcatProperties::getMaxThreads) + .when(TomcatCustomizer::isPositive) .to(maxThreads -> customizeMaxThreads(factory, tomcatProperties.getMaxThreads())); - propertyMapper.from(tomcatProperties::getMinSpareThreads).when(minSpareThreads -> minSpareThreads > 0) + propertyMapper.from(tomcatProperties::getMinSpareThreads) + .when(TomcatCustomizer::isPositive) .to(minSpareThreads -> customizeMinThreads(factory, minSpareThreads)); - propertyMapper.from(() -> (serverProperties.getMaxHttpHeaderSize() > 0 - ? serverProperties.getMaxHttpHeaderSize() - : tomcatProperties.getMaxHttpHeaderSize())) - .when(maxHttpHeaderSize -> maxHttpHeaderSize > 0) + propertyMapper.from(() -> determineMaxHttpHeaderSize(serverProperties, tomcatProperties)) + .when(TomcatCustomizer::isPositive) .to(maxHttpHeaderSize -> customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize)); - propertyMapper.from(tomcatProperties::getMaxHttpPostSize).when(maxHttpPostSize -> maxHttpPostSize != 0) + propertyMapper.from(tomcatProperties::getMaxHttpPostSize) + .when(maxHttpPostSize -> maxHttpPostSize != 0) .to(maxHttpPostSize -> customizeMaxHttpPostSize(factory, maxHttpPostSize)); - propertyMapper.from(tomcatProperties::getAccesslog).when(ServerProperties.Tomcat.Accesslog::isEnabled) + propertyMapper.from(tomcatProperties::getAccesslog) + .when(ServerProperties.Tomcat.Accesslog::isEnabled) .to(enabled -> customizeAccessLog(tomcatProperties, factory)); - propertyMapper.from(tomcatProperties::getUriEncoding).whenNonNull().to(factory::setUriEncoding); + propertyMapper.from(tomcatProperties::getUriEncoding).whenNonNull() + .to(factory::setUriEncoding); propertyMapper.from(serverProperties::getConnectionTimeout).whenNonNull() .to(connectionTimeout -> customizeConnectionTimeout(factory, connectionTimeout)); - propertyMapper.from(tomcatProperties::getMaxConnections).when(maxConnections -> maxConnections > 0) + propertyMapper.from(tomcatProperties::getMaxConnections) + .when(TomcatCustomizer::isPositive) .to(maxConnections -> customizeMaxConnections(factory, maxConnections)); - propertyMapper.from(tomcatProperties::getAcceptCount).when(acceptCount -> acceptCount > 0) + propertyMapper.from(tomcatProperties::getAcceptCount) + .when(TomcatCustomizer::isPositive) .to(acceptCount -> customizeAcceptCount(factory, acceptCount)); customizeStaticResources(serverProperties.getTomcat().getResource(), factory); } + private static boolean isPositive(int value) { + return value > 0; + } + + private static int determineMaxHttpHeaderSize(ServerProperties serverProperties, + ServerProperties.Tomcat tomcatProperties) { + return serverProperties.getMaxHttpHeaderSize() > 0 + ? serverProperties.getMaxHttpHeaderSize() + : tomcatProperties.getMaxHttpHeaderSize(); + } + private static void customizeAcceptCount(ConfigurableTomcatWebServerFactory factory, int acceptCount) { factory.addConnectorCustomizers((connector) -> { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/undertow/UndertowCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/undertow/UndertowCustomizer.java index ca6b420791..5c92bc7f7f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/undertow/UndertowCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/undertow/UndertowCustomizer.java @@ -32,6 +32,7 @@ import org.springframework.core.env.Environment; * * @author Brian Clozel * @author Yulin Qin + * @author Stephane Nicoll */ public final class UndertowCustomizer { @@ -43,30 +44,47 @@ public final class UndertowCustomizer { ServerProperties.Undertow undertowProperties = serverProperties.getUndertow(); ServerProperties.Undertow.Accesslog accesslogProperties = undertowProperties .getAccesslog(); - - PropertyMapper propertyMapper = PropertyMapper.get(); - propertyMapper.from(undertowProperties::getBufferSize).whenNonNull().to(factory::setBufferSize); - propertyMapper.from(undertowProperties::getIoThreads).whenNonNull().to(factory::setIoThreads); - propertyMapper.from(undertowProperties::getWorkerThreads).whenNonNull().to(factory::setWorkerThreads); - propertyMapper.from(undertowProperties::getDirectBuffers).whenNonNull().to(factory::setUseDirectBuffers); - propertyMapper.from(accesslogProperties::getEnabled).whenNonNull().to(factory::setAccessLogEnabled); - propertyMapper.from(accesslogProperties::getDir).to(factory::setAccessLogDirectory); - propertyMapper.from(accesslogProperties::getPattern).to(factory::setAccessLogPattern); - propertyMapper.from(accesslogProperties::getPrefix).to(factory::setAccessLogPrefix); - propertyMapper.from(accesslogProperties::getSuffix).to(factory::setAccessLogSuffix); - propertyMapper.from(accesslogProperties::isRotate).to(factory::setAccessLogRotate); - propertyMapper.from(() -> getOrDeduceUseForwardHeaders(serverProperties, environment)).to(factory::setUseForwardHeaders); - - propertyMapper.from(serverProperties::getMaxHttpHeaderSize).when(maxHttpHeaderSize -> maxHttpHeaderSize > 0) - .to(maxHttpHeaderSize -> customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize)); - propertyMapper.from(undertowProperties::getMaxHttpPostSize).when(maxHttpPostSize -> maxHttpPostSize > 0) - .to(maxHttpPostSize -> customizeMaxHttpPostSize(factory, maxHttpPostSize)); - propertyMapper.from(serverProperties::getConnectionTimeout).whenNonNull() - .to(connectionTimeout -> customizeConnectionTimeout(factory, connectionTimeout)); + PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull(); + propertyMapper.from(undertowProperties::getBufferSize).to(factory::setBufferSize); + propertyMapper.from(undertowProperties::getIoThreads).to(factory::setIoThreads); + propertyMapper.from(undertowProperties::getWorkerThreads) + .to(factory::setWorkerThreads); + propertyMapper.from(undertowProperties::getDirectBuffers) + .to(factory::setUseDirectBuffers); + propertyMapper.from(accesslogProperties::getEnabled) + .to(factory::setAccessLogEnabled); + propertyMapper.from(accesslogProperties::getDir) + .to(factory::setAccessLogDirectory); + propertyMapper.from(accesslogProperties::getPattern) + .to(factory::setAccessLogPattern); + propertyMapper.from(accesslogProperties::getPrefix) + .to(factory::setAccessLogPrefix); + propertyMapper.from(accesslogProperties::getSuffix) + .to(factory::setAccessLogSuffix); + propertyMapper.from(accesslogProperties::isRotate) + .to(factory::setAccessLogRotate); + propertyMapper.from(() -> + getOrDeduceUseForwardHeaders(serverProperties, environment)).to( + factory::setUseForwardHeaders); + propertyMapper.from(serverProperties::getMaxHttpHeaderSize) + .when(UndertowCustomizer::isPositive) + .to(maxHttpHeaderSize -> + customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize)); + propertyMapper.from(undertowProperties::getMaxHttpPostSize) + .when(UndertowCustomizer::isPositive) + .to(maxHttpPostSize -> + customizeMaxHttpPostSize(factory, maxHttpPostSize)); + propertyMapper.from(serverProperties::getConnectionTimeout) + .to(connectionTimeout -> + customizeConnectionTimeout(factory, connectionTimeout)); factory.addDeploymentInfoCustomizers(deploymentInfo -> deploymentInfo .setEagerFilterInit(undertowProperties.isEagerFilterInit())); } + private static boolean isPositive(Number value) { + return value.longValue() > 0; + } + private static void customizeConnectionTimeout( ConfigurableUndertowWebServerFactory factory, Duration connectionTimeout) { factory.addBuilderCustomizers((builder) -> builder.setSocketOption(