From ad767ca4e150bb25b2d73ce794950d51b7cb2502 Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Thu, 4 Apr 2019 12:25:13 -0700 Subject: [PATCH] Polish "Simplify the configuration of the ProtocolHandler" Closes gh-16342 --- ...ReactiveWebServerFactoryConfiguration.java | 2 +- .../ServletWebServerFactoryConfiguration.java | 2 +- .../ConfigurableTomcatWebServerFactory.java | 2 +- .../TomcatReactiveWebServerFactory.java | 24 ++++++++++++------- .../tomcat/TomcatServletWebServerFactory.java | 24 ++++++++++++------- 5 files changed, 35 insertions(+), 19 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryConfiguration.java index 22d2070a9f..78f7bd2ecd 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryConfiguration.java @@ -78,7 +78,7 @@ abstract class ReactiveWebServerFactoryConfiguration { public TomcatReactiveWebServerFactory tomcatReactiveWebServerFactory( ObjectProvider connectorCustomizers, ObjectProvider contextCustomizers, - ObjectProvider protocolHandlerCustomizers) { + ObjectProvider> protocolHandlerCustomizers) { TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory(); factory.getTomcatConnectorCustomizers().addAll( connectorCustomizers.orderedStream().collect(Collectors.toList())); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryConfiguration.java index a336f9ccd4..fceccda369 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryConfiguration.java @@ -68,7 +68,7 @@ class ServletWebServerFactoryConfiguration { public TomcatServletWebServerFactory tomcatServletWebServerFactory( ObjectProvider connectorCustomizers, ObjectProvider contextCustomizers, - ObjectProvider protocolHandlerCustomizers) { + ObjectProvider> protocolHandlerCustomizers) { TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(); factory.getTomcatConnectorCustomizers().addAll( connectorCustomizers.orderedStream().collect(Collectors.toList())); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/ConfigurableTomcatWebServerFactory.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/ConfigurableTomcatWebServerFactory.java index bcbabb7289..f4160f8bce 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/ConfigurableTomcatWebServerFactory.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/ConfigurableTomcatWebServerFactory.java @@ -74,7 +74,7 @@ public interface ConfigurableTomcatWebServerFactory extends ConfigurableWebServe * @param tomcatProtocolHandlerCustomizers the customizers to add */ void addProtocolHandlerCustomizers( - TomcatProtocolHandlerCustomizer... tomcatProtocolHandlerCustomizers); + TomcatProtocolHandlerCustomizer... tomcatProtocolHandlerCustomizers); /** * Set the character encoding to use for URL decoding. If not specified 'UTF-8' will diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactory.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactory.java index d97737945c..cb97180cf9 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactory.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactory.java @@ -34,9 +34,11 @@ import org.apache.catalina.core.AprLifecycleListener; import org.apache.catalina.loader.WebappLoader; import org.apache.catalina.startup.Tomcat; import org.apache.coyote.AbstractProtocol; +import org.apache.coyote.ProtocolHandler; import org.apache.coyote.http2.Http2Protocol; import org.apache.tomcat.util.scan.StandardJarScanFilter; +import org.springframework.boot.util.LambdaSafe; import org.springframework.boot.web.reactive.server.AbstractReactiveWebServerFactory; import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory; import org.springframework.boot.web.server.WebServer; @@ -72,7 +74,7 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac private List tomcatConnectorCustomizers = new ArrayList<>(); - private List tomcatProtocolHandlerCustomizers = new ArrayList<>(); + private List> tomcatProtocolHandlerCustomizers = new ArrayList<>(); private String protocol = DEFAULT_PROTOCOL; @@ -170,10 +172,7 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac if (connector.getProtocolHandler() instanceof AbstractProtocol) { customizeProtocol((AbstractProtocol) connector.getProtocolHandler()); } - - this.tomcatProtocolHandlerCustomizers.forEach( - (customizer) -> customizer.customize(connector.getProtocolHandler())); - + invokeProtocolHandlerCustomizers(connector); if (getUriEncoding() != null) { connector.setURIEncoding(getUriEncoding().name()); } @@ -190,6 +189,15 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac } } + @SuppressWarnings("unchecked") + private void invokeProtocolHandlerCustomizers(Connector connector) { + ProtocolHandler protocolHandler = connector.getProtocolHandler(); + LambdaSafe + .callbacks(TomcatProtocolHandlerCustomizer.class, + this.tomcatProtocolHandlerCustomizers, protocolHandler) + .invoke((customizer) -> customizer.customize(protocolHandler)); + } + private void customizeProtocol(AbstractProtocol protocol) { if (getAddress() != null) { protocol.setAddress(getAddress()); @@ -287,7 +295,7 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac * @param tomcatProtocolHandlerCustomizers the customizers to set */ public void setTomcatProtocolHandlerCustomizers( - Collection tomcatProtocolHandlerCustomizers) { + Collection> tomcatProtocolHandlerCustomizers) { Assert.notNull(tomcatProtocolHandlerCustomizers, "TomcatProtocolHandlerCustomizers must not be null"); this.tomcatProtocolHandlerCustomizers = new ArrayList<>( @@ -301,7 +309,7 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac */ @Override public void addProtocolHandlerCustomizers( - TomcatProtocolHandlerCustomizer... tomcatProtocolHandlerCustomizers) { + TomcatProtocolHandlerCustomizer... tomcatProtocolHandlerCustomizers) { Assert.notNull(tomcatProtocolHandlerCustomizers, "TomcatProtocolHandlerCustomizers must not be null"); this.tomcatProtocolHandlerCustomizers @@ -313,7 +321,7 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac * will be applied to the Tomcat {@link Connector}. * @return the customizers that will be applied */ - public Collection getTomcatProtocolHandlerCustomizers() { + public Collection> getTomcatProtocolHandlerCustomizers() { return this.tomcatProtocolHandlerCustomizers; } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java index 1c1e41f92e..7b0e9eb653 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java @@ -58,9 +58,11 @@ import org.apache.catalina.webresources.AbstractResourceSet; import org.apache.catalina.webresources.EmptyResource; import org.apache.catalina.webresources.StandardRoot; import org.apache.coyote.AbstractProtocol; +import org.apache.coyote.ProtocolHandler; import org.apache.coyote.http2.Http2Protocol; import org.apache.tomcat.util.scan.StandardJarScanFilter; +import org.springframework.boot.util.LambdaSafe; import org.springframework.boot.web.server.ErrorPage; import org.springframework.boot.web.server.MimeMappings; import org.springframework.boot.web.server.WebServer; @@ -117,7 +119,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto private List tomcatConnectorCustomizers = new ArrayList<>(); - private List tomcatProtocolHandlerCustomizers = new ArrayList<>(); + private List> tomcatProtocolHandlerCustomizers = new ArrayList<>(); private List additionalTomcatConnectors = new ArrayList<>(); @@ -305,10 +307,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto if (connector.getProtocolHandler() instanceof AbstractProtocol) { customizeProtocol((AbstractProtocol) connector.getProtocolHandler()); } - - this.tomcatProtocolHandlerCustomizers.forEach( - (customizer) -> customizer.customize(connector.getProtocolHandler())); - + invokeProtocolHandlerCustomizers(connector); if (getUriEncoding() != null) { connector.setURIEncoding(getUriEncoding().name()); } @@ -331,6 +330,15 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto } } + @SuppressWarnings("unchecked") + private void invokeProtocolHandlerCustomizers(Connector connector) { + ProtocolHandler protocolHandler = connector.getProtocolHandler(); + LambdaSafe + .callbacks(TomcatProtocolHandlerCustomizer.class, + this.tomcatProtocolHandlerCustomizers, protocolHandler) + .invoke((customizer) -> customizer.customize(protocolHandler)); + } + private void customizeSsl(Connector connector) { new SslConnectorCustomizer(getSsl(), getSslStoreProvider()).customize(connector); if (getHttp2() != null && getHttp2().isEnabled()) { @@ -631,7 +639,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto * @param tomcatProtocolHandlerCustomizer the customizers to set */ public void setTomcatProtocolHandlerCustomizers( - Collection tomcatProtocolHandlerCustomizer) { + Collection> tomcatProtocolHandlerCustomizer) { Assert.notNull(tomcatProtocolHandlerCustomizer, "TomcatProtocolHandlerCustomizers must not be null"); this.tomcatProtocolHandlerCustomizers = new ArrayList<>( @@ -645,7 +653,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto */ @Override public void addProtocolHandlerCustomizers( - TomcatProtocolHandlerCustomizer... tomcatProtocolHandlerCustomizers) { + TomcatProtocolHandlerCustomizer... tomcatProtocolHandlerCustomizers) { Assert.notNull(tomcatProtocolHandlerCustomizers, "TomcatProtocolHandlerCustomizers must not be null"); this.tomcatProtocolHandlerCustomizers @@ -657,7 +665,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto * will be applied to the Tomcat {@link Connector}. * @return the customizers that will be applied */ - public Collection getTomcatProtocolHandlerCustomizers() { + public Collection> getTomcatProtocolHandlerCustomizers() { return this.tomcatProtocolHandlerCustomizers; }