changes from review PR #8315

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2022-07-27 10:21:35 +10:00
parent cff4ff6c98
commit 99cb930d78
5 changed files with 49 additions and 33 deletions

View File

@ -271,8 +271,8 @@ public class WebAppProvider extends ScanningAppProvider
// Resource aliases (after getting name) to ensure baseResource is not an alias
if (resource.isAlias())
{
file = new File(resource.getAlias()).toPath().toRealPath().toFile();
resource = Resource.newResource(file);
resource = Resource.resolveAlias(resource);
file = resource.getFile();
if (!resource.exists())
throw new IllegalStateException("App resource does not exist " + resource);
}

View File

@ -861,9 +861,7 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
if (getBaseResource() != null && getBaseResource().isAlias())
{
// We may have symlink to baseResource, try to resolve symlink if possible.
File file = getBaseResource().getFile();
if (file != null)
_baseResource = Resource.newResource(file.toPath().toRealPath());
_baseResource = Resource.resolveAlias(_baseResource);
LOG.warn("BaseResource {} is aliased to {} in {}. May not be supported in future releases.",
getBaseResource(), getBaseResource().getAlias(), this);

View File

@ -77,6 +77,26 @@ public abstract class Resource implements ResourceFactory, Closeable
return __defaultUseCaches;
}
public static Resource resolveAlias(Resource resource)
{
if (!resource.isAlias())
return resource;
try
{
File file = resource.getFile();
if (file != null)
return Resource.newResource(file.toPath().toRealPath());
}
catch (IOException e)
{
if (LOG.isDebugEnabled())
LOG.debug("resolve alias failed", e);
}
return resource;
}
/**
* Construct a resource from a uri.
*

View File

@ -32,8 +32,8 @@ import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.resource.PathResource;
import org.eclipse.jetty.util.resource.Resource;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
@ -42,13 +42,13 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
public class AliasCheckerMultipleResourceBasesTest
{
private static Server _server;
private static ServerConnector _connector;
private static HttpClient _client;
private static ServletContextHandler _context;
private static Path _webRootPath;
private static Path _altDir1Symlink;
private static Path _altDir2Symlink;
private Server _server;
private ServerConnector _connector;
private HttpClient _client;
private ServletContextHandler _context;
private Path _webRootPath;
private Path _altDir1Symlink;
private Path _altDir2Symlink;
private static Path getResource(String path) throws Exception
{
@ -62,7 +62,7 @@ public class AliasCheckerMultipleResourceBasesTest
IO.delete(path.toFile());
}
private static void setAliasCheckers(ContextHandler.AliasCheck... aliasChecks)
private void setAliasCheckers(ContextHandler.AliasCheck... aliasChecks)
{
_context.clearAliasChecks();
if (aliasChecks != null)
@ -74,8 +74,8 @@ public class AliasCheckerMultipleResourceBasesTest
}
}
@BeforeAll
public static void beforeAll() throws Exception
@BeforeEach
public void before() throws Exception
{
_webRootPath = getResource("webroot");
@ -106,8 +106,8 @@ public class AliasCheckerMultipleResourceBasesTest
_client.start();
}
@AfterAll
public static void afterAll() throws Exception
@AfterEach
public void after() throws Exception
{
Files.delete(_altDir1Symlink);
Files.delete(_altDir2Symlink);
@ -119,8 +119,6 @@ public class AliasCheckerMultipleResourceBasesTest
@Test
public void test() throws Exception
{
System.err.println(_webRootPath.toAbsolutePath());
ServletHolder servletHolder;
servletHolder = _context.addServlet(DefaultServlet.class, "/defaultServlet1/*");
servletHolder.setInitParameter("resourceBase", _altDir1Symlink.toString());

View File

@ -35,8 +35,8 @@ import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.resource.PathResource;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
@ -48,11 +48,11 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
public class AliasCheckerWebRootIsSymlinkTest
{
private static Server _server;
private static ServerConnector _connector;
private static HttpClient _client;
private static ServletContextHandler _context;
private static Path _webrootSymlink;
private Server _server;
private ServerConnector _connector;
private HttpClient _client;
private ServletContextHandler _context;
private Path _webrootSymlink;
private static Path getResource(String path) throws Exception
{
@ -66,15 +66,15 @@ public class AliasCheckerWebRootIsSymlinkTest
IO.delete(path.toFile());
}
private static void setAliasChecker(ContextHandler.AliasCheck aliasChecker)
private void setAliasChecker(ContextHandler.AliasCheck aliasChecker)
{
_context.clearAliasChecks();
if (aliasChecker != null)
_context.addAliasCheck(aliasChecker);
}
@BeforeAll
public static void beforeAll() throws Exception
@BeforeEach
public void before() throws Exception
{
Path webRootPath = getResource("webroot");
@ -101,8 +101,8 @@ public class AliasCheckerWebRootIsSymlinkTest
_client.start();
}
@AfterAll
public static void afterAll() throws Exception
@AfterEach
public void after() throws Exception
{
// Try to delete all files now so that the symlinks do not confuse other tests.
Files.delete(_webrootSymlink);