Commit 19542b97 authored by yulin's avatar yulin Committed by Stephane Nicoll

Migrate server customizer to PropertyMapper

See gh-11772
parent 988fc18f
...@@ -30,6 +30,7 @@ import org.eclipse.jetty.server.handler.HandlerWrapper; ...@@ -30,6 +30,7 @@ import org.eclipse.jetty.server.handler.HandlerWrapper;
import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.cloud.CloudPlatform; 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.ConfigurableJettyWebServerFactory;
import org.springframework.boot.web.embedded.jetty.JettyServerCustomizer; import org.springframework.boot.web.embedded.jetty.JettyServerCustomizer;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
...@@ -50,24 +51,17 @@ public final class JettyCustomizer { ...@@ -50,24 +51,17 @@ public final class JettyCustomizer {
ServerProperties.Jetty jettyProperties = serverProperties.getJetty(); ServerProperties.Jetty jettyProperties = serverProperties.getJetty();
factory.setUseForwardHeaders( factory.setUseForwardHeaders(
getOrDeduceUseForwardHeaders(serverProperties, environment)); getOrDeduceUseForwardHeaders(serverProperties, environment));
if (jettyProperties.getAcceptors() != null) { PropertyMapper propertyMapper = PropertyMapper.get();
factory.setAcceptors(jettyProperties.getAcceptors()); propertyMapper.from(jettyProperties::getAcceptors).whenNonNull().to(factory::setAcceptors);
} propertyMapper.from(jettyProperties::getSelectors).whenNonNull().to(factory::setSelectors);
if (jettyProperties.getSelectors() != null) { propertyMapper.from(serverProperties::getMaxHttpHeaderSize).when(maxHttpHeaderSize -> maxHttpHeaderSize > 0)
factory.setSelectors(jettyProperties.getSelectors()); .to(maxHttpHeaderSize -> customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize));
} propertyMapper.from(jettyProperties::getMaxHttpPostSize).when(maxHttpPostSize -> maxHttpPostSize > 0)
if (serverProperties.getMaxHttpHeaderSize() > 0) { .to(maxHttpPostSize -> customizeMaxHttpPostSize(factory, maxHttpPostSize));
customizeMaxHttpHeaderSize(factory, serverProperties.getMaxHttpHeaderSize()); propertyMapper.from(serverProperties::getConnectionTimeout).whenNonNull()
} .to(connectionTimeout -> customizeConnectionTimeout(factory, connectionTimeout));
if (jettyProperties.getMaxHttpPostSize() > 0) { propertyMapper.from(jettyProperties::getAccesslog).when(ServerProperties.Jetty.Accesslog::isEnabled)
customizeMaxHttpPostSize(factory, jettyProperties.getMaxHttpPostSize()); .to(accesslog -> customizeAccessLog(factory, accesslog));
}
if (serverProperties.getConnectionTimeout() != null) {
customizeConnectionTimeout(factory, serverProperties.getConnectionTimeout());
}
if (jettyProperties.getAccesslog().isEnabled()) {
customizeAccessLog(factory, jettyProperties.getAccesslog());
}
} }
private static boolean getOrDeduceUseForwardHeaders(ServerProperties serverProperties, private static boolean getOrDeduceUseForwardHeaders(ServerProperties serverProperties,
......
...@@ -27,6 +27,7 @@ import org.apache.coyote.http11.AbstractHttp11Protocol; ...@@ -27,6 +27,7 @@ import org.apache.coyote.http11.AbstractHttp11Protocol;
import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.cloud.CloudPlatform; import org.springframework.boot.cloud.CloudPlatform;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory; import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
...@@ -36,6 +37,7 @@ import org.springframework.util.StringUtils; ...@@ -36,6 +37,7 @@ import org.springframework.util.StringUtils;
* servers. * servers.
* *
* @author Brian Clozel * @author Brian Clozel
* @author Yulin Qin
* @since 2.0.0 * @since 2.0.0
*/ */
public final class TomcatCustomizer { public final class TomcatCustomizer {
...@@ -46,44 +48,32 @@ public final class TomcatCustomizer { ...@@ -46,44 +48,32 @@ 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();
if (tomcatProperties.getBasedir() != null) {
factory.setBaseDirectory(tomcatProperties.getBasedir()); PropertyMapper propertyMapper = PropertyMapper.get();
} propertyMapper.from(tomcatProperties::getBasedir).whenNonNull().to(factory::setBaseDirectory);
if (tomcatProperties.getBackgroundProcessorDelay() != null) { propertyMapper.from(tomcatProperties::getBackgroundProcessorDelay).whenNonNull()
factory.setBackgroundProcessorDelay( .to((backgroundProcessorDelay) -> factory.setBackgroundProcessorDelay((int) backgroundProcessorDelay.getSeconds()));
(int) tomcatProperties.getBackgroundProcessorDelay().getSeconds());
}
customizeRemoteIpValve(serverProperties, environment, factory); customizeRemoteIpValve(serverProperties, environment, factory);
if (tomcatProperties.getMaxThreads() > 0) { propertyMapper.from(tomcatProperties::getMaxThreads).when(maxThreads -> maxThreads > 0)
customizeMaxThreads(factory, tomcatProperties.getMaxThreads()); .to(maxThreads -> customizeMaxThreads(factory, tomcatProperties.getMaxThreads()));
} propertyMapper.from(tomcatProperties::getMinSpareThreads).when(minSpareThreads -> minSpareThreads > 0)
if (tomcatProperties.getMinSpareThreads() > 0) { .to(minSpareThreads -> customizeMinThreads(factory, minSpareThreads));
customizeMinThreads(factory, tomcatProperties.getMinSpareThreads()); propertyMapper.from(() -> (serverProperties.getMaxHttpHeaderSize() > 0
}
int maxHttpHeaderSize = (serverProperties.getMaxHttpHeaderSize() > 0
? serverProperties.getMaxHttpHeaderSize() ? serverProperties.getMaxHttpHeaderSize()
: tomcatProperties.getMaxHttpHeaderSize()); : tomcatProperties.getMaxHttpHeaderSize()))
if (maxHttpHeaderSize > 0) { .when(maxHttpHeaderSize -> maxHttpHeaderSize > 0)
customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize); .to(maxHttpHeaderSize -> customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize));
} propertyMapper.from(tomcatProperties::getMaxHttpPostSize).when(maxHttpPostSize -> maxHttpPostSize != 0)
if (tomcatProperties.getMaxHttpPostSize() != 0) { .to(maxHttpPostSize -> customizeMaxHttpPostSize(factory, maxHttpPostSize));
customizeMaxHttpPostSize(factory, tomcatProperties.getMaxHttpPostSize()); propertyMapper.from(tomcatProperties::getAccesslog).when(ServerProperties.Tomcat.Accesslog::isEnabled)
} .to(enabled -> customizeAccessLog(tomcatProperties, factory));
if (tomcatProperties.getAccesslog().isEnabled()) { propertyMapper.from(tomcatProperties::getUriEncoding).whenNonNull().to(factory::setUriEncoding);
customizeAccessLog(tomcatProperties, factory); propertyMapper.from(serverProperties::getConnectionTimeout).whenNonNull()
} .to(connectionTimeout -> customizeConnectionTimeout(factory, connectionTimeout));
if (tomcatProperties.getUriEncoding() != null) { propertyMapper.from(tomcatProperties::getMaxConnections).when(maxConnections -> maxConnections > 0)
factory.setUriEncoding(tomcatProperties.getUriEncoding()); .to(maxConnections -> customizeMaxConnections(factory, maxConnections));
} propertyMapper.from(tomcatProperties::getAcceptCount).when(acceptCount -> acceptCount > 0)
if (serverProperties.getConnectionTimeout() != null) { .to(acceptCount -> customizeAcceptCount(factory, acceptCount));
customizeConnectionTimeout(factory, serverProperties.getConnectionTimeout());
}
if (tomcatProperties.getMaxConnections() > 0) {
customizeMaxConnections(factory, tomcatProperties.getMaxConnections());
}
if (tomcatProperties.getAcceptCount() > 0) {
customizeAcceptCount(factory, tomcatProperties.getAcceptCount());
}
customizeStaticResources(serverProperties.getTomcat().getResource(), factory); customizeStaticResources(serverProperties.getTomcat().getResource(), factory);
} }
......
...@@ -22,6 +22,7 @@ import io.undertow.UndertowOptions; ...@@ -22,6 +22,7 @@ import io.undertow.UndertowOptions;
import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.cloud.CloudPlatform; import org.springframework.boot.cloud.CloudPlatform;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.boot.web.embedded.undertow.ConfigurableUndertowWebServerFactory; import org.springframework.boot.web.embedded.undertow.ConfigurableUndertowWebServerFactory;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
...@@ -30,6 +31,7 @@ import org.springframework.core.env.Environment; ...@@ -30,6 +31,7 @@ import org.springframework.core.env.Environment;
* servers. * servers.
* *
* @author Brian Clozel * @author Brian Clozel
* @author Yulin Qin
*/ */
public final class UndertowCustomizer { public final class UndertowCustomizer {
...@@ -41,38 +43,27 @@ public final class UndertowCustomizer { ...@@ -41,38 +43,27 @@ 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();
if (undertowProperties.getBufferSize() != null) {
factory.setBufferSize(undertowProperties.getBufferSize()); PropertyMapper propertyMapper = PropertyMapper.get();
} propertyMapper.from(undertowProperties::getBufferSize).whenNonNull().to(factory::setBufferSize);
if (undertowProperties.getIoThreads() != null) { propertyMapper.from(undertowProperties::getIoThreads).whenNonNull().to(factory::setIoThreads);
factory.setIoThreads(undertowProperties.getIoThreads()); propertyMapper.from(undertowProperties::getWorkerThreads).whenNonNull().to(factory::setWorkerThreads);
} propertyMapper.from(undertowProperties::getDirectBuffers).whenNonNull().to(factory::setUseDirectBuffers);
if (undertowProperties.getWorkerThreads() != null) { propertyMapper.from(accesslogProperties::getEnabled).whenNonNull().to(factory::setAccessLogEnabled);
factory.setWorkerThreads(undertowProperties.getWorkerThreads()); propertyMapper.from(accesslogProperties::getDir).to(factory::setAccessLogDirectory);
} propertyMapper.from(accesslogProperties::getPattern).to(factory::setAccessLogPattern);
if (undertowProperties.getDirectBuffers() != null) { propertyMapper.from(accesslogProperties::getPrefix).to(factory::setAccessLogPrefix);
factory.setUseDirectBuffers(undertowProperties.getDirectBuffers()); propertyMapper.from(accesslogProperties::getSuffix).to(factory::setAccessLogSuffix);
} propertyMapper.from(accesslogProperties::isRotate).to(factory::setAccessLogRotate);
if (undertowProperties.getAccesslog().getEnabled() != null) { propertyMapper.from(() -> getOrDeduceUseForwardHeaders(serverProperties, environment)).to(factory::setUseForwardHeaders);
factory.setAccessLogEnabled(accesslogProperties.getEnabled());
} propertyMapper.from(serverProperties::getMaxHttpHeaderSize).when(maxHttpHeaderSize -> maxHttpHeaderSize > 0)
factory.setAccessLogDirectory(accesslogProperties.getDir()); .to(maxHttpHeaderSize -> customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize));
factory.setAccessLogPattern(accesslogProperties.getPattern()); propertyMapper.from(undertowProperties::getMaxHttpPostSize).when(maxHttpPostSize -> maxHttpPostSize > 0)
factory.setAccessLogPrefix(accesslogProperties.getPrefix()); .to(maxHttpPostSize -> customizeMaxHttpPostSize(factory, maxHttpPostSize));
factory.setAccessLogSuffix(accesslogProperties.getSuffix()); propertyMapper.from(serverProperties::getConnectionTimeout).whenNonNull()
factory.setAccessLogRotate(accesslogProperties.isRotate()); .to(connectionTimeout -> customizeConnectionTimeout(factory, connectionTimeout));
factory.setUseForwardHeaders( factory.addDeploymentInfoCustomizers(deploymentInfo -> deploymentInfo
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
.setEagerFilterInit(undertowProperties.isEagerFilterInit())); .setEagerFilterInit(undertowProperties.isEagerFilterInit()));
} }
......
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