magicDGS / jsr203-http

**Under development** HTTP/S FileSystem provider for Java NIO.2
BSD 3-Clause "New" or "Revised" License
2 stars 2 forks source link

Should be useable with Java NIO FileSystems API #44

Closed flappingeagle closed 6 years ago

flappingeagle commented 6 years ago

It would be good if the library could be used with the Java NIO FileSystems API. See: https://docs.oracle.com/javase/7/docs/api/java/nio/file/FileSystems.html

please try following code with your library: (after creating the usual "java.nio.file.spi.FileSystemProvider" file in META-INF)

final URI uri = new URI("http://localhost:8000/");
final Path rootPath = FileSystems.getFileSystem(uri).getPath("/test/test.txt");

it will crash with a "java.nio.file.FileSystemNotFoundException".

Could you please fix following code in your "AbstractHttpFileSystemProvider":

    @Override
    public final HttpFileSystem getFileSystem(final URI uri) {
        final HttpFileSystem fs = fileSystems.get(checkUri(uri).getAuthority());
        if (fs == null) {
            throw new FileSystemNotFoundException("URI: " + uri);
        }
        return fs;
    }

it should be like following to work:

    @Override
    public final HttpFileSystem getFileSystem(final URI uri) {
        final HttpFileSystem fs = fileSystems.get(checkUri(uri).getAuthority());
        if (fs == null) {
            try {
                return newFileSystem(uri, new HashMap<>());
            } catch (final IOException e) {
                throw new RuntimeException(e);
            }
        }
        return fs;
    }

thanx!, greetings.

flappingeagle commented 6 years ago

hmm, now that i think of it....

the Methods "FileSystems.createFileSystem(uri)" and "Paths.get" do work correctly!.... i think you can close this ticket because it seems it already works correctly.

magicDGS commented 6 years ago

@flappingeagle - thank you for your report. If I am correct, the current behavior follows the contract of FileSystems.getFileSystem(java.net.URI): returns a reference to an existing FileSystem

Thus, the code that you reported should be:

final URI uri = new URI("http://localhost:8000/");
final Path rootPath = FileSystems.newFileSystem(uri, Collections.emptyMap()).getPath("/test/test.txt");

And because it is the caller responsibility to assess that the filesystem is open, I would suggest to load the filesystem for the URI with the try block:

final URI uri = new URI("http://localhost:8000/");
final FileSystem fs;
try {
    fs = FileSystems.newFileSystem(uri, Collections.emptyMap());
} catch (final FileSystemAlreadyExistsException e) {
    fs = FileSystems.getFileSystem(uri);
}
final Path rootPath = fs.getPath("/test/test.txt);
flappingeagle commented 6 years ago

thanks, you are correct :)

magicDGS commented 6 years ago

@flappingeagle - no problem, thanks for the interest on the library. I guess that I would pre-release 0.1.0 soon (I hope!)