Issue #12124 fix jsp scratchdir location for ee9. (#12129)

* Issue #12124 fix jsp scratchdir location for ee9.

Also ensure the name of war appears in the name of the tmp dir for ee9
webapps.
This commit is contained in:
Jan Bartel 2024-08-06 09:11:16 +10:00 committed by GitHub
parent abab5949ea
commit 661546ecff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 195 additions and 83 deletions

View File

@ -14,13 +14,8 @@
package org.eclipse.jetty.ee10.webapp;
import java.io.File;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import jakarta.servlet.ServletContext;
import org.eclipse.jetty.server.Server;
@ -29,7 +24,6 @@ import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.toolchain.test.PathMatchers;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
import org.eclipse.jetty.util.IO;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ -152,25 +146,13 @@ public class TempDirTest
{
// Create war on the fly
Path testWebappDir = MavenTestingUtils.getProjectDirPath("src/test/webapp");
Path warFile = workDir.getEmptyPathDir().resolve("test.war");
Map<String, String> env = new HashMap<>();
env.put("create", "true");
URI uri = URI.create("jar:" + warFile.toUri().toASCIIString());
// Use ZipFS so that we can create paths that are just "/"
try (FileSystem zipfs = FileSystems.newFileSystem(uri, env))
{
Path root = zipfs.getPath("/");
IO.copyDir(testWebappDir, root);
}
//Test that if jetty is creating a tmp dir for the webapp, it is different on
//restart
_server = new Server();
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/");
webAppContext.setWarResource(webAppContext.getResourceFactory().newResource(warFile));
webAppContext.setWar(testWebappDir.toFile().getAbsolutePath());
_server.setHandler(webAppContext);
_server.start();
File tempDirectory = webAppContext.getTempDirectory();
@ -185,26 +167,14 @@ public class TempDirTest
{
// Create war on the fly
Path testWebappDir = MavenTestingUtils.getProjectDirPath("src/test/webapp");
Path warFile = workDir.getEmptyPathDir().resolve("test.war");
Map<String, String> env = new HashMap<>();
env.put("create", "true");
URI uri = URI.create("jar:" + warFile.toUri().toASCIIString());
// Use ZipFS so that we can create paths that are just "/"
try (FileSystem zipfs = FileSystems.newFileSystem(uri, env))
{
Path root = zipfs.getPath("/");
IO.copyDir(testWebappDir, root);
}
//Test that if we explicitly configure the temp dir, it is the same after restart
_server = new Server();
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/");
Path configuredTmpDir = workDir.getPath().resolve("tmp");
Path configuredTmpDir = workDir.getEmptyPathDir().resolve("tmp");
webAppContext.setTempDirectory(configuredTmpDir.toFile());
webAppContext.setWarResource(webAppContext.getResourceFactory().newResource(warFile));
webAppContext.setWar(testWebappDir.toFile().getAbsolutePath());
_server.setHandler(webAppContext);
_server.start();
File tempDirectory = webAppContext.getTempDirectory();
@ -213,6 +183,7 @@ public class TempDirTest
assertNotNull(webAppContext.getTempDirectory());
webAppContext.start();
assertThat(tempDirectory.toPath(), PathMatchers.isSame(webAppContext.getTempDirectory().toPath()));
_server.stop();
}
@Test
@ -220,23 +191,11 @@ public class TempDirTest
{
// Create war on the fly
Path testWebappDir = MavenTestingUtils.getProjectDirPath("src/test/webapp");
Path warFile = workDir.getEmptyPathDir().resolve("test.war");
Map<String, String> env = new HashMap<>();
env.put("create", "true");
URI uri = URI.create("jar:" + warFile.toUri().toASCIIString());
// Use ZipFS so that we can create paths that are just "/"
try (FileSystem zipfs = FileSystems.newFileSystem(uri, env))
{
Path root = zipfs.getPath("/");
IO.copyDir(testWebappDir, root);
}
_server = new Server();
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/");
webAppContext.setWarResource(webAppContext.getResourceFactory().newResource(warFile));
webAppContext.setWar(testWebappDir.toFile().getAbsolutePath());
_server.setHandler(webAppContext);
_server.start();
File tempDirectory = webAppContext.getTempDirectory();
@ -244,4 +203,24 @@ public class TempDirTest
assertThat("Temp dir exists", !Files.exists(tempDirectory.toPath()));
assertNull(webAppContext.getTempDirectory());
}
@Test
public void testExplicitTempDir(WorkDir workDir) throws Exception
{
Path testWebappDir = MavenTestingUtils.getProjectDirPath("src/test/webapp");
Path myTempDir = workDir.getEmptyPathDir().resolve("my-temp-dir");
FS.ensureDirExists(myTempDir);
//Tell jetty what the temp dir is for the webapp
_server = new Server();
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/");
webAppContext.setWar(testWebappDir.toFile().getAbsolutePath());
webAppContext.setTempDirectory(myTempDir.toFile());
_server.setHandler(webAppContext);
_server.start();
File tempDirectory = webAppContext.getTempDirectory();
assertThat(webAppContext.getAttribute(ServletContext.TEMPDIR), is(tempDirectory));
assertThat(tempDirectory.toPath(), is(myTempDir));
}
}

View File

@ -13,6 +13,7 @@
package org.eclipse.jetty.ee10.webapp;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
@ -28,6 +29,7 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import jakarta.servlet.ServletContext;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenPaths;
@ -46,7 +48,9 @@ import org.junit.jupiter.params.provider.MethodSource;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
@ -247,6 +251,30 @@ public class WebInfConfigurationTest
assertTrue(Files.exists(unpackedWebInfDir.resolve("WEB-INF").resolve("lib").resolve("alpha.jar")));
}
@Test
public void testResolveTempDirectory(WorkDir workDir) throws Exception
{
Path testPath = MavenPaths.targetTestDir("testSimple");
FS.ensureDirExists(testPath);
FS.ensureEmpty(testPath);
_server = new Server();
WebAppContext context = new WebAppContext();
context.setContextPath("/");
Path warPath = createWar(testPath, "test.war");
context.setExtractWAR(true);
context.setWar(warPath.toUri().toURL().toString());
_server.setHandler(context);
_server.start();
File tmpDir = context.getTempDirectory();
assertNotNull(tmpDir);
Path tmpPath = tmpDir.toPath();
Path lastName = tmpPath.getName(tmpPath.getNameCount() - 1);
assertThat(lastName.toString(), startsWith("jetty-test_war-_-any-"));
assertThat(context.getAttribute(ServletContext.TEMPDIR), is(tmpDir));
}
/**
* Assert that for each of the expected jar names (stripped of any path info),
* there is only 1 actual jar url in the context classloader

View File

@ -13,6 +13,7 @@
package org.eclipse.jetty.ee9.nested;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
@ -2687,10 +2688,21 @@ public class ContextHandler extends ScopedHandler implements Attributes, Supplie
@Override
public Resource getResourceForTempDirName()
{
return ContextHandler.this.getNestedResourceForTempDirName();
}
private Resource getSuperResourceForTempDirName()
{
return super.getResourceForTempDirName();
}
public void setTempDirectory(File dir)
{
super.setTempDirectory(dir);
setAttribute(ServletContext.TEMPDIR, super.getTempDirectory());
}
@Override
public void setContextPath(String contextPath)
{
@ -2852,4 +2864,9 @@ public class ContextHandler extends ScopedHandler implements Attributes, Supplie
}
}
}
public Resource getNestedResourceForTempDirName()
{
return getCoreContextHandler().getSuperResourceForTempDirName();
}
}

View File

@ -1162,7 +1162,6 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
public void setTempDirectory(File dir)
{
getCoreContextHandler().setTempDirectory(dir);
setAttribute(ServletContext.TEMPDIR, getCoreContextHandler().getTempDirectory());
}
@ManagedAttribute(value = "temporary directory location", readonly = true)
@ -1181,9 +1180,10 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
return getCoreContextHandler().getCanonicalNameForTmpDir();
}
protected Resource getResourceForTempDirName()
@Override
public Resource getNestedResourceForTempDirName()
{
Resource resource = getCoreContextHandler().getResourceForTempDirName();
Resource resource = super.getNestedResourceForTempDirName();
if (resource == null)
{

View File

@ -14,13 +14,8 @@
package org.eclipse.jetty.ee9.webapp;
import java.io.File;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import jakarta.servlet.ServletContext;
import org.eclipse.jetty.server.Server;
@ -29,8 +24,9 @@ import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.toolchain.test.PathMatchers;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.resource.FileSystemPool;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
@ -39,17 +35,24 @@ import org.junit.jupiter.params.provider.ValueSource;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ExtendWith(WorkDirExtension.class)
public class TempDirTest
{
public static void tearDown()
private Server _server;
@AfterEach
public void afterEach() throws Exception
{
assertThat(FileSystemPool.INSTANCE.mounts(), empty());
if (_server != null)
_server.stop();
}
/**
@ -86,9 +89,9 @@ public class TempDirTest
public void attributeWithNonExistentDirectory(String type, WorkDir workDir) throws Exception
{
Path jettyBase = workDir.getEmptyPathDir();
Server server = new Server();
_server = new Server();
WebAppContext webAppContext = new WebAppContext();
server.setHandler(webAppContext);
_server.setHandler(webAppContext);
Path tmpDir = jettyBase.resolve("foo_did_not_exist");
assertFalse(Files.exists(tmpDir));
@ -115,11 +118,11 @@ public class TempDirTest
{
Path jettyBase = workDir.getEmptyPathDir();
WebAppContext webAppContext = new WebAppContext();
Server server = new Server();
webAppContext.setServer(server);
_server = new Server();
webAppContext.setServer(_server);
Path tmpDir = jettyBase.resolve("temp_test");
FS.ensureDirExists(tmpDir);
server.setTempDirectory(tmpDir.toFile());
_server.setTempDirectory(tmpDir.toFile());
// Test we have correct value as the webapp temp directory.
WebInfConfiguration webInfConfiguration = new WebInfConfiguration();
@ -127,11 +130,11 @@ public class TempDirTest
File tempDirectory = webAppContext.getTempDirectory();
assertTrue(tempDirectory.exists());
assertThat(tempDirectory.getParentFile().toPath(), is(tmpDir));
assertThat(webAppContext.getAttribute(ServletContext.TEMPDIR), is(webAppContext.getTempDirectory()));
}
/**
* <code>${jetty.base}</code> directory exists and has a subdirectory called work
* so webappContent#tempDirectory is created under <code>java.io.tmpdir</code>
*/
@Test
public void jettyBaseWorkExists(WorkDir workDirExt) throws Exception
@ -140,12 +143,13 @@ public class TempDirTest
Path workDir = jettyBase.resolve("work");
FS.ensureDirExists(workDir);
WebInfConfiguration webInfConfiguration = new WebInfConfiguration();
Server server = new Server();
server.setTempDirectory(workDir.toFile());
_server = new Server();
_server.setTempDirectory(workDir.toFile());
WebAppContext webAppContext = new WebAppContext();
server.setHandler(webAppContext);
_server.setHandler(webAppContext);
webInfConfiguration.resolveTempDirectory(webAppContext);
assertThat(webAppContext.getTempDirectory().getParentFile().toPath(), PathMatchers.isSame(workDir));
assertThat(webAppContext.getAttribute(ServletContext.TEMPDIR), is(webAppContext.getTempDirectory()));
}
@Test
@ -153,29 +157,84 @@ public class TempDirTest
{
// Create war on the fly
Path testWebappDir = MavenTestingUtils.getProjectDirPath("src/test/webapp");
Path warFile = workDir.getEmptyPathDir().resolve("test.war");
Map<String, String> env = new HashMap<>();
env.put("create", "true");
URI uri = URI.create("jar:" + warFile.toUri().toASCIIString());
// Use ZipFS so that we can create paths that are just "/"
try (FileSystem zipfs = FileSystems.newFileSystem(uri, env))
{
Path root = zipfs.getPath("/");
IO.copyDir(testWebappDir, root);
}
//Let jetty create the tmp dir on the fly
Server server = new Server();
_server = new Server();
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/");
webAppContext.setWarResource(webAppContext.getResourceFactory().newResource(warFile));
server.setHandler(webAppContext);
server.start();
webAppContext.setWar(testWebappDir.toFile().getAbsolutePath());
_server.setHandler(webAppContext);
_server.start();
File tempDirectory = webAppContext.getTempDirectory();
server.stop();
assertThat(webAppContext.getAttribute(ServletContext.TEMPDIR), is(webAppContext.getTempDirectory()));
_server.stop();
assertNull(webAppContext.getTempDirectory());
assertThat("Temp dir exists", !Files.exists(tempDirectory.toPath()));
}
@Test
public void testExplicitTempDir(WorkDir workDir) throws Exception
{
Path myTempDir = workDir.getEmptyPathDir().resolve("my-temp-dir");
FS.ensureDirExists(myTempDir);
// Create war on the fly
Path testWebappDir = MavenTestingUtils.getProjectDirPath("src/test/webapp");
//Tell jetty what the temp dir is for the webapp
_server = new Server();
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/");
webAppContext.setWar(testWebappDir.toFile().getAbsolutePath());
webAppContext.setTempDirectory(myTempDir.toFile());
_server.setHandler(webAppContext);
_server.start();
File tempDirectory = webAppContext.getTempDirectory();
assertThat(webAppContext.getAttribute(ServletContext.TEMPDIR), is(tempDirectory));
assertThat(tempDirectory.toPath(), is(myTempDir));
}
@Test
public void testFreshTempDir(WorkDir workDir) throws Exception
{
// Create war on the fly
Path testWebappDir = MavenTestingUtils.getProjectDirPath("src/test/webapp");
//Test that if jetty is creating a tmp dir for the webapp, it is different on
//restart
_server = new Server();
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/");
webAppContext.setWar(testWebappDir.toFile().getAbsolutePath());
_server.setHandler(webAppContext);
_server.start();
File tempDirectory = webAppContext.getTempDirectory();
webAppContext.stop();
assertNull(webAppContext.getTempDirectory());
webAppContext.start();
assertThat(tempDirectory.toPath(), not(PathMatchers.isSame(webAppContext.getTempDirectory().toPath())));
}
@Test
public void testSameTempDir(WorkDir workDir) throws Exception
{
// Create war on the fly
Path testWebappDir = MavenTestingUtils.getProjectDirPath("src/test/webapp");
//Test that if we explicitly configure the temp dir, it is the same after restart
_server = new Server();
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/");
Path configuredTmpDir = workDir.getPath().resolve("tmp");
webAppContext.setTempDirectory(configuredTmpDir.toFile());
webAppContext.setWar(testWebappDir.toFile().getAbsolutePath());
_server.setHandler(webAppContext);
_server.start();
File tempDirectory = webAppContext.getTempDirectory();
assertThat(tempDirectory.toPath(), PathMatchers.isSame(configuredTmpDir));
webAppContext.stop();
assertNotNull(webAppContext.getTempDirectory());
webAppContext.start();
assertThat(tempDirectory.toPath(), PathMatchers.isSame(webAppContext.getTempDirectory().toPath()));
}
}

View File

@ -13,6 +13,7 @@
package org.eclipse.jetty.ee9.webapp;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
@ -28,6 +29,7 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import jakarta.servlet.ServletContext;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenPaths;
@ -45,9 +47,12 @@ import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
@ -243,6 +248,30 @@ public class WebInfConfigurationTest
assertTrue(Files.exists(unpackedWebInfDir.resolve("WEB-INF").resolve("lib").resolve("alpha.jar")));
}
@Test
public void testResolveTempDirectory(WorkDir workDir) throws Exception
{
Path testPath = MavenPaths.targetTestDir("testSimple");
FS.ensureDirExists(testPath);
FS.ensureEmpty(testPath);
_server = new Server();
WebAppContext context = new WebAppContext();
context.setContextPath("/");
Path warPath = createWar(testPath, "test.war");
context.setExtractWAR(true);
context.setWar(warPath.toUri().toURL().toString());
_server.setHandler(context);
_server.start();
File tmpDir = context.getTempDirectory();
assertNotNull(tmpDir);
Path tmpPath = tmpDir.toPath();
Path lastName = tmpPath.getName(tmpPath.getNameCount() - 1);
assertThat(lastName.toString(), startsWith("jetty-test_war-_-any-"));
assertThat(context.getAttribute(ServletContext.TEMPDIR), is(tmpDir));
}
/**
* Assert that for each of the expected jar names (stripped of any path info),
* there is only 1 actual jar url in the context classloader