Skip to content

Commit

Permalink
Issue #12124 fix jsp scratchdir location for ee9. (#12129)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
janbartel authored Aug 5, 2024
1 parent abab594 commit 661546e
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<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();
Expand All @@ -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<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();
Expand All @@ -213,35 +183,44 @@ public void testSameTempDir(WorkDir workDir) throws Exception
assertNotNull(webAppContext.getTempDirectory());
webAppContext.start();
assertThat(tempDirectory.toPath(), PathMatchers.isSame(webAppContext.getTempDirectory().toPath()));
_server.stop();
}

@Test
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<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();
_server.stop();
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -2852,4 +2864,9 @@ public boolean handle(org.eclipse.jetty.server.Request coreRequest, Response res
}
}
}

public Resource getNestedResourceForTempDirName()
{
return getCoreContextHandler().getSuperResourceForTempDirName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
{
Expand Down
Loading

0 comments on commit 661546e

Please sign in to comment.