From 824ad7753fb24ba92035bf8a7874ea9ea8aa5d87 Mon Sep 17 00:00:00 2001 From: JP Martin Date: Thu, 12 Jan 2017 10:10:54 -0800 Subject: [PATCH] Allow path in URIs passed to newFileSystem (#1470) * 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) --- .../contrib/nio/CloudStorageFileSystemProvider.java | 10 +++++----- .../nio/CloudStorageFileSystemProviderTest.java | 8 ++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java b/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java index 727327d215fd..f65549b69fec 100644 --- a/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java +++ b/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java @@ -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 env) { @@ -191,11 +192,10 @@ public CloudStorageFileSystem newFileSystem(URI uri, Map 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(); diff --git a/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProviderTest.java b/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProviderTest.java index 86e87d0e71e1..970d60217e57 100644 --- a/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProviderTest.java +++ b/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProviderTest.java @@ -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}. @@ -644,6 +646,12 @@ public void testProviderEquals() { assertThat(path1.getFileSystem().provider()).isNotEqualTo(path3.getFileSystem().provider()); } + @Test + public void testNewFileSystem() throws IOException { + Map 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(); }