diff --git a/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/TempDirTest.java b/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/TempDirTest.java index 93a2ddf09ed3..638538be4b70 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/TempDirTest.java +++ b/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/TempDirTest.java @@ -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.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 void testFreshTempDir(WorkDir workDir) throws Exception { // Create war on the fly Path testWebappDir = MavenTestingUtils.getProjectDirPath("src/test/webapp"); - Path warFile = workDir.getEmptyPathDir().resolve("test.war"); - - Map 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 void testSameTempDir(WorkDir workDir) throws Exception { // Create war on the fly Path testWebappDir = MavenTestingUtils.getProjectDirPath("src/test/webapp"); - Path warFile = workDir.getEmptyPathDir().resolve("test.war"); - - Map 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 void testSameTempDir(WorkDir workDir) throws Exception assertNotNull(webAppContext.getTempDirectory()); webAppContext.start(); assertThat(tempDirectory.toPath(), PathMatchers.isSame(webAppContext.getTempDirectory().toPath())); + _server.stop(); } @Test @@ -220,23 +191,11 @@ public void testTempDirDeleted(WorkDir workDir) throws Exception { // Create war on the fly Path testWebappDir = MavenTestingUtils.getProjectDirPath("src/test/webapp"); - Path warFile = workDir.getEmptyPathDir().resolve("test.war"); - - Map 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 void testTempDirDeleted(WorkDir workDir) throws Exception 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)); + } } diff --git a/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/WebInfConfigurationTest.java b/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/WebInfConfigurationTest.java index aec43d0a2b21..8998de930019 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/WebInfConfigurationTest.java +++ b/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/WebInfConfigurationTest.java @@ -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.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 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 void testShouldUnpackWarAndWebInf(WorkDir workDir) throws Exception 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 diff --git a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ContextHandler.java b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ContextHandler.java index 8e557e0d47fc..e01ad6a661b0 100644 --- a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ContextHandler.java +++ b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ContextHandler.java @@ -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 String getCanonicalNameForTmpDir() @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 boolean handle(org.eclipse.jetty.server.Request coreRequest, Response res } } } + + public Resource getNestedResourceForTempDirName() + { + return getCoreContextHandler().getSuperResourceForTempDirName(); + } } diff --git a/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/WebAppContext.java b/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/WebAppContext.java index 01a9849bc6b0..7ccf48b9e25d 100644 --- a/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/WebAppContext.java +++ b/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/WebAppContext.java @@ -1162,7 +1162,6 @@ public void setContextWhiteList(String... contextWhiteList) public void setTempDirectory(File dir) { getCoreContextHandler().setTempDirectory(dir); - setAttribute(ServletContext.TEMPDIR, getCoreContextHandler().getTempDirectory()); } @ManagedAttribute(value = "temporary directory location", readonly = true) @@ -1181,9 +1180,10 @@ protected String getCanonicalNameForTmpDir() return getCoreContextHandler().getCanonicalNameForTmpDir(); } - protected Resource getResourceForTempDirName() + @Override + public Resource getNestedResourceForTempDirName() { - Resource resource = getCoreContextHandler().getResourceForTempDirName(); + Resource resource = super.getNestedResourceForTempDirName(); if (resource == null) { diff --git a/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/TempDirTest.java b/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/TempDirTest.java index 6185b117b72f..83b1f0e4d623 100644 --- a/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/TempDirTest.java +++ b/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/TempDirTest.java @@ -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.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 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 void attributeWithValidDirectory(String type, WorkDir workDir) throws Exc 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 void serverTempDirAttributeWithValidDirectory(WorkDir workDir) throws Exc { 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 void serverTempDirAttributeWithValidDirectory(WorkDir workDir) throws Exc File tempDirectory = webAppContext.getTempDirectory(); assertTrue(tempDirectory.exists()); assertThat(tempDirectory.getParentFile().toPath(), is(tmpDir)); + assertThat(webAppContext.getAttribute(ServletContext.TEMPDIR), is(webAppContext.getTempDirectory())); } /** * ${jetty.base} directory exists and has a subdirectory called work - * so webappContent#tempDirectory is created under java.io.tmpdir */ @Test public void jettyBaseWorkExists(WorkDir workDirExt) throws Exception @@ -140,12 +143,13 @@ public void jettyBaseWorkExists(WorkDir workDirExt) throws Exception 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 void testTempDirDeleted(WorkDir workDir) throws Exception { // Create war on the fly Path testWebappDir = MavenTestingUtils.getProjectDirPath("src/test/webapp"); - Path warFile = workDir.getEmptyPathDir().resolve("test.war"); - - Map 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())); + } } diff --git a/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/WebInfConfigurationTest.java b/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/WebInfConfigurationTest.java index b4260550b1d6..81bd2b8ac2ca 100644 --- a/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/WebInfConfigurationTest.java +++ b/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/WebInfConfigurationTest.java @@ -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.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.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 void testShouldUnpackWarAndWebInf(WorkDir workDir) throws Exception 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