Skip to content

Commit

Permalink
Allow path in URIs passed to newFileSystem (#1470)
Browse files Browse the repository at this point in the history
* This makes it easier for users who start with a URI describing a full
path to get a FileSystem that can work with that path, since they no
longer have to needlessly remove the path from the URI.

Note that Oracle's description of newFileSystem [1] puts no restriction
on the passed URI.

[1]
https://docs.oracle.com/javase/7/docs/api/java/nio/file/FileSystems.html#newFileSystem(java.net.URI,%20java.util.Map)
  • Loading branch information
jean-philippe-martin authored and garrettjonesgoogle committed Jan 12, 2017
1 parent d1e1c85 commit 824ad77
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,15 @@ public CloudStorageFileSystem getFileSystem(URI uri) {
}

/**
* Returns Cloud Storage file system, provided a URI with no path, e.g. {@code gs://bucket}.
* Returns Cloud Storage file system, provided a URI, e.g. {@code gs://bucket}.
* The URI can include a path component (that will be ignored).
*
* @param uri bucket and current working directory, e.g. {@code gs://bucket}
* @param env map of configuration options, whose keys correspond to the method names of
* {@link CloudStorageConfiguration.Builder}. However you are not allowed to set the working
* directory, as that should be provided in the {@code uri}
* @throws IllegalArgumentException if {@code uri} specifies a user, query, fragment, or scheme is
* not {@value CloudStorageFileSystem#URI_SCHEME}
* @throws IllegalArgumentException if {@code uri} specifies a port, user, query, or fragment, or
* if scheme is not {@value CloudStorageFileSystem#URI_SCHEME}
*/
@Override
public CloudStorageFileSystem newFileSystem(URI uri, Map<String, ?> env) {
Expand All @@ -191,11 +192,10 @@ public CloudStorageFileSystem newFileSystem(URI uri, Map<String, ?> env) {
CloudStorageFileSystem.URI_SCHEME, uri);
checkArgument(
uri.getPort() == -1
&& isNullOrEmpty(uri.getPath())
&& isNullOrEmpty(uri.getQuery())
&& isNullOrEmpty(uri.getFragment())
&& isNullOrEmpty(uri.getUserInfo()),
"GCS FileSystem URIs mustn't have: port, userinfo, path, query, or fragment: %s",
"GCS FileSystem URIs mustn't have: port, userinfo, query, or fragment: %s",
uri);
CloudStorageUtil.checkBucket(uri.getHost());
initStorage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Unit tests for {@link CloudStorageFileSystemProvider}.
Expand Down Expand Up @@ -644,6 +646,12 @@ public void testProviderEquals() {
assertThat(path1.getFileSystem().provider()).isNotEqualTo(path3.getFileSystem().provider());
}

@Test
public void testNewFileSystem() throws IOException {
Map<String,String> env = new HashMap<>();
FileSystems.newFileSystem(URI.create("gs://bucket/path/to/file"), env);
}

private static CloudStorageConfiguration permitEmptyPathComponents(boolean value) {
return CloudStorageConfiguration.builder().permitEmptyPathComponents(value).build();
}
Expand Down

0 comments on commit 824ad77

Please sign in to comment.