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
This commit is contained in:
Andy Wilkinson 2015-01-16 13:30:03 +00:00
parent f019fb217d
commit ff2d423fcb
4 changed files with 50 additions and 11 deletions

View File

@ -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<String> helloWorldAsync() {
return new Callable<String>() {
@Override
public String call() throws Exception {
return "async: "
+ SampleController.this.helloWorldService.getHelloMessage();
}
};
}
}

View File

@ -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<String> 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());
}
}

View File

@ -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));

View File

@ -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<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());
}
}