From ff2d423fcb056b1e2fe9295563db926ca03bf085 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 16 Jan 2015 13:30:03 +0000 Subject: [PATCH] Correct the root context path used with Undertow Undertow, like Tomcat, uses "" for the context path of the root context. Previously, the Undertow deployment was being configured with "/" for the root context. This was leading to a silent failure in AsyncContextImpl.dispatch when it failed to look up the deployment manager for the current request. This commit updates UndertowEmbeddedServletContainerFactory to use the correct context path (an empty String) for the root context. Fixes gh-2365 --- .../sample/undertow/web/SampleController.java | 24 +++++++++++++++---- .../SampleUndertowApplicationTests.java | 16 ++++++++++--- ...dertowEmbeddedServletContainerFactory.java | 4 +--- ...wEmbeddedServletContainerFactoryTests.java | 17 +++++++++++++ 4 files changed, 50 insertions(+), 11 deletions(-) diff --git a/spring-boot-samples/spring-boot-sample-undertow/src/main/java/sample/undertow/web/SampleController.java b/spring-boot-samples/spring-boot-sample-undertow/src/main/java/sample/undertow/web/SampleController.java index c16bb6dabdb..b7edbd6d942 100644 --- a/spring-boot-samples/spring-boot-sample-undertow/src/main/java/sample/undertow/web/SampleController.java +++ b/spring-boot-samples/spring-boot-sample-undertow/src/main/java/sample/undertow/web/SampleController.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,23 +16,37 @@ package sample.undertow.web; +import java.util.concurrent.Callable; + import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; import sample.undertow.service.HelloWorldService; -@Controller +@RestController public class SampleController { @Autowired private HelloWorldService helloWorldService; @RequestMapping("/") - @ResponseBody public String helloWorld() { return this.helloWorldService.getHelloMessage(); } + @RequestMapping("/async") + public Callable helloWorldAsync() { + return new Callable() { + + @Override + public String call() throws Exception { + return "async: " + + SampleController.this.helloWorldService.getHelloMessage(); + } + + }; + + } + } diff --git a/spring-boot-samples/spring-boot-sample-undertow/src/test/java/sample/undertow/SampleUndertowApplicationTests.java b/spring-boot-samples/spring-boot-sample-undertow/src/test/java/sample/undertow/SampleUndertowApplicationTests.java index c786bf140fa..2e1f835ceb9 100644 --- a/spring-boot-samples/spring-boot-sample-undertow/src/test/java/sample/undertow/SampleUndertowApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-undertow/src/test/java/sample/undertow/SampleUndertowApplicationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,7 @@ import static org.junit.Assert.assertEquals; * Basic integration tests for demo application. * * @author Ivan Sopov + * @author Andy Wilkinson */ @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleUndertowApplication.class) @@ -47,10 +48,19 @@ public class SampleUndertowApplicationTests { @Test public void testHome() throws Exception { + assertOkResponse("/", "Hello World"); + } + + @Test + public void testAsync() throws Exception { + assertOkResponse("/async", "async: Hello World"); + } + + private void assertOkResponse(String path, String body) { ResponseEntity entity = new TestRestTemplate().getForEntity( - "http://localhost:" + this.port, String.class); + "http://localhost:" + this.port + path, String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); - assertEquals("Hello World", entity.getBody()); + assertEquals(body, entity.getBody()); } } diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactory.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactory.java index 5ead6a00e51..20e1cc36efa 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactory.java @@ -70,7 +70,6 @@ import org.springframework.core.io.ResourceLoader; import org.springframework.util.Assert; import org.springframework.util.ResourceUtils; import org.springframework.util.SocketUtils; -import org.springframework.util.StringUtils; import org.xnio.Options; import org.xnio.SslClientAuthMode; @@ -329,8 +328,7 @@ public class UndertowEmbeddedServletContainerFactory extends registerServletContainerInitializerToDriveServletContextInitializers(deployment, initializers); deployment.setClassLoader(getServletClassLoader()); - String contextPath = getContextPath(); - deployment.setContextPath(StringUtils.hasLength(contextPath) ? contextPath : "/"); + deployment.setContextPath(getContextPath()); deployment.setDeploymentName("spring-boot"); if (isRegisterDefaultServlet()) { deployment.addServlet(Servlets.servlet("default", DefaultServlet.class)); diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactoryTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactoryTests.java index 3bcf59deb27..63b78be7df1 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactoryTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactoryTests.java @@ -20,6 +20,7 @@ import io.undertow.Undertow.Builder; import io.undertow.servlet.api.DeploymentInfo; import java.util.Arrays; +import java.util.concurrent.atomic.AtomicReference; import org.junit.Test; import org.mockito.InOrder; @@ -31,6 +32,7 @@ import org.springframework.boot.context.embedded.ServletRegistrationBean; import org.springframework.http.HttpStatus; import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.mockito.Matchers.anyObject; import static org.mockito.Mockito.inOrder; @@ -132,4 +134,19 @@ public class UndertowEmbeddedServletContainerFactoryTests extends testBasicSslWithKeyStore("classpath:test.jks"); } + @Test + public void defaultContextPath() throws Exception { + UndertowEmbeddedServletContainerFactory factory = getFactory(); + final AtomicReference contextPath = new AtomicReference(); + factory.addDeploymentInfoCustomizers(new UndertowDeploymentInfoCustomizer() { + + @Override + public void customize(DeploymentInfo deploymentInfo) { + contextPath.set(deploymentInfo.getContextPath()); + } + }); + this.container = factory.getEmbeddedServletContainer(); + assertEquals("", contextPath.get()); + } + }