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 aab35adf26..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 @@ -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,26 @@ 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(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) + .to(accesslog -> customizeAccessLog(factory, accesslog)); + } + + private static boolean isPositive(Integer value) { + return value > 0; } 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 f5b8291322..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 @@ -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,8 @@ import org.springframework.util.StringUtils; * servers. * * @author Brian Clozel + * @author Yulin Qin + * @author Stephane Nicoll * @since 2.0.0 */ public final class TomcatCustomizer { @@ -46,47 +49,52 @@ 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() + .as(Duration::getSeconds).as(Long::intValue) + .to(factory::setBackgroundProcessorDelay); 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 - ? 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()); - } + propertyMapper.from(tomcatProperties::getMaxThreads) + .when(TomcatCustomizer::isPositive) + .to(maxThreads -> customizeMaxThreads(factory, tomcatProperties.getMaxThreads())); + propertyMapper.from(tomcatProperties::getMinSpareThreads) + .when(TomcatCustomizer::isPositive) + .to(minSpareThreads -> customizeMinThreads(factory, minSpareThreads)); + propertyMapper.from(() -> determineMaxHttpHeaderSize(serverProperties, tomcatProperties)) + .when(TomcatCustomizer::isPositive) + .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(TomcatCustomizer::isPositive) + .to(maxConnections -> customizeMaxConnections(factory, maxConnections)); + 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 09a9ea76b1..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 @@ -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,8 @@ import org.springframework.core.env.Environment; * servers. * * @author Brian Clozel + * @author Yulin Qin + * @author Stephane Nicoll */ public final class UndertowCustomizer { @@ -41,41 +44,47 @@ 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().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(