From f6bcbda689607e140cdd0db93d4ff724a51ecb52 Mon Sep 17 00:00:00 2001
From: Joakim Erdfelt
Date: Mon, 21 Sep 2020 09:42:26 -0500
Subject: [PATCH] Issue #5133 - Changes from Review with jan & greg
Signed-off-by: Joakim Erdfelt
---
.../jetty/server/handler/ContextHandler.java | 7 +++
.../eclipse/jetty/util/resource/Resource.java | 28 ++++-----
.../util/resource/ResourceCollection.java | 57 +++++++------------
3 files changed, 43 insertions(+), 49 deletions(-)
diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java
index c3cfd42eb274..593d5029e2c6 100644
--- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java
+++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java
@@ -1894,6 +1894,13 @@ public Map 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))
diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/Resource.java b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/Resource.java
index 0ee6c0f34f93..63ebdf673e28 100644
--- a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/Resource.java
+++ b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/Resource.java
@@ -923,14 +923,14 @@ public static URL toURL(File file) throws MalformedURLException
* found directories within the glob reference.
*
*
- * @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 fromList(String delimitedReferences, boolean globDirs) throws IOException
+ public static List fromList(String resources, boolean globDirs) throws IOException
{
- return fromList(delimitedReferences, globDirs, Resource::newResource);
+ return fromList(resources, globDirs, Resource::newResource);
}
/**
@@ -942,22 +942,22 @@ public static List fromList(String delimitedReferences, boolean globDi
* found directories within the glob reference.
*
*
- * @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 fromList(String delimitedReferences, boolean globDirs, ResourceFactory resourceFactory) throws IOException
+ public static List fromList(String resources, boolean globDirs, ResourceFactory resourceFactory) throws IOException
{
- if (StringUtil.isBlank(delimitedReferences))
+ if (StringUtil.isBlank(resources))
{
return Collections.emptyList();
}
- List resources = new ArrayList<>();
+ List 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 static List 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)
@@ -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;
}
}
diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/ResourceCollection.java b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/ResourceCollection.java
index d7da2a2f500e..e716cc19333b 100644
--- a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/ResourceCollection.java
+++ b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/ResourceCollection.java
@@ -141,7 +141,7 @@ public ResourceCollection(String[] resources)
*/
public ResourceCollection(String csvResources) throws IOException
{
- setResourcesAsCSV(csvResources);
+ setResources(csvResources);
}
/**
@@ -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 resources = Resource.fromList(csvResources, false);
- if (resources.isEmpty())
+ List list = Resource.fromList(resources, false);
+ if (list.isEmpty())
{
- throw new IllegalArgumentException("CSV String contains no entries");
+ throw new IllegalArgumentException("String contains no entries");
}
List ret = new ArrayList<>();
- for (Resource resource : resources)
+ for (Resource resource : list)
{
assertResourceValid(resource);
ret.add(resource);
@@ -241,46 +242,32 @@ public Resource addPath(String path) throws IOException
return this;
}
+ ArrayList 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 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