Skip to content

Commit

Permalink
Fixes #5521 ResourceCollection NPE
Browse files Browse the repository at this point in the history
Cleanup the vestiges of non existent directories detected by resource ending in /
  • Loading branch information
gregw committed Oct 28, 2020
1 parent a1e8375 commit 6066574
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 29 deletions.
Expand Up @@ -658,8 +658,8 @@ public void multipleResourcesHandler() throws Exception

// For multiple directories, use ResourceCollection.
ResourceCollection directories = new ResourceCollection();
directories.addPath("/path/to/static/resources/");
directories.addPath("/another/path/to/static/resources/");
directories.addPath("/path/to/static/resources");
directories.addPath("/another/path/to/static/resources");

handler.setBaseResource(directories);
// end::multipleResourcesHandler[]
Expand Down
Expand Up @@ -144,7 +144,7 @@ public boolean exists()
String fileUrl = _urlString.substring(4, _urlString.length() - 2);
try
{
return newResource(fileUrl).exists();
return _directory = newResource(fileUrl).exists();
}
catch (Exception e)
{
Expand Down Expand Up @@ -236,15 +236,10 @@ else if (entry.isDirectory())
return _exists;
}

/**
* Returns true if the represented resource is a container/directory.
* If the resource is not a file, resources ending with "/" are
* considered directories.
*/
@Override
public boolean isDirectory()
{
return _urlString.endsWith("/") || exists() && _directory;
return exists() && _directory;
}

/**
Expand Down
Expand Up @@ -320,8 +320,6 @@ public static boolean isContainedIn(Resource r, Resource containingResource) thr

/**
* @return true if the represented resource is a container/directory.
* if the resource is not a file, resources ending with "/" are
* considered directories.
*/
public abstract boolean isDirectory();

Expand Down
Expand Up @@ -385,7 +385,6 @@ public URI getURI()
public boolean isDirectory()
{
assertResourcesSet();

return true;
}

Expand Down
Expand Up @@ -128,11 +128,6 @@ public boolean exists()
return _in != null;
}

/**
* Returns true if the represented resource is a container/directory.
* If the resource is not a file, resources ending with "/" are
* considered directories.
*/
@Override
public boolean isDirectory()
{
Expand Down
Expand Up @@ -215,15 +215,15 @@ public static Stream<Arguments> scenarios() throws Exception
cases.addCase(new Scenario(tdata1, "alphabet.txt", EXISTS, !DIR, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
cases.addCase(new Scenario(tdata2, "alphabet.txt", EXISTS, !DIR, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));

cases.addCase(new Scenario("jar:file:/somejar.jar!/content/", !EXISTS, DIR));
cases.addCase(new Scenario("jar:file:/somejar.jar!/", !EXISTS, DIR));
cases.addCase(new Scenario("jar:file:/somejar.jar!/content/", !EXISTS, !DIR));
cases.addCase(new Scenario("jar:file:/somejar.jar!/", !EXISTS, !DIR));

String urlRef = cases.uriRef.toASCIIString();
Scenario zdata = new Scenario("jar:" + urlRef + "TestData/test.zip!/", EXISTS, DIR);
cases.addCase(zdata);

cases.addCase(new Scenario(zdata, "Unknown", !EXISTS, !DIR));
cases.addCase(new Scenario(zdata, "/Unknown/", !EXISTS, DIR));
cases.addCase(new Scenario(zdata, "/Unknown/", !EXISTS, !DIR));

cases.addCase(new Scenario(zdata, "subdir", EXISTS, DIR));
cases.addCase(new Scenario(zdata, "/subdir/", EXISTS, DIR));
Expand Down
Expand Up @@ -766,7 +766,7 @@ protected List<Resource> findWebInfLibJars(WebAppContext context)
return null;

List<Resource> jarResources = new ArrayList<Resource>();
Resource webInfLib = webInf.addPath("/lib");
Resource webInfLib = webInf.addPath("lib");
if (webInfLib.exists() && webInfLib.isDirectory())
{
String[] files = webInfLib.list();
Expand Down Expand Up @@ -834,7 +834,7 @@ protected Resource findWebInfClassesDir(WebAppContext context)
if (webInf != null && webInf.isDirectory())
{
// Look for classes directory
Resource classes = webInf.addPath("classes/");
Resource classes = webInf.addPath("classes");
if (classes.exists())
return classes;
}
Expand Down
Expand Up @@ -31,7 +31,6 @@
import java.util.EventListener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -835,7 +834,7 @@ public Resource getWebInf() throws IOException
return null;

// Iw there a WEB-INF directory?
Resource webInf = super.getBaseResource().addPath("WEB-INF/");
Resource webInf = super.getBaseResource().addPath("WEB-INF");
if (!webInf.exists() || !webInf.isDirectory())
return null;

Expand Down
Expand Up @@ -70,12 +70,12 @@ public void configure(WebAppContext context) throws Exception
if (webInf != null && webInf.isDirectory() && context.getClassLoader() instanceof WebAppClassLoader)
{
// Look for classes directory
Resource classes = webInf.addPath("classes/");
Resource classes = webInf.addPath("classes");
if (classes.exists())
((WebAppClassLoader)context.getClassLoader()).addClassPath(classes);

// Look for jars
Resource lib = webInf.addPath("lib/");
Resource lib = webInf.addPath("lib");
if (lib.exists() || lib.isDirectory())
((WebAppClassLoader)context.getClassLoader()).addJars(lib);
}
Expand Down Expand Up @@ -413,13 +413,13 @@ public void unpack(WebAppContext context) throws IOException
// Do we need to extract WEB-INF/lib?
if (context.isCopyWebInf() && !context.isCopyWebDir())
{
Resource webInf = webApp.addPath("WEB-INF/");
Resource webInf = webApp.addPath("WEB-INF");

File extractedWebInfDir = new File(context.getTempDirectory(), "webinf");
if (extractedWebInfDir.exists())
IO.delete(extractedWebInfDir);
extractedWebInfDir.mkdir();
Resource webInfLib = webInf.addPath("lib/");
Resource webInfLib = webInf.addPath("lib");
File webInfDir = new File(extractedWebInfDir, "WEB-INF");
webInfDir.mkdir();

Expand All @@ -435,7 +435,7 @@ public void unpack(WebAppContext context) throws IOException
webInfLib.copyTo(webInfLibDir);
}

Resource webInfClasses = webInf.addPath("classes/");
Resource webInfClasses = webInf.addPath("classes");
if (webInfClasses.exists())
{
File webInfClassesDir = new File(webInfDir, "classes");
Expand Down

0 comments on commit 6066574

Please sign in to comment.