Commit 1c195f5b authored by Stephane Nicoll's avatar Stephane Nicoll

Polish "Migrate server customizer to PropertyMapper"

Closes gh-11772
parent 19542b97
...@@ -52,18 +52,27 @@ public final class JettyCustomizer { ...@@ -52,18 +52,27 @@ public final class JettyCustomizer {
factory.setUseForwardHeaders( factory.setUseForwardHeaders(
getOrDeduceUseForwardHeaders(serverProperties, environment)); getOrDeduceUseForwardHeaders(serverProperties, environment));
PropertyMapper propertyMapper = PropertyMapper.get(); PropertyMapper propertyMapper = PropertyMapper.get();
propertyMapper.from(jettyProperties::getAcceptors).whenNonNull().to(factory::setAcceptors); propertyMapper.from(jettyProperties::getAcceptors).whenNonNull()
propertyMapper.from(jettyProperties::getSelectors).whenNonNull().to(factory::setSelectors); .to(factory::setAcceptors);
propertyMapper.from(serverProperties::getMaxHttpHeaderSize).when(maxHttpHeaderSize -> maxHttpHeaderSize > 0) propertyMapper.from(jettyProperties::getSelectors).whenNonNull()
.to(maxHttpHeaderSize -> customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize)); .to(factory::setSelectors);
propertyMapper.from(jettyProperties::getMaxHttpPostSize).when(maxHttpPostSize -> maxHttpPostSize > 0) propertyMapper.from(serverProperties::getMaxHttpHeaderSize)
.when(JettyCustomizer::isPositive).to(maxHttpHeaderSize ->
customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize));
propertyMapper.from(jettyProperties::getMaxHttpPostSize)
.when(JettyCustomizer::isPositive)
.to(maxHttpPostSize -> customizeMaxHttpPostSize(factory, maxHttpPostSize)); .to(maxHttpPostSize -> customizeMaxHttpPostSize(factory, maxHttpPostSize));
propertyMapper.from(serverProperties::getConnectionTimeout).whenNonNull() propertyMapper.from(serverProperties::getConnectionTimeout).whenNonNull()
.to(connectionTimeout -> customizeConnectionTimeout(factory, connectionTimeout)); .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)); .to(accesslog -> customizeAccessLog(factory, accesslog));
} }
private static boolean isPositive(Integer value) {
return value > 0;
}
private static boolean getOrDeduceUseForwardHeaders(ServerProperties serverProperties, private static boolean getOrDeduceUseForwardHeaders(ServerProperties serverProperties,
Environment environment) { Environment environment) {
if (serverProperties.isUseForwardHeaders() != null) { if (serverProperties.isUseForwardHeaders() != null) {
......
...@@ -38,6 +38,7 @@ import org.springframework.util.StringUtils; ...@@ -38,6 +38,7 @@ import org.springframework.util.StringUtils;
* *
* @author Brian Clozel * @author Brian Clozel
* @author Yulin Qin * @author Yulin Qin
* @author Stephane Nicoll
* @since 2.0.0 * @since 2.0.0
*/ */
public final class TomcatCustomizer { public final class TomcatCustomizer {
...@@ -48,35 +49,52 @@ public final class TomcatCustomizer { ...@@ -48,35 +49,52 @@ public final class TomcatCustomizer {
public static void customizeTomcat(ServerProperties serverProperties, public static void customizeTomcat(ServerProperties serverProperties,
Environment environment, ConfigurableTomcatWebServerFactory factory) { Environment environment, ConfigurableTomcatWebServerFactory factory) {
ServerProperties.Tomcat tomcatProperties = serverProperties.getTomcat(); ServerProperties.Tomcat tomcatProperties = serverProperties.getTomcat();
PropertyMapper propertyMapper = PropertyMapper.get(); 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() 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); 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())); .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)); .to(minSpareThreads -> customizeMinThreads(factory, minSpareThreads));
propertyMapper.from(() -> (serverProperties.getMaxHttpHeaderSize() > 0 propertyMapper.from(() -> determineMaxHttpHeaderSize(serverProperties, tomcatProperties))
? serverProperties.getMaxHttpHeaderSize() .when(TomcatCustomizer::isPositive)
: tomcatProperties.getMaxHttpHeaderSize()))
.when(maxHttpHeaderSize -> maxHttpHeaderSize > 0)
.to(maxHttpHeaderSize -> customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize)); .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)); .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)); .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() propertyMapper.from(serverProperties::getConnectionTimeout).whenNonNull()
.to(connectionTimeout -> customizeConnectionTimeout(factory, connectionTimeout)); .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)); .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)); .to(acceptCount -> customizeAcceptCount(factory, acceptCount));
customizeStaticResources(serverProperties.getTomcat().getResource(), factory); 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, private static void customizeAcceptCount(ConfigurableTomcatWebServerFactory factory,
int acceptCount) { int acceptCount) {
factory.addConnectorCustomizers((connector) -> { factory.addConnectorCustomizers((connector) -> {
......
...@@ -32,6 +32,7 @@ import org.springframework.core.env.Environment; ...@@ -32,6 +32,7 @@ import org.springframework.core.env.Environment;
* *
* @author Brian Clozel * @author Brian Clozel
* @author Yulin Qin * @author Yulin Qin
* @author Stephane Nicoll
*/ */
public final class UndertowCustomizer { public final class UndertowCustomizer {
...@@ -43,30 +44,47 @@ public final class UndertowCustomizer { ...@@ -43,30 +44,47 @@ public final class UndertowCustomizer {
ServerProperties.Undertow undertowProperties = serverProperties.getUndertow(); ServerProperties.Undertow undertowProperties = serverProperties.getUndertow();
ServerProperties.Undertow.Accesslog accesslogProperties = undertowProperties ServerProperties.Undertow.Accesslog accesslogProperties = undertowProperties
.getAccesslog(); .getAccesslog();
PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
PropertyMapper propertyMapper = PropertyMapper.get(); propertyMapper.from(undertowProperties::getBufferSize).to(factory::setBufferSize);
propertyMapper.from(undertowProperties::getBufferSize).whenNonNull().to(factory::setBufferSize); propertyMapper.from(undertowProperties::getIoThreads).to(factory::setIoThreads);
propertyMapper.from(undertowProperties::getIoThreads).whenNonNull().to(factory::setIoThreads); propertyMapper.from(undertowProperties::getWorkerThreads)
propertyMapper.from(undertowProperties::getWorkerThreads).whenNonNull().to(factory::setWorkerThreads); .to(factory::setWorkerThreads);
propertyMapper.from(undertowProperties::getDirectBuffers).whenNonNull().to(factory::setUseDirectBuffers); propertyMapper.from(undertowProperties::getDirectBuffers)
propertyMapper.from(accesslogProperties::getEnabled).whenNonNull().to(factory::setAccessLogEnabled); .to(factory::setUseDirectBuffers);
propertyMapper.from(accesslogProperties::getDir).to(factory::setAccessLogDirectory); propertyMapper.from(accesslogProperties::getEnabled)
propertyMapper.from(accesslogProperties::getPattern).to(factory::setAccessLogPattern); .to(factory::setAccessLogEnabled);
propertyMapper.from(accesslogProperties::getPrefix).to(factory::setAccessLogPrefix); propertyMapper.from(accesslogProperties::getDir)
propertyMapper.from(accesslogProperties::getSuffix).to(factory::setAccessLogSuffix); .to(factory::setAccessLogDirectory);
propertyMapper.from(accesslogProperties::isRotate).to(factory::setAccessLogRotate); propertyMapper.from(accesslogProperties::getPattern)
propertyMapper.from(() -> getOrDeduceUseForwardHeaders(serverProperties, environment)).to(factory::setUseForwardHeaders); .to(factory::setAccessLogPattern);
propertyMapper.from(accesslogProperties::getPrefix)
propertyMapper.from(serverProperties::getMaxHttpHeaderSize).when(maxHttpHeaderSize -> maxHttpHeaderSize > 0) .to(factory::setAccessLogPrefix);
.to(maxHttpHeaderSize -> customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize)); propertyMapper.from(accesslogProperties::getSuffix)
propertyMapper.from(undertowProperties::getMaxHttpPostSize).when(maxHttpPostSize -> maxHttpPostSize > 0) .to(factory::setAccessLogSuffix);
.to(maxHttpPostSize -> customizeMaxHttpPostSize(factory, maxHttpPostSize)); propertyMapper.from(accesslogProperties::isRotate)
propertyMapper.from(serverProperties::getConnectionTimeout).whenNonNull() .to(factory::setAccessLogRotate);
.to(connectionTimeout -> customizeConnectionTimeout(factory, connectionTimeout)); 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 factory.addDeploymentInfoCustomizers(deploymentInfo -> deploymentInfo
.setEagerFilterInit(undertowProperties.isEagerFilterInit())); .setEagerFilterInit(undertowProperties.isEagerFilterInit()));
} }
private static boolean isPositive(Number value) {
return value.longValue() > 0;
}
private static void customizeConnectionTimeout( private static void customizeConnectionTimeout(
ConfigurableUndertowWebServerFactory factory, Duration connectionTimeout) { ConfigurableUndertowWebServerFactory factory, Duration connectionTimeout) {
factory.addBuilderCustomizers((builder) -> builder.setSocketOption( factory.addBuilderCustomizers((builder) -> builder.setSocketOption(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment