Review of Resource throwables

This commit is contained in:
Joakim Erdfelt 2022-08-01 10:47:50 -05:00
parent e1ebef2ca6
commit 84f6dc7f4c
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
13 changed files with 100 additions and 185 deletions

View File

@ -189,12 +189,12 @@ public class PathResource extends Resource
* @param uri the URI to build this PathResource from. * @param uri the URI to build this PathResource from.
* @throws IOException if unable to construct the PathResource from the URI. * @throws IOException if unable to construct the PathResource from the URI.
*/ */
PathResource(URI uri) throws IOException PathResource(URI uri)
{ {
this(uri, false); this(uri, false);
} }
PathResource(URI uri, boolean bypassAllowedSchemeCheck) throws IOException PathResource(URI uri, boolean bypassAllowedSchemeCheck)
{ {
if (!uri.isAbsolute()) if (!uri.isAbsolute())
throw new IllegalArgumentException("not an absolute uri: " + uri); throw new IllegalArgumentException("not an absolute uri: " + uri);
@ -214,11 +214,6 @@ public class PathResource extends Resource
{ {
throw new IllegalStateException("No FileSystem mounted for : " + uri, e); throw new IllegalStateException("No FileSystem mounted for : " + uri, e);
} }
catch (Exception e)
{
LOG.trace("IGNORED", e);
throw new IOException("Unable to build Path from: " + uri, e);
}
} }
@Override @Override

View File

@ -272,9 +272,8 @@ public abstract class Resource implements ResourceFactory
* *
* @param resource A URL or filename. * @param resource A URL or filename.
* @return A Resource object. * @return A Resource object.
* @throws IOException Problem accessing URI
*/ */
public static Resource newResource(String resource) throws IOException public static Resource newResource(String resource)
{ {
return newResource(toURI(resource)); return newResource(toURI(resource));
} }
@ -331,9 +330,8 @@ public abstract class Resource implements ResourceFactory
* *
* @param resource Resource as string representation * @param resource Resource as string representation
* @return The new Resource * @return The new Resource
* @throws IOException Problem accessing resource.
*/ */
public static Resource newSystemResource(String resource) throws IOException public static Resource newSystemResource(String resource)
{ {
return newSystemResource(resource, null); return newSystemResource(resource, null);
} }
@ -346,9 +344,8 @@ public abstract class Resource implements ResourceFactory
* @param resource Resource as string representation * @param resource Resource as string representation
* @param mountConsumer a consumer that receives the mount in case the resource needs mounting * @param mountConsumer a consumer that receives the mount in case the resource needs mounting
* @return The new Resource * @return The new Resource
* @throws IOException Problem accessing resource.
*/ */
public static Resource newSystemResource(String resource, Consumer<Mount> mountConsumer) throws IOException public static Resource newSystemResource(String resource, Consumer<Mount> mountConsumer)
{ {
URL url = null; URL url = null;
// Try to format as a URL? // Try to format as a URL?
@ -436,9 +433,8 @@ public abstract class Resource implements ResourceFactory
* @param r the contained resource * @param r the contained resource
* @param containingResource the containing resource * @param containingResource the containing resource
* @return true if the Resource is contained, false otherwise * @return true if the Resource is contained, false otherwise
* @throws IOException Problem accessing resource
*/ */
public static boolean isContainedIn(Resource r, Resource containingResource) throws IOException public static boolean isContainedIn(Resource r, Resource containingResource)
{ {
return r.isContainedIn(containingResource); return r.isContainedIn(containingResource);
} }
@ -456,9 +452,8 @@ public abstract class Resource implements ResourceFactory
* *
* @param r the containing resource * @param r the containing resource
* @return true if this Resource is contained, false otherwise * @return true if this Resource is contained, false otherwise
* @throws IOException Problem accessing resource
*/ */
public abstract boolean isContainedIn(Resource r) throws IOException; public abstract boolean isContainedIn(Resource r);
/** /**
* Return true if the passed Resource represents the same resource as the Resource. * Return true if the passed Resource represents the same resource as the Resource.
@ -742,9 +737,8 @@ public abstract class Resource implements ResourceFactory
* @param parent True if the parent directory should be included * @param parent True if the parent directory should be included
* @param query query params * @param query query params
* @return String of HTML * @return String of HTML
* @throws IOException on failure to generate a list.
*/ */
public String getListHTML(String base, boolean parent, String query) throws IOException // TODO: move to helper class public String getListHTML(String base, boolean parent, String query) // TODO: move to helper class
{ {
// This method doesn't check aliases, so it is OK to canonicalize here. // This method doesn't check aliases, so it is OK to canonicalize here.
base = URIUtil.normalizePath(base); base = URIUtil.normalizePath(base);

View File

@ -13,7 +13,6 @@
package org.eclipse.jetty.ee10.annotations; package org.eclipse.jetty.ee10.annotations;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -170,15 +169,7 @@ public class AnnotationIntrospector
String descriptorLocation = holder.getSource().getResource(); String descriptorLocation = holder.getSource().getResource();
if (descriptorLocation == null) if (descriptorLocation == null)
return true; //no descriptor, can't be metadata-complete return true; //no descriptor, can't be metadata-complete
try return !WebDescriptor.isMetaDataComplete(_context.getMetaData().getFragmentDescriptor(Resource.newResource(descriptorLocation)));
{
return !WebDescriptor.isMetaDataComplete(_context.getMetaData().getFragmentDescriptor(Resource.newResource(descriptorLocation)));
}
catch (IOException e)
{
LOG.warn("Unable to get Resource for descriptor {}", descriptorLocation, e);
return false; //something wrong with the descriptor
}
} }
} }
} }

View File

@ -415,7 +415,7 @@ public class JettyHomeForker extends AbstractForker
try (Resource.Mount mount = Resource.mountJar(jettyHomeZip.toPath())) try (Resource.Mount mount = Resource.mountJar(jettyHomeZip.toPath()))
{ {
Resource res = mount.root(); Resource res = mount.root();
res.copyTo(baseDir.toPath()); res.copyTo(baseDir.toPath()); // TODO: Resource.copyTo() cannot copy dir to dir, only file to file
} }
//zip will unpack to target/jetty-home-<VERSION> //zip will unpack to target/jetty-home-<VERSION>
jettyHome = new File(baseDir, "jetty-home-" + version); jettyHome = new File(baseDir, "jetty-home-" + version);

View File

@ -14,7 +14,6 @@
package org.eclipse.jetty.ee10.maven.plugin; package org.eclipse.jetty.ee10.maven.plugin;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
@ -322,18 +321,11 @@ public class MavenWebAppContext extends WebAppContext
Configurations configurations = super.newConfigurations(); Configurations configurations = super.newConfigurations();
if (getJettyEnvXml() != null) if (getJettyEnvXml() != null)
{ {
try // inject configurations with config from maven plugin
for (Configuration c : configurations)
{ {
// inject configurations with config from maven plugin if (c instanceof EnvConfiguration)
for (Configuration c : configurations) ((EnvConfiguration)c).setJettyEnvResource(Resource.newResource(getJettyEnvXml()));
{
if (c instanceof EnvConfiguration)
((EnvConfiguration)c).setJettyEnvResource(Resource.newResource(getJettyEnvXml()));
}
}
catch (IOException e)
{
throw new RuntimeException(e);
} }
} }
@ -390,62 +382,51 @@ public class MavenWebAppContext extends WebAppContext
if (uri == null) if (uri == null)
return null; return null;
try // Replace /WEB-INF/classes with candidates for the classpath
if (uri.startsWith(WEB_INF_CLASSES_PREFIX))
{ {
// Replace /WEB-INF/classes with candidates for the classpath if (uri.equalsIgnoreCase(WEB_INF_CLASSES_PREFIX) || uri.equalsIgnoreCase(WEB_INF_CLASSES_PREFIX + "/"))
if (uri.startsWith(WEB_INF_CLASSES_PREFIX))
{ {
if (uri.equalsIgnoreCase(WEB_INF_CLASSES_PREFIX) || uri.equalsIgnoreCase(WEB_INF_CLASSES_PREFIX + "/")) // exact match for a WEB-INF/classes, so preferentially
// return the resource matching the web-inf classes
// rather than the test classes
if (_classes != null)
return Resource.newResource(_classes.toPath());
else if (_testClasses != null)
return Resource.newResource(_testClasses.toPath());
}
else
{
// try matching
Resource res = null;
int i = 0;
while (res == null && (i < _webInfClasses.size()))
{ {
// exact match for a WEB-INF/classes, so preferentially String newPath = StringUtil.replace(uri, WEB_INF_CLASSES_PREFIX, _webInfClasses.get(i).getPath());
// return the resource matching the web-inf classes res = Resource.newResource(newPath);
// rather than the test classes if (!res.exists())
if (_classes != null)
return Resource.newResource(_classes.toPath());
else if (_testClasses != null)
return Resource.newResource(_testClasses.toPath());
}
else
{
// try matching
Resource res = null;
int i = 0;
while (res == null && (i < _webInfClasses.size()))
{ {
String newPath = StringUtil.replace(uri, WEB_INF_CLASSES_PREFIX, _webInfClasses.get(i).getPath()); res = null;
res = Resource.newResource(newPath); i++;
if (!res.exists())
{
res = null;
i++;
}
} }
return res;
} }
return res;
} }
else if (uri.startsWith(WEB_INF_LIB_PREFIX)) }
{ else if (uri.startsWith(WEB_INF_LIB_PREFIX))
// Return the real jar file for all accesses to {
// /WEB-INF/lib/*.jar // Return the real jar file for all accesses to
String jarName = StringUtil.strip(uri, WEB_INF_LIB_PREFIX); // /WEB-INF/lib/*.jar
if (jarName.startsWith("/") || jarName.startsWith("\\")) String jarName = StringUtil.strip(uri, WEB_INF_LIB_PREFIX);
jarName = jarName.substring(1); if (jarName.startsWith("/") || jarName.startsWith("\\"))
if (jarName.length() == 0) jarName = jarName.substring(1);
return null; if (jarName.length() == 0)
File jarFile = _webInfJarMap.get(jarName);
if (jarFile != null)
return Resource.newResource(jarFile.getPath());
return null; return null;
} File jarFile = _webInfJarMap.get(jarName);
} if (jarFile != null)
catch (MalformedURLException e) return Resource.newResource(jarFile.getPath());
{
throw e; return null;
}
catch (IOException e)
{
LOG.trace("IGNORED", e);
} }
} }
return resource; return resource;

View File

@ -111,7 +111,7 @@ public class SelectiveJarResource extends Resource
} }
@Override @Override
public boolean isContainedIn(Resource r) throws IOException public boolean isContainedIn(Resource r)
{ {
return _delegate.isContainedIn(r); return _delegate.isContainedIn(r);
} }

View File

@ -13,8 +13,6 @@
package org.eclipse.jetty.ee10.webapp; package org.eclipse.jetty.ee10.webapp;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
@ -135,14 +133,7 @@ public class ClassMatcher extends AbstractSet<String>
super(name, inclusive); super(name, inclusive);
if (!getName().startsWith("file:")) if (!getName().startsWith("file:"))
throw new IllegalArgumentException(name); throw new IllegalArgumentException(name);
try _path = Resource.newResource(getName()).getPath();
{
_path = Resource.newResource(getName()).getPath();
}
catch (IOException e)
{
throw new UncheckedIOException(e);
}
} }
public Path getPath() public Path getPath()

View File

@ -15,7 +15,6 @@ package org.eclipse.jetty.ee10.webapp;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.nio.channels.ReadableByteChannel; import java.nio.channels.ReadableByteChannel;
import java.nio.file.Path; import java.nio.file.Path;
@ -95,7 +94,7 @@ public class OrderingTest
} }
@Override @Override
public boolean isContainedIn(Resource r) throws MalformedURLException public boolean isContainedIn(Resource r)
{ {
return false; return false;
} }

View File

@ -13,7 +13,6 @@
package org.eclipse.jetty.ee9.annotations; package org.eclipse.jetty.ee9.annotations;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -170,15 +169,7 @@ public class AnnotationIntrospector
String descriptorLocation = holder.getSource().getResource(); String descriptorLocation = holder.getSource().getResource();
if (descriptorLocation == null) if (descriptorLocation == null)
return true; //no descriptor, can't be metadata-complete return true; //no descriptor, can't be metadata-complete
try return !WebDescriptor.isMetaDataComplete(_context.getMetaData().getFragmentDescriptor(Resource.newResource(descriptorLocation)));
{
return !WebDescriptor.isMetaDataComplete(_context.getMetaData().getFragmentDescriptor(Resource.newResource(descriptorLocation)));
}
catch (IOException e)
{
LOG.warn("Unable to get Resource for descriptor {}", descriptorLocation, e);
return false; //something wrong with the descriptor
}
} }
} }
} }

View File

@ -322,18 +322,11 @@ public class MavenWebAppContext extends WebAppContext
Configurations configurations = super.newConfigurations(); Configurations configurations = super.newConfigurations();
if (getJettyEnvXml() != null) if (getJettyEnvXml() != null)
{ {
try // inject configurations with config from maven plugin
for (Configuration c : configurations)
{ {
// inject configurations with config from maven plugin if (c instanceof EnvConfiguration)
for (Configuration c : configurations) ((EnvConfiguration)c).setJettyEnvResource(Resource.newResource(getJettyEnvXml()));
{
if (c instanceof EnvConfiguration)
((EnvConfiguration)c).setJettyEnvResource(Resource.newResource(getJettyEnvXml()));
}
}
catch (IOException e)
{
throw new RuntimeException(e);
} }
} }
@ -390,62 +383,51 @@ public class MavenWebAppContext extends WebAppContext
if (uri == null) if (uri == null)
return null; return null;
try // Replace /WEB-INF/classes with candidates for the classpath
if (uri.startsWith(WEB_INF_CLASSES_PREFIX))
{ {
// Replace /WEB-INF/classes with candidates for the classpath if (uri.equalsIgnoreCase(WEB_INF_CLASSES_PREFIX) || uri.equalsIgnoreCase(WEB_INF_CLASSES_PREFIX + "/"))
if (uri.startsWith(WEB_INF_CLASSES_PREFIX))
{ {
if (uri.equalsIgnoreCase(WEB_INF_CLASSES_PREFIX) || uri.equalsIgnoreCase(WEB_INF_CLASSES_PREFIX + "/")) // exact match for a WEB-INF/classes, so preferentially
// return the resource matching the web-inf classes
// rather than the test classes
if (_classes != null)
return Resource.newResource(_classes.toPath());
else if (_testClasses != null)
return Resource.newResource(_testClasses.toPath());
}
else
{
// try matching
Resource res = null;
int i = 0;
while (res == null && (i < _webInfClasses.size()))
{ {
// exact match for a WEB-INF/classes, so preferentially String newPath = StringUtil.replace(uri, WEB_INF_CLASSES_PREFIX, _webInfClasses.get(i).getPath());
// return the resource matching the web-inf classes res = Resource.newResource(newPath);
// rather than the test classes if (!res.exists())
if (_classes != null)
return Resource.newResource(_classes.toPath());
else if (_testClasses != null)
return Resource.newResource(_testClasses.toPath());
}
else
{
// try matching
Resource res = null;
int i = 0;
while (res == null && (i < _webInfClasses.size()))
{ {
String newPath = StringUtil.replace(uri, WEB_INF_CLASSES_PREFIX, _webInfClasses.get(i).getPath()); res = null;
res = Resource.newResource(newPath); i++;
if (!res.exists())
{
res = null;
i++;
}
} }
return res;
} }
return res;
} }
else if (uri.startsWith(WEB_INF_LIB_PREFIX)) }
{ else if (uri.startsWith(WEB_INF_LIB_PREFIX))
// Return the real jar file for all accesses to {
// /WEB-INF/lib/*.jar // Return the real jar file for all accesses to
String jarName = StringUtil.strip(uri, WEB_INF_LIB_PREFIX); // /WEB-INF/lib/*.jar
if (jarName.startsWith("/") || jarName.startsWith("\\")) String jarName = StringUtil.strip(uri, WEB_INF_LIB_PREFIX);
jarName = jarName.substring(1); if (jarName.startsWith("/") || jarName.startsWith("\\"))
if (jarName.length() == 0) jarName = jarName.substring(1);
return null; if (jarName.length() == 0)
File jarFile = _webInfJarMap.get(jarName);
if (jarFile != null)
return Resource.newResource(jarFile.getPath());
return null; return null;
} File jarFile = _webInfJarMap.get(jarName);
} if (jarFile != null)
catch (MalformedURLException e) return Resource.newResource(jarFile.getPath());
{
throw e; return null;
}
catch (IOException e)
{
LOG.trace("IGNORED", e);
} }
} }
return resource; return resource;

View File

@ -111,7 +111,7 @@ public class SelectiveJarResource extends Resource
} }
@Override @Override
public boolean isContainedIn(Resource r) throws IOException public boolean isContainedIn(Resource r)
{ {
return _delegate.isContainedIn(r); return _delegate.isContainedIn(r);
} }

View File

@ -13,8 +13,6 @@
package org.eclipse.jetty.ee9.webapp; package org.eclipse.jetty.ee9.webapp;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
@ -135,14 +133,8 @@ public class ClassMatcher extends AbstractSet<String>
super(name, inclusive); super(name, inclusive);
if (!getName().startsWith("file:")) if (!getName().startsWith("file:"))
throw new IllegalArgumentException(name); throw new IllegalArgumentException(name);
try
{ _path = Resource.newResource(getName()).getPath();
_path = Resource.newResource(getName()).getPath();
}
catch (IOException e)
{
throw new UncheckedIOException(e);
}
} }
public Path getPath() public Path getPath()

View File

@ -15,7 +15,6 @@ package org.eclipse.jetty.ee9.webapp;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.nio.channels.ReadableByteChannel; import java.nio.channels.ReadableByteChannel;
import java.nio.file.Path; import java.nio.file.Path;
@ -95,7 +94,7 @@ public class OrderingTest
} }
@Override @Override
public boolean isContainedIn(Resource r) throws MalformedURLException public boolean isContainedIn(Resource r)
{ {
return false; return false;
} }