Merge remote-tracking branch 'origin/jetty-12.0.x' into jetty-12.0.x-rework-resource-factory

This commit is contained in:
Ludovic Orban 2022-08-03 17:00:24 +02:00
commit 50c19d317b
7 changed files with 5 additions and 152 deletions

View File

@ -19,7 +19,6 @@ import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.CopyOption;
import java.nio.file.DirectoryIteratorException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
@ -39,12 +38,9 @@ import java.util.Objects;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.Index;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.UrlEncoded;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* Abstract resource class.
* <p>
@ -153,19 +149,6 @@ public abstract class Resource
}
}
/**
* Return true if the Resource r is contained in the Resource containingResource, either because
* containingResource is a folder or a jar file or any form of resource capable of containing other resources.
*
* @param r the contained resource
* @param containingResource the containing resource
* @return true if the Resource is contained, false otherwise
*/
public static boolean isContainedIn(Resource r, Resource containingResource)
{
return r.isContainedIn(containingResource);
}
/**
* Return the Path corresponding to this resource.
*
@ -305,50 +288,6 @@ public abstract class Resource
return false;
}
/**
* Deletes the given resource
* Equivalent to {@link Files#deleteIfExists(Path)} with the following parameter:
* {@link #getPath()}.
*
* @return true if the resource was deleted by this method; false if the file could not be deleted because it did not exist
* or if {@link Files#deleteIfExists(Path)} throws {@link IOException}.
*/
public boolean delete()
{
try
{
return Files.deleteIfExists(getPath());
}
catch (IOException e)
{
LOG.trace("IGNORED", e);
return false;
}
}
/**
* Rename the given resource
* Equivalent to {@link Files#move(Path, Path, CopyOption...)} with the following parameter:
* {@link #getPath()}, {@code dest.getPath()} then returning the result of {@link Files#exists(Path, LinkOption...)}
* on the {@code Path} returned by {@code move()}.
*
* @param dest the destination name for the resource
* @return true if the resource was renamed, false if the resource didn't exist or was unable to be renamed.
*/
public boolean renameTo(Resource dest)
{
try
{
Path result = Files.move(getPath(), dest.getPath());
return Files.exists(result, NO_FOLLOW_LINKS);
}
catch (IOException e)
{
LOG.trace("IGNORED", e);
return false;
}
}
/**
* list of resource names contained in the given resource.
* Ordering is unspecified, so callers may wish to sort the return value to ensure deterministic behavior.
@ -457,56 +396,6 @@ public abstract class Resource
return null;
}
/**
* Get the raw (decoded if possible) Filename for this Resource.
* This is the last segment of the path.
*
* @return the raw / decoded filename for this resource
*/
private String getFileName()
{
try
{
// if a Resource supports File
Path path = getPath();
if (path != null)
{
return path.getFileName().toString();
}
}
catch (Throwable ignored)
{
}
// All others use raw getName
try
{
String rawName = getName(); // gets long name "/foo/bar/xxx"
int idx = rawName.lastIndexOf('/');
if (idx == rawName.length() - 1)
{
// hit a tail slash, aka a name for a directory "/foo/bar/"
idx = rawName.lastIndexOf('/', idx - 1);
}
String encodedFileName;
if (idx >= 0)
{
encodedFileName = rawName.substring(idx + 1);
}
else
{
encodedFileName = rawName; // entire name
}
return UrlEncoded.decodeString(encodedFileName, 0, encodedFileName.length(), UTF_8);
}
catch (Throwable ignored)
{
}
return null;
}
/**
* Copy the Resource to the new destination file.
* <p>

View File

@ -149,12 +149,6 @@ public class ResourceCollection extends Resource
return new ResourceCollection(resources);
}
@Override
public boolean delete() throws SecurityException
{
throw new UnsupportedOperationException();
}
@Override
public boolean exists()
{
@ -288,12 +282,6 @@ public class ResourceCollection extends Resource
return result;
}
@Override
public boolean renameTo(Resource dest) throws SecurityException
{
throw new UnsupportedOperationException();
}
@Override
public void copyTo(Path destination) throws IOException
{

View File

@ -407,7 +407,7 @@ public class FileSystemResourceTest
Resource res = base.resolve("foo");
assertThat("foo.exists", res.exists(), is(true));
// delete it
assertThat("foo.delete", res.delete(), is(true));
Files.delete(res.getPath());
// is it there?
assertThat("foo.exists", res.exists(), is(false));
}
@ -423,7 +423,7 @@ public class FileSystemResourceTest
Resource res = base.resolve("foo");
assertThat("foo.exists", res.exists(), is(false));
// delete it
assertThat("foo.delete", res.delete(), is(false));
Files.deleteIfExists(res.getPath());
// is it there?
assertThat("foo.exists", res.exists(), is(false));
}
@ -1276,7 +1276,7 @@ public class FileSystemResourceTest
assertThat("Specials URL", res.getURI().toASCIIString(), containsString("a%20file%20with,spe%23ials"));
assertThat("Specials Filename", res.getPath().toString(), containsString("a file with,spe#ials"));
res.delete();
Files.delete(res.getPath());
assertThat("File should have been deleted.", res.exists(), is(false));
}

View File

@ -405,7 +405,7 @@ public class MetaData
{
for (Resource r : resources)
{
if (Resource.isContainedIn(resource, r))
if (resource.isContainedIn(r))
{
enclosingResource = r;
break;

View File

@ -51,12 +51,6 @@ public class OrderingTest
return null;
}
@Override
public boolean delete() throws SecurityException
{
return false;
}
@Override
public boolean exists()
{
@ -122,12 +116,6 @@ public class OrderingTest
{
return null;
}
@Override
public boolean renameTo(Resource dest) throws SecurityException
{
return false;
}
}
@Test

View File

@ -405,7 +405,7 @@ public class MetaData
{
for (Resource r : resources)
{
if (Resource.isContainedIn(resource, r))
if (resource.isContainedIn(r))
{
enclosingResource = r;
break;

View File

@ -51,12 +51,6 @@ public class OrderingTest
return null;
}
@Override
public boolean delete() throws SecurityException
{
return false;
}
@Override
public boolean exists()
{
@ -122,12 +116,6 @@ public class OrderingTest
{
return null;
}
@Override
public boolean renameTo(Resource dest) throws SecurityException
{
return false;
}
}
@Test