Commit 89f8af4e authored by Andy Wilkinson's avatar Andy Wilkinson

Make the configuration of the document root consistent across containers

Previously, if getValidDocumentRoot() returned null, only the embedded
Tomcat container would use a temporary directory. This left Jetty and
Undertow unable to provide a URL for the root of the servlet context,
i.e. servletContext.getResource("/") would return null.

This commit updates the embedded containers for Jetty and Undertow to
behave in the same way as Tomcat. A test has been added to verify that
all three containers can produce a non-null URL for the root of the
servlet context.

Closes gh-2878
parent 649f7a80
...@@ -164,4 +164,25 @@ public abstract class AbstractEmbeddedServletContainerFactory ...@@ -164,4 +164,25 @@ public abstract class AbstractEmbeddedServletContainerFactory
return dir; return dir;
} }
/**
* Returns the absolute temp dir for given servlet container.
* @param prefix servlet container name
* @return The temp dir for given servlet container.
*/
protected File createTempDir(String prefix) {
try {
File tempFolder = File.createTempFile(prefix + ".", "." + getPort());
tempFolder.delete();
tempFolder.mkdir();
tempFolder.deleteOnExit();
return tempFolder;
}
catch (IOException ex) {
throw new EmbeddedServletContainerException(
"Unable to create tempdir. java.io.tmpdir is set to "
+ System.getProperty("java.io.tmpdir"),
ex);
}
}
} }
...@@ -319,7 +319,7 @@ public class JettyEmbeddedServletContainerFactory ...@@ -319,7 +319,7 @@ public class JettyEmbeddedServletContainerFactory
private void configureDocumentRoot(WebAppContext handler) { private void configureDocumentRoot(WebAppContext handler) {
File root = getValidDocumentRoot(); File root = getValidDocumentRoot();
if (root != null) { root = (root != null ? root : createTempDir("jetty-docbase"));
try { try {
if (!root.isDirectory()) { if (!root.isDirectory()) {
Resource resource = JarResource Resource resource = JarResource
...@@ -327,15 +327,13 @@ public class JettyEmbeddedServletContainerFactory ...@@ -327,15 +327,13 @@ public class JettyEmbeddedServletContainerFactory
handler.setBaseResource(resource); handler.setBaseResource(resource);
} }
else { else {
handler.setBaseResource( handler.setBaseResource(Resource.newResource(root.getCanonicalFile()));
Resource.newResource(root.getCanonicalFile()));
} }
} }
catch (Exception ex) { catch (Exception ex) {
throw new IllegalStateException(ex); throw new IllegalStateException(ex);
} }
} }
}
/** /**
* Add Jetty's {@code DefaultServlet} to the given {@link WebAppContext}. * Add Jetty's {@code DefaultServlet} to the given {@link WebAppContext}.
......
...@@ -457,27 +457,6 @@ public class TomcatEmbeddedServletContainerFactory ...@@ -457,27 +457,6 @@ public class TomcatEmbeddedServletContainerFactory
this.resourceLoader = resourceLoader; this.resourceLoader = resourceLoader;
} }
/**
* Returns the absolute temp dir for given web server.
* @param prefix webserver name
* @return The temp dir for given web server.
*/
protected File createTempDir(String prefix) {
try {
File tempFolder = File.createTempFile(prefix + ".", "." + getPort());
tempFolder.delete();
tempFolder.mkdir();
tempFolder.deleteOnExit();
return tempFolder;
}
catch (IOException ex) {
throw new EmbeddedServletContainerException(
"Unable to create Tomcat tempdir. java.io.tmpdir is set to "
+ System.getProperty("java.io.tmpdir"),
ex);
}
}
/** /**
* Set the Tomcat base directory. If not specified a temporary directory will be used. * Set the Tomcat base directory. If not specified a temporary directory will be used.
* @param baseDirectory the tomcat base directory * @param baseDirectory the tomcat base directory
......
...@@ -428,10 +428,11 @@ public class UndertowEmbeddedServletContainerFactory ...@@ -428,10 +428,11 @@ public class UndertowEmbeddedServletContainerFactory
private ResourceManager getDocumentRootResourceManager() { private ResourceManager getDocumentRootResourceManager() {
File root = getValidDocumentRoot(); File root = getValidDocumentRoot();
if (root != null && root.isDirectory()) { root = (root != null ? root : createTempDir("undertow-docbase"));
if (root.isDirectory()) {
return new FileResourceManager(root, 0); return new FileResourceManager(root, 0);
} }
if (root != null && root.isFile()) { if (root.isFile()) {
return new JarResourcemanager(root); return new JarResourcemanager(root);
} }
return ResourceManager.EMPTY_RESOURCE_MANAGER; return ResourceManager.EMPTY_RESOURCE_MANAGER;
......
...@@ -23,8 +23,10 @@ import java.io.FilenameFilter; ...@@ -23,8 +23,10 @@ import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.security.KeyStore; import java.security.KeyStore;
import java.util.Arrays; import java.util.Arrays;
...@@ -36,6 +38,7 @@ import java.util.Map.Entry; ...@@ -36,6 +38,7 @@ import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import javax.net.ssl.SSLException; import javax.net.ssl.SSLException;
...@@ -84,6 +87,7 @@ import static org.hamcrest.Matchers.hasEntry; ...@@ -84,6 +87,7 @@ import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThan; import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
...@@ -673,6 +677,27 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests { ...@@ -673,6 +677,27 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
is(equalTo(expectedMimeMappings.size()))); is(equalTo(expectedMimeMappings.size())));
} }
@Test
public void rootServletContextResource() throws Exception {
AbstractEmbeddedServletContainerFactory factory = getFactory();
AtomicReference<URL> rootResource = new AtomicReference<URL>();
this.container = factory
.getEmbeddedServletContainer(new ServletContextInitializer() {
@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
try {
rootResource.set(servletContext.getResource("/"));
}
catch (MalformedURLException ex) {
throw new ServletException(ex);
}
}
});
this.container.start();
assertThat(rootResource.get(), is(not(nullValue())));
}
private boolean doTestCompression(int contentSize, String[] mimeTypes, private boolean doTestCompression(int contentSize, String[] mimeTypes,
String[] excludedUserAgents) throws Exception { String[] excludedUserAgents) throws Exception {
String testContent = setUpFactoryForCompression(contentSize, mimeTypes, String testContent = setUpFactoryForCompression(contentSize, mimeTypes,
......
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