Commit ff2d423f authored by Andy Wilkinson's avatar Andy Wilkinson

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
parent f019fb21
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -16,23 +16,37 @@ ...@@ -16,23 +16,37 @@
package sample.undertow.web; package sample.undertow.web;
import java.util.concurrent.Callable;
import org.springframework.beans.factory.annotation.Autowired; 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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController;
import sample.undertow.service.HelloWorldService; import sample.undertow.service.HelloWorldService;
@Controller @RestController
public class SampleController { public class SampleController {
@Autowired @Autowired
private HelloWorldService helloWorldService; private HelloWorldService helloWorldService;
@RequestMapping("/") @RequestMapping("/")
@ResponseBody
public String helloWorld() { public String helloWorld() {
return this.helloWorldService.getHelloMessage(); return this.helloWorldService.getHelloMessage();
} }
@RequestMapping("/async")
public Callable<String> helloWorldAsync() {
return new Callable<String>() {
@Override
public String call() throws Exception {
return "async: "
+ SampleController.this.helloWorldService.getHelloMessage();
}
};
}
} }
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -34,6 +34,7 @@ import static org.junit.Assert.assertEquals; ...@@ -34,6 +34,7 @@ import static org.junit.Assert.assertEquals;
* Basic integration tests for demo application. * Basic integration tests for demo application.
* *
* @author Ivan Sopov * @author Ivan Sopov
* @author Andy Wilkinson
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SampleUndertowApplication.class) @SpringApplicationConfiguration(classes = SampleUndertowApplication.class)
...@@ -47,10 +48,19 @@ public class SampleUndertowApplicationTests { ...@@ -47,10 +48,19 @@ public class SampleUndertowApplicationTests {
@Test @Test
public void testHome() throws Exception { 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<String> entity = new TestRestTemplate().getForEntity( ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.port, String.class); "http://localhost:" + this.port + path, String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode()); assertEquals(HttpStatus.OK, entity.getStatusCode());
assertEquals("Hello World", entity.getBody()); assertEquals(body, entity.getBody());
} }
} }
...@@ -70,7 +70,6 @@ import org.springframework.core.io.ResourceLoader; ...@@ -70,7 +70,6 @@ import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ResourceUtils; import org.springframework.util.ResourceUtils;
import org.springframework.util.SocketUtils; import org.springframework.util.SocketUtils;
import org.springframework.util.StringUtils;
import org.xnio.Options; import org.xnio.Options;
import org.xnio.SslClientAuthMode; import org.xnio.SslClientAuthMode;
...@@ -329,8 +328,7 @@ public class UndertowEmbeddedServletContainerFactory extends ...@@ -329,8 +328,7 @@ public class UndertowEmbeddedServletContainerFactory extends
registerServletContainerInitializerToDriveServletContextInitializers(deployment, registerServletContainerInitializerToDriveServletContextInitializers(deployment,
initializers); initializers);
deployment.setClassLoader(getServletClassLoader()); deployment.setClassLoader(getServletClassLoader());
String contextPath = getContextPath(); deployment.setContextPath(getContextPath());
deployment.setContextPath(StringUtils.hasLength(contextPath) ? contextPath : "/");
deployment.setDeploymentName("spring-boot"); deployment.setDeploymentName("spring-boot");
if (isRegisterDefaultServlet()) { if (isRegisterDefaultServlet()) {
deployment.addServlet(Servlets.servlet("default", DefaultServlet.class)); deployment.addServlet(Servlets.servlet("default", DefaultServlet.class));
......
...@@ -20,6 +20,7 @@ import io.undertow.Undertow.Builder; ...@@ -20,6 +20,7 @@ import io.undertow.Undertow.Builder;
import io.undertow.servlet.api.DeploymentInfo; import io.undertow.servlet.api.DeploymentInfo;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Test; import org.junit.Test;
import org.mockito.InOrder; import org.mockito.InOrder;
...@@ -31,6 +32,7 @@ import org.springframework.boot.context.embedded.ServletRegistrationBean; ...@@ -31,6 +32,7 @@ import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyObject; import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.inOrder;
...@@ -132,4 +134,19 @@ public class UndertowEmbeddedServletContainerFactoryTests extends ...@@ -132,4 +134,19 @@ public class UndertowEmbeddedServletContainerFactoryTests extends
testBasicSslWithKeyStore("classpath:test.jks"); testBasicSslWithKeyStore("classpath:test.jks");
} }
@Test
public void defaultContextPath() throws Exception {
UndertowEmbeddedServletContainerFactory factory = getFactory();
final AtomicReference<String> contextPath = new AtomicReference<String>();
factory.addDeploymentInfoCustomizers(new UndertowDeploymentInfoCustomizer() {
@Override
public void customize(DeploymentInfo deploymentInfo) {
contextPath.set(deploymentInfo.getContextPath());
}
});
this.container = factory.getEmbeddedServletContainer();
assertEquals("", contextPath.get());
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment