Skip to content

Commit 90c4890

Browse files
Raheela1024wilkinsona
authored andcommitted
Apply context and connector customizer beans to Tomcat factories
See gh-15492
1 parent de59981 commit 90c4890

File tree

4 files changed

+140
-4
lines changed

4 files changed

+140
-4
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryConfiguration.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@
1616

1717
package org.springframework.boot.autoconfigure.web.reactive;
1818

19+
import java.util.stream.Collectors;
20+
1921
import io.undertow.Undertow;
2022
import reactor.netty.http.server.HttpServer;
2123

24+
import org.springframework.beans.factory.ObjectProvider;
2225
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2326
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2427
import org.springframework.boot.web.embedded.jetty.JettyReactiveWebServerFactory;
2528
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
29+
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
30+
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
2631
import org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFactory;
2732
import org.springframework.boot.web.embedded.undertow.UndertowReactiveWebServerFactory;
2833
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
@@ -38,6 +43,7 @@
3843
* their order of execution.
3944
*
4045
* @author Brian Clozel
46+
* @author Raheela Aslam
4147
*/
4248
abstract class ReactiveWebServerFactoryConfiguration {
4349

@@ -68,8 +74,15 @@ public NettyReactiveWebServerFactory nettyReactiveWebServerFactory(
6874
static class EmbeddedTomcat {
6975

7076
@Bean
71-
public TomcatReactiveWebServerFactory tomcatReactiveWebServerFactory() {
72-
return new TomcatReactiveWebServerFactory();
77+
public TomcatReactiveWebServerFactory tomcatReactiveWebServerFactory(
78+
ObjectProvider<TomcatConnectorCustomizer> connectorCustomizers,
79+
ObjectProvider<TomcatContextCustomizer> contextCustomizers) {
80+
TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory();
81+
factory.getTomcatConnectorCustomizers().addAll(
82+
connectorCustomizers.orderedStream().collect(Collectors.toList()));
83+
factory.getTomcatContextCustomizers().addAll(
84+
contextCustomizers.orderedStream().collect(Collectors.toList()));
85+
return factory;
7386
}
7487

7588
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryConfiguration.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.autoconfigure.web.servlet;
1818

19+
import java.util.stream.Collectors;
20+
1921
import javax.servlet.Servlet;
2022

2123
import io.undertow.Undertow;
@@ -26,10 +28,13 @@
2628
import org.eclipse.jetty.webapp.WebAppContext;
2729
import org.xnio.SslClientAuthMode;
2830

31+
import org.springframework.beans.factory.ObjectProvider;
2932
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3033
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3134
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
3235
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
36+
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
37+
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
3338
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
3439
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
3540
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
@@ -47,6 +52,7 @@
4752
* @author Ivan Sopov
4853
* @author Brian Clozel
4954
* @author Stephane Nicoll
55+
* @author Raheela Asalm
5056
*/
5157
@Configuration
5258
class ServletWebServerFactoryConfiguration {
@@ -57,8 +63,15 @@ class ServletWebServerFactoryConfiguration {
5763
public static class EmbeddedTomcat {
5864

5965
@Bean
60-
public TomcatServletWebServerFactory tomcatServletWebServerFactory() {
61-
return new TomcatServletWebServerFactory();
66+
public TomcatServletWebServerFactory tomcatServletWebServerFactory(
67+
ObjectProvider<TomcatConnectorCustomizer> connectorCustomizers,
68+
ObjectProvider<TomcatContextCustomizer> contextCustomizers) {
69+
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
70+
factory.getTomcatConnectorCustomizers().addAll(
71+
connectorCustomizers.orderedStream().collect(Collectors.toList()));
72+
factory.getTomcatContextCustomizers().addAll(
73+
contextCustomizers.orderedStream().collect(Collectors.toList()));
74+
return factory;
6275
}
6376

6477
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryAutoConfigurationTests.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121

2222
import org.springframework.boot.autoconfigure.AutoConfigurations;
2323
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
24+
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
25+
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
2426
import org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFactory;
27+
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext;
2528
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
2629
import org.springframework.boot.web.reactive.server.ConfigurableReactiveWebServerFactory;
2730
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
@@ -37,6 +40,7 @@
3740
* Tests for {@link ReactiveWebServerFactoryAutoConfiguration}.
3841
*
3942
* @author Brian Clozel
43+
* @author Raheela Aslam
4044
*/
4145
public class ReactiveWebServerFactoryAutoConfigurationTests {
4246

@@ -97,6 +101,36 @@ public void defaultWebServerIsTomcat() {
97101
.isInstanceOf(TomcatReactiveWebServerFactory.class));
98102
}
99103

104+
@Test
105+
public void tomcatConnectorCustomizerBeanIsAddedToFactory() {
106+
ReactiveWebApplicationContextRunner runner = new ReactiveWebApplicationContextRunner(
107+
AnnotationConfigReactiveWebApplicationContext::new)
108+
.withConfiguration(AutoConfigurations
109+
.of(ReactiveWebServerFactoryAutoConfiguration.class))
110+
.withUserConfiguration(
111+
TomcatConnectorCustomizerConfiguration.class);
112+
runner.run((context) -> {
113+
TomcatReactiveWebServerFactory factory = context
114+
.getBean(TomcatReactiveWebServerFactory.class);
115+
assertThat(factory.getTomcatConnectorCustomizers()).hasSize(1);
116+
});
117+
}
118+
119+
@Test
120+
public void tomcatContextCustomizerBeanIsAddedToFactory() {
121+
ReactiveWebApplicationContextRunner runner = new ReactiveWebApplicationContextRunner(
122+
AnnotationConfigReactiveWebApplicationContext::new)
123+
.withConfiguration(AutoConfigurations
124+
.of(ReactiveWebServerFactoryAutoConfiguration.class))
125+
.withUserConfiguration(
126+
TomcatContextCustomizerConfiguration.class);
127+
runner.run((context) -> {
128+
TomcatReactiveWebServerFactory factory = context
129+
.getBean(TomcatReactiveWebServerFactory.class);
130+
assertThat(factory.getTomcatContextCustomizers()).hasSize(1);
131+
});
132+
}
133+
100134
@Configuration
101135
protected static class HttpHandlerConfiguration {
102136

@@ -137,4 +171,26 @@ public MockReactiveWebServerFactory mockReactiveWebServerFactory() {
137171

138172
}
139173

174+
@Configuration
175+
static class TomcatConnectorCustomizerConfiguration {
176+
177+
@Bean
178+
public TomcatConnectorCustomizer connectorCustomizer() {
179+
return (connector) -> {
180+
};
181+
}
182+
183+
}
184+
185+
@Configuration
186+
static class TomcatContextCustomizerConfiguration {
187+
188+
@Bean
189+
public TomcatContextCustomizer connectorCustomizer() {
190+
return (context) -> {
191+
};
192+
}
193+
194+
}
195+
140196
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryAutoConfigurationTests.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
3131
import org.springframework.boot.test.context.runner.ContextConsumer;
3232
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
33+
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
34+
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
35+
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
3336
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
3437
import org.springframework.boot.web.servlet.ServletRegistrationBean;
3538
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
@@ -51,6 +54,7 @@
5154
* @author Dave Syer
5255
* @author Phillip Webb
5356
* @author Stephane Nicoll
57+
* @author Raheela Aslam
5458
*/
5559
public class ServletWebServerFactoryAutoConfigurationTests {
5660

@@ -138,6 +142,34 @@ public void initParametersAreConfiguredOnTheServletContext() {
138142
});
139143
}
140144

145+
@Test
146+
public void tomcatConnectorCustomizerBeanIsAddedToFactory() {
147+
WebApplicationContextRunner runner = new WebApplicationContextRunner(
148+
AnnotationConfigServletWebServerApplicationContext::new)
149+
.withConfiguration(AutoConfigurations
150+
.of(ServletWebServerFactoryAutoConfiguration.class))
151+
.withUserConfiguration(
152+
TomcatConnectorCustomizerConfiguration.class);
153+
runner.run((context) -> {
154+
TomcatServletWebServerFactory factory = context
155+
.getBean(TomcatServletWebServerFactory.class);
156+
assertThat(factory.getTomcatConnectorCustomizers()).hasSize(1);
157+
});
158+
}
159+
160+
@Test
161+
public void tomcatContextCustomizerBeanIsAddedToFactory() {
162+
WebApplicationContextRunner runner = new WebApplicationContextRunner(
163+
AnnotationConfigServletWebServerApplicationContext::new)
164+
.withConfiguration(AutoConfigurations
165+
.of(ServletWebServerFactoryAutoConfiguration.class));
166+
runner.run((context) -> {
167+
TomcatServletWebServerFactory factory = context
168+
.getBean(TomcatServletWebServerFactory.class);
169+
assertThat(factory.getTomcatContextCustomizers()).hasSize(1);
170+
});
171+
}
172+
141173
private ContextConsumer<AssertableWebApplicationContext> verifyContext() {
142174
return this::verifyContext;
143175
}
@@ -253,4 +285,26 @@ public void customize(ConfigurableServletWebServerFactory serverFactory) {
253285

254286
}
255287

288+
@Configuration
289+
static class TomcatConnectorCustomizerConfiguration {
290+
291+
@Bean
292+
public TomcatConnectorCustomizer connectorCustomizer() {
293+
return (connector) -> {
294+
};
295+
}
296+
297+
}
298+
299+
@Configuration
300+
static class TomcatContextCustomizerConfiguration {
301+
302+
@Bean
303+
public TomcatContextCustomizer connectorCustomizer() {
304+
return (context) -> {
305+
};
306+
}
307+
308+
}
309+
256310
}

0 commit comments

Comments
 (0)