Skip to content

Commit c37d55e

Browse files
Merge branch 'master' into clarify_adc_auth
2 parents 0171f06 + b191ab3 commit c37d55e

File tree

50 files changed

+2888
-94
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2888
-94
lines changed

README.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Google Cloud Java Client
1+
Google Cloud Client Library for Java
22
==========================
33

44
Java idiomatic client for [Google Cloud Platform][cloud-platform] services.
@@ -29,6 +29,7 @@ This client supports the following Google Cloud Platform services at a [Beta](#v
2929
This client supports the following Google Cloud Platform services at an [Alpha](#versioning) quality level:
3030

3131
- [Cloud Compute](#google-cloud-compute-alpha) (Alpha)
32+
- [Cloud Data Loss Prevention](#google-cloud-data-loss-prevention-alpha) (Alpha)
3233
- [Cloud DNS](#google-cloud-dns-alpha) (Alpha)
3334
- [Stackdriver Error Reporting](#stackdriver-error-reporting-alpha) (Alpha)
3435
- [Stackdriver Monitoring](#stackdriver-monitoring-alpha) (Alpha)
@@ -424,7 +425,7 @@ snippet elsewhere. Complete source code can be found at
424425
[CreateTopicAndPublishMessages.java](./google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java).
425426
426427
```java
427-
import com.google.api.gax.core.ApiFuture;
428+
import com.google.api.core.ApiFuture;
428429
import com.google.cloud.pubsub.v1.Publisher;
429430
import com.google.cloud.pubsub.v1.TopicAdminClient;
430431
import com.google.protobuf.ByteString;
@@ -589,6 +590,25 @@ if (operation.getErrors() == null) {
589590
}
590591
```
591592
593+
Google Cloud Data Loss Prevention (Alpha)
594+
----------------
595+
596+
- [API Documentation][dlp-api]
597+
- [Official Documentation][cloud-dlp-docs]
598+
599+
#### Preview
600+
601+
Here is a code snippet showing a simple usage example of DlpServiceClient. The example assumes that either default application
602+
credentials or a valid API key are available. (See [Authentication section](#authentication) for more information)
603+
604+
```java
605+
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
606+
InspectConfig inspectConfig = InspectConfig.newBuilder().build();
607+
List<ContentItem> items = new ArrayList<>();
608+
InspectContentResponse response = dlpServiceClient.inspectContent(inspectConfig, items);
609+
}
610+
```
611+
592612
Google Cloud DNS (Alpha)
593613
----------------------
594614
- [API Documentation][dns-api]
@@ -851,7 +871,7 @@ Please note that gRPC proxy support is currently experimental.
851871
Java Versions
852872
-------------
853873
854-
Java 7 or above is required for using this client.
874+
Java 7 or above is required for using the clients in this repository.
855875
856876
Supported Platforms
857877
-------------------
@@ -988,3 +1008,6 @@ Apache 2.0 - See [LICENSE] for more information.
9881008
[cloud-spanner]: https://cloud.google.com/spanner/
9891009
[cloud-spanner-docs]: https://cloud.google.com/spanner/docs/
9901010
[cloud-spanner-api]: https://googlecloudplatform.github.io/google-cloud-java/apidocs/index.html?com/google/cloud/spanner/package-summary.html
1011+
1012+
[cloud-dlp-docs]: https://cloud.google.com/dlp/docs/
1013+
[dlp-api]: https://googlecloudplatform.github.io/google-cloud-java/apidocs/index.html?com/google/cloud/dlp/package-summary.html

google-cloud-contrib/google-cloud-nio/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
<dependency>
3939
<groupId>com.google.auto.service</groupId>
4040
<artifactId>auto-service</artifactId>
41-
<version>1.0-rc2</version>
41+
<version>1.0-rc3</version>
42+
<optional>true</optional>
4243
<scope>provided</scope> <!-- to leave out of the all-deps jar -->
4344
</dependency>
4445
<dependency>

google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageConfiguration.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public abstract class CloudStorageConfiguration {
7272
* <li>Performing I/O on paths with extra slashes, e.g. {@code a//b} will throw an error.
7373
* <li>The prefix slash on absolute paths will be removed when converting to an object name.
7474
* <li>Pseudo-directories are enabled, so any path with a trailing slash is a fake directory.
75+
* <li>Channel re-opens are disabled.
7576
* </ul>
7677
*/
7778
public static Builder builder() {
@@ -159,11 +160,27 @@ public CloudStorageConfiguration build() {
159160
maxChannelReopens);
160161
}
161162

163+
Builder(CloudStorageConfiguration toModify) {
164+
workingDirectory = toModify.workingDirectory();
165+
permitEmptyPathComponents = toModify.permitEmptyPathComponents();
166+
stripPrefixSlash = toModify.stripPrefixSlash();
167+
usePseudoDirectories = toModify.usePseudoDirectories();
168+
blockSize = toModify.blockSize();
169+
maxChannelReopens = toModify.maxChannelReopens();
170+
}
171+
162172
Builder() {}
163173
}
164174

165175
static CloudStorageConfiguration fromMap(Map<String, ?> env) {
166-
Builder builder = builder();
176+
return fromMap(builder(), env);
177+
}
178+
179+
static CloudStorageConfiguration fromMap(CloudStorageConfiguration defaultValues, Map<String, ?> env) {
180+
return fromMap(new Builder(defaultValues), env);
181+
}
182+
183+
static private CloudStorageConfiguration fromMap(Builder builder, Map<String, ?> env) {
167184
for (Map.Entry<String, ?> entry : env.entrySet()) {
168185
switch (entry.getKey()) {
169186
case "workingDirectory":

google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystem.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,27 @@ public final class CloudStorageFileSystem extends FileSystem {
6262
private final String bucket;
6363
private final CloudStorageConfiguration config;
6464

65+
// Users can change this: then this affects every filesystem object created
66+
// later, including via SPI. This is meant to be done only once, at the beginning
67+
// of some main program, in order to force all libraries to use some settings we like.
68+
// Libraries should never call this. It'll cause surprise to the writers of the main
69+
// program and they'll be unhappy. Instead, create your own filesystem object with the
70+
// right configuration and pass it along.
71+
private static CloudStorageConfiguration userSpecifiedDefault = CloudStorageConfiguration.DEFAULT;
72+
73+
// Don't call this one, call the one in CloudStorageFileSystemProvider.
74+
static void setDefaultCloudStorageConfiguration(CloudStorageConfiguration config) {
75+
if (null == config) {
76+
userSpecifiedDefault = CloudStorageConfiguration.DEFAULT;
77+
} else {
78+
userSpecifiedDefault = config;
79+
}
80+
}
81+
82+
static CloudStorageConfiguration getDefaultCloudStorageConfiguration() {
83+
return userSpecifiedDefault;
84+
}
85+
6586
/**
6687
* Returns Google Cloud Storage {@link FileSystem} object for {@code bucket}.
6788
*
@@ -79,7 +100,7 @@ public final class CloudStorageFileSystem extends FileSystem {
79100
*/
80101
@CheckReturnValue
81102
public static CloudStorageFileSystem forBucket(String bucket) {
82-
return forBucket(bucket, CloudStorageConfiguration.DEFAULT);
103+
return forBucket(bucket, userSpecifiedDefault);
83104
}
84105

85106
/**

google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,34 @@ protected Path computeNext() {
133133
* Sets options that are only used by the constructor.
134134
*/
135135
@VisibleForTesting
136-
public static void setStorageOptions(StorageOptions newStorageOptions) {
136+
public static void setStorageOptions(@Nullable StorageOptions newStorageOptions) {
137137
futureStorageOptions = newStorageOptions;
138138
}
139139

140+
/**
141+
* Changes the default configuration for every filesystem object created
142+
* from here on, including via SPI. If null then future filesystem objects
143+
* will have the factory default configuration.
144+
*
145+
* <p>If options are specified later then they override the defaults.
146+
* Methods that take a whole CloudStorageConfiguration (eg.
147+
* CloudStorageFileSystem.forBucket) will completely override the defaults.
148+
* Methods that take individual options (eg.
149+
* CloudStorageFileSystemProvier.newFileSystem) will override only these options;
150+
* the rest will be taken from the defaults specified here.
151+
*
152+
* <p>This is meant to be done only once, at the beginning of some main program,
153+
* in order to force all libraries to use some settings we like.
154+
*
155+
* <p>Libraries should never call this. If you're a library then, instead, create your own
156+
* filesystem object with the right configuration and pass it along.
157+
*
158+
* @param newDefault new default CloudStorageConfiguration
159+
*/
160+
public static void setDefaultCloudStorageConfiguration(@Nullable CloudStorageConfiguration newDefault) {
161+
CloudStorageFileSystem.setDefaultCloudStorageConfiguration(newDefault);
162+
}
163+
140164
/**
141165
* Default constructor which should only be called by Java SPI.
142166
*
@@ -208,7 +232,11 @@ && isNullOrEmpty(uri.getUserInfo()),
208232
uri);
209233
CloudStorageUtil.checkBucket(uri.getHost());
210234
initStorage();
211-
return new CloudStorageFileSystem(this, uri.getHost(), CloudStorageConfiguration.fromMap(env));
235+
return new CloudStorageFileSystem(
236+
this,
237+
uri.getHost(),
238+
CloudStorageConfiguration.fromMap(
239+
CloudStorageFileSystem.getDefaultCloudStorageConfiguration(), env));
212240
}
213241

214242
@Override

google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static CloudStorageOption.OpenCopy withBlockSize(int size) {
9292
}
9393

9494
/**
95-
* Sets the max number of times that the channel can be reopen if reading
95+
* Sets the max number of times that the channel can be reopened if reading
9696
* fails because the channel unexpectedly closes.
9797
*
9898
* <p>The default is 0.

google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageReadChannel.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.cloud.storage.BlobInfo;
2222
import com.google.cloud.storage.Storage;
2323
import com.google.cloud.storage.StorageException;
24+
import com.google.common.annotations.VisibleForTesting;
2425

2526
import javax.annotation.CheckReturnValue;
2627
import javax.annotation.concurrent.ThreadSafe;
@@ -49,7 +50,8 @@ final class CloudStorageReadChannel implements SeekableByteChannel {
4950
private final Storage gcsStorage;
5051
private final BlobId file;
5152
// max # of times we may reopen the file
52-
private final int maxChannelReopens;
53+
@VisibleForTesting
54+
final int maxChannelReopens;
5355
// how many times we re-opened the file
5456
private int reopens;
5557
private ReadChannel channel;

google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProviderTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,9 +633,7 @@ public void testNullness() throws Exception {
633633
tester.setDefault(Path.class, fs.getPath("and/one"));
634634
tester.setDefault(OpenOption.class, CREATE);
635635
tester.setDefault(CopyOption.class, COPY_ATTRIBUTES);
636-
// can't do that, setStorageOptions accepts a null argument.
637-
// TODO(jart): Figure out how to re-enable this.
638-
// tester.testAllPublicStaticMethods(CloudStorageFileSystemProvider.class);
636+
tester.testAllPublicStaticMethods(CloudStorageFileSystemProvider.class);
639637
tester.testAllPublicInstanceMethods(new CloudStorageFileSystemProvider());
640638
}
641639
}

google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import java.io.IOException;
3535
import java.net.URI;
36+
import java.nio.channels.SeekableByteChannel;
3637
import java.nio.file.FileSystem;
3738
import java.nio.file.FileSystems;
3839
import java.nio.file.Files;
@@ -69,6 +70,57 @@ public void before() {
6970
CloudStorageFileSystemProvider.setStorageOptions(LocalStorageHelper.getOptions());
7071
}
7172

73+
@Test
74+
public void checkDefaultOptions() throws IOException {
75+
// 1. We get the normal default if we don't do anything special.
76+
Path path = Paths.get(URI.create("gs://bucket/file"));
77+
CloudStorageFileSystem gcs = (CloudStorageFileSystem)path.getFileSystem();
78+
assertThat(gcs.config().maxChannelReopens()).isEqualTo(0);
79+
80+
// 2(a). Override the default, and see it reflected.
81+
CloudStorageFileSystemProvider.setDefaultCloudStorageConfiguration(
82+
CloudStorageConfiguration.builder()
83+
.maxChannelReopens(123).build());
84+
Path path2 = Paths.get(URI.create("gs://newbucket/file"));
85+
CloudStorageFileSystem gcs2 = (CloudStorageFileSystem)path2.getFileSystem();
86+
assertThat(gcs2.config().maxChannelReopens()).isEqualTo(123);
87+
88+
// 2(b) ...even reflected if we try to open a file.
89+
try (FileSystem fs = CloudStorageFileSystem.forBucket("bucket")) {
90+
CloudStorageFileSystem csfs = (CloudStorageFileSystem)fs;
91+
assertThat(csfs.config().maxChannelReopens()).isEqualTo(123);
92+
Files.write(fs.getPath("/angel"), ALONE.getBytes(UTF_8));
93+
path2 = Paths.get(URI.create("gs://bucket/angel"));
94+
try (SeekableByteChannel seekableByteChannel = Files.newByteChannel(path2)) {
95+
CloudStorageReadChannel cloudChannel = (CloudStorageReadChannel) seekableByteChannel;
96+
assertThat(cloudChannel.maxChannelReopens).isEqualTo(123);
97+
}
98+
}
99+
100+
// 4. Clean up.
101+
CloudStorageFileSystemProvider.setDefaultCloudStorageConfiguration(null);
102+
Path path3 = Paths.get(URI.create("gs://newbucket/file"));
103+
CloudStorageFileSystem gcs3 = (CloudStorageFileSystem)path3.getFileSystem();
104+
assertThat(gcs3.config().maxChannelReopens()).isEqualTo(0);
105+
}
106+
107+
@Test
108+
public void canOverrideDefaultOptions() throws IOException {
109+
// Set a new default.
110+
CloudStorageFileSystemProvider.setDefaultCloudStorageConfiguration(
111+
CloudStorageConfiguration.builder()
112+
.maxChannelReopens(123).build());
113+
114+
// This code wants its own value.
115+
try (FileSystem fs = CloudStorageFileSystem.forBucket("bucket", CloudStorageConfiguration.builder().maxChannelReopens(7).build())) {
116+
CloudStorageFileSystem csfs = (CloudStorageFileSystem)fs;
117+
assertThat(csfs.config().maxChannelReopens()).isEqualTo(7);
118+
}
119+
120+
// Clean up.
121+
CloudStorageFileSystemProvider.setDefaultCloudStorageConfiguration(null);
122+
}
123+
72124
@Test
73125
public void testGetPath() throws IOException {
74126
try (FileSystem fs = CloudStorageFileSystem.forBucket("bucket")) {

google-cloud-core-grpc/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
<dependency>
6868
<groupId>com.google.guava</groupId>
6969
<artifactId>guava-testlib</artifactId>
70-
<version>19.0</version>
7170
<scope>test</scope>
7271
</dependency>
7372
</dependencies>

0 commit comments

Comments
 (0)