Skip to content

Commit

Permalink
Issue #5133 - Changes from Review with jan & greg
Browse files Browse the repository at this point in the history
Signed-off-by: Joakim Erdfelt <[email protected]>
  • Loading branch information
joakime committed Sep 21, 2020
1 parent ccc8637 commit f6bcbda
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,13 @@ public Map<String, String> getLocaleEncodings()
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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -923,14 +923,14 @@ public static URL toURL(File file) throws MalformedURLException
* 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);
}

/**
Expand All @@ -942,22 +942,22 @@ public static List<Resource> fromList(String delimitedReferences, boolean globDi
* 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();
Expand All @@ -982,11 +982,11 @@ public static List<Resource> fromList(String delimitedReferences, boolean globDi
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)
Expand All @@ -1000,10 +1000,10 @@ else if (globDirs)
else
{
// Simple reference, add as-is
resources.add(resourceFactory.getResource(token));
returnedResources.add(resourceFactory.getResource(token));
}
}

return resources;
return returnedResources;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public ResourceCollection(String[] resources)
*/
public ResourceCollection(String csvResources) throws IOException
{
setResourcesAsCSV(csvResources);
setResources(csvResources);
}

/**
Expand Down Expand Up @@ -197,24 +197,25 @@ public void setResources(Resource[] resources)
* 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);
Expand All @@ -241,46 +242,32 @@ public Resource addPath(String path) throws IOException
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())
Resource r = res.addPath(path);
if (!r.isDirectory() && r.exists())
{
if (!fileResource.isDirectory())
{
return fileResource;
}
// Return simple (non-directory) Resource
return r;
}
}

// 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 (resources == null)
{
if (potentialResources == null)
{
potentialResources = new ArrayList<>();
}

potentialResources.add(r);
resources = new ArrayList<>();
}
}

if (potentialResources == null || potentialResources.isEmpty())
{
throw new MalformedURLException("path does not result in Resource: " + path);
resources.add(r);
}

if (potentialResources.size() == 1)
if (resources.size() == 1)
{
return potentialResources.get(0);
return resources.get(0);
}

return new ResourceCollection(potentialResources);
return new ResourceCollection(resources);
}

@Override
Expand Down

0 comments on commit f6bcbda

Please sign in to comment.