From 67bb91bb76beb6d1d6e5118770217f66b4074c86 Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Wed, 20 Dec 2017 15:08:38 -0600 Subject: [PATCH] webflux-form sample uses Tomcat Work around gh-4923 --- ...ity-samples-javaconfig-webflux-form.gradle | 3 +- .../sample/WebfluxFormApplicationTests.java | 15 ++++++-- .../java/sample/WebfluxFormApplication.java | 34 +++++++++++++------ 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/samples/javaconfig/webflux-form/spring-security-samples-javaconfig-webflux-form.gradle b/samples/javaconfig/webflux-form/spring-security-samples-javaconfig-webflux-form.gradle index 504380adc6..b825ac8cc2 100644 --- a/samples/javaconfig/webflux-form/spring-security-samples-javaconfig-webflux-form.gradle +++ b/samples/javaconfig/webflux-form/spring-security-samples-javaconfig-webflux-form.gradle @@ -20,9 +20,8 @@ dependencies { compile project(':spring-security-core') compile project(':spring-security-config') compile project(':spring-security-web') + compile 'org.apache.tomcat.embed:tomcat-embed-core' compile 'com.fasterxml.jackson.core:jackson-databind' - compile 'io.netty:netty-buffer' - compile 'io.projectreactor.ipc:reactor-netty' compile 'org.springframework:spring-context' compile 'org.springframework:spring-webflux' compile 'org.thymeleaf:thymeleaf-spring5' diff --git a/samples/javaconfig/webflux-form/src/integration-test/java/sample/WebfluxFormApplicationTests.java b/samples/javaconfig/webflux-form/src/integration-test/java/sample/WebfluxFormApplicationTests.java index 7e6e137345..d159c5c7fe 100644 --- a/samples/javaconfig/webflux-form/src/integration-test/java/sample/WebfluxFormApplicationTests.java +++ b/samples/javaconfig/webflux-form/src/integration-test/java/sample/WebfluxFormApplicationTests.java @@ -28,17 +28,20 @@ import org.springframework.test.context.junit4.SpringRunner; import sample.webdriver.IndexPage; import sample.webdriver.LoginPage; +import java.io.IOException; +import java.net.ServerSocket; + /** * @author Rob Winch * @since 5.0 */ @RunWith(SpringRunner.class) @ContextConfiguration(classes = WebfluxFormApplication.class) -@TestPropertySource(properties = "server.port=0") +@TestPropertySource(properties = "server.port=#{T(sample.WebfluxFormApplicationTests).availablePort()}") public class WebfluxFormApplicationTests { WebDriver driver; - @Value("#{@nettyContext.address().getPort()}") + @Value("#{@tomcat.server.port}") int port; @Before @@ -76,4 +79,12 @@ public class WebfluxFormApplicationTests { .assertAt() .assertLogout(); } + + public static final int availablePort() { + try(ServerSocket socket = new ServerSocket(0)) { + return socket.getLocalPort(); + } catch(IOException e) { + throw new RuntimeException(e); + } + } } diff --git a/samples/javaconfig/webflux-form/src/main/java/sample/WebfluxFormApplication.java b/samples/javaconfig/webflux-form/src/main/java/sample/WebfluxFormApplication.java index f942a1cf15..f363d69532 100644 --- a/samples/javaconfig/webflux-form/src/main/java/sample/WebfluxFormApplication.java +++ b/samples/javaconfig/webflux-form/src/main/java/sample/WebfluxFormApplication.java @@ -16,15 +16,20 @@ package sample; +import org.apache.catalina.Context; +import org.apache.catalina.startup.Tomcat; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.*; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; import org.springframework.http.server.reactive.HttpHandler; -import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter; +import org.springframework.http.server.reactive.ServletHttpHandlerAdapter; import org.springframework.web.reactive.config.EnableWebFlux; import org.springframework.web.server.adapter.WebHttpHandlerBuilder; -import reactor.ipc.netty.NettyContext; -import reactor.ipc.netty.http.server.HttpServer; + +import javax.servlet.Servlet; /** * @author Rob Winch @@ -38,19 +43,26 @@ public class WebfluxFormApplication { private int port = 8080; public static void main(String[] args) throws Exception { + Object lock = new Object(); try(AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( WebfluxFormApplication.class)) { - context.getBean(NettyContext.class).onClose().block(); + synchronized (lock) { + lock.wait(); + } } } - @Profile("default") - @Bean - public NettyContext nettyContext(ApplicationContext context) { + @Bean(destroyMethod = "stop", initMethod = "start") + public Tomcat tomcat(ApplicationContext context) throws Exception { HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context) .build(); - ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler); - HttpServer httpServer = HttpServer.create("localhost", port); - return httpServer.newHandler(adapter).block(); + Servlet servlet = new ServletHttpHandlerAdapter(handler); + Tomcat server = new Tomcat(); + server.setPort(this.port); + server.getServer().setPort(this.port); + Context rootContext = server.addContext("", System.getProperty("java.io.tmpdir")); + Tomcat.addServlet(rootContext, "servlet", servlet); + rootContext.addServletMapping("/", "servlet"); + return server; } }