@ -16,10 +16,6 @@
package org.springframework.boot.autoconfigure.web.embedded ;
package org.springframework.boot.autoconfigure.web.embedded ;
import java.time.Duration ;
import java.util.List ;
import java.util.stream.Collectors ;
import org.apache.catalina.Lifecycle ;
import org.apache.catalina.Lifecycle ;
import org.apache.catalina.valves.AccessLogValve ;
import org.apache.catalina.valves.AccessLogValve ;
import org.apache.catalina.valves.ErrorReportValve ;
import org.apache.catalina.valves.ErrorReportValve ;
@ -29,7 +25,6 @@ import org.apache.coyote.ProtocolHandler;
import org.apache.coyote.UpgradeProtocol ;
import org.apache.coyote.UpgradeProtocol ;
import org.apache.coyote.http11.AbstractHttp11Protocol ;
import org.apache.coyote.http11.AbstractHttp11Protocol ;
import org.apache.coyote.http2.Http2Protocol ;
import org.apache.coyote.http2.Http2Protocol ;
import org.springframework.boot.autoconfigure.web.ErrorProperties ;
import org.springframework.boot.autoconfigure.web.ErrorProperties ;
import org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeAttribute ;
import org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeAttribute ;
import org.springframework.boot.autoconfigure.web.ServerProperties ;
import org.springframework.boot.autoconfigure.web.ServerProperties ;
@ -44,6 +39,11 @@ import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils ;
import org.springframework.util.StringUtils ;
import org.springframework.util.unit.DataSize ;
import org.springframework.util.unit.DataSize ;
import java.time.Duration ;
import java.util.List ;
import java.util.function.ObjIntConsumer ;
import java.util.stream.Collectors ;
/ * *
/ * *
* Customization for Tomcat - specific features common for both Servlet and Reactive
* Customization for Tomcat - specific features common for both Servlet and Reactive
* servers .
* servers .
@ -59,6 +59,8 @@ import org.springframework.util.unit.DataSize;
* @author Rafiullah Hamedy
* @author Rafiullah Hamedy
* @author Victor Mandujano
* @author Victor Mandujano
* @author Parviz Rozikov
* @author Parviz Rozikov
* @author Florian Storz
* @author Michael Weidmann
* @since 2.0 .0
* @since 2.0 .0
* /
* /
public class TomcatWebServerFactoryCustomizer
public class TomcatWebServerFactoryCustomizer
@ -95,6 +97,9 @@ public class TomcatWebServerFactoryCustomizer
propertyMapper . from ( this . serverProperties . getMaxHttpRequestHeaderSize ( ) ) . whenNonNull ( ) . asInt ( DataSize : : toBytes )
propertyMapper . from ( this . serverProperties . getMaxHttpRequestHeaderSize ( ) ) . whenNonNull ( ) . asInt ( DataSize : : toBytes )
. when ( this : : isPositive )
. when ( this : : isPositive )
. to ( ( maxHttpRequestHeaderSize ) - > customizeMaxHttpRequestHeaderSize ( factory , maxHttpRequestHeaderSize ) ) ;
. to ( ( maxHttpRequestHeaderSize ) - > customizeMaxHttpRequestHeaderSize ( factory , maxHttpRequestHeaderSize ) ) ;
propertyMapper . from ( tomcatProperties : : getMaxHttpResponseHeaderSize ) . whenNonNull ( ) . asInt ( DataSize : : toBytes )
. when ( this : : isPositive ) . to ( ( maxHttpResponseHeaderSize ) - > customizeMaxHttpResponseHeaderSize ( factory ,
maxHttpResponseHeaderSize ) ) ;
propertyMapper . from ( tomcatProperties : : getMaxSwallowSize ) . whenNonNull ( ) . asInt ( DataSize : : toBytes )
propertyMapper . from ( tomcatProperties : : getMaxSwallowSize ) . whenNonNull ( ) . asInt ( DataSize : : toBytes )
. to ( ( maxSwallowSize ) - > customizeMaxSwallowSize ( factory , maxSwallowSize ) ) ;
. to ( ( maxSwallowSize ) - > customizeMaxSwallowSize ( factory , maxSwallowSize ) ) ;
propertyMapper . from ( tomcatProperties : : getMaxHttpFormPostSize ) . asInt ( DataSize : : toBytes )
propertyMapper . from ( tomcatProperties : : getMaxHttpFormPostSize ) . asInt ( DataSize : : toBytes )
@ -129,22 +134,14 @@ public class TomcatWebServerFactoryCustomizer
return value > 0 ;
return value > 0 ;
}
}
@SuppressWarnings ( "rawtypes" )
private void customizeAcceptCount ( ConfigurableTomcatWebServerFactory factory , int acceptCount ) {
private void customizeAcceptCount ( ConfigurableTomcatWebServerFactory factory , int acceptCount ) {
factory . addConnectorCustomizers ( ( connector ) - > {
customizeHandler ( factory , acceptCount , AbstractProtocol . class , AbstractProtocol : : setAcceptCount ) ;
ProtocolHandler handler = connector . getProtocolHandler ( ) ;
if ( handler instanceof AbstractProtocol < ? > protocol ) {
protocol . setAcceptCount ( acceptCount ) ;
}
} ) ;
}
}
@SuppressWarnings ( "rawtypes" )
private void customizeProcessorCache ( ConfigurableTomcatWebServerFactory factory , int processorCache ) {
private void customizeProcessorCache ( ConfigurableTomcatWebServerFactory factory , int processorCache ) {
factory . addConnectorCustomizers ( ( connector ) - > {
customizeHandler ( factory , processorCache , AbstractProtocol . class , AbstractProtocol : : setProcessorCache ) ;
ProtocolHandler handler = connector . getProtocolHandler ( ) ;
if ( handler instanceof AbstractProtocol ) {
( ( AbstractProtocol < ? > ) handler ) . setProcessorCache ( processorCache ) ;
}
} ) ;
}
}
private void customizeKeepAliveTimeout ( ConfigurableTomcatWebServerFactory factory , Duration keepAliveTimeout ) {
private void customizeKeepAliveTimeout ( ConfigurableTomcatWebServerFactory factory , Duration keepAliveTimeout ) {
@ -161,31 +158,21 @@ public class TomcatWebServerFactoryCustomizer
} ) ;
} ) ;
}
}
@SuppressWarnings ( "rawtypes" )
private void customizeMaxKeepAliveRequests ( ConfigurableTomcatWebServerFactory factory , int maxKeepAliveRequests ) {
private void customizeMaxKeepAliveRequests ( ConfigurableTomcatWebServerFactory factory , int maxKeepAliveRequests ) {
factory . addConnectorCustomizers ( ( connector ) - > {
customizeHandler ( factory , maxKeepAliveRequests , AbstractHttp11Protocol . class ,
ProtocolHandler handler = connector . getProtocolHandler ( ) ;
AbstractHttp11Protocol : : setMaxKeepAliveRequests ) ;
if ( handler instanceof AbstractHttp11Protocol < ? > protocol ) {
protocol . setMaxKeepAliveRequests ( maxKeepAliveRequests ) ;
}
} ) ;
}
}
@SuppressWarnings ( "rawtypes" )
private void customizeMaxConnections ( ConfigurableTomcatWebServerFactory factory , int maxConnections ) {
private void customizeMaxConnections ( ConfigurableTomcatWebServerFactory factory , int maxConnections ) {
factory . addConnectorCustomizers ( ( connector ) - > {
customizeHandler ( factory , maxConnections , AbstractProtocol . class , AbstractProtocol : : setMaxConnections ) ;
ProtocolHandler handler = connector . getProtocolHandler ( ) ;
if ( handler instanceof AbstractProtocol < ? > protocol ) {
protocol . setMaxConnections ( maxConnections ) ;
}
} ) ;
}
}
@SuppressWarnings ( "rawtypes" )
private void customizeConnectionTimeout ( ConfigurableTomcatWebServerFactory factory , Duration connectionTimeout ) {
private void customizeConnectionTimeout ( ConfigurableTomcatWebServerFactory factory , Duration connectionTimeout ) {
factory . addConnectorCustomizers ( ( connector ) - > {
customizeHandler ( factory , ( int ) connectionTimeout . toMillis ( ) , AbstractProtocol . class ,
ProtocolHandler handler = connector . getProtocolHandler ( ) ;
AbstractProtocol : : setConnectionTimeout ) ;
if ( handler instanceof AbstractProtocol < ? > protocol ) {
protocol . setConnectionTimeout ( ( int ) connectionTimeout . toMillis ( ) ) ;
}
} ) ;
}
}
private void customizeRelaxedPathChars ( ConfigurableTomcatWebServerFactory factory , String relaxedChars ) {
private void customizeRelaxedPathChars ( ConfigurableTomcatWebServerFactory factory , String relaxedChars ) {
@ -248,40 +235,40 @@ public class TomcatWebServerFactoryCustomizer
@SuppressWarnings ( "rawtypes" )
@SuppressWarnings ( "rawtypes" )
private void customizeMaxThreads ( ConfigurableTomcatWebServerFactory factory , int maxThreads ) {
private void customizeMaxThreads ( ConfigurableTomcatWebServerFactory factory , int maxThreads ) {
factory . addConnectorCustomizers ( ( connector ) - > {
customizeHandler ( factory , maxThreads , AbstractProtocol . class , AbstractProtocol : : setMaxThreads ) ;
ProtocolHandler handler = connector . getProtocolHandler ( ) ;
if ( handler instanceof AbstractProtocol protocol ) {
protocol . setMaxThreads ( maxThreads ) ;
}
} ) ;
}
}
@SuppressWarnings ( "rawtypes" )
@SuppressWarnings ( "rawtypes" )
private void customizeMinThreads ( ConfigurableTomcatWebServerFactory factory , int minSpareThreads ) {
private void customizeMinThreads ( ConfigurableTomcatWebServerFactory factory , int minSpareThreads ) {
factory . addConnectorCustomizers ( ( connector ) - > {
customizeHandler ( factory , minSpareThreads , AbstractProtocol . class , AbstractProtocol : : setMinSpareThreads ) ;
ProtocolHandler handler = connector . getProtocolHandler ( ) ;
if ( handler instanceof AbstractProtocol protocol ) {
protocol . setMinSpareThreads ( minSpareThreads ) ;
}
} ) ;
}
}
@SuppressWarnings ( "rawtypes" )
@SuppressWarnings ( "rawtypes" )
private void customizeMaxHttpRequestHeaderSize ( ConfigurableTomcatWebServerFactory factory ,
private void customizeMaxHttpRequestHeaderSize ( ConfigurableTomcatWebServerFactory factory ,
int maxHttpRequestHeaderSize ) {
int maxHttpRequestHeaderSize ) {
factory . addConnectorCustomizers ( ( connector ) - > {
customizeHandler ( factory , maxHttpRequestHeaderSize , AbstractHttp11Protocol . class ,
ProtocolHandler handler = connector . getProtocolHandler ( ) ;
AbstractHttp11Protocol : : setMaxHttpRequestHeaderSize ) ;
if ( handler instanceof AbstractHttp11Protocol protocol ) {
protocol . setMaxHttpRequestHeaderSize ( maxHttpRequestHeaderSize ) ;
}
} ) ;
}
}
@SuppressWarnings ( "rawtypes" )
private void customizeMaxHttpResponseHeaderSize ( ConfigurableTomcatWebServerFactory factory ,
int maxHttpResponseHeaderSize ) {
customizeHandler ( factory , maxHttpResponseHeaderSize , AbstractHttp11Protocol . class ,
AbstractHttp11Protocol : : setMaxHttpResponseHeaderSize ) ;
}
@SuppressWarnings ( "rawtypes" )
private void customizeMaxSwallowSize ( ConfigurableTomcatWebServerFactory factory , int maxSwallowSize ) {
private void customizeMaxSwallowSize ( ConfigurableTomcatWebServerFactory factory , int maxSwallowSize ) {
customizeHandler ( factory , maxSwallowSize , AbstractHttp11Protocol . class ,
AbstractHttp11Protocol : : setMaxSwallowSize ) ;
}
private < T extends ProtocolHandler > void customizeHandler ( ConfigurableTomcatWebServerFactory factory , int value ,
Class < T > type , ObjIntConsumer < T > consumer ) {
factory . addConnectorCustomizers ( ( connector ) - > {
factory . addConnectorCustomizers ( ( connector ) - > {
ProtocolHandler handler = connector . getProtocolHandler ( ) ;
ProtocolHandler handler = connector . getProtocolHandler ( ) ;
if ( handler instanceof AbstractHttp11Protocol < ? > protocol ) {
if ( type. isAssignableFrom ( handler . getClass ( ) ) ) {
protocol . setMaxSwallowSize ( maxSwallowSize ) ;
consumer. accept ( type . cast ( handler ) , valu e) ;
}
}
} ) ;
} ) ;
}
}