Merge pull request #11772 from qinnnyul:master
* pr/11772: Polish "Migrate server customizer to PropertyMapper" Migrate server customizer to PropertyMapper
This commit is contained in:
commit
24ec441718
|
|
@ -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,26 @@ 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);
|
||||||
if (jettyProperties.getSelectors() != null) {
|
propertyMapper.from(jettyProperties::getSelectors).whenNonNull()
|
||||||
factory.setSelectors(jettyProperties.getSelectors());
|
.to(factory::setSelectors);
|
||||||
}
|
propertyMapper.from(serverProperties::getMaxHttpHeaderSize)
|
||||||
if (serverProperties.getMaxHttpHeaderSize() > 0) {
|
.when(JettyCustomizer::isPositive).to(maxHttpHeaderSize ->
|
||||||
customizeMaxHttpHeaderSize(factory, serverProperties.getMaxHttpHeaderSize());
|
customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize));
|
||||||
}
|
propertyMapper.from(jettyProperties::getMaxHttpPostSize)
|
||||||
if (jettyProperties.getMaxHttpPostSize() > 0) {
|
.when(JettyCustomizer::isPositive)
|
||||||
customizeMaxHttpPostSize(factory, jettyProperties.getMaxHttpPostSize());
|
.to(maxHttpPostSize -> customizeMaxHttpPostSize(factory, maxHttpPostSize));
|
||||||
}
|
propertyMapper.from(serverProperties::getConnectionTimeout).whenNonNull()
|
||||||
if (serverProperties.getConnectionTimeout() != null) {
|
.to(connectionTimeout -> customizeConnectionTimeout(factory, connectionTimeout));
|
||||||
customizeConnectionTimeout(factory, serverProperties.getConnectionTimeout());
|
propertyMapper.from(jettyProperties::getAccesslog)
|
||||||
}
|
.when(ServerProperties.Jetty.Accesslog::isEnabled)
|
||||||
if (jettyProperties.getAccesslog().isEnabled()) {
|
.to(accesslog -> customizeAccessLog(factory, accesslog));
|
||||||
customizeAccessLog(factory, jettyProperties.getAccesslog());
|
}
|
||||||
}
|
|
||||||
|
private static boolean isPositive(Integer value) {
|
||||||
|
return value > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean getOrDeduceUseForwardHeaders(ServerProperties serverProperties,
|
private static boolean getOrDeduceUseForwardHeaders(ServerProperties serverProperties,
|
||||||
|
|
|
||||||
|
|
@ -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,8 @@ import org.springframework.util.StringUtils;
|
||||||
* servers.
|
* servers.
|
||||||
*
|
*
|
||||||
* @author Brian Clozel
|
* @author Brian Clozel
|
||||||
|
* @author Yulin Qin
|
||||||
|
* @author Stephane Nicoll
|
||||||
* @since 2.0.0
|
* @since 2.0.0
|
||||||
*/
|
*/
|
||||||
public final class TomcatCustomizer {
|
public final class TomcatCustomizer {
|
||||||
|
|
@ -46,47 +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();
|
||||||
if (tomcatProperties.getBasedir() != null) {
|
PropertyMapper propertyMapper = PropertyMapper.get();
|
||||||
factory.setBaseDirectory(tomcatProperties.getBasedir());
|
propertyMapper.from(tomcatProperties::getBasedir).whenNonNull()
|
||||||
}
|
.to(factory::setBaseDirectory);
|
||||||
if (tomcatProperties.getBackgroundProcessorDelay() != null) {
|
propertyMapper.from(tomcatProperties::getBackgroundProcessorDelay).whenNonNull()
|
||||||
factory.setBackgroundProcessorDelay(
|
.as(Duration::getSeconds).as(Long::intValue)
|
||||||
(int) tomcatProperties.getBackgroundProcessorDelay().getSeconds());
|
.to(factory::setBackgroundProcessorDelay);
|
||||||
}
|
|
||||||
customizeRemoteIpValve(serverProperties, environment, factory);
|
customizeRemoteIpValve(serverProperties, environment, factory);
|
||||||
if (tomcatProperties.getMaxThreads() > 0) {
|
propertyMapper.from(tomcatProperties::getMaxThreads)
|
||||||
customizeMaxThreads(factory, tomcatProperties.getMaxThreads());
|
.when(TomcatCustomizer::isPositive)
|
||||||
}
|
.to(maxThreads -> customizeMaxThreads(factory, tomcatProperties.getMaxThreads()));
|
||||||
if (tomcatProperties.getMinSpareThreads() > 0) {
|
propertyMapper.from(tomcatProperties::getMinSpareThreads)
|
||||||
customizeMinThreads(factory, tomcatProperties.getMinSpareThreads());
|
.when(TomcatCustomizer::isPositive)
|
||||||
}
|
.to(minSpareThreads -> customizeMinThreads(factory, minSpareThreads));
|
||||||
int maxHttpHeaderSize = (serverProperties.getMaxHttpHeaderSize() > 0
|
propertyMapper.from(() -> determineMaxHttpHeaderSize(serverProperties, tomcatProperties))
|
||||||
? serverProperties.getMaxHttpHeaderSize()
|
.when(TomcatCustomizer::isPositive)
|
||||||
: tomcatProperties.getMaxHttpHeaderSize());
|
.to(maxHttpHeaderSize -> customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize));
|
||||||
if (maxHttpHeaderSize > 0) {
|
propertyMapper.from(tomcatProperties::getMaxHttpPostSize)
|
||||||
customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize);
|
.when(maxHttpPostSize -> maxHttpPostSize != 0)
|
||||||
}
|
.to(maxHttpPostSize -> customizeMaxHttpPostSize(factory, maxHttpPostSize));
|
||||||
if (tomcatProperties.getMaxHttpPostSize() != 0) {
|
propertyMapper.from(tomcatProperties::getAccesslog)
|
||||||
customizeMaxHttpPostSize(factory, tomcatProperties.getMaxHttpPostSize());
|
.when(ServerProperties.Tomcat.Accesslog::isEnabled)
|
||||||
}
|
.to(enabled -> customizeAccessLog(tomcatProperties, factory));
|
||||||
if (tomcatProperties.getAccesslog().isEnabled()) {
|
propertyMapper.from(tomcatProperties::getUriEncoding).whenNonNull()
|
||||||
customizeAccessLog(tomcatProperties, factory);
|
.to(factory::setUriEncoding);
|
||||||
}
|
propertyMapper.from(serverProperties::getConnectionTimeout).whenNonNull()
|
||||||
if (tomcatProperties.getUriEncoding() != null) {
|
.to(connectionTimeout -> customizeConnectionTimeout(factory, connectionTimeout));
|
||||||
factory.setUriEncoding(tomcatProperties.getUriEncoding());
|
propertyMapper.from(tomcatProperties::getMaxConnections)
|
||||||
}
|
.when(TomcatCustomizer::isPositive)
|
||||||
if (serverProperties.getConnectionTimeout() != null) {
|
.to(maxConnections -> customizeMaxConnections(factory, maxConnections));
|
||||||
customizeConnectionTimeout(factory, serverProperties.getConnectionTimeout());
|
propertyMapper.from(tomcatProperties::getAcceptCount)
|
||||||
}
|
.when(TomcatCustomizer::isPositive)
|
||||||
if (tomcatProperties.getMaxConnections() > 0) {
|
.to(acceptCount -> customizeAcceptCount(factory, acceptCount));
|
||||||
customizeMaxConnections(factory, tomcatProperties.getMaxConnections());
|
|
||||||
}
|
|
||||||
if (tomcatProperties.getAcceptCount() > 0) {
|
|
||||||
customizeAcceptCount(factory, tomcatProperties.getAcceptCount());
|
|
||||||
}
|
|
||||||
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) -> {
|
||||||
|
|
|
||||||
|
|
@ -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,8 @@ import org.springframework.core.env.Environment;
|
||||||
* servers.
|
* servers.
|
||||||
*
|
*
|
||||||
* @author Brian Clozel
|
* @author Brian Clozel
|
||||||
|
* @author Yulin Qin
|
||||||
|
* @author Stephane Nicoll
|
||||||
*/
|
*/
|
||||||
public final class UndertowCustomizer {
|
public final class UndertowCustomizer {
|
||||||
|
|
||||||
|
|
@ -41,41 +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();
|
||||||
if (undertowProperties.getBufferSize() != null) {
|
PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||||
factory.setBufferSize(undertowProperties.getBufferSize());
|
propertyMapper.from(undertowProperties::getBufferSize).to(factory::setBufferSize);
|
||||||
}
|
propertyMapper.from(undertowProperties::getIoThreads).to(factory::setIoThreads);
|
||||||
if (undertowProperties.getIoThreads() != null) {
|
propertyMapper.from(undertowProperties::getWorkerThreads)
|
||||||
factory.setIoThreads(undertowProperties.getIoThreads());
|
.to(factory::setWorkerThreads);
|
||||||
}
|
propertyMapper.from(undertowProperties::getDirectBuffers)
|
||||||
if (undertowProperties.getWorkerThreads() != null) {
|
.to(factory::setUseDirectBuffers);
|
||||||
factory.setWorkerThreads(undertowProperties.getWorkerThreads());
|
propertyMapper.from(accesslogProperties::getEnabled)
|
||||||
}
|
.to(factory::setAccessLogEnabled);
|
||||||
if (undertowProperties.getDirectBuffers() != null) {
|
propertyMapper.from(accesslogProperties::getDir)
|
||||||
factory.setUseDirectBuffers(undertowProperties.getDirectBuffers());
|
.to(factory::setAccessLogDirectory);
|
||||||
}
|
propertyMapper.from(accesslogProperties::getPattern)
|
||||||
if (undertowProperties.getAccesslog().getEnabled() != null) {
|
.to(factory::setAccessLogPattern);
|
||||||
factory.setAccessLogEnabled(accesslogProperties.getEnabled());
|
propertyMapper.from(accesslogProperties::getPrefix)
|
||||||
}
|
.to(factory::setAccessLogPrefix);
|
||||||
factory.setAccessLogDirectory(accesslogProperties.getDir());
|
propertyMapper.from(accesslogProperties::getSuffix)
|
||||||
factory.setAccessLogPattern(accesslogProperties.getPattern());
|
.to(factory::setAccessLogSuffix);
|
||||||
factory.setAccessLogPrefix(accesslogProperties.getPrefix());
|
propertyMapper.from(accesslogProperties::isRotate)
|
||||||
factory.setAccessLogSuffix(accesslogProperties.getSuffix());
|
.to(factory::setAccessLogRotate);
|
||||||
factory.setAccessLogRotate(accesslogProperties.isRotate());
|
propertyMapper.from(() ->
|
||||||
factory.setUseForwardHeaders(
|
getOrDeduceUseForwardHeaders(serverProperties, environment)).to(
|
||||||
getOrDeduceUseForwardHeaders(serverProperties, environment));
|
factory::setUseForwardHeaders);
|
||||||
if (serverProperties.getMaxHttpHeaderSize() > 0) {
|
propertyMapper.from(serverProperties::getMaxHttpHeaderSize)
|
||||||
customizeMaxHttpHeaderSize(factory, serverProperties.getMaxHttpHeaderSize());
|
.when(UndertowCustomizer::isPositive)
|
||||||
}
|
.to(maxHttpHeaderSize ->
|
||||||
if (undertowProperties.getMaxHttpPostSize() > 0) {
|
customizeMaxHttpHeaderSize(factory, maxHttpHeaderSize));
|
||||||
customizeMaxHttpPostSize(factory, undertowProperties.getMaxHttpPostSize());
|
propertyMapper.from(undertowProperties::getMaxHttpPostSize)
|
||||||
}
|
.when(UndertowCustomizer::isPositive)
|
||||||
if (serverProperties.getConnectionTimeout() != null) {
|
.to(maxHttpPostSize ->
|
||||||
customizeConnectionTimeout(factory, serverProperties.getConnectionTimeout());
|
customizeMaxHttpPostSize(factory, maxHttpPostSize));
|
||||||
}
|
propertyMapper.from(serverProperties::getConnectionTimeout)
|
||||||
factory.addDeploymentInfoCustomizers((deploymentInfo) -> deploymentInfo
|
.to(connectionTimeout ->
|
||||||
|
customizeConnectionTimeout(factory, connectionTimeout));
|
||||||
|
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(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue