From 19542b975fc67669e8495c9c03392f2f4bb41a07 Mon Sep 17 00:00:00 2001 From: yulin Date: Thu, 25 Jan 2018 15:42:28 +0800 Subject: [PATCH 1/2] Migrate server customizer to PropertyMapper See gh-11772 --- .../web/embedded/jetty/JettyCustomizer.java | 30 ++++----- .../web/embedded/tomcat/TomcatCustomizer.java | 62 ++++++++----------- .../embedded/undertow/UndertowCustomizer.java | 55 +++++++--------- 3 files changed, 61 insertions(+), 86 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 aab35adf267..8c075811227 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 @@ -30,6 +30,7 @@ import org.eclipse.jetty.server.handler.HandlerWrapper; import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.cloud.CloudPlatform; +import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.boot.web.embedded.jetty.ConfigurableJettyWebServerFactory; import org.springframework.boot.web.embedded.jetty.JettyServerCustomizer; import org.springframework.core.env.Environment; @@ -50,24 +51,17 @@ public final class JettyCustomizer { ServerProperties.Jetty jettyProperties = serverProperties.getJetty(); factory.setUseForwardHeaders( getOrDeduceUseForwardHeaders(serverProperties, environment)); - if (jettyProperties.getAcceptors() != null) { - factory.setAcceptors(jettyProperties.getAcceptors()); - } - if (jettyProperties.getSelectors() != null) { - factory.setSelectors(jettyProperties.getSelectors()); - } - if (serverProperties.getMaxHttpHeaderSize() > 0) { - customizeMaxHttpHeaderSize(factory, serverProperties.getMaxHttpHeaderSize()); - } - if (jettyProperties.getMaxHttpPostSize() > 0) { - customizeMaxHttpPostSize(factory, jettyProperties.getMaxHttpPostSize()); - } - if (serverProperties.getConnectionTimeout() != null) { - customizeConnectionTimeout(factory, serverProperties.getConnectionTimeout()); - } - if (jettyProperties.getAccesslog().isEnabled()) { - customizeAccessLog(factory, jettyProperties.getAccesslog()); - } + 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) + .to(maxHttpPostSize -> customizeMaxHttpPostSize(factory, maxHttpPostSize)); + propertyMapper.from(serverProperties::getConnectionTimeout).whenNonNull() + .to(connectionTimeout -> customizeConnectionTimeout(factory, connectionTimeout)); + propertyMapper.from(jettyProperties::getAccesslog).when(ServerProperties.Jetty.Accesslog::isEnabled) + .to(accesslog -> customizeAccessLog(factory, accesslog)); } private static boolean getOrDeduceUseForwardHeaders(ServerProperties serverProperties, 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 f5b82913221..62cee78d3d2 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 @@ -27,6 +27,7 @@ import org.apache.coyote.http11.AbstractHttp11Protocol; import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.cloud.CloudPlatform; +import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory; import org.springframework.core.env.Environment; import org.springframework.util.StringUtils; @@ -36,6 +37,7 @@ import org.springframework.util.StringUtils; * servers. * * @author Brian Clozel + * @author Yulin Qin * @since 2.0.0 */ public final class TomcatCustomizer { @@ -46,44 +48,32 @@ public final class TomcatCustomizer { public static void customizeTomcat(ServerProperties serverProperties, Environment environment, ConfigurableTomcatWebServerFactory factory) { ServerProperties.Tomcat tomcatProperties = serverProperties.getTomcat(); - if (tomcatProperties.getBasedir() != null) { - factory.setBaseDirectory(tomcatProperties.getBasedir()); - } - if (tomcatProperties.getBackgroundProcessorDelay() != null) { - factory.setBackgroundProcessorDelay( - (int) tomcatProperties.getBackgroundProcessorDelay().getSeconds()); - } + + PropertyMapper propertyMapper = PropertyMapper.get(); + propertyMapper.from(tomcatProperties::getBasedir).whenNonNull().to(factory::setBaseDirectory); + propertyMapper.from(tomcatProperties::getBackgroundProcessorDelay).whenNonNull() + .to((backgroundProcessorDelay) -> factory.setBackgroundProcessorDelay((int) backgroundProcessorDelay.getSeconds())); customizeRemoteIpValve(serverProperties, environment, factory); - if (tomcatProperties.getMaxThreads() > 0) { - customizeMaxThreads(factory, tomcatProperties.getMaxThreads()); - } - if (tomcatProperties.getMinSpareThreads() > 0) { - customizeMinThreads(factory, tomcatProperties.getMinSpareThreads()); - } - int maxHttpHeaderSize = (serverProperties.getMaxHttpHeaderSize() > 0 + propertyMapper.from(tomcatProperties::getMaxThreads).when(maxThreads -> maxThreads > 0) + .to(maxThreads -> customizeMaxThreads(factory, tomcatProperties.getMaxThreads())); + propertyMapper.from(tomcatProperties::getMinSpareThreads).when(minSpareThreads -> minSpareThreads > 0) + .to(minSpareThreads -> customizeMinThreads(factory, minSpareThreads)); + propertyMapper.from(() -> (serverProperties.getMaxHttpHeaderSize() > 0 ? serverProperties.getMaxHttpHeaderSize() - : tomcatProperties.getMaxHttpHeaderSize()); - if (maxHttpHeaderSize > 0) { - customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize); - } - if (tomcatProperties.getMaxHttpPostSize() != 0) { - customizeMaxHttpPostSize(factory, tomcatProperties.getMaxHttpPostSize()); - } - if (tomcatProperties.getAccesslog().isEnabled()) { - customizeAccessLog(tomcatProperties, factory); - } - if (tomcatProperties.getUriEncoding() != null) { - factory.setUriEncoding(tomcatProperties.getUriEncoding()); - } - if (serverProperties.getConnectionTimeout() != null) { - customizeConnectionTimeout(factory, serverProperties.getConnectionTimeout()); - } - if (tomcatProperties.getMaxConnections() > 0) { - customizeMaxConnections(factory, tomcatProperties.getMaxConnections()); - } - if (tomcatProperties.getAcceptCount() > 0) { - customizeAcceptCount(factory, tomcatProperties.getAcceptCount()); - } + : tomcatProperties.getMaxHttpHeaderSize())) + .when(maxHttpHeaderSize -> maxHttpHeaderSize > 0) + .to(maxHttpHeaderSize -> customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize)); + propertyMapper.from(tomcatProperties::getMaxHttpPostSize).when(maxHttpPostSize -> maxHttpPostSize != 0) + .to(maxHttpPostSize -> customizeMaxHttpPostSize(factory, maxHttpPostSize)); + propertyMapper.from(tomcatProperties::getAccesslog).when(ServerProperties.Tomcat.Accesslog::isEnabled) + .to(enabled -> customizeAccessLog(tomcatProperties, factory)); + 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) + .to(maxConnections -> customizeMaxConnections(factory, maxConnections)); + propertyMapper.from(tomcatProperties::getAcceptCount).when(acceptCount -> acceptCount > 0) + .to(acceptCount -> customizeAcceptCount(factory, acceptCount)); customizeStaticResources(serverProperties.getTomcat().getResource(), factory); } 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 09a9ea76b17..ca6b420791a 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 @@ -22,6 +22,7 @@ import io.undertow.UndertowOptions; import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.cloud.CloudPlatform; +import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.boot.web.embedded.undertow.ConfigurableUndertowWebServerFactory; import org.springframework.core.env.Environment; @@ -30,6 +31,7 @@ import org.springframework.core.env.Environment; * servers. * * @author Brian Clozel + * @author Yulin Qin */ public final class UndertowCustomizer { @@ -41,38 +43,27 @@ public final class UndertowCustomizer { ServerProperties.Undertow undertowProperties = serverProperties.getUndertow(); ServerProperties.Undertow.Accesslog accesslogProperties = undertowProperties .getAccesslog(); - if (undertowProperties.getBufferSize() != null) { - factory.setBufferSize(undertowProperties.getBufferSize()); - } - if (undertowProperties.getIoThreads() != null) { - factory.setIoThreads(undertowProperties.getIoThreads()); - } - if (undertowProperties.getWorkerThreads() != null) { - factory.setWorkerThreads(undertowProperties.getWorkerThreads()); - } - if (undertowProperties.getDirectBuffers() != null) { - factory.setUseDirectBuffers(undertowProperties.getDirectBuffers()); - } - if (undertowProperties.getAccesslog().getEnabled() != null) { - factory.setAccessLogEnabled(accesslogProperties.getEnabled()); - } - factory.setAccessLogDirectory(accesslogProperties.getDir()); - factory.setAccessLogPattern(accesslogProperties.getPattern()); - factory.setAccessLogPrefix(accesslogProperties.getPrefix()); - factory.setAccessLogSuffix(accesslogProperties.getSuffix()); - factory.setAccessLogRotate(accesslogProperties.isRotate()); - factory.setUseForwardHeaders( - getOrDeduceUseForwardHeaders(serverProperties, environment)); - if (serverProperties.getMaxHttpHeaderSize() > 0) { - customizeMaxHttpHeaderSize(factory, serverProperties.getMaxHttpHeaderSize()); - } - if (undertowProperties.getMaxHttpPostSize() > 0) { - customizeMaxHttpPostSize(factory, undertowProperties.getMaxHttpPostSize()); - } - if (serverProperties.getConnectionTimeout() != null) { - customizeConnectionTimeout(factory, serverProperties.getConnectionTimeout()); - } - factory.addDeploymentInfoCustomizers((deploymentInfo) -> deploymentInfo + + 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)); + factory.addDeploymentInfoCustomizers(deploymentInfo -> deploymentInfo .setEagerFilterInit(undertowProperties.isEagerFilterInit())); } From 1c195f5b9ac88a3573107a0f2bfa263886fe4b45 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 26 Jan 2018 13:37:49 +0100 Subject: [PATCH 2/2] 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 8c075811227..dcaadc61961 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 62cee78d3d2..6840d1b5dbb 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 ca6b420791a..5c92bc7f7fa 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(