Polish "Simplify the configuration of the ProtocolHandler"

Closes gh-16342
pull/16474/head
Madhura Bhave 6 years ago
parent 2eaa64f82e
commit ad767ca4e1

@ -78,7 +78,7 @@ abstract class ReactiveWebServerFactoryConfiguration {
public TomcatReactiveWebServerFactory tomcatReactiveWebServerFactory( public TomcatReactiveWebServerFactory tomcatReactiveWebServerFactory(
ObjectProvider<TomcatConnectorCustomizer> connectorCustomizers, ObjectProvider<TomcatConnectorCustomizer> connectorCustomizers,
ObjectProvider<TomcatContextCustomizer> contextCustomizers, ObjectProvider<TomcatContextCustomizer> contextCustomizers,
ObjectProvider<TomcatProtocolHandlerCustomizer> protocolHandlerCustomizers) { ObjectProvider<TomcatProtocolHandlerCustomizer<?>> protocolHandlerCustomizers) {
TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory(); TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory();
factory.getTomcatConnectorCustomizers().addAll( factory.getTomcatConnectorCustomizers().addAll(
connectorCustomizers.orderedStream().collect(Collectors.toList())); connectorCustomizers.orderedStream().collect(Collectors.toList()));

@ -68,7 +68,7 @@ class ServletWebServerFactoryConfiguration {
public TomcatServletWebServerFactory tomcatServletWebServerFactory( public TomcatServletWebServerFactory tomcatServletWebServerFactory(
ObjectProvider<TomcatConnectorCustomizer> connectorCustomizers, ObjectProvider<TomcatConnectorCustomizer> connectorCustomizers,
ObjectProvider<TomcatContextCustomizer> contextCustomizers, ObjectProvider<TomcatContextCustomizer> contextCustomizers,
ObjectProvider<TomcatProtocolHandlerCustomizer> protocolHandlerCustomizers) { ObjectProvider<TomcatProtocolHandlerCustomizer<?>> protocolHandlerCustomizers) {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(); TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.getTomcatConnectorCustomizers().addAll( factory.getTomcatConnectorCustomizers().addAll(
connectorCustomizers.orderedStream().collect(Collectors.toList())); connectorCustomizers.orderedStream().collect(Collectors.toList()));

@ -74,7 +74,7 @@ public interface ConfigurableTomcatWebServerFactory extends ConfigurableWebServe
* @param tomcatProtocolHandlerCustomizers the customizers to add * @param tomcatProtocolHandlerCustomizers the customizers to add
*/ */
void addProtocolHandlerCustomizers( void addProtocolHandlerCustomizers(
TomcatProtocolHandlerCustomizer... tomcatProtocolHandlerCustomizers); TomcatProtocolHandlerCustomizer<?>... tomcatProtocolHandlerCustomizers);
/** /**
* Set the character encoding to use for URL decoding. If not specified 'UTF-8' will * Set the character encoding to use for URL decoding. If not specified 'UTF-8' will

@ -34,9 +34,11 @@ import org.apache.catalina.core.AprLifecycleListener;
import org.apache.catalina.loader.WebappLoader; import org.apache.catalina.loader.WebappLoader;
import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.Tomcat;
import org.apache.coyote.AbstractProtocol; import org.apache.coyote.AbstractProtocol;
import org.apache.coyote.ProtocolHandler;
import org.apache.coyote.http2.Http2Protocol; import org.apache.coyote.http2.Http2Protocol;
import org.apache.tomcat.util.scan.StandardJarScanFilter; 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.AbstractReactiveWebServerFactory;
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory; import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
import org.springframework.boot.web.server.WebServer; import org.springframework.boot.web.server.WebServer;
@ -72,7 +74,7 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac
private List<TomcatConnectorCustomizer> tomcatConnectorCustomizers = new ArrayList<>(); private List<TomcatConnectorCustomizer> tomcatConnectorCustomizers = new ArrayList<>();
private List<TomcatProtocolHandlerCustomizer> tomcatProtocolHandlerCustomizers = new ArrayList<>(); private List<TomcatProtocolHandlerCustomizer<?>> tomcatProtocolHandlerCustomizers = new ArrayList<>();
private String protocol = DEFAULT_PROTOCOL; private String protocol = DEFAULT_PROTOCOL;
@ -170,10 +172,7 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac
if (connector.getProtocolHandler() instanceof AbstractProtocol) { if (connector.getProtocolHandler() instanceof AbstractProtocol) {
customizeProtocol((AbstractProtocol<?>) connector.getProtocolHandler()); customizeProtocol((AbstractProtocol<?>) connector.getProtocolHandler());
} }
invokeProtocolHandlerCustomizers(connector);
this.tomcatProtocolHandlerCustomizers.forEach(
(customizer) -> customizer.customize(connector.getProtocolHandler()));
if (getUriEncoding() != null) { if (getUriEncoding() != null) {
connector.setURIEncoding(getUriEncoding().name()); 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) { private void customizeProtocol(AbstractProtocol<?> protocol) {
if (getAddress() != null) { if (getAddress() != null) {
protocol.setAddress(getAddress()); protocol.setAddress(getAddress());
@ -287,7 +295,7 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac
* @param tomcatProtocolHandlerCustomizers the customizers to set * @param tomcatProtocolHandlerCustomizers the customizers to set
*/ */
public void setTomcatProtocolHandlerCustomizers( public void setTomcatProtocolHandlerCustomizers(
Collection<? extends TomcatProtocolHandlerCustomizer> tomcatProtocolHandlerCustomizers) { Collection<? extends TomcatProtocolHandlerCustomizer<?>> tomcatProtocolHandlerCustomizers) {
Assert.notNull(tomcatProtocolHandlerCustomizers, Assert.notNull(tomcatProtocolHandlerCustomizers,
"TomcatProtocolHandlerCustomizers must not be null"); "TomcatProtocolHandlerCustomizers must not be null");
this.tomcatProtocolHandlerCustomizers = new ArrayList<>( this.tomcatProtocolHandlerCustomizers = new ArrayList<>(
@ -301,7 +309,7 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac
*/ */
@Override @Override
public void addProtocolHandlerCustomizers( public void addProtocolHandlerCustomizers(
TomcatProtocolHandlerCustomizer... tomcatProtocolHandlerCustomizers) { TomcatProtocolHandlerCustomizer<?>... tomcatProtocolHandlerCustomizers) {
Assert.notNull(tomcatProtocolHandlerCustomizers, Assert.notNull(tomcatProtocolHandlerCustomizers,
"TomcatProtocolHandlerCustomizers must not be null"); "TomcatProtocolHandlerCustomizers must not be null");
this.tomcatProtocolHandlerCustomizers this.tomcatProtocolHandlerCustomizers
@ -313,7 +321,7 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac
* will be applied to the Tomcat {@link Connector}. * will be applied to the Tomcat {@link Connector}.
* @return the customizers that will be applied * @return the customizers that will be applied
*/ */
public Collection<TomcatProtocolHandlerCustomizer> getTomcatProtocolHandlerCustomizers() { public Collection<TomcatProtocolHandlerCustomizer<?>> getTomcatProtocolHandlerCustomizers() {
return this.tomcatProtocolHandlerCustomizers; return this.tomcatProtocolHandlerCustomizers;
} }

@ -58,9 +58,11 @@ import org.apache.catalina.webresources.AbstractResourceSet;
import org.apache.catalina.webresources.EmptyResource; import org.apache.catalina.webresources.EmptyResource;
import org.apache.catalina.webresources.StandardRoot; import org.apache.catalina.webresources.StandardRoot;
import org.apache.coyote.AbstractProtocol; import org.apache.coyote.AbstractProtocol;
import org.apache.coyote.ProtocolHandler;
import org.apache.coyote.http2.Http2Protocol; import org.apache.coyote.http2.Http2Protocol;
import org.apache.tomcat.util.scan.StandardJarScanFilter; 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.ErrorPage;
import org.springframework.boot.web.server.MimeMappings; import org.springframework.boot.web.server.MimeMappings;
import org.springframework.boot.web.server.WebServer; import org.springframework.boot.web.server.WebServer;
@ -117,7 +119,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto
private List<TomcatConnectorCustomizer> tomcatConnectorCustomizers = new ArrayList<>(); private List<TomcatConnectorCustomizer> tomcatConnectorCustomizers = new ArrayList<>();
private List<TomcatProtocolHandlerCustomizer> tomcatProtocolHandlerCustomizers = new ArrayList<>(); private List<TomcatProtocolHandlerCustomizer<?>> tomcatProtocolHandlerCustomizers = new ArrayList<>();
private List<Connector> additionalTomcatConnectors = new ArrayList<>(); private List<Connector> additionalTomcatConnectors = new ArrayList<>();
@ -305,10 +307,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto
if (connector.getProtocolHandler() instanceof AbstractProtocol) { if (connector.getProtocolHandler() instanceof AbstractProtocol) {
customizeProtocol((AbstractProtocol<?>) connector.getProtocolHandler()); customizeProtocol((AbstractProtocol<?>) connector.getProtocolHandler());
} }
invokeProtocolHandlerCustomizers(connector);
this.tomcatProtocolHandlerCustomizers.forEach(
(customizer) -> customizer.customize(connector.getProtocolHandler()));
if (getUriEncoding() != null) { if (getUriEncoding() != null) {
connector.setURIEncoding(getUriEncoding().name()); 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) { private void customizeSsl(Connector connector) {
new SslConnectorCustomizer(getSsl(), getSslStoreProvider()).customize(connector); new SslConnectorCustomizer(getSsl(), getSslStoreProvider()).customize(connector);
if (getHttp2() != null && getHttp2().isEnabled()) { if (getHttp2() != null && getHttp2().isEnabled()) {
@ -631,7 +639,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto
* @param tomcatProtocolHandlerCustomizer the customizers to set * @param tomcatProtocolHandlerCustomizer the customizers to set
*/ */
public void setTomcatProtocolHandlerCustomizers( public void setTomcatProtocolHandlerCustomizers(
Collection<? extends TomcatProtocolHandlerCustomizer> tomcatProtocolHandlerCustomizer) { Collection<? extends TomcatProtocolHandlerCustomizer<?>> tomcatProtocolHandlerCustomizer) {
Assert.notNull(tomcatProtocolHandlerCustomizer, Assert.notNull(tomcatProtocolHandlerCustomizer,
"TomcatProtocolHandlerCustomizers must not be null"); "TomcatProtocolHandlerCustomizers must not be null");
this.tomcatProtocolHandlerCustomizers = new ArrayList<>( this.tomcatProtocolHandlerCustomizers = new ArrayList<>(
@ -645,7 +653,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto
*/ */
@Override @Override
public void addProtocolHandlerCustomizers( public void addProtocolHandlerCustomizers(
TomcatProtocolHandlerCustomizer... tomcatProtocolHandlerCustomizers) { TomcatProtocolHandlerCustomizer<?>... tomcatProtocolHandlerCustomizers) {
Assert.notNull(tomcatProtocolHandlerCustomizers, Assert.notNull(tomcatProtocolHandlerCustomizers,
"TomcatProtocolHandlerCustomizers must not be null"); "TomcatProtocolHandlerCustomizers must not be null");
this.tomcatProtocolHandlerCustomizers this.tomcatProtocolHandlerCustomizers
@ -657,7 +665,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto
* will be applied to the Tomcat {@link Connector}. * will be applied to the Tomcat {@link Connector}.
* @return the customizers that will be applied * @return the customizers that will be applied
*/ */
public Collection<TomcatProtocolHandlerCustomizer> getTomcatProtocolHandlerCustomizers() { public Collection<TomcatProtocolHandlerCustomizer<?>> getTomcatProtocolHandlerCustomizers() {
return this.tomcatProtocolHandlerCustomizers; return this.tomcatProtocolHandlerCustomizers;
} }

Loading…
Cancel
Save