Issue #3840 - Adding Default FileSystem check to PathResource

+ Construction of PathResource now tests if path belongs
  to the Default FileSystem or not.  This important info
  for later actions against the PathResource that would
  need to know the File object for the Path object.
  Non-Default FileSystem == null
  Default FileSystem == Path.toFile()

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2019-07-30 13:58:09 -05:00
parent 5129f2c9ff
commit fd9ec22876
2 changed files with 40 additions and 0 deletions

View File

@ -28,6 +28,7 @@ import java.net.URL;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.DirectoryIteratorException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.LinkOption;
@ -56,6 +57,7 @@ public class PathResource extends Resource
private final Path path;
private final Path alias;
private final URI uri;
private final boolean belongsToDefaultFileSystem;
private final Path checkAliasPath()
{
@ -194,6 +196,7 @@ public class PathResource extends Resource
assertValidPath(path);
this.uri = this.path.toUri();
this.alias = checkAliasPath();
this.belongsToDefaultFileSystem = this.path.getFileSystem() == FileSystems.getDefault();
}
/**
@ -214,6 +217,7 @@ public class PathResource extends Resource
childPath += "/";
this.uri = URIUtil.addPath(parent.uri, childPath);
this.alias = checkAliasPath();
this.belongsToDefaultFileSystem = this.path.getFileSystem() == FileSystems.getDefault();
}
/**
@ -254,6 +258,7 @@ public class PathResource extends Resource
this.path = path.toAbsolutePath();
this.uri = path.toUri();
this.alias = checkAliasPath();
this.belongsToDefaultFileSystem = this.path.getFileSystem() == FileSystems.getDefault();
}
/**
@ -363,6 +368,8 @@ public class PathResource extends Resource
@Override
public File getFile() throws IOException
{
if (!belongsToDefaultFileSystem)
return null;
return path.toFile();
}

View File

@ -18,6 +18,7 @@
package org.eclipse.jetty.util.resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
@ -88,4 +89,36 @@ public class PathResourceTest
}
}
}
@Test
public void testNonDefaultFileSystem_GetFile() throws URISyntaxException, IOException
{
Path exampleJar = MavenTestingUtils.getTestResourcePathFile("example.jar");
URI uri = new URI("jar", exampleJar.toUri().toASCIIString(), null);
System.err.println("URI = " + uri);
Map<String, Object> env = new HashMap<>();
env.put("multi-release", "runtime");
try (FileSystem zipfs = FileSystems.newFileSystem(uri, env))
{
Path manifestPath = zipfs.getPath("/META-INF/MANIFEST.MF");
assertThat(manifestPath, is(not(nullValue())));
PathResource resource = new PathResource(manifestPath);
File file = resource.getFile();
assertThat("File should be null for non-default FileSystem", file, is(nullValue()));
}
}
@Test
public void testDefaultFileSystem_GetFile() throws Exception
{
Path exampleJar = MavenTestingUtils.getTestResourcePathFile("example.jar");
PathResource resource = new PathResource(exampleJar);
File file = resource.getFile();
assertThat("File for default FileSystem", file, is(exampleJar.toFile()));
}
}