Issue #5133 - Changes from Review with jan & greg

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2020-09-21 09:42:26 -05:00
parent ccc863726b
commit f6bcbda689
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
3 changed files with 46 additions and 52 deletions

View File

@ -1894,6 +1894,13 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu
return Collections.unmodifiableMap(_localeEncodingMap);
}
/**
* Attempt to get a Resource from the Context.
*
* @param path the path within the resource to attempt to get
* @return the resource, or null if not available.
* @throws MalformedURLException if unable to form a Resource from the provided path
*/
public Resource getResource(String path) throws MalformedURLException
{
if (path == null || !path.startsWith(URIUtil.SLASH))

View File

@ -923,14 +923,14 @@ public abstract class Resource implements ResourceFactory, Closeable
* found directories within the glob reference.
* </p>
*
* @param delimitedReferences the comma {@code ,} or semicolon {@code ;} delimited
* @param resources the comma {@code ,} or semicolon {@code ;} delimited
* String of resource references.
* @param globDirs true if glob references return directories within the glob as well
* @param globDirs true to return directories in addition to files at the level of the glob
* @return the list of resources parsed from input string.
*/
public static List<Resource> fromList(String delimitedReferences, boolean globDirs) throws IOException
public static List<Resource> fromList(String resources, boolean globDirs) throws IOException
{
return fromList(delimitedReferences, globDirs, Resource::newResource);
return fromList(resources, globDirs, Resource::newResource);
}
/**
@ -942,22 +942,22 @@ public abstract class Resource implements ResourceFactory, Closeable
* found directories within the glob reference.
* </p>
*
* @param delimitedReferences the comma {@code ,} or semicolon {@code ;} delimited
* @param resources the comma {@code ,} or semicolon {@code ;} delimited
* String of resource references.
* @param globDirs true if glob references return directories within the glob as well
* @param globDirs true to return directories in addition to files at the level of the glob
* @param resourceFactory the ResourceFactory used to create new Resource references
* @return the list of resources parsed from input string.
*/
public static List<Resource> fromList(String delimitedReferences, boolean globDirs, ResourceFactory resourceFactory) throws IOException
public static List<Resource> fromList(String resources, boolean globDirs, ResourceFactory resourceFactory) throws IOException
{
if (StringUtil.isBlank(delimitedReferences))
if (StringUtil.isBlank(resources))
{
return Collections.emptyList();
}
List<Resource> resources = new ArrayList<>();
List<Resource> returnedResources = new ArrayList<>();
StringTokenizer tokenizer = new StringTokenizer(delimitedReferences, StringUtil.DEFAULT_DELIMS);
StringTokenizer tokenizer = new StringTokenizer(resources, StringUtil.DEFAULT_DELIMS);
while (tokenizer.hasMoreTokens())
{
String token = tokenizer.nextToken().trim();
@ -982,11 +982,11 @@ public abstract class Resource implements ResourceFactory, Closeable
Resource resource = dirResource.addPath(entry);
if (!resource.isDirectory())
{
resources.add(resource);
returnedResources.add(resource);
}
else if (globDirs)
{
resources.add(resource);
returnedResources.add(resource);
}
}
catch (Exception ex)
@ -1000,10 +1000,10 @@ public abstract class Resource implements ResourceFactory, Closeable
else
{
// Simple reference, add as-is
resources.add(resourceFactory.getResource(token));
returnedResources.add(resourceFactory.getResource(token));
}
}
return resources;
return returnedResources;
}
}

View File

@ -141,7 +141,7 @@ public class ResourceCollection extends Resource
*/
public ResourceCollection(String csvResources) throws IOException
{
setResourcesAsCSV(csvResources);
setResources(csvResources);
}
/**
@ -197,24 +197,25 @@ public class ResourceCollection extends Resource
* Sets the resources as string of comma-separated values.
* This method should be used when configuring jetty-maven-plugin.
*
* @param csvResources the comma-separated string containing
* @param resources the comma-separated string containing
* one or more resource strings.
* @throws IOException if unable resource declared is not valid
* @see Resource#fromList(String, boolean)
*/
public void setResourcesAsCSV(String csvResources) throws IOException
public void setResources(String resources) throws IOException
{
if (StringUtil.isBlank(csvResources))
if (StringUtil.isBlank(resources))
{
throw new IllegalArgumentException("CSV String is blank");
throw new IllegalArgumentException("String is blank");
}
List<Resource> resources = Resource.fromList(csvResources, false);
if (resources.isEmpty())
List<Resource> list = Resource.fromList(resources, false);
if (list.isEmpty())
{
throw new IllegalArgumentException("CSV String contains no entries");
throw new IllegalArgumentException("String contains no entries");
}
List<Resource> ret = new ArrayList<>();
for (Resource resource : resources)
for (Resource resource : list)
{
assertResourceValid(resource);
ret.add(resource);
@ -241,46 +242,32 @@ public class ResourceCollection extends Resource
return this;
}
ArrayList<Resource> resources = null;
// Attempt a simple (single) Resource lookup that exists
for (Resource res : _resources)
{
Resource fileResource = res.addPath(path);
if (fileResource.exists())
{
if (!fileResource.isDirectory())
{
return fileResource;
}
}
}
// Create a list of potential resource for directories of this collection
ArrayList<Resource> potentialResources = null;
for (Resource res : _resources)
{
Resource r = res.addPath(path);
if (r.exists() && r.isDirectory())
if (!r.isDirectory() && r.exists())
{
if (potentialResources == null)
{
potentialResources = new ArrayList<>();
}
potentialResources.add(r);
// Return simple (non-directory) Resource
return r;
}
if (resources == null)
{
resources = new ArrayList<>();
}
resources.add(r);
}
if (potentialResources == null || potentialResources.isEmpty())
if (resources.size() == 1)
{
throw new MalformedURLException("path does not result in Resource: " + path);
return resources.get(0);
}
if (potentialResources.size() == 1)
{
return potentialResources.get(0);
}
return new ResourceCollection(potentialResources);
return new ResourceCollection(resources);
}
@Override