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
This commit is contained in:
parent
649f7a8011
commit
89f8af4e6e
|
|
@ -164,4 +164,25 @@ public abstract class AbstractEmbeddedServletContainerFactory
|
|||
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,22 +319,20 @@ public class JettyEmbeddedServletContainerFactory
|
|||
|
||||
private void configureDocumentRoot(WebAppContext handler) {
|
||||
File root = getValidDocumentRoot();
|
||||
if (root != null) {
|
||||
try {
|
||||
if (!root.isDirectory()) {
|
||||
Resource resource = JarResource
|
||||
.newJarResource(Resource.newResource(root));
|
||||
handler.setBaseResource(resource);
|
||||
}
|
||||
else {
|
||||
handler.setBaseResource(
|
||||
Resource.newResource(root.getCanonicalFile()));
|
||||
}
|
||||
root = (root != null ? root : createTempDir("jetty-docbase"));
|
||||
try {
|
||||
if (!root.isDirectory()) {
|
||||
Resource resource = JarResource
|
||||
.newJarResource(Resource.newResource(root));
|
||||
handler.setBaseResource(resource);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
else {
|
||||
handler.setBaseResource(Resource.newResource(root.getCanonicalFile()));
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -457,27 +457,6 @@ public class TomcatEmbeddedServletContainerFactory
|
|||
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.
|
||||
* @param baseDirectory the tomcat base directory
|
||||
|
|
|
|||
|
|
@ -428,10 +428,11 @@ public class UndertowEmbeddedServletContainerFactory
|
|||
|
||||
private ResourceManager getDocumentRootResourceManager() {
|
||||
File root = getValidDocumentRoot();
|
||||
if (root != null && root.isDirectory()) {
|
||||
root = (root != null ? root : createTempDir("undertow-docbase"));
|
||||
if (root.isDirectory()) {
|
||||
return new FileResourceManager(root, 0);
|
||||
}
|
||||
if (root != null && root.isFile()) {
|
||||
if (root.isFile()) {
|
||||
return new JarResourcemanager(root);
|
||||
}
|
||||
return ResourceManager.EMPTY_RESOURCE_MANAGER;
|
||||
|
|
|
|||
|
|
@ -23,8 +23,10 @@ import java.io.FilenameFilter;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
import java.security.KeyStore;
|
||||
import java.util.Arrays;
|
||||
|
|
@ -36,6 +38,7 @@ import java.util.Map.Entry;
|
|||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
import javax.net.ssl.SSLException;
|
||||
|
|
@ -84,6 +87,7 @@ import static org.hamcrest.Matchers.hasEntry;
|
|||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.lessThan;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
|
@ -673,6 +677,27 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
|
|||
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,
|
||||
String[] excludedUserAgents) throws Exception {
|
||||
String testContent = setUpFactoryForCompression(contentSize, mimeTypes,
|
||||
|
|
|
|||
Loading…
Reference in New Issue