Merge pull request #15492 from Raheela Aslam

* gh-15492:
  Polish "Apply context and connector customizer beans to Tomcat factories"
  Apply context and connector customizer beans to Tomcat factories
pull/15498/head
Andy Wilkinson 6 years ago
commit 361c2dbcc4

@ -16,13 +16,18 @@
package org.springframework.boot.autoconfigure.web.reactive;
import java.util.stream.Collectors;
import io.undertow.Undertow;
import reactor.netty.http.server.HttpServer;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.embedded.jetty.JettyReactiveWebServerFactory;
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFactory;
import org.springframework.boot.web.embedded.undertow.UndertowReactiveWebServerFactory;
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
@ -38,6 +43,7 @@ import org.springframework.http.client.reactive.ReactorResourceFactory;
* their order of execution.
*
* @author Brian Clozel
* @author Raheela Aslam
*/
abstract class ReactiveWebServerFactoryConfiguration {
@ -68,8 +74,15 @@ abstract class ReactiveWebServerFactoryConfiguration {
static class EmbeddedTomcat {
@Bean
public TomcatReactiveWebServerFactory tomcatReactiveWebServerFactory() {
return new TomcatReactiveWebServerFactory();
public TomcatReactiveWebServerFactory tomcatReactiveWebServerFactory(
ObjectProvider<TomcatConnectorCustomizer> connectorCustomizers,
ObjectProvider<TomcatContextCustomizer> contextCustomizers) {
TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory();
factory.getTomcatConnectorCustomizers().addAll(
connectorCustomizers.orderedStream().collect(Collectors.toList()));
factory.getTomcatContextCustomizers().addAll(
contextCustomizers.orderedStream().collect(Collectors.toList()));
return factory;
}
}

@ -16,6 +16,8 @@
package org.springframework.boot.autoconfigure.web.servlet;
import java.util.stream.Collectors;
import javax.servlet.Servlet;
import io.undertow.Undertow;
@ -26,10 +28,13 @@ import org.eclipse.jetty.util.Loader;
import org.eclipse.jetty.webapp.WebAppContext;
import org.xnio.SslClientAuthMode;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
@ -47,6 +52,7 @@ import org.springframework.context.annotation.Configuration;
* @author Ivan Sopov
* @author Brian Clozel
* @author Stephane Nicoll
* @author Raheela Asalm
*/
@Configuration
class ServletWebServerFactoryConfiguration {
@ -57,8 +63,15 @@ class ServletWebServerFactoryConfiguration {
public static class EmbeddedTomcat {
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory() {
return new TomcatServletWebServerFactory();
public TomcatServletWebServerFactory tomcatServletWebServerFactory(
ObjectProvider<TomcatConnectorCustomizer> connectorCustomizers,
ObjectProvider<TomcatContextCustomizer> contextCustomizers) {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.getTomcatConnectorCustomizers().addAll(
connectorCustomizers.orderedStream().collect(Collectors.toList()));
factory.getTomcatContextCustomizers().addAll(
contextCustomizers.orderedStream().collect(Collectors.toList()));
return factory;
}
}

@ -21,7 +21,10 @@ import org.mockito.Mockito;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFactory;
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext;
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
import org.springframework.boot.web.reactive.server.ConfigurableReactiveWebServerFactory;
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
@ -37,6 +40,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link ReactiveWebServerFactoryAutoConfiguration}.
*
* @author Brian Clozel
* @author Raheela Aslam
*/
public class ReactiveWebServerFactoryAutoConfigurationTests {
@ -97,6 +101,36 @@ public class ReactiveWebServerFactoryAutoConfigurationTests {
.isInstanceOf(TomcatReactiveWebServerFactory.class));
}
@Test
public void tomcatConnectorCustomizerBeanIsAddedToFactory() {
ReactiveWebApplicationContextRunner runner = new ReactiveWebApplicationContextRunner(
AnnotationConfigReactiveWebApplicationContext::new)
.withConfiguration(AutoConfigurations
.of(ReactiveWebServerFactoryAutoConfiguration.class))
.withUserConfiguration(
TomcatConnectorCustomizerConfiguration.class);
runner.run((context) -> {
TomcatReactiveWebServerFactory factory = context
.getBean(TomcatReactiveWebServerFactory.class);
assertThat(factory.getTomcatConnectorCustomizers()).hasSize(1);
});
}
@Test
public void tomcatContextCustomizerBeanIsAddedToFactory() {
ReactiveWebApplicationContextRunner runner = new ReactiveWebApplicationContextRunner(
AnnotationConfigReactiveWebApplicationContext::new)
.withConfiguration(AutoConfigurations
.of(ReactiveWebServerFactoryAutoConfiguration.class))
.withUserConfiguration(
TomcatContextCustomizerConfiguration.class);
runner.run((context) -> {
TomcatReactiveWebServerFactory factory = context
.getBean(TomcatReactiveWebServerFactory.class);
assertThat(factory.getTomcatContextCustomizers()).hasSize(1);
});
}
@Configuration
protected static class HttpHandlerConfiguration {
@ -137,4 +171,26 @@ public class ReactiveWebServerFactoryAutoConfigurationTests {
}
@Configuration
static class TomcatConnectorCustomizerConfiguration {
@Bean
public TomcatConnectorCustomizer connectorCustomizer() {
return (connector) -> {
};
}
}
@Configuration
static class TomcatContextCustomizerConfiguration {
@Bean
public TomcatContextCustomizer contextCustomizer() {
return (context) -> {
};
}
}
}

@ -30,6 +30,9 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
@ -51,6 +54,7 @@ import static org.mockito.Mockito.verify;
* @author Dave Syer
* @author Phillip Webb
* @author Stephane Nicoll
* @author Raheela Aslam
*/
public class ServletWebServerFactoryAutoConfigurationTests {
@ -138,6 +142,34 @@ public class ServletWebServerFactoryAutoConfigurationTests {
});
}
@Test
public void tomcatConnectorCustomizerBeanIsAddedToFactory() {
WebApplicationContextRunner runner = new WebApplicationContextRunner(
AnnotationConfigServletWebServerApplicationContext::new)
.withConfiguration(AutoConfigurations
.of(ServletWebServerFactoryAutoConfiguration.class))
.withUserConfiguration(
TomcatConnectorCustomizerConfiguration.class);
runner.run((context) -> {
TomcatServletWebServerFactory factory = context
.getBean(TomcatServletWebServerFactory.class);
assertThat(factory.getTomcatConnectorCustomizers()).hasSize(1);
});
}
@Test
public void tomcatContextCustomizerBeanIsAddedToFactory() {
WebApplicationContextRunner runner = new WebApplicationContextRunner(
AnnotationConfigServletWebServerApplicationContext::new)
.withConfiguration(AutoConfigurations
.of(ServletWebServerFactoryAutoConfiguration.class));
runner.run((context) -> {
TomcatServletWebServerFactory factory = context
.getBean(TomcatServletWebServerFactory.class);
assertThat(factory.getTomcatContextCustomizers()).hasSize(1);
});
}
private ContextConsumer<AssertableWebApplicationContext> verifyContext() {
return this::verifyContext;
}
@ -253,4 +285,26 @@ public class ServletWebServerFactoryAutoConfigurationTests {
}
@Configuration
static class TomcatConnectorCustomizerConfiguration {
@Bean
public TomcatConnectorCustomizer connectorCustomizer() {
return (connector) -> {
};
}
}
@Configuration
static class TomcatContextCustomizerConfiguration {
@Bean
public TomcatContextCustomizer contextCustomizer() {
return (context) -> {
};
}
}
}

Loading…
Cancel
Save