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); 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 public Resource getResource(String path) throws MalformedURLException
{ {
if (path == null || !path.startsWith(URIUtil.SLASH)) 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. * found directories within the glob reference.
* </p> * </p>
* *
* @param delimitedReferences the comma {@code ,} or semicolon {@code ;} delimited * @param resources the comma {@code ,} or semicolon {@code ;} delimited
* String of resource references. * 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. * @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. * found directories within the glob reference.
* </p> * </p>
* *
* @param delimitedReferences the comma {@code ,} or semicolon {@code ;} delimited * @param resources the comma {@code ,} or semicolon {@code ;} delimited
* String of resource references. * 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 * @param resourceFactory the ResourceFactory used to create new Resource references
* @return the list of resources parsed from input string. * @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(); 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()) while (tokenizer.hasMoreTokens())
{ {
String token = tokenizer.nextToken().trim(); String token = tokenizer.nextToken().trim();
@ -982,11 +982,11 @@ public abstract class Resource implements ResourceFactory, Closeable
Resource resource = dirResource.addPath(entry); Resource resource = dirResource.addPath(entry);
if (!resource.isDirectory()) if (!resource.isDirectory())
{ {
resources.add(resource); returnedResources.add(resource);
} }
else if (globDirs) else if (globDirs)
{ {
resources.add(resource); returnedResources.add(resource);
} }
} }
catch (Exception ex) catch (Exception ex)
@ -1000,10 +1000,10 @@ public abstract class Resource implements ResourceFactory, Closeable
else else
{ {
// Simple reference, add as-is // 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 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. * Sets the resources as string of comma-separated values.
* This method should be used when configuring jetty-maven-plugin. * 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. * one or more resource strings.
* @throws IOException if unable resource declared is not valid * @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); List<Resource> list = Resource.fromList(resources, false);
if (resources.isEmpty()) if (list.isEmpty())
{ {
throw new IllegalArgumentException("CSV String contains no entries"); throw new IllegalArgumentException("String contains no entries");
} }
List<Resource> ret = new ArrayList<>(); List<Resource> ret = new ArrayList<>();
for (Resource resource : resources) for (Resource resource : list)
{ {
assertResourceValid(resource); assertResourceValid(resource);
ret.add(resource); ret.add(resource);
@ -241,46 +242,32 @@ public class ResourceCollection extends Resource
return this; return this;
} }
ArrayList<Resource> resources = null;
// Attempt a simple (single) Resource lookup that exists // Attempt a simple (single) Resource lookup that exists
for (Resource res : _resources) 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); Resource r = res.addPath(path);
if (r.exists() && r.isDirectory()) if (!r.isDirectory() && r.exists())
{ {
if (potentialResources == null) // Return simple (non-directory) Resource
{ return r;
potentialResources = new ArrayList<>();
}
potentialResources.add(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 new ResourceCollection(resources);
{
return potentialResources.get(0);
}
return new ResourceCollection(potentialResources);
} }
@Override @Override