From 6848403d3480f07fb41048108d9946fab542db35 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Wed, 12 Aug 2020 13:06:09 -0500 Subject: [PATCH] Issue #5133 - Reworking WebAppContext.extraClasspath + Reverting name ResourceFactory.newResource(String) to .getResource(String) + Reintroducing Resource.getResource(String) + ResourceHandler.getResource(String) cleaned up in light of Exception handling requirement + Resource.addPath(String) implementations can never return null now Signed-off-by: Joakim Erdfelt --- .../jetty/server/handler/ResourceHandler.java | 27 +++++++------------ .../jetty/util/resource/EmptyResource.java | 2 +- .../eclipse/jetty/util/resource/Resource.java | 2 +- .../util/resource/ResourceCollection.java | 6 +++-- .../jetty/util/resource/URLResource.java | 4 ++- .../jetty/webapp/WebAppClassLoader.java | 1 - 6 files changed, 18 insertions(+), 24 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ResourceHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ResourceHandler.java index 733af0e699b5..9cb9b7487b0c 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ResourceHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ResourceHandler.java @@ -152,18 +152,9 @@ public MimeTypes getMimeTypes() public Resource getResource(String path) throws IOException { if (LOG.isDebugEnabled()) - LOG.debug("{} newResource({}): baseResource:{}", _context == null ? _baseResource : _context, path, _baseResource); + LOG.debug("{} getResource({}): baseResource:{}", _context == null ? _baseResource : _context, path, _baseResource); - if (path == null) - { - throw new IOException("null path"); - } - else if (!path.startsWith("/")) - { - throw new IOException("Invalid path reference: " + path); - } - - try + if (path != null && path.startsWith("/")) { Resource r = null; @@ -172,15 +163,19 @@ else if (!path.startsWith("/")) path = URIUtil.canonicalPath(path); r = _baseResource.addPath(path); - if (r != null && r.isAlias() && (_context == null || !_context.checkAlias(path, r))) + if (r.isAlias() && (_context == null || !_context.checkAlias(path, r))) { if (LOG.isDebugEnabled()) - LOG.debug("resource={} alias={}", r, r.getAlias()); - throw new IOException("Unacceptable alias reference: " + r); + LOG.debug("Rejected alias resource={} alias={}", r, r.getAlias()); + throw new IOException("Rejected (see debug logs)"); } } else if (_context != null) + { r = _context.getResource(path); + if (r != null) + return r; + } if ((r == null || !r.exists()) && path.endsWith("/jetty-dir.css")) r = getStylesheet(); @@ -188,10 +183,6 @@ else if (_context != null) if (r != null) return r; } - catch (Exception e) - { - LOG.debug("Unable to get Resource for {}", path, e); - } throw new IOException("Unable to find Resource for " + path); } diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/EmptyResource.java b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/EmptyResource.java index 07831e37b11b..ebce06439390 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/EmptyResource.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/EmptyResource.java @@ -124,6 +124,6 @@ public String[] list() @Override public Resource addPath(String path) throws IOException, MalformedURLException { - return null; + return this; } } 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 a15f466802c4..0ee6c0f34f93 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 @@ -413,7 +413,7 @@ public abstract boolean renameTo(Resource dest) * given name. * * @param path The path segment to add, which is not encoded - * @return the Resource for the resolved path within this Resource. + * @return the Resource for the resolved path within this Resource, never null * @throws IOException if unable to resolve the path * @throws MalformedURLException if the resolution of the path fails because the input path parameter is malformed. */ 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 d21596561069..87321e19412d 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 @@ -222,7 +222,7 @@ public Resource addPath(String path) throws IOException if (path == null) { - throw new MalformedURLException(); + throw new MalformedURLException("null path"); } if (path.length() == 0 || URIUtil.SLASH.equals(path)) @@ -270,11 +270,13 @@ public Resource addPath(String path) throws IOException { return resource; } + if (resources != null) { return new ResourceCollection(resources.toArray(new Resource[0])); } - return null; + + throw new MalformedURLException("path does not result in Resource: " + path); } @Override diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/URLResource.java b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/URLResource.java index 93ab4fa2ffcd..58e65bc0f998 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/URLResource.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/URLResource.java @@ -290,7 +290,9 @@ public Resource addPath(String path) throws IOException, MalformedURLException { if (path == null) - return null; + { + throw new MalformedURLException("null path"); + } path = URIUtil.canonicalPath(path); diff --git a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppClassLoader.java b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppClassLoader.java index f9702e8b8ee6..efc5d7ce3c9e 100644 --- a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppClassLoader.java +++ b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppClassLoader.java @@ -88,7 +88,6 @@ public class WebAppClassLoader extends URLClassLoader implements ClassVisibility */ public interface Context extends ClassVisibilityChecker { - /** * Convert a URL or path to a Resource. * The default implementation