diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index f7ecea18d76..4f06b36f605 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -7,6 +7,7 @@ Fixes #issue - [ ] Appropriate changes to README are included in PR - [ ] API's need to be enabled to test (tell us) - [ ] Environment Variables need to be set (ask us to set them) -- [ ] Tests pass (`mvn -P lint clean verify`) - * (Note- `Checkstyle` passing is required; `Spotbugs`, `ErrorProne`, `PMD`, etc. `ERROR`'s are advisory only) +- [ ] **Tests** pass: `mvn clean verify` **required** +- [ ] **Lint** passes: `mvn -P lint checkstyle:check` **required** +- [ ] **Static Analysis**: `mvn -P lint clean compile pmd:cpd-check spotbugs:check` **advisory only** - [ ] Please **merge** this PR for me once it is approved. diff --git a/.kokoro/lint/common.cfg b/.kokoro/lint/common.cfg new file mode 100644 index 00000000000..7a1f2aa1208 --- /dev/null +++ b/.kokoro/lint/common.cfg @@ -0,0 +1,38 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Format: //devtools/kokoro/config/proto/build.proto + +# Use the trampoline to bounce the script into docker. +gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/trampoline" +build_file: "java-docs-samples/.kokoro/trampoline.sh" +env_vars: { + key: "TRAMPOLINE_IMAGE" + value: "gcr.io/cloud-devrel-kokoro-resources/java11" +} +env_vars: { + key: "TRAMPOLINE_BUILD_FILE" + value: "github/java-docs-samples/.kokoro/tests/run_lint.sh" +} + +# Access btlr binaries used in the tests +gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/btlr" + + +# Upload logs to result-store +action { + define_artifacts { + regex: "**/*sponge_log.xml" + } +} diff --git a/.kokoro/lint/presubmit.cfg b/.kokoro/lint/presubmit.cfg new file mode 100644 index 00000000000..05df6c935a5 --- /dev/null +++ b/.kokoro/lint/presubmit.cfg @@ -0,0 +1,21 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Format: //devtools/kokoro/config/proto/build.proto + +# Tell the trampoline which build file to use. +env_vars: { + key: "GIT_DIFF" + value: "origin/master... ." +} diff --git a/.kokoro/static_analysis/common.cfg b/.kokoro/static_analysis/common.cfg new file mode 100644 index 00000000000..d4f8703d244 --- /dev/null +++ b/.kokoro/static_analysis/common.cfg @@ -0,0 +1,37 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Format: //devtools/kokoro/config/proto/build.proto + +# Use the trampoline to bounce the script into docker. +gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/trampoline" +build_file: "java-docs-samples/.kokoro/trampoline.sh" +env_vars: { + key: "TRAMPOLINE_IMAGE" + value: "gcr.io/cloud-devrel-kokoro-resources/java8" +} +env_vars: { + key: "TRAMPOLINE_BUILD_FILE" + value: "github/java-docs-samples/.kokoro/tests/run_static_analysis.sh" +} + +# Access btlr binaries used in the tests +gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/btlr" + +# Upload logs to result-store +action { + define_artifacts { + regex: "**/*sponge_log.xml" + } +} diff --git a/.kokoro/static_analysis/presubmit.cfg b/.kokoro/static_analysis/presubmit.cfg new file mode 100644 index 00000000000..5a32d08ee34 --- /dev/null +++ b/.kokoro/static_analysis/presubmit.cfg @@ -0,0 +1,19 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Format: //devtools/kokoro/config/proto/build.proto +env_vars: { + key: "GIT_DIFF" + value: "origin/master... ." +} diff --git a/.kokoro/tests/build_cloud_run.sh b/.kokoro/tests/build_cloud_run.sh index c964260fc3b..19e565040e7 100755 --- a/.kokoro/tests/build_cloud_run.sh +++ b/.kokoro/tests/build_cloud_run.sh @@ -27,7 +27,7 @@ if [ -n "$JIB" ]; then --platform=managed \ --region="${REGION:-us-central1}" \ --quiet --no-user-output-enabled - mvn clean + mvn -q -B clean } trap cleanup EXIT @@ -52,7 +52,7 @@ BASE_IMAGE_SAMPLES=("image-processing" "system-packages") # Build the service set -x - mvn jib:build -Dimage="${CONTAINER_IMAGE}" \ + mvn -q -B jib:build -Dimage="${CONTAINER_IMAGE}" \ `if [[ "${BASE_IMAGE_SAMPLES[@]}" =~ "${SAMPLE_NAME}" ]]; then echo "-Djib.from.image=${SPECIAL_BASE_IMAGE}"; fi` gcloud run deploy "${SERVICE_NAME}" \ diff --git a/.kokoro/tests/run_lint.sh b/.kokoro/tests/run_lint.sh new file mode 100755 index 00000000000..0a49d194b28 --- /dev/null +++ b/.kokoro/tests/run_lint.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# `-e` enables the script to automatically fail when a command fails +# `-o pipefail` sets the exit code to the rightmost comment to exit with a non-zero +set -eo pipefail + +# If on kokoro, add btlr to the path and cd into repo root +if [ -n "$KOKORO_GFILE_DIR" ]; then + bltr_dir="$KOKORO_GFILE_DIR/v0.0.1/" + chmod +x "${bltr_dir}"btlr + export PATH="$PATH:$bltr_dir" + cd github/java-docs-samples || exit +fi + +opts=() +if [ -n "$GIT_DIFF" ]; then + opts+=( + "--git-diff" + "$GIT_DIFF" + ) +fi + +btlr "${opts[@]}" run "**/pom.xml" -- mvn -P lint --quiet --batch-mode checkstyle:check diff --git a/.kokoro/tests/run_static_analysis.sh b/.kokoro/tests/run_static_analysis.sh new file mode 100755 index 00000000000..ee49976581b --- /dev/null +++ b/.kokoro/tests/run_static_analysis.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# `-e` enables the script to automatically fail when a command fails +# `-o pipefail` sets the exit code to the rightmost comment to exit with a non-zero +set -eo pipefail + +# If on kokoro, add btlr to the path and cd into repo root +if [ -n "$KOKORO_GFILE_DIR" ]; then + bltr_dir="$KOKORO_GFILE_DIR/v0.0.1/" + chmod +x "${bltr_dir}"btlr + export PATH="$PATH:$bltr_dir" + cd github/java-docs-samples || exit +fi + +opts=() +if [ -n "$GIT_DIFF" ]; then + opts+=( + "--git-diff" + "$GIT_DIFF" + ) +fi + +btlr "${opts[@]}" run "**/pom.xml" -- mvn -P lint --quiet --batch-mode compile pmd:cpd-check spotbugs:check diff --git a/.kokoro/tests/run_tests.sh b/.kokoro/tests/run_tests.sh index dd6b560a396..b6ed2845579 100755 --- a/.kokoro/tests/run_tests.sh +++ b/.kokoro/tests/run_tests.sh @@ -164,7 +164,7 @@ for file in **/pom.xml; do fi # Use maven to execute the tests for the project. - mvn -P lint -q --batch-mode --fail-at-end clean verify \ + mvn --quiet --batch-mode --fail-at-end clean verify \ -Dfile.encoding="UTF-8" \ -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn \ -Dmaven.test.redirectTestOutputToFile=true \ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 54b2641ddaa..efdb7034fa9 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -3,6 +3,13 @@ We'd love to accept your patches and contributions to this project. There are just a few small guidelines you need to follow. +In this repository, we are looking for patches that: +* Improve readibility +* Fix bugs +* Improve clarity and understandability + +If you want to contribute a full sample / tutorial, please consider contributing to our [community pages](https://cloud.google.com/community) [[How To](https://cloud.google.com/community/tutorials/write)] ([code](https://github.com/GoogleCloudPlatform/community)). + ## Contributor License Agreement Contributions to this project must be accompanied by a Contributor License diff --git a/appengine-java11/README.md b/appengine-java11/README.md index 29b84c82b42..5f2b7ed2553 100644 --- a/appengine-java11/README.md +++ b/appengine-java11/README.md @@ -47,11 +47,14 @@ To switch to an Open JDK 11 in a Cloud shell session, you can use: ``` ## Java 11 runtime +One way to deploy to App Engine Java 11 is directly from source. -The simplest way to deploy to App Engine Java 11 is using an executable [Uber JAR][uber-jar]. App Engine will automatically configure the `entrypoint` to run the JAR file. +* [`springboot-helloworld`](springboot-helloworld): Deploy a spring-boot application from source +* [`http-server`](http-server): Deploy an http application from source -* [`springboot-helloworld`](springboot-helloworld): Build a fat JAR with Spring Boot -* [`http-server`](http-server): Build a JAR using the Maven JAR Plugin +Another way is using the Maven App Engine Plugin to deploy an executable [Uber JAR][uber-jar]. App Engine will automatically configure the `entrypoint` to run the JAR file. Use this method when your application requires dependencies that are located locally, such as the [`appengine-simple-jetty-main`](appengine-simple-jetty-main) artifact. + +* [`gaeinfo`](gaeinfo): Build a JAR using the Maven JAR Plugin In addition, App Engine allows you to execute the `java` command directly in the `app.yaml` `entrypoint` field, so you can further customize your app's startup. diff --git a/appengine-java11/appengine-simple-jetty-main/README.md b/appengine-java11/appengine-simple-jetty-main/README.md index b5c4931f131..40fc75603a5 100644 --- a/appengine-java11/appengine-simple-jetty-main/README.md +++ b/appengine-java11/appengine-simple-jetty-main/README.md @@ -58,7 +58,6 @@ To use the dependency add the entrypoint to your `app.yaml` file. The entrypoint field will start the Jetty server and load your `WAR` file. ``` runtime: java11 -instance_class: F1 entrypoint: 'java -cp "*" com.example.appengine.demo.jettymain.Main helloworld.war' ``` diff --git a/appengine-java11/appengine-simple-jetty-main/pom.xml b/appengine-java11/appengine-simple-jetty-main/pom.xml index 89ff395499d..47ec0d3f495 100644 --- a/appengine-java11/appengine-simple-jetty-main/pom.xml +++ b/appengine-java11/appengine-simple-jetty-main/pom.xml @@ -14,7 +14,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -29,30 +29,30 @@ org.eclipse.jetty jetty-server - 9.4.29.v20200521 + 9.4.30.v20200611 org.eclipse.jetty jetty-webapp - 9.4.29.v20200521 + 9.4.30.v20200611 jar org.eclipse.jetty jetty-util - 9.4.29.v20200521 + 9.4.30.v20200611 org.eclipse.jetty jetty-annotations - 9.4.29.v20200521 + 9.4.30.v20200611 jar org.eclipse.jetty apache-jsp - 9.4.29.v20200521 + 9.4.30.v20200611 @@ -64,7 +64,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 diff --git a/appengine-java11/cloudsql/app.yaml b/appengine-java11/cloudsql/app.yaml index 6d89f733334..4ef8a485674 100644 --- a/appengine-java11/cloudsql/app.yaml +++ b/appengine-java11/cloudsql/app.yaml @@ -13,7 +13,6 @@ # limitations under the License. runtime: java11 -instance_class: F1 entrypoint: 'java -cp "*" com.example.appengine.demo.jettymain.Main cloudsql.war' env_variables: diff --git a/appengine-java11/cloudsql/pom.xml b/appengine-java11/cloudsql/pom.xml index 9d09c8ff1ee..cdd0af14a29 100644 --- a/appengine-java11/cloudsql/pom.xml +++ b/appengine-java11/cloudsql/pom.xml @@ -29,7 +29,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -96,7 +96,7 @@ org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 org.apache.maven.plugins @@ -137,7 +137,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG cloudsql diff --git a/appengine-java11/gaeinfo/pom.xml b/appengine-java11/gaeinfo/pom.xml index 5931d6e6b77..e34a46f49a4 100644 --- a/appengine-java11/gaeinfo/pom.xml +++ b/appengine-java11/gaeinfo/pom.xml @@ -27,7 +27,7 @@ Copyright 2019 Google LLC com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -84,7 +84,7 @@ Copyright 2019 Google LLC com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG gaeinfo @@ -94,7 +94,7 @@ Copyright 2019 Google LLC org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 org.apache.maven.plugins @@ -118,7 +118,7 @@ Copyright 2019 Google LLC org.eclipse.jetty jetty-maven-plugin - 9.4.29.v20200521 + 9.4.30.v20200611 diff --git a/appengine-java11/gaeinfo/src/main/appengine/app.yaml b/appengine-java11/gaeinfo/src/main/appengine/app.yaml index e3cf833766b..329b15fbced 100644 --- a/appengine-java11/gaeinfo/src/main/appengine/app.yaml +++ b/appengine-java11/gaeinfo/src/main/appengine/app.yaml @@ -13,5 +13,4 @@ # limitations under the License. runtime: java11 -instance_class: F1 entrypoint: 'java -cp "*" com.example.appengine.demo.jettymain.Main gaeinfo.war' diff --git a/appengine-java11/guestbook-cloud-firestore/pom.xml b/appengine-java11/guestbook-cloud-firestore/pom.xml index bbf5b383772..7de60dabfc5 100644 --- a/appengine-java11/guestbook-cloud-firestore/pom.xml +++ b/appengine-java11/guestbook-cloud-firestore/pom.xml @@ -26,7 +26,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -45,7 +45,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -59,9 +59,9 @@ provided - - - + + + com.example.appengine.demo simple-jetty-main @@ -94,7 +94,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG guestbook @@ -104,7 +104,7 @@ org.eclipse.jetty jetty-maven-plugin - 9.4.29.v20200521 + 9.4.30.v20200611 diff --git a/appengine-java11/guestbook-cloud-firestore/src/main/appengine/app.yaml b/appengine-java11/guestbook-cloud-firestore/src/main/appengine/app.yaml index d1ec5116101..bf22f662227 100644 --- a/appengine-java11/guestbook-cloud-firestore/src/main/appengine/app.yaml +++ b/appengine-java11/guestbook-cloud-firestore/src/main/appengine/app.yaml @@ -13,6 +13,5 @@ # [START gae_java11_firestore_yaml] runtime: java11 -instance_class: F1 entrypoint: 'java -cp "*" com.example.appengine.demo.jettymain.Main guestbook.war' # [END gae_java11_firestore_yaml] diff --git a/appengine-java11/helloworld-servlet/README.md b/appengine-java11/helloworld-servlet/README.md index 7d9a0647555..36ba51a2369 100644 --- a/appengine-java11/helloworld-servlet/README.md +++ b/appengine-java11/helloworld-servlet/README.md @@ -60,7 +60,6 @@ application settings: - The entrypoint field will start the Jetty server and load your `WAR` file. ``` runtime: java11 -instance_class: F1 entrypoint: 'java -cp "*" com.example.appengine.demo.jettymain.Main helloworld.war' ``` diff --git a/appengine-java11/helloworld-servlet/pom.xml b/appengine-java11/helloworld-servlet/pom.xml index 4d14b41b6ff..3621f4c546d 100644 --- a/appengine-java11/helloworld-servlet/pom.xml +++ b/appengine-java11/helloworld-servlet/pom.xml @@ -28,7 +28,7 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -70,7 +70,7 @@ limitations under the License. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG helloworld diff --git a/appengine-java11/helloworld-servlet/src/main/appengine/app.yaml b/appengine-java11/helloworld-servlet/src/main/appengine/app.yaml index eb920883627..af933b107ad 100644 --- a/appengine-java11/helloworld-servlet/src/main/appengine/app.yaml +++ b/appengine-java11/helloworld-servlet/src/main/appengine/app.yaml @@ -13,6 +13,5 @@ # [START gae_java11_servlet_yaml] runtime: java11 -instance_class: F1 entrypoint: 'java -cp "*" com.example.appengine.demo.jettymain.Main helloworld.war' # [END gae_java11_servlet_yaml] diff --git a/appengine-java11/http-server/README.md b/appengine-java11/http-server/README.md index e9551b1c81e..fbcc276a3a5 100644 --- a/appengine-java11/http-server/README.md +++ b/appengine-java11/http-server/README.md @@ -1,8 +1,7 @@ # Standalone HTTP Server on Google App Engine Standard with Java 11 -This sample shows how to deploy an application to Google App Engine using the -a fat jar. There is no `entrypoint` field listed in the [`app.yaml`](src/main/appengine/app.yaml), -as the application is a single fat jar with the correct MainClass field in the MANIFEST. +This sample shows how to deploy an application to Google App Engine from source. The `entrypoint` field listed in the [`app.yaml`](src/main/appengine/app.yaml) is not required, +as GAE will determine the entrypoint by searching the `target` directory for the .jar file with a Main-Class Manifest entry. ## Setup @@ -11,7 +10,7 @@ See [Prerequisites](../README.md#Prerequisites). ## Deploy to App Engine Standard ``` -mvn clean package appengine:deploy +gcloud app deploy ``` To view your app, use command: diff --git a/appengine-java11/http-server/pom.xml b/appengine-java11/http-server/pom.xml index 64ea82100c8..e3fa92a5800 100644 --- a/appengine-java11/http-server/pom.xml +++ b/appengine-java11/http-server/pom.xml @@ -11,7 +11,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -38,7 +38,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG http-server diff --git a/appengine-java11/kotlin-ktor/README.md b/appengine-java11/kotlin-ktor/README.md index 07642d80cba..0e4aec067da 100644 --- a/appengine-java11/kotlin-ktor/README.md +++ b/appengine-java11/kotlin-ktor/README.md @@ -10,7 +10,7 @@ See [Prerequisites](../README.md#Prerequisites). ## Deploying ```bash - mvn clean package appengine:deploy +gcloud app deploy ``` To view your app, use command: diff --git a/appengine-java11/kotlin-ktor/app.yaml b/appengine-java11/kotlin-ktor/app.yaml index 2f95e9a4de6..7d200ea8834 100644 --- a/appengine-java11/kotlin-ktor/app.yaml +++ b/appengine-java11/kotlin-ktor/app.yaml @@ -13,5 +13,4 @@ # limitations under the License. runtime: java11 -instance_class: F1 -entrypoint: 'java -Xmx32m -jar kotlin-ktor-0.0.1-jar-with-dependencies.jar' +entrypoint: 'java -jar target/kotlin-ktor-0.0.1-jar-with-dependencies.jar' diff --git a/appengine-java11/kotlin-ktor/pom.xml b/appengine-java11/kotlin-ktor/pom.xml index b8e82c57ce4..42591f05fb9 100644 --- a/appengine-java11/kotlin-ktor/pom.xml +++ b/appengine-java11/kotlin-ktor/pom.xml @@ -27,7 +27,7 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -173,7 +173,7 @@ limitations under the License. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG kotlin-ktor diff --git a/appengine-java11/micronaut-helloworld/README.md b/appengine-java11/micronaut-helloworld/README.md index ab62c4e6c42..be5db4ed8a3 100644 --- a/appengine-java11/micronaut-helloworld/README.md +++ b/appengine-java11/micronaut-helloworld/README.md @@ -10,7 +10,7 @@ See [Prerequisites](../README.md#Prerequisites). ## Deploying ```bash - mvn clean package appengine:deploy +gcloud app deploy ``` To view your app, use command: diff --git a/appengine-java11/micronaut-helloworld/pom.xml b/appengine-java11/micronaut-helloworld/pom.xml index 32325f23582..428e3d07d01 100644 --- a/appengine-java11/micronaut-helloworld/pom.xml +++ b/appengine-java11/micronaut-helloworld/pom.xml @@ -25,7 +25,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -40,7 +40,7 @@ io.micronaut micronaut-bom - 1.3.5 + 1.3.6 pom import @@ -91,7 +91,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG micronaut-helloworld @@ -100,7 +100,7 @@ org.apache.maven.plugins maven-shade-plugin - 3.2.3 + 3.2.4 package @@ -121,7 +121,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 java @@ -152,12 +152,12 @@ io.micronaut micronaut-inject-java - 1.3.5 + 1.3.6 io.micronaut micronaut-validation - 1.3.5 + 1.3.6 @@ -175,12 +175,12 @@ io.micronaut micronaut-inject-java - 1.3.5 + 1.3.6 io.micronaut micronaut-validation - 1.3.5 + 1.3.6 diff --git a/appengine-java11/micronaut-helloworld/src/main/appengine/app.yaml b/appengine-java11/micronaut-helloworld/src/main/appengine/app.yaml index 1440a7efa55..ad7258b8290 100644 --- a/appengine-java11/micronaut-helloworld/src/main/appengine/app.yaml +++ b/appengine-java11/micronaut-helloworld/src/main/appengine/app.yaml @@ -13,4 +13,3 @@ # limitations under the License. runtime: java11 -instance_class: F1 diff --git a/appengine-java11/oauth2/pom.xml b/appengine-java11/oauth2/pom.xml index 4b91542325d..7a90378d214 100644 --- a/appengine-java11/oauth2/pom.xml +++ b/appengine-java11/oauth2/pom.xml @@ -30,7 +30,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -99,7 +99,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG oauth2 @@ -109,7 +109,7 @@ org.eclipse.jetty jetty-maven-plugin - 9.4.29.v20200521 + 9.4.30.v20200611 diff --git a/appengine-java11/oauth2/src/main/appengine/app.yaml b/appengine-java11/oauth2/src/main/appengine/app.yaml index dea63eb178a..81198115969 100644 --- a/appengine-java11/oauth2/src/main/appengine/app.yaml +++ b/appengine-java11/oauth2/src/main/appengine/app.yaml @@ -13,7 +13,6 @@ # limitations under the License. runtime: java11 -instance_class: F1 entrypoint: 'java -cp "*" com.example.appengine.demo.jettymain.Main oauth2.war' # [START gae_java11_oauth2_yaml] diff --git a/appengine-java11/quarkus-helloworld/README.md b/appengine-java11/quarkus-helloworld/README.md index 54c3f7c1a74..6cd89e4e97d 100644 --- a/appengine-java11/quarkus-helloworld/README.md +++ b/appengine-java11/quarkus-helloworld/README.md @@ -10,7 +10,7 @@ See [Prerequisites](../README.md#Prerequisites). ## Deploying ```bash - mvn clean package appengine:deploy +gcloud app deploy ``` To view your app, use command: diff --git a/appengine-java11/quarkus-helloworld/pom.xml b/appengine-java11/quarkus-helloworld/pom.xml index 89cae36809f..cf821c1e99e 100644 --- a/appengine-java11/quarkus-helloworld/pom.xml +++ b/appengine-java11/quarkus-helloworld/pom.xml @@ -25,14 +25,14 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 2.22.2 11 11 - 1.4.2.Final + 1.5.2.Final UTF-8 @@ -92,7 +92,7 @@ limitations under the License. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG quarkus-helloworld diff --git a/appengine-java11/quarkus-helloworld/src/main/appengine/app.yaml b/appengine-java11/quarkus-helloworld/src/main/appengine/app.yaml index 1440a7efa55..ad7258b8290 100644 --- a/appengine-java11/quarkus-helloworld/src/main/appengine/app.yaml +++ b/appengine-java11/quarkus-helloworld/src/main/appengine/app.yaml @@ -13,4 +13,3 @@ # limitations under the License. runtime: java11 -instance_class: F1 diff --git a/appengine-java11/spanner/pom.xml b/appengine-java11/spanner/pom.xml index e961b9b266d..a077e74adba 100644 --- a/appengine-java11/spanner/pom.xml +++ b/appengine-java11/spanner/pom.xml @@ -30,7 +30,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -43,7 +43,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -84,7 +84,7 @@ org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 org.apache.maven.plugins @@ -108,7 +108,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG spanner diff --git a/appengine-java11/spanner/src/main/appengine/app.yaml b/appengine-java11/spanner/src/main/appengine/app.yaml index 0047b2062fb..1186677da27 100644 --- a/appengine-java11/spanner/src/main/appengine/app.yaml +++ b/appengine-java11/spanner/src/main/appengine/app.yaml @@ -13,7 +13,6 @@ # limitations under the License. runtime: java11 -instance_class: F1 entrypoint: 'java -cp "*" com.example.appengine.demo.jettymain.Main spanner.war' env_variables: diff --git a/appengine-java11/sparkjava-helloworld/README.md b/appengine-java11/sparkjava-helloworld/README.md index a4af6bccd47..8189053fc94 100644 --- a/appengine-java11/sparkjava-helloworld/README.md +++ b/appengine-java11/sparkjava-helloworld/README.md @@ -10,7 +10,7 @@ See [Prerequisites](../README.md#Prerequisites). ## Deploying ```bash - mvn clean package appengine:deploy +gcloud app deploy ``` To view your app, use command: diff --git a/appengine-java11/sparkjava-helloworld/pom.xml b/appengine-java11/sparkjava-helloworld/pom.xml index 17bc985196f..36d7aea82ae 100644 --- a/appengine-java11/sparkjava-helloworld/pom.xml +++ b/appengine-java11/sparkjava-helloworld/pom.xml @@ -29,7 +29,7 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -89,7 +89,7 @@ limitations under the License. org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 com.example.appengine.sparkdemo.Main @@ -101,7 +101,7 @@ limitations under the License. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG ${project.build.directory}/sparkjava-helloworld-1.0-jar-with-dependencies.jar diff --git a/appengine-java11/sparkjava-helloworld/src/main/appengine/app.yaml b/appengine-java11/sparkjava-helloworld/src/main/appengine/app.yaml index 1440a7efa55..ad7258b8290 100644 --- a/appengine-java11/sparkjava-helloworld/src/main/appengine/app.yaml +++ b/appengine-java11/sparkjava-helloworld/src/main/appengine/app.yaml @@ -13,4 +13,3 @@ # limitations under the License. runtime: java11 -instance_class: F1 diff --git a/appengine-java11/springboot-helloworld/README.md b/appengine-java11/springboot-helloworld/README.md index 33caf449dbd..c435497dd13 100644 --- a/appengine-java11/springboot-helloworld/README.md +++ b/appengine-java11/springboot-helloworld/README.md @@ -10,7 +10,7 @@ See [Prerequisites](../README.md#Prerequisites). ## Deploying ```bash - mvn clean package appengine:deploy +gcloud app deploy ``` To view your app, use command: diff --git a/appengine-java11/springboot-helloworld/pom.xml b/appengine-java11/springboot-helloworld/pom.xml index 0aaf5dff9e0..30ed9643d3d 100644 --- a/appengine-java11/springboot-helloworld/pom.xml +++ b/appengine-java11/springboot-helloworld/pom.xml @@ -25,7 +25,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -39,7 +39,7 @@ org.springframework.boot spring-boot-dependencies - 2.3.0.RELEASE + 2.3.1.RELEASE pom import @@ -47,7 +47,7 @@ org.springframework.cloud spring-cloud-dependencies - Hoxton.SR4 + Hoxton.SR6 pom import @@ -70,7 +70,7 @@ org.springframework.boot spring-boot-starter-jetty - 2.3.0.RELEASE + 2.3.1.RELEASE @@ -79,7 +79,7 @@ org.springframework.boot spring-boot-maven-plugin - 2.3.0.RELEASE + 2.3.1.RELEASE @@ -92,7 +92,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG springboot-helloworld diff --git a/appengine-java11/springboot-helloworld/src/main/appengine/app.yaml b/appengine-java11/springboot-helloworld/src/main/appengine/app.yaml index f59b90acf55..aa2763c27e2 100644 --- a/appengine-java11/springboot-helloworld/src/main/appengine/app.yaml +++ b/appengine-java11/springboot-helloworld/src/main/appengine/app.yaml @@ -14,9 +14,4 @@ # [START gae_java11_helloworld_yaml] runtime: java11 -instance_class: F1 - -# Explicitly set the memory limit and maximum heap size for the Spring Boot app -env_variables: - JAVA_TOOL_OPTIONS: "-XX:MaxRAM=256m -XX:ActiveProcessorCount=2 -Xmx32m" # [END gae_java11_helloworld_yaml] diff --git a/appengine-java11/tasks-handler/pom.xml b/appengine-java11/tasks-handler/pom.xml index eb881f38a74..726a0cf5813 100644 --- a/appengine-java11/tasks-handler/pom.xml +++ b/appengine-java11/tasks-handler/pom.xml @@ -28,7 +28,7 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -42,7 +42,7 @@ limitations under the License. org.springframework.boot spring-boot-dependencies - 2.3.0.RELEASE + 2.3.1.RELEASE pom import @@ -50,7 +50,7 @@ limitations under the License. org.springframework.cloud spring-cloud-dependencies - Hoxton.SR4 + Hoxton.SR6 pom import @@ -62,7 +62,7 @@ limitations under the License. org.springframework.boot spring-boot-starter-web - 2.3.0.RELEASE + 2.3.1.RELEASE @@ -74,7 +74,7 @@ limitations under the License. org.springframework.boot spring-boot-starter-jetty - 2.3.0.RELEASE + 2.3.1.RELEASE @@ -84,7 +84,7 @@ limitations under the License. org.springframework.boot spring-boot-maven-plugin - 2.3.0.RELEASE + 2.3.1.RELEASE @@ -96,7 +96,7 @@ limitations under the License. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG task-handler diff --git a/appengine-java11/tasks/README.md b/appengine-java11/tasks/README.md index 8a75733771f..2e8aafe414d 100644 --- a/appengine-java11/tasks/README.md +++ b/appengine-java11/tasks/README.md @@ -51,7 +51,7 @@ cd java-docs-samples/appengine-java11/task-handler - Deploy the app ``` -mvn clean package appengine:deploy +gcloud app deploy ``` ## Run the Sample Using the Command Line diff --git a/appengine-java11/tasks/pom.xml b/appengine-java11/tasks/pom.xml index 1b89bda7595..4ee66e86c65 100644 --- a/appengine-java11/tasks/pom.xml +++ b/appengine-java11/tasks/pom.xml @@ -30,7 +30,7 @@ Copyright 2019 Google LLC com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -47,7 +47,7 @@ Copyright 2019 Google LLC com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -82,7 +82,7 @@ Copyright 2019 Google LLC org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 false @@ -91,7 +91,7 @@ Copyright 2019 Google LLC org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 com.example.task.CreateTask false diff --git a/appengine-java11/tasks/src/test/java/com/example/task/CreateTaskIT.java b/appengine-java11/tasks/src/test/java/com/example/task/CreateTaskIT.java index 1fce0dff644..16793793482 100644 --- a/appengine-java11/tasks/src/test/java/com/example/task/CreateTaskIT.java +++ b/appengine-java11/tasks/src/test/java/com/example/task/CreateTaskIT.java @@ -17,11 +17,13 @@ package com.example.task; import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import org.junit.After; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.Timeout; @@ -34,6 +36,19 @@ public class CreateTaskIT { private ByteArrayOutputStream bout; private PrintStream out; + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable '%s' is required to perform these tests.".format(varName), + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("LOCATION_ID"); + requireEnvVar("QUEUE_ID"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + @Before public void setUp() { bout = new ByteArrayOutputStream(); diff --git a/appengine-java11/vertx-helloworld/README.md b/appengine-java11/vertx-helloworld/README.md index 6e0b0d7a6f5..021723af6b0 100644 --- a/appengine-java11/vertx-helloworld/README.md +++ b/appengine-java11/vertx-helloworld/README.md @@ -10,7 +10,7 @@ See [Prerequisites](../README.md#Prerequisites). ## Deploying ```bash - mvn clean package appengine:deploy +gcloud app deploy ``` ## See the application page diff --git a/appengine-java11/vertx-helloworld/pom.xml b/appengine-java11/vertx-helloworld/pom.xml index 3c294eeaba3..18dea634ddd 100644 --- a/appengine-java11/vertx-helloworld/pom.xml +++ b/appengine-java11/vertx-helloworld/pom.xml @@ -29,7 +29,7 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -97,7 +97,7 @@ limitations under the License. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG ${project.build.directory}/vertx-hello-j11-1.0-jar-with-dependencies.jar diff --git a/appengine-java8/analytics/pom.xml b/appengine-java8/analytics/pom.xml index c4d9a74ed60..80ba0a3b2c6 100644 --- a/appengine-java8/analytics/pom.xml +++ b/appengine-java8/analytics/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -110,7 +110,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG analytics diff --git a/appengine-java8/appidentity/pom.xml b/appengine-java8/appidentity/pom.xml index da560546eb4..ab5ebabcd7e 100644 --- a/appengine-java8/appidentity/pom.xml +++ b/appengine-java8/appidentity/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -51,7 +51,7 @@ org.json json - 20190722 + 20200518 @@ -108,7 +108,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/appengine-java8/bigquery/README.md b/appengine-java8/bigquery/README.md index 5f68312514b..0d64bf4d3df 100644 --- a/appengine-java8/bigquery/README.md +++ b/appengine-java8/bigquery/README.md @@ -1,13 +1,13 @@ Open in Cloud Shell -# Google Cloud API Showcase: Cloud BigQuery & StackDriver Monitoring in App Engine Standard Environment for Java 8 +# Google Cloud API Showcase: BigQuery & Cloud Monitoring in App Engine standard environment for Java 8 -This API Showcase demonstrates how to run an AppEngine standard environment application with dependencies on both -[Google BigQuery][bigquery] and [StackDriver Monitoring][stackdriver]. +This API Showcase demonstrates how to run an App Engine standard environment application with dependencies on both +[Google BigQuery][bigquery] and [StackDriver Monitoring][monitoring]. [bigquery]: https://cloud.google.com/bigquery/docs -[stackdriver]: https://cloud.google.com/monitoring/docs +[monitoring]: https://cloud.google.com/monitoring/docs The home page of this application provides a form to initiate a query of public data, in this case StackOverflow questions tagged with `google-bigquery`. @@ -35,12 +35,10 @@ cd appengine-java8/bigquery ``` - For local development, [set up][set-up] authentication - Enable [BigQuery][bigquery-api] and [Monitoring][monitoring-api] APIs -- If you have not already enabled your project for StackDriver, do so by following [these instructions][stackdriver-setup]. [set-up]: https://cloud.google.com/docs/authentication/getting-started [bigquery-api]: https://console.cloud.google.com/launcher/details/google/bigquery-json.googleapis.com [monitoring-api]: https://console.cloud.google.com/launcher/details/google/monitoring.googleapis.com -[stackdriver-setup]: https://cloud.google.com/monitoring/accounts/tiers#not-enabled ## Run locally Run using shown Maven command. You can then direct your browser to `http://localhost:8080/` to see the most recent query @@ -56,12 +54,12 @@ few moments and try again. ## Deploy -- Deploy to AppEngine standard environment using the following Maven command. +- Deploy to App Engine standard environment using the following Maven command. ``` mvn clean package appengine:deploy ``` - Direct your browser to `https://.appspot.com`. -- View more in-depth metrics data on the [StackDriver Monitoring Dashboard][dashboard] +- View more in-depth metrics data on the [Cloud Monitoring Dashboard][dashboard] [dashboard]: https://pantheon.corp.google.com/monitoring diff --git a/appengine-java8/bigquery/pom.xml b/appengine-java8/bigquery/pom.xml index 1cffd5aeec7..c056bdcac96 100644 --- a/appengine-java8/bigquery/pom.xml +++ b/appengine-java8/bigquery/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -52,7 +52,7 @@ com.google.cloud google-cloud-bigquery - 1.116.0 + 1.116.3 com.google.cloud @@ -117,7 +117,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG @@ -129,7 +129,7 @@ org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 diff --git a/appengine-java8/bigtable/gradle/wrapper/gradle-wrapper.properties b/appengine-java8/bigtable/gradle/wrapper/gradle-wrapper.properties index 4eb86d9dded..6799a302bd9 100644 --- a/appengine-java8/bigtable/gradle/wrapper/gradle-wrapper.properties +++ b/appengine-java8/bigtable/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip diff --git a/appengine-java8/bigtable/pom.xml b/appengine-java8/bigtable/pom.xml index 6c4277780e2..318c8ed4955 100644 --- a/appengine-java8/bigtable/pom.xml +++ b/appengine-java8/bigtable/pom.xml @@ -30,7 +30,7 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -122,7 +122,7 @@ limitations under the License. org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 true @@ -139,7 +139,7 @@ limitations under the License. org.apache.maven.plugins maven-failsafe-plugin - 3.0.0-M4 + 3.0.0-M5 ${bigtable.projectID} @@ -151,7 +151,7 @@ limitations under the License. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/appengine-java8/datastore-indexes-exploding/pom.xml b/appengine-java8/datastore-indexes-exploding/pom.xml index e0a2a11915a..497c2eba0a6 100644 --- a/appengine-java8/datastore-indexes-exploding/pom.xml +++ b/appengine-java8/datastore-indexes-exploding/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -95,7 +95,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/appengine-java8/datastore-indexes-perfect/pom.xml b/appengine-java8/datastore-indexes-perfect/pom.xml index 8fde40fd594..3b9e7ccc76b 100644 --- a/appengine-java8/datastore-indexes-perfect/pom.xml +++ b/appengine-java8/datastore-indexes-perfect/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -95,7 +95,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/appengine-java8/datastore-indexes/pom.xml b/appengine-java8/datastore-indexes/pom.xml index 94eb201f1a9..9ece6f8d291 100644 --- a/appengine-java8/datastore-indexes/pom.xml +++ b/appengine-java8/datastore-indexes/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -96,7 +96,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/appengine-java8/datastore-schedule-export/pom.xml b/appengine-java8/datastore-schedule-export/pom.xml index 378b13be2cd..94bb9209da6 100644 --- a/appengine-java8/datastore-schedule-export/pom.xml +++ b/appengine-java8/datastore-schedule-export/pom.xml @@ -10,7 +10,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -48,7 +48,7 @@ org.json json - 20190722 + 20200518 @@ -85,7 +85,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/appengine-java8/datastore/pom.xml b/appengine-java8/datastore/pom.xml index 5dacab89071..ab0ec83921f 100644 --- a/appengine-java8/datastore/pom.xml +++ b/appengine-java8/datastore/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -53,14 +53,14 @@ com.google.auto.value auto-value - 1.7.2 + 1.7.3 provided com.google.auto.value auto-value-annotations - 1.7.2 + 1.7.3 @@ -128,7 +128,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG @@ -145,7 +145,7 @@ com.google.auto.value auto-value - 1.7.2 + 1.7.3 diff --git a/appengine-java8/endpoints-v2-backend/gradle/wrapper/gradle-wrapper.properties b/appengine-java8/endpoints-v2-backend/gradle/wrapper/gradle-wrapper.properties index a4f0001d203..622ab64a3cb 100644 --- a/appengine-java8/endpoints-v2-backend/gradle/wrapper/gradle-wrapper.properties +++ b/appengine-java8/endpoints-v2-backend/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/appengine-java8/endpoints-v2-backend/pom.xml b/appengine-java8/endpoints-v2-backend/pom.xml index 4d0ca27880b..5e85dc52bf7 100644 --- a/appengine-java8/endpoints-v2-backend/pom.xml +++ b/appengine-java8/endpoints-v2-backend/pom.xml @@ -28,7 +28,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -84,7 +84,7 @@ org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 @@ -98,7 +98,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG diff --git a/appengine-java8/endpoints-v2-guice/gradle/wrapper/gradle-wrapper.properties b/appengine-java8/endpoints-v2-guice/gradle/wrapper/gradle-wrapper.properties index e1538e30a05..c26c61f9e76 100644 --- a/appengine-java8/endpoints-v2-guice/gradle/wrapper/gradle-wrapper.properties +++ b/appengine-java8/endpoints-v2-guice/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip diff --git a/appengine-java8/endpoints-v2-guice/pom.xml b/appengine-java8/endpoints-v2-guice/pom.xml index 5821f91f5e3..05cb69c3c5c 100644 --- a/appengine-java8/endpoints-v2-guice/pom.xml +++ b/appengine-java8/endpoints-v2-guice/pom.xml @@ -29,7 +29,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -85,7 +85,7 @@ org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 @@ -99,7 +99,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG diff --git a/appengine-java8/endpoints-v2-migration/gradle/wrapper/gradle-wrapper.properties b/appengine-java8/endpoints-v2-migration/gradle/wrapper/gradle-wrapper.properties index 41ad5c31a3e..8b75d9078fb 100644 --- a/appengine-java8/endpoints-v2-migration/gradle/wrapper/gradle-wrapper.properties +++ b/appengine-java8/endpoints-v2-migration/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip diff --git a/appengine-java8/endpoints-v2-migration/pom.xml b/appengine-java8/endpoints-v2-migration/pom.xml index 3f713d40c2e..cf93ca672e4 100644 --- a/appengine-java8/endpoints-v2-migration/pom.xml +++ b/appengine-java8/endpoints-v2-migration/pom.xml @@ -29,7 +29,7 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 3.0 @@ -71,7 +71,7 @@ limitations under the License. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG diff --git a/appengine-java8/endpoints-v2-skeleton/pom.xml b/appengine-java8/endpoints-v2-skeleton/pom.xml index b0ddacc511f..26cf98cae8f 100644 --- a/appengine-java8/endpoints-v2-skeleton/pom.xml +++ b/appengine-java8/endpoints-v2-skeleton/pom.xml @@ -27,7 +27,7 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -71,7 +71,7 @@ limitations under the License. org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 @@ -85,7 +85,7 @@ limitations under the License. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG diff --git a/appengine-java8/firebase-event-proxy/pom.xml b/appengine-java8/firebase-event-proxy/pom.xml index ed52bf00d27..502d3e83244 100644 --- a/appengine-java8/firebase-event-proxy/pom.xml +++ b/appengine-java8/firebase-event-proxy/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -68,12 +68,12 @@ com.fasterxml.jackson.core jackson-core - 2.11.0 + 2.11.1 com.fasterxml.jackson.core jackson-databind - 2.11.0 + 2.11.1 @@ -98,7 +98,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/appengine-java8/firebase-tictactoe/pom.xml b/appengine-java8/firebase-tictactoe/pom.xml index 846e12ab69a..6cf47e6af01 100644 --- a/appengine-java8/firebase-tictactoe/pom.xml +++ b/appengine-java8/firebase-tictactoe/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -49,16 +49,20 @@ jar provided - + + com.google.auth + google-auth-library-oauth2-http + 0.21.0 + com.google.code.gson gson 2.8.6 - com.googlecode.objectify - objectify - 5.1.24 + com.googlecode.objectify + objectify + 5.1.24 com.google.guava @@ -117,7 +121,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/appengine-java8/firebase-tictactoe/src/main/java/com/example/appengine/firetactoe/FirebaseChannel.java b/appengine-java8/firebase-tictactoe/src/main/java/com/example/appengine/firetactoe/FirebaseChannel.java index 76a992319af..2f9f5a238ed 100644 --- a/appengine-java8/firebase-tictactoe/src/main/java/com/example/appengine/firetactoe/FirebaseChannel.java +++ b/appengine-java8/firebase-tictactoe/src/main/java/com/example/appengine/firetactoe/FirebaseChannel.java @@ -16,9 +16,7 @@ package com.example.appengine.firetactoe; -import com.google.api.client.auth.oauth2.Credential; import com.google.api.client.extensions.appengine.http.UrlFetchTransport; -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.ByteArrayContent; import com.google.api.client.http.GenericUrl; import com.google.api.client.http.HttpRequestFactory; @@ -26,6 +24,8 @@ import com.google.api.client.http.HttpTransport; import com.google.appengine.api.appidentity.AppIdentityService; import com.google.appengine.api.appidentity.AppIdentityServiceFactory; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import com.google.common.io.BaseEncoding; import com.google.common.io.CharStreams; import com.google.gson.Gson; @@ -56,7 +56,7 @@ public class FirebaseChannel { "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit"; private String firebaseDbUrl; - private GoogleCredential credential; + private GoogleCredentials credential; // Keep this a package-private member variable, so that it can be mocked for unit tests HttpTransport httpTransport; @@ -91,7 +91,7 @@ private FirebaseChannel() { CharStreams.toString(new InputStreamReader(firebaseConfigStream, StandardCharsets.UTF_8)); firebaseDbUrl = parseFirebaseUrl(firebaseSnippet); - credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES); + credential = GoogleCredentials.getApplicationDefault().createScoped(FIREBASE_SCOPES); httpTransport = UrlFetchTransport.getDefaultInstance(); } catch (IOException e) { throw new RuntimeException(e); @@ -117,13 +117,15 @@ private static String parseFirebaseUrl(String firebaseSnippet) { /** * sendFirebaseMessage. + * * @param channelKey . * @param game . * @throws IOException . */ public void sendFirebaseMessage(String channelKey, Game game) throws IOException { // Make requests auth'ed using Application Default Credentials - HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential); + HttpRequestFactory requestFactory = + httpTransport.createRequestFactory(new HttpCredentialsAdapter(credential)); GenericUrl url = new GenericUrl(String.format("%s/channels/%s.json", firebaseDbUrl, channelKey)); HttpResponse response = null; @@ -152,9 +154,7 @@ url, new ByteArrayContent("application/json", gameJson.getBytes())) } } - /** - * Create a secure JWT token for the given userId. - */ + /** Create a secure JWT token for the given userId. */ public String createFirebaseToken(Game game, String userId) { final AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService(); final BaseEncoding base64 = BaseEncoding.base64(); @@ -186,6 +186,7 @@ public String createFirebaseToken(Game game, String userId) { /** * firebasePut. + * * @param path . * @param object . * @return . @@ -193,8 +194,10 @@ public String createFirebaseToken(Game game, String userId) { */ public HttpResponse firebasePut(String path, Object object) throws IOException { // Make requests auth'ed using Application Default Credentials - Credential credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES); - HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(FIREBASE_SCOPES); + HttpRequestFactory requestFactory = + httpTransport.createRequestFactory(new HttpCredentialsAdapter(credential)); String json = new Gson().toJson(object); GenericUrl url = new GenericUrl(path); @@ -206,6 +209,7 @@ public HttpResponse firebasePut(String path, Object object) throws IOException { /** * firebasePatch. + * * @param path . * @param object . * @return . @@ -213,8 +217,10 @@ public HttpResponse firebasePut(String path, Object object) throws IOException { */ public HttpResponse firebasePatch(String path, Object object) throws IOException { // Make requests auth'ed using Application Default Credentials - Credential credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES); - HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(FIREBASE_SCOPES); + HttpRequestFactory requestFactory = + httpTransport.createRequestFactory(new HttpCredentialsAdapter(credential)); String json = new Gson().toJson(object); GenericUrl url = new GenericUrl(path); @@ -226,6 +232,7 @@ public HttpResponse firebasePatch(String path, Object object) throws IOException /** * firebasePost. + * * @param path . * @param object . * @return . @@ -233,8 +240,10 @@ public HttpResponse firebasePatch(String path, Object object) throws IOException */ public HttpResponse firebasePost(String path, Object object) throws IOException { // Make requests auth'ed using Application Default Credentials - Credential credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES); - HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(FIREBASE_SCOPES); + HttpRequestFactory requestFactory = + httpTransport.createRequestFactory(new HttpCredentialsAdapter(credential)); String json = new Gson().toJson(object); GenericUrl url = new GenericUrl(path); @@ -246,14 +255,17 @@ public HttpResponse firebasePost(String path, Object object) throws IOException /** * firebaseGet. + * * @param path . * @return . * @throws IOException . */ public HttpResponse firebaseGet(String path) throws IOException { // Make requests auth'ed using Application Default Credentials - Credential credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES); - HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(FIREBASE_SCOPES); + HttpRequestFactory requestFactory = + httpTransport.createRequestFactory(new HttpCredentialsAdapter(credential)); GenericUrl url = new GenericUrl(path); @@ -262,14 +274,17 @@ public HttpResponse firebaseGet(String path) throws IOException { /** * firebaseDelete. + * * @param path . * @return . * @throws IOException . */ public HttpResponse firebaseDelete(String path) throws IOException { // Make requests auth'ed using Application Default Credentials - Credential credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES); - HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(FIREBASE_SCOPES); + HttpRequestFactory requestFactory = + httpTransport.createRequestFactory(new HttpCredentialsAdapter(credential)); GenericUrl url = new GenericUrl(path); diff --git a/appengine-java8/gaeinfo/pom.xml b/appengine-java8/gaeinfo/pom.xml index e49694094fd..4f5e0e2a560 100644 --- a/appengine-java8/gaeinfo/pom.xml +++ b/appengine-java8/gaeinfo/pom.xml @@ -28,7 +28,7 @@ Copyright 2017 Google Inc. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -81,7 +81,7 @@ Copyright 2017 Google Inc. org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 @@ -97,7 +97,7 @@ Copyright 2017 Google Inc. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/appengine-java8/guestbook-cloud-datastore/pom.xml b/appengine-java8/guestbook-cloud-datastore/pom.xml index 93add162859..ca87d0170a0 100644 --- a/appengine-java8/guestbook-cloud-datastore/pom.xml +++ b/appengine-java8/guestbook-cloud-datastore/pom.xml @@ -32,7 +32,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -112,7 +112,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/appengine-java8/helloworld/README.md b/appengine-java8/helloworld/README.md new file mode 100644 index 00000000000..2b9ee6cec2a --- /dev/null +++ b/appengine-java8/helloworld/README.md @@ -0,0 +1,91 @@ +HelloWorld for App Engine Standard (Java 8) +============================ + +This sample demonstrates how to deploy an application on Google App Engine. + +See the [Google App Engine standard environment documentation][ae-docs] for more +detailed instructions. + +[ae-docs]: https://cloud.google.com/appengine/docs/java/ + + +* [Java 8](http://www.oracle.com/technetwork/java/javase/downloads/index.html) +* [Maven](https://maven.apache.org/download.cgi) (at least 3.5) +* [Gradle](https://gradle.org/gradle-download/) (optional) +* [Google Cloud SDK](https://cloud.google.com/sdk/) (aka gcloud) + +## Setup + +• Download and initialize the [Cloud SDK](https://cloud.google.com/sdk/) + +``` +gcloud init +``` + +* Create an App Engine app within the current Google Cloud Project + +``` +gcloud app create +``` + +* In the `pom.xml`, update the [App Engine Maven Plugin](https://cloud.google.com/appengine/docs/standard/java/tools/maven-reference) +with your Google Cloud Project Id: + +``` + + com.google.cloud.tools + appengine-maven-plugin + 2.2.0 + + myProjectId + GCLOUD_CONFIG + + +``` +**Note:** `GCLOUD_CONFIG` is a special version for autogenerating an App Engine +version. Change this field to specify a specific version name. + +## Maven +### Running locally + + mvn package appengine:run + +To use vist: http://localhost:8080/ + +### Deploying + + mvn package appengine:deploy + +To use vist: https://YOUR-PROJECT-ID.appspot.com + +## Gradle +### Running locally + + gradle appengineRun + +If you do not have gradle installed, you can run using `./gradlew appengineRun`. + +To use vist: http://localhost:8080/ + +### Deploying + + gradle appengineDeploy + +If you do not have gradle installed, you can deploy using `./gradlew appengineDeploy`. + +To use vist: https://YOUR-PROJECT-ID.appspot.com + +## Testing + + mvn verify + + or + + gradle test + +As you add / modify the source code (`src/main/java/...`) it's very useful to add [unit testing](https://cloud.google.com/appengine/docs/java/tools/localunittesting) +to (`src/main/test/...`). The following resources are quite useful: + +* [Junit4](http://junit.org/junit4/) +* [Mockito](http://mockito.org/) +* [Truth](http://google.github.io/truth/) diff --git a/appengine-java8/helloworld/build.gradle b/appengine-java8/helloworld/build.gradle new file mode 100644 index 00000000000..e18ed2be836 --- /dev/null +++ b/appengine-java8/helloworld/build.gradle @@ -0,0 +1,86 @@ +// Copyright 2017 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// [START gradle] +buildscript { // Configuration for building + repositories { + jcenter() // Bintray's repository - a fast Maven Central mirror & more + mavenCentral() + } + dependencies { + classpath 'com.google.cloud.tools:appengine-gradle-plugin:2.2.0' // If a newer version is available, use it + } +} + +repositories { // repositories for Jar's you access in your code + maven { + url 'https://oss.sonatype.org/content/repositories/snapshots' // SNAPSHOT repository (if needed) + } + mavenCentral() + jcenter() +} + +apply plugin: 'java' // standard Java tasks +apply plugin: 'war' // standard Web Archive plugin +apply plugin: 'com.google.cloud.tools.appengine' // App Engine tasks + +dependencies { + compile 'com.google.appengine:appengine-api-1.0-sdk:+' // Latest App Engine Api's + providedCompile 'javax.servlet:javax.servlet-api:3.1.0' + + compile 'jstl:jstl:1.2' + +// Add your dependencies here. +// compile 'com.google.cloud:google-cloud:+' // Latest Cloud API's http://googlecloudplatform.github.io/google-cloud-java + + testCompile 'junit:junit:4.12' + testCompile 'com.google.truth:truth:0.33' + testCompile 'org.mockito:mockito-all:1.10.19' + + testCompile 'com.google.appengine:appengine-testing:+' + testCompile 'com.google.appengine:appengine-api-stubs:+' + testCompile 'com.google.appengine:appengine-tools-sdk:+' +} + +// Always run unit tests +appengineDeploy.dependsOn test +appengineStage.dependsOn test + +appengine { // App Engine tasks configuration + deploy { // deploy configuration + projectId = System.getenv('GOOGLE_CLOUD_PROJECT') + version = '1' + } +} + +test { + useJUnit() + testLogging.showStandardStreams = true + beforeTest { descriptor -> + logger.lifecycle("test: " + descriptor + " Running") + } + + onOutput { descriptor, event -> + logger.lifecycle("test: " + descriptor + ": " + event.message ) + } + afterTest { descriptor, result -> + logger.lifecycle("test: " + descriptor + ": " + result ) + } +} + +group = "com.example.appenginej8" // Generated output GroupId +version = "1.0-SNAPSHOT" // Version in generated output + +sourceCompatibility = 1.8 // App Engine Flexible uses Java 8 +targetCompatibility = 1.8 // App Engine Flexible uses Java 8 +// [END gradle] diff --git a/appengine-java8/helloworld/gradle/wrapper/gradle-wrapper.properties b/appengine-java8/helloworld/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000000..e79e02cafca --- /dev/null +++ b/appengine-java8/helloworld/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Tue Jun 13 16:53:48 PDT 2017 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip diff --git a/appengine-java8/helloworld/gradlew b/appengine-java8/helloworld/gradlew new file mode 100755 index 00000000000..4453ccea33d --- /dev/null +++ b/appengine-java8/helloworld/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save ( ) { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/appengine-java8/helloworld/gradlew.bat b/appengine-java8/helloworld/gradlew.bat new file mode 100644 index 00000000000..e95643d6a2c --- /dev/null +++ b/appengine-java8/helloworld/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/appengine-java8/helloworld/pom.xml b/appengine-java8/helloworld/pom.xml new file mode 100644 index 00000000000..028e81356d8 --- /dev/null +++ b/appengine-java8/helloworld/pom.xml @@ -0,0 +1,112 @@ + + + + + 4.0.0 + + war + + com.example.appengine-j8 + helloworld + 1.0-SNAPSHOT + + + com.google.cloud.samples + shared-configuration + 1.0.18 + + + + 1.8 + 1.8 + + false + + + + + + + com.google.appengine + appengine-api-1.0-sdk + 1.9.80 + + + javax.servlet + javax.servlet-api + 3.1.0 + jar + provided + + + + com.google.appengine + appengine-testing + 1.9.80 + test + + + com.google.appengine + appengine-api-stubs + 1.9.80 + test + + + com.google.appengine + appengine-tools-sdk + 1.9.80 + test + + + + com.google.truth + truth + 1.0.1 + test + + + junit + junit + 4.13 + test + + + org.mockito + mockito-core + 3.3.3 + test + + + + + + ${project.build.directory}/${project.build.finalName}/WEB-INF/classes + + + com.google.cloud.tools + appengine-maven-plugin + 2.3.0 + + + myProjectId + + GCLOUD_CONFIG + + + + + diff --git a/appengine-java8/helloworld/settings.gradle b/appengine-java8/helloworld/settings.gradle new file mode 100644 index 00000000000..cfa69e72f24 --- /dev/null +++ b/appengine-java8/helloworld/settings.gradle @@ -0,0 +1,18 @@ +/* + * This settings file was generated by the Gradle 'init' task. + * + * The settings file is used to specify which projects to include in your build. + * In a single project build this file can be empty or even removed. + * + * Detailed information about configuring a multi-project build in Gradle can be found + * in the user guide at https://docs.gradle.org/3.5/userguide/multi_project_builds.html + */ + +/* +// To declare projects as part of a multi-project build use the 'include' method +include 'shared' +include 'api' +include 'services:webservice' +*/ + +rootProject.name = 'helloworld' diff --git a/appengine-java8/helloworld/src/main/java/com/example/appengine/java8/HelloAppEngine.java b/appengine-java8/helloworld/src/main/java/com/example/appengine/java8/HelloAppEngine.java new file mode 100644 index 00000000000..77521a0c965 --- /dev/null +++ b/appengine-java8/helloworld/src/main/java/com/example/appengine/java8/HelloAppEngine.java @@ -0,0 +1,48 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.appengine.java8; + +import com.google.appengine.api.utils.SystemProperty; +import java.io.IOException; +import java.util.Properties; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +// With @WebServlet annotation the webapp/WEB-INF/web.xml is no longer required. +@WebServlet(name = "HelloAppEngine", value = "/hello") +public class HelloAppEngine extends HttpServlet { + + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws IOException { + Properties properties = System.getProperties(); + + response.setContentType("text/plain"); + response.getWriter().println("Hello App Engine - Standard using " + + SystemProperty.version.get() + " Java " + + properties.get("java.specification.version")); + } + + public static String getInfo() { + return "Version: " + System.getProperty("java.version") + + " OS: " + System.getProperty("os.name") + + " User: " + System.getProperty("user.name"); + } + +} diff --git a/appengine-java8/helloworld/src/main/webapp/WEB-INF/appengine-web.xml b/appengine-java8/helloworld/src/main/webapp/WEB-INF/appengine-web.xml new file mode 100644 index 00000000000..c5939d9f8ed --- /dev/null +++ b/appengine-java8/helloworld/src/main/webapp/WEB-INF/appengine-web.xml @@ -0,0 +1,22 @@ + + + + + java8 + true + + diff --git a/appengine-java8/helloworld/src/main/webapp/index.jsp b/appengine-java8/helloworld/src/main/webapp/index.jsp new file mode 100644 index 00000000000..6dbf14a3359 --- /dev/null +++ b/appengine-java8/helloworld/src/main/webapp/index.jsp @@ -0,0 +1,40 @@ + + +<%-- + ~ Copyright 2017 Google Inc. + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); you + ~ may not use this file except in compliance with the License. You may + ~ obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + ~ implied. See the License for the specific language governing + ~ permissions and limitations under the License. + --%> + +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ page import="com.example.appengine.java8.HelloAppEngine" %> + + + + Hello App Engine Standard Java 8 + + +

Hello App Engine -- Java 8!

+ +

This is <%= HelloAppEngine.getInfo() %>.

+ + + + + + + +
Available Servlets:
Hello App Engine
+ + + diff --git a/appengine-java8/helloworld/src/test/java/com/example/appengine/java8/HelloAppEngineTest.java b/appengine-java8/helloworld/src/test/java/com/example/appengine/java8/HelloAppEngineTest.java new file mode 100644 index 00000000000..cc8c39c0327 --- /dev/null +++ b/appengine-java8/helloworld/src/test/java/com/example/appengine/java8/HelloAppEngineTest.java @@ -0,0 +1,83 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.appengine.java8; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.when; + +import com.google.appengine.tools.development.testing.LocalServiceTestHelper; +import java.io.PrintWriter; +import java.io.StringWriter; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +/** + * Unit tests for {@link HelloAppEngine}. + */ +@RunWith(JUnit4.class) +public class HelloAppEngineTest { + private static final String FAKE_URL = "fake.fk/hello"; + // Set up a helper so that the ApiProxy returns a valid environment for local testing. + private final LocalServiceTestHelper helper = new LocalServiceTestHelper(); + + @Mock private HttpServletRequest mockRequest; + @Mock private HttpServletResponse mockResponse; + private StringWriter responseWriter; + private HelloAppEngine servletUnderTest; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + helper.setUp(); + + // Set up some fake HTTP requests + when(mockRequest.getRequestURI()).thenReturn(FAKE_URL); + + // Set up a fake HTTP response. + responseWriter = new StringWriter(); + when(mockResponse.getWriter()).thenReturn(new PrintWriter(responseWriter)); + + servletUnderTest = new HelloAppEngine(); + } + + @After public void tearDown() { + helper.tearDown(); + } + + @Test + public void doGetWritesResponse() throws Exception { + servletUnderTest.doGet(mockRequest, mockResponse); + + // We expect our hello world response. + assertThat(responseWriter.toString()) + .contains("Hello App Engine - Standard "); + } + + @Test + public void helloInfoTest() { + String result = HelloAppEngine.getInfo(); + assertThat(result) + .containsMatch("^Version:\\s+.+OS:\\s+.+User:\\s"); + } +} diff --git a/appengine-java8/iap/pom.xml b/appengine-java8/iap/pom.xml index e8930d97c55..8b1e6d21f3e 100644 --- a/appengine-java8/iap/pom.xml +++ b/appengine-java8/iap/pom.xml @@ -27,7 +27,7 @@ Copyright 2017 Google Inc. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -52,24 +52,11 @@ Copyright 2017 Google Inc. org.apache.maven.plugins maven-compiler-plugin 3.8.1 - - - -XDcompilePolicy=simple - -Xplugin:ErrorProne - - - - com.google.errorprone - error_prone_core - 2.3.4 - - -
com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/appengine-java8/images/pom.xml b/appengine-java8/images/pom.xml index c082a45a03e..9097af1d5d7 100644 --- a/appengine-java8/images/pom.xml +++ b/appengine-java8/images/pom.xml @@ -27,7 +27,7 @@ Copyright 2015 Google Inc. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -64,7 +64,7 @@ Copyright 2015 Google Inc. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG @@ -76,7 +76,7 @@ Copyright 2015 Google Inc. org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 diff --git a/appengine-java8/mail/pom.xml b/appengine-java8/mail/pom.xml index e17f153b214..838a3deac80 100644 --- a/appengine-java8/mail/pom.xml +++ b/appengine-java8/mail/pom.xml @@ -27,7 +27,7 @@ Copyright 2016 Google Inc. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -62,7 +62,7 @@ Copyright 2016 Google Inc. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/appengine-java8/mailgun/pom.xml b/appengine-java8/mailgun/pom.xml index e1c9a9625ac..1ec87c03224 100644 --- a/appengine-java8/mailgun/pom.xml +++ b/appengine-java8/mailgun/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -68,7 +68,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/appengine-java8/mailjet/pom.xml b/appengine-java8/mailjet/pom.xml index f28ed7d98db..fa9b82f154c 100644 --- a/appengine-java8/mailjet/pom.xml +++ b/appengine-java8/mailjet/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 1.8 @@ -74,7 +74,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/appengine-java8/memcache/pom.xml b/appengine-java8/memcache/pom.xml index e37521e3602..c64b19c1ea4 100644 --- a/appengine-java8/memcache/pom.xml +++ b/appengine-java8/memcache/pom.xml @@ -27,7 +27,7 @@ Copyright 2015 Google Inc. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -64,7 +64,7 @@ Copyright 2015 Google Inc. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG @@ -76,7 +76,7 @@ Copyright 2015 Google Inc. org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 diff --git a/appengine-java8/metadata/pom.xml b/appengine-java8/metadata/pom.xml index f4024604cdc..7e176a80173 100644 --- a/appengine-java8/metadata/pom.xml +++ b/appengine-java8/metadata/pom.xml @@ -28,7 +28,7 @@ Copyright 2017 Google Inc. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -76,7 +76,7 @@ Copyright 2017 Google Inc. org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 @@ -92,7 +92,7 @@ Copyright 2017 Google Inc. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/appengine-java8/multitenancy/pom.xml b/appengine-java8/multitenancy/pom.xml index 133af463f46..c369c4a653f 100644 --- a/appengine-java8/multitenancy/pom.xml +++ b/appengine-java8/multitenancy/pom.xml @@ -30,7 +30,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 1.8 @@ -119,7 +119,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/appengine-java8/oauth2/pom.xml b/appengine-java8/oauth2/pom.xml index efdd8221685..e5dd1bf9d02 100644 --- a/appengine-java8/oauth2/pom.xml +++ b/appengine-java8/oauth2/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 1.8 @@ -63,7 +63,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG @@ -75,7 +75,7 @@ org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 diff --git a/appengine-java8/pubsub/pom.xml b/appengine-java8/pubsub/pom.xml index 25f10f5f7b4..cf8a7c9a69e 100644 --- a/appengine-java8/pubsub/pom.xml +++ b/appengine-java8/pubsub/pom.xml @@ -28,7 +28,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -45,7 +45,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -84,7 +84,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG diff --git a/appengine-java8/remote-client/pom.xml b/appengine-java8/remote-client/pom.xml index df8fc53f0b6..a49c655d7fd 100644 --- a/appengine-java8/remote-client/pom.xml +++ b/appengine-java8/remote-client/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 1.8 diff --git a/appengine-java8/remote-server/pom.xml b/appengine-java8/remote-server/pom.xml index 9a29e3747ea..d856c0fbf46 100644 --- a/appengine-java8/remote-server/pom.xml +++ b/appengine-java8/remote-server/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -64,7 +64,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/appengine-java8/requests/pom.xml b/appengine-java8/requests/pom.xml index 6b14f426527..6153a1aeca2 100644 --- a/appengine-java8/requests/pom.xml +++ b/appengine-java8/requests/pom.xml @@ -28,7 +28,7 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -54,7 +54,7 @@ limitations under the License. org.json json - 20190722 + 20200518 @@ -103,7 +103,7 @@ limitations under the License. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG @@ -115,7 +115,7 @@ limitations under the License. org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 diff --git a/appengine-java8/search/pom.xml b/appengine-java8/search/pom.xml index c368152d117..d9d1e77b520 100644 --- a/appengine-java8/search/pom.xml +++ b/appengine-java8/search/pom.xml @@ -27,7 +27,7 @@ Copyright 2015 Google Inc. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -93,7 +93,7 @@ Copyright 2015 Google Inc. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG @@ -105,7 +105,7 @@ Copyright 2015 Google Inc. org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 diff --git a/appengine-java8/sendgrid/pom.xml b/appengine-java8/sendgrid/pom.xml index 3962c4b71e7..9d1bdb90cd8 100644 --- a/appengine-java8/sendgrid/pom.xml +++ b/appengine-java8/sendgrid/pom.xml @@ -27,7 +27,7 @@ Copyright 2018 Google LLC com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -48,7 +48,7 @@ Copyright 2018 Google LLC com.sendgrid sendgrid-java - 4.5.0 + 4.6.0 @@ -59,7 +59,7 @@ Copyright 2018 Google LLC com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG @@ -70,7 +70,7 @@ Copyright 2018 Google LLC org.eclipse.jetty jetty-maven-plugin - 9.4.29.v20200521 + 9.4.30.v20200611 diff --git a/appengine-java8/spanner/pom.xml b/appengine-java8/spanner/pom.xml index b832d6b9646..fefb5b08084 100644 --- a/appengine-java8/spanner/pom.xml +++ b/appengine-java8/spanner/pom.xml @@ -30,7 +30,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -45,7 +45,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -83,7 +83,7 @@ org.eclipse.jetty jetty-maven-plugin - 9.4.29.v20200521 + 9.4.30.v20200611 @@ -99,7 +99,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/appengine-java8/sparkjava-helloworld/.gitignore b/appengine-java8/sparkjava-helloworld/.gitignore new file mode 100644 index 00000000000..2d855fa45a4 --- /dev/null +++ b/appengine-java8/sparkjava-helloworld/.gitignore @@ -0,0 +1,4 @@ +.idea/* +**/target/* +*.iml +/target/ \ No newline at end of file diff --git a/appengine-java8/sparkjava-helloworld/README.md b/appengine-java8/sparkjava-helloworld/README.md new file mode 100644 index 00000000000..9bb638f00c1 --- /dev/null +++ b/appengine-java8/sparkjava-helloworld/README.md @@ -0,0 +1,72 @@ +App Engine SparkJava with Java8 +=== + +## Sample written in SparkJava for use with App Engine Java8 Standard. + +See the [Google App Engine standard environment documentation][ae-docs] for more +detailed instructions. + +[ae-docs]: https://cloud.google.com/appengine/docs/java/ + +* [Java 8](http://www.oracle.com/technetwork/java/javase/downloads/index.html) +* [Maven](https://maven.apache.org/download.cgi) (at least 3.5) +* [Google Cloud SDK](https://cloud.google.com/sdk/) (aka gcloud command line tool) + +## Setup + +* Download and initialize the [Cloud SDK](https://cloud.google.com/sdk/) + +``` +gcloud init +``` + +* Create an App Engine app within the current Google Cloud Project + +``` +gcloud app create +``` + +* In the `pom.xml`, update the [App Engine Maven Plugin](https://cloud.google.com/appengine/docs/standard/java/tools/maven-reference) +with your Google Cloud Project Id: + +``` + + com.google.cloud.tools + appengine-maven-plugin + 2.2.0 + + myProjectId + GCLOUD_CONFIG + + +``` +**Note:** `GCLOUD_CONFIG` is a special version for autogenerating an App Engine +version. Change this field to specify a specific version name. + +## Maven +### Running locally + +`mvn package appengine:run` + +To use vist: http://localhost:8080/ + +### Deploying + +`mvn package appengine:deploy` + +To use vist: https://YOUR-PROJECT-ID.appspot.com + +## Testing + +`mvn verify` + +As you add / modify the source code (`src/main/java/...`) it's very useful to add [unit testing](https://cloud.google.com/appengine/docs/java/tools/localunittesting) +to (`src/main/test/...`). The following resources are quite useful: + +* [Junit4](http://junit.org/junit4/) +* [Mockito](http://mockito.org/) +* [Truth](http://google.github.io/truth/) + + +For further information, consult the +[Java App Engine](https://developers.google.com/appengine/docs/java/overview) documentation. diff --git a/appengine-java8/sparkjava-helloworld/pom.xml b/appengine-java8/sparkjava-helloworld/pom.xml new file mode 100644 index 00000000000..4ffa742c28f --- /dev/null +++ b/appengine-java8/sparkjava-helloworld/pom.xml @@ -0,0 +1,138 @@ + + + + 4.0.0 + + sparkjava-hello-world + sparkjava-hello-world-java8-war-standard + 1.0 + war + + + com.google.cloud.samples + shared-configuration + 1.0.18 + + + + 1.8 + 1.8 + + + + + com.sparkjava + spark-core + 2.9.1 + + + org.eclipse.jetty + jetty-http + + + org.eclipse.jetty + jetty-io + + + org.eclipse.jetty + jetty-security + + + org.eclipse.jetty + jetty-server + + + org.eclipse.jetty + jetty-servlet + + + org.eclipse.jetty + jetty-webapp + + + + org.eclipse.jetty.websocket + websocket-api + + + org.eclipse.jetty.websocket + websocket-client + + + org.eclipse.jetty.websocket + websocket-common + + + org.eclipse.jetty.websocket + websocket-server + + + org.eclipse.jetty.websocket + websocket-servlet + + + + + com.google.appengine + appengine-api-1.0-sdk + 1.9.80 + jar + + + javax.servlet + javax.servlet-api + 4.0.1 + provided + + + + + + + maven-war-plugin + 3.3.0 + + false + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + + + + + com.google.cloud.tools + appengine-maven-plugin + 2.3.0 + + + myProjectId + + GCLOUD_CONFIG + + + + + + diff --git a/appengine-java8/sparkjava-helloworld/src/main/java/HelloWorld.java b/appengine-java8/sparkjava-helloworld/src/main/java/HelloWorld.java new file mode 100644 index 00000000000..098b42d8a36 --- /dev/null +++ b/appengine-java8/sparkjava-helloworld/src/main/java/HelloWorld.java @@ -0,0 +1,49 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import static spark.Spark.get; + +import javax.servlet.annotation.WebFilter; +import javax.servlet.annotation.WebInitParam; +import spark.servlet.SparkApplication; + + + +public class HelloWorld implements SparkApplication { + + public static void main(String[] args) { + new HelloWorld().init(); + } + + @Override + public void init() { + get("/", (request, response) + -> "Hello from SparkJava running on GAE Standard Java8 runtime."); + + get("/hello/:name", (request, response) -> { + return "SparkJava running on GAE Java8 says: Hello: " + request.params(":name"); + }); + } + + // Use Servlet annotation to define the Spark filter without web.xml: + @WebFilter( + filterName = "SparkInitFilter", urlPatterns = {"/*"}, + initParams = { + @WebInitParam(name = "applicationClass", value = "HelloWorld") + }) + public static class SparkInitFilter extends spark.servlet.SparkFilter { + } +} diff --git a/appengine-java8/sparkjava-helloworld/src/main/webapp/WEB-INF/appengine-web.xml b/appengine-java8/sparkjava-helloworld/src/main/webapp/WEB-INF/appengine-web.xml new file mode 100644 index 00000000000..159be9b20ab --- /dev/null +++ b/appengine-java8/sparkjava-helloworld/src/main/webapp/WEB-INF/appengine-web.xml @@ -0,0 +1,25 @@ + + + + true + false + true + java8 + + + + diff --git a/appengine-java8/sparkjava-helloworld/src/main/webapp/WEB-INF/logging.properties b/appengine-java8/sparkjava-helloworld/src/main/webapp/WEB-INF/logging.properties new file mode 100644 index 00000000000..b3b02da23df --- /dev/null +++ b/appengine-java8/sparkjava-helloworld/src/main/webapp/WEB-INF/logging.properties @@ -0,0 +1,24 @@ +# Copyright 2017 Google Inc. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# A default java.util.logging configuration. +# (All App Engine logging is through java.util.logging by default). +# +# To use this configuration, copy it into your application's WEB-INF +# folder and add the following to your appengine-web.xml: +# +# +# +# +# + +# Set the default logging level for all loggers to WARNING +.level = WARNING diff --git a/appengine-java8/springboot-helloworld/.gitignore b/appengine-java8/springboot-helloworld/.gitignore new file mode 100644 index 00000000000..2af7cefb0a3 --- /dev/null +++ b/appengine-java8/springboot-helloworld/.gitignore @@ -0,0 +1,24 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ \ No newline at end of file diff --git a/appengine-java8/springboot-helloworld/.mvn/wrapper/maven-wrapper.jar b/appengine-java8/springboot-helloworld/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000000..9cc84ea9b4d Binary files /dev/null and b/appengine-java8/springboot-helloworld/.mvn/wrapper/maven-wrapper.jar differ diff --git a/appengine-java8/springboot-helloworld/.mvn/wrapper/maven-wrapper.properties b/appengine-java8/springboot-helloworld/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000000..c3150437037 --- /dev/null +++ b/appengine-java8/springboot-helloworld/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1 @@ +distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip diff --git a/appengine-java8/springboot-helloworld/README.md b/appengine-java8/springboot-helloworld/README.md new file mode 100644 index 00000000000..4d31de6c1d8 --- /dev/null +++ b/appengine-java8/springboot-helloworld/README.md @@ -0,0 +1,182 @@ +SpringBoot HelloWorld for App Engine Standard (Java 8) +============================ + +This sample demonstrates how to deploy a Java 8 Spring Boot application on +Google App Engine. The Java 8 App Engine runtime expects a +[WAR file to be uploaded](https://cloud.google.com/appengine/docs/standard/java/tools/uploadinganapp). + +Note: If your project's root directory includes other Maven modules, EAR packages, +or .jar files that do not define an App Engine service, the command will fail when +configuration files are not found. + +See the [Google App Engine standard environment documentation][ae-docs] for more +detailed instructions. + +[ae-docs]: https://cloud.google.com/appengine/docs/java/ + +* [Java 8](http://www.oracle.com/technetwork/java/javase/downloads/index.html) +* [Maven](https://maven.apache.org/download.cgi) (at least 3.5) +* [Google Cloud SDK](https://cloud.google.com/sdk/) (aka gcloud command line tool) + +## Setup + +* Download and initialize the [Cloud SDK](https://cloud.google.com/sdk/) + +``` +gcloud init +``` + +* Create an App Engine app within the current Google Cloud Project + +``` +gcloud app create +``` + +* In the `pom.xml`, update the [App Engine Maven Plugin](https://cloud.google.com/appengine/docs/standard/java/tools/maven-reference) +with your Google Cloud Project Id: + +``` + + com.google.cloud.tools + appengine-maven-plugin + 2.2.0 + + myProjectId + GCLOUD_CONFIG + + +``` +**Note:** `GCLOUD_CONFIG` is a special version for autogenerating an App Engine +version. Change this field to specify a specific version name. + +## Maven +### Running locally + +`mvn package appengine:run` + +To use vist: http://localhost:8080/ + +### Deploying + +`mvn package appengine:deploy` + +To use vist: https://YOUR-PROJECT-ID.appspot.com + +## Testing + +`mvn verify` + +As you add / modify the source code (`src/main/java/...`) it's very useful to add [unit testing](https://cloud.google.com/appengine/docs/java/tools/localunittesting) +to (`src/main/test/...`). The following resources are quite useful: + +* [Junit4](http://junit.org/junit4/) +* [Mockito](http://mockito.org/) +* [Truth](http://google.github.io/truth/) + + +For further information, consult the +[Java App Engine](https://developers.google.com/appengine/docs/java/overview) documentation. + +## Steps to convert a Spring Boot application for App Engine Standard +### Use the WAR packaging +You must use WAR packaging to deploy into Google App Engine Standard. + +If you generate a Spring Boot project from [start.spring.io](http://start.spring.io/), +make sure you *switch to the full version* view of the initializer site, and select *WAR* +packaging. + +If you have an existing `JAR` packaging project, you can convert it into a `WAR` project by: +1. In `pom.xml`, change `jar` to `war` +1. Create a new `SpringBootServletInitializer` implementation: + +```java +public class ServletInitializer extends SpringBootServletInitializer { + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + return application.sources(YourApplication.class); + } +} +``` + +### Remove Tomcat Starter +Google App Engine Standard deploys your `WAR` into a Jetty server. Spring Boot's starter +includes Tomcat by default. This will introduce conflicts. Exclude Tomcat dependencies: +```xml + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-tomcat + + + +``` + +Do not include the Jetty dependencies. But you must include Servlet API dependency: +```xml + + javax.servlet + javax.servlet-api + 3.1.0 + provided + +``` + +### Add App Engine Standard Plugin +In the `pom.xml`, add the App Engine Standard plugin: +```xml + + com.google.cloud.tools + appengine-maven-plugin + 2.2.0 + +``` + +This plugin is used to run local development server as well as deploying the application +into Google App Engine. + +### Add App Engine Configuration +Add a `src/main/webapp/WEB-INF/appengine-web.xml`: +```xml + + 1 + true + java8 + +``` + +This configure is required for applications running in Google App Engine. + +### Exclude JUL to SLF4J Bridge +Spring Boot's default logging bridge conflicts with Jetty's logging system. +To be able to capture the Spring Boot startup logs, you need to exclude +`org.slf4j:jul-to-slf4j` dependency. The easiest way to do this is to +set the dependency scope to `provided`, so that it won't be included in +the `WAR` file: + +```xml + + + org.slf4j + jul-to-slf4j + provided + +``` + +### Out of memory errors + +With Spring Boot >= 1.5.6, you may run into out of memory errors on startup. +Please follow these instructions to work around this issue: + +1. Inside src/main/resources, adding a logging.properties file with: +```ini +.level = INFO +``` +2. Inside src/main/webapp/WEB-INF/appengine-web.xml, add a config that points to the new logging.properties file. +```xml + + + +``` diff --git a/appengine-java8/springboot-helloworld/mvnw b/appengine-java8/springboot-helloworld/mvnw new file mode 100755 index 00000000000..5bf251c0774 --- /dev/null +++ b/appengine-java8/springboot-helloworld/mvnw @@ -0,0 +1,225 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven2 Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Migwn, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" + # TODO classpath? +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +echo $MAVEN_PROJECTBASEDIR +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/appengine-java8/springboot-helloworld/mvnw.cmd b/appengine-java8/springboot-helloworld/mvnw.cmd new file mode 100644 index 00000000000..019bd74d766 --- /dev/null +++ b/appengine-java8/springboot-helloworld/mvnw.cmd @@ -0,0 +1,143 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM http://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven2 Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" + +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/appengine-java8/springboot-helloworld/pom.xml b/appengine-java8/springboot-helloworld/pom.xml new file mode 100644 index 00000000000..03579ac202b --- /dev/null +++ b/appengine-java8/springboot-helloworld/pom.xml @@ -0,0 +1,130 @@ + + + 4.0.0 + + com.google.appengine.demos + springboot-appengine-standard + 0.0.1-SNAPSHOT + war + + springboot-appengine-standard + Demo project for Spring Boot + + + com.google.cloud.samples + shared-configuration + 1.0.18 + + + + UTF-8 + UTF-8 + 2.3.1.RELEASE + 1.8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + ${spring.boot.version} + + + + org.springframework.boot + spring-boot-starter-tomcat + + + + + + + org.slf4j + jul-to-slf4j + provided + + + + + javax.servlet + javax.servlet-api + 4.0.1 + provided + + + + org.springframework.boot + spring-boot-starter-test + ${spring.boot.version} + test + + + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring.boot.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring.boot.version} + + + com.google.cloud.tools + appengine-maven-plugin + 2.3.0 + + + myProjectId + + GCLOUD_CONFIG + + + + + org.codehaus.mojo + versions-maven-plugin + 2.7 + + + compile + + display-dependency-updates + display-plugin-updates + + + + + + javax.servlet:javax.servlet-api + com.google.guava:guava + + + + + + maven-war-plugin + 3.3.0 + + false + + + + + + + + diff --git a/appengine-java8/springboot-helloworld/src/main/java/com/example/appengine/demos/springboot/HelloworldController.java b/appengine-java8/springboot-helloworld/src/main/java/com/example/appengine/demos/springboot/HelloworldController.java new file mode 100644 index 00000000000..f0866e71770 --- /dev/null +++ b/appengine-java8/springboot-helloworld/src/main/java/com/example/appengine/demos/springboot/HelloworldController.java @@ -0,0 +1,28 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.appengine.demos.springboot; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HelloworldController { + @GetMapping("/") + public String hello() { + return "Hello world - springboot-appengine-standard!"; + } +} diff --git a/appengine-java8/springboot-helloworld/src/main/java/com/example/appengine/demos/springboot/ServletInitializer.java b/appengine-java8/springboot-helloworld/src/main/java/com/example/appengine/demos/springboot/ServletInitializer.java new file mode 100644 index 00000000000..202df907f9b --- /dev/null +++ b/appengine-java8/springboot-helloworld/src/main/java/com/example/appengine/demos/springboot/ServletInitializer.java @@ -0,0 +1,29 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.appengine.demos.springboot; + +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; + +public class ServletInitializer extends SpringBootServletInitializer { + + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + return application.sources(SpringBootExampleApplication.class); + } + +} diff --git a/appengine-java8/springboot-helloworld/src/main/java/com/example/appengine/demos/springboot/SpringBootExampleApplication.java b/appengine-java8/springboot-helloworld/src/main/java/com/example/appengine/demos/springboot/SpringBootExampleApplication.java new file mode 100644 index 00000000000..e0f25c84bac --- /dev/null +++ b/appengine-java8/springboot-helloworld/src/main/java/com/example/appengine/demos/springboot/SpringBootExampleApplication.java @@ -0,0 +1,28 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.appengine.demos.springboot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootExampleApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootExampleApplication.class, args); + } +} diff --git a/appengine-java8/springboot-helloworld/src/main/resources/application.properties b/appengine-java8/springboot-helloworld/src/main/resources/application.properties new file mode 100644 index 00000000000..c054a219499 --- /dev/null +++ b/appengine-java8/springboot-helloworld/src/main/resources/application.properties @@ -0,0 +1,14 @@ +# Copyright 2017 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# diff --git a/appengine-java8/springboot-helloworld/src/main/resources/application.yaml b/appengine-java8/springboot-helloworld/src/main/resources/application.yaml new file mode 100644 index 00000000000..65aaf033ee4 --- /dev/null +++ b/appengine-java8/springboot-helloworld/src/main/resources/application.yaml @@ -0,0 +1,17 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +logging: + level.ROOT: INFO + level.org.springframework.test.web.servlet.result: DEBUG \ No newline at end of file diff --git a/appengine-java8/springboot-helloworld/src/main/resources/logging.properties b/appengine-java8/springboot-helloworld/src/main/resources/logging.properties new file mode 100644 index 00000000000..a2ff664a075 --- /dev/null +++ b/appengine-java8/springboot-helloworld/src/main/resources/logging.properties @@ -0,0 +1,15 @@ +# Copyright 2017 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +.level = INFO diff --git a/appengine-java8/springboot-helloworld/src/main/webapp/WEB-INF/appengine-web.xml b/appengine-java8/springboot-helloworld/src/main/webapp/WEB-INF/appengine-web.xml new file mode 100644 index 00000000000..c4a1b38d85d --- /dev/null +++ b/appengine-java8/springboot-helloworld/src/main/webapp/WEB-INF/appengine-web.xml @@ -0,0 +1,20 @@ + + + + true + java8 + + + + diff --git a/errorreporting/src/test/java/com/example/errorreporting/QuickStartIT.java b/appengine-java8/springboot-helloworld/src/test/java/com/example/appengine/demos/springboot/SpringBootExampleApplicationTests.java similarity index 68% rename from errorreporting/src/test/java/com/example/errorreporting/QuickStartIT.java rename to appengine-java8/springboot-helloworld/src/test/java/com/example/appengine/demos/springboot/SpringBootExampleApplicationTests.java index 2c9e3099b46..e6eda8e0036 100644 --- a/errorreporting/src/test/java/com/example/errorreporting/QuickStartIT.java +++ b/appengine-java8/springboot-helloworld/src/test/java/com/example/appengine/demos/springboot/SpringBootExampleApplicationTests.java @@ -14,19 +14,19 @@ * limitations under the License. */ -package com.example.errorreporting; +package com.example.appengine.demos.springboot; import org.junit.Test; import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; -@RunWith(JUnit4.class) -@SuppressWarnings("AbbreviationAsWordInName") -public class QuickStartIT { +@RunWith(SpringRunner.class) +@SpringBootTest +public class SpringBootExampleApplicationTests { @Test - public void testQuickStart() throws Exception { - // Ensure quick start runs without any exception - QuickStart.main(new String[]{}); + public void contextLoads() { } + } diff --git a/appengine-java8/static-files/pom.xml b/appengine-java8/static-files/pom.xml index 53c18f30ef7..e6e3d96f03b 100644 --- a/appengine-java8/static-files/pom.xml +++ b/appengine-java8/static-files/pom.xml @@ -27,7 +27,7 @@ Copyright 2015 Google Inc. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -51,7 +51,7 @@ Copyright 2015 Google Inc. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/appengine-java8/taskqueues-deferred/pom.xml b/appengine-java8/taskqueues-deferred/pom.xml index 2c2fed6540f..d95e9427e86 100644 --- a/appengine-java8/taskqueues-deferred/pom.xml +++ b/appengine-java8/taskqueues-deferred/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -87,7 +87,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG @@ -99,7 +99,7 @@ org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 diff --git a/appengine-java8/taskqueues-pull/pom.xml b/appengine-java8/taskqueues-pull/pom.xml index 69e4f18fd8c..a271454ee16 100644 --- a/appengine-java8/taskqueues-pull/pom.xml +++ b/appengine-java8/taskqueues-pull/pom.xml @@ -30,7 +30,7 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -81,7 +81,7 @@ limitations under the License. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG @@ -93,7 +93,7 @@ limitations under the License. org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 diff --git a/appengine-java8/taskqueues-push/pom.xml b/appengine-java8/taskqueues-push/pom.xml index 1ac097508ee..35ed88261ad 100644 --- a/appengine-java8/taskqueues-push/pom.xml +++ b/appengine-java8/taskqueues-push/pom.xml @@ -28,7 +28,7 @@ Copyright 2016 Google Inc. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -48,7 +48,7 @@ Copyright 2016 Google Inc. org.json json - 20190722 + 20200518 com.google.appengine @@ -101,7 +101,7 @@ Copyright 2016 Google Inc. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG @@ -113,7 +113,7 @@ Copyright 2016 Google Inc. org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 diff --git a/appengine-java8/tasks/app/pom.xml b/appengine-java8/tasks/app/pom.xml index 2fe9fd58271..9392dd46674 100644 --- a/appengine-java8/tasks/app/pom.xml +++ b/appengine-java8/tasks/app/pom.xml @@ -30,7 +30,7 @@ Copyright 2019 Google LLC com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -50,7 +50,7 @@ Copyright 2019 Google LLC com.google.cloud google-cloud-tasks - 1.29.1 + 1.30.0 @@ -80,7 +80,7 @@ Copyright 2019 Google LLC com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG @@ -92,7 +92,7 @@ Copyright 2019 Google LLC org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 diff --git a/appengine-java8/tasks/quickstart/pom.xml b/appengine-java8/tasks/quickstart/pom.xml index d826228838f..0b55c2f7bc3 100644 --- a/appengine-java8/tasks/quickstart/pom.xml +++ b/appengine-java8/tasks/quickstart/pom.xml @@ -30,7 +30,7 @@ Copyright 2018 Google LLC com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -52,7 +52,7 @@ Copyright 2018 Google LLC com.google.cloud google-cloud-tasks - 1.29.1 + 1.30.0 commons-cli @@ -83,7 +83,7 @@ Copyright 2018 Google LLC com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG @@ -92,16 +92,16 @@ Copyright 2018 Google LLC - + org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 com.example.task.CreateTask false diff --git a/appengine-java8/tasks/quickstart/src/test/java/com/example/task/CreateTaskIT.java b/appengine-java8/tasks/quickstart/src/test/java/com/example/task/CreateTaskIT.java index fce9382a169..16b788687e5 100644 --- a/appengine-java8/tasks/quickstart/src/test/java/com/example/task/CreateTaskIT.java +++ b/appengine-java8/tasks/quickstart/src/test/java/com/example/task/CreateTaskIT.java @@ -17,11 +17,13 @@ package com.example.task; import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import org.junit.After; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.Timeout; @@ -38,6 +40,19 @@ public class CreateTaskIT { private String location = System.getenv("LOCATION_ID"); private String queueName = System.getenv("QUEUE_ID"); + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable '%s' is required to perform these tests.".format(varName), + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("LOCATION_ID"); + requireEnvVar("QUEUE_ID"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + @Before public void setUp() { bout = new ByteArrayOutputStream(); diff --git a/appengine-java8/tasks/snippets/pom.xml b/appengine-java8/tasks/snippets/pom.xml index 27b95af9bde..59c9a204559 100644 --- a/appengine-java8/tasks/snippets/pom.xml +++ b/appengine-java8/tasks/snippets/pom.xml @@ -30,7 +30,7 @@ Copyright 2019 Google LLC com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -44,12 +44,12 @@ Copyright 2019 Google LLC com.google.cloud google-cloud-tasks - 1.29.1 + 1.30.0 com.google.protobuf protobuf-java - 3.12.1 + 3.12.2 diff --git a/appengine-java8/translate-pubsub/pom.xml b/appengine-java8/translate-pubsub/pom.xml index 0254d705443..e8920092ea6 100644 --- a/appengine-java8/translate-pubsub/pom.xml +++ b/appengine-java8/translate-pubsub/pom.xml @@ -28,7 +28,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -44,7 +44,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -82,7 +82,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG @@ -94,7 +94,7 @@ org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 diff --git a/appengine-java8/twilio/pom.xml b/appengine-java8/twilio/pom.xml index b01f4c316f7..5e37b438076 100644 --- a/appengine-java8/twilio/pom.xml +++ b/appengine-java8/twilio/pom.xml @@ -27,7 +27,7 @@ Copyright 2015 Google Inc. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -59,7 +59,7 @@ Copyright 2015 Google Inc. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/appengine-java8/urlfetch/pom.xml b/appengine-java8/urlfetch/pom.xml index 03bbfe2296e..075adbe9ff2 100644 --- a/appengine-java8/urlfetch/pom.xml +++ b/appengine-java8/urlfetch/pom.xml @@ -27,7 +27,7 @@ Copyright 2015 Google Inc. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -47,7 +47,7 @@ Copyright 2015 Google Inc. org.json json - 20190722 + 20200518 @@ -58,7 +58,7 @@ Copyright 2015 Google Inc. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/appengine-java8/users/pom.xml b/appengine-java8/users/pom.xml index bf7b4dc7fd5..a9621d06bde 100644 --- a/appengine-java8/users/pom.xml +++ b/appengine-java8/users/pom.xml @@ -29,7 +29,7 @@ Copyright 2015 Google Inc. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 1.8 @@ -59,7 +59,7 @@ Copyright 2015 Google Inc. org.json json - 20190722 + 20200518 @@ -108,7 +108,7 @@ Copyright 2015 Google Inc. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/asset/cloud-client/pom.xml b/asset/cloud-client/pom.xml index acf65ebac07..34a4ea9860b 100644 --- a/asset/cloud-client/pom.xml +++ b/asset/cloud-client/pom.xml @@ -23,7 +23,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -35,7 +35,7 @@ com.google.cloud google-cloud-asset - 1.2.0 + 1.4.0 @@ -55,18 +55,18 @@ com.google.cloud google-cloud-storage - 1.108.0 + 1.111.0 test com.google.cloud google-cloud-bigquery - 1.116.0 + 1.116.3 com.google.cloud google-cloud-pubsub - 1.105.1 + 1.107.0 com.google.cloud diff --git a/asset/cloud-client/src/main/java/com/example/asset/SearchAllIamPoliciesExample.java b/asset/cloud-client/src/main/java/com/example/asset/SearchAllIamPoliciesExample.java new file mode 100644 index 00000000000..202f85af29a --- /dev/null +++ b/asset/cloud-client/src/main/java/com/example/asset/SearchAllIamPoliciesExample.java @@ -0,0 +1,58 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.asset; + +// [START asset_quickstart_search_all_iam_policies] +import com.google.api.gax.rpc.ApiException; +import com.google.api.gax.rpc.InvalidArgumentException; +import com.google.cloud.asset.v1.AssetServiceClient; +import com.google.cloud.asset.v1.AssetServiceClient.SearchAllIamPoliciesPagedResponse; +import com.google.cloud.asset.v1.SearchAllIamPoliciesRequest; +import java.io.IOException; + +public class SearchAllIamPoliciesExample { + + // Searches for all the iam policies within the given scope. + public static void searchAllIamPolicies(String scope, String query) { + // TODO(developer): Replace these variables before running the sample. + int pageSize = 0; + String pageToken = ""; + + SearchAllIamPoliciesRequest request = + SearchAllIamPoliciesRequest.newBuilder() + .setScope(scope) + .setQuery(query) + .setPageSize(pageSize) + .setPageToken(pageToken) + .build(); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AssetServiceClient client = AssetServiceClient.create()) { + SearchAllIamPoliciesPagedResponse response = client.searchAllIamPolicies(request); + System.out.println("Search completed successfully:\n" + response.getPage().getValues()); + } catch (IOException e) { + System.out.println(String.format("Failed to create client:%n%s", e.toString())); + } catch (InvalidArgumentException e) { + System.out.println(String.format("Invalid request:%n%s", e.toString())); + } catch (ApiException e) { + System.out.println(String.format("Error during SearchAllIamPolicies:%n%s", e.toString())); + } + } +} +// [END asset_quickstart_search_all_iam_policies] diff --git a/asset/cloud-client/src/main/java/com/example/asset/SearchAllResourcesExample.java b/asset/cloud-client/src/main/java/com/example/asset/SearchAllResourcesExample.java new file mode 100644 index 00000000000..fa961008c19 --- /dev/null +++ b/asset/cloud-client/src/main/java/com/example/asset/SearchAllResourcesExample.java @@ -0,0 +1,63 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.asset; + +// [START asset_quickstart_search_all_resources] +import com.google.api.gax.rpc.ApiException; +import com.google.api.gax.rpc.InvalidArgumentException; +import com.google.cloud.asset.v1.AssetServiceClient; +import com.google.cloud.asset.v1.AssetServiceClient.SearchAllResourcesPagedResponse; +import com.google.cloud.asset.v1.SearchAllResourcesRequest; +import java.io.IOException; +import java.util.Arrays; + +public class SearchAllResourcesExample { + + // Searches for all the resources within the given scope. + public static void searchAllResources(String scope, String query) { + // TODO(developer): Replace these variables before running the sample. + String[] assetTypes = {}; + int pageSize = 0; + String pageToken = ""; + String orderBy = ""; + + SearchAllResourcesRequest request = + SearchAllResourcesRequest.newBuilder() + .setScope(scope) + .setQuery(query) + .addAllAssetTypes(Arrays.asList(assetTypes)) + .setPageSize(pageSize) + .setPageToken(pageToken) + .setOrderBy(orderBy) + .build(); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AssetServiceClient client = AssetServiceClient.create()) { + SearchAllResourcesPagedResponse response = client.searchAllResources(request); + System.out.println("Search completed successfully:\n" + response.getPage().getValues()); + } catch (IOException e) { + System.out.println(String.format("Failed to create client:%n%s", e.toString())); + } catch (InvalidArgumentException e) { + System.out.println(String.format("Invalid request:%n%s", e.toString())); + } catch (ApiException e) { + System.out.println(String.format("Error during SearchAllResources:%n%s", e.toString())); + } + } +} +// [END asset_quickstart_search_all_resources] diff --git a/asset/cloud-client/src/test/java/com/example/asset/Search.java b/asset/cloud-client/src/test/java/com/example/asset/Search.java new file mode 100644 index 00000000000..c15c08c4645 --- /dev/null +++ b/asset/cloud-client/src/test/java/com/example/asset/Search.java @@ -0,0 +1,84 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.asset; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.DatasetId; +import com.google.cloud.bigquery.DatasetInfo; +import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for search samples. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class Search { + + private static final String projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String datasetName = RemoteBigQueryHelper.generateDatasetName(); + private ByteArrayOutputStream bout; + private PrintStream out; + private BigQuery bigquery; + + @Before + public void setUp() { + bigquery = BigQueryOptions.getDefaultInstance().getService(); + if (bigquery.getDataset(datasetName) == null) { + bigquery.create(DatasetInfo.newBuilder(datasetName).build()); + } + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + bout.reset(); + DatasetId datasetId = DatasetId.of(bigquery.getOptions().getProjectId(), datasetName); + bigquery.delete(datasetId, DatasetDeleteOption.deleteContents()); + } + + @Test + public void testSearchAllResourcesExample() throws Exception { + // Wait 10 seconds to let dataset creation event go to CAI + Thread.sleep(10000); + String scope = "projects/" + projectId; + String query = "name:" + datasetName; + SearchAllResourcesExample.searchAllResources(scope, query); + String got = bout.toString(); + assertThat(got).contains(datasetName); + } + + @Test + public void testSearchAllIamPoliciesExample() throws Exception { + String scope = "projects/" + projectId; + String query = "policy:roles/owner"; + SearchAllIamPoliciesExample.searchAllIamPolicies(scope, query); + String got = bout.toString(); + assertThat(got).contains("roles/owner"); + } +} diff --git a/auth/pom.xml b/auth/pom.xml index bb7f19a5a32..3388ff03906 100644 --- a/auth/pom.xml +++ b/auth/pom.xml @@ -28,7 +28,7 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -45,7 +45,7 @@ limitations under the License. com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -60,13 +60,13 @@ limitations under the License. com.google.auth google-auth-library-appengine - 0.20.0 + 0.21.0 commons-io commons-io - 2.6 + 2.7 junit diff --git a/automl/beta/pom.xml b/automl/beta/pom.xml index 17a1fec5cb3..5dfad200c32 100644 --- a/automl/beta/pom.xml +++ b/automl/beta/pom.xml @@ -23,7 +23,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 diff --git a/automl/cloud-client/pom.xml b/automl/cloud-client/pom.xml index 45d0b31f7a2..a25baaadf51 100644 --- a/automl/cloud-client/pom.xml +++ b/automl/cloud-client/pom.xml @@ -23,7 +23,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -40,7 +40,7 @@ com.google.cloud libraries-bom - 5.2.0 + 7.0.2 pom import diff --git a/bigquery/bigquerystorage/README.md b/bigquery/bigquerystorage/README.md index 40ff936baaf..8b1e9671724 100644 --- a/bigquery/bigquerystorage/README.md +++ b/bigquery/bigquerystorage/README.md @@ -1,37 +1,4 @@ -# Getting Started with the BigQuery Storage API +# Getting Started with BigQuery and the Google Java API Client library - -Open in Cloud Shell - -Google's BigQuery Service features a Storage API for performing reads of BigQuery-managed data at -scale. This sample demonstrates using the API to read a sample table from the BigQuery public -datasets, projecting a subset of columns, and filtering that data on the server side. - -## Quickstart - -Install [Maven](http://maven.apache.org/). - -Build your project with: - - mvn clean package -DskipTests - -You can then run a given `ClassName` via: - - mvn exec:java -Dexec.mainClass=com.example.bigquery.ClassName \ - -Dexec.args="any arguments to the app" - -### Reading a Table with the BigQuery Storage API - - mvn exec:java -Dexec.mainClass=com.example.bigquerystorage.BigQueryStorage \ - -Dexec.args="project-id" - -## Testing - -To run the tests for this sample, first set the `GOOGLE_CLOUD_PROJECT` -environment variable. - - export GOOGLE_CLOUD_PROJECT=my-project - -Then run the tests with Maven. - - mvn clean verify +This sample has moved to a new location: +https://github.com/googleapis/java-bigquerystorage/tree/master/samples \ No newline at end of file diff --git a/bigquery/bigquerystorage/pom.xml b/bigquery/bigquerystorage/pom.xml deleted file mode 100644 index b727144e0d8..00000000000 --- a/bigquery/bigquerystorage/pom.xml +++ /dev/null @@ -1,74 +0,0 @@ - - - 4.0.0 - com.example.bigquerystorage - bigquery-storage-google-cloud-samples - jar - - - - com.google.cloud.samples - shared-configuration - 1.0.17 - - - - 1.8 - 1.8 - UTF-8 - - - -Dio.netty.tryReflectionSetAccessible=true - - - - - com.google.cloud - google-cloud-bigquerystorage - 0.125.0-beta - - - - org.apache.avro - avro - 1.9.2 - - - - org.apache.arrow - arrow-vector - 0.17.1 - - - - - junit - junit - 4.13 - test - - - com.google.truth - truth - 1.0.1 - test - - - diff --git a/bigquery/bigquerystorage/src/main/java/com/example/bigquerystorage/StorageArrowSample.java b/bigquery/bigquerystorage/src/main/java/com/example/bigquerystorage/StorageArrowSample.java deleted file mode 100644 index e497ae87844..00000000000 --- a/bigquery/bigquerystorage/src/main/java/com/example/bigquerystorage/StorageArrowSample.java +++ /dev/null @@ -1,187 +0,0 @@ -/* - * Copyright 2019 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.bigquerystorage; - -// [START bigquerystorage_arrow_quickstart] - -import com.google.api.gax.rpc.ServerStream; -import com.google.cloud.bigquery.storage.v1.ArrowRecordBatch; -import com.google.cloud.bigquery.storage.v1.ArrowSchema; -import com.google.cloud.bigquery.storage.v1.BigQueryReadClient; -import com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest; -import com.google.cloud.bigquery.storage.v1.DataFormat; -import com.google.cloud.bigquery.storage.v1.ReadRowsRequest; -import com.google.cloud.bigquery.storage.v1.ReadRowsResponse; -import com.google.cloud.bigquery.storage.v1.ReadSession; -import com.google.cloud.bigquery.storage.v1.ReadSession.TableModifiers; -import com.google.cloud.bigquery.storage.v1.ReadSession.TableReadOptions; -import com.google.common.base.Preconditions; -import com.google.protobuf.Timestamp; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import org.apache.arrow.memory.BufferAllocator; -import org.apache.arrow.memory.RootAllocator; -import org.apache.arrow.vector.FieldVector; -import org.apache.arrow.vector.VectorLoader; -import org.apache.arrow.vector.VectorSchemaRoot; -import org.apache.arrow.vector.ipc.ReadChannel; -import org.apache.arrow.vector.ipc.message.MessageSerializer; -import org.apache.arrow.vector.types.pojo.Field; -import org.apache.arrow.vector.types.pojo.Schema; -import org.apache.arrow.vector.util.ByteArrayReadableSeekableByteChannel; - -public class StorageArrowSample { - - /* - * SimpleRowReader handles deserialization of the Apache Arrow-encoded row batches transmitted - * from the storage API using a generic datum decoder. - */ - private static class SimpleRowReader implements AutoCloseable { - - BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE); - - // Decoder object will be reused to avoid re-allocation and too much garbage collection. - private final VectorSchemaRoot root; - private final VectorLoader loader; - - public SimpleRowReader(ArrowSchema arrowSchema) throws IOException { - Schema schema = - MessageSerializer.deserializeSchema( - new ReadChannel( - new ByteArrayReadableSeekableByteChannel( - arrowSchema.getSerializedSchema().toByteArray()))); - Preconditions.checkNotNull(schema); - List vectors = new ArrayList<>(); - for (Field field : schema.getFields()) { - vectors.add(field.createVector(allocator)); - } - root = new VectorSchemaRoot(vectors); - loader = new VectorLoader(root); - } - - /** - * Sample method for processing Arrow data which only validates decoding. - * - * @param batch object returned from the ReadRowsResponse. - */ - public void processRows(ArrowRecordBatch batch) throws IOException { - org.apache.arrow.vector.ipc.message.ArrowRecordBatch deserializedBatch = - MessageSerializer.deserializeRecordBatch( - new ReadChannel( - new ByteArrayReadableSeekableByteChannel( - batch.getSerializedRecordBatch().toByteArray())), - allocator); - - loader.load(deserializedBatch); - // Release buffers from batch (they are still held in the vectors in root). - deserializedBatch.close(); - System.out.println(root.contentToTSVString()); - // Release buffers from vectors in root. - root.clear(); - } - - @Override - public void close() { - root.close(); - allocator.close(); - } - } - - public static void main(String... args) throws Exception { - // Sets your Google Cloud Platform project ID. - // String projectId = "YOUR_PROJECT_ID"; - String projectId = args[0]; - Integer snapshotMillis = null; - if (args.length > 1) { - snapshotMillis = Integer.parseInt(args[1]); - } - - try (BigQueryReadClient client = BigQueryReadClient.create()) { - String parent = String.format("projects/%s", projectId); - - // This example uses baby name data from the public datasets. - String srcTable = - String.format( - "projects/%s/datasets/%s/tables/%s", - "bigquery-public-data", "usa_names", "usa_1910_current"); - - // We specify the columns to be projected by adding them to the selected fields, - // and set a simple filter to restrict which rows are transmitted. - TableReadOptions options = - TableReadOptions.newBuilder() - .addSelectedFields("name") - .addSelectedFields("number") - .addSelectedFields("state") - .setRowRestriction("state = \"WA\"") - .build(); - - // Start specifying the read session we want created. - ReadSession.Builder sessionBuilder = - ReadSession.newBuilder() - .setTable(srcTable) - // This API can also deliver data serialized in Apache Avro format. - // This example leverages Apache Arrow. - .setDataFormat(DataFormat.ARROW) - .setReadOptions(options); - - // Optionally specify the snapshot time. When unspecified, snapshot time is "now". - if (snapshotMillis != null) { - Timestamp t = - Timestamp.newBuilder() - .setSeconds(snapshotMillis / 1000) - .setNanos((int) ((snapshotMillis % 1000) * 1000000)) - .build(); - TableModifiers modifiers = TableModifiers.newBuilder().setSnapshotTime(t).build(); - sessionBuilder.setTableModifiers(modifiers); - } - - // Begin building the session creation request. - CreateReadSessionRequest.Builder builder = - CreateReadSessionRequest.newBuilder() - .setParent(parent) - .setReadSession(sessionBuilder) - .setMaxStreamCount(1); - - ReadSession session = client.createReadSession(builder.build()); - // Setup a simple reader and start a read session. - try (SimpleRowReader reader = new SimpleRowReader(session.getArrowSchema())) { - - // Assert that there are streams available in the session. An empty table may not have - // data available. If no sessions are available for an anonymous (cached) table, consider - // writing results of a query to a named table rather than consuming cached results - // directly. - Preconditions.checkState(session.getStreamsCount() > 0); - - // Use the first stream to perform reading. - String streamName = session.getStreams(0).getName(); - - ReadRowsRequest readRowsRequest = - ReadRowsRequest.newBuilder().setReadStream(streamName).build(); - - // Process each block of rows as they arrive and decode using our simple row reader. - ServerStream stream = client.readRowsCallable().call(readRowsRequest); - for (ReadRowsResponse response : stream) { - Preconditions.checkState(response.hasArrowRecordBatch()); - reader.processRows(response.getArrowRecordBatch()); - } - } - } - } -} - -// [END bigquerystorage_arrow_quickstart] diff --git a/bigquery/bigquerystorage/src/main/java/com/example/bigquerystorage/StorageSample.java b/bigquery/bigquerystorage/src/main/java/com/example/bigquerystorage/StorageSample.java deleted file mode 100644 index 45abc910208..00000000000 --- a/bigquery/bigquerystorage/src/main/java/com/example/bigquerystorage/StorageSample.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Copyright 2019 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.bigquerystorage; - -// [START bigquerystorage_quickstart] - -import com.google.api.gax.rpc.ServerStream; -import com.google.cloud.bigquery.storage.v1.AvroRows; -import com.google.cloud.bigquery.storage.v1.BigQueryReadClient; -import com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest; -import com.google.cloud.bigquery.storage.v1.DataFormat; -import com.google.cloud.bigquery.storage.v1.ReadRowsRequest; -import com.google.cloud.bigquery.storage.v1.ReadRowsResponse; -import com.google.cloud.bigquery.storage.v1.ReadSession; -import com.google.cloud.bigquery.storage.v1.ReadSession.TableModifiers; -import com.google.cloud.bigquery.storage.v1.ReadSession.TableReadOptions; -import com.google.common.base.Preconditions; -import com.google.protobuf.Timestamp; -import java.io.IOException; -import org.apache.avro.Schema; -import org.apache.avro.generic.GenericDatumReader; -import org.apache.avro.generic.GenericRecord; -import org.apache.avro.io.BinaryDecoder; -import org.apache.avro.io.DatumReader; -import org.apache.avro.io.DecoderFactory; - -public class StorageSample { - - /* - * SimpleRowReader handles deserialization of the Avro-encoded row blocks transmitted - * from the storage API using a generic datum decoder. - */ - private static class SimpleRowReader { - - private final DatumReader datumReader; - - // Decoder object will be reused to avoid re-allocation and too much garbage collection. - private BinaryDecoder decoder = null; - - // GenericRecord object will be reused. - private GenericRecord row = null; - - public SimpleRowReader(Schema schema) { - Preconditions.checkNotNull(schema); - datumReader = new GenericDatumReader<>(schema); - } - - /** - * Sample method for processing AVRO rows which only validates decoding. - * - * @param avroRows object returned from the ReadRowsResponse. - */ - public void processRows(AvroRows avroRows) throws IOException { - decoder = - DecoderFactory.get() - .binaryDecoder(avroRows.getSerializedBinaryRows().toByteArray(), decoder); - - while (!decoder.isEnd()) { - // Reusing object row - row = datumReader.read(row, decoder); - System.out.println(row.toString()); - } - } - } - - public static void main(String... args) throws Exception { - // Sets your Google Cloud Platform project ID. - // String projectId = "YOUR_PROJECT_ID"; - String projectId = args[0]; - Integer snapshotMillis = null; - if (args.length > 1) { - snapshotMillis = Integer.parseInt(args[1]); - } - - try (BigQueryReadClient client = BigQueryReadClient.create()) { - String parent = String.format("projects/%s", projectId); - - // This example uses baby name data from the public datasets. - String srcTable = - String.format( - "projects/%s/datasets/%s/tables/%s", - "bigquery-public-data", "usa_names", "usa_1910_current"); - - // We specify the columns to be projected by adding them to the selected fields, - // and set a simple filter to restrict which rows are transmitted. - TableReadOptions options = - TableReadOptions.newBuilder() - .addSelectedFields("name") - .addSelectedFields("number") - .addSelectedFields("state") - .setRowRestriction("state = \"WA\"") - .build(); - - // Start specifying the read session we want created. - ReadSession.Builder sessionBuilder = - ReadSession.newBuilder() - .setTable(srcTable) - // This API can also deliver data serialized in Apache Avro format. - // This example leverages Apache Avro. - .setDataFormat(DataFormat.AVRO) - .setReadOptions(options); - - // Optionally specify the snapshot time. When unspecified, snapshot time is "now". - if (snapshotMillis != null) { - Timestamp t = - Timestamp.newBuilder() - .setSeconds(snapshotMillis / 1000) - .setNanos((int) ((snapshotMillis % 1000) * 1000000)) - .build(); - TableModifiers modifiers = TableModifiers.newBuilder().setSnapshotTime(t).build(); - sessionBuilder.setTableModifiers(modifiers); - } - - // Begin building the session creation request. - CreateReadSessionRequest.Builder builder = - CreateReadSessionRequest.newBuilder() - .setParent(parent) - .setReadSession(sessionBuilder) - .setMaxStreamCount(1); - - // Request the session creation. - ReadSession session = client.createReadSession(builder.build()); - - SimpleRowReader reader = - new SimpleRowReader(new Schema.Parser().parse(session.getAvroSchema().getSchema())); - - // Assert that there are streams available in the session. An empty table may not have - // data available. If no sessions are available for an anonymous (cached) table, consider - // writing results of a query to a named table rather than consuming cached results directly. - Preconditions.checkState(session.getStreamsCount() > 0); - - // Use the first stream to perform reading. - String streamName = session.getStreams(0).getName(); - - ReadRowsRequest readRowsRequest = - ReadRowsRequest.newBuilder().setReadStream(streamName).build(); - - // Process each block of rows as they arrive and decode using our simple row reader. - ServerStream stream = client.readRowsCallable().call(readRowsRequest); - for (ReadRowsResponse response : stream) { - Preconditions.checkState(response.hasAvroRows()); - reader.processRows(response.getAvroRows()); - } - } - } -} - -// [END bigquerystorage_quickstart] diff --git a/bigquery/datatransfer/README.md b/bigquery/datatransfer/README.md new file mode 100644 index 00000000000..c76b7ab1786 --- /dev/null +++ b/bigquery/datatransfer/README.md @@ -0,0 +1,4 @@ +# Getting Started with BigQuery and the Google Java API Client library + +This sample has moved to a new location: +https://github.com/googleapis/java-bigquerydatatransfer/tree/master/samples \ No newline at end of file diff --git a/bigquery/datatransfer/cloud-client/README.md b/bigquery/datatransfer/cloud-client/README.md deleted file mode 100644 index 940ceba793e..00000000000 --- a/bigquery/datatransfer/cloud-client/README.md +++ /dev/null @@ -1,35 +0,0 @@ -# Getting Started with BigQuery Data Transfer API - - -Open in Cloud Shell - -[BigQuery Data Transfer Service][BigQuery Data Transfer] features an API that -allows developers to create transfer jobs from data sources to BigQuery. -These sample Java applications demonstrate how to access the BigQuery Data -Transfer API using the [Google Cloud Client Library for -Java][google-cloud-java]. - -[BigQuery Data Transfer]: https://cloud.google.com/bigquery/docs/transfer-service-overview -[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java - -## Quickstart - -Install [Maven](http://maven.apache.org/). - -[Authenticate using a service account](https://cloud.google.com/docs/authentication/getting-started). -Create a service account, download a JSON key file, and set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable. - -Build your project with: - - mvn clean package -DskipTests - -You can then run a given `ClassName` via: - - mvn exec:java -Dexec.mainClass=com.example.bigquerydatatransfer.ClassName \ - -DpropertyName=propertyValue \ - -Dexec.args="any arguments to the app" - -### Listing available data sources - - mvn exec:java -Dexec.mainClass=com.example.bigquerydatatransfer.QuickstartSample \ - -Dexec.args='YOUR_PROJECT_ID' diff --git a/bigquery/datatransfer/cloud-client/pom.xml b/bigquery/datatransfer/cloud-client/pom.xml deleted file mode 100644 index 5dcc35bdae6..00000000000 --- a/bigquery/datatransfer/cloud-client/pom.xml +++ /dev/null @@ -1,76 +0,0 @@ - - - 4.0.0 - com.example.bigquerydatatransfer - bigquery-datatransfer-google-cloud-samples - jar - - - - com.google.cloud.samples - shared-configuration - 1.0.17 - - - - 1.8 - 1.8 - UTF-8 - - - - - - - - com.google.cloud - libraries-bom - 4.4.1 - pom - import - - - - - - - com.google.cloud - google-cloud-bigquerydatatransfer - - - - - - junit - junit - 4.13 - test - - - com.google.truth - truth - 1.0.1 - test - - - - - diff --git a/bigquery/datatransfer/cloud-client/src/main/java/com/example/bigquerydatatransfer/QuickstartSample.java b/bigquery/datatransfer/cloud-client/src/main/java/com/example/bigquerydatatransfer/QuickstartSample.java deleted file mode 100644 index 943b978edc9..00000000000 --- a/bigquery/datatransfer/cloud-client/src/main/java/com/example/bigquerydatatransfer/QuickstartSample.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2018 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.bigquerydatatransfer; - -// [START bigquerydatatransfer_quickstart] -// Imports the Google Cloud client library - -import com.google.cloud.bigquery.datatransfer.v1.DataSource; -import com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient; -import com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient.ListDataSourcesPagedResponse; -import com.google.cloud.bigquery.datatransfer.v1.ListDataSourcesRequest; - -public class QuickstartSample { - /** - * List available data sources for the BigQuery Data Transfer service. - */ - public static void main(String... args) throws Exception { - // Sets your Google Cloud Platform project ID. - // String projectId = "YOUR_PROJECT_ID"; - String projectId = args[0]; - - // Instantiate a client. If you don't specify credentials when constructing a client, the - // client library will look for credentials in the environment, such as the - // GOOGLE_APPLICATION_CREDENTIALS environment variable. - try (DataTransferServiceClient client = DataTransferServiceClient.create()) { - // Request the list of available data sources. - String parent = String.format("projects/%s", projectId); - ListDataSourcesRequest request = - ListDataSourcesRequest.newBuilder() - .setParent(parent) - .build(); - ListDataSourcesPagedResponse response = client.listDataSources(request); - - // Print the results. - System.out.println("Supported Data Sources:"); - for (DataSource dataSource : response.iterateAll()) { - System.out.println(dataSource.getDisplayName()); - System.out.printf("\tID: %s%n", dataSource.getDataSourceId()); - System.out.printf("\tFull path: %s%n", dataSource.getName()); - System.out.printf("\tDescription: %s%n", dataSource.getDescription()); - } - } - } -} -// [END bigquerydatatransfer_quickstart] diff --git a/bigquery/rest/pom.xml b/bigquery/rest/pom.xml index 914f3f668e1..3e5d9563cfd 100644 --- a/bigquery/rest/pom.xml +++ b/bigquery/rest/pom.xml @@ -28,7 +28,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -66,15 +66,9 @@
- com.google.oauth-client - google-oauth-client - 1.30.6 - - - com.google.guava - guava-jdk5 - - + com.google.auth + google-auth-library-oauth2-http + 0.21.0 diff --git a/bigquery/rest/src/main/java/com/example/bigquery/LabelsSample.java b/bigquery/rest/src/main/java/com/example/bigquery/LabelsSample.java index 4545bfc3db4..e1a41aa3503 100644 --- a/bigquery/rest/src/main/java/com/example/bigquery/LabelsSample.java +++ b/bigquery/rest/src/main/java/com/example/bigquery/LabelsSample.java @@ -16,7 +16,6 @@ package com.example.bigquery; -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.GenericUrl; import com.google.api.client.http.HttpContent; import com.google.api.client.http.HttpHeaders; @@ -29,6 +28,8 @@ import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.client.util.Key; +import com.google.auth.oauth2.AccessToken; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Arrays; import java.util.HashMap; @@ -60,21 +61,20 @@ public Dataset addLabel(String key, String value) { /** * Add or modify a label on a dataset. * - *

See the BigQuery + * See the BigQuery * documentation. */ public static void labelDataset( String projectId, String datasetId, String labelKey, String labelValue) throws IOException { // Authenticate requests using Google Application Default credentials. - GoogleCredential credential = GoogleCredential.getApplicationDefault(); + GoogleCredentials credential = GoogleCredentials.getApplicationDefault(); credential = credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/bigquery")); // Get a new access token. // Note that access tokens have an expiration. You can reuse a token rather than requesting a // new one if it is not yet expired. - credential.refreshToken(); - String accessToken = credential.getAccessToken(); + AccessToken accessToken = credential.refreshAccessToken(); // Set the content of the request. Dataset dataset = new Dataset(); @@ -85,7 +85,8 @@ public static void labelDataset( String urlFormat = "https://www.googleapis.com/bigquery/v2/projects/%s/datasets/%s" + "?fields=labels&access_token=%s"; - GenericUrl url = new GenericUrl(String.format(urlFormat, projectId, datasetId, accessToken)); + GenericUrl url = + new GenericUrl(String.format(urlFormat, projectId, datasetId, accessToken.getTokenValue())); HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(); HttpRequest request = requestFactory.buildPostRequest(url, content); request.setParser(JSON_FACTORY.createJsonObjectParser()); @@ -127,26 +128,21 @@ public Table addLabel(String key, String value) { /** * Add or modify a label on a table. * - *

See the BigQuery + * See the BigQuery * documentation. */ public static void labelTable( - String projectId, - String datasetId, - String tableId, - String labelKey, - String labelValue) + String projectId, String datasetId, String tableId, String labelKey, String labelValue) throws IOException { // Authenticate requests using Google Application Default credentials. - GoogleCredential credential = GoogleCredential.getApplicationDefault(); + GoogleCredentials credential = GoogleCredentials.getApplicationDefault(); credential = credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/bigquery")); // Get a new access token. // Note that access tokens have an expiration. You can reuse a token rather than requesting a // new one if it is not yet expired. - credential.refreshToken(); - String accessToken = credential.getAccessToken(); + AccessToken accessToken = credential.refreshAccessToken(); // Set the content of the request. Table table = new Table(); @@ -158,7 +154,8 @@ public static void labelTable( "https://www.googleapis.com/bigquery/v2/projects/%s/datasets/%s/tables/%s" + "?fields=labels&access_token=%s"; GenericUrl url = - new GenericUrl(String.format(urlFormat, projectId, datasetId, tableId, accessToken)); + new GenericUrl( + String.format(urlFormat, projectId, datasetId, tableId, accessToken.getTokenValue())); HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(); HttpRequest request = requestFactory.buildPostRequest(url, content); request.setParser(JSON_FACTORY.createJsonObjectParser()); diff --git a/bigtable/beam/helloworld/pom.xml b/bigtable/beam/helloworld/pom.xml index 7f68153a1c8..e0c731a886a 100644 --- a/bigtable/beam/helloworld/pom.xml +++ b/bigtable/beam/helloworld/pom.xml @@ -23,11 +23,21 @@ helloworld 1.0-SNAPSHOT + + + com.google.cloud.samples + shared-configuration + 1.0.18 + + UTF-8 1.8 1.8 - 2.20.0 + 2.22.0 diff --git a/bigtable/beam/keyviz-art/pom.xml b/bigtable/beam/keyviz-art/pom.xml index c134f22100d..8d6de498200 100644 --- a/bigtable/beam/keyviz-art/pom.xml +++ b/bigtable/beam/keyviz-art/pom.xml @@ -23,12 +23,21 @@ keyviz-art 1.0-SNAPSHOT + + + com.google.cloud.samples + shared-configuration + 1.0.18 + UTF-8 1.8 1.8 - 2.20.0 + 2.22.0 diff --git a/bigtable/beam/keyviz-art/src/test/java/KeyVizArtTest.java b/bigtable/beam/keyviz-art/src/test/java/KeyVizArtTest.java index 901c6b4c5a8..304701f7cf4 100644 --- a/bigtable/beam/keyviz-art/src/test/java/KeyVizArtTest.java +++ b/bigtable/beam/keyviz-art/src/test/java/KeyVizArtTest.java @@ -18,8 +18,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import keyviz.ReadData.ReadDataOptions; -import keyviz.ReadData.ReadFromTableFn; import com.google.cloud.bigtable.beam.CloudBigtableTableConfiguration; import com.google.cloud.bigtable.hbase.BigtableConfiguration; import java.io.ByteArrayOutputStream; @@ -27,6 +25,8 @@ import java.io.PrintStream; import java.util.UUID; import keyviz.LoadData; +import keyviz.ReadData.ReadDataOptions; +import keyviz.ReadData.ReadFromTableFn; import org.apache.beam.sdk.Pipeline; import org.apache.beam.sdk.options.PipelineOptionsFactory; import org.apache.beam.sdk.transforms.Create; @@ -143,7 +143,7 @@ public void testWriteAndRead() { .build(); // Initiates a new pipeline every second - p.apply(Create.of(1l)) + p.apply(Create.of(1L)) .apply(ParDo.of(new ReadFromTableFn(bigtableTableConfig, options))); p.run().waitUntilFinish(); @@ -160,7 +160,7 @@ public void testWriteAndRead() { p = Pipeline.create(options); // Initiates a new pipeline every second - p.apply(Create.of(1l)) + p.apply(Create.of(1L)) .apply(ParDo.of(new ReadFromTableFn(bigtableTableConfig, options))); p.run().waitUntilFinish(); diff --git a/bigtable/cassandra-migration-codelab/pom.xml b/bigtable/cassandra-migration-codelab/pom.xml index 46dea9d6254..b42df085c1f 100644 --- a/bigtable/cassandra-migration-codelab/pom.xml +++ b/bigtable/cassandra-migration-codelab/pom.xml @@ -23,6 +23,16 @@ bigtable 1.0-SNAPSHOT + + + com.google.cloud.samples + shared-configuration + 1.0.18 + + UTF-8 1.8 @@ -30,17 +40,18 @@ + + + com.google.cloud + google-cloud-bigtable + 1.13.0 + + junit junit 4.13 test - - - com.google.cloud - google-cloud-bigtable - 1.11.0 - \ No newline at end of file diff --git a/bigtable/cassandra-migration-codelab/src/main/java/CassandraMigrationCodelab.java b/bigtable/cassandra-migration-codelab/src/main/java/CassandraMigrationCodelab.java index 321fa4e16c3..597725a8c8e 100644 --- a/bigtable/cassandra-migration-codelab/src/main/java/CassandraMigrationCodelab.java +++ b/bigtable/cassandra-migration-codelab/src/main/java/CassandraMigrationCodelab.java @@ -26,9 +26,10 @@ import com.google.protobuf.ByteString; public class CassandraMigrationCodelab { + private BigtableDataClient dataClient; private String tableId; - private final String COLUMN_FAMILY_NAME = "stats_summary"; + private static final String COLUMN_FAMILY_NAME = "stats_summary"; public CassandraMigrationCodelab(String projectId, String instanceId, String tableId) { this.tableId = tableId; @@ -59,7 +60,7 @@ public void write() { long timestamp = (long) 1556712000 * 1000; // Timestamp of June 1, 2019 12:00 String rowKey = "phone#4c410523#20190501"; - ByteString one = ByteString.copyFrom(new byte[] {0, 0, 0, 0, 0, 0, 0, 1}); + ByteString one = ByteString.copyFrom(new byte[]{0, 0, 0, 0, 0, 0, 0, 1}); RowMutation rowMutation = RowMutation.create(tableId, rowKey) @@ -122,7 +123,7 @@ public void update2() { String rowKey = "phone#4c410523#20190501"; - ByteString zero = ByteString.copyFrom(new byte[] {0, 0, 0, 0, 0, 0, 0, 0}); + ByteString zero = ByteString.copyFrom(new byte[]{0, 0, 0, 0, 0, 0, 0, 0}); RowMutation rowMutation = RowMutation.create(tableId, rowKey) diff --git a/bigtable/cassandra-migration-codelab/src/test/java/CassandraMigrationCodelabTest.java b/bigtable/cassandra-migration-codelab/src/test/java/CassandraMigrationCodelabTest.java index e37d2d8c6d3..9679698e115 100644 --- a/bigtable/cassandra-migration-codelab/src/test/java/CassandraMigrationCodelabTest.java +++ b/bigtable/cassandra-migration-codelab/src/test/java/CassandraMigrationCodelabTest.java @@ -27,8 +27,9 @@ import org.junit.Test; public class CassandraMigrationCodelabTest { + private static final String INSTANCE_ENV = "BIGTABLE_TESTING_INSTANCE"; - private final String COLUMN_FAMILY_NAME = "stats_summary"; + private static final String COLUMN_FAMILY_NAME = "stats_summary"; private static final String TABLE_PREFIX = "cass-"; private static final String TABLE_ID = diff --git a/bigtable/hbase/snippets/pom.xml b/bigtable/hbase/snippets/pom.xml index d7b05bbcc73..4f3c8caadee 100644 --- a/bigtable/hbase/snippets/pom.xml +++ b/bigtable/hbase/snippets/pom.xml @@ -24,6 +24,16 @@ 1.0-SNAPSHOT docs-samples + + + com.google.cloud.samples + shared-configuration + 1.0.18 + + UTF-8 1.8 @@ -31,28 +41,31 @@ + - junit - junit - 4.13 - test + com.google.cloud + google-cloud-bigtable + 1.13.0 + com.google.cloud.bigtable bigtable-hbase-1.x 1.14.1 + + + + junit + junit + 4.13 + test + com.google.truth truth 1.0.1 test - - - com.google.cloud - google-cloud-bigtable - 1.11.0 - diff --git a/bigtable/hbase/snippets/src/main/java/com/example/bigtable/Filters.java b/bigtable/hbase/snippets/src/main/java/com/example/bigtable/Filters.java index 2f0c78475e9..cdd24cf7333 100644 --- a/bigtable/hbase/snippets/src/main/java/com/example/bigtable/Filters.java +++ b/bigtable/hbase/snippets/src/main/java/com/example/bigtable/Filters.java @@ -16,6 +16,8 @@ package com.example.bigtable; +// [START bigtable_filters_print] + import static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS; import com.google.api.gax.rpc.ServerStream; @@ -52,37 +54,10 @@ import org.apache.hadoop.hbase.filter.ValueFilter; import org.apache.hadoop.hbase.util.Bytes; -// [START bigtable_filters_limit_row_sample] -// [START bigtable_filters_limit_row_regex] -// [START bigtable_filters_limit_cells_per_col] -// [START bigtable_filters_limit_cells_per_row] -// [START bigtable_filters_limit_cells_per_row_offset] -// [START bigtable_filters_limit_col_family_regex] -// [START bigtable_filters_limit_col_qualifier_regex] -// [START bigtable_filters_limit_col_range] -// [START bigtable_filters_limit_value_range] -// [START bigtable_filters_limit_value_regex] -// [START bigtable_filters_limit_timestamp_range] -// [START bigtable_filters_limit_block_all] -// [START bigtable_filters_composing_chain] -// [START bigtable_filters_composing_interleave] - public class Filters { - // [END bigtable_filters_limit_row_sample] - // [END bigtable_filters_limit_row_regex] - // [END bigtable_filters_limit_cells_per_col] - // [END bigtable_filters_limit_cells_per_row] - // [END bigtable_filters_limit_cells_per_row_offset] - // [END bigtable_filters_limit_col_family_regex] - // [END bigtable_filters_limit_col_qualifier_regex] - // [END bigtable_filters_limit_col_range] - // [END bigtable_filters_limit_value_range] - // [END bigtable_filters_limit_value_regex] - // [END bigtable_filters_limit_timestamp_range] - // [END bigtable_filters_limit_block_all] - // [END bigtable_filters_composing_chain] - // [END bigtable_filters_composing_interleave] + // Write your code here. + // [START_EXCLUDE] // [START bigtable_filters_limit_row_sample] public static void filterLimitRowSample() { // TODO(developer): Replace these variables before running the sample. @@ -362,21 +337,8 @@ public static void filterComposingInterleave( readWithFilter(projectId, instanceId, tableId, scan); } // [END bigtable_filters_composing_interleave] + // [END_EXCLUDE] - // [START bigtable_filters_limit_row_sample] - // [START bigtable_filters_limit_row_regex] - // [START bigtable_filters_limit_cells_per_col] - // [START bigtable_filters_limit_cells_per_row] - // [START bigtable_filters_limit_cells_per_row_offset] - // [START bigtable_filters_limit_col_family_regex] - // [START bigtable_filters_limit_col_qualifier_regex] - // [START bigtable_filters_limit_col_range] - // [START bigtable_filters_limit_value_range] - // [START bigtable_filters_limit_value_regex] - // [START bigtable_filters_limit_timestamp_range] - // [START bigtable_filters_limit_block_all] - // [START bigtable_filters_composing_chain] - // [START bigtable_filters_composing_interleave] public static void readWithFilter( String projectId, String instanceId, String tableId, Scan scan) { // Initialize client that will be used to send requests. This client only needs to be created @@ -414,17 +376,4 @@ private static void printRow(Result row) { System.out.println(); } } -// [END bigtable_filters_limit_row_sample] -// [END bigtable_filters_limit_row_regex] -// [END bigtable_filters_limit_cells_per_col] -// [END bigtable_filters_limit_cells_per_row] -// [END bigtable_filters_limit_cells_per_row_offset] -// [END bigtable_filters_limit_col_family_regex] -// [END bigtable_filters_limit_col_qualifier_regex] -// [END bigtable_filters_limit_col_range] -// [END bigtable_filters_limit_value_range] -// [END bigtable_filters_limit_value_regex] -// [END bigtable_filters_limit_timestamp_range] -// [END bigtable_filters_limit_block_all] -// [END bigtable_filters_composing_chain] -// [END bigtable_filters_composing_interleave] +// [END bigtable_filters_print] diff --git a/bigtable/hbase/snippets/src/main/java/com/example/bigtable/Reads.java b/bigtable/hbase/snippets/src/main/java/com/example/bigtable/Reads.java index 1d2198d1417..90167b312f0 100644 --- a/bigtable/hbase/snippets/src/main/java/com/example/bigtable/Reads.java +++ b/bigtable/hbase/snippets/src/main/java/com/example/bigtable/Reads.java @@ -16,13 +16,7 @@ package com.example.bigtable; -// [START bigtable_reads_row] -// [START bigtable_reads_row_partial] -// [START bigtable_reads_rows] -// [START bigtable_reads_row_range] -// [START bigtable_reads_row_ranges] -// [START bigtable_reads_prefix] -// [START bigtable_reads_filter] +// [START bigtable_reads_print] import com.google.cloud.bigtable.hbase.BigtableConfiguration; import java.io.IOException; @@ -44,15 +38,11 @@ import org.apache.hadoop.hbase.filter.ValueFilter; import org.apache.hadoop.hbase.util.Bytes; + public class Reads { - // [END bigtable_reads_row] - // [END bigtable_reads_row_partial] - // [END bigtable_reads_rows] - // [END bigtable_reads_row_range] - // [END bigtable_reads_row_ranges] - // [END bigtable_reads_prefix] - // [END bigtable_reads_filter] + // Write your code here. + // [START_EXCLUDE] // [START bigtable_reads_row] public static void readRow() { // TODO(developer): Replace these variables before running the sample. @@ -79,6 +69,8 @@ public static void readRow(String projectId, String instanceId, String tableId) "Unable to initialize service client, as a network error occurred: \n" + e.toString()); } } + // [END bigtable_reads_row] + // [START bigtable_reads_row_partial] public static void readRowPartial() { // TODO(developer): Replace these variables before running the sample. @@ -276,14 +268,8 @@ public static void readFilter(String projectId, String instanceId, String tableI } } // [END bigtable_reads_filter] + // [END_EXCLUDE] - // [START bigtable_reads_row] - // [START bigtable_reads_row_partial] - // [START bigtable_reads_rows] - // [START bigtable_reads_row_range] - // [START bigtable_reads_row_ranges] - // [START bigtable_reads_prefix] - // [START bigtable_reads_filter] private static void printRow(Result row) { System.out.printf("Reading data for %s%n", Bytes.toString(row.rawCells()[0].getRowArray())); String colFamily = ""; @@ -302,10 +288,4 @@ private static void printRow(Result row) { System.out.println(); } } -// [END bigtable_reads_row] -// [END bigtable_reads_row_partial] -// [END bigtable_reads_rows] -// [END bigtable_reads_row_range] -// [END bigtable_reads_row_ranges] -// [END bigtable_reads_prefix] -// [END bigtable_reads_filter] +// [END bigtable_reads_print] diff --git a/bigtable/hbase/snippets/src/main/java/com/example/bigtable/WriteBatch.java b/bigtable/hbase/snippets/src/main/java/com/example/bigtable/WriteBatch.java index 5d9a3ff0792..b4523e0400c 100644 --- a/bigtable/hbase/snippets/src/main/java/com/example/bigtable/WriteBatch.java +++ b/bigtable/hbase/snippets/src/main/java/com/example/bigtable/WriteBatch.java @@ -28,6 +28,7 @@ import org.apache.hadoop.hbase.util.Bytes; public class WriteBatch { + private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("stats_summary"); public static void writeBatch(String projectId, String instanceId, String tableId) { @@ -36,9 +37,9 @@ public static void writeBatch(String projectId, String instanceId, String tableI // String tableId = "mobile-time-series"; try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) { - Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(tableId))); + final Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(tableId))); long timestamp = System.currentTimeMillis(); - byte[] one = new byte[] {0, 0, 0, 0, 0, 0, 0, 1}; + byte[] one = new byte[]{0, 0, 0, 0, 0, 0, 0, 1}; List puts = new ArrayList(); puts.add(new Put(Bytes.toBytes("tablet#a0b81f74#20190501"))); diff --git a/bigtable/hbase/snippets/src/main/java/com/example/bigtable/WriteConditionally.java b/bigtable/hbase/snippets/src/main/java/com/example/bigtable/WriteConditionally.java index 28869fe639a..e338ffbaed9 100644 --- a/bigtable/hbase/snippets/src/main/java/com/example/bigtable/WriteConditionally.java +++ b/bigtable/hbase/snippets/src/main/java/com/example/bigtable/WriteConditionally.java @@ -28,6 +28,7 @@ import org.apache.hadoop.hbase.util.Bytes; public class WriteConditionally { + private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("stats_summary"); public static void writeConditionally(String projectId, String instanceId, String tableId) { diff --git a/bigtable/hbase/snippets/src/main/java/com/example/bigtable/WriteIncrement.java b/bigtable/hbase/snippets/src/main/java/com/example/bigtable/WriteIncrement.java index 55d98bf3bf7..17b9c9d1c83 100644 --- a/bigtable/hbase/snippets/src/main/java/com/example/bigtable/WriteIncrement.java +++ b/bigtable/hbase/snippets/src/main/java/com/example/bigtable/WriteIncrement.java @@ -25,6 +25,7 @@ import org.apache.hadoop.hbase.util.Bytes; public class WriteIncrement { + private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("stats_summary"); public static void writeIncrement(String projectId, String instanceId, String tableId) { diff --git a/bigtable/hbase/snippets/src/main/java/com/example/bigtable/WriteSimple.java b/bigtable/hbase/snippets/src/main/java/com/example/bigtable/WriteSimple.java index 60bdc65a4cd..855fba9423b 100644 --- a/bigtable/hbase/snippets/src/main/java/com/example/bigtable/WriteSimple.java +++ b/bigtable/hbase/snippets/src/main/java/com/example/bigtable/WriteSimple.java @@ -26,6 +26,7 @@ import org.apache.hadoop.hbase.util.Bytes; public class WriteSimple { + private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("stats_summary"); public static void writeSimple(String projectId, String instanceId, String tableId) { @@ -34,9 +35,9 @@ public static void writeSimple(String projectId, String instanceId, String table // String tableId = "mobile-time-series"; try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) { - Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(tableId))); + final Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(tableId))); long timestamp = System.currentTimeMillis(); - byte[] one = new byte[] {0, 0, 0, 0, 0, 0, 0, 1}; + byte[] one = new byte[]{0, 0, 0, 0, 0, 0, 0, 1}; String rowKey = "phone#4c410523#20190501"; Put put = new Put(Bytes.toBytes(rowKey)); diff --git a/bigtable/pom.xml b/bigtable/pom.xml index bb4756db769..8c392152d91 100644 --- a/bigtable/pom.xml +++ b/bigtable/pom.xml @@ -30,7 +30,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 bigtable @@ -42,17 +42,18 @@ + + + com.google.cloud + google-cloud-bigtable + 1.13.0 + + junit junit 4.13 test - - - com.google.cloud - google-cloud-bigtable - 1.11.0 - diff --git a/bigtable/snippets/pom.xml b/bigtable/snippets/pom.xml index bb0347f80a4..3ff26903c72 100644 --- a/bigtable/snippets/pom.xml +++ b/bigtable/snippets/pom.xml @@ -24,6 +24,16 @@ 1.0-SNAPSHOT docs-samples + + + com.google.cloud.samples + shared-configuration + 1.0.18 + + UTF-8 1.8 @@ -31,6 +41,14 @@ + + + com.google.cloud + google-cloud-bigtable + 1.13.0 + + + junit junit @@ -43,11 +61,5 @@ 1.0.1 test - - - com.google.cloud - google-cloud-bigtable - 1.11.0 - diff --git a/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java b/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java index bb8d8cbd3fe..cb6e6175d8f 100644 --- a/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java +++ b/bigtable/snippets/src/main/java/com/example/bigtable/Filters.java @@ -16,25 +16,10 @@ package com.example.bigtable; +// [START bigtable_filters_print] + import static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS; -// [START bigtable_filters_limit_row_sample] -// [START bigtable_filters_limit_row_regex] -// [START bigtable_filters_limit_cells_per_col] -// [START bigtable_filters_limit_cells_per_row] -// [START bigtable_filters_limit_cells_per_row_offset] -// [START bigtable_filters_limit_col_family_regex] -// [START bigtable_filters_limit_col_qualifier_regex] -// [START bigtable_filters_limit_col_range] -// [START bigtable_filters_limit_value_range] -// [START bigtable_filters_limit_value_regex] -// [START bigtable_filters_limit_timestamp_range] -// [START bigtable_filters_limit_block_all] -// [START bigtable_filters_limit_pass_all] -// [START bigtable_filters_modify_strip_value] -// [START bigtable_filters_modify_apply_label] -// [START bigtable_filters_composing_chain] -// [START bigtable_filters_composing_interleave] -// [START bigtable_filters_composing_condition] + import com.google.api.gax.rpc.ServerStream; import com.google.cloud.bigtable.data.v2.BigtableDataClient; import com.google.cloud.bigtable.data.v2.models.Filters.Filter; @@ -45,26 +30,11 @@ import java.time.Instant; import java.time.temporal.ChronoUnit; + public class Filters { - // [END bigtable_filters_limit_row_sample] - // [END bigtable_filters_limit_row_regex] - // [END bigtable_filters_limit_cells_per_col] - // [END bigtable_filters_limit_cells_per_row] - // [END bigtable_filters_limit_cells_per_row_offset] - // [END bigtable_filters_limit_col_family_regex] - // [END bigtable_filters_limit_col_qualifier_regex] - // [END bigtable_filters_limit_col_range] - // [END bigtable_filters_limit_value_range] - // [END bigtable_filters_limit_value_regex] - // [END bigtable_filters_limit_timestamp_range] - // [END bigtable_filters_limit_block_all] - // [END bigtable_filters_limit_pass_all] - // [END bigtable_filters_modify_strip_value] - // [END bigtable_filters_modify_apply_label] - // [END bigtable_filters_composing_chain] - // [END bigtable_filters_composing_interleave] - // [END bigtable_filters_composing_condition] + // Write your code here. + // [START_EXCLUDE] // [START bigtable_filters_limit_row_sample] public static void filterLimitRowSample() { // TODO(developer): Replace these variables before running the sample. @@ -383,25 +353,8 @@ public static void filterComposingCondition(String projectId, String instanceId, readFilter(projectId, instanceId, tableId, filter); } // [END bigtable_filters_composing_condition] + // [END_EXCLUDE] - // [START bigtable_filters_limit_row_sample] - // [START bigtable_filters_limit_row_regex] - // [START bigtable_filters_limit_cells_per_col] - // [START bigtable_filters_limit_cells_per_row] - // [START bigtable_filters_limit_cells_per_row_offset] - // [START bigtable_filters_limit_col_family_regex] - // [START bigtable_filters_limit_col_qualifier_regex] - // [START bigtable_filters_limit_col_range] - // [START bigtable_filters_limit_value_range] - // [START bigtable_filters_limit_value_regex] - // [START bigtable_filters_limit_timestamp_range] - // [START bigtable_filters_limit_block_all] - // [START bigtable_filters_limit_pass_all] - // [START bigtable_filters_modify_strip_value] - // [START bigtable_filters_modify_apply_label] - // [START bigtable_filters_composing_chain] - // [START bigtable_filters_composing_interleave] - // [START bigtable_filters_composing_condition] private static void readFilter( String projectId, String instanceId, String tableId, Filter filter) { // Initialize client that will be used to send requests. This client only needs to be created @@ -439,21 +392,4 @@ private static void printRow(Row row) { System.out.println(); } } -// [END bigtable_filters_limit_row_sample] -// [END bigtable_filters_limit_row_regex] -// [END bigtable_filters_limit_cells_per_col] -// [END bigtable_filters_limit_cells_per_row] -// [END bigtable_filters_limit_cells_per_row_offset] -// [END bigtable_filters_limit_col_family_regex] -// [END bigtable_filters_limit_col_qualifier_regex] -// [END bigtable_filters_limit_col_range] -// [END bigtable_filters_limit_value_range] -// [END bigtable_filters_limit_value_regex] -// [END bigtable_filters_limit_timestamp_range] -// [END bigtable_filters_limit_block_all] -// [END bigtable_filters_limit_pass_all] -// [END bigtable_filters_modify_strip_value] -// [END bigtable_filters_modify_apply_label] -// [END bigtable_filters_composing_chain] -// [END bigtable_filters_composing_interleave] -// [END bigtable_filters_composing_condition] +// [END bigtable_filters_print] diff --git a/bigtable/snippets/src/main/java/com/example/bigtable/Reads.java b/bigtable/snippets/src/main/java/com/example/bigtable/Reads.java index 778108725af..989da4f62f1 100644 --- a/bigtable/snippets/src/main/java/com/example/bigtable/Reads.java +++ b/bigtable/snippets/src/main/java/com/example/bigtable/Reads.java @@ -16,13 +16,7 @@ package com.example.bigtable; -// [START bigtable_reads_row] -// [START bigtable_reads_row_partial] -// [START bigtable_reads_rows] -// [START bigtable_reads_row_range] -// [START bigtable_reads_row_ranges] -// [START bigtable_reads_prefix] -// [START bigtable_reads_filter] +// [START bigtable_reads_print] import static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS; @@ -37,14 +31,9 @@ import java.io.IOException; public class Reads { - // [END bigtable_reads_row] - // [END bigtable_reads_row_partial] - // [END bigtable_reads_rows] - // [END bigtable_reads_row_range] - // [END bigtable_reads_row_ranges] - // [END bigtable_reads_prefix] - // [END bigtable_reads_filter] + // Write your code here. + // [START_EXCLUDE] // [START bigtable_reads_row] public static void readRow() { // TODO(developer): Replace these variables before running the sample. @@ -240,14 +229,8 @@ public static void readFilter(String projectId, String instanceId, String tableI } } // [END bigtable_reads_filter] + // [END_EXCLUDE] - // [START bigtable_reads_row] - // [START bigtable_reads_row_partial] - // [START bigtable_reads_rows] - // [START bigtable_reads_row_range] - // [START bigtable_reads_row_ranges] - // [START bigtable_reads_prefix] - // [START bigtable_reads_filter] private static void printRow(Row row) { System.out.printf("Reading data for %s%n", row.getKey().toStringUtf8()); String colFamily = ""; @@ -263,10 +246,4 @@ private static void printRow(Row row) { System.out.println(); } } -// [END bigtable_reads_row] -// [END bigtable_reads_row_partial] -// [END bigtable_reads_rows] -// [END bigtable_reads_row_range] -// [END bigtable_reads_row_ranges] -// [END bigtable_reads_prefix] -// [END bigtable_reads_filter] +// [END bigtable_reads_print] diff --git a/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java b/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java index ab9c7af6692..429b5712c49 100644 --- a/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java +++ b/bigtable/snippets/src/test/java/com/example/bigtable/FiltersTest.java @@ -57,7 +57,9 @@ public class FiltersTest { private static String requireEnv(String varName) { String value = System.getenv(varName); - assertNotNull(String.format("Environment variable '%s' is required to perform these tests.", varName), value); + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName), + value); return value; } @@ -597,36 +599,46 @@ public void testFilterApplyLabel() { + "\tdata_plan_01gb: true @%2$s [labelled]\n" + "\tdata_plan_05gb: true @%1$s [labelled]\n" + "Column Family stats_summary\n" - + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [labelled]\n" - + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [labelled]\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s " + + "[labelled]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s " + + "[labelled]\n" + "\tos_build: PQ2A.190405.003 @%1$s [labelled]\n\n" + "Reading data for phone#4c410523#20190502\n" + "Column Family cell_plan\n" + "\tdata_plan_05gb: true @%1$s [labelled]\n" + "Column Family stats_summary\n" - + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [labelled]\n" - + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [labelled]\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s " + + "[labelled]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s " + + "[labelled]\n" + "\tos_build: PQ2A.190405.004 @%1$s [labelled]\n\n" + "Reading data for phone#4c410523#20190505\n" + "Column Family cell_plan\n" + "\tdata_plan_05gb: true @%1$s [labelled]\n" + "Column Family stats_summary\n" - + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s [labelled]\n" - + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [labelled]\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s " + + "[labelled]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s " + + "[labelled]\n" + "\tos_build: PQ2A.190406.000 @%1$s [labelled]\n\n" + "Reading data for phone#5c10102#20190501\n" + "Column Family cell_plan\n" + "\tdata_plan_10gb: true @%1$s [labelled]\n" + "Column Family stats_summary\n" - + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [labelled]\n" - + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [labelled]\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s " + + "[labelled]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s " + + "[labelled]\n" + "\tos_build: PQ2A.190401.002 @%1$s [labelled]\n\n" + "Reading data for phone#5c10102#20190502\n" + "Column Family cell_plan\n" + "\tdata_plan_10gb: true @%1$s [labelled]\n" + "Column Family stats_summary\n" - + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [labelled]\n" - + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s [labelled]\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s " + + "[labelled]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s " + + "[labelled]\n" + "\tos_build: PQ2A.190406.000 @%1$s [labelled]", TIMESTAMP, TIMESTAMP_MINUS_HR)); } @@ -709,36 +721,46 @@ public void testFilterCondition() { + "\tdata_plan_01gb: true @%2$s [filtered-out]\n" + "\tdata_plan_05gb: true @%1$s [filtered-out]\n" + "Column Family stats_summary\n" - + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [filtered-out]\n" - + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [filtered-out]\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s " + + "[filtered-out]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s " + + "[filtered-out]\n" + "\tos_build: PQ2A.190405.003 @%1$s [filtered-out]\n\n" + "Reading data for phone#4c410523#20190502\n" + "Column Family cell_plan\n" + "\tdata_plan_05gb: true @%1$s [filtered-out]\n" + "Column Family stats_summary\n" - + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [filtered-out]\n" - + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [filtered-out]\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s " + + "[filtered-out]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s " + + "[filtered-out]\n" + "\tos_build: PQ2A.190405.004 @%1$s [filtered-out]\n\n" + "Reading data for phone#4c410523#20190505\n" + "Column Family cell_plan\n" + "\tdata_plan_05gb: true @%1$s [filtered-out]\n" + "Column Family stats_summary\n" - + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s [filtered-out]\n" - + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [filtered-out]\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s " + + "[filtered-out]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s " + + "[filtered-out]\n" + "\tos_build: PQ2A.190406.000 @%1$s [filtered-out]\n\n" + "Reading data for phone#5c10102#20190501\n" + "Column Family cell_plan\n" + "\tdata_plan_10gb: true @%1$s [passed-filter]\n" + "Column Family stats_summary\n" - + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [passed-filter]\n" - + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [passed-filter]\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s " + + "[passed-filter]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s " + + "[passed-filter]\n" + "\tos_build: PQ2A.190401.002 @%1$s [passed-filter]\n\n" + "Reading data for phone#5c10102#20190502\n" + "Column Family cell_plan\n" + "\tdata_plan_10gb: true @%1$s [passed-filter]\n" + "Column Family stats_summary\n" - + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s [passed-filter]\n" - + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s [passed-filter]\n" + + "\tconnected_cell: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001 @%1$s " + + "[passed-filter]\n" + + "\tconnected_wifi: \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000 @%1$s " + + "[passed-filter]\n" + "\tos_build: PQ2A.190406.000 @%1$s [passed-filter]", TIMESTAMP, TIMESTAMP_MINUS_HR)); } diff --git a/bigtable/snippets/src/test/java/com/example/bigtable/ReadsTest.java b/bigtable/snippets/src/test/java/com/example/bigtable/ReadsTest.java index e7afadee07d..7b526c87ea0 100644 --- a/bigtable/snippets/src/test/java/com/example/bigtable/ReadsTest.java +++ b/bigtable/snippets/src/test/java/com/example/bigtable/ReadsTest.java @@ -52,7 +52,9 @@ public class ReadsTest { private static String requireEnv(String varName) { String value = System.getenv(varName); - assertNotNull(String.format("Environment variable '%s' is required to perform these tests.", varName), value); + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName), + value); return value; } diff --git a/bigtable/snippets/src/test/java/com/example/bigtable/WritesTest.java b/bigtable/snippets/src/test/java/com/example/bigtable/WritesTest.java index 364b527a962..b28a1ef74d4 100644 --- a/bigtable/snippets/src/test/java/com/example/bigtable/WritesTest.java +++ b/bigtable/snippets/src/test/java/com/example/bigtable/WritesTest.java @@ -43,9 +43,12 @@ public class WritesTest { private static String projectId; private static String instanceId; private ByteArrayOutputStream bout; + private static String requireEnv(String varName) { String value = System.getenv(varName); - assertNotNull(String.format("Environment variable '%s' is required to perform these tests.", varName), value); + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName), + value); return value; } diff --git a/cdn/signed-urls/pom.xml b/cdn/signed-urls/pom.xml index 729ee638683..32afe3eb427 100644 --- a/cdn/signed-urls/pom.xml +++ b/cdn/signed-urls/pom.xml @@ -14,7 +14,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 signedurls diff --git a/cloud-sql/mysql/servlet/pom.xml b/cloud-sql/mysql/servlet/pom.xml index 4bb7b6c7447..2e8d977ed07 100644 --- a/cloud-sql/mysql/servlet/pom.xml +++ b/cloud-sql/mysql/servlet/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -81,7 +81,7 @@ org.eclipse.jetty jetty-maven-plugin - 9.4.29.v20200521 + 9.4.30.v20200611 1 @@ -90,7 +90,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/cloud-sql/postgres/servlet/pom.xml b/cloud-sql/postgres/servlet/pom.xml index 52364815132..b23a322fcd3 100644 --- a/cloud-sql/postgres/servlet/pom.xml +++ b/cloud-sql/postgres/servlet/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -52,7 +52,7 @@ org.postgresql postgresql - 42.2.12 + 42.2.14 com.google.cloud.sql @@ -71,7 +71,7 @@ org.eclipse.jetty jetty-maven-plugin - 9.4.29.v20200521 + 9.4.30.v20200611 1 @@ -80,7 +80,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/compute/cmdline/pom.xml b/compute/cmdline/pom.xml index 8c3e689bacb..91a347a559a 100644 --- a/compute/cmdline/pom.xml +++ b/compute/cmdline/pom.xml @@ -26,21 +26,21 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 - 8 - 8 - v1-rev20200427-1.30.9 + 1.8 + 1.8 + v1-rev20200526-1.30.9 UTF-8 - com.google.api-client - google-api-client - 1.30.9 + com.google.auth + google-auth-library-oauth2-http + 0.21.0 com.google.apis @@ -55,7 +55,7 @@ limitations under the License. org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 diff --git a/compute/cmdline/src/main/java/ComputeEngineSample.java b/compute/cmdline/src/main/java/ComputeEngineSample.java index 82a2d70654b..d4b1331d04f 100644 --- a/compute/cmdline/src/main/java/ComputeEngineSample.java +++ b/compute/cmdline/src/main/java/ComputeEngineSample.java @@ -14,8 +14,8 @@ * limitations under the License. */ -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.HttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; @@ -30,7 +30,8 @@ import com.google.api.services.compute.model.NetworkInterface; import com.google.api.services.compute.model.Operation; import com.google.api.services.compute.model.ServiceAccount; - +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -59,13 +60,16 @@ public class ComputeEngineSample { /** Set the name of the sample VM instance to be created. */ private static final String SAMPLE_INSTANCE_NAME = "my-sample-instance"; - /** Set the path of the OS image for the sample VM instance to be created. */ - private static final String SOURCE_IMAGE_PREFIX = "https://www.googleapis.com/compute/v1/projects/"; + /** Set the path of the OS image for the sample VM instance to be created. */ + private static final String SOURCE_IMAGE_PREFIX = + "https://www.googleapis.com/compute/v1/projects/"; + private static final String SOURCE_IMAGE_PATH = - "debian-cloud/global/images/debian-7-wheezy-v20150710"; + "ubuntu-os-cloud/global/images/ubuntu-2004-focal-v20200529"; /** Set the Network configuration values of the sample VM instance to be created. */ private static final String NETWORK_INTERFACE_CONFIG = "ONE_TO_ONE_NAT"; + private static final String NETWORK_ACCESS_CONFIG = "External NAT"; /** Set the time out limit for operation calls to the Compute Engine API. */ @@ -77,14 +81,12 @@ public class ComputeEngineSample { /** Global instance of the JSON factory. */ private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); - - public static void main(String[] args) { try { httpTransport = GoogleNetHttpTransport.newTrustedTransport(); // Authenticate using Google Application Default Credentials. - GoogleCredential credential = GoogleCredential.getApplicationDefault(); + GoogleCredentials credential = GoogleCredentials.getApplicationDefault(); if (credential.createScopedRequired()) { List scopes = new ArrayList<>(); // Set Google Cloud Storage scope to Full Control. @@ -93,10 +95,10 @@ public static void main(String[] args) { scopes.add(ComputeScopes.COMPUTE); credential = credential.createScoped(scopes); } - + HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credential); // Create Compute Engine object for listing instances. Compute compute = - new Compute.Builder(httpTransport, JSON_FACTORY, credential) + new Compute.Builder(httpTransport, JSON_FACTORY, requestInitializer) .setApplicationName(APPLICATION_NAME) .build(); @@ -139,8 +141,9 @@ public static boolean printInstances(Compute compute) throws IOException { InstanceList list = instances.execute(); boolean found = false; if (list.getItems() == null) { - System.out.println("No instances found. Sign in to the Google Developers Console and create " - + "an instance at: https://console.developers.google.com/"); + System.out.println( + "No instances found. Sign in to the Google Developers Console and create " + + "an instance at: https://console.developers.google.com/"); } else { for (Instance instance : list.getItems()) { System.out.println(instance.toPrettyString()); @@ -157,17 +160,19 @@ public static boolean printInstances(Compute compute) throws IOException { public static Operation startInstance(Compute compute, String instanceName) throws IOException { System.out.println("================== Starting New Instance =================="); - // Create VM Instance object with the required properties. Instance instance = new Instance(); instance.setName(instanceName); instance.setMachineType( - "https://www.googleapis.com/compute/v1/projects/" - + PROJECT_ID + "/zones/" + ZONE_NAME + "/machineTypes/n1-standard-1"); - + String.format( + "https://www.googleapis.com/compute/v1/projects/%s/zones/%s/machineTypes/e2-standard-1", + PROJECT_ID, ZONE_NAME)); // Add Network Interface to be used by VM Instance. NetworkInterface ifc = new NetworkInterface(); - ifc.setNetwork("https://www.googleapis.com/compute/v1/projects/" + PROJECT_ID + "/global/networks/default"); + ifc.setNetwork( + String.format( + "https://www.googleapis.com/compute/v1/projects/%s/global/networks/default", + PROJECT_ID)); List configs = new ArrayList<>(); AccessConfig config = new AccessConfig(); config.setType(NETWORK_INTERFACE_CONFIG); @@ -187,8 +192,10 @@ public static Operation startInstance(Compute compute, String instanceName) thro // Specify the source operating system machine image to be used by the VM Instance. params.setSourceImage(SOURCE_IMAGE_PREFIX + SOURCE_IMAGE_PATH); // Specify the disk type as Standard Persistent Disk - params.setDiskType("https://www.googleapis.com/compute/v1/projects/" + PROJECT_ID + "/zones/" - + ZONE_NAME + "/diskTypes/pd-standard"); + params.setDiskType( + String.format( + "https://www.googleapis.com/compute/v1/projects/%s/zones/%s/diskTypes/pd-standard", + PROJECT_ID, ZONE_NAME)); disk.setInitializeParams(params); instance.setDisks(Collections.singletonList(disk)); @@ -208,8 +215,9 @@ public static Operation startInstance(Compute compute, String instanceName) thro // If you put a script called "vm-startup.sh" in this Google Cloud Storage // bucket, it will execute on VM startup. This assumes you've created a // bucket named the same as your PROJECT_ID. - // For info on creating buckets see: https://cloud.google.com/storage/docs/cloud-console#_creatingbuckets - item.setValue("gs://" + PROJECT_ID + "/vm-startup.sh"); + // For info on creating buckets see: + // https://cloud.google.com/storage/docs/cloud-console#_creatingbuckets + item.setValue(String.format("gs://%s/vm-startup.sh", PROJECT_ID)); meta.setItems(Collections.singletonList(item)); instance.setMetadata(meta); @@ -230,6 +238,7 @@ private static Operation deleteInstance(Compute compute, String instanceName) th // [START wait_until_complete] /** * Wait until {@code operation} is completed. + * * @param compute the {@code Compute} object * @param operation the operation returned by the original request * @param timeout the timeout, in millis @@ -241,7 +250,7 @@ public static Operation.Error blockUntilComplete( Compute compute, Operation operation, long timeout) throws Exception { long start = System.currentTimeMillis(); final long pollInterval = 5 * 1000; - String zone = operation.getZone(); // null for global/regional operations + String zone = operation.getZone(); // null for global/regional operations if (zone != null) { String[] bits = zone.split("/"); zone = bits[bits.length - 1]; diff --git a/compute/error-reporting/pom.xml b/compute/error-reporting/pom.xml index a955e0999b8..61f888a9821 100644 --- a/compute/error-reporting/pom.xml +++ b/compute/error-reporting/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 1.8 diff --git a/compute/mailjet/pom.xml b/compute/mailjet/pom.xml index cff4cd3eff1..3415e0cd94f 100644 --- a/compute/mailjet/pom.xml +++ b/compute/mailjet/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 1.8 @@ -91,21 +91,6 @@ org.apache.maven.plugins maven-compiler-plugin 3.8.1 - - 1.8 - 1.8 - - -XDcompilePolicy=simple - -Xplugin:ErrorProne - - - - com.google.errorprone - error_prone_core - 2.3.4 - - - diff --git a/compute/sendgrid/pom.xml b/compute/sendgrid/pom.xml index c98c97b973c..ee6e98b731d 100644 --- a/compute/sendgrid/pom.xml +++ b/compute/sendgrid/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -40,7 +40,7 @@ com.sendgrid sendgrid-java - 4.5.0 + 4.6.0 diff --git a/compute/signed-metadata/pom.xml b/compute/signed-metadata/pom.xml index fbf5b463279..b6e3af9a298 100644 --- a/compute/signed-metadata/pom.xml +++ b/compute/signed-metadata/pom.xml @@ -28,7 +28,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 diff --git a/container-registry/container-analysis/pom.xml b/container-registry/container-analysis/pom.xml index 716d6b375de..a22bb1210e7 100644 --- a/container-registry/container-analysis/pom.xml +++ b/container-registry/container-analysis/pom.xml @@ -18,7 +18,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -35,7 +35,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -77,7 +77,7 @@ io.netty netty-tcnative-boringssl-static - 2.0.30.Final + 2.0.31.Final diff --git a/datacatalog/cloud-client/pom.xml b/datacatalog/cloud-client/pom.xml index 78ce71f78bf..62643da5cf0 100644 --- a/datacatalog/cloud-client/pom.xml +++ b/datacatalog/cloud-client/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -39,7 +39,7 @@ com.google.cloud google-cloud-datacatalog - 0.35.0 + 1.0.0 diff --git a/datacatalog/quickstart/pom.xml b/datacatalog/quickstart/pom.xml index b6c2cdf3ffc..42782690b70 100644 --- a/datacatalog/quickstart/pom.xml +++ b/datacatalog/quickstart/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -39,7 +39,7 @@ com.google.cloud google-cloud-datacatalog - 0.35.0 + 1.0.0 diff --git a/dataflow/encryption-keys/pom.xml b/dataflow/encryption-keys/pom.xml index 8d821524d78..7ce13cdbc9a 100644 --- a/dataflow/encryption-keys/pom.xml +++ b/dataflow/encryption-keys/pom.xml @@ -28,12 +28,12 @@ 11 UTF-8 - 2.20.0 + 2.22.0 3.8.1 - 1.6.0 + 3.0.0 3.2.0 - 3.2.3 + 3.2.4 1.7.30 diff --git a/dataflow/flex-templates/kafka_to_bigquery/pom.xml b/dataflow/flex-templates/kafka_to_bigquery/pom.xml index 7d2a95b06ee..c4da26acb13 100644 --- a/dataflow/flex-templates/kafka_to_bigquery/pom.xml +++ b/dataflow/flex-templates/kafka_to_bigquery/pom.xml @@ -28,13 +28,13 @@ 11 UTF-8 - 2.20.0 + 2.22.0 2.5.0 3.0.0-M3 3.8.1 - 3.2.3 - 1.6.0 + 3.2.4 + 3.0.0 1.7.30 diff --git a/dataflow/flex-templates/streaming_beam_sql/pom.xml b/dataflow/flex-templates/streaming_beam_sql/pom.xml index 628da989a28..67bf8b4a41c 100644 --- a/dataflow/flex-templates/streaming_beam_sql/pom.xml +++ b/dataflow/flex-templates/streaming_beam_sql/pom.xml @@ -28,12 +28,12 @@ 11 UTF-8 - 2.20.0 + 2.22.0 3.0.0-M3 3.8.1 - 3.2.3 - 1.6.0 + 3.2.4 + 3.0.0 1.7.30 diff --git a/dataflow/spanner-io/pom.xml b/dataflow/spanner-io/pom.xml index 7b339b4d11f..19bf802145b 100644 --- a/dataflow/spanner-io/pom.xml +++ b/dataflow/spanner-io/pom.xml @@ -30,7 +30,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -38,7 +38,7 @@ 1.8 1.8 1.8 - 2.20.0 + 2.22.0 @@ -51,7 +51,7 @@ org.apache.maven.plugins maven-failsafe-plugin - 3.0.0-M4 + 3.0.0-M5 default-instance diff --git a/dataflow/templates/pom.xml b/dataflow/templates/pom.xml index 2ad18bfda64..9b00436dec1 100644 --- a/dataflow/templates/pom.xml +++ b/dataflow/templates/pom.xml @@ -28,12 +28,12 @@ 11 UTF-8 - 2.20.0 + 2.22.0 3.8.1 - 1.6.0 + 3.0.0 3.2.0 - 3.2.3 + 3.2.4 1.7.30 diff --git a/dataflow/transforms/pom.xml b/dataflow/transforms/pom.xml index 81d512eeae1..2c8eb78c93a 100644 --- a/dataflow/transforms/pom.xml +++ b/dataflow/transforms/pom.xml @@ -28,22 +28,22 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 - 2.20.0 + 2.22.0 1.22.0 1.3 4.13 1.8 1.8 - 1.6.0 + 3.0.0 3.2.0 - 3.2.3 + 3.2.4 1.7.30 - 3.0.0-M4 + 3.0.0-M5 diff --git a/datalabeling/beta/cloud-client/pom.xml b/datalabeling/beta/cloud-client/pom.xml index 2d7e0136faa..d505e29e724 100644 --- a/datalabeling/beta/cloud-client/pom.xml +++ b/datalabeling/beta/cloud-client/pom.xml @@ -23,7 +23,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 diff --git a/dataproc/pom.xml b/dataproc/pom.xml index a836603acf3..1410ef5c0dc 100644 --- a/dataproc/pom.xml +++ b/dataproc/pom.xml @@ -15,7 +15,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -38,7 +38,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 Quickstart @@ -51,7 +51,7 @@ com.google.cloud libraries-bom - 4.4.1 + 8.0.0 pom import diff --git a/dataproc/src/main/java/CreateClusterWithAutoscaling.java b/dataproc/src/main/java/CreateClusterWithAutoscaling.java new file mode 100644 index 00000000000..79b50c984f5 --- /dev/null +++ b/dataproc/src/main/java/CreateClusterWithAutoscaling.java @@ -0,0 +1,162 @@ +/* +* Copyright 2020 Google LLC +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +* This sample creates a Dataproc cluster with an autoscaling policy enabled. The policy we will be creating mirrors +* the following YAML representation: +* + workerConfig: + minInstances: 2 + maxInstances: 100 + weight: 1 + secondaryWorkerConfig: + minInstances: 0 + maxInstances: 100 + weight: 1 + basicAlgorithm: + cooldownPeriod: 4m + yarnConfig: + scaleUpFactor: 0.05 + scaleDownFactor: 1.0 + scaleUpMinWorkerFraction: 0.0 + scaleDownMinWorkerFraction: 0.0 + gracefulDecommissionTimeout: 1h +*/ + +// [START dataproc_create_autoscaling_cluster] + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.dataproc.v1.*; +import com.google.protobuf.Duration; + +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +public class CreateClusterWithAutoscaling { + + public static void createClusterwithAutoscaling() throws IOException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String region = "your-project-region"; + String clusterName = "your-cluster-name"; + String autoscalingPolicyName = "your-autoscaling-policy"; + createClusterwithAutoscaling(projectId, region, clusterName, autoscalingPolicyName); + } + + public static void createClusterwithAutoscaling( + String projectId, String region, String clusterName, String autoscalingPolicyName) + throws IOException, InterruptedException { + String myEndpoint = String.format("%s-dataproc.googleapis.com:443", region); + + // Configure the settings for the cluster controller client. + ClusterControllerSettings clusterControllerSettings = + ClusterControllerSettings.newBuilder().setEndpoint(myEndpoint).build(); + + // Configure the settings for the autoscaling policy service client. + AutoscalingPolicyServiceSettings autoscalingPolicyServiceSettings = + AutoscalingPolicyServiceSettings.newBuilder().setEndpoint(myEndpoint).build(); + + // Create a cluster controller client and an autoscaling controller client with the configured + // settings. The clients only need to be created once and can be reused for multiple requests. + // Using a + // try-with-resources closes the client, but this can also be done manually with the .close() + // method. + try (ClusterControllerClient clusterControllerClient = + ClusterControllerClient.create(clusterControllerSettings); + AutoscalingPolicyServiceClient autoscalingPolicyServiceClient = + AutoscalingPolicyServiceClient.create(autoscalingPolicyServiceSettings)) { + + // Create the Autoscaling policy. + InstanceGroupAutoscalingPolicyConfig workerInstanceGroupAutoscalingPolicyConfig = + InstanceGroupAutoscalingPolicyConfig.newBuilder() + .setMinInstances(2) + .setMaxInstances(100) + .setWeight(1) + .build(); + InstanceGroupAutoscalingPolicyConfig secondaryWorkerInstanceGroupAutoscalingPolicyConfig = + InstanceGroupAutoscalingPolicyConfig.newBuilder() + .setMinInstances(0) + .setMaxInstances(100) + .setWeight(1) + .build(); + BasicYarnAutoscalingConfig basicYarnApplicationConfig = + BasicYarnAutoscalingConfig.newBuilder() + .setScaleUpFactor(0.05) + .setScaleDownFactor(1.0) + .setScaleUpMinWorkerFraction(0.0) + .setScaleUpMinWorkerFraction(0.0) + .setGracefulDecommissionTimeout(Duration.newBuilder().setSeconds(3600).build()) + .build(); + BasicAutoscalingAlgorithm basicAutoscalingAlgorithm = + BasicAutoscalingAlgorithm.newBuilder() + .setCooldownPeriod(Duration.newBuilder().setSeconds(240).build()) + .setYarnConfig(basicYarnApplicationConfig) + .build(); + AutoscalingPolicy autoscalingPolicy = + AutoscalingPolicy.newBuilder() + .setId(autoscalingPolicyName) + .setWorkerConfig(workerInstanceGroupAutoscalingPolicyConfig) + .setSecondaryWorkerConfig(secondaryWorkerInstanceGroupAutoscalingPolicyConfig) + .setBasicAlgorithm(basicAutoscalingAlgorithm) + .build(); + RegionName parent = RegionName.of(projectId, region); + + // Policy is uploaded here. + autoscalingPolicyServiceClient.createAutoscalingPolicy(parent, autoscalingPolicy); + + // Now the policy can be referenced when creating a cluster. + String autoscalingPolicyURI = + String.format( + "projects/%s/locations/%s/autoscalingPolicies/%s", + projectId, region, autoscalingPolicyName); + AutoscalingConfig autoscalingConfig = + AutoscalingConfig.newBuilder().setPolicyUri(autoscalingPolicyURI).build(); + + // Configure the settings for our cluster. + InstanceGroupConfig masterConfig = + InstanceGroupConfig.newBuilder() + .setMachineTypeUri("n1-standard-1") + .setNumInstances(1) + .build(); + InstanceGroupConfig workerConfig = + InstanceGroupConfig.newBuilder() + .setMachineTypeUri("n1-standard-1") + .setNumInstances(2) + .build(); + ClusterConfig clusterConfig = + ClusterConfig.newBuilder() + .setMasterConfig(masterConfig) + .setWorkerConfig(workerConfig) + .setAutoscalingConfig(autoscalingConfig) + .build(); + + // Create the cluster object with the desired cluster config. + Cluster cluster = + Cluster.newBuilder().setClusterName(clusterName).setConfig(clusterConfig).build(); + + // Create the Dataproc cluster. + OperationFuture createClusterAsyncRequest = + clusterControllerClient.createClusterAsync(projectId, region, cluster); + Cluster response = createClusterAsyncRequest.get(); + + // Print out a success message. + System.out.printf("Cluster created successfully: %s", response.getClusterName()); + + } catch (ExecutionException e) { + // If cluster creation does not complete successfully, print the error message. + System.err.println(String.format("createClusterWithAutoscaling: %s ", e.getMessage())); + } + } +} +// [END dataproc_create_autoscaling_cluster] diff --git a/dataproc/src/main/java/SubmitHadoopFSJob.java b/dataproc/src/main/java/SubmitHadoopFSJob.java new file mode 100644 index 00000000000..1d7b9f1571f --- /dev/null +++ b/dataproc/src/main/java/SubmitHadoopFSJob.java @@ -0,0 +1,96 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [START dataproc_submit_hadoop_fs_job] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.dataproc.v1.*; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.concurrent.ExecutionException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class SubmitHadoopFSJob { + + public static ArrayList stringToList(String s) { + return new ArrayList<>(Arrays.asList(s.split(" "))); + } + + public static void submitHadoopFSQuery() throws IOException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String region = "your-project-region"; + String clusterName = "your-cluster-name"; + String hadoopFSQuery = "your-hadoop-fs-query"; + submitHadoopFSJob(projectId, region, clusterName, hadoopFSQuery); + } + + public static void submitHadoopFSJob( + String projectId, String region, String clusterName, String hadoopFSQuery) + throws IOException, InterruptedException { + String myEndpoint = String.format("%s-dataproc.googleapis.com:443", region); + + // Configure the settings for the job controller client. + JobControllerSettings jobControllerSettings = + JobControllerSettings.newBuilder().setEndpoint(myEndpoint).build(); + + // Create a job controller client with the configured settings. Using a try-with-resources + // closes the client, + // but this can also be done manually with the .close() method. + try (JobControllerClient jobControllerClient = + JobControllerClient.create(jobControllerSettings)) { + + // Configure cluster placement for the job. + JobPlacement jobPlacement = JobPlacement.newBuilder().setClusterName(clusterName).build(); + + // Configure Hadoop job settings. The HadoopFS query is set here. + HadoopJob hadoopJob = + HadoopJob.newBuilder() + .setMainClass("org.apache.hadoop.fs.FsShell") + .addAllArgs(stringToList(hadoopFSQuery)) + .build(); + + Job job = Job.newBuilder().setPlacement(jobPlacement).setHadoopJob(hadoopJob).build(); + + // Submit an asynchronous request to execute the job. + OperationFuture submitJobAsOperationAsyncRequest = + jobControllerClient.submitJobAsOperationAsync(projectId, region, job); + + Job response = submitJobAsOperationAsyncRequest.get(); + + // Print output from Google Cloud Storage. + Matcher matches = + Pattern.compile("gs://(.*?)/(.*)").matcher(response.getDriverOutputResourceUri()); + matches.matches(); + + Storage storage = StorageOptions.getDefaultInstance().getService(); + Blob blob = storage.get(matches.group(1), String.format("%s.000000000", matches.group(2))); + + System.out.println( + String.format("Job finished successfully: %s", new String(blob.getContent()))); + + } catch (ExecutionException e) { + // If the job does not complete successfully, print the error message. + System.err.println(String.format("submitHadoopFSJob: %s ", e.getMessage())); + } + } +} +// [END dataproc_submit_hadoop_fs_job] diff --git a/dataproc/src/test/java/CreateClusterWithAutoscalingTest.java b/dataproc/src/test/java/CreateClusterWithAutoscalingTest.java new file mode 100644 index 00000000000..3312e5ce14e --- /dev/null +++ b/dataproc/src/test/java/CreateClusterWithAutoscalingTest.java @@ -0,0 +1,100 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import static junit.framework.TestCase.assertNotNull; +import static org.hamcrest.MatcherAssert.assertThat; + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.dataproc.v1.*; +import com.google.protobuf.Empty; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import org.hamcrest.CoreMatchers; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class CreateClusterWithAutoscalingTest { + + private static final String CLUSTER_NAME = + String.format("java-as-test-%s", UUID.randomUUID().toString()); + private static final String REGION = "us-central1"; + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String AUTOSCALING_POLICY_NAME = + String.format("java-as-test-%s", UUID.randomUUID().toString()); + + private ByteArrayOutputStream bout; + + private static void requireEnv(String varName) { + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName), + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnv("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnv("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + } + + @After + public void tearDown() throws IOException, InterruptedException, ExecutionException { + String myEndpoint = String.format("%s-dataproc.googleapis.com:443", REGION); + + ClusterControllerSettings clusterControllerSettings = + ClusterControllerSettings.newBuilder().setEndpoint(myEndpoint).build(); + + AutoscalingPolicyServiceSettings autoscalingPolicyServiceSettings = + AutoscalingPolicyServiceSettings.newBuilder().setEndpoint(myEndpoint).build(); + + try (ClusterControllerClient clusterControllerClient = + ClusterControllerClient.create(clusterControllerSettings); + AutoscalingPolicyServiceClient autoscalingPolicyServiceClient = + AutoscalingPolicyServiceClient.create(autoscalingPolicyServiceSettings)) { + + OperationFuture deleteClusterAsyncRequest = + clusterControllerClient.deleteClusterAsync(PROJECT_ID, REGION, CLUSTER_NAME); + deleteClusterAsyncRequest.get(); + + AutoscalingPolicyName name = + AutoscalingPolicyName.ofProjectLocationAutoscalingPolicyName( + PROJECT_ID, REGION, AUTOSCALING_POLICY_NAME); + autoscalingPolicyServiceClient.deleteAutoscalingPolicy(name); + } + } + + @Test + public void createClusterWithAutoscalingTest() throws IOException, InterruptedException { + CreateClusterWithAutoscaling.createClusterwithAutoscaling( + PROJECT_ID, REGION, CLUSTER_NAME, AUTOSCALING_POLICY_NAME); + String output = bout.toString(); + + assertThat(output, CoreMatchers.containsString(CLUSTER_NAME)); + } +} diff --git a/dataproc/src/test/java/SubmitHadoopFSJobTest.java b/dataproc/src/test/java/SubmitHadoopFSJobTest.java new file mode 100644 index 00000000000..976b649459c --- /dev/null +++ b/dataproc/src/test/java/SubmitHadoopFSJobTest.java @@ -0,0 +1,101 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.dataproc.v1.*; +import com.google.protobuf.Empty; +import org.hamcrest.CoreMatchers; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; + +import static junit.framework.TestCase.assertNotNull; +import static org.hamcrest.MatcherAssert.assertThat; + +@RunWith(JUnit4.class) +public class SubmitHadoopFSJobTest { + + private static final String CLUSTER_NAME = + String.format("java-fs-test--%s", UUID.randomUUID().toString()); + private static final String REGION = "us-central1"; + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String ENDPOINT = String.format("%s-dataproc.googleapis.com:443", REGION); + private static final String HADOOP_FS_QUERY = "-ls /"; + + private ByteArrayOutputStream bout; + + private static void requireEnv(String varName) { + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName), + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnv("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnv("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws IOException, ExecutionException, InterruptedException { + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + ClusterControllerSettings clusterControllerSettings = + ClusterControllerSettings.newBuilder().setEndpoint(ENDPOINT).build(); + + try (ClusterControllerClient clusterControllerClient = + ClusterControllerClient.create(clusterControllerSettings)) { + // Create the Dataproc cluster. + Cluster cluster = Cluster.newBuilder().setClusterName(CLUSTER_NAME).build(); + OperationFuture createClusterAsyncRequest = + clusterControllerClient.createClusterAsync(PROJECT_ID, REGION, cluster); + createClusterAsyncRequest.get(); + } + } + + + @Test + public void submitHadoopFSJobTest() throws IOException, InterruptedException { + SubmitHadoopFSJob.submitHadoopFSJob(PROJECT_ID, REGION, CLUSTER_NAME, HADOOP_FS_QUERY); + String output = bout.toString(); + + assertThat(output, CoreMatchers.containsString("/tmp")); + } + + @After + public void tearDown() throws IOException, InterruptedException, ExecutionException { + + ClusterControllerSettings clusterControllerSettings = + ClusterControllerSettings.newBuilder().setEndpoint(ENDPOINT).build(); + + try (ClusterControllerClient clusterControllerClient = + ClusterControllerClient.create(clusterControllerSettings)) { + OperationFuture deleteClusterAsyncRequest = + clusterControllerClient.deleteClusterAsync(PROJECT_ID, REGION, CLUSTER_NAME); + deleteClusterAsyncRequest.get(); + } + } +} diff --git a/datastore/cloud-client/pom.xml b/datastore/cloud-client/pom.xml index ea82c40bff5..858eb30b815 100644 --- a/datastore/cloud-client/pom.xml +++ b/datastore/cloud-client/pom.xml @@ -26,7 +26,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -39,7 +39,7 @@ com.google.cloud google-cloud-datastore - 1.102.4 + 1.104.0 diff --git a/datastore/pom.xml b/datastore/pom.xml index 616d7201906..b7836acbb1b 100644 --- a/datastore/pom.xml +++ b/datastore/pom.xml @@ -32,7 +32,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -44,7 +44,7 @@ com.google.cloud google-cloud-datastore - 1.102.4 + 1.104.0 @@ -60,7 +60,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 com.google.datastore.snippets.TaskList diff --git a/dialogflow/cloud-client/pom.xml b/dialogflow/cloud-client/pom.xml index 88e66bdf511..7f95c9725e8 100644 --- a/dialogflow/cloud-client/pom.xml +++ b/dialogflow/cloud-client/pom.xml @@ -23,7 +23,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 diff --git a/dlp/README.md b/dlp/README.md index 07839b6b6d4..63cc3737ce5 100644 --- a/dlp/README.md +++ b/dlp/README.md @@ -10,77 +10,38 @@ a powerful detection engine for personally identifiable information and other pr ## Setup - A Google Cloud project with billing enabled - [Enable](https://console.cloud.google.com/launcher/details/google/dlp.googleapis.com) the DLP API. -- (Local testing) [Create a service account](https://cloud.google.com/docs/authentication/getting-started) +- [Create a service account](https://cloud.google.com/docs/authentication/getting-started) and set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable pointing to the downloaded credentials file. -- (Local testing) Set the `DLP_DEID_WRAPPED_KEY` environment variable to an AES-256 key encrypted ('wrapped') [with a Cloud Key Management Service (KMS) key](https://cloud.google.com/kms/docs/encrypt-decrypt). -- (Local testing) Set the `DLP_DEID_KEY_NAME` environment variable to the path-name of the Cloud KMS key you wrapped `DLP_DEID_WRAPPED_KEY` with. -## Build -This project uses the [Assembly Plugin](https://maven.apache.org/plugins/maven-assembly-plugin/usage.html) to build an uber jar. -Run: -``` - mvn clean package -DskipTests -``` +## Running -## Retrieve InfoTypes -An [InfoType identifier](https://cloud.google.com/dlp/docs/infotypes-categories) represents an element of sensitive data. - -[InfoTypes](https://cloud.google.com/dlp/docs/infotypes-reference#global) are updated periodically. Use the API to retrieve the most current InfoTypes. - ``` - java -cp dlp/target/dlp-samples-1.0-jar-with-dependencies.jar com.example.dlp.Metadata - ``` +To run a specific sample, edit any variables under the `TODO(developer):` in the +function at the top of each sample, and then execute the function as convenient. -## Run the quickstart - -The Quickstart demonstrates using the DLP API to identify an InfoType in a given string. -``` - java -cp dlp/target/dlp-samples-1.0-jar-with-dependencies.jar dlp.snippets.QuickStart +For example, if using the command line you might use the following (replacing +`` with the name of the sample): +```bash +mvn exec:java -Dexec.mainClass="dlp.snippets." ``` -## Inspect data for sensitive elements -Inspect strings, files locally and on Google Cloud Storage, Cloud Datastore, and BigQuery with the DLP API. - -Note: image scanning is not currently supported on Google Cloud Storage. -For more information, refer to the [API documentation](https://cloud.google.com/dlp/docs). -Optional flags are explained in [this resource](https://cloud.google.com/dlp/docs/reference/rest/v2beta1/content/inspect#InspectConfig). -## Automatic redaction of sensitive data from images -[Automatic redaction](https://cloud.google.com/dlp/docs/redacting-sensitive-data-images) produces an output image with sensitive data matches removed. - -``` -Commands: - -f Source image file - -o Destination image file - Options: - --help Show help - -minLikelihood choices: "LIKELIHOOD_UNSPECIFIED", "VERY_UNLIKELY", "UNLIKELY", "POSSIBLE", "LIKELY", "VERY_LIKELY"] - [default: "LIKELIHOOD_UNSPECIFIED"] - specifies the minimum reporting likelihood threshold. - - -infoTypes set of infoTypes to search for [eg. PHONE_NUMBER US_PASSPORT] -``` - -### Example -- Redact phone numbers and email addresses from `test.png`: - ``` - java -cp dlp/target/dlp-samples-1.0-jar-with-dependencies.jar com.example.dlp.Redact -f src/test/resources/test.png -o test-redacted.png -infoTypes PHONE_NUMBER EMAIL_ADDRESS - ``` +## Testing -## Integration tests ### Setup - Ensure that `GOOGLE_APPLICATION_CREDENTIALS` points to authorized service account credentials file. +- Set the `DLP_DEID_WRAPPED_KEY` environment variable to an AES-256 key encrypted ('wrapped') [with a Cloud Key Management Service (KMS) key](https://cloud.google.com/kms/docs/encrypt-decrypt). +- Set the `DLP_DEID_KEY_NAME` environment variable to the path-name of the Cloud KMS key you wrapped `DLP_DEID_WRAPPED_KEY` with. - [Create a Google Cloud Storage bucket](https://console.cloud.google.com/storage) and upload [test.txt](src/test/resources/test.txt). - Set the `GCS_PATH` environment variable to point to the path for the bucket. - Copy and paste the data below into a CSV file and [create a BigQuery table](https://cloud.google.com/bigquery/docs/loading-data-local) from the file: ```$xslt Name,TelephoneNumber,Mystery,Age,Gender James,(567) 890-1234,8291 3627 8250 1234,19,Male - Gandalf,(123) 456-7890,4231 5555 6781 9876,27,Male + Gandalf,(223) 456-7890,4231 5555 6781 9876,27,Male Dumbledore,(313) 337-1337,6291 8765 1095 7629,27,Male - Joe,(452) 123-1234,3782 2288 1166 3030,35,Male - Marie,(452) 123-1234,8291 3627 8250 1234,35,Female + Joe,(452) 223-1234,3782 2288 1166 3030,35,Male + Marie,(452) 223-1234,8291 3627 8250 1234,35,Female Carrie,(567) 890-1234,2253 5218 4251 4526,35,Female - ``` - Set the `BIGQUERY_DATASET` and `BIGQUERY_TABLE` environment values. - [Create a Google Cloud Pub/Sub](https://console.cloud.google.com/datastore) topic and and a subscription that is subscribed to the topic. @@ -91,7 +52,7 @@ Commands: - Update the Datastore kind in [InspectTests.java](src/test/java/dlp/snippets/InspectTests.java). -## Run +### Run Run all tests: ``` mvn clean verify diff --git a/dlp/pom.xml b/dlp/pom.xml index ce8d519d2b4..d7aad64ffff 100644 --- a/dlp/pom.xml +++ b/dlp/pom.xml @@ -29,7 +29,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -43,7 +43,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -71,6 +71,12 @@ 4.13 test + + com.google.truth + truth + 1.0.1 + test + diff --git a/dlp/src/main/java/dlp/snippets/DeIdentifyTableBucketing.java b/dlp/src/main/java/dlp/snippets/DeIdentifyTableBucketing.java new file mode 100644 index 00000000000..055c19c7658 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/DeIdentifyTableBucketing.java @@ -0,0 +1,121 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_deidentify_table_bucketing] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.DeidentifyConfig; +import com.google.privacy.dlp.v2.DeidentifyContentRequest; +import com.google.privacy.dlp.v2.DeidentifyContentResponse; +import com.google.privacy.dlp.v2.FieldId; +import com.google.privacy.dlp.v2.FieldTransformation; +import com.google.privacy.dlp.v2.FixedSizeBucketingConfig; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.PrimitiveTransformation; +import com.google.privacy.dlp.v2.RecordTransformations; +import com.google.privacy.dlp.v2.Table; +import com.google.privacy.dlp.v2.Table.Row; +import com.google.privacy.dlp.v2.Value; +import java.io.IOException; + +public class DeIdentifyTableBucketing { + + public static void deIdentifyTableBucketing() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + Table tableToDeIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .build()) + .build(); + + deIdentifyTableBucketing(projectId, tableToDeIdentify); + } + + public static Table deIdentifyTableBucketing(String projectId, Table tableToDeIdentify) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify what content you want the service to de-identify. + ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build(); + + // Specify how the content should be de-identified. + FixedSizeBucketingConfig fixedSizeBucketingConfig = + FixedSizeBucketingConfig.newBuilder() + .setBucketSize(10) + .setLowerBound(Value.newBuilder().setIntegerValue(0).build()) + .setUpperBound(Value.newBuilder().setIntegerValue(100).build()) + .build(); + PrimitiveTransformation primitiveTransformation = + PrimitiveTransformation.newBuilder() + .setFixedSizeBucketingConfig(fixedSizeBucketingConfig) + .build(); + + // Specify field to be encrypted. + FieldId fieldId = FieldId.newBuilder().setName("HAPPINESS SCORE").build(); + + // Associate the encryption with the specified field. + FieldTransformation fieldTransformation = + FieldTransformation.newBuilder() + .setPrimitiveTransformation(primitiveTransformation) + .addFields(fieldId) + .build(); + RecordTransformations transformations = + RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build(); + + DeidentifyConfig deidentifyConfig = + DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build(); + + // Combine configurations into a request for the service. + DeidentifyContentRequest request = + DeidentifyContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(contentItem) + .setDeidentifyConfig(deidentifyConfig) + .build(); + + // Send the request and receive response from the service. + DeidentifyContentResponse response = dlp.deidentifyContent(request); + + // Print the results. + System.out.println( + "Table after de-identification: " + response.getItem().getTable()); + + return response.getItem().getTable(); + } + } +} +// [END dlp_deidentify_table_bucketing] diff --git a/dlp/src/main/java/dlp/snippets/DeIdentifyTableConditionInfoTypes.java b/dlp/src/main/java/dlp/snippets/DeIdentifyTableConditionInfoTypes.java new file mode 100644 index 00000000000..0737f97b903 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/DeIdentifyTableConditionInfoTypes.java @@ -0,0 +1,163 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_deidentify_table_condition_infotypes] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.DeidentifyConfig; +import com.google.privacy.dlp.v2.DeidentifyContentRequest; +import com.google.privacy.dlp.v2.DeidentifyContentResponse; +import com.google.privacy.dlp.v2.FieldId; +import com.google.privacy.dlp.v2.FieldTransformation; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InfoTypeTransformations; +import com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.PrimitiveTransformation; +import com.google.privacy.dlp.v2.RecordCondition; +import com.google.privacy.dlp.v2.RecordCondition.Condition; +import com.google.privacy.dlp.v2.RecordCondition.Conditions; +import com.google.privacy.dlp.v2.RecordCondition.Expressions; +import com.google.privacy.dlp.v2.RecordTransformations; +import com.google.privacy.dlp.v2.RelationalOperator; +import com.google.privacy.dlp.v2.ReplaceWithInfoTypeConfig; +import com.google.privacy.dlp.v2.Table; +import com.google.privacy.dlp.v2.Table.Row; +import com.google.privacy.dlp.v2.Value; +import java.io.IOException; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class DeIdentifyTableConditionInfoTypes { + + public static void deIdentifyTableConditionInfoTypes() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + Table tableToDeIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addHeaders(FieldId.newBuilder().setName("FACTOID").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .addValues(Value.newBuilder().setStringValue( + "Charles Dickens name was a curse, possibly invented by Shakespeare.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .addValues(Value.newBuilder().setStringValue( + "There are 14 kisses in Jane Austen's novels.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain loved cats.").build()) + .build()) + .build(); + + deIdentifyTableConditionInfoTypes(projectId, tableToDeIdentify); + } + + public static Table deIdentifyTableConditionInfoTypes(String projectId, Table tableToDeIdentify) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify what content you want the service to de-identify. + ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build(); + + // Specify how the content should be de-identified. + // Select type of info to be replaced. + InfoType infoType = InfoType.newBuilder().setName("PERSON_NAME").build(); + // Specify that findings should be replaced with corresponding info type name. + ReplaceWithInfoTypeConfig replaceWithInfoTypeConfig = + ReplaceWithInfoTypeConfig.getDefaultInstance(); + PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder() + .setReplaceWithInfoTypeConfig(replaceWithInfoTypeConfig).build(); + // Associate info type with the replacement strategy + InfoTypeTransformation infoTypeTransformation = + InfoTypeTransformation.newBuilder() + .addInfoTypes(infoType) + .setPrimitiveTransformation(primitiveTransformation) + .build(); + InfoTypeTransformations infoTypeTransformations = + InfoTypeTransformations.newBuilder() + .addTransformations(infoTypeTransformation) + .build(); + + // Specify fields to be de-identified. + List fieldIds = Stream.of("PATIENT", "FACTOID") + .map(id -> FieldId.newBuilder().setName(id).build()) + .collect(Collectors.toList()); + + // Specify when the above fields should be de-identified. + Condition condition = Condition.newBuilder() + .setField(FieldId.newBuilder().setName("AGE").build()) + .setOperator(RelationalOperator.GREATER_THAN) + .setValue(Value.newBuilder().setIntegerValue(89).build()) + .build(); + // Apply the condition to records + RecordCondition recordCondition = RecordCondition.newBuilder() + .setExpressions(Expressions.newBuilder() + .setConditions(Conditions.newBuilder() + .addConditions(condition) + .build()) + .build()) + .build(); + + // Associate the de-identification and conditions with the specified fields. + FieldTransformation fieldTransformation = + FieldTransformation.newBuilder() + .setInfoTypeTransformations(infoTypeTransformations) + .addAllFields(fieldIds) + .setCondition(recordCondition) + .build(); + RecordTransformations transformations = + RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build(); + + DeidentifyConfig deidentifyConfig = + DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build(); + + // Combine configurations into a request for the service. + DeidentifyContentRequest request = + DeidentifyContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(contentItem) + .setDeidentifyConfig(deidentifyConfig) + .build(); + + // Send the request and receive response from the service. + DeidentifyContentResponse response = dlp.deidentifyContent(request); + + // Print the results. + System.out.println( + "Table after de-identification: " + response.getItem().getTable()); + + return response.getItem().getTable(); + } + } +} +// [END dlp_deidentify_table_condition_infotypes] diff --git a/dlp/src/main/java/dlp/snippets/DeIdentifyTableConditionMasking.java b/dlp/src/main/java/dlp/snippets/DeIdentifyTableConditionMasking.java new file mode 100644 index 00000000000..71083fe6276 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/DeIdentifyTableConditionMasking.java @@ -0,0 +1,140 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_deidentify_table_condition_masking] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.CharacterMaskConfig; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.DeidentifyConfig; +import com.google.privacy.dlp.v2.DeidentifyContentRequest; +import com.google.privacy.dlp.v2.DeidentifyContentResponse; +import com.google.privacy.dlp.v2.FieldId; +import com.google.privacy.dlp.v2.FieldTransformation; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.PrimitiveTransformation; +import com.google.privacy.dlp.v2.RecordCondition; +import com.google.privacy.dlp.v2.RecordCondition.Condition; +import com.google.privacy.dlp.v2.RecordCondition.Conditions; +import com.google.privacy.dlp.v2.RecordCondition.Expressions; +import com.google.privacy.dlp.v2.RecordTransformations; +import com.google.privacy.dlp.v2.RelationalOperator; +import com.google.privacy.dlp.v2.Table; +import com.google.privacy.dlp.v2.Table.Row; +import com.google.privacy.dlp.v2.Value; +import java.io.IOException; + +public class DeIdentifyTableConditionMasking { + + public static void deIdentifyTableConditionMasking() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + Table tableToDeIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .build()) + .build(); + + deIdentifyTableConditionMasking(projectId, tableToDeIdentify); + } + + public static Table deIdentifyTableConditionMasking(String projectId, Table tableToDeIdentify) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify what content you want the service to de-identify. + ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build(); + + // Specify how the content should be de-identified. + CharacterMaskConfig characterMaskConfig = + CharacterMaskConfig.newBuilder() + .setMaskingCharacter("*") + .build(); + PrimitiveTransformation primitiveTransformation = + PrimitiveTransformation.newBuilder() + .setCharacterMaskConfig(characterMaskConfig) + .build(); + + // Specify field to be de-identified. + FieldId fieldId = FieldId.newBuilder().setName("HAPPINESS SCORE").build(); + + // Specify when the above field should be de-identified. + Condition condition = Condition.newBuilder() + .setField(FieldId.newBuilder().setName("AGE").build()) + .setOperator(RelationalOperator.GREATER_THAN) + .setValue(Value.newBuilder().setIntegerValue(89).build()) + .build(); + // Apply the condition to records + RecordCondition recordCondition = RecordCondition.newBuilder() + .setExpressions(Expressions.newBuilder() + .setConditions(Conditions.newBuilder() + .addConditions(condition) + .build()) + .build()) + .build(); + + // Associate the de-identification and conditions with the specified field. + FieldTransformation fieldTransformation = + FieldTransformation.newBuilder() + .setPrimitiveTransformation(primitiveTransformation) + .addFields(fieldId) + .setCondition(recordCondition) + .build(); + RecordTransformations transformations = + RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build(); + + DeidentifyConfig deidentifyConfig = + DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build(); + + // Combine configurations into a request for the service. + DeidentifyContentRequest request = + DeidentifyContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(contentItem) + .setDeidentifyConfig(deidentifyConfig) + .build(); + + // Send the request and receive response from the service. + DeidentifyContentResponse response = dlp.deidentifyContent(request); + + // Print the results. + System.out.println( + "Table after de-identification: " + response.getItem().getTable()); + + return response.getItem().getTable(); + } + } +} +// [END dlp_deidentify_table_condition_masking] diff --git a/dlp/src/main/java/dlp/snippets/DeIdentifyTableInfoTypes.java b/dlp/src/main/java/dlp/snippets/DeIdentifyTableInfoTypes.java new file mode 100644 index 00000000000..56070bdb678 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/DeIdentifyTableInfoTypes.java @@ -0,0 +1,142 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_deidentify_table_infotypes] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.DeidentifyConfig; +import com.google.privacy.dlp.v2.DeidentifyContentRequest; +import com.google.privacy.dlp.v2.DeidentifyContentResponse; +import com.google.privacy.dlp.v2.FieldId; +import com.google.privacy.dlp.v2.FieldTransformation; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InfoTypeTransformations; +import com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.PrimitiveTransformation; +import com.google.privacy.dlp.v2.RecordTransformations; +import com.google.privacy.dlp.v2.ReplaceWithInfoTypeConfig; +import com.google.privacy.dlp.v2.Table; +import com.google.privacy.dlp.v2.Table.Row; +import com.google.privacy.dlp.v2.Value; +import java.io.IOException; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class DeIdentifyTableInfoTypes { + + public static void deIdentifyTableInfoTypes() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + Table tableToDeIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addHeaders(FieldId.newBuilder().setName("FACTOID").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .addValues(Value.newBuilder().setStringValue( + "Charles Dickens name was a curse, possibly invented by Shakespeare.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .addValues(Value.newBuilder().setStringValue( + "There are 14 kisses in Jane Austen's novels.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain loved cats.").build()) + .build()) + .build(); + + deIdentifyTableInfoTypes(projectId, tableToDeIdentify); + } + + public static Table deIdentifyTableInfoTypes(String projectId, Table tableToDeIdentify) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify what content you want the service to de-identify. + ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build(); + + // Specify how the content should be de-identified. + // Select type of info to be replaced. + InfoType infoType = InfoType.newBuilder().setName("PERSON_NAME").build(); + // Specify that findings should be replaced with corresponding info type name. + ReplaceWithInfoTypeConfig replaceWithInfoTypeConfig = + ReplaceWithInfoTypeConfig.getDefaultInstance(); + PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder() + .setReplaceWithInfoTypeConfig(replaceWithInfoTypeConfig).build(); + // Associate info type with the replacement strategy + InfoTypeTransformation infoTypeTransformation = + InfoTypeTransformation.newBuilder() + .addInfoTypes(infoType) + .setPrimitiveTransformation(primitiveTransformation) + .build(); + InfoTypeTransformations infoTypeTransformations = + InfoTypeTransformations.newBuilder() + .addTransformations(infoTypeTransformation) + .build(); + + // Specify fields to be de-identified. + List fieldIds = Stream.of("PATIENT", "FACTOID") + .map(id -> FieldId.newBuilder().setName(id).build()) + .collect(Collectors.toList()); + + // Associate the de-identification and conditions with the specified field. + FieldTransformation fieldTransformation = + FieldTransformation.newBuilder() + .setInfoTypeTransformations(infoTypeTransformations) + .addAllFields(fieldIds) + .build(); + RecordTransformations transformations = + RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build(); + + DeidentifyConfig deidentifyConfig = + DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build(); + + // Combine configurations into a request for the service. + DeidentifyContentRequest request = + DeidentifyContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(contentItem) + .setDeidentifyConfig(deidentifyConfig) + .build(); + + // Send the request and receive response from the service. + DeidentifyContentResponse response = dlp.deidentifyContent(request); + + // Print the results. + System.out.println( + "Table after de-identification: " + response.getItem().getTable()); + + return response.getItem().getTable(); + } + } +} +// [END dlp_deidentify_table_infotypes] diff --git a/dlp/src/main/java/dlp/snippets/DeIdentifyTableRowSuppress.java b/dlp/src/main/java/dlp/snippets/DeIdentifyTableRowSuppress.java new file mode 100644 index 00000000000..db573ee3b9f --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/DeIdentifyTableRowSuppress.java @@ -0,0 +1,120 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_deidentify_table_row_suppress] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.DeidentifyConfig; +import com.google.privacy.dlp.v2.DeidentifyContentRequest; +import com.google.privacy.dlp.v2.DeidentifyContentResponse; +import com.google.privacy.dlp.v2.FieldId; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.RecordCondition; +import com.google.privacy.dlp.v2.RecordCondition.Condition; +import com.google.privacy.dlp.v2.RecordCondition.Conditions; +import com.google.privacy.dlp.v2.RecordCondition.Expressions; +import com.google.privacy.dlp.v2.RecordSuppression; +import com.google.privacy.dlp.v2.RecordTransformations; +import com.google.privacy.dlp.v2.RelationalOperator; +import com.google.privacy.dlp.v2.Table; +import com.google.privacy.dlp.v2.Table.Row; +import com.google.privacy.dlp.v2.Value; +import java.io.IOException; + +public class DeIdentifyTableRowSuppress { + + public static void deIdentifyTableRowSuppress() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + Table tableToDeIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .build()) + .build(); + + deIdentifyTableRowSuppress(projectId, tableToDeIdentify); + } + + public static Table deIdentifyTableRowSuppress(String projectId, Table tableToDeIdentify) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify what content you want the service to de-identify. + ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build(); + + // Specify when the content should be de-identified. + Condition condition = Condition.newBuilder() + .setField(FieldId.newBuilder().setName("AGE").build()) + .setOperator(RelationalOperator.GREATER_THAN) + .setValue(Value.newBuilder().setIntegerValue(89).build()).build(); + // Apply the condition to record suppression. + RecordSuppression recordSuppressions = + RecordSuppression.newBuilder() + .setCondition(RecordCondition.newBuilder() + .setExpressions(Expressions.newBuilder() + .setConditions(Conditions.newBuilder().addConditions(condition).build()) + .build()) + .build()) + .build(); + // Use record suppression as the only transformation + RecordTransformations transformations = + RecordTransformations.newBuilder() + .addRecordSuppressions(recordSuppressions) + .build(); + + DeidentifyConfig deidentifyConfig = + DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build(); + + // Combine configurations into a request for the service. + DeidentifyContentRequest request = + DeidentifyContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(contentItem) + .setDeidentifyConfig(deidentifyConfig) + .build(); + + // Send the request and receive response from the service. + DeidentifyContentResponse response = dlp.deidentifyContent(request); + + // Print the results. + System.out.println( + "Table after de-identification: " + response.getItem().getTable()); + + return response.getItem().getTable(); + } + } +} +// [END dlp_deidentify_table_row_suppress] diff --git a/dlp/src/main/java/dlp/snippets/DeIdentifyTableWithFpe.java b/dlp/src/main/java/dlp/snippets/DeIdentifyTableWithFpe.java new file mode 100644 index 00000000000..0d57d368ff4 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/DeIdentifyTableWithFpe.java @@ -0,0 +1,144 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_deidentify_table_fpe] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.common.io.BaseEncoding; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.CryptoKey; +import com.google.privacy.dlp.v2.CryptoReplaceFfxFpeConfig; +import com.google.privacy.dlp.v2.CryptoReplaceFfxFpeConfig.FfxCommonNativeAlphabet; +import com.google.privacy.dlp.v2.DeidentifyConfig; +import com.google.privacy.dlp.v2.DeidentifyContentRequest; +import com.google.privacy.dlp.v2.DeidentifyContentResponse; +import com.google.privacy.dlp.v2.FieldId; +import com.google.privacy.dlp.v2.FieldTransformation; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InfoTypeTransformations; +import com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.KmsWrappedCryptoKey; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.PrimitiveTransformation; +import com.google.privacy.dlp.v2.RecordTransformations; +import com.google.privacy.dlp.v2.Table; +import com.google.privacy.dlp.v2.Table.Row; +import com.google.privacy.dlp.v2.Value; +import com.google.protobuf.ByteString; +import java.io.IOException; +import java.util.Arrays; + +public class DeIdentifyTableWithFpe { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String kmsKeyName = + "projects/YOUR_PROJECT/" + + "locations/YOUR_KEYRING_REGION/" + + "keyRings/YOUR_KEYRING_NAME/" + + "cryptoKeys/YOUR_KEY_NAME"; + String wrappedAesKey = "YOUR_ENCRYPTED_AES_256_KEY"; + Table tableToDeIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("Employee ID").build()) + .addHeaders(FieldId.newBuilder().setName("Date").build()) + .addHeaders(FieldId.newBuilder().setName("Compensation").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("11111").build()) + .addValues(Value.newBuilder().setStringValue("2015").build()) + .addValues(Value.newBuilder().setStringValue("$10").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("11111").build()) + .addValues(Value.newBuilder().setStringValue("2016").build()) + .addValues(Value.newBuilder().setStringValue("$20").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22222").build()) + .addValues(Value.newBuilder().setStringValue("2016").build()) + .addValues(Value.newBuilder().setStringValue("$15").build()) + .build()) + .build(); + deIdentifyTableWithFpe(projectId, tableToDeIdentify, kmsKeyName, wrappedAesKey); + } + + public static void deIdentifyTableWithFpe( + String projectId, Table tableToDeIdentify, String kmsKeyName, String wrappedAesKey) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify what content you want the service to de-identify. + ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build(); + + // Specify an encrypted AES-256 key and the name of the Cloud KMS key that encrypted it + KmsWrappedCryptoKey kmsWrappedCryptoKey = + KmsWrappedCryptoKey.newBuilder() + .setWrappedKey(ByteString.copyFrom(BaseEncoding.base64().decode(wrappedAesKey))) + .setCryptoKeyName(kmsKeyName) + .build(); + CryptoKey cryptoKey = CryptoKey.newBuilder().setKmsWrapped(kmsWrappedCryptoKey).build(); + + // Specify how the content should be encrypted. + CryptoReplaceFfxFpeConfig cryptoReplaceFfxFpeConfig = + CryptoReplaceFfxFpeConfig.newBuilder() + .setCryptoKey(cryptoKey) + // Set of characters in the input text. For more info, see + // https://cloud.google.com/dlp/docs/reference/rest/v2/organizations.deidentifyTemplates#DeidentifyTemplate.FfxCommonNativeAlphabet + .setCommonAlphabet(FfxCommonNativeAlphabet.NUMERIC) + .build(); + PrimitiveTransformation primitiveTransformation = + PrimitiveTransformation.newBuilder() + .setCryptoReplaceFfxFpeConfig(cryptoReplaceFfxFpeConfig) + .build(); + + // Specify field to be encrypted. + FieldId fieldId = FieldId.newBuilder().setName("Employee ID").build(); + + // Associate the encryption with the specified field. + FieldTransformation fieldTransformation = + FieldTransformation.newBuilder() + .setPrimitiveTransformation(primitiveTransformation) + .addFields(fieldId) + .build(); + RecordTransformations transformations = + RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build(); + + DeidentifyConfig deidentifyConfig = + DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build(); + + // Combine configurations into a request for the service. + DeidentifyContentRequest request = + DeidentifyContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(contentItem) + .setDeidentifyConfig(deidentifyConfig) + .build(); + + // Send the request and receive response from the service. + DeidentifyContentResponse response = dlp.deidentifyContent(request); + + // Print the results. + System.out.println( + "Table after format-preserving encryption: " + response.getItem().getTable()); + } + } +} +// [END dlp_deidentify_table_fpe] diff --git a/dlp/src/main/java/dlp/snippets/DeIdentifyTextWithFpe.java b/dlp/src/main/java/dlp/snippets/DeIdentifyTextWithFpe.java new file mode 100644 index 00000000000..8922db0b471 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/DeIdentifyTextWithFpe.java @@ -0,0 +1,122 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_deidentify_text_fpe] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.common.io.BaseEncoding; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.CryptoKey; +import com.google.privacy.dlp.v2.CryptoReplaceFfxFpeConfig; +import com.google.privacy.dlp.v2.CryptoReplaceFfxFpeConfig.FfxCommonNativeAlphabet; +import com.google.privacy.dlp.v2.DeidentifyConfig; +import com.google.privacy.dlp.v2.DeidentifyContentRequest; +import com.google.privacy.dlp.v2.DeidentifyContentResponse; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InfoTypeTransformations; +import com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.KmsWrappedCryptoKey; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.PrimitiveTransformation; +import com.google.protobuf.ByteString; +import java.io.IOException; +import java.util.Arrays; + +public class DeIdentifyTextWithFpe { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String textToDeIdentify = "I'm Gary and my email is gary@example.com"; + String kmsKeyName = + "projects/YOUR_PROJECT/" + + "locations/YOUR_KEYRING_REGION/" + + "keyRings/YOUR_KEYRING_NAME/" + + "cryptoKeys/YOUR_KEY_NAME"; + String wrappedAesKey = "YOUR_ENCRYPTED_AES_256_KEY"; + deIdentifyTextWithFpe(projectId, textToDeIdentify, kmsKeyName, wrappedAesKey); + } + + public static void deIdentifyTextWithFpe( + String projectId, String textToDeIdentify, String kmsKeyName, String wrappedAesKey) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify what content you want the service to de-identify. + ContentItem contentItem = ContentItem.newBuilder().setValue(textToDeIdentify).build(); + + // Specify the type of info you want the service to de-identify. + // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types. + InfoType infoType = InfoType.newBuilder().setName("PHONE_NUMBER").build(); + InspectConfig inspectConfig = + InspectConfig.newBuilder().addAllInfoTypes(Arrays.asList(infoType)).build(); + + // Specify an encrypted AES-256 key and the name of the Cloud KMS key that encrypted it. + KmsWrappedCryptoKey kmsWrappedCryptoKey = + KmsWrappedCryptoKey.newBuilder() + .setWrappedKey(ByteString.copyFrom(BaseEncoding.base64().decode(wrappedAesKey))) + .setCryptoKeyName(kmsKeyName) + .build(); + CryptoKey cryptoKey = CryptoKey.newBuilder().setKmsWrapped(kmsWrappedCryptoKey).build(); + + // Specify how the info from the inspection should be encrypted. + InfoType surrogateInfoType = InfoType.newBuilder().setName("PHONE_TOKEN").build(); + CryptoReplaceFfxFpeConfig cryptoReplaceFfxFpeConfig = + CryptoReplaceFfxFpeConfig.newBuilder() + .setCryptoKey(cryptoKey) + // Set of characters in the input text. For more info, see + // https://cloud.google.com/dlp/docs/reference/rest/v2/organizations.deidentifyTemplates#DeidentifyTemplate.FfxCommonNativeAlphabet + .setCommonAlphabet(FfxCommonNativeAlphabet.NUMERIC) + .setSurrogateInfoType(surrogateInfoType) + .build(); + PrimitiveTransformation primitiveTransformation = + PrimitiveTransformation.newBuilder() + .setCryptoReplaceFfxFpeConfig(cryptoReplaceFfxFpeConfig) + .build(); + InfoTypeTransformation infoTypeTransformation = + InfoTypeTransformation.newBuilder() + .setPrimitiveTransformation(primitiveTransformation) + .build(); + InfoTypeTransformations transformations = + InfoTypeTransformations.newBuilder().addTransformations(infoTypeTransformation).build(); + + DeidentifyConfig deidentifyConfig = + DeidentifyConfig.newBuilder().setInfoTypeTransformations(transformations).build(); + + // Combine configurations into a request for the service. + DeidentifyContentRequest request = + DeidentifyContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(contentItem) + .setInspectConfig(inspectConfig) + .setDeidentifyConfig(deidentifyConfig) + .build(); + + // Send the request and receive response from the service. + DeidentifyContentResponse response = dlp.deidentifyContent(request); + + // Print the results. + System.out.println( + "Text after format-preserving encryption: " + response.getItem().getValue()); + } + } +} +// [END dlp_deidentify_text_fpe] diff --git a/dlp/src/main/java/dlp/snippets/DeIdentifyWithDateShift.java b/dlp/src/main/java/dlp/snippets/DeIdentifyWithDateShift.java index 1e7cf9aca7f..abbc2dae5c3 100644 --- a/dlp/src/main/java/dlp/snippets/DeIdentifyWithDateShift.java +++ b/dlp/src/main/java/dlp/snippets/DeIdentifyWithDateShift.java @@ -27,8 +27,8 @@ import com.google.privacy.dlp.v2.DeidentifyContentResponse; import com.google.privacy.dlp.v2.FieldId; import com.google.privacy.dlp.v2.FieldTransformation; +import com.google.privacy.dlp.v2.LocationName; import com.google.privacy.dlp.v2.PrimitiveTransformation; -import com.google.privacy.dlp.v2.ProjectName; import com.google.privacy.dlp.v2.RecordTransformations; import com.google.privacy.dlp.v2.Table; import com.google.privacy.dlp.v2.Value; @@ -47,7 +47,7 @@ public class DeIdentifyWithDateShift { - public static void deIdentifyWithDateShift() throws IOException { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; Path inputCsvFile = Paths.get("path/to/your/input/file.csv"); @@ -98,7 +98,7 @@ public static void deIdentifyWithDateShift( // Combine configurations into a request for the service. DeidentifyContentRequest request = DeidentifyContentRequest.newBuilder() - .setParent(ProjectName.of(projectId).toString()) + .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setDeidentifyConfig(deidentifyConfig) .build(); diff --git a/dlp/src/main/java/dlp/snippets/DeIdentifyWithExceptionList.java b/dlp/src/main/java/dlp/snippets/DeIdentifyWithExceptionList.java new file mode 100644 index 00000000000..42c5c25d2d8 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/DeIdentifyWithExceptionList.java @@ -0,0 +1,118 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_deidentify_exception_list] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.CustomInfoType; +import com.google.privacy.dlp.v2.CustomInfoType.Dictionary; +import com.google.privacy.dlp.v2.CustomInfoType.Dictionary.WordList; +import com.google.privacy.dlp.v2.DeidentifyConfig; +import com.google.privacy.dlp.v2.DeidentifyContentRequest; +import com.google.privacy.dlp.v2.DeidentifyContentResponse; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InfoTypeTransformations; +import com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.PrimitiveTransformation; +import com.google.privacy.dlp.v2.ReplaceWithInfoTypeConfig; +import java.io.IOException; + +public class DeIdentifyWithExceptionList { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String textToDeIdentify = "jack@example.org accessed customer record of user5@example.com"; + deIdentifyWithExceptionList(projectId, textToDeIdentify); + } + + public static void deIdentifyWithExceptionList(String projectId, String textToDeIdentify) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + + // Specify what content you want the service to DeIdentify. + ContentItem contentItem = ContentItem.newBuilder().setValue(textToDeIdentify).build(); + + // Construct the custom word list to be detected. + Dictionary wordList = + Dictionary.newBuilder() + .setWordList( + WordList.newBuilder() + .addWords("jack@example.org") + .addWords("jill@example.org") + .build()) + .build(); + + // Construct the custom dictionary detector associated with the word list. + InfoType developerEmail = InfoType.newBuilder().setName("DEVELOPER_EMAIL").build(); + CustomInfoType customInfoType = + CustomInfoType.newBuilder().setInfoType(developerEmail).setDictionary(wordList).build(); + + // Specify the word list custom info type and build-in info type the inspection will look for. + InfoType emailAddress = InfoType.newBuilder().setName("EMAIL_ADDRESS").build(); + InspectConfig inspectConfig = + InspectConfig.newBuilder() + .addInfoTypes(emailAddress) + .addCustomInfoTypes(customInfoType) + .build(); + + // Define type of deidentification as replacement. + PrimitiveTransformation primitiveTransformation = + PrimitiveTransformation.newBuilder() + .setReplaceWithInfoTypeConfig(ReplaceWithInfoTypeConfig.getDefaultInstance()) + .build(); + + // Associate de-identification type with info type. + InfoTypeTransformation transformation = + InfoTypeTransformation.newBuilder() + .addInfoTypes(emailAddress) + .setPrimitiveTransformation(primitiveTransformation) + .build(); + + // Construct the configuration for the de-id request and list all desired transformations. + DeidentifyConfig deidentifyConfig = + DeidentifyConfig.newBuilder() + .setInfoTypeTransformations( + InfoTypeTransformations.newBuilder().addTransformations(transformation)) + .build(); + + // Combine configurations into a request for the service. + DeidentifyContentRequest request = + DeidentifyContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(contentItem) + .setInspectConfig(inspectConfig) + .setDeidentifyConfig(deidentifyConfig) + .build(); + + // Send the request and receive response from the service + DeidentifyContentResponse response = dlp.deidentifyContent(request); + + // Print the results + System.out.println( + "Text after replace with infotype config: " + response.getItem().getValue()); + } + } +} +// [END dlp_deidentify_exception_list] diff --git a/dlp/src/main/java/dlp/snippets/DeIdentifyWithFpe.java b/dlp/src/main/java/dlp/snippets/DeIdentifyWithFpe.java index a82215fb3b5..bb947c9a677 100644 --- a/dlp/src/main/java/dlp/snippets/DeIdentifyWithFpe.java +++ b/dlp/src/main/java/dlp/snippets/DeIdentifyWithFpe.java @@ -32,15 +32,15 @@ import com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation; import com.google.privacy.dlp.v2.InspectConfig; import com.google.privacy.dlp.v2.KmsWrappedCryptoKey; +import com.google.privacy.dlp.v2.LocationName; import com.google.privacy.dlp.v2.PrimitiveTransformation; -import com.google.privacy.dlp.v2.ProjectName; import com.google.protobuf.ByteString; import java.io.IOException; import java.util.Arrays; public class DeIdentifyWithFpe { - public static void deIdentifyWithFpe() throws IOException { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String textToDeIdentify = "I'm Gary and my email is gary@example.com"; @@ -104,7 +104,7 @@ public static void deIdentifyWithFpe( // Combine configurations into a request for the service. DeidentifyContentRequest request = DeidentifyContentRequest.newBuilder() - .setParent(ProjectName.of(projectId).toString()) + .setParent(LocationName.of(projectId, "global").toString()) .setItem(contentItem) .setInspectConfig(inspectConfig) .setDeidentifyConfig(deidentifyConfig) diff --git a/dlp/src/main/java/dlp/snippets/DeIdentifyWithInfoType.java b/dlp/src/main/java/dlp/snippets/DeIdentifyWithInfoType.java new file mode 100644 index 00000000000..0899c2bbf1c --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/DeIdentifyWithInfoType.java @@ -0,0 +1,94 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_deidentify_infotype] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.DeidentifyConfig; +import com.google.privacy.dlp.v2.DeidentifyContentRequest; +import com.google.privacy.dlp.v2.DeidentifyContentResponse; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InfoTypeTransformations; +import com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.PrimitiveTransformation; +import com.google.privacy.dlp.v2.ReplaceWithInfoTypeConfig; +import java.io.IOException; + +public class DeIdentifyWithInfoType { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String textToInspect = + "My email is test@example.com"; + deIdentifyWithInfoType(projectId, textToInspect); + } + + public static void deIdentifyWithInfoType(String projectId, String textToRedact) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the content to be inspected. + ContentItem item = ContentItem.newBuilder() + .setValue(textToRedact).build(); + + // Specify the type of info the inspection will look for. + // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types + InfoType infoType = InfoType.newBuilder().setName("EMAIL_ADDRESS").build(); + InspectConfig inspectConfig = InspectConfig.newBuilder().addInfoTypes(infoType).build(); + // Specify replacement string to be used for the finding. + ReplaceWithInfoTypeConfig replaceWithInfoTypeConfig = + ReplaceWithInfoTypeConfig.newBuilder().build(); + // Define type of deidentification as replacement with info type. + PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder() + .setReplaceWithInfoTypeConfig(replaceWithInfoTypeConfig) + .build(); + // Associate deidentification type with info type. + InfoTypeTransformation transformation = InfoTypeTransformation.newBuilder() + .addInfoTypes(infoType) + .setPrimitiveTransformation(primitiveTransformation) + .build(); + // Construct the configuration for the Redact request and list all desired transformations. + DeidentifyConfig redactConfig = DeidentifyConfig.newBuilder() + .setInfoTypeTransformations(InfoTypeTransformations.newBuilder() + .addTransformations(transformation)) + .build(); + + // Construct the Redact request to be sent by the client. + DeidentifyContentRequest request = + DeidentifyContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(item) + .setDeidentifyConfig(redactConfig) + .setInspectConfig(inspectConfig) + .build(); + + // Use the client to send the API request. + DeidentifyContentResponse response = dlp.deidentifyContent(request); + + // Parse the response and process results + System.out.println("Text after redaction: " + response.getItem().getValue()); + } + } +} +// [END dlp_deidentify_replace] diff --git a/dlp/src/main/java/dlp/snippets/DeIdentifyWithMasking.java b/dlp/src/main/java/dlp/snippets/DeIdentifyWithMasking.java index 1449c635006..5d387688c0a 100644 --- a/dlp/src/main/java/dlp/snippets/DeIdentifyWithMasking.java +++ b/dlp/src/main/java/dlp/snippets/DeIdentifyWithMasking.java @@ -28,15 +28,15 @@ import com.google.privacy.dlp.v2.InfoTypeTransformations; import com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation; import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.LocationName; import com.google.privacy.dlp.v2.PrimitiveTransformation; -import com.google.privacy.dlp.v2.ProjectName; import com.google.privacy.dlp.v2.ReplaceWithInfoTypeConfig; import java.io.IOException; import java.util.Arrays; public class DeIdentifyWithMasking { - public static void deIdentifyWithMasking() throws IOException { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String textToDeIdentify = "My SSN is 372819127"; @@ -82,7 +82,7 @@ public static void deIdentifyWithMasking(String projectId, String textToDeIdenti // Combine configurations into a request for the service. DeidentifyContentRequest request = DeidentifyContentRequest.newBuilder() - .setParent(ProjectName.of(projectId).toString()) + .setParent(LocationName.of(projectId, "global").toString()) .setItem(contentItem) .setInspectConfig(inspectConfig) .setDeidentifyConfig(deidentifyConfig) diff --git a/dlp/src/main/java/dlp/snippets/DeIdentifyWithRedaction.java b/dlp/src/main/java/dlp/snippets/DeIdentifyWithRedaction.java new file mode 100644 index 00000000000..a4fca89df70 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/DeIdentifyWithRedaction.java @@ -0,0 +1,92 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_deidentify_redact] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.DeidentifyConfig; +import com.google.privacy.dlp.v2.DeidentifyContentRequest; +import com.google.privacy.dlp.v2.DeidentifyContentResponse; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InfoTypeTransformations; +import com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.PrimitiveTransformation; +import com.google.privacy.dlp.v2.RedactConfig; + +public class DeIdentifyWithRedaction { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String textToInspect = + "My name is Alicia Abernathy, and my email address is aabernathy@example.com."; + deIdentifyWithRedaction(projectId, textToInspect); + } + + // Inspects the provided text. + public static void deIdentifyWithRedaction(String projectId, String textToRedact) { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the content to be inspected. + ContentItem item = ContentItem.newBuilder() + .setValue(textToRedact).build(); + + // Specify the type of info the inspection will look for. + // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types + InfoType infoType = InfoType.newBuilder().setName("EMAIL_ADDRESS").build(); + InspectConfig inspectConfig = InspectConfig.newBuilder().addInfoTypes(infoType).build(); + // Define type of deidentification. + PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder() + .setRedactConfig(RedactConfig.getDefaultInstance()) + .build(); + // Associate deidentification type with info type. + InfoTypeTransformation transformation = InfoTypeTransformation.newBuilder() + .addInfoTypes(infoType) + .setPrimitiveTransformation(primitiveTransformation) + .build(); + // Construct the configuration for the Redact request and list all desired transformations. + DeidentifyConfig redactConfig = DeidentifyConfig.newBuilder() + .setInfoTypeTransformations(InfoTypeTransformations.newBuilder() + .addTransformations(transformation)) + .build(); + + // Construct the Redact request to be sent by the client. + DeidentifyContentRequest request = + DeidentifyContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(item) + .setDeidentifyConfig(redactConfig) + .setInspectConfig(inspectConfig) + .build(); + + // Use the client to send the API request. + DeidentifyContentResponse response = dlp.deidentifyContent(request); + + // Parse the response and process results + System.out.println("Text after redaction: " + response.getItem().getValue()); + } catch (Exception e) { + System.out.println("Error during inspectString: \n" + e.toString()); + } + } +} +// [END dlp_deidentify_redact] diff --git a/dlp/src/main/java/dlp/snippets/DeIdentifyWithReplacement.java b/dlp/src/main/java/dlp/snippets/DeIdentifyWithReplacement.java new file mode 100644 index 00000000000..824bd8d3db3 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/DeIdentifyWithReplacement.java @@ -0,0 +1,98 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_deidentify_replace] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.DeidentifyConfig; +import com.google.privacy.dlp.v2.DeidentifyContentRequest; +import com.google.privacy.dlp.v2.DeidentifyContentResponse; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InfoTypeTransformations; +import com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.PrimitiveTransformation; +import com.google.privacy.dlp.v2.RedactConfig; +import com.google.privacy.dlp.v2.ReplaceValueConfig; +import com.google.privacy.dlp.v2.Value; + +public class DeIdentifyWithReplacement { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String textToInspect = + "My name is Alicia Abernathy, and my email address is aabernathy@example.com."; + deIdentifyWithReplacement(projectId, textToInspect); + } + + // Inspects the provided text. + public static void deIdentifyWithReplacement(String projectId, String textToRedact) { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the content to be inspected. + ContentItem item = ContentItem.newBuilder() + .setValue(textToRedact).build(); + + // Specify the type of info the inspection will look for. + // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types + InfoType infoType = InfoType.newBuilder().setName("EMAIL_ADDRESS").build(); + InspectConfig inspectConfig = InspectConfig.newBuilder().addInfoTypes(infoType).build(); + // Specify replacement string to be used for the finding. + ReplaceValueConfig replaceValueConfig = ReplaceValueConfig.newBuilder() + .setNewValue(Value.newBuilder().setStringValue("[email-address]").build()) + .build(); + // Define type of deidentification as replacement. + PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder() + .setReplaceConfig(replaceValueConfig) + .build(); + // Associate deidentification type with info type. + InfoTypeTransformation transformation = InfoTypeTransformation.newBuilder() + .addInfoTypes(infoType) + .setPrimitiveTransformation(primitiveTransformation) + .build(); + // Construct the configuration for the Redact request and list all desired transformations. + DeidentifyConfig redactConfig = DeidentifyConfig.newBuilder() + .setInfoTypeTransformations(InfoTypeTransformations.newBuilder() + .addTransformations(transformation)) + .build(); + + // Construct the Redact request to be sent by the client. + DeidentifyContentRequest request = + DeidentifyContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(item) + .setDeidentifyConfig(redactConfig) + .setInspectConfig(inspectConfig) + .build(); + + // Use the client to send the API request. + DeidentifyContentResponse response = dlp.deidentifyContent(request); + + // Parse the response and process results + System.out.println("Text after redaction: " + response.getItem().getValue()); + } catch (Exception e) { + System.out.println("Error during inspectString: \n" + e.toString()); + } + } +} +// [END dlp_deidentify_replace] diff --git a/dlp/src/main/java/dlp/snippets/DeIdentifyWithSimpleWordList.java b/dlp/src/main/java/dlp/snippets/DeIdentifyWithSimpleWordList.java new file mode 100644 index 00000000000..993277a0b90 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/DeIdentifyWithSimpleWordList.java @@ -0,0 +1,113 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_deidentify_simple_word_list] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.CustomInfoType; +import com.google.privacy.dlp.v2.CustomInfoType.Dictionary; +import com.google.privacy.dlp.v2.CustomInfoType.Dictionary.WordList; +import com.google.privacy.dlp.v2.DeidentifyConfig; +import com.google.privacy.dlp.v2.DeidentifyContentRequest; +import com.google.privacy.dlp.v2.DeidentifyContentResponse; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InfoTypeTransformations; +import com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.PrimitiveTransformation; +import com.google.privacy.dlp.v2.ReplaceWithInfoTypeConfig; +import java.io.IOException; + +public class DeIdentifyWithSimpleWordList { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String textToDeIdentify = "Patient was seen in RM-YELLOW then transferred to rm green."; + deidentifyWithSimpleWordList(projectId, textToDeIdentify); + } + + public static void deidentifyWithSimpleWordList(String projectId, String textToDeIdentify) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + + // Specify what content you want the service to DeIdentify. + ContentItem contentItem = ContentItem.newBuilder().setValue(textToDeIdentify).build(); + + // Construct the word list to be detected + Dictionary wordList = + Dictionary.newBuilder() + .setWordList( + WordList.newBuilder() + .addWords("RM-GREEN") + .addWords("RM-YELLOW") + .addWords("RM-ORANGE") + .build()) + .build(); + + // Specify the word list custom info type the inspection will look for. + InfoType infoType = InfoType.newBuilder().setName("CUSTOM_ROOM_ID").build(); + CustomInfoType customInfoType = + CustomInfoType.newBuilder().setInfoType(infoType).setDictionary(wordList).build(); + InspectConfig inspectConfig = + InspectConfig.newBuilder().addCustomInfoTypes(customInfoType).build(); + + // Define type of deidentification as replacement. + PrimitiveTransformation primitiveTransformation = + PrimitiveTransformation.newBuilder() + .setReplaceWithInfoTypeConfig(ReplaceWithInfoTypeConfig.getDefaultInstance()) + .build(); + + // Associate deidentification type with info type. + InfoTypeTransformation transformation = + InfoTypeTransformation.newBuilder() + .addInfoTypes(infoType) + .setPrimitiveTransformation(primitiveTransformation) + .build(); + + // Construct the configuration for the Redact request and list all desired transformations. + DeidentifyConfig deidentifyConfig = + DeidentifyConfig.newBuilder() + .setInfoTypeTransformations( + InfoTypeTransformations.newBuilder().addTransformations(transformation)) + .build(); + + // Combine configurations into a request for the service. + DeidentifyContentRequest request = + DeidentifyContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(contentItem) + .setInspectConfig(inspectConfig) + .setDeidentifyConfig(deidentifyConfig) + .build(); + + // Send the request and receive response from the service + DeidentifyContentResponse response = dlp.deidentifyContent(request); + + // Print the results + System.out.println( + "Text after replace with infotype config: " + response.getItem().getValue()); + } + } +} +// [END dlp_deidentify_simple_word_list] diff --git a/dlp/src/main/java/dlp/snippets/InfoTypesList.java b/dlp/src/main/java/dlp/snippets/InfoTypesList.java index e251aa2266d..43a304ead9a 100644 --- a/dlp/src/main/java/dlp/snippets/InfoTypesList.java +++ b/dlp/src/main/java/dlp/snippets/InfoTypesList.java @@ -26,6 +26,10 @@ public class InfoTypesList { + public static void main(String[] args) throws IOException { + listInfoTypes(); + } + // Lists the types of sensitive information the DLP API supports. public static void listInfoTypes() throws IOException { // Initialize client that will be used to send requests. This client only needs to be created diff --git a/dlp/src/main/java/dlp/snippets/InspectBigQueryTable.java b/dlp/src/main/java/dlp/snippets/InspectBigQueryTable.java index 2e7f2a968d9..032641f2609 100644 --- a/dlp/src/main/java/dlp/snippets/InspectBigQueryTable.java +++ b/dlp/src/main/java/dlp/snippets/InspectBigQueryTable.java @@ -34,7 +34,7 @@ import com.google.privacy.dlp.v2.InspectConfig; import com.google.privacy.dlp.v2.InspectDataSourceDetails; import com.google.privacy.dlp.v2.InspectJobConfig; -import com.google.privacy.dlp.v2.ProjectName; +import com.google.privacy.dlp.v2.LocationName; import com.google.privacy.dlp.v2.StorageConfig; import com.google.pubsub.v1.ProjectSubscriptionName; import com.google.pubsub.v1.PubsubMessage; @@ -48,8 +48,7 @@ public class InspectBigQueryTable { - public static void inspectBigQueryTable() - throws InterruptedException, ExecutionException, IOException { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String bigQueryDatasetId = "your-bigquery-dataset-id"; @@ -94,7 +93,10 @@ public static void inspectBigQueryTable( // Specify how the content should be inspected. InspectConfig inspectConfig = - InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setIncludeQuote(true).build(); + InspectConfig.newBuilder() + .addAllInfoTypes(infoTypes) + .setIncludeQuote(true) + .build(); // Specify the action that is triggered when the job completes. String pubSubTopic = String.format("projects/%s/topics/%s", projectId, topicId); @@ -113,7 +115,7 @@ public static void inspectBigQueryTable( // Create the request for the job configured above. CreateDlpJobRequest createDlpJobRequest = CreateDlpJobRequest.newBuilder() - .setParent(ProjectName.of(projectId).toString()) + .setParent(LocationName.of(projectId, "global").toString()) .setInspectJob(inspectJobConfig) .build(); @@ -134,12 +136,16 @@ public static void inspectBigQueryTable( Subscriber subscriber = Subscriber.newBuilder(subscriptionName, messageHandler).build(); subscriber.startAsync(); - // Wait for the original job to complete + // Wait for job completion semi-synchronously + // For long jobs, consider using a truly asynchronous execution model such as Cloud Functions try { done.get(15, TimeUnit.MINUTES); } catch (TimeoutException e) { System.out.println("Job was not completed after 15 minutes."); return; + } finally { + subscriber.stopAsync(); + subscriber.awaitTerminated(); } // Get the latest state of the job from the service @@ -156,7 +162,7 @@ public static void inspectBigQueryTable( } } } - + // handleMessage injects the job and settableFuture into the message reciever interface private static void handleMessage( DlpJob job, diff --git a/dlp/src/main/java/dlp/snippets/InspectBigQueryTableWithSampling.java b/dlp/src/main/java/dlp/snippets/InspectBigQueryTableWithSampling.java new file mode 100644 index 00000000000..a5336cdaea1 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/InspectBigQueryTableWithSampling.java @@ -0,0 +1,176 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_inspect_bigquery_with_sampling] + +import com.google.api.core.SettableApiFuture; +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.cloud.pubsub.v1.AckReplyConsumer; +import com.google.cloud.pubsub.v1.MessageReceiver; +import com.google.cloud.pubsub.v1.Subscriber; +import com.google.privacy.dlp.v2.Action; +import com.google.privacy.dlp.v2.BigQueryOptions; +import com.google.privacy.dlp.v2.BigQueryOptions.SampleMethod; +import com.google.privacy.dlp.v2.BigQueryTable; +import com.google.privacy.dlp.v2.CreateDlpJobRequest; +import com.google.privacy.dlp.v2.DlpJob; +import com.google.privacy.dlp.v2.FieldId; +import com.google.privacy.dlp.v2.GetDlpJobRequest; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InfoTypeStats; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.InspectDataSourceDetails; +import com.google.privacy.dlp.v2.InspectJobConfig; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.StorageConfig; +import com.google.pubsub.v1.ProjectSubscriptionName; +import com.google.pubsub.v1.PubsubMessage; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class InspectBigQueryTableWithSampling { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String topicId = "your-pubsub-topic-id"; + String subscriptionId = "your-pubsub-subscription-id"; + inspectBigQueryTableWithSampling(projectId, topicId, subscriptionId); + } + + // Inspects a BigQuery Table + public static void inspectBigQueryTableWithSampling( + String projectId, String topicId, String subscriptionId) + throws ExecutionException, InterruptedException, IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the BigQuery table to be inspected. + BigQueryTable tableReference = + BigQueryTable.newBuilder() + .setProjectId("bigquery-public-data") + .setDatasetId("usa_names") + .setTableId("usa_1910_current") + .build(); + + BigQueryOptions bigQueryOptions = + BigQueryOptions.newBuilder() + .setTableReference(tableReference) + .setRowsLimit(1000) + .setSampleMethod(SampleMethod.RANDOM_START) + .addIdentifyingFields(FieldId.newBuilder().setName("name")) + .build(); + + StorageConfig storageConfig = + StorageConfig.newBuilder().setBigQueryOptions(bigQueryOptions).build(); + + // Specify the type of info the inspection will look for. + // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types + InfoType infoType = InfoType.newBuilder().setName("PERSON_NAME").build(); + + // Specify how the content should be inspected. + InspectConfig inspectConfig = + InspectConfig.newBuilder() + .addInfoTypes(infoType) + .setIncludeQuote(true) + .build(); + + // Specify the action that is triggered when the job completes. + String pubSubTopic = String.format("projects/%s/topics/%s", projectId, topicId); + Action.PublishToPubSub publishToPubSub = + Action.PublishToPubSub.newBuilder().setTopic(pubSubTopic).build(); + Action action = Action.newBuilder().setPubSub(publishToPubSub).build(); + + // Configure the long running job we want the service to perform. + InspectJobConfig inspectJobConfig = + InspectJobConfig.newBuilder() + .setStorageConfig(storageConfig) + .setInspectConfig(inspectConfig) + .addActions(action) + .build(); + + // Create the request for the job configured above. + CreateDlpJobRequest createDlpJobRequest = + CreateDlpJobRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setInspectJob(inspectJobConfig) + .build(); + + // Use the client to send the request. + final DlpJob dlpJob = dlp.createDlpJob(createDlpJobRequest); + System.out.println("Job created: " + dlpJob.getName()); + + // Set up a Pub/Sub subscriber to listen on the job completion status + final SettableApiFuture done = SettableApiFuture.create(); + + ProjectSubscriptionName subscriptionName = + ProjectSubscriptionName.of(projectId, subscriptionId); + + MessageReceiver messageHandler = + (PubsubMessage pubsubMessage, AckReplyConsumer ackReplyConsumer) -> { + handleMessage(dlpJob, done, pubsubMessage, ackReplyConsumer); + }; + Subscriber subscriber = Subscriber.newBuilder(subscriptionName, messageHandler).build(); + subscriber.startAsync(); + + // Wait for job completion semi-synchronously + // For long jobs, consider using a truly asynchronous execution model such as Cloud Functions + try { + done.get(15, TimeUnit.MINUTES); + } catch (TimeoutException e) { + System.out.println("Job was not completed after 15 minutes."); + return; + } finally { + subscriber.stopAsync(); + subscriber.awaitTerminated(); + } + + // Get the latest state of the job from the service + GetDlpJobRequest request = GetDlpJobRequest.newBuilder().setName(dlpJob.getName()).build(); + DlpJob completedJob = dlp.getDlpJob(request); + + // Parse the response and process results. + System.out.println("Job status: " + completedJob.getState()); + InspectDataSourceDetails.Result result = completedJob.getInspectDetails().getResult(); + System.out.println("Findings: "); + for (InfoTypeStats infoTypeStat : result.getInfoTypeStatsList()) { + System.out.print("\tInfo type: " + infoTypeStat.getInfoType().getName()); + System.out.println("\tCount: " + infoTypeStat.getCount()); + } + } + } + + // handleMessage injects the job and settableFuture into the message reciever interface + private static void handleMessage( + DlpJob job, + SettableApiFuture done, + PubsubMessage pubsubMessage, + AckReplyConsumer ackReplyConsumer) { + String messageAttribute = pubsubMessage.getAttributesMap().get("DlpJobName"); + if (job.getName().equals(messageAttribute)) { + done.set(true); + ackReplyConsumer.ack(); + } else { + ackReplyConsumer.nack(); + } + } +} +// [END dlp_inspect_bigquery_with_sampling] diff --git a/dlp/src/main/java/dlp/snippets/InspectDatastoreEntity.java b/dlp/src/main/java/dlp/snippets/InspectDatastoreEntity.java index ef6810b49b2..c601747db44 100644 --- a/dlp/src/main/java/dlp/snippets/InspectDatastoreEntity.java +++ b/dlp/src/main/java/dlp/snippets/InspectDatastoreEntity.java @@ -34,8 +34,8 @@ import com.google.privacy.dlp.v2.InspectDataSourceDetails; import com.google.privacy.dlp.v2.InspectJobConfig; import com.google.privacy.dlp.v2.KindExpression; +import com.google.privacy.dlp.v2.LocationName; import com.google.privacy.dlp.v2.PartitionId; -import com.google.privacy.dlp.v2.ProjectName; import com.google.privacy.dlp.v2.StorageConfig; import com.google.pubsub.v1.ProjectSubscriptionName; import com.google.pubsub.v1.PubsubMessage; @@ -49,8 +49,7 @@ public class InspectDatastoreEntity { - public static void insepctDatastoreEntity() - throws InterruptedException, ExecutionException, IOException { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String datastoreNamespace = "your-datastore-namespace"; @@ -95,7 +94,10 @@ public static void insepctDatastoreEntity( // Specify how the content should be inspected. InspectConfig inspectConfig = - InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setIncludeQuote(true).build(); + InspectConfig.newBuilder() + .addAllInfoTypes(infoTypes) + .setIncludeQuote(true) + .build(); // Specify the action that is triggered when the job completes. String pubSubTopic = String.format("projects/%s/topics/%s", projectId, topicId); @@ -114,7 +116,7 @@ public static void insepctDatastoreEntity( // Create the request for the job configured above. CreateDlpJobRequest createDlpJobRequest = CreateDlpJobRequest.newBuilder() - .setParent(ProjectName.of(projectId).toString()) + .setParent(LocationName.of(projectId, "global").toString()) .setInspectJob(inspectJobConfig) .build(); @@ -135,12 +137,16 @@ public static void insepctDatastoreEntity( Subscriber subscriber = Subscriber.newBuilder(subscriptionName, messageHandler).build(); subscriber.startAsync(); - // Wait for the original job to complete + // Wait for job completion semi-synchronously + // For long jobs, consider using a truly asynchronous execution model such as Cloud Functions try { done.get(15, TimeUnit.MINUTES); } catch (TimeoutException e) { System.out.println("Job was not completed after 15 minutes."); return; + } finally { + subscriber.stopAsync(); + subscriber.awaitTerminated(); } // Get the latest state of the job from the service diff --git a/dlp/src/main/java/dlp/snippets/InspectGcsFile.java b/dlp/src/main/java/dlp/snippets/InspectGcsFile.java index 5b9a7a01618..102affcaf98 100644 --- a/dlp/src/main/java/dlp/snippets/InspectGcsFile.java +++ b/dlp/src/main/java/dlp/snippets/InspectGcsFile.java @@ -17,12 +17,12 @@ package dlp.snippets; // [START dlp_inspect_gcs] + import com.google.api.core.SettableApiFuture; import com.google.cloud.dlp.v2.DlpServiceClient; import com.google.cloud.pubsub.v1.AckReplyConsumer; import com.google.cloud.pubsub.v1.MessageReceiver; import com.google.cloud.pubsub.v1.Subscriber; -import com.google.common.util.concurrent.SettableFuture; import com.google.privacy.dlp.v2.Action; import com.google.privacy.dlp.v2.CloudStorageOptions; import com.google.privacy.dlp.v2.CloudStorageOptions.FileSet; @@ -34,7 +34,7 @@ import com.google.privacy.dlp.v2.InspectConfig; import com.google.privacy.dlp.v2.InspectDataSourceDetails; import com.google.privacy.dlp.v2.InspectJobConfig; -import com.google.privacy.dlp.v2.ProjectName; +import com.google.privacy.dlp.v2.LocationName; import com.google.privacy.dlp.v2.StorageConfig; import com.google.pubsub.v1.ProjectSubscriptionName; import com.google.pubsub.v1.PubsubMessage; @@ -48,7 +48,7 @@ public class InspectGcsFile { - public static void inspectGcsFile() throws InterruptedException, ExecutionException, IOException { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String gcsUri = "gs://" + "your-bucket-name" + "/path/to/your/file.txt"; @@ -66,9 +66,11 @@ public static void inspectGcsFile( // the "close" method on the client to safely clean up any remaining background resources. try (DlpServiceClient dlp = DlpServiceClient.create()) { // Specify the GCS file to be inspected. - FileSet fileSet = FileSet.newBuilder().setUrl(gcsUri).build(); CloudStorageOptions cloudStorageOptions = - CloudStorageOptions.newBuilder().setFileSet(fileSet).build(); + CloudStorageOptions.newBuilder() + .setFileSet(FileSet.newBuilder().setUrl(gcsUri)) + .build(); + StorageConfig storageConfig = StorageConfig.newBuilder().setCloudStorageOptions(cloudStorageOptions).build(); @@ -81,7 +83,10 @@ public static void inspectGcsFile( // Specify how the content should be inspected. InspectConfig inspectConfig = - InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setIncludeQuote(true).build(); + InspectConfig.newBuilder() + .addAllInfoTypes(infoTypes) + .setIncludeQuote(true) + .build(); // Specify the action that is triggered when the job completes. String pubSubTopic = String.format("projects/%s/topics/%s", projectId, topicId); @@ -100,7 +105,7 @@ public static void inspectGcsFile( // Create the request for the job configured above. CreateDlpJobRequest createDlpJobRequest = CreateDlpJobRequest.newBuilder() - .setParent(ProjectName.of(projectId).toString()) + .setParent(LocationName.of(projectId, "global").toString()) .setInspectJob(inspectJobConfig) .build(); @@ -121,12 +126,16 @@ public static void inspectGcsFile( Subscriber subscriber = Subscriber.newBuilder(subscriptionName, messageHandler).build(); subscriber.startAsync(); - // Wait for the original job to complete + // Wait for job completion semi-synchronously + // For long jobs, consider using a truly asynchronous execution model such as Cloud Functions try { done.get(15, TimeUnit.MINUTES); } catch (TimeoutException e) { System.out.println("Job was not completed after 15 minutes."); return; + } finally { + subscriber.stopAsync(); + subscriber.awaitTerminated(); } // Get the latest state of the job from the service diff --git a/dlp/src/main/java/dlp/snippets/InspectGcsFileWithSampling.java b/dlp/src/main/java/dlp/snippets/InspectGcsFileWithSampling.java new file mode 100644 index 00000000000..1c08e508419 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/InspectGcsFileWithSampling.java @@ -0,0 +1,174 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_inspect_gcs_with_sampling] + +import com.google.api.core.SettableApiFuture; +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.cloud.pubsub.v1.AckReplyConsumer; +import com.google.cloud.pubsub.v1.MessageReceiver; +import com.google.cloud.pubsub.v1.Subscriber; +import com.google.privacy.dlp.v2.Action; +import com.google.privacy.dlp.v2.CloudStorageOptions; +import com.google.privacy.dlp.v2.CloudStorageOptions.FileSet; +import com.google.privacy.dlp.v2.CloudStorageOptions.SampleMethod; +import com.google.privacy.dlp.v2.CreateDlpJobRequest; +import com.google.privacy.dlp.v2.DlpJob; +import com.google.privacy.dlp.v2.FileType; +import com.google.privacy.dlp.v2.GetDlpJobRequest; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InfoTypeStats; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.InspectDataSourceDetails; +import com.google.privacy.dlp.v2.InspectJobConfig; +import com.google.privacy.dlp.v2.Likelihood; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.StorageConfig; +import com.google.pubsub.v1.ProjectSubscriptionName; +import com.google.pubsub.v1.PubsubMessage; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class InspectGcsFileWithSampling { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String gcsUri = "gs://" + "your-bucket-name" + "/path/to/your/file.txt"; + String topicId = "your-pubsub-topic-id"; + String subscriptionId = "your-pubsub-subscription-id"; + inspectGcsFileWithSampling(projectId, gcsUri, topicId, subscriptionId); + } + + // Inspects a file in a Google Cloud Storage Bucket. + public static void inspectGcsFileWithSampling( + String projectId, String gcsUri, String topicId, String subscriptionId) + throws ExecutionException, InterruptedException, IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the GCS file to be inspected and sampling configuration + CloudStorageOptions cloudStorageOptions = + CloudStorageOptions.newBuilder() + .setFileSet(FileSet.newBuilder().setUrl(gcsUri)) + .setBytesLimitPerFile(200) + .addFileTypes(FileType.TEXT_FILE) + .setFilesLimitPercent(90) + .setSampleMethod(SampleMethod.RANDOM_START) + .build(); + + StorageConfig storageConfig = + StorageConfig.newBuilder().setCloudStorageOptions(cloudStorageOptions).build(); + + // Specify the type of info the inspection will look for. + // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types + InfoType infoType = InfoType.newBuilder().setName("PERSON_NAME").build(); + + // Specify how the content should be inspected. + InspectConfig inspectConfig = + InspectConfig.newBuilder() + .addInfoTypes(infoType) + .setExcludeInfoTypes(true) + .setIncludeQuote(true) + .setMinLikelihood(Likelihood.POSSIBLE) + .build(); + + // Specify the action that is triggered when the job completes. + String pubSubTopic = String.format("projects/%s/topics/%s", projectId, topicId); + Action.PublishToPubSub publishToPubSub = + Action.PublishToPubSub.newBuilder().setTopic(pubSubTopic).build(); + Action action = Action.newBuilder().setPubSub(publishToPubSub).build(); + + // Configure the long running job we want the service to perform. + InspectJobConfig inspectJobConfig = + InspectJobConfig.newBuilder() + .setStorageConfig(storageConfig) + .setInspectConfig(inspectConfig) + .addActions(action) + .build(); + + // Create the request for the job configured above. + CreateDlpJobRequest createDlpJobRequest = + CreateDlpJobRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setInspectJob(inspectJobConfig) + .build(); + + // Use the client to send the request. + final DlpJob dlpJob = dlp.createDlpJob(createDlpJobRequest); + System.out.println("Job created: " + dlpJob.getName()); + + // Set up a Pub/Sub subscriber to listen on the job completion status + final SettableApiFuture done = SettableApiFuture.create(); + + ProjectSubscriptionName subscriptionName = + ProjectSubscriptionName.of(projectId, subscriptionId); + + MessageReceiver messageHandler = + (PubsubMessage pubsubMessage, AckReplyConsumer ackReplyConsumer) -> { + handleMessage(dlpJob, done, pubsubMessage, ackReplyConsumer); + }; + Subscriber subscriber = Subscriber.newBuilder(subscriptionName, messageHandler).build(); + subscriber.startAsync(); + + // Wait for job completion semi-synchronously + // For long jobs, consider using a truly asynchronous execution model such as Cloud Functions + try { + done.get(15, TimeUnit.MINUTES); + } catch (TimeoutException e) { + System.out.println("Job was not completed after 15 minutes."); + return; + } finally { + subscriber.stopAsync(); + subscriber.awaitTerminated(); + } + + // Get the latest state of the job from the service + GetDlpJobRequest request = GetDlpJobRequest.newBuilder().setName(dlpJob.getName()).build(); + DlpJob completedJob = dlp.getDlpJob(request); + + // Parse the response and process results. + System.out.println("Job status: " + completedJob.getState()); + InspectDataSourceDetails.Result result = completedJob.getInspectDetails().getResult(); + System.out.println("Findings: "); + for (InfoTypeStats infoTypeStat : result.getInfoTypeStatsList()) { + System.out.print("\tInfo type: " + infoTypeStat.getInfoType().getName()); + System.out.println("\tCount: " + infoTypeStat.getCount()); + } + } + } + + // handleMessage injects the job and settableFuture into the message reciever interface + private static void handleMessage( + DlpJob job, + SettableApiFuture done, + PubsubMessage pubsubMessage, + AckReplyConsumer ackReplyConsumer) { + String messageAttribute = pubsubMessage.getAttributesMap().get("DlpJobName"); + if (job.getName().equals(messageAttribute)) { + done.set(true); + ackReplyConsumer.ack(); + } else { + ackReplyConsumer.nack(); + } + } +} +// [END dlp_inspect_gcs_with_sampling] diff --git a/dlp/src/main/java/dlp/snippets/InspectImageFile.java b/dlp/src/main/java/dlp/snippets/InspectImageFile.java index e4425826ecc..2103b008818 100644 --- a/dlp/src/main/java/dlp/snippets/InspectImageFile.java +++ b/dlp/src/main/java/dlp/snippets/InspectImageFile.java @@ -17,6 +17,7 @@ package dlp.snippets; // [START dlp_inspect_image_file] + import com.google.cloud.dlp.v2.DlpServiceClient; import com.google.privacy.dlp.v2.ByteContentItem; import com.google.privacy.dlp.v2.ByteContentItem.BytesType; @@ -26,15 +27,16 @@ import com.google.privacy.dlp.v2.InspectConfig; import com.google.privacy.dlp.v2.InspectContentRequest; import com.google.privacy.dlp.v2.InspectContentResponse; -import com.google.privacy.dlp.v2.ProjectName; +import com.google.privacy.dlp.v2.LocationName; import com.google.protobuf.ByteString; import java.io.FileInputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.List; public class InspectImageFile { - public static void inspectImageFile() { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String filePath = "path/to/image.png"; @@ -42,14 +44,11 @@ public static void inspectImageFile() { } // Inspects the specified image file. - public static void inspectImageFile(String projectId, String filePath) { + public static void inspectImageFile(String projectId, String filePath) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (DlpServiceClient dlp = DlpServiceClient.create()) { - // Specify the project used for request. - ProjectName project = ProjectName.of(projectId); - // Specify the type and content to be inspected. ByteString fileBytes = ByteString.readFrom(new FileInputStream(filePath)); ByteContentItem byteItem = @@ -59,18 +58,21 @@ public static void inspectImageFile(String projectId, String filePath) { // Specify the type of info the inspection will look for. List infoTypes = new ArrayList<>(); // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types - for (String typeName : new String[] {"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) { + for (String typeName : new String[]{"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) { infoTypes.add(InfoType.newBuilder().setName(typeName).build()); } // Construct the configuration for the Inspect request. InspectConfig config = - InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setIncludeQuote(true).build(); + InspectConfig.newBuilder() + .addAllInfoTypes(infoTypes) + .setIncludeQuote(true) + .build(); // Construct the Inspect request to be sent by the client. InspectContentRequest request = InspectContentRequest.newBuilder() - .setParent(project.toString()) + .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setInspectConfig(config) .build(); @@ -85,8 +87,6 @@ public static void inspectImageFile(String projectId, String filePath) { System.out.println("\tInfo type: " + f.getInfoType().getName()); System.out.println("\tLikelihood: " + f.getLikelihood()); } - } catch (Exception e) { - System.out.println("Error during inspectImageFile: \n" + e.toString()); } } } diff --git a/dlp/src/main/java/dlp/snippets/InspectImageFileAllInfoTypes.java b/dlp/src/main/java/dlp/snippets/InspectImageFileAllInfoTypes.java new file mode 100644 index 00000000000..1cb5aac931f --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/InspectImageFileAllInfoTypes.java @@ -0,0 +1,74 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_inspect_image_all_infotypes] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ByteContentItem; +import com.google.privacy.dlp.v2.ByteContentItem.BytesType; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.Finding; +import com.google.privacy.dlp.v2.InspectContentRequest; +import com.google.privacy.dlp.v2.InspectContentResponse; +import com.google.privacy.dlp.v2.LocationName; +import com.google.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.IOException; + +class InspectImageFileAllInfoTypes { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String inputPath = "src/test/resources/sensitive-data-image.jpeg"; + inspectImageFileAllInfoTypes(projectId, inputPath); + } + + static void inspectImageFileAllInfoTypes(String projectId, String inputPath) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the content to be inspected. + ByteString fileBytes = ByteString.readFrom(new FileInputStream(inputPath)); + ByteContentItem byteItem = + ByteContentItem.newBuilder().setType(BytesType.IMAGE_JPEG).setData(fileBytes).build(); + + // Construct the Inspect request to be sent by the client. + // Do not specify the type of info to inspect. + InspectContentRequest request = + InspectContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(ContentItem.newBuilder().setByteItem(byteItem).build()) + .build(); + + // Use the client to send the API request. + InspectContentResponse response = dlp.inspectContent(request); + + // Parse the response and process results. + System.out.println("Findings: " + response.getResult().getFindingsCount()); + for (Finding f : response.getResult().getFindingsList()) { + System.out.println("\tQuote: " + f.getQuote()); + System.out.println("\tInfo type: " + f.getInfoType().getName()); + System.out.println("\tLikelihood: " + f.getLikelihood()); + } + } + } +} +// [END dlp_inspect_image_all_infotypes] diff --git a/dlp/src/main/java/dlp/snippets/InspectImageFileListedInfoTypes.java b/dlp/src/main/java/dlp/snippets/InspectImageFileListedInfoTypes.java new file mode 100644 index 00000000000..86e73022a45 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/InspectImageFileListedInfoTypes.java @@ -0,0 +1,92 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_inspect_image_listed_infotypes] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ByteContentItem; +import com.google.privacy.dlp.v2.ByteContentItem.BytesType; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.Finding; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.InspectContentRequest; +import com.google.privacy.dlp.v2.InspectContentResponse; +import com.google.privacy.dlp.v2.LocationName; +import com.google.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +class InspectImageFileListedInfoTypes { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String inputPath = "src/test/resources/sensitive-data-image.jpeg"; + inspectImageFileListedInfoTypes(projectId, inputPath); + } + + static void inspectImageFileListedInfoTypes(String projectId, String inputPath) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the content to be inspected. + ByteString fileBytes = ByteString.readFrom(new FileInputStream(inputPath)); + ByteContentItem byteItem = + ByteContentItem.newBuilder().setType(BytesType.IMAGE_JPEG).setData(fileBytes).build(); + + // Specify the type of info the inspection will look for. + List infoTypes = new ArrayList<>(); + // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types + for (String typeName : + new String[] {"US_SOCIAL_SECURITY_NUMBER", "EMAIL_ADDRESS", "PHONE_NUMBER"}) { + infoTypes.add(InfoType.newBuilder().setName(typeName).build()); + } + + // Construct the configuration for the Inspect request. + InspectConfig inspectConfig = + InspectConfig.newBuilder() + .addAllInfoTypes(infoTypes) + .build(); + + // Construct the Inspect request to be sent by the client. + InspectContentRequest request = + InspectContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(ContentItem.newBuilder().setByteItem(byteItem).build()) + .setInspectConfig(inspectConfig) + .build(); + + // Use the client to send the API request. + InspectContentResponse response = dlp.inspectContent(request); + + // Parse the response and process results. + System.out.println("Findings: " + response.getResult().getFindingsCount()); + for (Finding f : response.getResult().getFindingsList()) { + System.out.println("\tQuote: " + f.getQuote()); + System.out.println("\tInfo type: " + f.getInfoType().getName()); + System.out.println("\tLikelihood: " + f.getLikelihood()); + } + } + } +} +// [END dlp_inspect_image_listed_infotypes] diff --git a/dlp/src/main/java/dlp/snippets/InspectPhoneNumber.java b/dlp/src/main/java/dlp/snippets/InspectPhoneNumber.java new file mode 100644 index 00000000000..6c21e8bd43a --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/InspectPhoneNumber.java @@ -0,0 +1,85 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_inspect_phone_number] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.Finding; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.InspectContentRequest; +import com.google.privacy.dlp.v2.InspectContentResponse; +import com.google.privacy.dlp.v2.Likelihood; +import com.google.privacy.dlp.v2.LocationName; +import java.io.IOException; + +public class InspectPhoneNumber { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String textToInspect = "My name is Gary and my email is gary@example.com"; + inspectString(projectId, textToInspect); + } + + // Inspects the provided text. + public static void inspectString(String projectId, String textToInspect) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the type and content to be inspected. + ContentItem item = ContentItem.newBuilder() + .setValue(textToInspect) + .build(); + + // Specify the type of info the inspection will look for. + // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types + InfoType infoType = InfoType.newBuilder().setName("PHONE_NUMBER").build(); + + // Construct the configuration for the Inspect request. + InspectConfig config = + InspectConfig.newBuilder() + .setIncludeQuote(true) + .setMinLikelihood(Likelihood.POSSIBLE) + .addInfoTypes(infoType) + .build(); + + // Construct the Inspect request to be sent by the client. + InspectContentRequest request = + InspectContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(item) + .setInspectConfig(config) + .build(); + + // Use the client to send the API request. + InspectContentResponse response = dlp.inspectContent(request); + + // Parse the response and process results + System.out.println("Findings: " + response.getResult().getFindingsCount()); + for (Finding f : response.getResult().getFindingsList()) { + System.out.println("\tQuote: " + f.getQuote()); + System.out.println("\tInfo type: " + f.getInfoType().getName()); + System.out.println("\tLikelihood: " + f.getLikelihood()); + } + } + } +} +// [END dlp_inspect_phone_number] diff --git a/dlp/src/main/java/dlp/snippets/InspectString.java b/dlp/src/main/java/dlp/snippets/InspectString.java index f8180b8323d..80c5040c8d3 100644 --- a/dlp/src/main/java/dlp/snippets/InspectString.java +++ b/dlp/src/main/java/dlp/snippets/InspectString.java @@ -17,6 +17,7 @@ package dlp.snippets; // [START dlp_inspect_string] + import com.google.cloud.dlp.v2.DlpServiceClient; import com.google.privacy.dlp.v2.ByteContentItem; import com.google.privacy.dlp.v2.ByteContentItem.BytesType; @@ -26,14 +27,15 @@ import com.google.privacy.dlp.v2.InspectConfig; import com.google.privacy.dlp.v2.InspectContentRequest; import com.google.privacy.dlp.v2.InspectContentResponse; -import com.google.privacy.dlp.v2.ProjectName; +import com.google.privacy.dlp.v2.LocationName; import com.google.protobuf.ByteString; +import java.io.IOException; import java.util.ArrayList; import java.util.List; public class InspectString { - public static void inspectString() { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String textToInspect = "My name is Gary and my email is gary@example.com"; @@ -41,14 +43,11 @@ public static void inspectString() { } // Inspects the provided text. - public static void inspectString(String projectId, String textToInspect) { + public static void inspectString(String projectId, String textToInspect) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (DlpServiceClient dlp = DlpServiceClient.create()) { - // Specify the project used for request. - ProjectName project = ProjectName.of(projectId); - // Specify the type and content to be inspected. ByteContentItem byteItem = ByteContentItem.newBuilder() @@ -66,12 +65,15 @@ public static void inspectString(String projectId, String textToInspect) { // Construct the configuration for the Inspect request. InspectConfig config = - InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setIncludeQuote(true).build(); + InspectConfig.newBuilder() + .addAllInfoTypes(infoTypes) + .setIncludeQuote(true) + .build(); // Construct the Inspect request to be sent by the client. InspectContentRequest request = InspectContentRequest.newBuilder() - .setParent(project.toString()) + .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setInspectConfig(config) .build(); @@ -86,8 +88,6 @@ public static void inspectString(String projectId, String textToInspect) { System.out.println("\tInfo type: " + f.getInfoType().getName()); System.out.println("\tLikelihood: " + f.getLikelihood()); } - } catch (Exception e) { - System.out.println("Error during inspectString: \n" + e.toString()); } } } diff --git a/dlp/src/main/java/dlp/snippets/InspectStringCustomExcludingSubstring.java b/dlp/src/main/java/dlp/snippets/InspectStringCustomExcludingSubstring.java new file mode 100644 index 00000000000..1201a3643f4 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/InspectStringCustomExcludingSubstring.java @@ -0,0 +1,122 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_inspect_string_custom_excluding_substring] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ByteContentItem; +import com.google.privacy.dlp.v2.ByteContentItem.BytesType; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.CustomInfoType; +import com.google.privacy.dlp.v2.CustomInfoType.Dictionary; +import com.google.privacy.dlp.v2.CustomInfoType.Dictionary.WordList; +import com.google.privacy.dlp.v2.CustomInfoType.Regex; +import com.google.privacy.dlp.v2.ExclusionRule; +import com.google.privacy.dlp.v2.Finding; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.InspectContentRequest; +import com.google.privacy.dlp.v2.InspectContentResponse; +import com.google.privacy.dlp.v2.InspectionRule; +import com.google.privacy.dlp.v2.InspectionRuleSet; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.MatchingType; +import com.google.protobuf.ByteString; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +public class InspectStringCustomExcludingSubstring { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String textToInspect = "Name: Doe, John. Name: Example, Jimmy"; + String customDetectorPattern = "[A-Z][a-z]{1,15}, [A-Z][a-z]{1,15}"; + List excludedSubstringList = Arrays.asList("Jimmy"); + inspectStringCustomExcludingSubstring(projectId, textToInspect, customDetectorPattern, + excludedSubstringList); + } + + // Inspects the provided text, avoiding matches specified in the exclusion list. + public static void inspectStringCustomExcludingSubstring(String projectId, String textToInspect, + String customDetectorPattern, List excludedSubstringList) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the type and content to be inspected. + ByteContentItem byteItem = + ByteContentItem.newBuilder() + .setType(BytesType.TEXT_UTF8) + .setData(ByteString.copyFromUtf8(textToInspect)) + .build(); + ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); + + // Specify the type of info the inspection will look for. + InfoType infoType = InfoType.newBuilder().setName("CUSTOM_NAME_DETECTOR").build(); + CustomInfoType customInfoType = + CustomInfoType.newBuilder() + .setInfoType(infoType).setRegex(Regex.newBuilder().setPattern(customDetectorPattern)) + .build(); + + // Exclude partial matches from the specified excludedSubstringList. + ExclusionRule exclusionRule = + ExclusionRule.newBuilder() + .setMatchingType(MatchingType.MATCHING_TYPE_PARTIAL_MATCH) + .setDictionary(Dictionary.newBuilder() + .setWordList(WordList.newBuilder().addAllWords(excludedSubstringList))) + .build(); + + // Construct a ruleset that applies the exclusion rule to the EMAIL_ADDRESSES infotype. + InspectionRuleSet ruleSet = + InspectionRuleSet.newBuilder() + .addInfoTypes(infoType) + .addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule)) + .build(); + + // Construct the configuration for the Inspect request, including the ruleset. + InspectConfig config = + InspectConfig.newBuilder() + .addCustomInfoTypes(customInfoType) + .setIncludeQuote(true) + .addRuleSet(ruleSet) + .build(); + + // Construct the Inspect request to be sent by the client. + InspectContentRequest request = + InspectContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(item) + .setInspectConfig(config) + .build(); + + // Use the client to send the API request. + InspectContentResponse response = dlp.inspectContent(request); + + // Parse the response and process results + System.out.println("Findings: " + response.getResult().getFindingsCount()); + for (Finding f : response.getResult().getFindingsList()) { + System.out.println("\tQuote: " + f.getQuote()); + System.out.println("\tInfo type: " + f.getInfoType().getName()); + System.out.println("\tLikelihood: " + f.getLikelihood()); + } + } + } +} +// [END dlp_inspect_string_custom_excluding_substring] diff --git a/dlp/src/main/java/dlp/snippets/InspectStringCustomHotword.java b/dlp/src/main/java/dlp/snippets/InspectStringCustomHotword.java new file mode 100644 index 00000000000..a5408f9f68e --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/InspectStringCustomHotword.java @@ -0,0 +1,112 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_inspect_string_custom_hotword] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ByteContentItem; +import com.google.privacy.dlp.v2.ByteContentItem.BytesType; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.CustomInfoType.DetectionRule.HotwordRule; +import com.google.privacy.dlp.v2.CustomInfoType.DetectionRule.LikelihoodAdjustment; +import com.google.privacy.dlp.v2.CustomInfoType.DetectionRule.Proximity; +import com.google.privacy.dlp.v2.CustomInfoType.Regex; +import com.google.privacy.dlp.v2.Finding; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.InspectContentRequest; +import com.google.privacy.dlp.v2.InspectContentResponse; +import com.google.privacy.dlp.v2.InspectionRule; +import com.google.privacy.dlp.v2.InspectionRuleSet; +import com.google.privacy.dlp.v2.Likelihood; +import com.google.privacy.dlp.v2.LocationName; +import com.google.protobuf.ByteString; +import java.io.IOException; + +public class InspectStringCustomHotword { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String textToInspect = "patient name: John Doe"; + String customHotword = "patient"; + inspectStringCustomHotword(projectId, textToInspect, customHotword); + } + + // Inspects the provided text. + public static void inspectStringCustomHotword(String projectId, String textToInspect, + String customHotword) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the type and content to be inspected. + ByteContentItem byteItem = + ByteContentItem.newBuilder() + .setType(BytesType.TEXT_UTF8) + .setData(ByteString.copyFromUtf8(textToInspect)) + .build(); + ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); + + // Increase likelihood of matches that have customHotword nearby + HotwordRule hotwordRule = + HotwordRule.newBuilder() + .setHotwordRegex(Regex.newBuilder().setPattern(customHotword)) + .setProximity(Proximity.newBuilder().setWindowBefore(50)) + .setLikelihoodAdjustment( + LikelihoodAdjustment.newBuilder().setFixedLikelihood(Likelihood.VERY_LIKELY)) + .build(); + + // Construct a ruleset that applies the hotword rule to the PERSON_NAME infotype. + InspectionRuleSet ruleSet = + InspectionRuleSet.newBuilder() + .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME").build()) + .addRules(InspectionRule.newBuilder().setHotwordRule(hotwordRule)) + .build(); + + // Construct the configuration for the Inspect request. + InspectConfig config = + InspectConfig.newBuilder() + .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME").build()) + .setIncludeQuote(true) + .addRuleSet(ruleSet) + .setMinLikelihood(Likelihood.VERY_LIKELY) + .build(); + + // Construct the Inspect request to be sent by the client. + InspectContentRequest request = + InspectContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(item) + .setInspectConfig(config) + .build(); + + // Use the client to send the API request. + InspectContentResponse response = dlp.inspectContent(request); + + // Parse the response and process results + System.out.println("Findings: " + response.getResult().getFindingsCount()); + for (Finding f : response.getResult().getFindingsList()) { + System.out.println("\tQuote: " + f.getQuote()); + System.out.println("\tInfo type: " + f.getInfoType().getName()); + System.out.println("\tLikelihood: " + f.getLikelihood()); + } + } + } +} +// [END dlp_inspect_string_custom_hotword] diff --git a/dlp/src/main/java/dlp/snippets/InspectStringCustomOmitOverlap.java b/dlp/src/main/java/dlp/snippets/InspectStringCustomOmitOverlap.java new file mode 100644 index 00000000000..0cf017f3436 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/InspectStringCustomOmitOverlap.java @@ -0,0 +1,119 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_inspect_string_custom_omit_overlap] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ByteContentItem; +import com.google.privacy.dlp.v2.ByteContentItem.BytesType; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.CustomInfoType; +import com.google.privacy.dlp.v2.CustomInfoType.ExclusionType; +import com.google.privacy.dlp.v2.CustomInfoType.Regex; +import com.google.privacy.dlp.v2.ExcludeInfoTypes; +import com.google.privacy.dlp.v2.ExclusionRule; +import com.google.privacy.dlp.v2.Finding; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.InspectContentRequest; +import com.google.privacy.dlp.v2.InspectContentResponse; +import com.google.privacy.dlp.v2.InspectionRule; +import com.google.privacy.dlp.v2.InspectionRuleSet; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.MatchingType; +import com.google.protobuf.ByteString; +import java.io.IOException; + +public class InspectStringCustomOmitOverlap { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String textToInspect = "Name: Jane Doe. Name: Larry Page."; + inspectStringCustomOmitOverlap(projectId, textToInspect); + } + + // Inspects the provided text, avoiding matches specified in the exclusion list. + public static void inspectStringCustomOmitOverlap(String projectId, String textToInspect) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the type and content to be inspected. + ByteContentItem byteItem = + ByteContentItem.newBuilder() + .setType(BytesType.TEXT_UTF8) + .setData(ByteString.copyFromUtf8(textToInspect)) + .build(); + ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); + + // Construct the custom infotype. + CustomInfoType customInfoType = + CustomInfoType.newBuilder() + .setInfoType(InfoType.newBuilder().setName("VIP_DETECTOR")) + .setRegex(Regex.newBuilder().setPattern("Larry Page|Sergey Brin")) + .setExclusionType(ExclusionType.EXCLUSION_TYPE_EXCLUDE) + .build(); + + // Exclude matches that also match the custom infotype. + ExclusionRule exclusionRule = + ExclusionRule.newBuilder() + .setExcludeInfoTypes( + ExcludeInfoTypes.newBuilder().addInfoTypes(customInfoType.getInfoType())) + .setMatchingType(MatchingType.MATCHING_TYPE_FULL_MATCH) + .build(); + + // Construct a ruleset that applies the exclusion rule to the PERSON_NAME infotype. + InspectionRuleSet ruleSet = + InspectionRuleSet.newBuilder() + .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME")) + .addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule)) + .build(); + + // Construct the configuration for the Inspect request, including the ruleset. + InspectConfig config = + InspectConfig.newBuilder() + .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME")) + .addCustomInfoTypes(customInfoType) + .setIncludeQuote(true) + .addRuleSet(ruleSet) + .build(); + + // Construct the Inspect request to be sent by the client. + InspectContentRequest request = + InspectContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(item) + .setInspectConfig(config) + .build(); + + // Use the client to send the API request. + InspectContentResponse response = dlp.inspectContent(request); + + // Parse the response and process results + System.out.println("Findings: " + response.getResult().getFindingsCount()); + for (Finding f : response.getResult().getFindingsList()) { + System.out.println("\tQuote: " + f.getQuote()); + System.out.println("\tInfo type: " + f.getInfoType().getName()); + System.out.println("\tLikelihood: " + f.getLikelihood()); + } + } + } +} +// [END dlp_inspect_string_custom_omit_overlap] diff --git a/dlp/src/main/java/dlp/snippets/InspectStringMultipleRules.java b/dlp/src/main/java/dlp/snippets/InspectStringMultipleRules.java new file mode 100644 index 00000000000..821cfab1117 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/InspectStringMultipleRules.java @@ -0,0 +1,139 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_inspect_string_multiple_rules] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ByteContentItem; +import com.google.privacy.dlp.v2.ByteContentItem.BytesType; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.CustomInfoType.DetectionRule.HotwordRule; +import com.google.privacy.dlp.v2.CustomInfoType.DetectionRule.LikelihoodAdjustment; +import com.google.privacy.dlp.v2.CustomInfoType.DetectionRule.Proximity; +import com.google.privacy.dlp.v2.CustomInfoType.Dictionary; +import com.google.privacy.dlp.v2.CustomInfoType.Dictionary.WordList; +import com.google.privacy.dlp.v2.CustomInfoType.Regex; +import com.google.privacy.dlp.v2.ExclusionRule; +import com.google.privacy.dlp.v2.Finding; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.InspectContentRequest; +import com.google.privacy.dlp.v2.InspectContentResponse; +import com.google.privacy.dlp.v2.InspectionRule; +import com.google.privacy.dlp.v2.InspectionRuleSet; +import com.google.privacy.dlp.v2.Likelihood; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.MatchingType; +import com.google.protobuf.ByteString; +import java.io.IOException; + +public class InspectStringMultipleRules { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String textToInspect = "patient: Jane Doe"; + inspectStringMultipleRules(projectId, textToInspect); + } + + // Inspects the provided text, avoiding matches specified in the exclusion list. + public static void inspectStringMultipleRules(String projectId, String textToInspect) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the type and content to be inspected. + ByteContentItem byteItem = + ByteContentItem.newBuilder() + .setType(BytesType.TEXT_UTF8) + .setData(ByteString.copyFromUtf8(textToInspect)) + .build(); + ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); + + // Construct hotword rules + HotwordRule patientRule = + HotwordRule.newBuilder() + .setHotwordRegex(Regex.newBuilder().setPattern("patient")) + .setProximity(Proximity.newBuilder().setWindowBefore(10)) + .setLikelihoodAdjustment( + LikelihoodAdjustment.newBuilder().setFixedLikelihood(Likelihood.VERY_LIKELY)) + .build(); + + HotwordRule doctorRule = + HotwordRule.newBuilder() + .setHotwordRegex(Regex.newBuilder().setPattern("doctor")) + .setProximity(Proximity.newBuilder().setWindowBefore(10)) + .setLikelihoodAdjustment( + LikelihoodAdjustment.newBuilder().setFixedLikelihood(Likelihood.UNLIKELY)) + .build(); + + // Construct exclusion rules + ExclusionRule quasimodoRule = + ExclusionRule.newBuilder() + .setDictionary( + Dictionary.newBuilder().setWordList(WordList.newBuilder().addWords("Quasimodo"))) + .setMatchingType(MatchingType.MATCHING_TYPE_PARTIAL_MATCH) + .build(); + + ExclusionRule redactedRule = + ExclusionRule.newBuilder() + .setRegex(Regex.newBuilder().setPattern("REDACTED")) + .setMatchingType(MatchingType.MATCHING_TYPE_PARTIAL_MATCH) + .build(); + + // Construct a ruleset that applies the rules to the PERSON_NAME infotype. + InspectionRuleSet ruleSet = + InspectionRuleSet.newBuilder() + .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME")) + .addRules(InspectionRule.newBuilder().setHotwordRule(patientRule)) + .addRules(InspectionRule.newBuilder().setHotwordRule(doctorRule)) + .addRules(InspectionRule.newBuilder().setExclusionRule(quasimodoRule)) + .addRules(InspectionRule.newBuilder().setExclusionRule(redactedRule)) + .build(); + + // Construct the configuration for the Inspect request, including the ruleset. + InspectConfig config = + InspectConfig.newBuilder() + .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME")) + .setIncludeQuote(true) + .addRuleSet(ruleSet) + .build(); + + // Construct the Inspect request to be sent by the client. + InspectContentRequest request = + InspectContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(item) + .setInspectConfig(config) + .build(); + + // Use the client to send the API request. + InspectContentResponse response = dlp.inspectContent(request); + + // Parse the response and process results + System.out.println("Findings: " + response.getResult().getFindingsCount()); + for (Finding f : response.getResult().getFindingsList()) { + System.out.println("\tQuote: " + f.getQuote()); + System.out.println("\tInfo type: " + f.getInfoType().getName()); + System.out.println("\tLikelihood: " + f.getLikelihood()); + } + } + } +} +// [END dlp_inspect_string_multiple_rules] diff --git a/dlp/src/main/java/dlp/snippets/InspectStringOmitOverlap.java b/dlp/src/main/java/dlp/snippets/InspectStringOmitOverlap.java new file mode 100644 index 00000000000..6786a290d49 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/InspectStringOmitOverlap.java @@ -0,0 +1,118 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_inspect_string_omit_overlap] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ByteContentItem; +import com.google.privacy.dlp.v2.ByteContentItem.BytesType; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.ExcludeInfoTypes; +import com.google.privacy.dlp.v2.ExclusionRule; +import com.google.privacy.dlp.v2.Finding; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.InspectContentRequest; +import com.google.privacy.dlp.v2.InspectContentResponse; +import com.google.privacy.dlp.v2.InspectionRule; +import com.google.privacy.dlp.v2.InspectionRuleSet; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.MatchingType; +import com.google.protobuf.ByteString; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class InspectStringOmitOverlap { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String textToInspect = "james@example.com"; + inspectStringOmitOverlap(projectId, textToInspect); + } + + // Inspects the provided text, avoiding matches specified in the exclusion list. + public static void inspectStringOmitOverlap(String projectId, String textToInspect) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the type and content to be inspected. + ByteContentItem byteItem = + ByteContentItem.newBuilder() + .setType(BytesType.TEXT_UTF8) + .setData(ByteString.copyFromUtf8(textToInspect)) + .build(); + ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); + + // Specify the type of info the inspection will look for. + // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types. + List infoTypes = new ArrayList<>(); + for (String typeName : new String[]{"PERSON_NAME", "EMAIL_ADDRESS"}) { + infoTypes.add(InfoType.newBuilder().setName(typeName).build()); + } + + // Exclude EMAIL_ADDRESS matches + ExclusionRule exclusionRule = + ExclusionRule.newBuilder() + .setExcludeInfoTypes(ExcludeInfoTypes.newBuilder() + .addInfoTypes(InfoType.newBuilder().setName("EMAIL_ADDRESS"))) + .setMatchingType(MatchingType.MATCHING_TYPE_PARTIAL_MATCH) + .build(); + + // Construct a ruleset that applies the exclusion rule to the PERSON_NAME infotype. + // If a PERSON_NAME match overlaps with an EMAIL_ADDRESS match, the PERSON_NAME match will + // be excluded. + InspectionRuleSet ruleSet = + InspectionRuleSet.newBuilder() + .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME")) + .addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule)) + .build(); + + // Construct the configuration for the Inspect request, including the ruleset. + InspectConfig config = + InspectConfig.newBuilder() + .addAllInfoTypes(infoTypes) + .setIncludeQuote(true) + .addRuleSet(ruleSet) + .build(); + + // Construct the Inspect request to be sent by the client. + InspectContentRequest request = + InspectContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(item) + .setInspectConfig(config) + .build(); + + // Use the client to send the API request. + InspectContentResponse response = dlp.inspectContent(request); + + // Parse the response and process results + System.out.println("Findings: " + response.getResult().getFindingsCount()); + for (Finding f : response.getResult().getFindingsList()) { + System.out.println("\tQuote: " + f.getQuote()); + System.out.println("\tInfo type: " + f.getInfoType().getName()); + System.out.println("\tLikelihood: " + f.getLikelihood()); + } + } + } +} +// [END dlp_inspect_string_omit_overlap] diff --git a/dlp/src/main/java/dlp/snippets/InspectStringWithExclusionDict.java b/dlp/src/main/java/dlp/snippets/InspectStringWithExclusionDict.java new file mode 100644 index 00000000000..a73bffc08c6 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/InspectStringWithExclusionDict.java @@ -0,0 +1,119 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_inspect_string_with_exclusion_dict] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ByteContentItem; +import com.google.privacy.dlp.v2.ByteContentItem.BytesType; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.CustomInfoType.Dictionary; +import com.google.privacy.dlp.v2.CustomInfoType.Dictionary.WordList; +import com.google.privacy.dlp.v2.ExclusionRule; +import com.google.privacy.dlp.v2.Finding; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.InspectContentRequest; +import com.google.privacy.dlp.v2.InspectContentResponse; +import com.google.privacy.dlp.v2.InspectionRule; +import com.google.privacy.dlp.v2.InspectionRuleSet; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.MatchingType; +import com.google.protobuf.ByteString; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class InspectStringWithExclusionDict { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String textToInspect = "Some email addresses: gary@example.com, example@example.com"; + List excludedMatchList = Arrays.asList("example@example.com"); + inspectStringWithExclusionDict(projectId, textToInspect, excludedMatchList); + } + + // Inspects the provided text, avoiding matches specified in the exclusion list. + public static void inspectStringWithExclusionDict(String projectId, String textToInspect, + List excludedMatchList) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the type and content to be inspected. + ByteContentItem byteItem = + ByteContentItem.newBuilder() + .setType(BytesType.TEXT_UTF8) + .setData(ByteString.copyFromUtf8(textToInspect)) + .build(); + ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); + + // Specify the type of info the inspection will look for. + // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types. + List infoTypes = new ArrayList<>(); + for (String typeName : new String[]{"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) { + infoTypes.add(InfoType.newBuilder().setName(typeName).build()); + } + + // Exclude matches from the specified excludedMatchList. + ExclusionRule exclusionRule = + ExclusionRule.newBuilder() + .setMatchingType(MatchingType.MATCHING_TYPE_FULL_MATCH) + .setDictionary(Dictionary.newBuilder() + .setWordList(WordList.newBuilder().addAllWords(excludedMatchList))) + .build(); + + // Construct a ruleset that applies the exclusion rule to the EMAIL_ADDRESSES infotype. + InspectionRuleSet ruleSet = + InspectionRuleSet.newBuilder() + .addInfoTypes(InfoType.newBuilder().setName("EMAIL_ADDRESS")) + .addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule)) + .build(); + + // Construct the configuration for the Inspect request, including the ruleset. + InspectConfig config = + InspectConfig.newBuilder() + .addAllInfoTypes(infoTypes) + .setIncludeQuote(true) + .addRuleSet(ruleSet) + .build(); + + // Construct the Inspect request to be sent by the client. + InspectContentRequest request = + InspectContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(item) + .setInspectConfig(config) + .build(); + + // Use the client to send the API request. + InspectContentResponse response = dlp.inspectContent(request); + + // Parse the response and process results + System.out.println("Findings: " + response.getResult().getFindingsCount()); + for (Finding f : response.getResult().getFindingsList()) { + System.out.println("\tQuote: " + f.getQuote()); + System.out.println("\tInfo type: " + f.getInfoType().getName()); + System.out.println("\tLikelihood: " + f.getLikelihood()); + } + } + } +} +// [END dlp_inspect_string_with_exclusion_dict] diff --git a/dlp/src/main/java/dlp/snippets/InspectStringWithExclusionDictSubstring.java b/dlp/src/main/java/dlp/snippets/InspectStringWithExclusionDictSubstring.java new file mode 100644 index 00000000000..e0f836ee4f9 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/InspectStringWithExclusionDictSubstring.java @@ -0,0 +1,120 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_inspect_string_with_exclusion_dict_substring] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ByteContentItem; +import com.google.privacy.dlp.v2.ByteContentItem.BytesType; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.CustomInfoType.Dictionary; +import com.google.privacy.dlp.v2.CustomInfoType.Dictionary.WordList; +import com.google.privacy.dlp.v2.ExclusionRule; +import com.google.privacy.dlp.v2.Finding; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.InspectContentRequest; +import com.google.privacy.dlp.v2.InspectContentResponse; +import com.google.privacy.dlp.v2.InspectionRule; +import com.google.privacy.dlp.v2.InspectionRuleSet; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.MatchingType; +import com.google.protobuf.ByteString; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class InspectStringWithExclusionDictSubstring { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String textToInspect = "Some email addresses: gary@example.com, TEST@example.com"; + List excludedSubstringList = Arrays.asList("TEST"); + inspectStringWithExclusionDictSubstring(projectId, textToInspect, excludedSubstringList); + } + + // Inspects the provided text, avoiding matches specified in the exclusion list. + public static void inspectStringWithExclusionDictSubstring(String projectId, String textToInspect, + List excludedSubstringList) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the type and content to be inspected. + ByteContentItem byteItem = + ByteContentItem.newBuilder() + .setType(BytesType.TEXT_UTF8) + .setData(ByteString.copyFromUtf8(textToInspect)) + .build(); + ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); + + // Specify the type of info the inspection will look for. + // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types. + List infoTypes = new ArrayList<>(); + for (String typeName : new String[]{"EMAIL_ADDRESS", "DOMAIN_NAME", "PHONE_NUMBER", + "PERSON_NAME"}) { + infoTypes.add(InfoType.newBuilder().setName(typeName).build()); + } + + // Exclude partial matches from the specified excludedSubstringList. + ExclusionRule exclusionRule = + ExclusionRule.newBuilder() + .setMatchingType(MatchingType.MATCHING_TYPE_PARTIAL_MATCH) + .setDictionary(Dictionary.newBuilder() + .setWordList(WordList.newBuilder().addAllWords(excludedSubstringList))) + .build(); + + // Construct a ruleset that applies the exclusion rule to the EMAIL_ADDRESSES infotype. + InspectionRuleSet ruleSet = + InspectionRuleSet.newBuilder() + .addAllInfoTypes(infoTypes) + .addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule)) + .build(); + + // Construct the configuration for the Inspect request, including the ruleset. + InspectConfig config = + InspectConfig.newBuilder() + .addAllInfoTypes(infoTypes) + .setIncludeQuote(true) + .addRuleSet(ruleSet) + .build(); + + // Construct the Inspect request to be sent by the client. + InspectContentRequest request = + InspectContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(item) + .setInspectConfig(config) + .build(); + + // Use the client to send the API request. + InspectContentResponse response = dlp.inspectContent(request); + + // Parse the response and process results + System.out.println("Findings: " + response.getResult().getFindingsCount()); + for (Finding f : response.getResult().getFindingsList()) { + System.out.println("\tQuote: " + f.getQuote()); + System.out.println("\tInfo type: " + f.getInfoType().getName()); + System.out.println("\tLikelihood: " + f.getLikelihood()); + } + } + } +} +// [END dlp_inspect_string_with_exclusion_dict_substring] diff --git a/dlp/src/main/java/dlp/snippets/InspectStringWithExclusionRegex.java b/dlp/src/main/java/dlp/snippets/InspectStringWithExclusionRegex.java new file mode 100644 index 00000000000..a8a6d7dbaa4 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/InspectStringWithExclusionRegex.java @@ -0,0 +1,116 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_inspect_string_with_exclusion_regex] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ByteContentItem; +import com.google.privacy.dlp.v2.ByteContentItem.BytesType; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.CustomInfoType.Regex; +import com.google.privacy.dlp.v2.ExclusionRule; +import com.google.privacy.dlp.v2.Finding; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.InspectContentRequest; +import com.google.privacy.dlp.v2.InspectContentResponse; +import com.google.privacy.dlp.v2.InspectionRule; +import com.google.privacy.dlp.v2.InspectionRuleSet; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.MatchingType; +import com.google.protobuf.ByteString; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class InspectStringWithExclusionRegex { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String textToInspect = "Some email addresses: gary@example.com, bob@example.org"; + String excludedRegex = ".+@example.com"; + inspectStringWithExclusionRegex(projectId, textToInspect, excludedRegex); + } + + // Inspects the provided text, avoiding matches specified in the exclusion list. + public static void inspectStringWithExclusionRegex(String projectId, String textToInspect, + String excludedRegex) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the type and content to be inspected. + ByteContentItem byteItem = + ByteContentItem.newBuilder() + .setType(BytesType.TEXT_UTF8) + .setData(ByteString.copyFromUtf8(textToInspect)) + .build(); + ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); + + // Specify the type of info the inspection will look for. + // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types. + List infoTypes = new ArrayList<>(); + for (String typeName : new String[]{"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) { + infoTypes.add(InfoType.newBuilder().setName(typeName).build()); + } + + // Exclude matches from the specified excludedMatchList. + ExclusionRule exclusionRule = + ExclusionRule.newBuilder() + .setMatchingType(MatchingType.MATCHING_TYPE_FULL_MATCH) + .setRegex(Regex.newBuilder().setPattern(excludedRegex)) + .build(); + + // Construct a ruleset that applies the exclusion rule to the EMAIL_ADDRESSES infotype. + InspectionRuleSet ruleSet = + InspectionRuleSet.newBuilder() + .addInfoTypes(InfoType.newBuilder().setName("EMAIL_ADDRESS")) + .addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule)) + .build(); + + // Construct the configuration for the Inspect request, including the ruleset. + InspectConfig config = + InspectConfig.newBuilder() + .addAllInfoTypes(infoTypes) + .setIncludeQuote(true) + .addRuleSet(ruleSet) + .build(); + + // Construct the Inspect request to be sent by the client. + InspectContentRequest request = + InspectContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(item) + .setInspectConfig(config) + .build(); + + // Use the client to send the API request. + InspectContentResponse response = dlp.inspectContent(request); + + // Parse the response and process results + System.out.println("Findings: " + response.getResult().getFindingsCount()); + for (Finding f : response.getResult().getFindingsList()) { + System.out.println("\tQuote: " + f.getQuote()); + System.out.println("\tInfo type: " + f.getInfoType().getName()); + System.out.println("\tLikelihood: " + f.getLikelihood()); + } + } + } +} +// [END dlp_inspect_string_with_exclusion_regex] \ No newline at end of file diff --git a/dlp/src/main/java/dlp/snippets/InspectStringWithoutOverlap.java b/dlp/src/main/java/dlp/snippets/InspectStringWithoutOverlap.java new file mode 100644 index 00000000000..85d9a5abff2 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/InspectStringWithoutOverlap.java @@ -0,0 +1,128 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_inspect_string_without_overlap] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ByteContentItem; +import com.google.privacy.dlp.v2.ByteContentItem.BytesType; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.CustomInfoType; +import com.google.privacy.dlp.v2.CustomInfoType.ExclusionType; +import com.google.privacy.dlp.v2.ExcludeInfoTypes; +import com.google.privacy.dlp.v2.ExclusionRule; +import com.google.privacy.dlp.v2.Finding; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.InspectContentRequest; +import com.google.privacy.dlp.v2.InspectContentResponse; +import com.google.privacy.dlp.v2.InspectionRule; +import com.google.privacy.dlp.v2.InspectionRuleSet; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.MatchingType; +import com.google.protobuf.ByteString; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class InspectStringWithoutOverlap { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String textToInspect = "example.com is a domain, james@example.org is an email."; + inspectStringWithoutOverlap(projectId, textToInspect); + } + + // Inspects the provided text, avoiding matches specified in the exclusion list. + public static void inspectStringWithoutOverlap(String projectId, String textToInspect) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the type and content to be inspected. + ByteContentItem byteItem = + ByteContentItem.newBuilder() + .setType(BytesType.TEXT_UTF8) + .setData(ByteString.copyFromUtf8(textToInspect)) + .build(); + ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); + + // Specify the type of info the inspection will look for. + // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types. + List infoTypes = new ArrayList<>(); + for (String typeName : new String[]{"DOMAIN_NAME", "EMAIL_ADDRESS"}) { + infoTypes.add(InfoType.newBuilder().setName(typeName).build()); + } + + // Define a custom info type to exclude email addresses + CustomInfoType customInfoType = + CustomInfoType.newBuilder() + .setInfoType(InfoType.newBuilder().setName("EMAIL_ADDRESS")) + .setExclusionType(ExclusionType.EXCLUSION_TYPE_EXCLUDE) + .build(); + + // Exclude EMAIL_ADDRESS matches + ExclusionRule exclusionRule = + ExclusionRule.newBuilder() + .setExcludeInfoTypes(ExcludeInfoTypes.newBuilder() + .addInfoTypes(InfoType.newBuilder().setName("EMAIL_ADDRESS"))) + .setMatchingType(MatchingType.MATCHING_TYPE_PARTIAL_MATCH) + .build(); + + // Construct a ruleset that applies the exclusion rule to the DOMAIN_NAME infotype. + // If a DOMAIN_NAME match is part of an EMAIL_ADDRESS match, the DOMAIN_NAME match will + // be excluded. + InspectionRuleSet ruleSet = + InspectionRuleSet.newBuilder() + .addInfoTypes(InfoType.newBuilder().setName("DOMAIN_NAME")) + .addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule)) + .build(); + + // Construct the configuration for the Inspect request, including the ruleset. + InspectConfig config = + InspectConfig.newBuilder() + .addAllInfoTypes(infoTypes) + .addCustomInfoTypes(customInfoType) + .setIncludeQuote(true) + .addRuleSet(ruleSet) + .build(); + + // Construct the Inspect request to be sent by the client. + InspectContentRequest request = + InspectContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(item) + .setInspectConfig(config) + .build(); + + // Use the client to send the API request. + InspectContentResponse response = dlp.inspectContent(request); + + // Parse the response and process results + System.out.println("Findings: " + response.getResult().getFindingsCount()); + for (Finding f : response.getResult().getFindingsList()) { + System.out.println("\tQuote: " + f.getQuote()); + System.out.println("\tInfo type: " + f.getInfoType().getName()); + System.out.println("\tLikelihood: " + f.getLikelihood()); + } + } + } +} +// [END dlp_inspect_string_without_overlap] diff --git a/dlp/src/main/java/dlp/snippets/InspectTable.java b/dlp/src/main/java/dlp/snippets/InspectTable.java new file mode 100644 index 00000000000..c25488ff29b --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/InspectTable.java @@ -0,0 +1,100 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_inspect_table] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ByteContentItem; +import com.google.privacy.dlp.v2.ByteContentItem.BytesType; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.FieldId; +import com.google.privacy.dlp.v2.Finding; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.InspectContentRequest; +import com.google.privacy.dlp.v2.InspectContentResponse; +import com.google.privacy.dlp.v2.Likelihood; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.Table; +import com.google.privacy.dlp.v2.Table.Row; +import com.google.privacy.dlp.v2.Value; +import com.google.protobuf.ByteString; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class InspectTable { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + Table tableToInspect = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("name").build()) + .addHeaders(FieldId.newBuilder().setName("phone").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("John Doe").build()) + .addValues(Value.newBuilder().setStringValue("(206) 555-0123").build())) + .build(); + + inspectTable(projectId, tableToInspect); + } + + // Inspects the provided text. + public static void inspectTable(String projectId, Table tableToInspect) { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the table to be inspected. + ContentItem item = ContentItem.newBuilder().setTable(tableToInspect).build(); + + // Specify the type of info the inspection will look for. + // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types + InfoType infoType = InfoType.newBuilder().setName("PHONE_NUMBER").build(); + + // Construct the configuration for the Inspect request. + InspectConfig config = + InspectConfig.newBuilder() + .addInfoTypes(infoType) + .setIncludeQuote(true) + .build(); + + // Construct the Inspect request to be sent by the client. + InspectContentRequest request = + InspectContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(item) + .setInspectConfig(config) + .build(); + + // Use the client to send the API request. + InspectContentResponse response = dlp.inspectContent(request); + + // Parse the response and process results + System.out.println("Findings: " + response.getResult().getFindingsCount()); + for (Finding f : response.getResult().getFindingsList()) { + System.out.println("\tQuote: " + f.getQuote()); + System.out.println("\tInfo type: " + f.getInfoType().getName()); + System.out.println("\tLikelihood: " + f.getLikelihood()); + } + } catch (Exception e) { + System.out.println("Error during inspectString: \n" + e.toString()); + } + } +} +// [END dlp_inspect_table] diff --git a/dlp/src/main/java/dlp/snippets/InspectTextFile.java b/dlp/src/main/java/dlp/snippets/InspectTextFile.java index 0050c8de8cd..c83fb98423b 100644 --- a/dlp/src/main/java/dlp/snippets/InspectTextFile.java +++ b/dlp/src/main/java/dlp/snippets/InspectTextFile.java @@ -17,6 +17,7 @@ package dlp.snippets; // [START dlp_inspect_file] + import com.google.cloud.dlp.v2.DlpServiceClient; import com.google.privacy.dlp.v2.ByteContentItem; import com.google.privacy.dlp.v2.ByteContentItem.BytesType; @@ -26,15 +27,16 @@ import com.google.privacy.dlp.v2.InspectConfig; import com.google.privacy.dlp.v2.InspectContentRequest; import com.google.privacy.dlp.v2.InspectContentResponse; -import com.google.privacy.dlp.v2.ProjectName; +import com.google.privacy.dlp.v2.LocationName; import com.google.protobuf.ByteString; import java.io.FileInputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.List; public class InspectTextFile { - public static void inspectTextFile() { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String filePath = "path/to/file.txt"; @@ -42,14 +44,11 @@ public static void inspectTextFile() { } // Inspects the specified text file. - public static void inspectTextFile(String projectId, String filePath) { + public static void inspectTextFile(String projectId, String filePath) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (DlpServiceClient dlp = DlpServiceClient.create()) { - // Specify the project used for request. - ProjectName project = ProjectName.of(projectId); - // Specify the type and content to be inspected. ByteString fileBytes = ByteString.readFrom(new FileInputStream(filePath)); ByteContentItem byteItem = @@ -59,18 +58,21 @@ public static void inspectTextFile(String projectId, String filePath) { // Specify the type of info the inspection will look for. List infoTypes = new ArrayList<>(); // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types - for (String typeName : new String[] {"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) { + for (String typeName : new String[]{"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) { infoTypes.add(InfoType.newBuilder().setName(typeName).build()); } // Construct the configuration for the Inspect request. InspectConfig config = - InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setIncludeQuote(true).build(); + InspectConfig.newBuilder() + .addAllInfoTypes(infoTypes) + .setIncludeQuote(true) + .build(); // Construct the Inspect request to be sent by the client. InspectContentRequest request = InspectContentRequest.newBuilder() - .setParent(project.toString()) + .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setInspectConfig(config) .build(); @@ -85,8 +87,6 @@ public static void inspectTextFile(String projectId, String filePath) { System.out.println("\tInfo type: " + f.getInfoType().getName()); System.out.println("\tLikelihood: " + f.getLikelihood()); } - } catch (Exception e) { - System.out.println("Error during inspectFile: \n" + e.toString()); } } } diff --git a/dlp/src/main/java/dlp/snippets/InspectWithCustomRegex.java b/dlp/src/main/java/dlp/snippets/InspectWithCustomRegex.java new file mode 100644 index 00000000000..5baf820ed29 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/InspectWithCustomRegex.java @@ -0,0 +1,101 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_inspect_custom_regex] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ByteContentItem; +import com.google.privacy.dlp.v2.ByteContentItem.BytesType; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.CustomInfoType; +import com.google.privacy.dlp.v2.CustomInfoType.Regex; +import com.google.privacy.dlp.v2.Finding; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.InspectContentRequest; +import com.google.privacy.dlp.v2.InspectContentResponse; +import com.google.privacy.dlp.v2.Likelihood; +import com.google.privacy.dlp.v2.LocationName; +import com.google.protobuf.ByteString; +import java.io.IOException; + +public class InspectWithCustomRegex { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String textToInspect = "Patients MRN 444-5-22222"; + String customRegexPattern = "[1-9]{3}-[1-9]{1}-[1-9]{5}"; + inspectWithCustomRegex(projectId, textToInspect, customRegexPattern); + } + + // Inspects a BigQuery Table + public static void inspectWithCustomRegex( + String projectId, String textToInspect, String customRegexPattern) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the type and content to be inspected. + ByteContentItem byteItem = + ByteContentItem.newBuilder() + .setType(BytesType.TEXT_UTF8) + .setData(ByteString.copyFromUtf8(textToInspect)) + .build(); + ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); + + // Specify the regex pattern the inspection will look for. + Regex regex = Regex.newBuilder().setPattern(customRegexPattern).build(); + + // Construct the custom regex detector. + InfoType infoType = InfoType.newBuilder().setName("C_MRN").build(); + CustomInfoType customInfoType = + CustomInfoType.newBuilder() + .setInfoType(infoType) + .setRegex(regex) + .build(); + + // Construct the configuration for the Inspect request. + InspectConfig config = + InspectConfig.newBuilder() + .addCustomInfoTypes(customInfoType) + .setIncludeQuote(true) + .setMinLikelihood(Likelihood.POSSIBLE) + .build(); + + // Construct the Inspect request to be sent by the client. + InspectContentRequest request = + InspectContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()).setItem(item) + .setInspectConfig(config) + .build(); + + // Use the client to send the API request. + InspectContentResponse response = dlp.inspectContent(request); + + // Parse the response and process results + System.out.println("Findings: " + response.getResult().getFindingsCount()); + for (Finding f : response.getResult().getFindingsList()) { + System.out.println("\tQuote: " + f.getQuote()); + System.out.println("\tInfo type: " + f.getInfoType().getName()); + System.out.println("\tLikelihood: " + f.getLikelihood()); + } + } + } +} +// [END dlp_inspect_custom_regex] diff --git a/dlp/src/main/java/dlp/snippets/InspectWithHotwordRules.java b/dlp/src/main/java/dlp/snippets/InspectWithHotwordRules.java new file mode 100644 index 00000000000..39c253279bf --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/InspectWithHotwordRules.java @@ -0,0 +1,129 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_inspect_hotword_rule] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ByteContentItem; +import com.google.privacy.dlp.v2.ByteContentItem.BytesType; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.CustomInfoType; +import com.google.privacy.dlp.v2.CustomInfoType.DetectionRule.HotwordRule; +import com.google.privacy.dlp.v2.CustomInfoType.DetectionRule.LikelihoodAdjustment; +import com.google.privacy.dlp.v2.CustomInfoType.DetectionRule.Proximity; +import com.google.privacy.dlp.v2.CustomInfoType.Regex; +import com.google.privacy.dlp.v2.Finding; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.InspectContentRequest; +import com.google.privacy.dlp.v2.InspectContentResponse; +import com.google.privacy.dlp.v2.InspectionRule; +import com.google.privacy.dlp.v2.InspectionRuleSet; +import com.google.privacy.dlp.v2.Likelihood; +import com.google.privacy.dlp.v2.LocationName; +import com.google.protobuf.ByteString; +import java.io.IOException; + +public class InspectWithHotwordRules { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String textToInspect = "Patient's MRN 444-5-22222 and just a number 333-2-33333"; + String customRegexPattern = "[1-9]{3}-[1-9]{1}-[1-9]{5}"; + String hotwordRegexPattern = "(?i)(mrn|medical)(?-i)"; + inspectWithHotwordRules(projectId, textToInspect, customRegexPattern, hotwordRegexPattern); + } + + // Inspects a BigQuery Table + public static void inspectWithHotwordRules( + String projectId, String textToInspect, String customRegexPattern, String hotwordRegexPattern) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the type and content to be inspected. + ByteContentItem byteItem = + ByteContentItem.newBuilder() + .setType(BytesType.TEXT_UTF8) + .setData(ByteString.copyFromUtf8(textToInspect)) + .build(); + ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); + + // Specify the regex pattern the inspection will look for. + Regex regex = Regex.newBuilder().setPattern(customRegexPattern).build(); + + // Construct the custom regex detector. + InfoType infoType = InfoType.newBuilder().setName("C_MRN").build(); + CustomInfoType customInfoType = + CustomInfoType.newBuilder().setInfoType(infoType).setRegex(regex).build(); + + // Specify hotword likelihood adjustment. + LikelihoodAdjustment likelihoodAdjustment = + LikelihoodAdjustment.newBuilder().setFixedLikelihood(Likelihood.VERY_LIKELY).build(); + + // Specify a window around a finding to apply a detection rule. + Proximity proximity = Proximity.newBuilder().setWindowBefore(10).build(); + + // Construct hotword rule. + HotwordRule hotwordRule = + HotwordRule.newBuilder() + .setHotwordRegex(Regex.newBuilder().setPattern(hotwordRegexPattern).build()) + .setLikelihoodAdjustment(likelihoodAdjustment) + .setProximity(proximity) + .build(); + + // Construct rule set for the inspect config. + InspectionRuleSet inspectionRuleSet = + InspectionRuleSet.newBuilder() + .addInfoTypes(infoType) + .addRules(InspectionRule.newBuilder().setHotwordRule(hotwordRule)) + .build(); + + // Construct the configuration for the Inspect request. + InspectConfig config = + InspectConfig.newBuilder() + .addCustomInfoTypes(customInfoType) + .setIncludeQuote(true) + .setMinLikelihood(Likelihood.POSSIBLE) + .addRuleSet(inspectionRuleSet) + .build(); + + // Construct the Inspect request to be sent by the client. + InspectContentRequest request = + InspectContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(item) + .setInspectConfig(config) + .build(); + + // Use the client to send the API request. + InspectContentResponse response = dlp.inspectContent(request); + + // Parse the response and process results + System.out.println("Findings: " + response.getResult().getFindingsCount()); + for (Finding f : response.getResult().getFindingsList()) { + System.out.println("\tQuote: " + f.getQuote()); + System.out.println("\tInfo type: " + f.getInfoType().getName()); + System.out.println("\tLikelihood: " + f.getLikelihood()); + } + } + } +} +// [END dlp_inspect_hotword_rule] diff --git a/dlp/src/main/java/dlp/snippets/JobsCreate.java b/dlp/src/main/java/dlp/snippets/JobsCreate.java new file mode 100644 index 00000000000..54325579721 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/JobsCreate.java @@ -0,0 +1,120 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_create_job] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.Action; +import com.google.privacy.dlp.v2.CloudStorageOptions; +import com.google.privacy.dlp.v2.CreateDlpJobRequest; +import com.google.privacy.dlp.v2.DlpJob; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.InspectJobConfig; +import com.google.privacy.dlp.v2.Likelihood; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.StorageConfig; +import com.google.privacy.dlp.v2.StorageConfig.TimespanConfig; +import java.io.IOException; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class JobsCreate { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String gcsPath = "gs://" + "your-bucket-name" + "path/to/file.txt"; + createJobs(projectId, gcsPath); + } + + // Creates a DLP Job + public static void createJobs(String projectId, String gcsPath) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) { + + // Set autoPopulateTimespan to true to scan only new content + boolean autoPopulateTimespan = true; + TimespanConfig timespanConfig = + TimespanConfig.newBuilder() + .setEnableAutoPopulationOfTimespanConfig(autoPopulateTimespan) + .build(); + + // Specify the GCS file to be inspected. + CloudStorageOptions cloudStorageOptions = + CloudStorageOptions.newBuilder() + .setFileSet(CloudStorageOptions.FileSet.newBuilder().setUrl(gcsPath)) + .build(); + StorageConfig storageConfig = + StorageConfig.newBuilder() + .setCloudStorageOptions(cloudStorageOptions) + .setTimespanConfig(timespanConfig) + .build(); + + // Specify the type of info the inspection will look for. + // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types + List infoTypes = + Stream.of("EMAIL_ADDRESS", "PERSON_NAME", "LOCATION", "PHONE_NUMBER") + .map(it -> InfoType.newBuilder().setName(it).build()) + .collect(Collectors.toList()); + // The minimum likelihood required before returning a match: + // See: https://cloud.google.com/dlp/docs/likelihood + Likelihood minLikelihood = Likelihood.UNLIKELY; + + // The maximum number of findings to report (0 = server maximum) + InspectConfig.FindingLimits findingLimits = + InspectConfig.FindingLimits.newBuilder().setMaxFindingsPerItem(100).build(); + + InspectConfig inspectConfig = + InspectConfig.newBuilder() + .addAllInfoTypes(infoTypes) + .setIncludeQuote(true) + .setMinLikelihood(minLikelihood) + .setLimits(findingLimits) + .build(); + + // Specify the action that is triggered when the job completes. + Action.PublishSummaryToCscc publishSummaryToCscc = + Action.PublishSummaryToCscc.getDefaultInstance(); + Action action = Action.newBuilder().setPublishSummaryToCscc(publishSummaryToCscc).build(); + + // Configure the inspection job we want the service to perform. + InspectJobConfig inspectJobConfig = + InspectJobConfig.newBuilder() + .setInspectConfig(inspectConfig) + .setStorageConfig(storageConfig) + .addActions(action) + .build(); + + // Construct the job creation request to be sent by the client. + CreateDlpJobRequest createDlpJobRequest = + CreateDlpJobRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setInspectJob(inspectJobConfig) + .build(); + + // Send the job creation request and process the response. + DlpJob createdDlpJob = dlpServiceClient.createDlpJob(createDlpJobRequest); + System.out.println("Job created successfully: " + createdDlpJob.getName()); + } + } +} +// [END dlp_create_job] diff --git a/dlp/src/main/java/dlp/snippets/JobsDelete.java b/dlp/src/main/java/dlp/snippets/JobsDelete.java index d751c8e2cdc..c3000d69fd2 100644 --- a/dlp/src/main/java/dlp/snippets/JobsDelete.java +++ b/dlp/src/main/java/dlp/snippets/JobsDelete.java @@ -24,7 +24,7 @@ import java.io.IOException; public class JobsDelete { - public static void deleteJobs() throws IOException { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String jobId = "your-job-id"; diff --git a/dlp/src/main/java/dlp/snippets/JobsGet.java b/dlp/src/main/java/dlp/snippets/JobsGet.java new file mode 100644 index 00000000000..9f43eccc93e --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/JobsGet.java @@ -0,0 +1,55 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_get_job] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.DlpJobName; +import com.google.privacy.dlp.v2.GetDlpJobRequest; +import java.io.IOException; + +public class JobsGet { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String jobId = "your-job-id"; + getJobs(projectId, jobId); + } + + // Gets a DLP Job with the given jobId + public static void getJobs(String projectId, String jobId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) { + + // Construct the complete job name from the projectId and jobId + DlpJobName jobName = DlpJobName.of(projectId, jobId); + + // Construct the get job request to be sent by the client. + GetDlpJobRequest getDlpJobRequest = + GetDlpJobRequest.newBuilder().setName(jobName.toString()).build(); + + // Send the get job request + dlpServiceClient.getDlpJob(getDlpJobRequest); + System.out.println("Job got successfully."); + } + } +} +// [END dlp_get_job] diff --git a/dlp/src/main/java/dlp/snippets/JobsList.java b/dlp/src/main/java/dlp/snippets/JobsList.java index 07d3d6ef0de..892f58e1586 100644 --- a/dlp/src/main/java/dlp/snippets/JobsList.java +++ b/dlp/src/main/java/dlp/snippets/JobsList.java @@ -22,12 +22,12 @@ import com.google.privacy.dlp.v2.DlpJob; import com.google.privacy.dlp.v2.DlpJobType; import com.google.privacy.dlp.v2.ListDlpJobsRequest; -import com.google.privacy.dlp.v2.ProjectName; +import com.google.privacy.dlp.v2.LocationName; import java.io.IOException; public class JobsList { - public static void listJobs() throws IOException { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; listJobs(projectId); @@ -45,7 +45,7 @@ public static void listJobs(String projectId) throws IOException { // see https://cloud.google.com/dlp/docs/reference/rest/v2/projects.dlpJobs/list ListDlpJobsRequest listDlpJobsRequest = ListDlpJobsRequest.newBuilder() - .setParent(ProjectName.of(projectId).toString()) + .setParent(LocationName.of(projectId, "global").toString()) .setFilter("state=DONE") .setType(DlpJobType.valueOf("INSPECT_JOB")) .build(); diff --git a/dlp/src/main/java/dlp/snippets/QuickStart.java b/dlp/src/main/java/dlp/snippets/QuickStart.java index 829c864dc93..b4b724deb74 100644 --- a/dlp/src/main/java/dlp/snippets/QuickStart.java +++ b/dlp/src/main/java/dlp/snippets/QuickStart.java @@ -28,7 +28,7 @@ import com.google.privacy.dlp.v2.InspectContentResponse; import com.google.privacy.dlp.v2.InspectResult; import com.google.privacy.dlp.v2.Likelihood; -import com.google.privacy.dlp.v2.ProjectName; +import com.google.privacy.dlp.v2.LocationName; import com.google.protobuf.ByteString; import java.io.IOException; import java.util.List; @@ -84,7 +84,7 @@ public static void quickstart(String projectId) throws IOException { // Create the request from previous configs InspectContentRequest request = InspectContentRequest.newBuilder() - .setParent(ProjectName.of(projectId).toString()) + .setParent(LocationName.of(projectId, "global").toString()) .setInspectConfig(inspectConfig) .setItem(contentItem) .build(); diff --git a/dlp/src/main/java/dlp/snippets/ReIdentifyTableWithFpe.java b/dlp/src/main/java/dlp/snippets/ReIdentifyTableWithFpe.java new file mode 100644 index 00000000000..6373698981d --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/ReIdentifyTableWithFpe.java @@ -0,0 +1,131 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_reidentify_table_fpe] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.common.io.BaseEncoding; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.CryptoKey; +import com.google.privacy.dlp.v2.CryptoReplaceFfxFpeConfig; +import com.google.privacy.dlp.v2.CryptoReplaceFfxFpeConfig.FfxCommonNativeAlphabet; +import com.google.privacy.dlp.v2.CustomInfoType; +import com.google.privacy.dlp.v2.CustomInfoType.SurrogateType; +import com.google.privacy.dlp.v2.DeidentifyConfig; +import com.google.privacy.dlp.v2.FieldId; +import com.google.privacy.dlp.v2.FieldTransformation; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InfoTypeTransformations; +import com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.KmsWrappedCryptoKey; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.PrimitiveTransformation; +import com.google.privacy.dlp.v2.RecordTransformations; +import com.google.privacy.dlp.v2.ReidentifyContentRequest; +import com.google.privacy.dlp.v2.ReidentifyContentResponse; +import com.google.privacy.dlp.v2.Table; +import com.google.privacy.dlp.v2.Table.Row; +import com.google.privacy.dlp.v2.Value; +import com.google.protobuf.ByteString; +import java.io.IOException; + +public class ReIdentifyTableWithFpe { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String kmsKeyName = + "projects/YOUR_PROJECT/" + + "locations/YOUR_KEYRING_REGION/" + + "keyRings/YOUR_KEYRING_NAME/" + + "cryptoKeys/YOUR_KEY_NAME"; + String wrappedAesKey = "YOUR_ENCRYPTED_AES_256_KEY"; + Table tableToReIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("Employee ID").build()) + .addRows( + Row.newBuilder().addValues( + Value.newBuilder().setStringValue("28777").build()) + .build()) + .build(); + reIdentifyTableWithFpe(projectId, tableToReIdentify, kmsKeyName, wrappedAesKey); + } + + public static void reIdentifyTableWithFpe( + String projectId, Table tableToReIdentify, String kmsKeyName, String wrappedAesKey) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify what content you want the service to re-identify. + ContentItem contentItem = ContentItem.newBuilder().setTable(tableToReIdentify).build(); + + // Specify an encrypted AES-256 key and the name of the Cloud KMS key that encrypted it. + KmsWrappedCryptoKey kmsWrappedCryptoKey = + KmsWrappedCryptoKey.newBuilder() + .setWrappedKey(ByteString.copyFrom(BaseEncoding.base64().decode(wrappedAesKey))) + .setCryptoKeyName(kmsKeyName) + .build(); + CryptoKey cryptoKey = CryptoKey.newBuilder().setKmsWrapped(kmsWrappedCryptoKey).build(); + + // Specify how to un-encrypt the previously de-identified information. + CryptoReplaceFfxFpeConfig cryptoReplaceFfxFpeConfig = + CryptoReplaceFfxFpeConfig.newBuilder() + .setCryptoKey(cryptoKey) + // Set of characters in the input text. For more info, see + // https://cloud.google.com/dlp/docs/reference/rest/v2/organizations.deidentifyTemplates#DeidentifyTemplate.FfxCommonNativeAlphabet + .setCommonAlphabet(FfxCommonNativeAlphabet.NUMERIC) + .build(); + PrimitiveTransformation primitiveTransformation = + PrimitiveTransformation.newBuilder() + .setCryptoReplaceFfxFpeConfig(cryptoReplaceFfxFpeConfig) + .build(); + + // Specify field to be decrypted. + FieldId fieldId = FieldId.newBuilder().setName("Employee ID").build(); + + // Associate the decryption with the specified field. + FieldTransformation fieldTransformation = + FieldTransformation.newBuilder() + .setPrimitiveTransformation(primitiveTransformation) + .addFields(fieldId) + .build(); + RecordTransformations transformations = + RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build(); + + DeidentifyConfig reidentifyConfig = + DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build(); + + // Combine configurations into a request for the service. + ReidentifyContentRequest request = + ReidentifyContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(contentItem) + .setReidentifyConfig(reidentifyConfig) + .build(); + + // Send the request and receive response from the service + ReidentifyContentResponse response = dlp.reidentifyContent(request); + + // Print the results + System.out.println("Table after re-identification: " + response.getItem().getValue()); + } + } +} +// [END dlp_reidentify_table_fpe] diff --git a/dlp/src/main/java/dlp/snippets/ReIdentifyTextWithFpe.java b/dlp/src/main/java/dlp/snippets/ReIdentifyTextWithFpe.java new file mode 100644 index 00000000000..1ccdb58ecf2 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/ReIdentifyTextWithFpe.java @@ -0,0 +1,128 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_reidentify_text_fpe] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.common.io.BaseEncoding; +import com.google.privacy.dlp.v2.ContentItem; +import com.google.privacy.dlp.v2.CryptoKey; +import com.google.privacy.dlp.v2.CryptoReplaceFfxFpeConfig; +import com.google.privacy.dlp.v2.CryptoReplaceFfxFpeConfig.FfxCommonNativeAlphabet; +import com.google.privacy.dlp.v2.CustomInfoType; +import com.google.privacy.dlp.v2.CustomInfoType.SurrogateType; +import com.google.privacy.dlp.v2.DeidentifyConfig; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InfoTypeTransformations; +import com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.KmsWrappedCryptoKey; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.PrimitiveTransformation; +import com.google.privacy.dlp.v2.ReidentifyContentRequest; +import com.google.privacy.dlp.v2.ReidentifyContentResponse; +import com.google.protobuf.ByteString; +import java.io.IOException; + +public class ReIdentifyTextWithFpe { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "your-project-id"; + String textToReIdentify = "My phone number is PHONE_TOKEN(10):9617256398"; + String kmsKeyName = + "projects/YOUR_PROJECT/" + + "locations/YOUR_KEYRING_REGION/" + + "keyRings/YOUR_KEYRING_NAME/" + + "cryptoKeys/YOUR_KEY_NAME"; + String wrappedAesKey = "YOUR_ENCRYPTED_AES_256_KEY"; + reIdentifyTextWithFpe(projectId, textToReIdentify, kmsKeyName, wrappedAesKey); + } + + public static void reIdentifyTextWithFpe( + String projectId, String textToReIdentify, String kmsKeyName, String wrappedAesKey) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify what content you want the service to re-identify. + ContentItem contentItem = ContentItem.newBuilder().setValue(textToReIdentify).build(); + + // Specify the type of info the inspection will re-identify. This must use the same custom + // into type that was used as a surrogate during the initial encryption. + InfoType surrogateInfoType = InfoType.newBuilder().setName("PHONE_NUMBER").build(); + + CustomInfoType customInfoType = + CustomInfoType.newBuilder() + .setInfoType(surrogateInfoType) + .setSurrogateType(SurrogateType.getDefaultInstance()) + .build(); + InspectConfig inspectConfig = + InspectConfig.newBuilder().addCustomInfoTypes(customInfoType).build(); + + // Specify an encrypted AES-256 key and the name of the Cloud KMS key that encrypted it. + KmsWrappedCryptoKey kmsWrappedCryptoKey = + KmsWrappedCryptoKey.newBuilder() + .setWrappedKey(ByteString.copyFrom(BaseEncoding.base64().decode(wrappedAesKey))) + .setCryptoKeyName(kmsKeyName) + .build(); + CryptoKey cryptoKey = CryptoKey.newBuilder().setKmsWrapped(kmsWrappedCryptoKey).build(); + + // Specify how to un-encrypt the previously de-identified information. + CryptoReplaceFfxFpeConfig cryptoReplaceFfxFpeConfig = + CryptoReplaceFfxFpeConfig.newBuilder() + .setCryptoKey(cryptoKey) + // Set of characters in the input text. For more info, see + // https://cloud.google.com/dlp/docs/reference/rest/v2/organizations.deidentifyTemplates#DeidentifyTemplate.FfxCommonNativeAlphabet + .setCommonAlphabet(FfxCommonNativeAlphabet.NUMERIC) + .setSurrogateInfoType(surrogateInfoType) + .build(); + PrimitiveTransformation primitiveTransformation = + PrimitiveTransformation.newBuilder() + .setCryptoReplaceFfxFpeConfig(cryptoReplaceFfxFpeConfig) + .build(); + InfoTypeTransformation infoTypeTransformation = + InfoTypeTransformation.newBuilder() + .setPrimitiveTransformation(primitiveTransformation) + .addInfoTypes(surrogateInfoType) + .build(); + InfoTypeTransformations transformations = + InfoTypeTransformations.newBuilder().addTransformations(infoTypeTransformation).build(); + + DeidentifyConfig reidentifyConfig = + DeidentifyConfig.newBuilder().setInfoTypeTransformations(transformations).build(); + + // Combine configurations into a request for the service. + ReidentifyContentRequest request = + ReidentifyContentRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setItem(contentItem) + .setInspectConfig(inspectConfig) + .setReidentifyConfig(reidentifyConfig) + .build(); + + // Send the request and receive response from the service + ReidentifyContentResponse response = dlp.reidentifyContent(request); + + // Print the results + System.out.println("Text after re-identification: " + response.getItem().getValue()); + } + } +} +// [END dlp_reidentify_text_fpe] diff --git a/dlp/src/main/java/dlp/snippets/ReIdentifyWithFpe.java b/dlp/src/main/java/dlp/snippets/ReIdentifyWithFpe.java index 8847890c13e..02c436cd2aa 100644 --- a/dlp/src/main/java/dlp/snippets/ReIdentifyWithFpe.java +++ b/dlp/src/main/java/dlp/snippets/ReIdentifyWithFpe.java @@ -32,8 +32,8 @@ import com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation; import com.google.privacy.dlp.v2.InspectConfig; import com.google.privacy.dlp.v2.KmsWrappedCryptoKey; +import com.google.privacy.dlp.v2.LocationName; import com.google.privacy.dlp.v2.PrimitiveTransformation; -import com.google.privacy.dlp.v2.ProjectName; import com.google.privacy.dlp.v2.ReidentifyContentRequest; import com.google.privacy.dlp.v2.ReidentifyContentResponse; import com.google.protobuf.ByteString; @@ -41,7 +41,7 @@ public class ReIdentifyWithFpe { - public static void reIdentifyWithFpe() throws IOException { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String textToReIdentify = "My SSN is SSN_TOKEN(9):731997681"; @@ -111,7 +111,7 @@ public static void reIdentifyWithFpe( // Combine configurations into a request for the service. ReidentifyContentRequest request = ReidentifyContentRequest.newBuilder() - .setParent(ProjectName.of(projectId).toString()) + .setParent(LocationName.of(projectId, "global").toString()) .setItem(contentItem) .setInspectConfig(inspectConfig) .setReidentifyConfig(reidentifyConfig) diff --git a/dlp/src/main/java/dlp/snippets/RedactImageFile.java b/dlp/src/main/java/dlp/snippets/RedactImageFile.java index 86413db1619..5d574a4a242 100644 --- a/dlp/src/main/java/dlp/snippets/RedactImageFile.java +++ b/dlp/src/main/java/dlp/snippets/RedactImageFile.java @@ -17,40 +17,41 @@ package dlp.snippets; // [START dlp_redact_image] + import com.google.cloud.dlp.v2.DlpServiceClient; import com.google.privacy.dlp.v2.ByteContentItem; import com.google.privacy.dlp.v2.ByteContentItem.BytesType; import com.google.privacy.dlp.v2.InfoType; import com.google.privacy.dlp.v2.InspectConfig; import com.google.privacy.dlp.v2.Likelihood; -import com.google.privacy.dlp.v2.ProjectName; +import com.google.privacy.dlp.v2.LocationName; import com.google.privacy.dlp.v2.RedactImageRequest; import com.google.privacy.dlp.v2.RedactImageResponse; import com.google.protobuf.ByteString; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.List; class RedactImageFile { - public static void redactImageFile() { + public static void main(String[] args) throws IOException { // TODO(developer): Replace these variables before running the sample. - String projectId = "your-project-id"; - String filePath = "path/to/image.png"; - redactImageFile(projectId, filePath); + String projectId = "my-project-id"; + String inputPath = "src/test/resources/test.png"; + String outputPath = "redacted.png"; + redactImageFile(projectId, inputPath, outputPath); } - static void redactImageFile(String projectId, String filePath) { + static void redactImageFile(String projectId, String inputPath, String outputPath) + throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (DlpServiceClient dlp = DlpServiceClient.create()) { - // Specify the project used for request. - ProjectName project = ProjectName.of(projectId); - // Specify the content to be inspected. - ByteString fileBytes = ByteString.readFrom(new FileInputStream(filePath)); + ByteString fileBytes = ByteString.readFrom(new FileInputStream(inputPath)); ByteContentItem byteItem = ByteContentItem.newBuilder().setType(BytesType.IMAGE).setData(fileBytes).build(); @@ -69,7 +70,7 @@ static void redactImageFile(String projectId, String filePath) { // Construct the Redact request to be sent by the client. RedactImageRequest request = RedactImageRequest.newBuilder() - .setParent(project.toString()) + .setParent(LocationName.of(projectId, "global").toString()) .setByteItem(byteItem) .setInspectConfig(config) .build(); @@ -78,14 +79,11 @@ static void redactImageFile(String projectId, String filePath) { RedactImageResponse response = dlp.redactImage(request); // Parse the response and process results. - String outputPath = "redacted.png"; FileOutputStream redacted = new FileOutputStream(outputPath); redacted.write(response.getRedactedImage().toByteArray()); redacted.close(); System.out.println("Redacted image written to " + outputPath); - } catch (Exception e) { - System.out.println("Error during inspectFile: \n" + e.toString()); } } } diff --git a/dlp/src/main/java/dlp/snippets/RedactImageFileAllInfoTypes.java b/dlp/src/main/java/dlp/snippets/RedactImageFileAllInfoTypes.java new file mode 100644 index 00000000000..c7223fcfdcf --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/RedactImageFileAllInfoTypes.java @@ -0,0 +1,72 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_redact_image_all_infotypes] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ByteContentItem; +import com.google.privacy.dlp.v2.ByteContentItem.BytesType; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.RedactImageRequest; +import com.google.privacy.dlp.v2.RedactImageResponse; +import com.google.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +class RedactImageFileAllInfoTypes { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String inputPath = "src/test/resources/sensitive-data-image.jpeg"; + String outputPath = "sensitive-data-image-redacted.jpeg"; + redactImageFileAllInfoTypes(projectId, inputPath, outputPath); + } + + static void redactImageFileAllInfoTypes(String projectId, String inputPath, String outputPath) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the content to be redacted. + ByteString fileBytes = ByteString.readFrom(new FileInputStream(inputPath)); + ByteContentItem byteItem = + ByteContentItem.newBuilder().setType(BytesType.IMAGE_JPEG).setData(fileBytes).build(); + + // Construct the Redact request to be sent by the client. + // Do not specify the type of info to redact. + RedactImageRequest request = + RedactImageRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setByteItem(byteItem) + .build(); + + // Use the client to send the API request. + RedactImageResponse response = dlp.redactImage(request); + + // Parse the response and process results. + FileOutputStream redacted = new FileOutputStream(outputPath); + redacted.write(response.getRedactedImage().toByteArray()); + redacted.close(); + System.out.println("Redacted image written to " + outputPath); + } + } +} +// [END dlp_redact_image_all_infotypes] diff --git a/dlp/src/main/java/dlp/snippets/RedactImageFileAllText.java b/dlp/src/main/java/dlp/snippets/RedactImageFileAllText.java new file mode 100644 index 00000000000..c875f2e00db --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/RedactImageFileAllText.java @@ -0,0 +1,78 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_redact_image_all_text] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ByteContentItem; +import com.google.privacy.dlp.v2.ByteContentItem.BytesType; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.RedactImageRequest; +import com.google.privacy.dlp.v2.RedactImageRequest.ImageRedactionConfig; +import com.google.privacy.dlp.v2.RedactImageResponse; +import com.google.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +class RedactImageFileAllText { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String inputPath = "src/test/resources/sensitive-data-image.jpeg"; + String outputPath = "sensitive-data-image-redacted.jpeg"; + redactImageFileAllText(projectId, inputPath, outputPath); + } + + static void redactImageFileAllText(String projectId, String inputPath, String outputPath) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the content to be redacted. + ByteString fileBytes = ByteString.readFrom(new FileInputStream(inputPath)); + ByteContentItem byteItem = + ByteContentItem.newBuilder().setType(BytesType.IMAGE_JPEG).setData(fileBytes).build(); + + // Enable redaction of all text. + ImageRedactionConfig imageRedactionConfig = + ImageRedactionConfig.newBuilder().setRedactAllText(true).build(); + + // Construct the Redact request to be sent by the client. + // Do not specify the type of info to redact. + RedactImageRequest request = + RedactImageRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setByteItem(byteItem) + .addImageRedactionConfigs(imageRedactionConfig) + .build(); + + // Use the client to send the API request. + RedactImageResponse response = dlp.redactImage(request); + + // Parse the response and process results. + FileOutputStream redacted = new FileOutputStream(outputPath); + redacted.write(response.getRedactedImage().toByteArray()); + redacted.close(); + System.out.println("Redacted image written to " + outputPath); + } + } +} +// [END dlp_redact_image_all_text] diff --git a/dlp/src/main/java/dlp/snippets/RedactImageFileColoredInfoTypes.java b/dlp/src/main/java/dlp/snippets/RedactImageFileColoredInfoTypes.java new file mode 100644 index 00000000000..fd412ce31db --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/RedactImageFileColoredInfoTypes.java @@ -0,0 +1,108 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_redact_image_colored_infotypes] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ByteContentItem; +import com.google.privacy.dlp.v2.ByteContentItem.BytesType; +import com.google.privacy.dlp.v2.Color; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.RedactImageRequest; +import com.google.privacy.dlp.v2.RedactImageRequest.ImageRedactionConfig; +import com.google.privacy.dlp.v2.RedactImageResponse; +import com.google.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +class RedactImageFileColoredInfoTypes { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String inputPath = "src/test/resources/test.png"; + String outputPath = "redacted.png"; + redactImageFileColoredInfoTypes(projectId, inputPath, outputPath); + } + + static void redactImageFileColoredInfoTypes(String projectId, String inputPath, String outputPath) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the content to be redacted. + ByteString fileBytes = ByteString.readFrom(new FileInputStream(inputPath)); + ByteContentItem byteItem = + ByteContentItem.newBuilder().setType(BytesType.IMAGE_JPEG).setData(fileBytes).build(); + + // Define types of info to redact associate each one with a different color. + // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types + ImageRedactionConfig ssnRedactionConfig = ImageRedactionConfig.newBuilder() + .setInfoType(InfoType.newBuilder().setName("US_SOCIAL_SECURITY_NUMBER").build()) + .setRedactionColor(Color.newBuilder().setRed(.3f).setGreen(.1f).setBlue(.6f).build()) + .build(); + ImageRedactionConfig emailRedactionConfig = ImageRedactionConfig.newBuilder() + .setInfoType(InfoType.newBuilder().setName("EMAIL_ADDRESS").build()) + .setRedactionColor(Color.newBuilder().setRed(.5f).setGreen(.5f).setBlue(1).build()) + .build(); + ImageRedactionConfig phoneRedactionConfig = ImageRedactionConfig.newBuilder() + .setInfoType(InfoType.newBuilder().setName("PHONE_NUMBER").build()) + .setRedactionColor(Color.newBuilder().setRed(1).setGreen(0).setBlue(.6f).build()) + .build(); + + // Create collection of all redact configurations. + List imageRedactionConfigs = + Arrays.asList(ssnRedactionConfig, emailRedactionConfig, phoneRedactionConfig); + + // List types of info to search for. + InspectConfig config = + InspectConfig.newBuilder() + .addAllInfoTypes(imageRedactionConfigs.stream() + .map(ImageRedactionConfig::getInfoType) + .collect(Collectors.toList())) + .build(); + + // Construct the Redact request to be sent by the client. + RedactImageRequest request = + RedactImageRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setByteItem(byteItem) + .addAllImageRedactionConfigs(imageRedactionConfigs) + .setInspectConfig(config) + .build(); + + // Use the client to send the API request. + RedactImageResponse response = dlp.redactImage(request); + + // Parse the response and process results. + FileOutputStream redacted = new FileOutputStream(outputPath); + redacted.write(response.getRedactedImage().toByteArray()); + redacted.close(); + System.out.println("Redacted image written to " + outputPath); + + } + } +} +// [END dlp_redact_image_colored_infotypes] diff --git a/dlp/src/main/java/dlp/snippets/RedactImageFileListedInfoTypes.java b/dlp/src/main/java/dlp/snippets/RedactImageFileListedInfoTypes.java new file mode 100644 index 00000000000..7c8c8c871c9 --- /dev/null +++ b/dlp/src/main/java/dlp/snippets/RedactImageFileListedInfoTypes.java @@ -0,0 +1,97 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +// [START dlp_redact_image_listed_infotypes] + +import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.privacy.dlp.v2.ByteContentItem; +import com.google.privacy.dlp.v2.ByteContentItem.BytesType; +import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.InspectConfig; +import com.google.privacy.dlp.v2.LocationName; +import com.google.privacy.dlp.v2.RedactImageRequest; +import com.google.privacy.dlp.v2.RedactImageRequest.ImageRedactionConfig; +import com.google.privacy.dlp.v2.RedactImageResponse; +import com.google.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +class RedactImageFileListedInfoTypes { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String inputPath = "src/test/resources/sensitive-data-image.jpeg"; + String outputPath = "sensitive-data-image-redacted.jpeg"; + redactImageFileListedInfoTypes(projectId, inputPath, outputPath); + } + + static void redactImageFileListedInfoTypes(String projectId, String inputPath, String outputPath) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (DlpServiceClient dlp = DlpServiceClient.create()) { + // Specify the content to be redacted. + ByteString fileBytes = ByteString.readFrom(new FileInputStream(inputPath)); + ByteContentItem byteItem = + ByteContentItem.newBuilder().setType(BytesType.IMAGE_JPEG).setData(fileBytes).build(); + + // Specify the types of info necessary to redact. + List infoTypes = new ArrayList<>(); + // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types + for (String typeName : + new String[] {"US_SOCIAL_SECURITY_NUMBER", "EMAIL_ADDRESS", "PHONE_NUMBER"}) { + infoTypes.add(InfoType.newBuilder().setName(typeName).build()); + } + InspectConfig inspectConfig = + InspectConfig.newBuilder() + .addAllInfoTypes(infoTypes) + .build(); + + // Prepare redaction configs. + List imageRedactionConfigs = infoTypes.stream() + .map(infoType -> ImageRedactionConfig.newBuilder().setInfoType(infoType).build()) + .collect(Collectors.toList()); + + // Construct the Redact request to be sent by the client. + RedactImageRequest request = + RedactImageRequest.newBuilder() + .setParent(LocationName.of(projectId, "global").toString()) + .setByteItem(byteItem) + .addAllImageRedactionConfigs(imageRedactionConfigs) + .setInspectConfig(inspectConfig) + .build(); + + // Use the client to send the API request. + RedactImageResponse response = dlp.redactImage(request); + + // Parse the response and process results. + FileOutputStream redacted = new FileOutputStream(outputPath); + redacted.write(response.getRedactedImage().toByteArray()); + redacted.close(); + System.out.println("Redacted image written to " + outputPath); + + } + } +} +// [END dlp_redact_image_listed_infotypes] diff --git a/dlp/src/main/java/dlp/snippets/RiskAnalysisCategoricalStats.java b/dlp/src/main/java/dlp/snippets/RiskAnalysisCategoricalStats.java index e02253b3cb0..2383d777adc 100644 --- a/dlp/src/main/java/dlp/snippets/RiskAnalysisCategoricalStats.java +++ b/dlp/src/main/java/dlp/snippets/RiskAnalysisCategoricalStats.java @@ -31,21 +31,23 @@ import com.google.privacy.dlp.v2.DlpJob; import com.google.privacy.dlp.v2.FieldId; import com.google.privacy.dlp.v2.GetDlpJobRequest; +import com.google.privacy.dlp.v2.LocationName; import com.google.privacy.dlp.v2.PrivacyMetric; import com.google.privacy.dlp.v2.PrivacyMetric.CategoricalStatsConfig; -import com.google.privacy.dlp.v2.ProjectName; import com.google.privacy.dlp.v2.RiskAnalysisJobConfig; import com.google.privacy.dlp.v2.ValueFrequency; import com.google.pubsub.v1.ProjectSubscriptionName; import com.google.pubsub.v1.ProjectTopicName; import com.google.pubsub.v1.PubsubMessage; +import java.io.IOException; import java.util.List; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; class RiskAnalysisCategoricalStats { - public static void categoricalStatsAnalysis() throws Exception { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String datasetId = "your-bigquery-dataset-id"; @@ -57,7 +59,7 @@ public static void categoricalStatsAnalysis() throws Exception { public static void categoricalStatsAnalysis( String projectId, String datasetId, String tableId, String topicId, String subscriptionId) - throws Exception { + throws ExecutionException, InterruptedException, IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. @@ -97,7 +99,7 @@ public static void categoricalStatsAnalysis( // Build the job creation request to be sent by the client CreateDlpJobRequest createDlpJobRequest = CreateDlpJobRequest.newBuilder() - .setParent(ProjectName.of(projectId).toString()) + .setParent(LocationName.of(projectId, "global").toString()) .setRiskJob(riskAnalysisJobConfig) .build(); @@ -120,10 +122,13 @@ public static void categoricalStatsAnalysis( // Wait for job completion semi-synchronously // For long jobs, consider using a truly asynchronous execution model such as Cloud Functions try { - done.get(1, TimeUnit.MINUTES); - Thread.sleep(500); // Wait for the job to become available + done.get(15, TimeUnit.MINUTES); } catch (TimeoutException e) { - System.out.println("Unable to verify job completion."); + System.out.println("Job was not completed after 15 minutes."); + return; + } finally { + subscriber.stopAsync(); + subscriber.awaitTerminated(); } // Build a request to get the completed job diff --git a/dlp/src/main/java/dlp/snippets/RiskAnalysisKAnonymity.java b/dlp/src/main/java/dlp/snippets/RiskAnalysisKAnonymity.java index bd918d92043..252c87c2ad4 100644 --- a/dlp/src/main/java/dlp/snippets/RiskAnalysisKAnonymity.java +++ b/dlp/src/main/java/dlp/snippets/RiskAnalysisKAnonymity.java @@ -33,23 +33,25 @@ import com.google.privacy.dlp.v2.DlpJob; import com.google.privacy.dlp.v2.FieldId; import com.google.privacy.dlp.v2.GetDlpJobRequest; +import com.google.privacy.dlp.v2.LocationName; import com.google.privacy.dlp.v2.PrivacyMetric; import com.google.privacy.dlp.v2.PrivacyMetric.KAnonymityConfig; -import com.google.privacy.dlp.v2.ProjectName; import com.google.privacy.dlp.v2.RiskAnalysisJobConfig; import com.google.privacy.dlp.v2.Value; import com.google.pubsub.v1.ProjectSubscriptionName; import com.google.pubsub.v1.ProjectTopicName; import com.google.pubsub.v1.PubsubMessage; +import java.io.IOException; import java.util.Arrays; import java.util.List; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.stream.Collectors; class RiskAnalysisKAnonymity { - public static void calculateKAnonymity() throws Exception { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String datasetId = "your-bigquery-dataset-id"; @@ -61,7 +63,7 @@ public static void calculateKAnonymity() throws Exception { public static void calculateKAnonymity( String projectId, String datasetId, String tableId, String topicId, String subscriptionId) - throws Exception { + throws ExecutionException, InterruptedException, IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. @@ -105,7 +107,7 @@ public static void calculateKAnonymity( // Build the request to be sent by the client CreateDlpJobRequest createDlpJobRequest = CreateDlpJobRequest.newBuilder() - .setParent(ProjectName.of(projectId).toString()) + .setParent(LocationName.of(projectId, "global").toString()) .setRiskJob(riskAnalysisJobConfig) .build(); @@ -128,10 +130,13 @@ public static void calculateKAnonymity( // Wait for job completion semi-synchronously // For long jobs, consider using a truly asynchronous execution model such as Cloud Functions try { - done.get(1, TimeUnit.MINUTES); - Thread.sleep(500); // Wait for the job to become available + done.get(15, TimeUnit.MINUTES); } catch (TimeoutException e) { - System.out.println("Unable to verify job completion."); + System.out.println("Job was not completed after 15 minutes."); + return; + } finally { + subscriber.stopAsync(); + subscriber.awaitTerminated(); } // Build a request to get the completed job diff --git a/dlp/src/main/java/dlp/snippets/RiskAnalysisKMap.java b/dlp/src/main/java/dlp/snippets/RiskAnalysisKMap.java index 556452fd6f8..a698b0be5e3 100644 --- a/dlp/src/main/java/dlp/snippets/RiskAnalysisKMap.java +++ b/dlp/src/main/java/dlp/snippets/RiskAnalysisKMap.java @@ -33,24 +33,26 @@ import com.google.privacy.dlp.v2.FieldId; import com.google.privacy.dlp.v2.GetDlpJobRequest; import com.google.privacy.dlp.v2.InfoType; +import com.google.privacy.dlp.v2.LocationName; import com.google.privacy.dlp.v2.PrivacyMetric; import com.google.privacy.dlp.v2.PrivacyMetric.KMapEstimationConfig; import com.google.privacy.dlp.v2.PrivacyMetric.KMapEstimationConfig.TaggedField; -import com.google.privacy.dlp.v2.ProjectName; import com.google.privacy.dlp.v2.RiskAnalysisJobConfig; import com.google.pubsub.v1.ProjectSubscriptionName; import com.google.pubsub.v1.ProjectTopicName; import com.google.pubsub.v1.PubsubMessage; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.stream.Collectors; class RiskAnalysisKMap { - public static void calculateKMap() throws Exception { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String datasetId = "your-bigquery-dataset-id"; @@ -62,8 +64,7 @@ public static void calculateKMap() throws Exception { public static void calculateKMap( String projectId, String datasetId, String tableId, String topicId, String subscriptionId) - throws Exception { - + throws ExecutionException, InterruptedException, IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. @@ -131,7 +132,7 @@ public static void calculateKMap( // Build the request to be sent by the client CreateDlpJobRequest createDlpJobRequest = CreateDlpJobRequest.newBuilder() - .setParent(ProjectName.of(projectId).toString()) + .setParent(LocationName.of(projectId, "global").toString()) .setRiskJob(riskAnalysisJobConfig) .build(); @@ -154,10 +155,13 @@ public static void calculateKMap( // Wait for job completion semi-synchronously // For long jobs, consider using a truly asynchronous execution model such as Cloud Functions try { - done.get(1, TimeUnit.MINUTES); - Thread.sleep(500); // Wait for the job to become available + done.get(15, TimeUnit.MINUTES); } catch (TimeoutException e) { - System.out.println("Unable to verify job completion."); + System.out.println("Job was not completed after 15 minutes."); + return; + } finally { + subscriber.stopAsync(); + subscriber.awaitTerminated(); } // Build a request to get the completed job diff --git a/dlp/src/main/java/dlp/snippets/RiskAnalysisLDiversity.java b/dlp/src/main/java/dlp/snippets/RiskAnalysisLDiversity.java index 3ae4b589cdd..005e54d2152 100644 --- a/dlp/src/main/java/dlp/snippets/RiskAnalysisLDiversity.java +++ b/dlp/src/main/java/dlp/snippets/RiskAnalysisLDiversity.java @@ -33,24 +33,26 @@ import com.google.privacy.dlp.v2.DlpJob; import com.google.privacy.dlp.v2.FieldId; import com.google.privacy.dlp.v2.GetDlpJobRequest; +import com.google.privacy.dlp.v2.LocationName; import com.google.privacy.dlp.v2.PrivacyMetric; import com.google.privacy.dlp.v2.PrivacyMetric.LDiversityConfig; -import com.google.privacy.dlp.v2.ProjectName; import com.google.privacy.dlp.v2.RiskAnalysisJobConfig; import com.google.privacy.dlp.v2.Value; import com.google.privacy.dlp.v2.ValueFrequency; import com.google.pubsub.v1.ProjectSubscriptionName; import com.google.pubsub.v1.ProjectTopicName; import com.google.pubsub.v1.PubsubMessage; +import java.io.IOException; import java.util.Arrays; import java.util.List; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.stream.Collectors; class RiskAnalysisLDiversity { - public static void calculateLDiversity() throws Exception { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String datasetId = "your-bigquery-dataset-id"; @@ -62,7 +64,7 @@ public static void calculateLDiversity() throws Exception { public static void calculateLDiversity( String projectId, String datasetId, String tableId, String topicId, String subscriptionId) - throws Exception { + throws ExecutionException, InterruptedException, IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call @@ -113,7 +115,7 @@ public static void calculateLDiversity( // Build the request to be sent by the client CreateDlpJobRequest createDlpJobRequest = CreateDlpJobRequest.newBuilder() - .setParent(ProjectName.of(projectId).toString()) + .setParent(LocationName.of(projectId, "global").toString()) .setRiskJob(riskAnalysisJobConfig) .build(); @@ -136,10 +138,13 @@ public static void calculateLDiversity( // Wait for job completion semi-synchronously // For long jobs, consider using a truly asynchronous execution model such as Cloud Functions try { - done.get(1, TimeUnit.MINUTES); - Thread.sleep(500); // Wait for the job to become available + done.get(15, TimeUnit.MINUTES); } catch (TimeoutException e) { - System.out.println("Unable to verify job completion."); + System.out.println("Job was not completed after 15 minutes."); + return; + } finally { + subscriber.stopAsync(); + subscriber.awaitTerminated(); } // Build a request to get the completed job diff --git a/dlp/src/main/java/dlp/snippets/RiskAnalysisNumericalStats.java b/dlp/src/main/java/dlp/snippets/RiskAnalysisNumericalStats.java index 5094b81b3cf..dde9467f90d 100644 --- a/dlp/src/main/java/dlp/snippets/RiskAnalysisNumericalStats.java +++ b/dlp/src/main/java/dlp/snippets/RiskAnalysisNumericalStats.java @@ -31,20 +31,22 @@ import com.google.privacy.dlp.v2.DlpJob; import com.google.privacy.dlp.v2.FieldId; import com.google.privacy.dlp.v2.GetDlpJobRequest; +import com.google.privacy.dlp.v2.LocationName; import com.google.privacy.dlp.v2.PrivacyMetric; import com.google.privacy.dlp.v2.PrivacyMetric.NumericalStatsConfig; -import com.google.privacy.dlp.v2.ProjectName; import com.google.privacy.dlp.v2.RiskAnalysisJobConfig; import com.google.privacy.dlp.v2.Value; import com.google.pubsub.v1.ProjectSubscriptionName; import com.google.pubsub.v1.ProjectTopicName; import com.google.pubsub.v1.PubsubMessage; +import java.io.IOException; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; class RiskAnalysisNumericalStats { - public static void numericalStatsAnalysis() throws Exception { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String datasetId = "your-bigquery-dataset-id"; @@ -56,7 +58,7 @@ public static void numericalStatsAnalysis() throws Exception { public static void numericalStatsAnalysis( String projectId, String datasetId, String tableId, String topicId, String subscriptionId) - throws Exception { + throws ExecutionException, InterruptedException, IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call @@ -97,7 +99,7 @@ public static void numericalStatsAnalysis( CreateDlpJobRequest createDlpJobRequest = CreateDlpJobRequest.newBuilder() - .setParent(ProjectName.of(projectId).toString()) + .setParent(LocationName.of(projectId, "global").toString()) .setRiskJob(riskAnalysisJobConfig) .build(); @@ -120,10 +122,13 @@ public static void numericalStatsAnalysis( // Wait for job completion semi-synchronously // For long jobs, consider using a truly asynchronous execution model such as Cloud Functions try { - done.get(1, TimeUnit.MINUTES); - Thread.sleep(500); // Wait for the job to become available + done.get(15, TimeUnit.MINUTES); } catch (TimeoutException e) { - System.out.println("Unable to verify job completion."); + System.out.println("Job was not completed after 15 minutes."); + return; + } finally { + subscriber.stopAsync(); + subscriber.awaitTerminated(); } // Build a request to get the completed job diff --git a/dlp/src/main/java/dlp/snippets/TemplatesCreate.java b/dlp/src/main/java/dlp/snippets/TemplatesCreate.java index 26b037736db..fe8ef939bb1 100644 --- a/dlp/src/main/java/dlp/snippets/TemplatesCreate.java +++ b/dlp/src/main/java/dlp/snippets/TemplatesCreate.java @@ -23,7 +23,7 @@ import com.google.privacy.dlp.v2.InfoType; import com.google.privacy.dlp.v2.InspectConfig; import com.google.privacy.dlp.v2.InspectTemplate; -import com.google.privacy.dlp.v2.ProjectName; +import com.google.privacy.dlp.v2.LocationName; import java.io.IOException; import java.util.List; import java.util.stream.Collectors; @@ -31,7 +31,7 @@ class TemplatesCreate { - public static void createInspectTemplate() throws IOException { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; createInspectTemplate(projectId); @@ -68,7 +68,7 @@ public static void createInspectTemplate(String projectId) throws IOException { // Create the request to be sent by the client CreateInspectTemplateRequest createInspectTemplateRequest = CreateInspectTemplateRequest.newBuilder() - .setParent(ProjectName.of(projectId).toString()) + .setParent(LocationName.of(projectId, "global").toString()) .setInspectTemplate(inspectTemplate) .build(); diff --git a/dlp/src/main/java/dlp/snippets/TemplatesDelete.java b/dlp/src/main/java/dlp/snippets/TemplatesDelete.java index d291c87e6bc..f22c362ff9f 100644 --- a/dlp/src/main/java/dlp/snippets/TemplatesDelete.java +++ b/dlp/src/main/java/dlp/snippets/TemplatesDelete.java @@ -24,7 +24,7 @@ class TemplatesDelete { - public static void deleteInspectTemplate() throws IOException { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String templateId = "your-template-id"; diff --git a/dlp/src/main/java/dlp/snippets/TemplatesList.java b/dlp/src/main/java/dlp/snippets/TemplatesList.java index a7217e31713..729782f529b 100644 --- a/dlp/src/main/java/dlp/snippets/TemplatesList.java +++ b/dlp/src/main/java/dlp/snippets/TemplatesList.java @@ -24,12 +24,12 @@ import com.google.privacy.dlp.v2.InspectConfig; import com.google.privacy.dlp.v2.InspectTemplate; import com.google.privacy.dlp.v2.ListInspectTemplatesRequest; -import com.google.privacy.dlp.v2.ProjectName; +import com.google.privacy.dlp.v2.LocationName; import java.io.IOException; class TemplatesList { - public static void listInspectTemplates() throws IOException { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; listInspectTemplates(projectId); @@ -45,7 +45,7 @@ public static void listInspectTemplates(String projectId) throws IOException { // Create the request to be sent by the client ListInspectTemplatesRequest request = ListInspectTemplatesRequest.newBuilder() - .setParent(ProjectName.of(projectId).toString()) + .setParent(LocationName.of(projectId, "global").toString()) .setPageSize(1) .build(); diff --git a/dlp/src/main/java/dlp/snippets/TriggersCreate.java b/dlp/src/main/java/dlp/snippets/TriggersCreate.java index 209e9e4dbce..de0dd80dfd8 100644 --- a/dlp/src/main/java/dlp/snippets/TriggersCreate.java +++ b/dlp/src/main/java/dlp/snippets/TriggersCreate.java @@ -25,25 +25,26 @@ import com.google.privacy.dlp.v2.InspectConfig; import com.google.privacy.dlp.v2.InspectJobConfig; import com.google.privacy.dlp.v2.JobTrigger; -import com.google.privacy.dlp.v2.ProjectName; +import com.google.privacy.dlp.v2.LocationName; import com.google.privacy.dlp.v2.Schedule; import com.google.privacy.dlp.v2.StorageConfig; import com.google.privacy.dlp.v2.StorageConfig.TimespanConfig; import com.google.protobuf.Duration; +import java.io.IOException; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class TriggersCreate { - public static void createTrigger() throws Exception { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String gcsPath = "gs://" + "your-bucket-name" + "path/to/file.txt"; createTrigger(projectId, gcsPath); } - public static void createTrigger(String projectId, String gcsPath) throws Exception { + public static void createTrigger(String projectId, String gcsPath) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. @@ -106,7 +107,7 @@ public static void createTrigger(String projectId, String gcsPath) throws Except // Create scan request to be sent by client CreateJobTriggerRequest createJobTriggerRequest = CreateJobTriggerRequest.newBuilder() - .setParent(ProjectName.of(projectId).toString()) + .setParent(LocationName.of(projectId, "global").toString()) .setJobTrigger(jobTrigger) .build(); diff --git a/dlp/src/main/java/dlp/snippets/TriggersDelete.java b/dlp/src/main/java/dlp/snippets/TriggersDelete.java index e569ad98d73..539f2537dbc 100644 --- a/dlp/src/main/java/dlp/snippets/TriggersDelete.java +++ b/dlp/src/main/java/dlp/snippets/TriggersDelete.java @@ -20,17 +20,18 @@ import com.google.cloud.dlp.v2.DlpServiceClient; import com.google.privacy.dlp.v2.DeleteJobTriggerRequest; import com.google.privacy.dlp.v2.ProjectJobTriggerName; +import java.io.IOException; class TriggersDelete { - public static void deleteTrigger() throws Exception { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; String triggerId = "your-trigger-id"; deleteTrigger(projectId, triggerId); } - public static void deleteTrigger(String projectId, String triggerId) throws Exception { + public static void deleteTrigger(String projectId, String triggerId) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. diff --git a/dlp/src/main/java/dlp/snippets/TriggersList.java b/dlp/src/main/java/dlp/snippets/TriggersList.java index cc0c24103c4..d52146399d7 100644 --- a/dlp/src/main/java/dlp/snippets/TriggersList.java +++ b/dlp/src/main/java/dlp/snippets/TriggersList.java @@ -17,19 +17,21 @@ package dlp.snippets; // [START dlp_list_triggers] + import com.google.cloud.dlp.v2.DlpServiceClient; import com.google.privacy.dlp.v2.JobTrigger; import com.google.privacy.dlp.v2.ListJobTriggersRequest; -import com.google.privacy.dlp.v2.ProjectName; +import com.google.privacy.dlp.v2.LocationName; +import java.io.IOException; class TriggersList { - public static void listTriggers() throws Exception { + public static void main(String[] args) throws Exception { // TODO(developer): Replace these variables before running the sample. String projectId = "your-project-id"; listTriggers(projectId); } - public static void listTriggers(String projectId) throws Exception { + public static void listTriggers(String projectId) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. @@ -37,7 +39,7 @@ public static void listTriggers(String projectId) throws Exception { // Build the request to be sent by the client ListJobTriggersRequest listJobTriggersRequest = ListJobTriggersRequest.newBuilder() - .setParent(ProjectName.of(projectId).toString()) + .setParent(LocationName.of(projectId, "global").toString()) .build(); // Use the client to send the API request. diff --git a/dlp/src/test/java/dlp/snippets/DeIdentificationTests.java b/dlp/src/test/java/dlp/snippets/DeIdentificationTests.java index b2b00b53210..c326559e98a 100644 --- a/dlp/src/test/java/dlp/snippets/DeIdentificationTests.java +++ b/dlp/src/test/java/dlp/snippets/DeIdentificationTests.java @@ -16,56 +16,30 @@ package dlp.snippets; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.StringContains.containsString; -import static org.junit.Assert.assertNotNull; +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; -import java.io.ByteArrayOutputStream; +import com.google.common.collect.ImmutableList; +import com.google.privacy.dlp.v2.FieldId; +import com.google.privacy.dlp.v2.Table; +import com.google.privacy.dlp.v2.Table.Row; +import com.google.privacy.dlp.v2.Value; import java.io.IOException; -import java.io.PrintStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import org.junit.After; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -public class DeIdentificationTests { +public class DeIdentificationTests extends TestBase { - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - // TODO: Update as ENV_VARs - private static final String bucketName = PROJECT_ID + "/dlp"; - private ByteArrayOutputStream bout; - private String wrappedKey = System.getenv("DLP_DEID_WRAPPED_KEY"); - private String kmsKeyName = System.getenv("DLP_DEID_KEY_NAME"); - - private static void requireEnvVar(String varName) { - assertNotNull( - String.format("Environment variable '%s' must be set to perform these tests.", varName), - System.getenv(varName)); - } - - @Before - public void checkRequirements() { - requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); - requireEnvVar("GOOGLE_CLOUD_PROJECT"); - requireEnvVar("DLP_DEID_WRAPPED_KEY"); - requireEnvVar("DLP_DEID_KEY_NAME"); - } - - @Before - public void captureOut() { - bout = new ByteArrayOutputStream(); - System.setOut(new PrintStream(bout)); - } - - @After - public void releaseOut() { - System.setOut(null); - bout.reset(); + @Override + protected ImmutableList requiredEnvVars() { + return ImmutableList + .of("GOOGLE_APPLICATION_CREDENTIALS", "GOOGLE_CLOUD_PROJECT", "DLP_DEID_WRAPPED_KEY", + "DLP_DEID_KEY_NAME"); } @Test @@ -73,7 +47,7 @@ public void testDeIdentifyWithMasking() throws IOException { DeIdentifyWithMasking.deIdentifyWithMasking(PROJECT_ID, "My SSN is 372819127"); String output = bout.toString(); - assertThat(output, containsString("Text after masking:")); + assertThat(output).contains("Text after masking:"); } @Test @@ -81,7 +55,7 @@ public void testDeIdentifyWithFpe() throws IOException { DeIdentifyWithFpe.deIdentifyWithFpe(PROJECT_ID, "My SSN is 372819127", kmsKeyName, wrappedKey); String output = bout.toString(); - assertThat(output, containsString("Text after format-preserving encryption:")); + assertThat(output).contains("Text after format-preserving encryption:"); } @Test @@ -90,21 +64,415 @@ public void testReIdentifyWithFpe() throws IOException { PROJECT_ID, "My SSN is SSN_TOKEN(9):731997681", kmsKeyName, wrappedKey); String output = bout.toString(); - assertThat(output, containsString("Text after re-identification:")); + assertThat(output).contains("Text after re-identification:"); + } + + @Test + public void testDeIdentifyTextWithFpe() throws IOException { + DeIdentifyTextWithFpe.deIdentifyTextWithFpe( + PROJECT_ID, "My phone number is 4359916732", kmsKeyName, wrappedKey); + + String output = bout.toString(); + assertThat(output).contains("Text after format-preserving encryption: "); + } + + @Test + public void testReIdentifyTextWithFpe() throws IOException { + ReIdentifyTextWithFpe.reIdentifyTextWithFpe( + PROJECT_ID, + "My phone number is PHONE_TOKEN(10):9617256398", + kmsKeyName, + wrappedKey); + + String output = bout.toString(); + assertThat(output).contains("Text after re-identification: "); + } + + @Test + public void testDeIdentifyTableWithFpe() throws IOException { + Table tableToDeIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("Employee ID").build()) + .addHeaders(FieldId.newBuilder().setName("Date").build()) + .addHeaders(FieldId.newBuilder().setName("Compensation").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("11111").build()) + .addValues(Value.newBuilder().setStringValue("2015").build()) + .addValues(Value.newBuilder().setStringValue("$10").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("11111").build()) + .addValues(Value.newBuilder().setStringValue("2016").build()) + .addValues(Value.newBuilder().setStringValue("$20").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22222").build()) + .addValues(Value.newBuilder().setStringValue("2016").build()) + .addValues(Value.newBuilder().setStringValue("$15").build()) + .build()) + .build(); + + DeIdentifyTableWithFpe.deIdentifyTableWithFpe( + PROJECT_ID, tableToDeIdentify, kmsKeyName, wrappedKey); + + String output = bout.toString(); + assertThat(output).contains("Table after format-preserving encryption:"); + } + + @Test + public void testReIdentifyTableWithFpe() throws IOException { + Table tableToReIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("Employee ID").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("28777").build()).build()) + .build(); + + ReIdentifyTableWithFpe.reIdentifyTableWithFpe( + PROJECT_ID, tableToReIdentify, kmsKeyName, wrappedKey); + + String output = bout.toString(); + assertThat(output).contains("Table after re-identification:"); + } + + @Test + public void testDeIdentifyTableBucketing() throws IOException { + // Transform a column based on the value of another column + Table tableToDeIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .build()) + .build(); + Table expectedTable = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("90:100").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("20:30").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("70:80").build()) + .build()) + .build(); + + Table table = DeIdentifyTableBucketing.deIdentifyTableBucketing(PROJECT_ID, tableToDeIdentify); + + String output = bout.toString(); + assertThat(output).contains("Table after de-identification:"); + assertThat(table).isEqualTo(expectedTable); + } + + @Test + public void testDeIdentifyTableConditionMasking() throws IOException { + // Transform a column based on the value of another column + Table tableToDeIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .build()) + .build(); + Table expectedTable = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("**").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .build()) + .build(); + + Table table = DeIdentifyTableConditionMasking.deIdentifyTableConditionMasking( + PROJECT_ID, tableToDeIdentify); + + String output = bout.toString(); + assertThat(output).contains("Table after de-identification:"); + assertThat(table).isEqualTo(expectedTable); + } + + @Test + public void testDeIdentifyTableInfoTypes() throws IOException { + // Transform findings found in column + Table tableToDeIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addHeaders(FieldId.newBuilder().setName("FACTOID").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .addValues(Value.newBuilder().setStringValue( + "Charles Dickens name was a curse, possibly invented by Shakespeare.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .addValues(Value.newBuilder().setStringValue( + "There are 14 kisses in Jane Austen's novels.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain loved cats.").build()) + .build()) + .build(); + Table expectedTable = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addHeaders(FieldId.newBuilder().setName("FACTOID").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("[PERSON_NAME]").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .addValues(Value.newBuilder().setStringValue( + "[PERSON_NAME] name was a curse, possibly invented by Shakespeare.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("[PERSON_NAME]").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .addValues(Value.newBuilder().setStringValue( + "There are 14 kisses in [PERSON_NAME] novels.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("[PERSON_NAME]").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .addValues(Value.newBuilder().setStringValue("[PERSON_NAME] loved cats.").build()) + .build()) + .build(); + + Table table = DeIdentifyTableInfoTypes.deIdentifyTableInfoTypes(PROJECT_ID, tableToDeIdentify); + + String output = bout.toString(); + assertThat(output).contains("Table after de-identification:"); + assertThat(table).isEqualTo(expectedTable); + } + + @Test + public void testDeIdentifyTableRowSuppress() throws IOException { + // Suppress a row based on the content of a column + Table tableToDeIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .build()) + .build(); + Table expectedTable = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .build()) + .build(); + + Table table = DeIdentifyTableRowSuppress.deIdentifyTableRowSuppress( + PROJECT_ID, tableToDeIdentify); + + String output = bout.toString(); + assertThat(output).contains("Table after de-identification:"); + assertThat(table).isEqualTo(expectedTable); + } + + @Test + public void testDeIdentifyTableConditionsInfoTypes() throws IOException { + // Transform findings only when specific conditions are met on another field + Table tableToDeIdentify = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addHeaders(FieldId.newBuilder().setName("FACTOID").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .addValues(Value.newBuilder().setStringValue( + "Charles Dickens name was a curse, possibly invented by Shakespeare.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .addValues(Value.newBuilder().setStringValue( + "There are 14 kisses in Jane Austen's novels.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain loved cats.").build()) + .build()) + .build(); + Table expectedTable = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("AGE").build()) + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) + .addHeaders(FieldId.newBuilder().setName("FACTOID").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("101").build()) + .addValues(Value.newBuilder().setStringValue("[PERSON_NAME]").build()) + .addValues(Value.newBuilder().setStringValue("95").build()) + .addValues(Value.newBuilder().setStringValue( + "[PERSON_NAME] name was a curse, possibly invented by Shakespeare.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("22").build()) + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) + .addValues(Value.newBuilder().setStringValue("21").build()) + .addValues(Value.newBuilder().setStringValue( + "There are 14 kisses in Jane Austen's novels.").build()) + .build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("55").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) + .addValues(Value.newBuilder().setStringValue("75").build()) + .addValues(Value.newBuilder().setStringValue("Mark Twain loved cats.").build()) + .build()) + .build(); + + Table table = DeIdentifyTableConditionInfoTypes.deIdentifyTableConditionInfoTypes( + PROJECT_ID, tableToDeIdentify); + + String output = bout.toString(); + assertThat(output).contains("Table after de-identification:"); + assertThat(table).isEqualTo(expectedTable); } @Test public void testDeIdentifyWithDateShift() throws IOException { Path inputFile = Paths.get("src/test/resources/dates.csv"); - assertThat("Input file must exist", inputFile.toFile().exists()); + assertWithMessage("Input file must exist").that(inputFile.toFile().exists()).isTrue(); Path outputFile = Paths.get("src/test/resources/results.csv"); - assertThat("Output file must be writeable", inputFile.toFile().canWrite()); + assertWithMessage("Output file must be writeable").that(inputFile.toFile().canWrite()).isTrue(); DeIdentifyWithDateShift.deIdentifyWithDateShift(PROJECT_ID, inputFile, outputFile); String output = bout.toString(); - assertThat(output, containsString("Content written to file: ")); + assertThat(output).contains("Content written to file: "); // Clean up test output Files.delete(outputFile); } + + @Test + public void testDeIdentifyWithRedaction() throws IOException { + DeIdentifyWithRedaction.deIdentifyWithRedaction( + PROJECT_ID, + "My name is Alicia Abernathy, and my email address is aabernathy@example.com."); + + String output = bout.toString(); + assertThat(output).contains("Text after redaction: " + + "My name is Alicia Abernathy, and my email address is ."); + } + + @Test + public void testDeIdentifyWithReplacement() throws IOException { + DeIdentifyWithReplacement.deIdentifyWithReplacement( + PROJECT_ID, + "My name is Alicia Abernathy, and my email address is aabernathy@example.com."); + + String output = bout.toString(); + assertThat(output).contains("Text after redaction: " + + "My name is Alicia Abernathy, and my email address is [email-address]."); + } + + @Test + public void testDeIdentifyWithInfoType() throws IOException { + DeIdentifyWithInfoType.deIdentifyWithInfoType( + PROJECT_ID, + "My email is test@example.com"); + + String output = bout.toString(); + assertThat(output).contains("Text after redaction: " + + "My email is [EMAIL_ADDRESS]"); + } + + @Test + public void testDeIdentifyWithSimpleWordList() throws IOException { + DeIdentifyWithSimpleWordList.deidentifyWithSimpleWordList( + PROJECT_ID, "Patient was seen in RM-YELLOW then transferred to rm green."); + + String output = bout.toString(); + assertThat(output).contains("Text after replace with infotype config: "); + } + + @Test + public void testDeIdentifyWithExceptionList() throws IOException { + DeIdentifyWithExceptionList.deIdentifyWithExceptionList( + PROJECT_ID, "jack@example.org accessed customer record of user5@example.com"); + + String output = bout.toString(); + assertThat(output).contains("Text after replace with infotype config: "); + } } diff --git a/dlp/src/test/java/dlp/snippets/InfoTypesTests.java b/dlp/src/test/java/dlp/snippets/InfoTypesTests.java index c056bfcc990..39792218457 100644 --- a/dlp/src/test/java/dlp/snippets/InfoTypesTests.java +++ b/dlp/src/test/java/dlp/snippets/InfoTypesTests.java @@ -16,51 +16,26 @@ package dlp.snippets; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; +import static com.google.common.truth.Truth.assertThat; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import org.hamcrest.CoreMatchers; -import org.junit.After; -import org.junit.Before; +import com.google.common.collect.ImmutableList; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -public class InfoTypesTests { +public class InfoTypesTests extends TestBase { - private ByteArrayOutputStream bout; - - private static void requireEnvVar(String varName) { - assertNotNull( - String.format("Environment variable '%s' must be set to perform these tests.", varName), - System.getenv(varName)); - } - - @Before - public void checkRequirements() { - requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); - } - - @Before - public void setUp() { - bout = new ByteArrayOutputStream(); - System.setOut(new PrintStream(bout)); - } - - @After - public void tearDown() { - System.setOut(null); - bout.reset(); + @Override + protected ImmutableList requiredEnvVars() { + return ImmutableList.of("GOOGLE_APPLICATION_CREDENTIALS"); } @Test public void testListInfoTypes() throws Exception { InfoTypesList.listInfoTypes(); String output = bout.toString(); - assertThat(output, CoreMatchers.containsString("Name")); - assertThat(output, CoreMatchers.containsString("Display name")); + assertThat(output).contains("Name"); + assertThat(output).contains("Display name"); } } diff --git a/dlp/src/test/java/dlp/snippets/InspectTests.java b/dlp/src/test/java/dlp/snippets/InspectTests.java index d42ea7c758a..340bb8514be 100644 --- a/dlp/src/test/java/dlp/snippets/InspectTests.java +++ b/dlp/src/test/java/dlp/snippets/InspectTests.java @@ -16,17 +16,21 @@ package dlp.snippets; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.StringContains.containsString; -import static org.junit.Assert.assertNotNull; - -import com.google.cloud.dlp.v2.DlpServiceClient; -import com.google.privacy.dlp.v2.CancelDlpJobRequest; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.PrintStream; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; +import static com.google.common.truth.Truth.assertThat; + +import com.google.api.gax.rpc.ApiException; +import com.google.cloud.pubsub.v1.SubscriptionAdminClient; +import com.google.cloud.pubsub.v1.TopicAdminClient; +import com.google.common.collect.ImmutableList; +import com.google.privacy.dlp.v2.FieldId; +import com.google.privacy.dlp.v2.Table; +import com.google.privacy.dlp.v2.Table.Row; +import com.google.privacy.dlp.v2.Value; +import com.google.pubsub.v1.ProjectSubscriptionName; +import com.google.pubsub.v1.PushConfig; +import com.google.pubsub.v1.TopicName; +import java.util.Arrays; +import java.util.UUID; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -34,128 +38,330 @@ import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -public class InspectTests { - - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String GCS_PATH = System.getenv("GCS_PATH"); - private static final String TOPIC_ID = System.getenv("PUB_SUB_TOPIC"); - private static final String SUBSCRIPTION_ID = System.getenv("PUB_SUB_SUBSCRIPTION"); - private static final String DATASET_ID = System.getenv("BIGQUERY_DATASET"); - private static final String TABLE_ID = System.getenv("BIGQUERY_TABLE"); +public class InspectTests extends TestBase { + // TODO: Update as ENV_VARs private static final String datastoreNamespace = ""; private static final String datastoreKind = "dlp"; - private ByteArrayOutputStream bout; + private static final String DOCUMENT_INPUT_FILE = "src/test/resources/sensitive-data-image.jpg"; - private static void requireEnvVar(String varName) { - assertNotNull( - String.format("Environment variable '%s' must be set to perform these tests.", varName), - System.getenv(varName)); - } + private UUID testRunUuid = UUID.randomUUID(); + private TopicName topicName = TopicName.of( + PROJECT_ID, + String.format("%s-%s", TOPIC_ID, testRunUuid.toString())); + private ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of( + PROJECT_ID, + String.format("%s-%s", SUBSCRIPTION_ID, testRunUuid.toString())); - @Before - public void checkRequirements() { - requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); - requireEnvVar("GOOGLE_CLOUD_PROJECT"); - requireEnvVar("GCS_PATH"); - requireEnvVar("PUB_SUB_TOPIC"); - requireEnvVar("PUB_SUB_SUBSCRIPTION"); - requireEnvVar("BIGQUERY_DATASET"); - requireEnvVar("BIGQUERY_TABLE"); + @Override + protected ImmutableList requiredEnvVars() { + return ImmutableList + .of("GOOGLE_APPLICATION_CREDENTIALS", "GOOGLE_CLOUD_PROJECT", "GCS_PATH", "PUB_SUB_TOPIC", + "PUB_SUB_SUBSCRIPTION", "BIGQUERY_DATASET", "BIGQUERY_TABLE"); } @Before - public void captureOut() { - bout = new ByteArrayOutputStream(); - System.setOut(new PrintStream(bout)); + public void before() throws Exception { + // Create a new topic + try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { + topicAdminClient.createTopic(topicName); + } + + // Create a new subscription + try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) { + subscriptionAdminClient + .createSubscription(subscriptionName, topicName, PushConfig.getDefaultInstance(), 0); + } } + @After - public void releaseOut() { - System.setOut(null); - bout.reset(); + public void after() throws Exception { + // Delete the test topic + try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { + topicAdminClient.deleteTopic(topicName); + } catch (ApiException e) { + System.err.println(String.format("Error deleting topic %s: %s", + topicName.getTopic(), e)); + // Keep trying to clean up + } + + // Delete the test subscription + try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) { + subscriptionAdminClient.deleteSubscription(subscriptionName); + } catch (ApiException e) { + System.err.println(String.format("Error deleting subscription %s: %s", + subscriptionName.getSubscription(), e)); + // Keep trying to clean up + } } @Test - public void testInspectString() { + public void testInspectPhoneNumber() throws Exception { + InspectString.inspectString(PROJECT_ID, "My phone number is (415) 555-0890"); + + String output = bout.toString(); + assertThat(output).contains("Info type: PHONE_NUMBER"); + } + + @Test + public void testInspectString() throws Exception { InspectString.inspectString(PROJECT_ID, "I'm Gary and my email is gary@example.com"); String output = bout.toString(); - assertThat(output, containsString("Info type: EMAIL_ADDRESS")); + assertThat(output).contains("Info type: EMAIL_ADDRESS"); + } + + @Test + public void testInspectWithCustomRegex() throws Exception { + InspectWithCustomRegex.inspectWithCustomRegex( + PROJECT_ID, "Patients MRN 444-5-22222", "[1-9]{3}-[1-9]{1}-[1-9]{5}"); + + String output = bout.toString(); + assertThat(output).contains("Info type: C_MRN"); + } + + @Test + public void testInspectStringWithExclusionDict() throws Exception { + InspectStringWithExclusionDict.inspectStringWithExclusionDict(PROJECT_ID, + "Some email addresses: gary@example.com, example@example.com", + Arrays.asList("example@example.com")); + + String output = bout.toString(); + assertThat(output).contains("gary@example.com"); + assertThat(output).doesNotContain("example@example.com"); + } + + @Test + public void testInspectStringWithExclusionDictSubstring() throws Exception { + InspectStringWithExclusionDictSubstring.inspectStringWithExclusionDictSubstring(PROJECT_ID, + "Some email addresses: gary@example.com, TEST@example.com", + Arrays.asList("TEST")); + + String output = bout.toString(); + assertThat(output).contains("gary@example.com"); + assertThat(output).doesNotContain("TEST@example.com"); + } + + @Test + public void testInspectStringWithExclusionRegex() throws Exception { + InspectStringWithExclusionRegex.inspectStringWithExclusionRegex(PROJECT_ID, + "Some email addresses: gary@example.com, bob@example.org", + ".+@example.com"); + + String output = bout.toString(); + assertThat(output).contains("bob@example.org"); + assertThat(output).doesNotContain("gary@example.com"); } @Test - public void textInspectTestFile() { + public void testInspectStringCustomExcludingSubstring() throws Exception { + InspectStringCustomExcludingSubstring.inspectStringCustomExcludingSubstring(PROJECT_ID, + "Name: Doe, John. Name: Example, Jimmy", + "[A-Z][a-z]{1,15}, [A-Z][a-z]{1,15}", + Arrays.asList("Jimmy")); + + String output = bout.toString(); + assertThat(output).contains("Doe, John"); + assertThat(output).doesNotContain("Example, Jimmy"); + } + + @Test + public void testInspectStringCustomOmitOverlap() throws Exception { + InspectStringCustomOmitOverlap.inspectStringCustomOmitOverlap(PROJECT_ID, + "Name: Jane Doe. Name: Larry Page."); + + String output = bout.toString(); + assertThat(output).contains("Jane Doe"); + assertThat(output).doesNotContain("Larry Page"); + } + + @Test + public void testInspectStringOmitOverlap() throws Exception { + InspectStringOmitOverlap.inspectStringOmitOverlap(PROJECT_ID, "james@example.com"); + + String output = bout.toString(); + assertThat(output).contains("EMAIL_ADDRESS"); + assertThat(output).doesNotContain("PERSON_NAME"); + } + + @Test + public void testInspectStringWithoutOverlap() throws Exception { + InspectStringWithoutOverlap.inspectStringWithoutOverlap(PROJECT_ID, + "example.com is a domain, james@example.org is an email."); + + String output = bout.toString(); + assertThat(output).contains("example.com"); + assertThat(output).doesNotContain("example.org"); + } + + @Test + public void testInspectTable() { + Table tableToInspect = Table.newBuilder() + .addHeaders(FieldId.newBuilder().setName("name").build()) + .addHeaders(FieldId.newBuilder().setName("phone").build()) + .addRows(Row.newBuilder() + .addValues(Value.newBuilder().setStringValue("John Doe").build()) + .addValues(Value.newBuilder().setStringValue("(206) 555-0123").build())) + .build(); + InspectTable.inspectTable(PROJECT_ID, tableToInspect); + + String output = bout.toString(); + assertThat(output).contains("Info type: PHONE_NUMBER"); + } + + @Test + public void testInspectStringCustomHotword() throws Exception { + InspectStringCustomHotword.inspectStringCustomHotword(PROJECT_ID, + "patient name: John Doe", "patient"); + + String output = bout.toString(); + assertThat(output).contains("John Doe"); + } + + @Test + public void testInspectStringCustomHotwordNegativeExample() throws Exception { + InspectStringCustomHotword.inspectStringCustomHotword(PROJECT_ID, + "name: John Doe", "patient"); + + String output = bout.toString(); + assertThat(output).doesNotContain("John Doe"); + } + + @Test + public void testInspectStringMultipleRulesPatientRule() throws Exception { + InspectStringMultipleRules.inspectStringMultipleRules(PROJECT_ID, + "patient: Jane Doe"); + + String output = bout.toString(); + assertThat(output).contains("VERY_LIKELY"); + } + + @Test + public void testInspectStringMultipleRulesDoctorRule() throws Exception { + InspectStringMultipleRules.inspectStringMultipleRules(PROJECT_ID, + "doctor: Jane Doe"); + + String output = bout.toString(); + assertThat(output).contains("Findings: 0"); + } + + @Test + public void testInspectStringMultipleRulesQuasimodoRule() throws Exception { + InspectStringMultipleRules.inspectStringMultipleRules(PROJECT_ID, + "patient: Quasimodo"); + + String output = bout.toString(); + assertThat(output).contains("Findings: 0"); + } + + @Test + public void testInspectStringMultipleRulesRedactedRule() throws Exception { + InspectStringMultipleRules.inspectStringMultipleRules(PROJECT_ID, + "name of patient: REDACTED"); + + String output = bout.toString(); + assertThat(output).contains("Findings: 0"); + } + + @Test + public void textInspectTestFile() throws Exception { InspectTextFile.inspectTextFile(PROJECT_ID, "src/test/resources/test.txt"); String output = bout.toString(); - assertThat(output, containsString("Info type: PHONE_NUMBER")); - assertThat(output, containsString("Info type: EMAIL_ADDRESS")); + assertThat(output).contains("Info type: PHONE_NUMBER"); + assertThat(output).contains("Info type: EMAIL_ADDRESS"); } @Test - public void testInspectImageFile() { + public void testInspectImageFile() throws Exception { InspectImageFile.inspectImageFile(PROJECT_ID, "src/test/resources/test.png"); String output = bout.toString(); - assertThat(output, containsString("Info type: PHONE_NUMBER")); - assertThat(output, containsString("Info type: EMAIL_ADDRESS")); + assertThat(output).contains("Info type: PHONE_NUMBER"); + assertThat(output).contains("Info type: EMAIL_ADDRESS"); } @Test - public void testInspectGcsFile() throws InterruptedException, ExecutionException, IOException { - - InspectGcsFile.inspectGcsFile(PROJECT_ID, GCS_PATH, TOPIC_ID, SUBSCRIPTION_ID); - // Await job creation - TimeUnit.SECONDS.sleep(3); + public void testRedactImageAllInfoTypes() throws Exception { + InspectImageFileAllInfoTypes.inspectImageFileAllInfoTypes(PROJECT_ID, DOCUMENT_INPUT_FILE); String output = bout.toString(); - assertThat(output, containsString("Job created: ")); + assertThat(output).contains("Info type: PHONE_NUMBER"); + assertThat(output).contains("Info type: EMAIL_ADDRESS"); + assertThat(output).contains("Info type: DATE"); + } - // Cancelling the job early to conserve quota - String jobId = output.split("Job created: ")[1].split("\n")[0]; - CancelDlpJobRequest request = CancelDlpJobRequest.newBuilder().setName(jobId).build(); - try (DlpServiceClient client = DlpServiceClient.create()) { - client.cancelDlpJob(request); - } + @Test + public void testRedactImageListedInfoTypes() throws Exception { + InspectImageFileListedInfoTypes.inspectImageFileListedInfoTypes( + PROJECT_ID, DOCUMENT_INPUT_FILE); + + String output = bout.toString(); + assertThat(output).contains("Info type: PHONE_NUMBER"); + assertThat(output).contains("Info type: EMAIL_ADDRESS"); + assertThat(output).doesNotContain("Info type: DATE"); } @Test - public void testInspectDatastoreEntity() - throws InterruptedException, ExecutionException, IOException { + public void testInspectGcsFile() throws Exception { + InspectGcsFile + .inspectGcsFile(PROJECT_ID, GCS_PATH, topicName.getTopic(), + subscriptionName.getSubscription()); - InspectDatastoreEntity.insepctDatastoreEntity( - PROJECT_ID, datastoreNamespace, datastoreKind, TOPIC_ID, SUBSCRIPTION_ID); - // Await job creation - TimeUnit.SECONDS.sleep(3); + String output = bout.toString(); + assertThat(output).contains("Job status: DONE"); + } + + @Test + public void testInspectGcsFileWithSampling() throws Exception { + InspectGcsFileWithSampling + .inspectGcsFileWithSampling(PROJECT_ID, GCS_PATH, topicName.getTopic(), + subscriptionName.getSubscription()); String output = bout.toString(); - assertThat(output, containsString("Job created: ")); + assertThat(output).contains("Job status: DONE"); + } - // Cancelling the job early to conserve quota - String jobId = output.split("Job created: ")[1].split("\n")[0]; - CancelDlpJobRequest request = CancelDlpJobRequest.newBuilder().setName(jobId).build(); - try (DlpServiceClient client = DlpServiceClient.create()) { - client.cancelDlpJob(request); - } + @Test + public void testInspectDatastoreEntity() throws Exception { + InspectDatastoreEntity + .insepctDatastoreEntity(PROJECT_ID, datastoreNamespace, datastoreKind, topicName.getTopic(), + subscriptionName.getSubscription()); + + String output = bout.toString(); + assertThat(output).contains("Job status: DONE"); } @Test - public void testInspectBigQueryTable() - throws InterruptedException, ExecutionException, IOException { + public void testInspectBigQueryTable() throws Exception { + InspectBigQueryTable + .inspectBigQueryTable(PROJECT_ID, DATASET_ID, TABLE_ID, topicName.getTopic(), + subscriptionName.getSubscription()); + + String output = bout.toString(); + assertThat(output).contains("Job status: DONE"); + } - InspectBigQueryTable.inspectBigQueryTable( - PROJECT_ID, DATASET_ID, TABLE_ID, TOPIC_ID, SUBSCRIPTION_ID); - // Await job creation - TimeUnit.SECONDS.sleep(3); + @Test + public void testInspectBigQueryTableWithSampling() throws Exception { + InspectBigQueryTableWithSampling + .inspectBigQueryTableWithSampling(PROJECT_ID, topicName.getTopic(), + subscriptionName.getSubscription()); String output = bout.toString(); - assertThat(output, containsString("Job created: ")); + assertThat(output).contains("Job status: DONE"); + } - // Cancelling the job early to conserve quota - String jobId = output.split("Job created: ")[1].split("\n")[0]; - CancelDlpJobRequest request = CancelDlpJobRequest.newBuilder().setName(jobId).build(); - try (DlpServiceClient client = DlpServiceClient.create()) { - client.cancelDlpJob(request); - } + @Test + public void testInspectWithHotwordRules() throws Exception { + InspectWithHotwordRules.inspectWithHotwordRules( + PROJECT_ID, + "Patient's MRN 444-5-22222 and just a number 333-2-33333", + "[1-9]{3}-[1-9]{1}-[1-9]{5}", + "(?i)(mrn|medical)(?-i)"); + + String output = bout.toString(); + assertThat(output).contains("Findings: 2"); + assertThat(output).contains("Info type: C_MRN"); } } diff --git a/dlp/src/test/java/dlp/snippets/JobsTests.java b/dlp/src/test/java/dlp/snippets/JobsTests.java index f2ab301f403..7a0a13551fb 100644 --- a/dlp/src/test/java/dlp/snippets/JobsTests.java +++ b/dlp/src/test/java/dlp/snippets/JobsTests.java @@ -16,42 +16,31 @@ package dlp.snippets; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; +import static com.google.common.truth.Truth.assertThat; import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.common.collect.ImmutableList; import com.google.privacy.dlp.v2.CloudStorageOptions; import com.google.privacy.dlp.v2.CloudStorageOptions.FileSet; import com.google.privacy.dlp.v2.CreateDlpJobRequest; +import com.google.privacy.dlp.v2.DeleteDlpJobRequest; import com.google.privacy.dlp.v2.DlpJob; import com.google.privacy.dlp.v2.InspectConfig; import com.google.privacy.dlp.v2.InspectJobConfig; -import com.google.privacy.dlp.v2.ProjectName; +import com.google.privacy.dlp.v2.LocationName; import com.google.privacy.dlp.v2.StorageConfig; -import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.PrintStream; import java.util.UUID; -import org.hamcrest.CoreMatchers; -import org.junit.After; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -public class JobsTests { +public class JobsTests extends TestBase { - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String GCS_PATH = System.getenv("GCS_PATH"); - - private ByteArrayOutputStream bout; - - private static void requireEnvVar(String varName) { - assertNotNull( - String.format("Environment variable '%s' must be set to perform these tests.", varName), - System.getenv(varName)); + @Override + protected ImmutableList requiredEnvVars() { + return ImmutableList.of("GOOGLE_APPLICATION_CREDENTIALS", "GOOGLE_CLOUD_PROJECT", "GCS_PATH"); } private static DlpJob createJob(String jobId) throws IOException { @@ -70,7 +59,7 @@ private static DlpJob createJob(String jobId) throws IOException { CreateDlpJobRequest createDlpJobRequest = CreateDlpJobRequest.newBuilder() - .setParent(ProjectName.of(PROJECT_ID).toString()) + .setParent(LocationName.of(PROJECT_ID, "global").toString()) .setInspectJob(inspectJobConfig) .setJobId(jobId) .build(); @@ -79,23 +68,40 @@ private static DlpJob createJob(String jobId) throws IOException { } } - @BeforeClass - public static void checkRequirements() { - requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); - requireEnvVar("GOOGLE_CLOUD_PROJECT"); - requireEnvVar("GCS_PATH"); + @Test + public void testCreateJobs() throws Exception { + // Call createJobs to create a Dlp job from project id and gcs path. + JobsCreate.createJobs(PROJECT_ID, GCS_PATH); + String output = bout.toString(); + assertThat(output).contains("Job created successfully:"); + + // Delete the created Dlp Job + String dlpJobName = output.split("Job created successfully: ")[1].split("\n")[0]; + DeleteDlpJobRequest deleteDlpJobRequest = + DeleteDlpJobRequest.newBuilder().setName(dlpJobName).build(); + try (DlpServiceClient client = DlpServiceClient.create()) { + client.deleteDlpJob(deleteDlpJobRequest); + } } - @Before - public void setUp() { - bout = new ByteArrayOutputStream(); - System.setOut(new PrintStream(bout)); - } + @Test + public void testGetJobs() throws Exception { + // Create a job with a unique UUID to be gotten + String jobId = UUID.randomUUID().toString(); + DlpJob createdDlpJob = createJob(jobId); - @After - public void tearDown() { - System.setOut(null); - bout.reset(); + // Get the job with the specified ID + JobsGet.getJobs(PROJECT_ID, "i-" + jobId); + String output = bout.toString(); + assertThat(output).contains("Job got successfully."); + + // Delete the created Dlp Job + String dlpJobName = createdDlpJob.getName(); + DeleteDlpJobRequest deleteDlpJobRequest = + DeleteDlpJobRequest.newBuilder().setName(dlpJobName).build(); + try (DlpServiceClient client = DlpServiceClient.create()) { + client.deleteDlpJob(deleteDlpJobRequest); + } } @Test @@ -105,7 +111,7 @@ public void testListJobs() throws Exception { String output = bout.toString(); // Check that the output contains a list of jobs, or is empty - assertThat(output, CoreMatchers.containsString("DLP jobs found:")); + assertThat(output).contains("DLP jobs found:"); } @Test @@ -117,6 +123,6 @@ public void testDeleteJobs() throws Exception { // Delete the job with the specified ID JobsDelete.deleteJobs(PROJECT_ID, "i-" + jobId); String output = bout.toString(); - assertThat(output, CoreMatchers.containsString("Job deleted successfully.")); + assertThat(output).contains("Job deleted successfully."); } } diff --git a/dlp/src/test/java/dlp/snippets/QuickstartTests.java b/dlp/src/test/java/dlp/snippets/QuickstartTests.java index b3cac2738a5..eb333a4ac3a 100644 --- a/dlp/src/test/java/dlp/snippets/QuickstartTests.java +++ b/dlp/src/test/java/dlp/snippets/QuickstartTests.java @@ -16,48 +16,20 @@ package dlp.snippets; -import static junit.framework.TestCase.assertNotNull; -import static org.hamcrest.core.StringContains.containsString; -import static org.junit.Assert.assertThat; +import static com.google.common.truth.Truth.assertThat; -import java.io.ByteArrayOutputStream; +import com.google.common.collect.ImmutableList; import java.io.IOException; -import java.io.PrintStream; -import org.junit.After; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -public class QuickstartTests { +public class QuickstartTests extends TestBase { - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - private ByteArrayOutputStream bout; - - private static void requireEnvVar(String varName) { - assertNotNull( - String.format("Environment variable '%s' must be set to perform these tests.", varName), - System.getenv(varName)); - } - - @BeforeClass - public static void checkRequirements() { - requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); - requireEnvVar("GOOGLE_CLOUD_PROJECT"); - } - - @Before - public void beforeTest() { - bout = new ByteArrayOutputStream(); - System.setOut(new PrintStream(bout)); - } - - @After - public void tearDown() { - System.setOut(null); - bout.reset(); + @Override + protected ImmutableList requiredEnvVars() { + return ImmutableList.of("GOOGLE_APPLICATION_CREDENTIALS", "GOOGLE_CLOUD_PROJECT"); } @Test @@ -65,6 +37,6 @@ public void testQuickstart() throws IOException { QuickStart.quickstart(PROJECT_ID); String output = bout.toString(); - assertThat(output, containsString("Inspect of text complete")); + assertThat(output).contains("Inspect of text complete"); } } diff --git a/dlp/src/test/java/dlp/snippets/RedactTests.java b/dlp/src/test/java/dlp/snippets/RedactTests.java index 73c08815c47..73015928cd7 100644 --- a/dlp/src/test/java/dlp/snippets/RedactTests.java +++ b/dlp/src/test/java/dlp/snippets/RedactTests.java @@ -16,64 +16,79 @@ package dlp.snippets; -import static junit.framework.TestCase.assertNotNull; -import static org.hamcrest.core.StringContains.containsString; -import static org.junit.Assert.assertThat; +import static com.google.common.truth.Truth.assertThat; -import java.io.ByteArrayOutputStream; +import com.google.common.collect.ImmutableList; import java.io.IOException; -import java.io.PrintStream; import java.nio.file.Files; -import java.nio.file.Path; import java.nio.file.Paths; import org.junit.After; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -public class RedactTests { +public class RedactTests extends TestBase { - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - private ByteArrayOutputStream bout; + private static final String SIMPLE_INPUT_FILE = "src/test/resources/test.png"; + private static final String SIMPLE_OUTPUT_FILE = "redacted.png"; + private static final String DOCUMENT_INPUT_FILE = + "src/test/resources/sensitive-data-image.jpg"; + private static final String DOCUMENT_OUTPUT_FILE = + "sensitive-data-image-redacted.jpg"; - private static void requireEnvVar(String varName) { - assertNotNull( - String.format("Environment variable '%s' must be set to perform these tests.", varName), - System.getenv(varName)); + @Override + protected ImmutableList requiredEnvVars() { + return ImmutableList.of("GOOGLE_APPLICATION_CREDENTIALS", "GOOGLE_CLOUD_PROJECT"); } - @BeforeClass - public static void checkRequirements() { - requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); - requireEnvVar("GOOGLE_CLOUD_PROJECT"); + @After + public void after() throws IOException { + Files.deleteIfExists(Paths.get(SIMPLE_OUTPUT_FILE)); + Files.deleteIfExists(Paths.get(DOCUMENT_OUTPUT_FILE)); } - @Before - public void beforeTest() { - bout = new ByteArrayOutputStream(); - System.setOut(new PrintStream(bout)); + @Test + public void testRedactImage() throws Exception { + RedactImageFile.redactImageFile(PROJECT_ID, SIMPLE_INPUT_FILE, SIMPLE_OUTPUT_FILE); + + String output = bout.toString(); + assertThat(output).contains("Redacted image written"); } - @After - public void tearDown() { - System.setOut(null); - bout.reset(); + @Test + public void testRedactImageAllInfoTypes() throws Exception { + RedactImageFileAllInfoTypes.redactImageFileAllInfoTypes(PROJECT_ID, DOCUMENT_INPUT_FILE, + DOCUMENT_OUTPUT_FILE); + + String output = bout.toString(); + assertThat(output).contains("Redacted image written"); } - @After - public void cleanUp() throws IOException { - Path outputFile = Paths.get("redacted.png"); - Files.delete(outputFile); + @Test + public void testRedactImageListedInfoTypes() throws Exception { + RedactImageFileListedInfoTypes.redactImageFileListedInfoTypes(PROJECT_ID, DOCUMENT_INPUT_FILE, + DOCUMENT_OUTPUT_FILE); + + String output = bout.toString(); + assertThat(output).contains("Redacted image written"); + } + + @Test + public void testRedactImageColoredInfoTypes() throws Exception { + RedactImageFileColoredInfoTypes.redactImageFileColoredInfoTypes(PROJECT_ID, DOCUMENT_INPUT_FILE, + DOCUMENT_OUTPUT_FILE); + + String output = bout.toString(); + assertThat(output).contains("Redacted image written"); } @Test - public void testRedactImage() { - RedactImageFile.redactImageFile(PROJECT_ID, "src/test/resources/test.png"); + public void testRedactImageAllText() throws Exception { + RedactImageFileAllText.redactImageFileAllText(PROJECT_ID, DOCUMENT_INPUT_FILE, + DOCUMENT_OUTPUT_FILE); String output = bout.toString(); - assertThat(output, containsString("Redacted image written")); + assertThat(output).contains("Redacted image written"); } } diff --git a/dlp/src/test/java/dlp/snippets/RiskAnalysisTests.java b/dlp/src/test/java/dlp/snippets/RiskAnalysisTests.java index d1ca2a848e8..de2091998f1 100644 --- a/dlp/src/test/java/dlp/snippets/RiskAnalysisTests.java +++ b/dlp/src/test/java/dlp/snippets/RiskAnalysisTests.java @@ -16,106 +16,122 @@ package dlp.snippets; -import static org.hamcrest.CoreMatchers.containsString; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.regex.Pattern; +import static com.google.common.truth.Truth.assertThat; + +import com.google.api.gax.rpc.ApiException; +import com.google.cloud.pubsub.v1.SubscriptionAdminClient; +import com.google.cloud.pubsub.v1.TopicAdminClient; +import com.google.common.collect.ImmutableList; +import com.google.pubsub.v1.ProjectSubscriptionName; +import com.google.pubsub.v1.PushConfig; +import com.google.pubsub.v1.TopicName; +import java.util.UUID; import org.junit.After; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -public class RiskAnalysisTests { - - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String TOPIC_ID = System.getenv("PUB_SUB_TOPIC"); - private static final String SUBSCRIPTION_ID = System.getenv("PUB_SUB_SUBSCRIPTION"); - private static final String DATASET_ID = System.getenv("BIGQUERY_DATASET"); - private static final String TABLE_ID = System.getenv("BIGQUERY_TABLE"); - private ByteArrayOutputStream bout; - - private static void requireEnvVar(String varName) { - assertNotNull( - String.format("Environment variable '%s' must be set to perform these tests.", varName), - System.getenv(varName)); +public class RiskAnalysisTests extends TestBase { + + private UUID testRunUuid = UUID.randomUUID(); + private TopicName topicName = TopicName.of( + PROJECT_ID, + String.format("%s-%s", TOPIC_ID, testRunUuid.toString())); + private ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of( + PROJECT_ID, + String.format("%s-%s", SUBSCRIPTION_ID, testRunUuid.toString())); + + @Override + protected ImmutableList requiredEnvVars() { + return ImmutableList + .of("GOOGLE_APPLICATION_CREDENTIALS", "GOOGLE_CLOUD_PROJECT", "PUB_SUB_TOPIC", + "PUB_SUB_SUBSCRIPTION", "BIGQUERY_DATASET", "BIGQUERY_TABLE"); } - @BeforeClass - public static void checkRequirements() { - requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); - requireEnvVar("GOOGLE_CLOUD_PROJECT"); - requireEnvVar("PUB_SUB_TOPIC"); - requireEnvVar("PUB_SUB_SUBSCRIPTION"); - requireEnvVar("BIGQUERY_DATASET"); - requireEnvVar("BIGQUERY_TABLE"); + @Before + public void before() throws Exception { + // Create a new topic + try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { + topicAdminClient.createTopic(topicName); + } + // Create a new subscription + try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) { + subscriptionAdminClient + .createSubscription(subscriptionName, topicName, PushConfig.getDefaultInstance(), 0); + } } - @Before - public void setUp() { - bout = new ByteArrayOutputStream(); - System.setOut(new PrintStream(bout)); + @After + public void after() throws Exception { + // Delete the test topic + try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { + topicAdminClient.deleteTopic(topicName); + } catch (ApiException e) { + System.err.println(String.format("Error deleting topic %s: %s", + topicName.getTopic(), e)); + // Keep trying to clean up + } + + // Delete the test subscription + try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) { + subscriptionAdminClient.deleteSubscription(subscriptionName); + } catch (ApiException e) { + System.err.println(String.format("Error deleting subscription %s: %s", + subscriptionName.getSubscription(), e)); + // Keep trying to clean up + } } @Test public void testNumericalStats() throws Exception { RiskAnalysisNumericalStats.numericalStatsAnalysis( - PROJECT_ID, DATASET_ID, TABLE_ID, TOPIC_ID, SUBSCRIPTION_ID); + PROJECT_ID, DATASET_ID, TABLE_ID, topicName.getTopic(), subscriptionName.getSubscription()); String output = bout.toString(); - assertThat(output, containsString("Value at ")); + assertThat(output).contains("Value at "); } @Test public void testCategoricalStats() throws Exception { RiskAnalysisCategoricalStats.categoricalStatsAnalysis( - PROJECT_ID, DATASET_ID, TABLE_ID, TOPIC_ID, SUBSCRIPTION_ID); + PROJECT_ID, DATASET_ID, TABLE_ID, topicName.getTopic(), subscriptionName.getSubscription()); String output = bout.toString(); - assertTrue(Pattern.compile("Most common value occurs \\d time").matcher(output).find()); - assertTrue(Pattern.compile("Least common value occurs \\d time").matcher(output).find()); + assertThat(output).containsMatch("Most common value occurs \\d time"); + assertThat(output).containsMatch("Least common value occurs \\d time"); } @Test public void testKAnonymity() throws Exception { RiskAnalysisKAnonymity.calculateKAnonymity( - PROJECT_ID, DATASET_ID, TABLE_ID, TOPIC_ID, SUBSCRIPTION_ID); + PROJECT_ID, DATASET_ID, TABLE_ID, topicName.getTopic(), subscriptionName.getSubscription()); String output = bout.toString(); - assertTrue(Pattern.compile("Bucket size range: \\[\\d, \\d\\]").matcher(output).find()); - assertTrue(output.contains("Quasi-ID values: integer_value: 19")); - assertTrue(output.contains("Class size: 1")); + assertThat(output).containsMatch("Bucket size range: \\[\\d, \\d\\]"); + assertThat(output).contains("Quasi-ID values: integer_value: 19"); + assertThat(output).contains("Class size: 1"); } @Test public void testLDiversity() throws Exception { RiskAnalysisLDiversity.calculateLDiversity( - PROJECT_ID, DATASET_ID, TABLE_ID, TOPIC_ID, SUBSCRIPTION_ID); + PROJECT_ID, DATASET_ID, TABLE_ID, topicName.getTopic(), subscriptionName.getSubscription()); String output = bout.toString(); - assertTrue(output.contains("Quasi-ID values: integer_value: 19")); - assertTrue(output.contains("Class size: 1")); - assertTrue(output.contains("Sensitive value string_value: \"James\"")); + assertThat(output).contains("Quasi-ID values: integer_value: 19"); + assertThat(output).contains("Class size: 1"); + assertThat(output).contains("Sensitive value string_value: \"James\""); } @Test public void testKMap() throws Exception { - RiskAnalysisKMap.calculateKMap(PROJECT_ID, DATASET_ID, TABLE_ID, TOPIC_ID, SUBSCRIPTION_ID); + RiskAnalysisKMap.calculateKMap( + PROJECT_ID, DATASET_ID, TABLE_ID, topicName.getTopic(), subscriptionName.getSubscription()); String output = bout.toString(); - assertTrue(Pattern.compile("Anonymity range: \\[\\d, \\d]").matcher(output).find()); - assertTrue(Pattern.compile("Size: \\d").matcher(output).find()); - assertTrue(Pattern.compile("Values: \\{\\d{2}, \"Female\"\\}").matcher(output).find()); - } - - @After - public void tearDown() { - System.setOut(null); - bout.reset(); + assertThat(output).containsMatch("Anonymity range: \\[\\d, \\d]"); + assertThat(output).containsMatch("Size: \\d"); + assertThat(output).containsMatch("Values: \\{\\d{2}, \"Female\"\\}"); } } diff --git a/dlp/src/test/java/dlp/snippets/TemplatesTests.java b/dlp/src/test/java/dlp/snippets/TemplatesTests.java index 80216487a84..1af435a56c4 100644 --- a/dlp/src/test/java/dlp/snippets/TemplatesTests.java +++ b/dlp/src/test/java/dlp/snippets/TemplatesTests.java @@ -16,46 +16,32 @@ package dlp.snippets; -import static org.hamcrest.CoreMatchers.containsString; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; +import static com.google.common.truth.Truth.assertThat; import com.google.cloud.dlp.v2.DlpServiceClient; -import com.google.cloud.dlp.v2.DlpServiceClient.ListInspectTemplatesPagedResponse; +import com.google.common.collect.ImmutableList; import com.google.privacy.dlp.v2.CreateInspectTemplateRequest; import com.google.privacy.dlp.v2.DeleteInspectTemplateRequest; import com.google.privacy.dlp.v2.InfoType; import com.google.privacy.dlp.v2.InspectConfig; import com.google.privacy.dlp.v2.InspectTemplate; -import com.google.privacy.dlp.v2.ListInspectTemplatesRequest; -import com.google.privacy.dlp.v2.ProjectName; -import java.io.ByteArrayOutputStream; +import com.google.privacy.dlp.v2.LocationName; import java.io.IOException; -import java.io.PrintStream; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -public class TemplatesTests { +public class TemplatesTests extends TestBase { - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - - private ByteArrayOutputStream bout; - - private static void requireEnvVar(String varName) { - assertNotNull( - String.format("Environment variable '%s' must be set to perform these tests.", varName), - System.getenv(varName)); + @Override + protected ImmutableList requiredEnvVars() { + return ImmutableList.of("GOOGLE_APPLICATION_CREDENTIALS", "GOOGLE_CLOUD_PROJECT"); } private static InspectTemplate createTemplate() throws IOException { @@ -72,7 +58,7 @@ private static InspectTemplate createTemplate() throws IOException { CreateInspectTemplateRequest createInspectTemplateRequest = CreateInspectTemplateRequest.newBuilder() - .setParent(ProjectName.of(PROJECT_ID).toString()) + .setParent(LocationName.of(PROJECT_ID, "global").toString()) .setInspectTemplate(inspectTemplate) .build(); @@ -80,31 +66,11 @@ private static InspectTemplate createTemplate() throws IOException { } } - @BeforeClass - public static void checkRequirements() { - requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); - requireEnvVar("GOOGLE_CLOUD_PROJECT"); - } - - @Before - public void setUp() { - bout = new ByteArrayOutputStream(); - System.setOut(new PrintStream(bout)); - } - - - @After - public void tearDown() { - System.setOut(null); - bout.reset(); - } - @Test public void testCreateInspectTemplate() throws Exception { TemplatesCreate.createInspectTemplate(PROJECT_ID); String output = bout.toString(); - assertThat(output, containsString("Template created: ")); - + assertThat(output).contains("Template created: "); // Delete the created template String templateId = output.split("Template created: ")[1].split("\n")[0]; @@ -119,7 +85,7 @@ public void testCreateInspectTemplate() throws Exception { public void testListInspectTemplate() throws Exception { TemplatesList.listInspectTemplates(PROJECT_ID); String output = bout.toString(); - assertThat(output, containsString("Templates found:")); + assertThat(output).contains("Templates found:"); } @Test @@ -139,6 +105,6 @@ public void testDeleteInspectTemplate() throws Exception { // Delete the template with the specified ID TemplatesDelete.deleteInspectTemplate(PROJECT_ID, templateId); String output = bout.toString(); - assertThat(output, containsString("Deleted template:")); + assertThat(output).contains("Deleted template:"); } } diff --git a/dlp/src/test/java/dlp/snippets/TestBase.java b/dlp/src/test/java/dlp/snippets/TestBase.java new file mode 100644 index 00000000000..40007d31e51 --- /dev/null +++ b/dlp/src/test/java/dlp/snippets/TestBase.java @@ -0,0 +1,67 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dlp.snippets; + +import static com.google.common.truth.Truth.assertWithMessage; + +import com.google.common.collect.ImmutableList; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; + +/** + * Common base class for DLP snippet tests + */ +abstract class TestBase { + + protected static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + protected static final String GCS_PATH = System.getenv("GCS_PATH"); + protected static final String TOPIC_ID = System.getenv("PUB_SUB_TOPIC"); + protected static final String SUBSCRIPTION_ID = System.getenv("PUB_SUB_SUBSCRIPTION"); + protected static final String DATASET_ID = System.getenv("BIGQUERY_DATASET"); + protected static final String TABLE_ID = System.getenv("BIGQUERY_TABLE"); + protected static final String wrappedKey = System.getenv("DLP_DEID_WRAPPED_KEY"); + protected static final String kmsKeyName = System.getenv("DLP_DEID_KEY_NAME"); + + protected ByteArrayOutputStream bout; + private PrintStream originalOut = System.out; + + protected abstract ImmutableList requiredEnvVars(); + + private static void requireEnvVar(String varName) { + assertWithMessage( + String.format("Environment variable '%s' must be set to perform these tests.", varName)) + .that(System.getenv(varName)).isNotEmpty(); + } + + @Before + public void beforeBase() { + requiredEnvVars().stream().forEach(TestBase::requireEnvVar); + + // Capture stdout + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + } + + @After + public void afterBase() { + // Restore stdout + System.setOut(originalOut); + bout.reset(); + } +} diff --git a/dlp/src/test/java/dlp/snippets/TriggersTests.java b/dlp/src/test/java/dlp/snippets/TriggersTests.java index f46692523bd..b84710fb345 100644 --- a/dlp/src/test/java/dlp/snippets/TriggersTests.java +++ b/dlp/src/test/java/dlp/snippets/TriggersTests.java @@ -16,47 +16,33 @@ package dlp.snippets; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; +import static com.google.common.truth.Truth.assertThat; import com.google.cloud.dlp.v2.DlpServiceClient; +import com.google.common.collect.ImmutableList; import com.google.privacy.dlp.v2.CloudStorageOptions; import com.google.privacy.dlp.v2.CreateJobTriggerRequest; import com.google.privacy.dlp.v2.DeleteJobTriggerRequest; import com.google.privacy.dlp.v2.InspectConfig; import com.google.privacy.dlp.v2.InspectJobConfig; import com.google.privacy.dlp.v2.JobTrigger; -import com.google.privacy.dlp.v2.ListJobTriggersRequest; -import com.google.privacy.dlp.v2.ProjectName; +import com.google.privacy.dlp.v2.LocationName; import com.google.privacy.dlp.v2.Schedule; import com.google.privacy.dlp.v2.StorageConfig; import com.google.protobuf.Duration; -import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.PrintStream; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.hamcrest.CoreMatchers; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -public class TriggersTests { +public class TriggersTests extends TestBase { - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String GCS_PATH = System.getenv("GCS_PATH"); - - private ByteArrayOutputStream bout; - - private static void requireEnvVar(String varName) { - assertNotNull( - String.format("Environment variable '%s' must be set to perform these tests.", varName), - System.getenv(varName)); + @Override + protected ImmutableList requiredEnvVars() { + return ImmutableList.of("GOOGLE_APPLICATION_CREDENTIALS", "GOOGLE_CLOUD_PROJECT", "GCS_PATH"); } private static JobTrigger createTrigger() throws IOException { @@ -86,7 +72,7 @@ private static JobTrigger createTrigger() throws IOException { CreateJobTriggerRequest createJobTriggerRequest = CreateJobTriggerRequest.newBuilder() - .setParent(ProjectName.of(PROJECT_ID).toString()) + .setParent(LocationName.of(PROJECT_ID, "global").toString()) .setJobTrigger(jobTrigger) .build(); @@ -94,32 +80,11 @@ private static JobTrigger createTrigger() throws IOException { } } - @BeforeClass - public static void checkRequirements() { - requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); - requireEnvVar("GOOGLE_CLOUD_PROJECT"); - requireEnvVar("GCS_PATH"); - } - - @Before - public void setUp() { - bout = new ByteArrayOutputStream(); - System.setOut(new PrintStream(bout)); - } - - - @After - public void tearDown() { - System.setOut(null); - bout.reset(); - } - - @Test public void testCreateTrigger() throws Exception { TriggersCreate.createTrigger(PROJECT_ID, GCS_PATH); String output = bout.toString(); - assertThat(output, CoreMatchers.containsString("Created Trigger:")); + assertThat(output).contains("Created Trigger:"); // Delete the created trigger String triggerId = output.split("Created Trigger: ")[1].split("\n")[0]; @@ -134,7 +99,7 @@ public void testCreateTrigger() throws Exception { public void testListTrigger() throws Exception { TriggersList.listTriggers(PROJECT_ID); String output = bout.toString(); - assertThat(output, CoreMatchers.containsString("DLP triggers found:")); + assertThat(output).contains("DLP triggers found:"); } @Test @@ -153,6 +118,6 @@ public void testDeleteTrigger() throws Exception { // Delete the job with the specified ID TriggersDelete.deleteTrigger(PROJECT_ID, triggerId); String output = bout.toString(); - assertThat(output, CoreMatchers.containsString("Trigger deleted:")); + assertThat(output).contains("Trigger deleted:"); } } diff --git a/dlp/src/test/resources/sensitive-data-image.jpg b/dlp/src/test/resources/sensitive-data-image.jpg new file mode 100644 index 00000000000..6e2d84546e4 Binary files /dev/null and b/dlp/src/test/resources/sensitive-data-image.jpg differ diff --git a/endpoints/bookstore-grpc/gradle/wrapper/gradle-wrapper.properties b/endpoints/bookstore-grpc/gradle/wrapper/gradle-wrapper.properties index 0d6bd81b750..0f162a0bb16 100644 --- a/endpoints/bookstore-grpc/gradle/wrapper/gradle-wrapper.properties +++ b/endpoints/bookstore-grpc/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip diff --git a/endpoints/getting-started-grpc/gradle/wrapper/gradle-wrapper.properties b/endpoints/getting-started-grpc/gradle/wrapper/gradle-wrapper.properties index e7416f5a11b..7da3c010787 100644 --- a/endpoints/getting-started-grpc/gradle/wrapper/gradle-wrapper.properties +++ b/endpoints/getting-started-grpc/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip diff --git a/endpoints/getting-started/clients/pom.xml b/endpoints/getting-started/clients/pom.xml index e81f0ff86ee..919a50766e3 100644 --- a/endpoints/getting-started/clients/pom.xml +++ b/endpoints/getting-started/clients/pom.xml @@ -15,7 +15,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -26,9 +26,9 @@ - com.google.api-client - google-api-client - 1.30.9 + com.google.auth + google-auth-library-oauth2-http + 0.21.0 com.auth0 @@ -43,25 +43,12 @@ org.apache.maven.plugins maven-compiler-plugin 3.8.1 - - - -XDcompilePolicy=simple - -Xplugin:ErrorProne - - - - com.google.errorprone - error_prone_core - 2.3.4 - - - org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 com.example.app.GoogleJwtClient diff --git a/endpoints/getting-started/clients/src/main/java/com/example/app/GoogleJwtClient.java b/endpoints/getting-started/clients/src/main/java/com/example/app/GoogleJwtClient.java index 6e9f472ac5d..20e6efc8bc6 100644 --- a/endpoints/getting-started/clients/src/main/java/com/example/app/GoogleJwtClient.java +++ b/endpoints/getting-started/clients/src/main/java/com/example/app/GoogleJwtClient.java @@ -19,7 +19,7 @@ import com.auth0.jwt.JWT; import com.auth0.jwt.JWTCreator; import com.auth0.jwt.algorithms.Algorithm; -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.auth.oauth2.ServiceAccountCredentials; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -68,8 +68,8 @@ public static String generateJwt(final String saKeyfile, final String saEmail, // Sign the JWT with a service account FileInputStream stream = new FileInputStream(saKeyfile); - GoogleCredential cred = GoogleCredential.fromStream(stream); - RSAPrivateKey key = (RSAPrivateKey) cred.getServiceAccountPrivateKey(); + ServiceAccountCredentials cred = ServiceAccountCredentials.fromStream(stream); + RSAPrivateKey key = (RSAPrivateKey) cred.getPrivateKey(); Algorithm algorithm = Algorithm.RSA256(null, key); return token.sign(algorithm); } diff --git a/endpoints/getting-started/pom.xml b/endpoints/getting-started/pom.xml index 2990360570a..33146d1a1a2 100644 --- a/endpoints/getting-started/pom.xml +++ b/endpoints/getting-started/pom.xml @@ -29,7 +29,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -63,19 +63,19 @@ org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 org.eclipse.jetty jetty-maven-plugin - 9.4.29.v20200521 + 9.4.30.v20200611 diff --git a/endpoints/getting-started/src/main/java/com/example/endpoints/AuthInfoServlet.java b/endpoints/getting-started/src/main/java/com/example/endpoints/AuthInfoServlet.java index 5ec58b038e1..1e6b573b583 100644 --- a/endpoints/getting-started/src/main/java/com/example/endpoints/AuthInfoServlet.java +++ b/endpoints/getting-started/src/main/java/com/example/endpoints/AuthInfoServlet.java @@ -1,6 +1,6 @@ /* * Copyright 2017 Google Inc. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -12,17 +12,14 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - */ - + */ package com.example.endpoints; import com.google.gson.Gson; import com.google.gson.JsonObject; - import java.io.IOException; import java.util.Base64; - import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -30,8 +27,8 @@ // [START endpoints_auth_info_backend] /** - * A servlet that returns authentication information. - * See openapi.yaml for authentication mechanisms (e.g. JWT tokens, Google ID token). + * A servlet that returns authentication information. See openapi.yaml for authentication mechanisms + * (e.g. JWT tokens, Google ID token). */ @WebServlet("/auth/info/*") public class AuthInfoServlet extends HttpServlet { diff --git a/endpoints/getting-started/src/main/java/com/example/endpoints/EchoServlet.java b/endpoints/getting-started/src/main/java/com/example/endpoints/EchoServlet.java index 84e3152f404..288ae769b62 100644 --- a/endpoints/getting-started/src/main/java/com/example/endpoints/EchoServlet.java +++ b/endpoints/getting-started/src/main/java/com/example/endpoints/EchoServlet.java @@ -20,18 +20,14 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import com.google.gson.stream.JsonReader; - import java.io.IOException; import java.util.Map; - import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -/** - * A servlet that echoes JSON message bodies. - */ +/** A servlet that echoes JSON message bodies. */ @WebServlet("/echo") public class EchoServlet extends HttpServlet { diff --git a/endpoints/multiple-versions/pom.xml b/endpoints/multiple-versions/pom.xml index a40dff9fed2..e54fbd6d8d6 100644 --- a/endpoints/multiple-versions/pom.xml +++ b/endpoints/multiple-versions/pom.xml @@ -29,7 +29,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -38,8 +38,8 @@ 2.6 - 2.2.0 - 9.4.29.v20200521 + 2.3.0 + 9.4.30.v20200611 false diff --git a/errorreporting/README.md b/errorreporting/README.md deleted file mode 100644 index 176e6256456..00000000000 --- a/errorreporting/README.md +++ /dev/null @@ -1,38 +0,0 @@ -# Stackdriver Error Reporting sample - - -Open in Cloud Shell - -[Stackdriver Error Reporting][error-reporting] Stackdriver Error Reporting counts, analyzes and aggregates the crashes in your running cloud services. -A [centralized error management interface](https://console.cloud.google.com/errors) displays the results with sorting and filtering capabilities. - -This sample Java application demonstrates how to send custom error events using the [Error Reporting API][api-ref-docs]. -Note: Runtime exceptions and stack traces are automatically sent to Error reporting in applications running in [App Engine Flex environment][ae-flex]. - -[ae-flex]: https://cloud.google.com/appengine/docs/flexible/java -[error-reporting]: https://cloud.google.com/error-reporting/ -[api-ref-docs]: https://googlecloudplatform.github.io/google-cloud-java/latest/apidocs/index.html?com/google/cloud/errorreporting/v1beta1/package-summary.html - -## Setup - -1. Install [Maven](http://maven.apache.org/). -1. [Enable](https://console.cloud.google.com/apis/api/clouderrorreporting.googleapis.com/overview) Stack Driver Error Reporting API. - -## Build - -Build your project with: -``` - mvn clean package -``` - -## Local testing - -1. [Create a service account](https://cloud.google.com/docs/authentication/getting-started#creating_the_service_account) -and set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable. -2. Run -``` - mvn clean verify -``` -Check the [error reporting console](https://console.cloud.google.com/errors). - -Confirm that you see the custom errors reported using the Error Reporting API. diff --git a/errorreporting/pom.xml b/errorreporting/pom.xml deleted file mode 100644 index bf30e94a354..00000000000 --- a/errorreporting/pom.xml +++ /dev/null @@ -1,51 +0,0 @@ - - - 4.0.0 - com.google.cloud.errorreporting.samples - cloud-errorreporting-samples - pom - - - - com.google.cloud.samples - shared-configuration - 1.0.17 - - - 1.8 - 1.8 - - - - - com.google.cloud - google-cloud-errorreporting - 0.117.0-beta - - - - junit - junit - 4.13 - test - - - diff --git a/errorreporting/src/main/java/com/example/errorreporting/QuickStart.java b/errorreporting/src/main/java/com/example/errorreporting/QuickStart.java deleted file mode 100644 index 28d99b41d8e..00000000000 --- a/errorreporting/src/main/java/com/example/errorreporting/QuickStart.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 2017 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.errorreporting; - -// [START errorreporting_quickstart] -// [START error_reporting_quickstart] -// [START error_reporting_setup_java] - -import com.google.cloud.ServiceOptions; -import com.google.cloud.errorreporting.v1beta1.ReportErrorsServiceClient; -import com.google.devtools.clouderrorreporting.v1beta1.ErrorContext; -import com.google.devtools.clouderrorreporting.v1beta1.ProjectName; -import com.google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent; -import com.google.devtools.clouderrorreporting.v1beta1.SourceLocation; - -/** - * Snippet demonstrates using the Stackdriver Error Reporting API to report a custom error event. - *

- * This library is not required on App Engine, errors written to stderr are automatically written - * to Stackdriver Error Reporting. - * It is also not required if you are writing logs to Stackdriver Logging. - * Errors written to Stackdriver Logging that contain an exception or stack trace - * are automatically written out to Stackdriver Error Reporting. - */ -public class QuickStart { - public static void main(String[] args) throws Exception { - - // Google Cloud Platform Project ID - String projectId = (args.length > 0) ? args[0] : ServiceOptions.getDefaultProjectId(); - ProjectName projectName = ProjectName.of(projectId); - - // Instantiate an Error Reporting Client - try (ReportErrorsServiceClient reportErrorsServiceClient = ReportErrorsServiceClient.create()) { - - // Custom error events require an error reporting location as well. - ErrorContext errorContext = ErrorContext.newBuilder() - .setReportLocation(SourceLocation.newBuilder() - .setFilePath("Test.java") - .setLineNumber(10) - .setFunctionName("myMethod") - .build()) - .build(); - - //Report a custom error event - ReportedErrorEvent customErrorEvent = ReportedErrorEvent.getDefaultInstance() - .toBuilder() - .setMessage("custom error event") - .setContext(errorContext) - .build(); - // Report an event synchronously, use .reportErrorEventCallable for asynchronous reporting. - reportErrorsServiceClient.reportErrorEvent(projectName, customErrorEvent); - } - } -} -// [END error_reporting_setup_java] -// [END error_reporting_quickstart] -// [END errorreporting_quickstart] diff --git a/firestore/README.md b/firestore/README.md index 918210a81d7..27723c999d3 100644 --- a/firestore/README.md +++ b/firestore/README.md @@ -15,7 +15,7 @@ project. ## Setup - Install [Maven](http://maven.apache.org/). -- Open the [Firebase Console](https://console.firebase.com) and click **Add project**. +- Open the [Firebase Console](https://console.firebase.google.com) and click **Add project**. - Select the option to **Enable Cloud Firestore Beta** for this project. - Click **Create Project**. When you create a Cloud Firestore project, it also enables the API in the diff --git a/firestore/pom.xml b/firestore/pom.xml index 594e9aa520a..f7c4403a90e 100644 --- a/firestore/pom.xml +++ b/firestore/pom.xml @@ -32,7 +32,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 1.8 @@ -50,7 +50,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -79,7 +79,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 diff --git a/firestore/src/main/java/com/example/firestore/Quickstart.java b/firestore/src/main/java/com/example/firestore/Quickstart.java index dd7091b0b68..ffbc1f355a4 100644 --- a/firestore/src/main/java/com/example/firestore/Quickstart.java +++ b/firestore/src/main/java/com/example/firestore/Quickstart.java @@ -17,6 +17,7 @@ package com.example.firestore; import com.google.api.core.ApiFuture; +import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.firestore.DocumentReference; // [START fs_include_dependencies] import com.google.cloud.firestore.Firestore; @@ -48,11 +49,12 @@ public Quickstart() { this.db = db; } - public Quickstart(String projectId) { + public Quickstart(String projectId) throws Exception { // [START fs_initialize_project_id] FirestoreOptions firestoreOptions = FirestoreOptions.getDefaultInstance().toBuilder() .setProjectId(projectId) + .setCredentials(GoogleCredentials.getApplicationDefault()) .build(); Firestore db = firestoreOptions.getService(); // [END fs_initialize_project_id] diff --git a/firestore/src/test/java/com/example/firestore/BaseIntegrationTest.java b/firestore/src/test/java/com/example/firestore/BaseIntegrationTest.java index e2c7f666b99..53939494122 100644 --- a/firestore/src/test/java/com/example/firestore/BaseIntegrationTest.java +++ b/firestore/src/test/java/com/example/firestore/BaseIntegrationTest.java @@ -21,6 +21,7 @@ import com.example.firestore.snippets.ManageDataSnippetsIT; import com.example.firestore.snippets.model.City; import com.google.api.core.ApiFuture; +import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.firestore.DocumentReference; import com.google.cloud.firestore.DocumentSnapshot; import com.google.cloud.firestore.Firestore; @@ -49,6 +50,7 @@ private static String getEnvVar(String varName) { public static void baseSetup() throws Exception { projectId = getEnvVar("FIRESTORE_PROJECT_ID"); FirestoreOptions firestoreOptions = FirestoreOptions.getDefaultInstance().toBuilder() + .setCredentials(GoogleCredentials.getApplicationDefault()) .setProjectId(projectId) .build(); db = firestoreOptions.getService(); diff --git a/flexible/analytics/pom.xml b/flexible/analytics/pom.xml index 2d11c3037a4..4db0ba24f97 100644 --- a/flexible/analytics/pom.xml +++ b/flexible/analytics/pom.xml @@ -27,15 +27,15 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 1.8 1.8 - 2.2.0 - 9.4.29.v20200521 + 2.3.0 + 9.4.30.v20200611 false diff --git a/flexible/async-rest/pom.xml b/flexible/async-rest/pom.xml index a07a0f79239..2e29b912abb 100644 --- a/flexible/async-rest/pom.xml +++ b/flexible/async-rest/pom.xml @@ -28,7 +28,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -36,8 +36,8 @@ false - 2.2.0 - 9.4.29.v20200521 + 2.3.0 + 9.4.30.v20200611 1.8 1.8 diff --git a/flexible/cloudsql/pom.xml b/flexible/cloudsql/pom.xml index adffbfd1fa1..f2e11973fa6 100644 --- a/flexible/cloudsql/pom.xml +++ b/flexible/cloudsql/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -46,7 +46,7 @@ false - 9.4.29.v20200521 + 9.4.30.v20200611 jdbc:mysql://google/${database}?cloudSqlInstance=${INSTANCE_CONNECTION_NAME}&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=${user}&password=${password}&useSSL=false @@ -103,7 +103,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/flexible/cloudstorage/pom.xml b/flexible/cloudstorage/pom.xml index 5665219f68f..890bfef209f 100644 --- a/flexible/cloudstorage/pom.xml +++ b/flexible/cloudstorage/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -36,8 +36,8 @@ false - 2.2.0 - 9.4.29.v20200521 + 2.3.0 + 9.4.30.v20200611 @@ -48,7 +48,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import diff --git a/flexible/cron/pom.xml b/flexible/cron/pom.xml index f2bb1bcd16f..9aa9614f1f5 100644 --- a/flexible/cron/pom.xml +++ b/flexible/cron/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -36,8 +36,8 @@ false - 2.2.0 - 9.4.29.v20200521 + 2.3.0 + 9.4.30.v20200611 diff --git a/flexible/datastore/pom.xml b/flexible/datastore/pom.xml index 5c30da5e0c2..42186507256 100644 --- a/flexible/datastore/pom.xml +++ b/flexible/datastore/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -36,8 +36,8 @@ false - 2.2.0 - 9.4.29.v20200521 + 2.3.0 + 9.4.30.v20200611 @@ -48,7 +48,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import diff --git a/flexible/disk/pom.xml b/flexible/disk/pom.xml index 13252ca4ca0..09b32fb6db2 100644 --- a/flexible/disk/pom.xml +++ b/flexible/disk/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -36,8 +36,8 @@ false - 2.2.0 - 9.4.29.v20200521 + 2.3.0 + 9.4.30.v20200611 diff --git a/flexible/errorreporting/pom.xml b/flexible/errorreporting/pom.xml index 3fc82490526..73499bc6d32 100644 --- a/flexible/errorreporting/pom.xml +++ b/flexible/errorreporting/pom.xml @@ -27,15 +27,15 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 - 2.2.0 + 2.3.0 1.8 1.8 false - 9.4.29.v20200521 + 9.4.30.v20200611 diff --git a/flexible/extending-runtime/pom.xml b/flexible/extending-runtime/pom.xml index b38a2c372b4..eb64b2fb51b 100644 --- a/flexible/extending-runtime/pom.xml +++ b/flexible/extending-runtime/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -36,8 +36,8 @@ false - 2.2.0 - 9.4.29.v20200521 + 2.3.0 + 9.4.30.v20200611 diff --git a/flexible/gaeinfo/pom.xml b/flexible/gaeinfo/pom.xml index 1d046fe0abf..94561c32a7a 100644 --- a/flexible/gaeinfo/pom.xml +++ b/flexible/gaeinfo/pom.xml @@ -27,7 +27,7 @@ Copyright 2017 Google Inc. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -78,7 +78,7 @@ Copyright 2017 Google Inc. org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 @@ -94,7 +94,7 @@ Copyright 2017 Google Inc. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/flexible/helloworld-springboot/pom.xml b/flexible/helloworld-springboot/pom.xml index 48cea7cb329..314e00253df 100644 --- a/flexible/helloworld-springboot/pom.xml +++ b/flexible/helloworld-springboot/pom.xml @@ -28,7 +28,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -40,17 +40,17 @@ org.springframework.boot spring-boot-starter-web - 2.3.0.RELEASE + 2.3.1.RELEASE org.springframework.boot spring-boot-starter-actuator - 2.3.0.RELEASE + 2.3.1.RELEASE org.springframework.boot spring-boot-starter-test - 2.3.0.RELEASE + 2.3.1.RELEASE test @@ -66,7 +66,7 @@ org.springframework.boot spring-boot-maven-plugin - 2.3.0.RELEASE + 2.3.1.RELEASE @@ -78,7 +78,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG diff --git a/flexible/helloworld/gradle/wrapper/gradle-wrapper.properties b/flexible/helloworld/gradle/wrapper/gradle-wrapper.properties index 3c5a6cbe1f4..25dddea0715 100644 --- a/flexible/helloworld/gradle/wrapper/gradle-wrapper.properties +++ b/flexible/helloworld/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip diff --git a/flexible/helloworld/pom.xml b/flexible/helloworld/pom.xml index c313c259e55..4bb2a6ce524 100644 --- a/flexible/helloworld/pom.xml +++ b/flexible/helloworld/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -36,8 +36,8 @@ false - 2.2.0 - 9.4.29.v20200521 + 2.3.0 + 9.4.30.v20200611 diff --git a/flexible/mailgun/pom.xml b/flexible/mailgun/pom.xml index 034b0ff1336..dbf4645e58e 100644 --- a/flexible/mailgun/pom.xml +++ b/flexible/mailgun/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -36,8 +36,8 @@ false - 2.2.0 - 9.4.29.v20200521 + 2.3.0 + 9.4.30.v20200611 diff --git a/flexible/mailjet/pom.xml b/flexible/mailjet/pom.xml index 5568697dfaf..b7e3158caf0 100644 --- a/flexible/mailjet/pom.xml +++ b/flexible/mailjet/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -36,8 +36,8 @@ false - 2.2.0 - 9.4.29.v20200521 + 2.3.0 + 9.4.30.v20200611 4.5.0 diff --git a/flexible/memcache/pom.xml b/flexible/memcache/pom.xml index f849f0d1338..2f2d3fcd570 100644 --- a/flexible/memcache/pom.xml +++ b/flexible/memcache/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -37,8 +37,8 @@ false - 2.2.0 - 9.4.29.v20200521 + 2.3.0 + 9.4.30.v20200611 diff --git a/flexible/postgres/pom.xml b/flexible/postgres/pom.xml index ea929c61d56..ba085040f9a 100644 --- a/flexible/postgres/pom.xml +++ b/flexible/postgres/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -46,7 +46,7 @@ false - 9.4.29.v20200521 + 9.4.30.v20200611 jdbc:postgresql://google/${database}?useSSL=false&socketFactoryArg=${INSTANCE_CONNECTION_NAME}&socketFactory=com.google.cloud.sql.postgres.SocketFactory&user=${user}&password=${password} @@ -79,7 +79,7 @@ org.postgresql postgresql - 42.2.12 + 42.2.14 @@ -105,7 +105,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/flexible/pubsub/pom.xml b/flexible/pubsub/pom.xml index f0b4d0e6138..5a6aaad5a74 100644 --- a/flexible/pubsub/pom.xml +++ b/flexible/pubsub/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -36,8 +36,8 @@ false - 2.2.0 - 9.4.29.v20200521 + 2.3.0 + 9.4.30.v20200611 @@ -64,12 +64,12 @@ com.google.cloud google-cloud-pubsub - 1.105.1 + 1.107.0 com.google.cloud google-cloud-datastore - 1.102.4 + 1.104.0 diff --git a/flexible/sendgrid/pom.xml b/flexible/sendgrid/pom.xml index 36fa3fe5668..163444804cd 100644 --- a/flexible/sendgrid/pom.xml +++ b/flexible/sendgrid/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -36,8 +36,8 @@ false - 2.2.0 - 9.4.29.v20200521 + 2.3.0 + 9.4.30.v20200611 @@ -52,7 +52,7 @@ com.sendgrid sendgrid-java - 4.5.0 + 4.6.0 diff --git a/flexible/sparkjava/pom.xml b/flexible/sparkjava/pom.xml index f0bd48ef61d..209fd5605ea 100644 --- a/flexible/sparkjava/pom.xml +++ b/flexible/sparkjava/pom.xml @@ -29,14 +29,14 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 1.8 1.8 - 2.2.0 + 2.3.0 ${project.build.directory}/spark-1.0-jar-with-dependencies.jar @@ -64,7 +64,7 @@ limitations under the License. com.google.cloud google-cloud-datastore - 1.102.4 + 1.104.0 @@ -94,7 +94,7 @@ limitations under the License. org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 com.google.appengine.sparkdemo.Main diff --git a/flexible/static-files/pom.xml b/flexible/static-files/pom.xml index 4af19bb88d5..ac149448963 100644 --- a/flexible/static-files/pom.xml +++ b/flexible/static-files/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -36,8 +36,8 @@ false - 2.2.0 - 9.4.29.v20200521 + 2.3.0 + 9.4.30.v20200611 diff --git a/flexible/twilio/pom.xml b/flexible/twilio/pom.xml index d6c9364d7d7..8b37743ae96 100644 --- a/flexible/twilio/pom.xml +++ b/flexible/twilio/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -36,8 +36,8 @@ false - 2.2.0 - 9.4.29.v20200521 + 2.3.0 + 9.4.30.v20200611 diff --git a/flexible/websocket-jetty/pom.xml b/flexible/websocket-jetty/pom.xml index 7a55a2f3236..029e815d2d4 100644 --- a/flexible/websocket-jetty/pom.xml +++ b/flexible/websocket-jetty/pom.xml @@ -30,14 +30,14 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 1.8 1.8 false - 9.4.29.v20200521 + 9.4.30.v20200611 @@ -76,7 +76,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/flexible/websocket-jsr356/pom.xml b/flexible/websocket-jsr356/pom.xml index 61eaba3d2ac..7056c294371 100644 --- a/flexible/websocket-jsr356/pom.xml +++ b/flexible/websocket-jsr356/pom.xml @@ -27,14 +27,14 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 1.8 1.8 false - 9.4.29.v20200521 + 9.4.30.v20200611 @@ -73,7 +73,7 @@ limitations under the License. com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG GCLOUD_CONFIG diff --git a/functions/concepts/env-vars/pom.xml b/functions/concepts/env-vars/pom.xml index 31844538af6..91caf87d79c 100644 --- a/functions/concepts/env-vars/pom.xml +++ b/functions/concepts/env-vars/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -107,12 +107,12 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/concepts/execution-count/pom.xml b/functions/concepts/execution-count/pom.xml index 7c38b72b814..42806b5c157 100644 --- a/functions/concepts/execution-count/pom.xml +++ b/functions/concepts/execution-count/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -105,7 +105,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.EnvVars @@ -113,7 +113,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/concepts/file-system/pom.xml b/functions/concepts/file-system/pom.xml index af2ec97744d..fc010d52fe6 100644 --- a/functions/concepts/file-system/pom.xml +++ b/functions/concepts/file-system/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -99,7 +99,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.FileSystem @@ -107,7 +107,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/concepts/lazy-fields/pom.xml b/functions/concepts/lazy-fields/pom.xml index e41b59bd3cb..d7ffefd8f14 100644 --- a/functions/concepts/lazy-fields/pom.xml +++ b/functions/concepts/lazy-fields/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -99,7 +99,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.EnvVars @@ -107,7 +107,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/concepts/retry-pubsub/pom.xml b/functions/concepts/retry-pubsub/pom.xml index c80b7361249..4f6c6d38a36 100644 --- a/functions/concepts/retry-pubsub/pom.xml +++ b/functions/concepts/retry-pubsub/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -110,7 +110,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.RetryPubSub @@ -118,7 +118,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/concepts/retry-pubsub/src/test/java/functions/RetryPubSubTest.java b/functions/concepts/retry-pubsub/src/test/java/functions/RetryPubSubTest.java index 8113fd8bc54..fbb77a38382 100644 --- a/functions/concepts/retry-pubsub/src/test/java/functions/RetryPubSubTest.java +++ b/functions/concepts/retry-pubsub/src/test/java/functions/RetryPubSubTest.java @@ -19,10 +19,12 @@ import static com.google.common.truth.Truth.assertThat; import com.google.common.testing.TestLogHandler; +import com.google.gson.Gson; import functions.eventpojos.PubSubMessage; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Base64; +import java.util.Map; import java.util.logging.Logger; import org.junit.After; import org.junit.BeforeClass; @@ -38,6 +40,8 @@ public class RetryPubSubTest { private static final TestLogHandler LOG_HANDLER = new TestLogHandler(); + private static final Gson gson = new Gson(); + @BeforeClass public static void beforeClass() { logger.addHandler(LOG_HANDLER); @@ -50,7 +54,7 @@ public void afterTest() { @Test(expected = RuntimeException.class) public void retryPubsub_handlesRetryMsg() throws IOException { - String data = "{\"retry\": true}"; + String data = gson.toJson(Map.of("retry", true)); String encodedData = new String( Base64.getEncoder().encode(data.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8); @@ -62,7 +66,7 @@ public void retryPubsub_handlesRetryMsg() throws IOException { @Test public void retryPubsub_handlesStopMsg() throws IOException { - String data = "{\"retry\": false}"; + String data = gson.toJson(Map.of("retry", false)); String encodedData = new String( Base64.getEncoder().encode(data.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8); diff --git a/functions/concepts/retry-timeout/pom.xml b/functions/concepts/retry-timeout/pom.xml index ca68161a68b..0f5bd48288b 100644 --- a/functions/concepts/retry-timeout/pom.xml +++ b/functions/concepts/retry-timeout/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -111,7 +111,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.RetryTimeout @@ -119,7 +119,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/concepts/retry-timeout/src/main/java/functions/RetryTimeout.java b/functions/concepts/retry-timeout/src/main/java/functions/RetryTimeout.java index b89181f19d9..a5fcd42a409 100644 --- a/functions/concepts/retry-timeout/src/main/java/functions/RetryTimeout.java +++ b/functions/concepts/retry-timeout/src/main/java/functions/RetryTimeout.java @@ -59,6 +59,7 @@ public void accept(PubSubMessage message, Context context) { } // Process events that are recent enough + // To retry this invocation, throw an exception here logger.info(String.format("Processing event %s.", data)); } } diff --git a/functions/concepts/retry-timeout/src/test/java/functions/RetryTimeoutTest.java b/functions/concepts/retry-timeout/src/test/java/functions/RetryTimeoutTest.java index dd85a5ef16a..05e115b1014 100644 --- a/functions/concepts/retry-timeout/src/test/java/functions/RetryTimeoutTest.java +++ b/functions/concepts/retry-timeout/src/test/java/functions/RetryTimeoutTest.java @@ -19,10 +19,12 @@ import static com.google.common.truth.Truth.assertThat; import com.google.common.testing.TestLogHandler; +import com.google.gson.Gson; import functions.eventpojos.PubSubMessage; import java.time.Instant; import java.time.ZoneOffset; import java.time.ZonedDateTime; +import java.util.Map; import java.util.logging.Logger; import org.junit.After; import org.junit.Before; @@ -41,6 +43,8 @@ public class RetryTimeoutTest { private static final TestLogHandler LOG_HANDLER = new TestLogHandler(); + private static final Gson gson = new Gson(); + @BeforeClass public static void beforeClass() { logger.addHandler(LOG_HANDLER); @@ -61,8 +65,8 @@ public void afterTest() { @Test public void retryTimeout_handlesRetryMsg() { - String timestampData = String.format( - "{\"timestamp\":\"%s\"}", ZonedDateTime.now(ZoneOffset.UTC).toString()); + String timestampData = gson.toJson(Map.of( + "timestamp", ZonedDateTime.now(ZoneOffset.UTC).toString())); PubSubMessage pubsubMessage = new PubSubMessage(); pubsubMessage.setData(timestampData); @@ -76,7 +80,7 @@ public void retryTimeout_handlesRetryMsg() { @Test public void retryTimeout_handlesStopMsg() { String timestamp = ZonedDateTime.ofInstant(Instant.ofEpochMilli(0), ZoneOffset.UTC).toString(); - String timestampData = String.format("{\"timestamp\":\"%s\"}", timestamp); + String timestampData = gson.toJson(Map.of("timestamp", timestamp)); PubSubMessage pubsubMessage = new PubSubMessage(); diff --git a/functions/concepts/scopes/pom.xml b/functions/concepts/scopes/pom.xml index f3630036123..7b4227c6bf5 100644 --- a/functions/concepts/scopes/pom.xml +++ b/functions/concepts/scopes/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -99,7 +99,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.Scopes @@ -107,7 +107,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/firebase/auth/pom.xml b/functions/firebase/auth/pom.xml index 3f3e2413414..d85b60e9d5b 100644 --- a/functions/firebase/auth/pom.xml +++ b/functions/firebase/auth/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -104,7 +104,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.FirebaseAuth @@ -114,7 +114,7 @@ maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 **/*Test.java diff --git a/functions/firebase/auth/src/test/java/functions/FirebaseAuthTest.java b/functions/firebase/auth/src/test/java/functions/FirebaseAuthTest.java index 5d3c2cfe0fe..2fac9a6fca3 100644 --- a/functions/firebase/auth/src/test/java/functions/FirebaseAuthTest.java +++ b/functions/firebase/auth/src/test/java/functions/FirebaseAuthTest.java @@ -18,7 +18,9 @@ import com.google.common.testing.TestLogHandler; import com.google.common.truth.Truth; +import com.google.gson.Gson; import java.io.IOException; +import java.util.Map; import java.util.logging.Logger; import org.junit.Before; import org.junit.BeforeClass; @@ -35,6 +37,8 @@ public class FirebaseAuthTest { private static final TestLogHandler LOG_HANDLER = new TestLogHandler(); + private static final Gson gson = new Gson(); + @BeforeClass public static void beforeClass() { logger.addHandler(LOG_HANDLER); @@ -47,7 +51,8 @@ public void beforeTest() throws IOException { @Test public void functionsFirebaseAuth_shouldShowUserId() { - new FirebaseAuth().accept("{\"uid\": \"foo\"}", null); + String jsonStr = gson.toJson(Map.of("uid", "foo")); + new FirebaseAuth().accept(jsonStr, null); Truth.assertThat(LOG_HANDLER.getStoredLogRecords().get(0).getMessage()).isEqualTo( "Function triggered by change to user: foo"); @@ -55,7 +60,8 @@ public void functionsFirebaseAuth_shouldShowUserId() { @Test public void functionsFirebaseAuth_shouldShowOrigin() { - new FirebaseAuth().accept("{\"metadata\": {\"createdAt\": \"123\"}}", null); + String jsonStr = gson.toJson(Map.of("metadata", Map.of("createdAt", "123"))); + new FirebaseAuth().accept(jsonStr, null); Truth.assertThat(LOG_HANDLER.getStoredLogRecords().get(0).getMessage()).isEqualTo( "Created at: 123"); @@ -63,7 +69,8 @@ public void functionsFirebaseAuth_shouldShowOrigin() { @Test public void functionsFirebaseAuth_shouldShowVersion() { - new FirebaseAuth().accept("{\"email\": \"foo@google.com\"}", null); + String jsonStr = gson.toJson(Map.of("email", "foo@google.com")); + new FirebaseAuth().accept(jsonStr, null); Truth.assertThat(LOG_HANDLER.getStoredLogRecords().get(0).getMessage()).isEqualTo( "Email: foo@google.com"); diff --git a/functions/firebase/firestore-reactive/pom.xml b/functions/firebase/firestore-reactive/pom.xml index 8dc9eca8cd3..ae0fd033f8b 100644 --- a/functions/firebase/firestore-reactive/pom.xml +++ b/functions/firebase/firestore-reactive/pom.xml @@ -30,7 +30,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -44,7 +44,7 @@ com.google.cloud google-cloud-firestore - 1.33.0 + 1.35.0 @@ -117,7 +117,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.FirebaseFirestoreReactive @@ -127,7 +127,7 @@ maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 diff --git a/functions/firebase/firestore-reactive/src/main/java/functions/FirebaseFirestoreReactive.java b/functions/firebase/firestore-reactive/src/main/java/functions/FirebaseFirestoreReactive.java index 9d980f69d83..912de8ae0f9 100644 --- a/functions/firebase/firestore-reactive/src/main/java/functions/FirebaseFirestoreReactive.java +++ b/functions/firebase/firestore-reactive/src/main/java/functions/FirebaseFirestoreReactive.java @@ -75,8 +75,7 @@ public void accept(String json, Context context) { String newValue = currentValue.toUpperCase(Locale.getDefault()); // Update Firestore DB with ALL CAPS value - Map newFields = new HashMap<>(); - newFields.put("original", newValue); + Map newFields = Map.of("original", newValue); String affectedDoc = context.resource().split("/documents/")[1].replace("\"", ""); diff --git a/functions/firebase/firestore-reactive/src/test/java/functions/FirebaseFirestoreReactiveTest.java b/functions/firebase/firestore-reactive/src/test/java/functions/FirebaseFirestoreReactiveTest.java index 15073ba3816..f5e79a5814a 100644 --- a/functions/firebase/firestore-reactive/src/test/java/functions/FirebaseFirestoreReactiveTest.java +++ b/functions/firebase/firestore-reactive/src/test/java/functions/FirebaseFirestoreReactiveTest.java @@ -23,7 +23,9 @@ import com.google.cloud.firestore.Firestore; import com.google.common.testing.TestLogHandler; import com.google.common.truth.Truth; +import com.google.gson.Gson; import functions.eventpojos.MockContext; +import java.util.Map; import java.util.logging.Logger; import org.junit.After; import org.junit.Before; @@ -47,6 +49,8 @@ public class FirebaseFirestoreReactiveTest { private static final TestLogHandler LOG_HANDLER = new TestLogHandler(); + private static final Gson gson = new Gson(); + @BeforeClass public static void beforeClass() { logger.addHandler(LOG_HANDLER); @@ -71,7 +75,11 @@ public void afterTest() { @Test public void functionsFirebaseReactive_shouldCapitalizeOriginalValue() { - String jsonStr = "{\"value\":{\"fields\":{\"original\":{\"stringValue\":\"foo\"}}}}"; + + String jsonStr = gson.toJson(Map.of("value", + Map.of("fields", + Map.of("original", + Map.of("stringValue", "foo"))))); MockContext context = new MockContext(); context.resource = "projects/_/databases/(default)/documents/messages/ABCDE12345"; @@ -86,7 +94,10 @@ public void functionsFirebaseReactive_shouldCapitalizeOriginalValue() { @Test public void functionsFirebaseReactive_shouldReportBadJson() { - String jsonStr = "{\"value\":{\"fields\":{\"original\":{\"missingValue\":\"foo\"}}}}"; + String jsonStr = gson.toJson(Map.of("value", + Map.of("fields", + Map.of("original", + Map.of("missingValue", "foo"))))); MockContext context = new MockContext(); context.resource = "projects/_/databases/(default)/documents/messages/ABCDE12345"; diff --git a/functions/firebase/firestore/pom.xml b/functions/firebase/firestore/pom.xml index 80bbe75516d..cb8d2f90c8b 100644 --- a/functions/firebase/firestore/pom.xml +++ b/functions/firebase/firestore/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -40,7 +40,7 @@ com.google.cloud google-cloud-firestore - 1.33.0 + 1.35.0 @@ -104,7 +104,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.FirebaseFirestore @@ -114,7 +114,7 @@ maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 **/*Test.java diff --git a/functions/firebase/firestore/src/test/java/functions/FirebaseFirestoreTest.java b/functions/firebase/firestore/src/test/java/functions/FirebaseFirestoreTest.java index d6035dd98bf..7e147ec3b50 100644 --- a/functions/firebase/firestore/src/test/java/functions/FirebaseFirestoreTest.java +++ b/functions/firebase/firestore/src/test/java/functions/FirebaseFirestoreTest.java @@ -18,8 +18,10 @@ import com.google.common.testing.TestLogHandler; import com.google.common.truth.Truth; +import com.google.gson.Gson; import functions.eventpojos.MockContext; import java.util.List; +import java.util.Map; import java.util.logging.LogRecord; import java.util.logging.Logger; import org.junit.After; @@ -37,6 +39,8 @@ public class FirebaseFirestoreTest { private static final TestLogHandler LOG_HANDLER = new TestLogHandler(); + private static final Gson gson = new Gson(); + @BeforeClass public static void beforeClass() { logger.addHandler(LOG_HANDLER); @@ -64,7 +68,7 @@ public void functionsFirebaseFirestore_shouldIgnoreMissingValuesTest() { @Test public void functionsFirebaseFirestore_shouldProcessPresentValues() { - String jsonStr = "{\"oldValue\": 999, \"value\": 777 }"; + String jsonStr = gson.toJson(Map.of("oldValue", 999, "value", 777)); MockContext context = new MockContext(); context.resource = "resource_1"; diff --git a/functions/firebase/remote-config/pom.xml b/functions/firebase/remote-config/pom.xml index 68ad716953f..65674057101 100644 --- a/functions/firebase/remote-config/pom.xml +++ b/functions/firebase/remote-config/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -104,7 +104,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.FirebaseRemoteConfig @@ -114,7 +114,7 @@ maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 **/*Test.java diff --git a/functions/firebase/remote-config/src/test/java/functions/FirebaseRemoteConfigTest.java b/functions/firebase/remote-config/src/test/java/functions/FirebaseRemoteConfigTest.java index f0786248eae..b6a41824bf5 100644 --- a/functions/firebase/remote-config/src/test/java/functions/FirebaseRemoteConfigTest.java +++ b/functions/firebase/remote-config/src/test/java/functions/FirebaseRemoteConfigTest.java @@ -18,6 +18,8 @@ import com.google.common.testing.TestLogHandler; import com.google.common.truth.Truth; +import com.google.gson.Gson; +import java.util.Map; import java.util.logging.Logger; import org.junit.After; import org.junit.BeforeClass; @@ -33,6 +35,8 @@ public class FirebaseRemoteConfigTest { private static final TestLogHandler LOG_HANDLER = new TestLogHandler(); + private static final Gson gson = new Gson(); + @BeforeClass public static void beforeClass() { logger.addHandler(LOG_HANDLER); @@ -45,7 +49,8 @@ public void afterTest() { @Test public void functionsFirebaseRemoteConfig_shouldShowUpdateType() { - new FirebaseRemoteConfig().accept("{\"updateType\": \"foo\"}", null); + String jsonStr = gson.toJson(Map.of("updateType", "foo")); + new FirebaseRemoteConfig().accept(jsonStr, null); Truth.assertThat(LOG_HANDLER.getStoredLogRecords().get(0).getMessage()).isEqualTo( "Update type: foo"); @@ -53,7 +58,8 @@ public void functionsFirebaseRemoteConfig_shouldShowUpdateType() { @Test public void functionsFirebaseRemoteConfig_shouldShowOrigin() { - new FirebaseRemoteConfig().accept("{\"updateOrigin\": \"foo\"}", null); + String jsonStr = gson.toJson(Map.of("updateOrigin", "foo")); + new FirebaseRemoteConfig().accept(jsonStr, null); Truth.assertThat(LOG_HANDLER.getStoredLogRecords().get(0).getMessage()).isEqualTo( "Origin: foo"); @@ -61,7 +67,8 @@ public void functionsFirebaseRemoteConfig_shouldShowOrigin() { @Test public void functionsFirebaseRemoteConfig_shouldShowVersion() { - new FirebaseRemoteConfig().accept("{\"versionNumber\": 2}", null); + String jsonStr = gson.toJson(Map.of("versionNumber", 2)); + new FirebaseRemoteConfig().accept(jsonStr, null); Truth.assertThat(LOG_HANDLER.getStoredLogRecords().get(0).getMessage()).isEqualTo("Version: 2"); } diff --git a/functions/firebase/rtdb/pom.xml b/functions/firebase/rtdb/pom.xml index a4226dfcf87..cf1dec65225 100644 --- a/functions/firebase/rtdb/pom.xml +++ b/functions/firebase/rtdb/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -103,7 +103,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.FirebaseRtdb @@ -113,7 +113,7 @@ maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 **/*Test.java diff --git a/functions/firebase/rtdb/src/test/java/functions/FirebaseRtdbTest.java b/functions/firebase/rtdb/src/test/java/functions/FirebaseRtdbTest.java index 34ca16e80d3..a35eec7de68 100644 --- a/functions/firebase/rtdb/src/test/java/functions/FirebaseRtdbTest.java +++ b/functions/firebase/rtdb/src/test/java/functions/FirebaseRtdbTest.java @@ -18,9 +18,11 @@ import com.google.common.testing.TestLogHandler; import com.google.common.truth.Truth; +import com.google.gson.Gson; import functions.eventpojos.MockContext; import java.io.IOException; import java.util.List; +import java.util.Map; import java.util.logging.LogRecord; import java.util.logging.Logger; import org.junit.Before; @@ -37,6 +39,8 @@ public class FirebaseRtdbTest { private static final TestLogHandler LOG_HANDLER = new TestLogHandler(); + private static final Gson gson = new Gson(); + @BeforeClass public static void beforeClass() { logger.addHandler(LOG_HANDLER); @@ -62,7 +66,7 @@ public void functionsFirebaseRtdb_shouldDefaultAdminToZero() { @Test public void functionsFirebaseRtdb_shouldDisplayAdminStatus() { - String jsonStr = "{\"auth\": { \"admin\": true }}"; + String jsonStr = gson.toJson(Map.of("auth", Map.of("admin", true))); MockContext context = new MockContext(); context.resource = "resource_1"; @@ -78,7 +82,7 @@ public void functionsFirebaseRtdb_shouldDisplayAdminStatus() { @Test public void functionsFirebaseRtdb_shouldShowDelta() { - String jsonStr = "{\"delta\": { \"value\": 2 }}"; + String jsonStr = gson.toJson(Map.of("delta", Map.of("value", 2))); MockContext context = new MockContext(); context.resource = "resource_1"; @@ -91,7 +95,9 @@ public void functionsFirebaseRtdb_shouldShowDelta() { Truth.assertThat(logs.get(0).getMessage()).isEqualTo( "Function triggered by change to: resource_1"); Truth.assertThat(logs.get(2).getMessage()).isEqualTo("Delta:"); - Truth.assertThat(logs.get(3).getMessage()).isEqualTo("{\"value\":2}"); + + String expectedJsonStr = gson.toJson(Map.of("value", 2)); + Truth.assertThat(logs.get(3).getMessage()).isEqualTo(expectedJsonStr); } } diff --git a/functions/helloworld/groovy-hello-pubsub/pom.xml b/functions/helloworld/groovy-hello-pubsub/pom.xml index 82faea265e1..3feca19497a 100644 --- a/functions/helloworld/groovy-hello-pubsub/pom.xml +++ b/functions/helloworld/groovy-hello-pubsub/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -107,7 +107,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.GroovyHelloPubSub @@ -140,7 +140,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/helloworld/groovy-helloworld/pom.xml b/functions/helloworld/groovy-helloworld/pom.xml index 7b9fef4f950..cb06a76eabd 100644 --- a/functions/helloworld/groovy-helloworld/pom.xml +++ b/functions/helloworld/groovy-helloworld/pom.xml @@ -29,7 +29,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -121,7 +121,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.GroovyHelloWorld @@ -155,7 +155,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/helloworld/hello-error/pom.xml b/functions/helloworld/hello-error/pom.xml index e406ea139a6..cb58d80e5a6 100644 --- a/functions/helloworld/hello-error/pom.xml +++ b/functions/helloworld/hello-error/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -78,7 +78,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.HelloError @@ -88,7 +88,7 @@ maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 **/*Test.java diff --git a/functions/helloworld/hello-gcs-generic/pom.xml b/functions/helloworld/hello-gcs-generic/pom.xml index a7105c0682f..b139d2ad69b 100644 --- a/functions/helloworld/hello-gcs-generic/pom.xml +++ b/functions/helloworld/hello-gcs-generic/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -92,7 +92,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.HelloGcsGeneric @@ -102,7 +102,7 @@ maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 **/*Test.java diff --git a/functions/helloworld/hello-gcs/pom.xml b/functions/helloworld/hello-gcs/pom.xml index 0cee6336141..9f38e60c0a3 100644 --- a/functions/helloworld/hello-gcs/pom.xml +++ b/functions/helloworld/hello-gcs/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -42,7 +42,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -101,13 +101,13 @@ io.github.resilience4j resilience4j-core - 1.4.0 + 1.5.0 test io.github.resilience4j resilience4j-retry - 1.4.0 + 1.5.0 test @@ -115,7 +115,7 @@ com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 test @@ -152,7 +152,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.HelloGcs @@ -160,7 +160,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 **/*Test.java diff --git a/functions/helloworld/hello-http/pom.xml b/functions/helloworld/hello-http/pom.xml index 8fa821cda52..38db06d86a3 100644 --- a/functions/helloworld/hello-http/pom.xml +++ b/functions/helloworld/hello-http/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -69,13 +69,13 @@ io.github.resilience4j resilience4j-core - 1.4.0 + 1.5.0 test io.github.resilience4j resilience4j-retry - 1.4.0 + 1.5.0 test @@ -83,7 +83,7 @@ com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 test @@ -127,7 +127,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.HelloHttp @@ -137,7 +137,7 @@ maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 **/*Test.java diff --git a/functions/helloworld/hello-http/src/test/java/functions/ExampleSystemTest.java b/functions/helloworld/hello-http/src/test/java/functions/ExampleSystemTest.java index 1fb8f4584da..e274af87d7e 100644 --- a/functions/helloworld/hello-http/src/test/java/functions/ExampleSystemTest.java +++ b/functions/helloworld/hello-http/src/test/java/functions/ExampleSystemTest.java @@ -38,7 +38,7 @@ public class ExampleSystemTest { @Test public void helloHttp_shouldRunWithFunctionsFramework() throws IOException, InterruptedException { - String functionUrl = BASE_URL + "/helloHttp"; + String functionUrl = BASE_URL + "/HelloHttp"; java.net.http.HttpRequest getRequest = java.net.http.HttpRequest.newBuilder().uri(URI.create(functionUrl)).GET().build(); diff --git a/functions/helloworld/hello-http/src/test/java/functions/HelloHttpTest.java b/functions/helloworld/hello-http/src/test/java/functions/HelloHttpTest.java index e626bdadd8c..3c429d10f37 100644 --- a/functions/helloworld/hello-http/src/test/java/functions/HelloHttpTest.java +++ b/functions/helloworld/hello-http/src/test/java/functions/HelloHttpTest.java @@ -23,11 +23,13 @@ import com.google.cloud.functions.HttpRequest; import com.google.cloud.functions.HttpResponse; +import com.google.gson.Gson; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; +import java.util.Map; import java.util.Optional; import org.junit.Before; import org.junit.Test; @@ -43,6 +45,7 @@ public class HelloHttpTest { private BufferedWriter writerOut; private StringWriter responseOut; + private static final Gson gson = new Gson(); @Before public void beforeTest() throws IOException { @@ -78,6 +81,7 @@ public void helloHttp_urlParamsGet() throws IOException { @Test public void helloHttp_bodyParamsPost() throws IOException { + String requestJson = gson.toJson(Map.of("name", "Jane")); BufferedReader jsonReader = new BufferedReader(new StringReader("{'name': 'Jane'}")); when(request.getReader()).thenReturn(jsonReader); diff --git a/functions/helloworld/hello-pubsub/pom.xml b/functions/helloworld/hello-pubsub/pom.xml index 408c465ed46..b877d244ca3 100644 --- a/functions/helloworld/hello-pubsub/pom.xml +++ b/functions/helloworld/hello-pubsub/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -42,7 +42,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -102,13 +102,13 @@ io.github.resilience4j resilience4j-core - 1.4.0 + 1.5.0 test io.github.resilience4j resilience4j-retry - 1.4.0 + 1.5.0 test @@ -116,7 +116,7 @@ com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 test @@ -153,7 +153,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.HelloPubSub @@ -163,7 +163,7 @@ maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 **/*Test.java diff --git a/functions/helloworld/helloworld/pom.xml b/functions/helloworld/helloworld/pom.xml index c1d901042c9..3e151a00173 100644 --- a/functions/helloworld/helloworld/pom.xml +++ b/functions/helloworld/helloworld/pom.xml @@ -32,7 +32,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -111,7 +111,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.HelloWorld @@ -122,7 +122,7 @@ maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 **/*Test.java diff --git a/functions/helloworld/kotlin-hello-pubsub/pom.xml b/functions/helloworld/kotlin-hello-pubsub/pom.xml index 267bab99ebc..2e0af3998bc 100644 --- a/functions/helloworld/kotlin-hello-pubsub/pom.xml +++ b/functions/helloworld/kotlin-hello-pubsub/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -111,7 +111,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.KotlinHelloBackground @@ -149,7 +149,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/helloworld/kotlin-helloworld/pom.xml b/functions/helloworld/kotlin-helloworld/pom.xml index b8b58a9febb..e3dcedc1365 100644 --- a/functions/helloworld/kotlin-helloworld/pom.xml +++ b/functions/helloworld/kotlin-helloworld/pom.xml @@ -29,7 +29,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -124,7 +124,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.KotlinHelloWorld @@ -162,7 +162,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/helloworld/scala-hello-pubsub/pom.xml b/functions/helloworld/scala-hello-pubsub/pom.xml index 8d10265746b..42add4724a9 100644 --- a/functions/helloworld/scala-hello-pubsub/pom.xml +++ b/functions/helloworld/scala-hello-pubsub/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -41,7 +41,7 @@ org.scala-lang scala-library - 2.13.2 + 2.13.3 @@ -106,7 +106,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.ScalaHelloBackground @@ -129,7 +129,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/helloworld/scala-helloworld/pom.xml b/functions/helloworld/scala-helloworld/pom.xml index 2b7458d8119..9e82d234f14 100644 --- a/functions/helloworld/scala-helloworld/pom.xml +++ b/functions/helloworld/scala-helloworld/pom.xml @@ -29,7 +29,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -44,7 +44,7 @@ org.scala-lang scala-library - 2.13.2 + 2.13.3 @@ -118,7 +118,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.ScalaHelloWorld @@ -142,7 +142,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/http/bearer-token-http/pom.xml b/functions/http/bearer-token-http/pom.xml index 39eda7354bd..1995774b275 100644 --- a/functions/http/bearer-token-http/pom.xml +++ b/functions/http/bearer-token-http/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -77,7 +77,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.BearerTokenHttp @@ -85,7 +85,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/http/cors-enabled-auth/pom.xml b/functions/http/cors-enabled-auth/pom.xml index 434e22b2847..a06dd1adba3 100644 --- a/functions/http/cors-enabled-auth/pom.xml +++ b/functions/http/cors-enabled-auth/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -98,7 +98,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.CorsEnabledAuth @@ -106,7 +106,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/http/cors-enabled/pom.xml b/functions/http/cors-enabled/pom.xml index 6bdfed96f05..796e850615b 100644 --- a/functions/http/cors-enabled/pom.xml +++ b/functions/http/cors-enabled/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -98,7 +98,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.CorsEnabled @@ -106,7 +106,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/http/http-form-data/pom.xml b/functions/http/http-form-data/pom.xml index 2f6ad7e29e3..a0687885058 100644 --- a/functions/http/http-form-data/pom.xml +++ b/functions/http/http-form-data/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -104,7 +104,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.HttpFormData @@ -112,7 +112,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/http/http-method/pom.xml b/functions/http/http-method/pom.xml index d038775f17e..e1defdc79d2 100644 --- a/functions/http/http-method/pom.xml +++ b/functions/http/http-method/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -97,7 +97,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.HttpMethod @@ -105,7 +105,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/http/parse-content-type/pom.xml b/functions/http/parse-content-type/pom.xml index 884f28aa8e0..cbbfe04887e 100644 --- a/functions/http/parse-content-type/pom.xml +++ b/functions/http/parse-content-type/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -103,7 +103,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.ParseContentType @@ -111,7 +111,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/http/parse-content-type/src/test/java/functions/ParseContentTypeTest.java b/functions/http/parse-content-type/src/test/java/functions/ParseContentTypeTest.java index 36022d2ce15..6a3f09aa7f1 100644 --- a/functions/http/parse-content-type/src/test/java/functions/ParseContentTypeTest.java +++ b/functions/http/parse-content-type/src/test/java/functions/ParseContentTypeTest.java @@ -21,6 +21,7 @@ import com.google.cloud.functions.HttpRequest; import com.google.cloud.functions.HttpResponse; +import com.google.gson.Gson; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; @@ -30,6 +31,7 @@ import java.io.StringWriter; import java.nio.charset.StandardCharsets; import java.util.Base64; +import java.util.Map; import java.util.Optional; import org.junit.Before; import org.junit.Test; @@ -45,6 +47,7 @@ public class ParseContentTypeTest { private BufferedWriter writerOut; private StringWriter responseOut; + private static Gson gson = new Gson(); @Before public void beforeTest() throws IOException { @@ -58,7 +61,8 @@ public void beforeTest() throws IOException { @Test public void parseContentTypeTest_json() throws IOException { // Send a request with JSON data - BufferedReader bodyReader = new BufferedReader(new StringReader("{\"name\":\"John\"}")); + String requestJson = gson.toJson(Map.of("name", "John")); + BufferedReader bodyReader = new BufferedReader(new StringReader(requestJson)); when(request.getContentType()).thenReturn(Optional.of("application/json")); when(request.getReader()).thenReturn(bodyReader); diff --git a/functions/http/send-http-request/pom.xml b/functions/http/send-http-request/pom.xml index 3137f24f313..3b65eaef356 100644 --- a/functions/http/send-http-request/pom.xml +++ b/functions/http/send-http-request/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -98,7 +98,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.SendHttpRequest @@ -106,7 +106,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/imagemagick/pom.xml b/functions/imagemagick/pom.xml index 8e673ddaf16..399c8930746 100644 --- a/functions/imagemagick/pom.xml +++ b/functions/imagemagick/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -41,7 +41,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -117,7 +117,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.ImageMagick @@ -125,7 +125,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/imagemagick/src/main/java/functions/ImageMagick.java b/functions/imagemagick/src/main/java/functions/ImageMagick.java index 0955b1cb406..c2b6f3bcaa0 100644 --- a/functions/imagemagick/src/main/java/functions/ImageMagick.java +++ b/functions/imagemagick/src/main/java/functions/ImageMagick.java @@ -68,13 +68,12 @@ public void accept(GcsEvent gcsEvent, Context context) { logger.info(String.format("Analyzing %s", gcsEvent.getName())); // Construct request. - List requests = new ArrayList<>(); ImageSource imgSource = ImageSource.newBuilder().setImageUri(gcsPath).build(); Image img = Image.newBuilder().setSource(imgSource).build(); Feature feature = Feature.newBuilder().setType(Type.SAFE_SEARCH_DETECTION).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feature).setImage(img).build(); - requests.add(request); + List requests = List.of(request); // Send request to the Vision API. try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { @@ -113,13 +112,8 @@ private static void blur(BlobInfo blobInfo) throws IOException { blob.downloadTo(download); // Construct the command. - List args = new ArrayList(); - args.add("convert"); - args.add(download.toString()); - args.add("-blur"); - args.add("0x8"); Path upload = Paths.get("/tmp/", "blurred-" + fileName); - args.add(upload.toString()); + List args = List.of("convert", download.toString(), "-blur", "0x8", upload.toString()); try { ProcessBuilder pb = new ProcessBuilder(args); Process process = pb.start(); diff --git a/functions/logging/log-helloworld/pom.xml b/functions/logging/log-helloworld/pom.xml index 8c9654021cc..d4dca437ac6 100644 --- a/functions/logging/log-helloworld/pom.xml +++ b/functions/logging/log-helloworld/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -78,7 +78,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.LogHelloWorld @@ -88,7 +88,7 @@ maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 **/*Test.java diff --git a/functions/logging/retrieve-logs/pom.xml b/functions/logging/retrieve-logs/pom.xml index db502d4bc07..5391d5370a0 100644 --- a/functions/logging/retrieve-logs/pom.xml +++ b/functions/logging/retrieve-logs/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -41,7 +41,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -121,7 +121,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.RetrieveLogs @@ -131,7 +131,7 @@ maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 **/*Test.java diff --git a/functions/logging/stackdriver-logging/pom.xml b/functions/logging/stackdriver-logging/pom.xml index 1bac826122a..148d36f905a 100644 --- a/functions/logging/stackdriver-logging/pom.xml +++ b/functions/logging/stackdriver-logging/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -104,7 +104,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.StackdriverLogging @@ -114,7 +114,7 @@ maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 **/*Test.java diff --git a/functions/logging/stackdriver-logging/src/test/java/functions/StackdriverLoggingTest.java b/functions/logging/stackdriver-logging/src/test/java/functions/StackdriverLoggingTest.java index 49b12c2d4e9..1478b454428 100644 --- a/functions/logging/stackdriver-logging/src/test/java/functions/StackdriverLoggingTest.java +++ b/functions/logging/stackdriver-logging/src/test/java/functions/StackdriverLoggingTest.java @@ -22,6 +22,7 @@ import com.google.gson.Gson; import functions.eventpojos.PubSubMessage; import java.io.IOException; +import java.util.Map; import java.util.logging.Logger; import org.junit.BeforeClass; import org.junit.Test; @@ -46,8 +47,11 @@ public static void beforeClass() { @Test public void stackdriverLogging() throws IOException { - PubSubMessage pubsubMessage = gson.fromJson( - "{\"data\":\"ZGF0YQ==\",\"messageId\":\"id\"}", PubSubMessage.class); + String messageJson = gson.toJson(Map.of( + "data", "ZGF0YQ==", + "messageId", "id" + )); + PubSubMessage pubsubMessage = gson.fromJson(messageJson, PubSubMessage.class); new StackdriverLogging().accept(pubsubMessage, null); String logMessage = LOG_HANDLER.getStoredLogRecords().get(0).getMessage(); diff --git a/functions/ocr/ocr-process-image/pom.xml b/functions/ocr/ocr-process-image/pom.xml index 115c661b12d..a158ca33690 100644 --- a/functions/ocr/ocr-process-image/pom.xml +++ b/functions/ocr/ocr-process-image/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -41,7 +41,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -121,7 +121,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.OcrProcessImage @@ -131,7 +131,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/ocr/ocr-save-result/pom.xml b/functions/ocr/ocr-save-result/pom.xml index 46520b0d2ef..4ce006b4d52 100644 --- a/functions/ocr/ocr-save-result/pom.xml +++ b/functions/ocr/ocr-save-result/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -41,7 +41,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -121,7 +121,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.OcrSaveResult @@ -131,7 +131,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/ocr/ocr-translate-text/pom.xml b/functions/ocr/ocr-translate-text/pom.xml index 91fb643fb6d..634ef9ab801 100644 --- a/functions/ocr/ocr-translate-text/pom.xml +++ b/functions/ocr/ocr-translate-text/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -41,7 +41,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -117,7 +117,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.OcrTranslateText @@ -127,7 +127,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/ocr/ocr-translate-text/src/main/java/functions/OcrTranslateText.java b/functions/ocr/ocr-translate-text/src/main/java/functions/OcrTranslateText.java index 5c4b575d8d0..e955e7887cc 100644 --- a/functions/ocr/ocr-translate-text/src/main/java/functions/OcrTranslateText.java +++ b/functions/ocr/ocr-translate-text/src/main/java/functions/OcrTranslateText.java @@ -36,10 +36,11 @@ import java.util.logging.Logger; public class OcrTranslateText implements BackgroundFunction { - // TODO set these environment variables - private static final String PROJECT_ID = System.getenv("GCP_PROJECT"); - private static final String RESULTS_TOPIC_NAME = System.getenv("RESULT_TOPIC"); private static final Logger logger = Logger.getLogger(OcrTranslateText.class.getName()); + + // TODO set these environment variables + private static final String PROJECT_ID = getenv("GCP_PROJECT"); + private static final String RESULTS_TOPIC_NAME = getenv("RESULT_TOPIC"); private static final String LOCATION_NAME = LocationName.of(PROJECT_ID, "global").toString(); private Publisher publisher; @@ -97,5 +98,16 @@ public void accept(PubSubMessage pubSubMessage, Context context) { logger.log(Level.SEVERE, "Error publishing translation save request: " + e.getMessage(), e); } } + + // Avoid ungraceful deployment failures due to unset environment variables. + // If you get this warning you should redeploy with the variable set. + private static String getenv(String name) { + String value = System.getenv(name); + if (value == null) { + logger.warning("Environment variable " + name + " was not set"); + value = "MISSING"; + } + return value; + } } // [END functions_ocr_translate] diff --git a/functions/pubsub/publish-message/pom.xml b/functions/pubsub/publish-message/pom.xml index c1d50b2677a..4567fc936a4 100644 --- a/functions/pubsub/publish-message/pom.xml +++ b/functions/pubsub/publish-message/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -41,7 +41,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -120,7 +120,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.PublishMessage @@ -130,7 +130,7 @@ maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 **/*Test.java diff --git a/functions/pubsub/subscribe-to-topic/pom.xml b/functions/pubsub/subscribe-to-topic/pom.xml index 44fc91d71a6..915b4862e9e 100644 --- a/functions/pubsub/subscribe-to-topic/pom.xml +++ b/functions/pubsub/subscribe-to-topic/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -92,7 +92,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.SubscribeToTopic @@ -102,7 +102,7 @@ maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 **/*Test.java diff --git a/functions/slack/pom.xml b/functions/slack/pom.xml index e1b3613adf3..6148dd80c5e 100644 --- a/functions/slack/pom.xml +++ b/functions/slack/pom.xml @@ -27,11 +27,10 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 - 2.0.7 11 11 UTF-8 @@ -42,7 +41,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -101,24 +100,6 @@ 29.0-jre test - - org.powermock - powermock-core - ${powermock.version} - test - - - org.powermock - powermock-module-junit4 - ${powermock.version} - test - - - org.powermock - powermock-api-mockito2 - ${powermock.version} - test - @@ -153,7 +134,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.SlackSlashCommand @@ -161,7 +142,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 ${skipTests} sponge_log diff --git a/functions/slack/src/main/java/functions/SlackSlashCommand.java b/functions/slack/src/main/java/functions/SlackSlashCommand.java index 640000c7a24..eb101f73268 100644 --- a/functions/slack/src/main/java/functions/SlackSlashCommand.java +++ b/functions/slack/src/main/java/functions/SlackSlashCommand.java @@ -30,25 +30,47 @@ import java.io.IOException; import java.net.HttpURLConnection; import java.security.GeneralSecurityException; -import java.util.HashMap; -import java.util.List; +import java.util.Optional; +import java.util.logging.Logger; import java.util.stream.Collectors; public class SlackSlashCommand implements HttpFunction { // [START functions_slack_setup] - private static final String API_KEY = System.getenv("KG_API_KEY"); - private static final String SLACK_SECRET = System.getenv("SLACK_SECRET"); + private static final Logger logger = Logger.getLogger(SlackSlashCommand.class.getName()); + private static final String API_KEY = getenv("KG_API_KEY"); + private static final String SLACK_SECRET = getenv("SLACK_SECRET"); private static final Gson gson = new Gson(); - private Kgsearch kgClient; - private SlackSignature.Verifier verifier; + private final String apiKey; + private final Kgsearch kgClient; + private final SlackSignature.Verifier verifier; public SlackSlashCommand() throws IOException, GeneralSecurityException { - kgClient = new Kgsearch.Builder( + this(new SlackSignature.Verifier(new SlackSignature.Generator(SLACK_SECRET))); + } + + SlackSlashCommand(SlackSignature.Verifier verifier) throws IOException, GeneralSecurityException { + this(verifier, API_KEY); + } + + SlackSlashCommand(SlackSignature.Verifier verifier, String apiKey) + throws IOException, GeneralSecurityException { + this.verifier = verifier; + this.apiKey = apiKey; + this.kgClient = new Kgsearch.Builder( GoogleNetHttpTransport.newTrustedTransport(), new JacksonFactory(), null).build(); + } - verifier = new SlackSignature.Verifier(new SlackSignature.Generator(SLACK_SECRET)); + // Avoid ungraceful deployment failures due to unset environment variables. + // If you get this warning you should redeploy with the variable set. + private static String getenv(String name) { + String value = System.getenv(name); + if (value == null) { + logger.warning("Environment variable " + name + " was not set"); + value = "MISSING"; + } + return value; } // [END functions_slack_setup] @@ -60,18 +82,13 @@ public SlackSlashCommand() throws IOException, GeneralSecurityException { * @return true if the provided request came from Slack, false otherwise */ boolean isValidSlackWebhook(HttpRequest request, String requestBody) { - // Check for headers - HashMap> headers = new HashMap(request.getHeaders()); - if (!headers.containsKey("X-Slack-Request-Timestamp") - || !headers.containsKey("X-Slack-Signature")) { + Optional maybeTimestamp = request.getFirstHeader("X-Slack-Request-Timestamp"); + Optional maybeSignature = request.getFirstHeader("X-Slack-Signature"); + if (!maybeTimestamp.isPresent() || !maybeSignature.isPresent()) { return false; } - return verifier.isValid( - headers.get("X-Slack-Request-Timestamp").get(0), - requestBody, - headers.get("X-Slack-Signature").get(0), - 1L); + return verifier.isValid(maybeTimestamp.get(), requestBody, maybeSignature.get(), 1L); } // [END functions_verify_webhook] @@ -145,7 +162,7 @@ String formatSlackMessage(JsonObject kgResponse, String query) { JsonObject searchKnowledgeGraph(String query) throws IOException { Kgsearch.Entities.Search kgRequest = kgClient.entities().search(); kgRequest.setQuery(query); - kgRequest.setKey(API_KEY); + kgRequest.setKey(apiKey); return gson.fromJson(kgRequest.execute().toString(), JsonObject.class); } diff --git a/functions/slack/src/test/java/functions/SlackSlashCommandTest.java b/functions/slack/src/test/java/functions/SlackSlashCommandTest.java index 3f2d01da5f5..7ce6e619c13 100644 --- a/functions/slack/src/test/java/functions/SlackSlashCommandTest.java +++ b/functions/slack/src/test/java/functions/SlackSlashCommandTest.java @@ -17,15 +17,18 @@ package functions; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.anyLong; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.powermock.api.mockito.PowerMockito.mock; -import static org.powermock.api.mockito.PowerMockito.when; +import static org.mockito.Mockito.when; import com.github.seratch.jslack.app_backend.SlackSignature; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.cloud.functions.HttpRequest; import com.google.cloud.functions.HttpResponse; +import com.google.gson.Gson; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; @@ -33,20 +36,20 @@ import java.io.StringWriter; import java.net.HttpURLConnection; import java.security.GeneralSecurityException; -import java.util.Arrays; -import java.util.HashMap; import java.util.List; +import java.util.Map; import org.junit.Before; import org.junit.Test; -import org.mockito.ArgumentMatchers; import org.mockito.Mock; -import org.powermock.reflect.Whitebox; +import org.mockito.MockitoAnnotations; public class SlackSlashCommandTest { private BufferedWriter writerOut; private StringWriter responseOut; + private static final Gson gson = new Gson(); + @Mock private HttpRequest request; @Mock private HttpResponse response; @@ -54,36 +57,27 @@ public class SlackSlashCommandTest { @Before public void beforeTest() throws IOException { - request = mock(HttpRequest.class); - when(request.getReader()).thenReturn(new BufferedReader(new StringReader(""))); + MockitoAnnotations.initMocks(this); - response = mock(HttpResponse.class); + when(request.getReader()).thenReturn(new BufferedReader(new StringReader(""))); responseOut = new StringWriter(); writerOut = new BufferedWriter(responseOut); when(response.getWriter()).thenReturn(writerOut); - alwaysValidVerifier = mock(SlackSignature.Verifier.class); - when(alwaysValidVerifier.isValid( - ArgumentMatchers.any(), - ArgumentMatchers.any(), - ArgumentMatchers.any(), - ArgumentMatchers.anyLong()) - ).thenReturn(true); + when(alwaysValidVerifier.isValid(any(), any(), any(), anyLong())).thenReturn(true); // Construct valid header list - HashMap> validHeaders = new HashMap>(); String validSlackSignature = System.getenv("SLACK_TEST_SIGNATURE"); String timestamp = "0"; // start of Unix epoch - validHeaders.put("X-Slack-Signature", Arrays.asList(validSlackSignature)); - validHeaders.put("X-Slack-Request-Timestamp", Arrays.asList(timestamp)); + Map> validHeaders = Map.of( + "X-Slack-Signature", List.of(validSlackSignature), + "X-Slack-Request-Timestamp", List.of(timestamp)); when(request.getHeaders()).thenReturn(validHeaders); - - // Reset knowledge graph API key - Whitebox.setInternalState(SlackSlashCommand.class, "API_KEY", System.getenv("KG_API_KEY")); + when(request.getFirstHeader(any())).thenCallRealMethod(); } @Test @@ -97,7 +91,8 @@ public void onlyAcceptsPostRequestsTest() throws IOException, GeneralSecurityExc @Test public void requiresSlackAuthHeadersTest() throws IOException, GeneralSecurityException { - StringReader requestReadable = new StringReader("{ \"text\": \"foo\" }\n"); + String jsonStr = gson.toJson(Map.of("text", "foo")); + StringReader requestReadable = new StringReader(jsonStr); when(request.getMethod()).thenReturn("POST"); when(request.getReader()).thenReturn(new BufferedReader(requestReadable)); @@ -120,31 +115,30 @@ public void recognizesValidSlackTokenTest() throws IOException, GeneralSecurityE verify(response, times(1)).setStatusCode(HttpURLConnection.HTTP_BAD_REQUEST); } - @Test(expected = GoogleJsonResponseException.class) + @Test public void handlesSearchErrorTest() throws IOException, GeneralSecurityException { - StringReader requestReadable = new StringReader("{ \"text\": \"foo\" }\n"); + String jsonStr = gson.toJson(Map.of("text", "foo")); + StringReader requestReadable = new StringReader(jsonStr); when(request.getReader()).thenReturn(new BufferedReader(requestReadable)); when(request.getMethod()).thenReturn("POST"); - SlackSlashCommand functionInstance = new SlackSlashCommand(); - Whitebox.setInternalState(functionInstance, "verifier", alwaysValidVerifier); - Whitebox.setInternalState(SlackSlashCommand.class, "API_KEY", "gibberish"); + SlackSlashCommand functionInstance = new SlackSlashCommand(alwaysValidVerifier, "gibberish"); // Should throw a GoogleJsonResponseException (due to invalid API key) - functionInstance.service(request, response); + assertThrows( + GoogleJsonResponseException.class, () -> functionInstance.service(request, response)); } @Test public void handlesEmptyKgResultsTest() throws IOException, GeneralSecurityException { - StringReader requestReadable = new StringReader("{ \"text\": \"asdfjkl13579\" }\n"); + String jsonStr = gson.toJson(Map.of("text", "asdfjkl13579")); + StringReader requestReadable = new StringReader(jsonStr); when(request.getReader()).thenReturn(new BufferedReader(requestReadable)); when(request.getMethod()).thenReturn("POST"); - SlackSlashCommand functionInstance = new SlackSlashCommand(); - Whitebox.setInternalState(functionInstance, "verifier", alwaysValidVerifier); - + SlackSlashCommand functionInstance = new SlackSlashCommand(alwaysValidVerifier); functionInstance.service(request, response); @@ -154,14 +148,13 @@ public void handlesEmptyKgResultsTest() throws IOException, GeneralSecurityExcep @Test public void handlesPopulatedKgResultsTest() throws IOException, GeneralSecurityException { - StringReader requestReadable = new StringReader("{ \"text\": \"lion\" }\n"); + String jsonStr = gson.toJson(Map.of("text", "lion")); + StringReader requestReadable = new StringReader(jsonStr); when(request.getReader()).thenReturn(new BufferedReader(requestReadable)); when(request.getMethod()).thenReturn("POST"); - SlackSlashCommand functionInstance = new SlackSlashCommand(); - Whitebox.setInternalState(functionInstance, "verifier", alwaysValidVerifier); - + SlackSlashCommand functionInstance = new SlackSlashCommand(alwaysValidVerifier); functionInstance.service(request, response); diff --git a/functions/spanner/pom.xml b/functions/spanner/pom.xml index ba644099d71..1a8627642b7 100644 --- a/functions/spanner/pom.xml +++ b/functions/spanner/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -41,7 +41,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -114,7 +114,7 @@ --> com.google.cloud.functions function-maven-plugin - 0.9.2 + 0.9.3 functions.HelloSpanner @@ -122,7 +122,7 @@ org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 **/*Test.java diff --git a/healthcare/v1/pom.xml b/healthcare/v1/pom.xml index 5355dbc6c2b..dedc223ff60 100644 --- a/healthcare/v1/pom.xml +++ b/healthcare/v1/pom.xml @@ -42,7 +42,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -64,7 +64,7 @@ com.google.cloud libraries-bom - 5.4.0 + 7.0.2 pom import @@ -75,13 +75,18 @@ com.google.apis google-api-services-healthcare - v1-rev20200501-1.30.9 + v1-rev20200605-1.30.9 com.google.api-client google-api-client 1.30.9 + + com.google.auth + google-auth-library-oauth2-http + 0.21.0 + com.google.cloud google-cloud-core diff --git a/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetCreate.java b/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetCreate.java index 26c69e75fcd..f8226352f31 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetCreate.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetCreate.java @@ -17,8 +17,6 @@ package snippets.healthcare.datasets; // [START healthcare_create_dataset] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -28,6 +26,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.Dataset; import com.google.api.services.healthcare.v1.model.Operation; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -80,14 +80,14 @@ public static void datasetCreate(String projectId, String regionId, String datas private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetDeIdentify.java b/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetDeIdentify.java index d76cc8044b2..1f244c5097c 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetDeIdentify.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetDeIdentify.java @@ -17,8 +17,6 @@ package snippets.healthcare.datasets; // [START healthcare_dicom_keeplist_deidentify_dataset] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -31,6 +29,8 @@ import com.google.api.services.healthcare.v1.model.DicomConfig; import com.google.api.services.healthcare.v1.model.Operation; import com.google.api.services.healthcare.v1.model.TagFilterList; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Arrays; import java.util.Collections; @@ -89,14 +89,14 @@ public static void datasetDeIdentify(String srcDatasetName, String destDatasetNa private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetDelete.java b/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetDelete.java index d64675c4d98..0b75cc9aceb 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetDelete.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetDelete.java @@ -17,8 +17,6 @@ package snippets.healthcare.datasets; // [START healthcare_delete_dataset] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -26,6 +24,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -52,14 +52,14 @@ public static void datasetDelete(String datasetName) throws IOException { private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetGet.java b/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetGet.java index b8e6edf72df..e124f232b7f 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetGet.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetGet.java @@ -17,8 +17,6 @@ package snippets.healthcare.datasets; // [START healthcare_get_dataset] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -27,6 +25,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.Dataset; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -53,14 +53,14 @@ public static void datasetGet(String datasetName) throws IOException { private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetGetIamPolicy.java b/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetGetIamPolicy.java index c0f1e534cee..0159be977a6 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetGetIamPolicy.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetGetIamPolicy.java @@ -17,8 +17,6 @@ package snippets.healthcare.datasets; // [START healthcare_dataset_get_iam_policy] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -27,6 +25,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.Policy; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -54,14 +54,14 @@ public static void datasetGetIamPolicy(String datasetName) throws IOException { private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetList.java b/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetList.java index 1e633971117..782cce407f2 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetList.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetList.java @@ -17,8 +17,6 @@ package snippets.healthcare.datasets; // [START healthcare_list_datasets] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -28,6 +26,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.Dataset; import com.google.api.services.healthcare.v1.model.ListDatasetsResponse; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -77,14 +77,14 @@ public static void datasetList(String projectId, String regionId) throws IOExcep private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetPatch.java b/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetPatch.java index eb6ae45b5ed..c399532dd2d 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetPatch.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetPatch.java @@ -17,8 +17,6 @@ package snippets.healthcare.datasets; // [START healthcare_patch_dataset] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -27,6 +25,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.Dataset; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -67,14 +67,14 @@ public static void datasetPatch(String datasetName) throws IOException { private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetSetIamPolicy.java b/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetSetIamPolicy.java index abccc78a551..0a7e966866f 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetSetIamPolicy.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/datasets/DatasetSetIamPolicy.java @@ -17,8 +17,6 @@ package snippets.healthcare.datasets; // [START healthcare_dataset_set_iam_policy] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -29,6 +27,8 @@ import com.google.api.services.healthcare.v1.model.Binding; import com.google.api.services.healthcare.v1.model.Policy; import com.google.api.services.healthcare.v1.model.SetIamPolicyRequest; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Arrays; import java.util.Collections; @@ -67,14 +67,14 @@ public static void datasetSetIamPolicy(String datasetName) throws IOException { private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreCreate.java b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreCreate.java index e9f06f62aa1..a5d0d365f1f 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreCreate.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreCreate.java @@ -17,8 +17,6 @@ package snippets.healthcare.dicom; // [START healthcare_create_dicom_store] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -27,6 +25,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.DicomStores; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.DicomStore; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; import java.util.HashMap; @@ -68,14 +68,14 @@ public static void dicomStoreCreate(String datasetName, String dicomStoreId) thr private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreDelete.java b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreDelete.java index 6f0431ee01c..fc937c985b9 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreDelete.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreDelete.java @@ -17,8 +17,6 @@ package snippets.healthcare.dicom; // [START healthcare_delete_dicom_store] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -26,6 +24,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.DicomStores; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -54,14 +54,14 @@ public static void deleteDicomStore(String dicomStoreName) throws IOException { private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreExport.java b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreExport.java index 69071f721aa..ed1a63f984b 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreExport.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreExport.java @@ -17,8 +17,6 @@ package snippets.healthcare.dicom; // [START healthcare_export_dicom_instance_gcs] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -29,6 +27,8 @@ import com.google.api.services.healthcare.v1.model.ExportDicomDataRequest; import com.google.api.services.healthcare.v1.model.GoogleCloudHealthcareV1DicomGcsDestination; import com.google.api.services.healthcare.v1.model.Operation; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -86,14 +86,14 @@ public static void dicomStoreExport(String dicomStoreName, String gcsUri) throws private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreGet.java b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreGet.java index 74a39e898fe..6f0bda847e4 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreGet.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreGet.java @@ -17,8 +17,6 @@ package snippets.healthcare.dicom; // [START healthcare_get_dicom_store] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -27,6 +25,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.DicomStores; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.DicomStore; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -55,14 +55,14 @@ public static void dicomeStoreGet(String dicomStoreName) throws IOException { private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreGetIamPolicy.java b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreGetIamPolicy.java index 9b363f58f3b..566a86d5fce 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreGetIamPolicy.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreGetIamPolicy.java @@ -17,8 +17,6 @@ package snippets.healthcare.dicom; // [START healthcare_dicom_store_get_iam_policy] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -27,6 +25,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.DicomStores; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.Policy; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -55,14 +55,14 @@ public static void dicomStoreGetIamPolicy(String dicomStoreName) throws IOExcept private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreImport.java b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreImport.java index 84de170fe96..3458e431ff9 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreImport.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreImport.java @@ -17,8 +17,6 @@ package snippets.healthcare.dicom; // [START healthcare_import_dicom_instance] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -29,6 +27,8 @@ import com.google.api.services.healthcare.v1.model.GoogleCloudHealthcareV1DicomGcsSource; import com.google.api.services.healthcare.v1.model.ImportDicomDataRequest; import com.google.api.services.healthcare.v1.model.Operation; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -85,14 +85,14 @@ public static void dicomStoreImport(String dicomStoreName, String gcsUri) throws private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreList.java b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreList.java index 468a03c46c1..c0e17f548e2 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreList.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreList.java @@ -17,8 +17,6 @@ package snippets.healthcare.dicom; // [START healthcare_list_dicom_stores] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -28,6 +26,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.DicomStore; import com.google.api.services.healthcare.v1.model.ListDicomStoresResponse; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -78,14 +78,14 @@ public static void dicomStoreList(String datasetName) throws IOException { private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStorePatch.java b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStorePatch.java index 9a3ed372a9c..38fd8856421 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStorePatch.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStorePatch.java @@ -17,8 +17,6 @@ package snippets.healthcare.dicom; // [START healthcare_patch_dicom_store] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -28,6 +26,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.DicomStore; import com.google.api.services.healthcare.v1.model.NotificationConfig; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -72,14 +72,14 @@ public static void patchDicomStore(String dicomStoreName, String pubsubTopic) th private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreSetIamPolicy.java b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreSetIamPolicy.java index c014b606661..5fb1f84e8a8 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreSetIamPolicy.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomStoreSetIamPolicy.java @@ -17,8 +17,6 @@ package snippets.healthcare.dicom; // [START healthcare_dicom_store_set_iam_policy] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -29,6 +27,8 @@ import com.google.api.services.healthcare.v1.model.Binding; import com.google.api.services.healthcare.v1.model.Policy; import com.google.api.services.healthcare.v1.model.SetIamPolicyRequest; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Arrays; import java.util.Collections; @@ -73,14 +73,14 @@ public static void dicomStoreSetIamPolicy(String dicomStoreName) throws IOExcept private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebDeleteStudy.java b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebDeleteStudy.java index 3f65e951b92..7491242d0dc 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebDeleteStudy.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebDeleteStudy.java @@ -17,8 +17,6 @@ package snippets.healthcare.dicom; // [START healthcare_dicomweb_delete_study] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -26,6 +24,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.DicomStores.Studies; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -61,14 +61,14 @@ public static void dicomWebDeleteStudy(String dicomStoreName, String studyId) th private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebRetrieveInstance.java b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebRetrieveInstance.java index a924fae55f3..3edbab2a42c 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebRetrieveInstance.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebRetrieveInstance.java @@ -17,7 +17,6 @@ package snippets.healthcare.dicom; // [START healthcare_dicomweb_retrieve_instance] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.HttpResponse; @@ -27,14 +26,13 @@ import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.DicomStores.Studies.Series.Instances; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; -import java.io.BufferedReader; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStreamReader; import java.io.OutputStream; import java.util.Collections; -import java.util.stream.Collectors; public class DicomWebRetrieveInstance { private static final String DICOM_NAME = "projects/%s/locations/%s/datasets/%s/dicomStores/%s"; @@ -79,18 +77,16 @@ public static void dicomWebRetrieveInstance(String dicomStoreName, String dicomW if (!response.isSuccessStatusCode()) { System.err.print( - String.format( - "Exception retrieving DICOM instance: %s\n", response.getStatusMessage())); + String.format("Exception retrieving DICOM instance: %s\n", response.getStatusMessage())); throw new RuntimeException(); } - } private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); HttpHeaders headers = new HttpHeaders(); @@ -102,8 +98,7 @@ private static CloudHealthcare createClient() throws IOException { // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); - request.setHeaders(headers); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; @@ -115,4 +110,3 @@ private static CloudHealthcare createClient() throws IOException { } } // [END healthcare_dicomweb_retrieve_instance] - diff --git a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebRetrieveRendered.java b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebRetrieveRendered.java index 71df54edf2a..ea802325be5 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebRetrieveRendered.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebRetrieveRendered.java @@ -17,7 +17,6 @@ package snippets.healthcare.dicom; // [START healthcare_dicomweb_retrieve_rendered] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.HttpResponse; @@ -27,14 +26,13 @@ import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.DicomStores.Studies.Series.Instances; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; -import java.io.BufferedReader; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStreamReader; import java.io.OutputStream; import java.util.Collections; -import java.util.stream.Collectors; public class DicomWebRetrieveRendered { private static final String DICOM_NAME = "projects/%s/locations/%s/datasets/%s/dicomStores/%s"; @@ -83,14 +81,13 @@ public static void dicomWebRetrieveRendered(String dicomStoreName, String dicomW "Exception retrieving DICOM rendered image: %s\n", response.getStatusMessage())); throw new RuntimeException(); } - } private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); HttpHeaders headers = new HttpHeaders(); @@ -100,8 +97,7 @@ private static CloudHealthcare createClient() throws IOException { // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); - request.setHeaders(headers); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; @@ -113,4 +109,3 @@ private static CloudHealthcare createClient() throws IOException { } } // [END healthcare_dicomweb_retrieve_rendered] - diff --git a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebRetrieveStudy.java b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebRetrieveStudy.java index 39d6d9931b3..006359b9804 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebRetrieveStudy.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebRetrieveStudy.java @@ -17,7 +17,6 @@ package snippets.healthcare.dicom; // [START healthcare_dicomweb_retrieve_study] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.HttpResponse; @@ -27,14 +26,13 @@ import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.DicomStores.Studies; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; -import java.io.BufferedReader; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStreamReader; import java.io.OutputStream; import java.util.Collections; -import java.util.stream.Collectors; public class DicomWebRetrieveStudy { private static final String DICOM_NAME = "projects/%s/locations/%s/datasets/%s/dicomStores/%s"; @@ -78,18 +76,16 @@ public static void dicomWebRetrieveStudy(String dicomStoreName, String studyId) if (!response.isSuccessStatusCode()) { System.err.print( - String.format( - "Exception retrieving DICOM study: %s\n", response.getStatusMessage())); + String.format("Exception retrieving DICOM study: %s\n", response.getStatusMessage())); throw new RuntimeException(); } - } private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); HttpHeaders headers = new HttpHeaders(); @@ -104,8 +100,7 @@ private static CloudHealthcare createClient() throws IOException { // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); - request.setHeaders(headers); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; @@ -117,4 +112,3 @@ private static CloudHealthcare createClient() throws IOException { } } // [END healthcare_dicomweb_retrieve_study] - diff --git a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebSearchForInstances.java b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebSearchForInstances.java index e050a0e41e8..3dbc89b4d3e 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebSearchForInstances.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebSearchForInstances.java @@ -17,7 +17,6 @@ package snippets.healthcare.dicom; // [START healthcare_dicomweb_search_instances] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.HttpResponse; import com.google.api.client.http.javanet.NetHttpTransport; @@ -26,6 +25,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.DicomStores; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -59,14 +60,14 @@ public static void dicomWebSearchForInstances(String dicomStoreName) throws IOEx private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebSearchStudies.java b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebSearchStudies.java index f4839e0cf4f..e63d172416e 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebSearchStudies.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebSearchStudies.java @@ -17,7 +17,6 @@ package snippets.healthcare.dicom; // [START healthcare_dicomweb_search_studies] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.HttpResponse; import com.google.api.client.http.javanet.NetHttpTransport; @@ -26,6 +25,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.DicomStores; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -62,14 +63,14 @@ public static void dicomWebSearchStudies(String dicomStoreName) throws IOExcepti private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebStoreInstance.java b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebStoreInstance.java index aef52d4ed54..8cb6d63a685 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebStoreInstance.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/dicom/DicomWebStoreInstance.java @@ -17,13 +17,14 @@ package snippets.healthcare.dicom; // [START healthcare_dicomweb_store_instance] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; @@ -56,27 +57,26 @@ public static void dicomWebStoreInstance(String dicomStoreName, String filePath) CloudHealthcare client = createClient(); HttpClient httpClient = HttpClients.createDefault(); - String uri = String.format( - "%sv1/%s/dicomWeb/studies", client.getRootUrl(), dicomStoreName); - URIBuilder uriBuilder = new URIBuilder(uri) - .setParameter("access_token", getAccessToken()); + String uri = String.format("%sv1/%s/dicomWeb/studies", client.getRootUrl(), dicomStoreName); + URIBuilder uriBuilder = new URIBuilder(uri).setParameter("access_token", getAccessToken()); // Load the data from file representing the study. File f = new File(filePath); byte[] dicomBytes = Files.readAllBytes(Paths.get(filePath)); ByteArrayEntity requestEntity = new ByteArrayEntity(dicomBytes); - HttpUriRequest request = RequestBuilder - .post(uriBuilder.build()) - .setEntity(requestEntity) - .addHeader("Content-Type","application/dicom") - .build(); + HttpUriRequest request = + RequestBuilder.post(uriBuilder.build()) + .setEntity(requestEntity) + .addHeader("Content-Type", "application/dicom") + .build(); // Execute the request and process the results. HttpResponse response = httpClient.execute(request); HttpEntity responseEntity = response.getEntity(); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { - System.err.print(String.format( - "Exception storing DICOM instance: %s\n", response.getStatusLine().toString())); + System.err.print( + String.format( + "Exception storing DICOM instance: %s\n", response.getStatusLine().toString())); responseEntity.writeTo(System.err); throw new RuntimeException(); } @@ -87,14 +87,14 @@ public static void dicomWebStoreInstance(String dicomStoreName, String filePath) private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; @@ -106,11 +106,11 @@ private static CloudHealthcare createClient() throws IOException { } private static String getAccessToken() throws IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); - credential.refreshToken(); - return credential.getAccessToken(); + + return credential.refreshAccessToken().getTokenValue(); } } // [END healthcare_dicomweb_store_instance] diff --git a/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreCreate.java b/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreCreate.java index bdd9b29ef20..a32b4e6db4d 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreCreate.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreCreate.java @@ -17,8 +17,6 @@ package snippets.healthcare.fhir; // [START healthcare_create_fhir_store] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -27,6 +25,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.FhirStores; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.FhirStore; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; import java.util.HashMap; @@ -70,14 +70,14 @@ public static void fhirStoreCreate(String datasetName, String fhirStoreId) throw private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreDelete.java b/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreDelete.java index 54f60b3bb86..45a50a40c02 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreDelete.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreDelete.java @@ -17,8 +17,6 @@ package snippets.healthcare.fhir; // [START healthcare_delete_fhir_store] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -26,6 +24,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.FhirStores; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -54,14 +54,14 @@ public static void fhirStoreDelete(String fhirStoreName) throws IOException { private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreExecuteBundle.java b/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreExecuteBundle.java index 53ef44f9e39..0201af252d6 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreExecuteBundle.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreExecuteBundle.java @@ -17,13 +17,14 @@ package snippets.healthcare.fhir; // [START healthcare_fhir_execute_bundle] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.net.URISyntaxException; import java.util.Collections; @@ -53,25 +54,25 @@ public static void fhirStoreExecuteBundle(String fhirStoreName, String data) CloudHealthcare client = createClient(); HttpClient httpClient = HttpClients.createDefault(); String baseUri = String.format("%sv1/%s/fhir", client.getRootUrl(), fhirStoreName); - URIBuilder uriBuilder = new URIBuilder(baseUri) - .setParameter("access_token", getAccessToken()); + URIBuilder uriBuilder = new URIBuilder(baseUri).setParameter("access_token", getAccessToken()); StringEntity requestEntity = new StringEntity(data); - HttpUriRequest request = RequestBuilder - .post() - .setUri(uriBuilder.build()) - .setEntity(requestEntity) - .addHeader("Content-Type", "application/fhir+json") - .addHeader("Accept-Charset", "utf-8") - .addHeader("Accept", "application/fhir+json; charset=utf-8") - .build(); + HttpUriRequest request = + RequestBuilder.post() + .setUri(uriBuilder.build()) + .setEntity(requestEntity) + .addHeader("Content-Type", "application/fhir+json") + .addHeader("Accept-Charset", "utf-8") + .addHeader("Accept", "application/fhir+json; charset=utf-8") + .build(); // Execute the request and process the results. HttpResponse response = httpClient.execute(request); HttpEntity responseEntity = response.getEntity(); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { - System.err.print(String.format( - "Exception executing FHIR bundle: %s\n", response.getStatusLine().toString())); + System.err.print( + String.format( + "Exception executing FHIR bundle: %s\n", response.getStatusLine().toString())); responseEntity.writeTo(System.err); throw new RuntimeException(); } @@ -82,14 +83,14 @@ public static void fhirStoreExecuteBundle(String fhirStoreName, String data) private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; @@ -101,11 +102,11 @@ private static CloudHealthcare createClient() throws IOException { } private static String getAccessToken() throws IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); - credential.refreshToken(); - return credential.getAccessToken(); + + return credential.refreshAccessToken().getTokenValue(); } } // [END healthcare_fhir_execute_bundle] diff --git a/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreExport.java b/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreExport.java index eaf1faffae2..97492d654f7 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreExport.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreExport.java @@ -17,8 +17,6 @@ package snippets.healthcare.fhir; // [START healthcare_export_fhir_resources_gcs] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -29,6 +27,8 @@ import com.google.api.services.healthcare.v1.model.ExportResourcesRequest; import com.google.api.services.healthcare.v1.model.GoogleCloudHealthcareV1FhirGcsDestination; import com.google.api.services.healthcare.v1.model.Operation; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -81,14 +81,14 @@ public static void fhirStoreExport(String fhirStoreName, String gcsUri) throws I private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreGet.java b/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreGet.java index 45d5e59254e..d8286ae6ddc 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreGet.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreGet.java @@ -17,8 +17,6 @@ package snippets.healthcare.fhir; // [START healthcare_get_fhir_store] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -27,6 +25,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.FhirStores; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.FhirStore; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -55,14 +55,14 @@ public static void fhirStoreGet(String fhirStoreName) throws IOException { private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreGetIamPolicy.java b/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreGetIamPolicy.java index ff4ab3fa569..efa97dcd8d5 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreGetIamPolicy.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreGetIamPolicy.java @@ -17,8 +17,6 @@ package snippets.healthcare.fhir; // [START healthcare_fhir_store_get_iam_policy] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -27,6 +25,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.FhirStores; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.Policy; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -55,14 +55,14 @@ public static void fhirStoreGetIamPolicy(String fhirStoreName) throws IOExceptio private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreGetMetadata.java b/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreGetMetadata.java index cba58f8abb3..b6c7707c93e 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreGetMetadata.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreGetMetadata.java @@ -17,7 +17,6 @@ package snippets.healthcare.fhir; // [START healthcare_get_metadata] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -26,12 +25,13 @@ import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.FhirStores.Fhir.Capabilities; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.HttpBody; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; public class FhirStoreGetMetadata { - private static final String FHIR_NAME = - "projects/%s/locations/%s/datasets/%s/fhirStores/%s"; + private static final String FHIR_NAME = "projects/%s/locations/%s/datasets/%s/fhirStores/%s"; private static final JsonFactory JSON_FACTORY = new JacksonFactory(); private static final NetHttpTransport HTTP_TRANSPORT = new NetHttpTransport(); @@ -55,14 +55,14 @@ public static void fhirStoreGetMetadata(String fhirStoreName) throws IOException private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreImport.java b/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreImport.java index 9bb3575d26e..d4e812137c1 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreImport.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreImport.java @@ -17,8 +17,6 @@ package snippets.healthcare.fhir; // [START healthcare_import_fhir_resources] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -29,6 +27,8 @@ import com.google.api.services.healthcare.v1.model.GoogleCloudHealthcareV1FhirGcsSource; import com.google.api.services.healthcare.v1.model.ImportResourcesRequest; import com.google.api.services.healthcare.v1.model.Operation; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -85,14 +85,14 @@ public static void fhirStoreImport(String fhirStoreName, String gcsUri) throws I private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreList.java b/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreList.java index ec70ef42fde..62f21dfa553 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreList.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreList.java @@ -17,8 +17,6 @@ package snippets.healthcare.fhir; // [START healthcare_list_fhir_stores] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -28,6 +26,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.FhirStore; import com.google.api.services.healthcare.v1.model.ListFhirStoresResponse; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -78,14 +78,14 @@ public static void fhirStoreList(String datasetName) throws IOException { private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStorePatch.java b/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStorePatch.java index ff379400558..3ef8f6d1e49 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStorePatch.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStorePatch.java @@ -17,8 +17,6 @@ package snippets.healthcare.fhir; // [START healthcare_patch_fhir_store] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -28,6 +26,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.FhirStore; import com.google.api.services.healthcare.v1.model.NotificationConfig; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -72,14 +72,14 @@ public static void fhirStorePatch(String fhirStoreName, String pubsubTopic) thro private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreSetIamPolicy.java b/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreSetIamPolicy.java index 2ab09db85f6..4ec2b8d9211 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreSetIamPolicy.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/fhir/FhirStoreSetIamPolicy.java @@ -17,8 +17,6 @@ package snippets.healthcare.fhir; // [START healthcare_fhir_store_set_iam_policy] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -29,6 +27,8 @@ import com.google.api.services.healthcare.v1.model.Binding; import com.google.api.services.healthcare.v1.model.Policy; import com.google.api.services.healthcare.v1.model.SetIamPolicyRequest; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Arrays; import java.util.Collections; @@ -73,14 +73,14 @@ public static void fhirStoreSetIamPolicy(String fhirStoreName) throws IOExceptio private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceCreate.java b/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceCreate.java index acc1bad37b0..615eeb2b34e 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceCreate.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceCreate.java @@ -17,13 +17,14 @@ package snippets.healthcare.fhir.resources; // [START healthcare_create_resource] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.net.URISyntaxException; import java.util.Collections; @@ -52,28 +53,27 @@ public static void fhirResourceCreate(String fhirStoreName, String resourceType) // Initialize the client, which will be used to interact with the service. CloudHealthcare client = createClient(); HttpClient httpClient = HttpClients.createDefault(); - String uri = String.format( - "%sv1/%s/fhir/%s", client.getRootUrl(), fhirStoreName, resourceType); - URIBuilder uriBuilder = new URIBuilder(uri) - .setParameter("access_token", getAccessToken()); - StringEntity requestEntity = new StringEntity( - "{\"resourceType\": \"" + resourceType + "\", \"language\": \"en\"}"); + String uri = String.format("%sv1/%s/fhir/%s", client.getRootUrl(), fhirStoreName, resourceType); + URIBuilder uriBuilder = new URIBuilder(uri).setParameter("access_token", getAccessToken()); + StringEntity requestEntity = + new StringEntity("{\"resourceType\": \"" + resourceType + "\", \"language\": \"en\"}"); - HttpUriRequest request = RequestBuilder - .post() - .setUri(uriBuilder.build()) - .setEntity(requestEntity) - .addHeader("Content-Type", "application/fhir+json") - .addHeader("Accept-Charset", "utf-8") - .addHeader("Accept", "application/fhir+json; charset=utf-8") - .build(); + HttpUriRequest request = + RequestBuilder.post() + .setUri(uriBuilder.build()) + .setEntity(requestEntity) + .addHeader("Content-Type", "application/fhir+json") + .addHeader("Accept-Charset", "utf-8") + .addHeader("Accept", "application/fhir+json; charset=utf-8") + .build(); // Execute the request and process the results. HttpResponse response = httpClient.execute(request); HttpEntity responseEntity = response.getEntity(); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) { - System.err.print(String.format( - "Exception creating FHIR resource: %s\n", response.getStatusLine().toString())); + System.err.print( + String.format( + "Exception creating FHIR resource: %s\n", response.getStatusLine().toString())); responseEntity.writeTo(System.err); throw new RuntimeException(); } @@ -84,14 +84,14 @@ public static void fhirResourceCreate(String fhirStoreName, String resourceType) private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; @@ -103,11 +103,11 @@ private static CloudHealthcare createClient() throws IOException { } private static String getAccessToken() throws IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); - credential.refreshToken(); - return credential.getAccessToken(); + + return credential.refreshAccessToken().getTokenValue(); } } // [END healthcare_create_resource] diff --git a/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceDelete.java b/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceDelete.java index 64c7949ac7e..55cb20f21c1 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceDelete.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceDelete.java @@ -17,13 +17,14 @@ package snippets.healthcare.fhir.resources; // [START healthcare_delete_resource] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.net.URISyntaxException; import java.util.Collections; @@ -53,25 +54,28 @@ public static void fhirResourceDelete(String resourceName) CloudHealthcare client = createClient(); HttpClient httpClient = HttpClients.createDefault(); - String uri = String.format( - "%sv1/%s", client.getRootUrl(), resourceName); - URIBuilder uriBuilder = new URIBuilder(uri) - .setParameter("access_token", getAccessToken()); + String uri = String.format("%sv1/%s", client.getRootUrl(), resourceName); + URIBuilder uriBuilder = new URIBuilder(uri).setParameter("access_token", getAccessToken()); - HttpUriRequest request = RequestBuilder - .delete() - .setUri(uriBuilder.build()) - .addHeader("Content-Type", "application/fhir+json") - .addHeader("Accept-Charset", "utf-8") - .addHeader("Accept", "application/fhir+json; charset=utf-8") - .build(); + HttpUriRequest request = + RequestBuilder.delete() + .setUri(uriBuilder.build()) + .addHeader("Content-Type", "application/fhir+json") + .addHeader("Accept-Charset", "utf-8") + .addHeader("Accept", "application/fhir+json; charset=utf-8") + .build(); // Execute the request and process the results. + // Regardless of whether the operation succeeds or + // fails, the server returns a 200 OK HTTP status code. To check that the + // resource was successfully deleted, search for or get the resource and + // see if it exists. HttpResponse response = httpClient.execute(request); HttpEntity responseEntity = response.getEntity(); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { - String errorMessage = String.format( - "Exception deleting FHIR resource: %s\n", response.getStatusLine().toString()); + String errorMessage = + String.format( + "Exception deleting FHIR resource: %s\n", response.getStatusLine().toString()); System.err.print(errorMessage); responseEntity.writeTo(System.err); throw new RuntimeException(errorMessage); @@ -83,14 +87,14 @@ public static void fhirResourceDelete(String resourceName) private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; @@ -102,11 +106,11 @@ private static CloudHealthcare createClient() throws IOException { } private static String getAccessToken() throws IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); - credential.refreshToken(); - return credential.getAccessToken(); + + return credential.refreshAccessToken().getTokenValue(); } } // [END healthcare_delete_resource] diff --git a/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceDeletePurge.java b/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceDeletePurge.java index b0739ca505a..88fb4b95a04 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceDeletePurge.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceDeletePurge.java @@ -17,13 +17,14 @@ package snippets.healthcare.fhir.resources; // [START healthcare_delete_resource_purge] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.net.URISyntaxException; import java.util.Collections; @@ -52,25 +53,24 @@ public static void fhirResourceDeletePurge(String resourceName) CloudHealthcare client = createClient(); HttpClient httpClient = HttpClients.createDefault(); - String uri = String.format( - "%sv1/%s/$purge", client.getRootUrl(), resourceName); - URIBuilder uriBuilder = new URIBuilder(uri) - .setParameter("access_token", getAccessToken()); + String uri = String.format("%sv1/%s/$purge", client.getRootUrl(), resourceName); + URIBuilder uriBuilder = new URIBuilder(uri).setParameter("access_token", getAccessToken()); - HttpUriRequest request = RequestBuilder - .delete() - .setUri(uriBuilder.build()) - .addHeader("Content-Type", "application/fhir+json") - .addHeader("Accept-Charset", "utf-8") - .addHeader("Accept", "application/fhir+json; charset=utf-8") - .build(); + HttpUriRequest request = + RequestBuilder.delete() + .setUri(uriBuilder.build()) + .addHeader("Content-Type", "application/fhir+json") + .addHeader("Accept-Charset", "utf-8") + .addHeader("Accept", "application/fhir+json; charset=utf-8") + .build(); // Execute the request and process the results. HttpResponse response = httpClient.execute(request); HttpEntity responseEntity = response.getEntity(); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { - String errorMessage = String.format( - "Exception purging FHIR resource: %s\n", response.getStatusLine().toString()); + String errorMessage = + String.format( + "Exception purging FHIR resource: %s\n", response.getStatusLine().toString()); System.err.print(errorMessage); responseEntity.writeTo(System.err); throw new RuntimeException(errorMessage); @@ -82,14 +82,14 @@ public static void fhirResourceDeletePurge(String resourceName) private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; @@ -101,11 +101,11 @@ private static CloudHealthcare createClient() throws IOException { } private static String getAccessToken() throws IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); - credential.refreshToken(); - return credential.getAccessToken(); + + return credential.refreshAccessToken().getTokenValue(); } } // [END healthcare_delete_resource_purge] diff --git a/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceGet.java b/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceGet.java index ff4c2a465f9..3e3da1b03d1 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceGet.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceGet.java @@ -17,13 +17,14 @@ package snippets.healthcare.fhir.resources; // [START healthcare_get_resource] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.net.URISyntaxException; import java.util.Collections; @@ -51,22 +52,18 @@ public static void fhirResourceGet(String resourceName) throws IOException, URIS // Initialize the client, which will be used to interact with the service. CloudHealthcare client = createClient(); HttpClient httpClient = HttpClients.createDefault(); - String uri = String.format( - "%sv1/%s", client.getRootUrl(), resourceName); - URIBuilder uriBuilder = new URIBuilder(uri) - .setParameter("access_token", getAccessToken()); + String uri = String.format("%sv1/%s", client.getRootUrl(), resourceName); + URIBuilder uriBuilder = new URIBuilder(uri).setParameter("access_token", getAccessToken()); - HttpUriRequest request = RequestBuilder - .get() - .setUri(uriBuilder.build()) - .build(); + HttpUriRequest request = RequestBuilder.get().setUri(uriBuilder.build()).build(); // Execute the request and process the results. HttpResponse response = httpClient.execute(request); HttpEntity responseEntity = response.getEntity(); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { - String errorMessage = String.format( - "Exception retrieving FHIR resource: %s\n", response.getStatusLine().toString()); + String errorMessage = + String.format( + "Exception retrieving FHIR resource: %s\n", response.getStatusLine().toString()); System.err.print(errorMessage); responseEntity.writeTo(System.err); throw new RuntimeException(errorMessage); @@ -78,14 +75,14 @@ public static void fhirResourceGet(String resourceName) throws IOException, URIS private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; @@ -97,11 +94,11 @@ private static CloudHealthcare createClient() throws IOException { } private static String getAccessToken() throws IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); - credential.refreshToken(); - return credential.getAccessToken(); + + return credential.refreshAccessToken().getTokenValue(); } } // [END healthcare_get_resource] diff --git a/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceGetHistory.java b/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceGetHistory.java index 251c619220c..7778f263071 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceGetHistory.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceGetHistory.java @@ -17,13 +17,14 @@ package snippets.healthcare.fhir.resources; // [START healthcare_get_resource_history] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.net.URISyntaxException; import java.util.Collections; @@ -54,25 +55,24 @@ public static void fhirResourceGetHistory(String resourceName, String versionId) CloudHealthcare client = createClient(); HttpClient httpClient = HttpClients.createDefault(); - String uri = String.format( - "%sv1/%s/_history/%s", client.getRootUrl(), resourceName, versionId); - URIBuilder uriBuilder = new URIBuilder(uri) - .setParameter("access_token", getAccessToken()); + String uri = String.format("%sv1/%s/_history/%s", client.getRootUrl(), resourceName, versionId); + URIBuilder uriBuilder = new URIBuilder(uri).setParameter("access_token", getAccessToken()); - HttpUriRequest request = RequestBuilder - .get() - .setUri(uriBuilder.build()) - .addHeader("Content-Type", "application/fhir+json") - .addHeader("Accept-Charset", "utf-8") - .addHeader("Accept", "application/fhir+json; charset=utf-8") - .build(); + HttpUriRequest request = + RequestBuilder.get() + .setUri(uriBuilder.build()) + .addHeader("Content-Type", "application/fhir+json") + .addHeader("Accept-Charset", "utf-8") + .addHeader("Accept", "application/fhir+json; charset=utf-8") + .build(); // Execute the request and process the results. HttpResponse response = httpClient.execute(request); HttpEntity responseEntity = response.getEntity(); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { - System.err.print(String.format( - "Exception retrieving FHIR history: %s\n", response.getStatusLine().toString())); + System.err.print( + String.format( + "Exception retrieving FHIR history: %s\n", response.getStatusLine().toString())); responseEntity.writeTo(System.err); throw new RuntimeException(); } @@ -83,16 +83,16 @@ public static void fhirResourceGetHistory(String resourceName, String versionId) private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); - request.setConnectTimeout(1 * 60 * 1000); // 1 minute connect timeout - request.setReadTimeout(1 * 60 * 1000); // 1 minute read timeout + new HttpCredentialsAdapter(credential).initialize(request); + request.setConnectTimeout(60000); // 1 minute connect timeout + request.setReadTimeout(60000); // 1 minute read timeout }; // Build the client for interacting with the service. @@ -102,11 +102,11 @@ private static CloudHealthcare createClient() throws IOException { } private static String getAccessToken() throws IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); - credential.refreshToken(); - return credential.getAccessToken(); + + return credential.refreshAccessToken().getTokenValue(); } } // [END healthcare_get_resource_history] diff --git a/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceGetPatientEverything.java b/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceGetPatientEverything.java index 79d0bcc7565..e686ffbd519 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceGetPatientEverything.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceGetPatientEverything.java @@ -17,13 +17,14 @@ package snippets.healthcare.fhir.resources; // [START healthcare_get_patient_everything] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.net.URISyntaxException; import java.util.Collections; @@ -52,25 +53,24 @@ public static void fhirResourceGetPatientEverything(String resourceName) CloudHealthcare client = createClient(); HttpClient httpClient = HttpClients.createDefault(); - String uri = String.format( - "%sv1/%s/$everything", client.getRootUrl(), resourceName); - URIBuilder uriBuilder = new URIBuilder(uri) - .setParameter("access_token", getAccessToken()); + String uri = String.format("%sv1/%s/$everything", client.getRootUrl(), resourceName); + URIBuilder uriBuilder = new URIBuilder(uri).setParameter("access_token", getAccessToken()); - HttpUriRequest request = RequestBuilder - .get(uriBuilder.build()) - .addHeader("Content-Type", "application/json-patch+json") - .addHeader("Accept-Charset", "utf-8") - .addHeader("Accept", "application/fhir+json; charset=utf-8") - .build(); + HttpUriRequest request = + RequestBuilder.get(uriBuilder.build()) + .addHeader("Content-Type", "application/json-patch+json") + .addHeader("Accept-Charset", "utf-8") + .addHeader("Accept", "application/fhir+json; charset=utf-8") + .build(); // Execute the request and process the results. HttpResponse response = httpClient.execute(request); HttpEntity responseEntity = response.getEntity(); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { - System.err.print(String.format( - "Exception getting patient everythingresource: %s\n", - response.getStatusLine().toString())); + System.err.print( + String.format( + "Exception getting patient everythingresource: %s\n", + response.getStatusLine().toString())); responseEntity.writeTo(System.err); throw new RuntimeException(); } @@ -81,14 +81,14 @@ public static void fhirResourceGetPatientEverything(String resourceName) private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; @@ -100,11 +100,11 @@ private static CloudHealthcare createClient() throws IOException { } private static String getAccessToken() throws IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); - credential.refreshToken(); - return credential.getAccessToken(); + + return credential.refreshAccessToken().getTokenValue(); } } // [END healthcare_get_patient_everything] diff --git a/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceListHistory.java b/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceListHistory.java index 45b315aceba..d28c8d3d970 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceListHistory.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceListHistory.java @@ -18,13 +18,14 @@ // [START healthcare_list_resource_history] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.net.URISyntaxException; import java.util.Collections; @@ -54,25 +55,24 @@ public static void fhirResourceListHistory(String resourceName) CloudHealthcare client = createClient(); HttpClient httpClient = HttpClients.createDefault(); - String uri = String.format( - "%sv1/%s/_history", client.getRootUrl(), resourceName); - URIBuilder uriBuilder = new URIBuilder(uri) - .setParameter("access_token", getAccessToken()); + String uri = String.format("%sv1/%s/_history", client.getRootUrl(), resourceName); + URIBuilder uriBuilder = new URIBuilder(uri).setParameter("access_token", getAccessToken()); - HttpUriRequest request = RequestBuilder - .get() - .setUri(uriBuilder.build()) - .addHeader("Content-Type", "application/fhir+json") - .addHeader("Accept-Charset", "utf-8") - .addHeader("Accept", "application/fhir+json; charset=utf-8") - .build(); + HttpUriRequest request = + RequestBuilder.get() + .setUri(uriBuilder.build()) + .addHeader("Content-Type", "application/fhir+json") + .addHeader("Accept-Charset", "utf-8") + .addHeader("Accept", "application/fhir+json; charset=utf-8") + .build(); // Execute the request and process the results. HttpResponse response = httpClient.execute(request); HttpEntity responseEntity = response.getEntity(); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { - System.err.print(String.format( - "Exception retrieving FHIR history: %s\n", response.getStatusLine().toString())); + System.err.print( + String.format( + "Exception retrieving FHIR history: %s\n", response.getStatusLine().toString())); responseEntity.writeTo(System.err); throw new RuntimeException(); } @@ -83,16 +83,16 @@ public static void fhirResourceListHistory(String resourceName) private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); - request.setConnectTimeout(1 * 60 * 1000); // 1 minute connect timeout - request.setReadTimeout(1 * 60 * 1000); // 1 minute read timeout + new HttpCredentialsAdapter(credential).initialize(request); + request.setConnectTimeout(60000); // 1 minute connect timeout + request.setReadTimeout(60000); // 1 minute read timeout }; // Build the client for interacting with the service. @@ -102,11 +102,11 @@ private static CloudHealthcare createClient() throws IOException { } private static String getAccessToken() throws IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); - credential.refreshToken(); - return credential.getAccessToken(); + + return credential.refreshAccessToken().getTokenValue(); } } // [END healthcare_list_resource_history] diff --git a/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourcePatch.java b/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourcePatch.java index 73f4330701d..02b12d9e671 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourcePatch.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourcePatch.java @@ -17,13 +17,14 @@ package snippets.healthcare.fhir.resources; // [START healthcare_patch_resource] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.net.URISyntaxException; import java.util.Collections; @@ -55,26 +56,25 @@ public static void fhirResourcePatch(String resourceName, String data) CloudHealthcare client = createClient(); HttpClient httpClient = HttpClients.createDefault(); - String uri = String.format( - "%sv1/%s", client.getRootUrl(), resourceName); - URIBuilder uriBuilder = new URIBuilder(uri) - .setParameter("access_token", getAccessToken()); + String uri = String.format("%sv1/%s", client.getRootUrl(), resourceName); + URIBuilder uriBuilder = new URIBuilder(uri).setParameter("access_token", getAccessToken()); StringEntity requestEntity = new StringEntity(data); - HttpUriRequest request = RequestBuilder - .patch(uriBuilder.build()) - .setEntity(requestEntity) - .addHeader("Content-Type", "application/json-patch+json") - .addHeader("Accept-Charset", "utf-8") - .addHeader("Accept", "application/fhir+json; charset=utf-8") - .build(); + HttpUriRequest request = + RequestBuilder.patch(uriBuilder.build()) + .setEntity(requestEntity) + .addHeader("Content-Type", "application/json-patch+json") + .addHeader("Accept-Charset", "utf-8") + .addHeader("Accept", "application/fhir+json; charset=utf-8") + .build(); // Execute the request and process the results. HttpResponse response = httpClient.execute(request); HttpEntity responseEntity = response.getEntity(); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { - System.err.print(String.format( - "Exception patching FHIR resource: %s\n", response.getStatusLine().toString())); + System.err.print( + String.format( + "Exception patching FHIR resource: %s\n", response.getStatusLine().toString())); responseEntity.writeTo(System.err); throw new RuntimeException(); } @@ -85,14 +85,14 @@ public static void fhirResourcePatch(String resourceName, String data) private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; @@ -104,11 +104,11 @@ private static CloudHealthcare createClient() throws IOException { } private static String getAccessToken() throws IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); - credential.refreshToken(); - return credential.getAccessToken(); + + return credential.refreshAccessToken().getTokenValue(); } } // [END healthcare_patch_resource] diff --git a/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceSearchGet.java b/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceSearchGet.java index 18d0ad81950..cd323464351 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceSearchGet.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceSearchGet.java @@ -17,13 +17,14 @@ package snippets.healthcare.fhir.resources; // [START healthcare_search_resources_get] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.net.URISyntaxException; import java.util.Collections; @@ -52,25 +53,24 @@ public static void fhirResourceSearchGet(String resourceName) CloudHealthcare client = createClient(); HttpClient httpClient = HttpClients.createDefault(); - String uri = String.format( - "%sv1/%s", client.getRootUrl(), resourceName); - URIBuilder uriBuilder = new URIBuilder(uri) - .setParameter("access_token", getAccessToken()); + String uri = String.format("%sv1/%s", client.getRootUrl(), resourceName); + URIBuilder uriBuilder = new URIBuilder(uri).setParameter("access_token", getAccessToken()); - HttpUriRequest request = RequestBuilder - .get() - .setUri(uriBuilder.build()) - .addHeader("Content-Type", "application/fhir+json") - .addHeader("Accept-Charset", "utf-8") - .addHeader("Accept", "application/fhir+json; charset=utf-8") - .build(); + HttpUriRequest request = + RequestBuilder.get() + .setUri(uriBuilder.build()) + .addHeader("Content-Type", "application/fhir+json") + .addHeader("Accept-Charset", "utf-8") + .addHeader("Accept", "application/fhir+json; charset=utf-8") + .build(); // Execute the request and process the results. HttpResponse response = httpClient.execute(request); HttpEntity responseEntity = response.getEntity(); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { - System.err.print(String.format( - "Exception searching GET FHIR resources: %s\n", response.getStatusLine().toString())); + System.err.print( + String.format( + "Exception searching GET FHIR resources: %s\n", response.getStatusLine().toString())); responseEntity.writeTo(System.err); throw new RuntimeException(); } @@ -81,14 +81,14 @@ public static void fhirResourceSearchGet(String resourceName) private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; @@ -100,11 +100,11 @@ private static CloudHealthcare createClient() throws IOException { } private static String getAccessToken() throws IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); - credential.refreshToken(); - return credential.getAccessToken(); + + return credential.refreshAccessToken().getTokenValue(); } } // [END healthcare_search_resources_get] diff --git a/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceSearchPost.java b/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceSearchPost.java index fa0dabb8f49..6a3dff2797f 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceSearchPost.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/fhir/resources/FhirResourceSearchPost.java @@ -17,13 +17,14 @@ package snippets.healthcare.fhir.resources; // [START healthcare_search_resources_post] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.net.URISyntaxException; import java.util.Collections; @@ -53,46 +54,45 @@ public static void fhirResourceSearchPost(String resourceName) CloudHealthcare client = createClient(); HttpClient httpClient = HttpClients.createDefault(); - String uri = String.format( - "%sv1/%s/_search", client.getRootUrl(), resourceName); - URIBuilder uriBuilder = new URIBuilder(uri) - .setParameter("access_token", getAccessToken()); + String uri = String.format("%sv1/%s/_search", client.getRootUrl(), resourceName); + URIBuilder uriBuilder = new URIBuilder(uri).setParameter("access_token", getAccessToken()); StringEntity requestEntity = new StringEntity(""); - HttpUriRequest request = RequestBuilder - .post() - .setUri(uriBuilder.build()) - .setEntity(requestEntity) - .addHeader("Content-Type", "application/fhir+json") - .addHeader("Accept-Charset", "utf-8") - .addHeader("Accept", "application/fhir+json; charset=utf-8") - .build(); + HttpUriRequest request = + RequestBuilder.post() + .setUri(uriBuilder.build()) + .setEntity(requestEntity) + .addHeader("Content-Type", "application/fhir+json") + .addHeader("Accept-Charset", "utf-8") + .addHeader("Accept", "application/fhir+json; charset=utf-8") + .build(); // Execute the request and process the results. HttpResponse response = httpClient.execute(request); HttpEntity responseEntity = response.getEntity(); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { - System.err.print(String.format( - "Exception searching POST FHIR resources: %s\n", response.getStatusLine().toString())); + System.err.print( + String.format( + "Exception searching POST FHIR resources: %s\n", + response.getStatusLine().toString())); responseEntity.writeTo(System.err); throw new RuntimeException(); } System.out.println("FHIR resource search results: "); responseEntity.writeTo(System.out); - } private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; @@ -104,11 +104,11 @@ private static CloudHealthcare createClient() throws IOException { } private static String getAccessToken() throws IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); - credential.refreshToken(); - return credential.getAccessToken(); + + return credential.refreshAccessToken().getTokenValue(); } } // [END healthcare_search_resources_post] diff --git a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreCreate.java b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreCreate.java index f9ce662214e..b852e560226 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreCreate.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreCreate.java @@ -17,8 +17,6 @@ package snippets.healthcare.hl7v2; // [START healthcare_create_hl7v2_store] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -27,6 +25,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.Hl7V2Stores; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.Hl7V2Store; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; import java.util.HashMap; @@ -69,14 +69,14 @@ public static void hl7v2StoreCreate(String datasetName, String hl7v2StoreId) thr private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreDelete.java b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreDelete.java index 82d7472b844..47637bb1ddf 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreDelete.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreDelete.java @@ -17,8 +17,6 @@ package snippets.healthcare.hl7v2; // [START healthcare_delete_hl7v2_store] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -26,6 +24,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.Hl7V2Stores; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -54,14 +54,14 @@ public static void hl7v2StoreDelete(String hl7v2StoreName) throws IOException { private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreGet.java b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreGet.java index a0d81512c86..0f05c5599b1 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreGet.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreGet.java @@ -17,8 +17,6 @@ package snippets.healthcare.hl7v2; // [START healthcare_get_hl7v2_store] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -27,6 +25,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.Hl7V2Stores; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.Hl7V2Store; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -55,14 +55,14 @@ public static void hl7v2eStoreGet(String hl7v2StoreName) throws IOException { private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreGetIamPolicy.java b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreGetIamPolicy.java index 9b9eed70669..501bf7572bd 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreGetIamPolicy.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreGetIamPolicy.java @@ -17,8 +17,6 @@ package snippets.healthcare.hl7v2; // [START healthcare_hl7v2_store_get_iam_policy] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -27,6 +25,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.Hl7V2Stores; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.Policy; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -55,14 +55,14 @@ public static void hl7v2StoreGetIamPolicy(String hl7v2StoreName) throws IOExcept private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreList.java b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreList.java index 1a7e98cd0a8..39e1bfa8629 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreList.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreList.java @@ -17,8 +17,6 @@ package snippets.healthcare.hl7v2; // [START healthcare_list_hl7v2_stores] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -28,6 +26,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.Hl7V2Store; import com.google.api.services.healthcare.v1.model.ListHl7V2StoresResponse; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -78,14 +78,14 @@ public static void hl7v2StoreList(String datasetName) throws IOException { private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StorePatch.java b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StorePatch.java index 7b9941d6806..35e0a4ec5e0 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StorePatch.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StorePatch.java @@ -17,8 +17,6 @@ package snippets.healthcare.hl7v2; // [START healthcare_patch_hl7v2_store] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -28,6 +26,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.Hl7V2NotificationConfig; import com.google.api.services.healthcare.v1.model.Hl7V2Store; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -78,14 +78,14 @@ public static void patchHl7v2Store(String hl7v2StoreName, String pubsubTopic) th private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreSetIamPolicy.java b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreSetIamPolicy.java index fcde538a107..05d62e36307 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreSetIamPolicy.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/Hl7v2StoreSetIamPolicy.java @@ -17,8 +17,6 @@ package snippets.healthcare.hl7v2; // [START healthcare_hl7v2_store_set_iam_policy] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -29,6 +27,8 @@ import com.google.api.services.healthcare.v1.model.Binding; import com.google.api.services.healthcare.v1.model.Policy; import com.google.api.services.healthcare.v1.model.SetIamPolicyRequest; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Arrays; import java.util.Collections; @@ -73,14 +73,14 @@ public static void hl7v2StoreSetIamPolicy(String hl7v2StoreName) throws IOExcept private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessageCreate.java b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessageCreate.java index 1b6f69ce175..c4852f5ecf3 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessageCreate.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessageCreate.java @@ -17,8 +17,6 @@ package snippets.healthcare.hl7v2.messages; // [START healthcare_create_hl7v2_message] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -28,6 +26,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.CreateMessageRequest; import com.google.api.services.healthcare.v1.model.Message; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; @@ -75,14 +75,14 @@ public static void hl7v2MessageCreate(String hl7v2StoreName, String messageId, S private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessageDelete.java b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessageDelete.java index 6438a74f00f..edbec86a8d1 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessageDelete.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessageDelete.java @@ -17,8 +17,6 @@ package snippets.healthcare.hl7v2.messages; // [START healthcare_delete_hl7v2_message] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -26,6 +24,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcare; import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.Hl7V2Stores.Messages; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -55,14 +55,14 @@ public static void hl7v2MessageDelete(String hl7v2MessageName) throws IOExceptio private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessageGet.java b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessageGet.java index b26120383e2..e87b185f299 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessageGet.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessageGet.java @@ -17,7 +17,6 @@ package snippets.healthcare.hl7v2.messages; // [START healthcare_get_hl7v2_message] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -26,6 +25,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.Hl7V2Stores.Messages; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.Message; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -55,14 +56,14 @@ public static void hl7v2MessageGet(String hl7v2MessageName) throws IOException { private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessageIngest.java b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessageIngest.java index 187361f11e3..8f0548d108e 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessageIngest.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessageIngest.java @@ -17,8 +17,6 @@ package snippets.healthcare.hl7v2.messages; // [START healthcare_ingest_hl7v2_message] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -29,6 +27,8 @@ import com.google.api.services.healthcare.v1.model.IngestMessageRequest; import com.google.api.services.healthcare.v1.model.IngestMessageResponse; import com.google.api.services.healthcare.v1.model.Message; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; @@ -75,14 +75,14 @@ public static void hl7v2MessageIngest(String hl7v2StoreName, String filePath) th private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessageList.java b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessageList.java index a04dd3dc69f..6bbfdac5c58 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessageList.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessageList.java @@ -17,7 +17,6 @@ package snippets.healthcare.hl7v2.messages; // [START healthcare_list_hl7v2_messages] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -27,6 +26,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.ListMessagesResponse; import com.google.api.services.healthcare.v1.model.Message; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; @@ -80,14 +81,14 @@ public static void hl7v2MessageList(String hl7v2StoreName) throws IOException { private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessagePatch.java b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessagePatch.java index ee6e788104f..78e34511c2b 100644 --- a/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessagePatch.java +++ b/healthcare/v1/src/main/java/snippets/healthcare/hl7v2/messages/HL7v2MessagePatch.java @@ -17,8 +17,6 @@ package snippets.healthcare.hl7v2.messages; // [START healthcare_patch_hl7v2_message] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; @@ -27,6 +25,8 @@ import com.google.api.services.healthcare.v1.CloudHealthcare.Projects.Locations.Datasets.Hl7V2Stores.Messages; import com.google.api.services.healthcare.v1.CloudHealthcareScopes; import com.google.api.services.healthcare.v1.model.Message; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; import java.util.HashMap; @@ -77,14 +77,14 @@ public static void hl7v2MessagePatch(String hl7v2MessageName) throws IOException private static CloudHealthcare createClient() throws IOException { // Use Application Default Credentials (ADC) to authenticate the requests // For more information see https://cloud.google.com/docs/authentication/production - GoogleCredential credential = - GoogleCredential.getApplicationDefault(HTTP_TRANSPORT, JSON_FACTORY) + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(CloudHealthcareScopes.CLOUD_PLATFORM)); // Create a HttpRequestInitializer, which will provide a baseline configuration to all requests. HttpRequestInitializer requestInitializer = request -> { - credential.initialize(request); + new HttpCredentialsAdapter(credential).initialize(request); request.setConnectTimeout(60000); // 1 minute connect timeout request.setReadTimeout(60000); // 1 minute read timeout }; diff --git a/healthcare/v1/src/test/java/snippets/healthcare/FhirResourceTests.java b/healthcare/v1/src/test/java/snippets/healthcare/FhirResourceTests.java index 5977fc977c2..d9c241bd8ac 100644 --- a/healthcare/v1/src/test/java/snippets/healthcare/FhirResourceTests.java +++ b/healthcare/v1/src/test/java/snippets/healthcare/FhirResourceTests.java @@ -20,7 +20,6 @@ import static org.hamcrest.CoreMatchers.containsString; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import com.google.gson.Gson; import com.google.gson.JsonArray; @@ -37,7 +36,6 @@ import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; diff --git a/healthcare/v1/src/test/java/snippets/healthcare/Hl7v2MessageTests.java b/healthcare/v1/src/test/java/snippets/healthcare/Hl7v2MessageTests.java index f98efb49934..4ee160f0bc2 100644 --- a/healthcare/v1/src/test/java/snippets/healthcare/Hl7v2MessageTests.java +++ b/healthcare/v1/src/test/java/snippets/healthcare/Hl7v2MessageTests.java @@ -136,8 +136,6 @@ public void test_GetHL7v2Message() throws Exception { @Test public void test_Hl7v2MessageList() throws Exception { - HL7v2MessageCreate.hl7v2MessageCreate( - hl7v2StoreName, "new-hl7v2-message", "src/test/resources/hl7v2-sample-ingest.txt"); HL7v2MessageList.hl7v2MessageList(hl7v2StoreName); String output = bout.toString(); diff --git a/iam/api-client/pom.xml b/iam/api-client/pom.xml index 88e2146ec66..4584f1e5bdc 100644 --- a/iam/api-client/pom.xml +++ b/iam/api-client/pom.xml @@ -27,26 +27,31 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 1.8 1.8 - + com.google.apis google-api-services-iam - v1-rev20200221-1.30.9 + v1-rev20200611-1.30.9 + + com.google.auth + google-auth-library-oauth2-http + 0.21.0 + com.google.apis google-api-services-cloudresourcemanager - v1-rev20200504-1.30.9 + v1-rev20200617-1.30.9 commons-cli @@ -74,12 +79,12 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 com.google.iam.snippets.GrantableRoles - + diff --git a/iam/api-client/src/main/java/iam/snippets/CreateServiceAccount.java b/iam/api-client/src/main/java/iam/snippets/CreateServiceAccount.java index af434f3263f..ba45eed90fe 100644 --- a/iam/api-client/src/main/java/iam/snippets/CreateServiceAccount.java +++ b/iam/api-client/src/main/java/iam/snippets/CreateServiceAccount.java @@ -16,13 +16,14 @@ package iam.snippets; // [START iam_create_service_account] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.iam.v1.Iam; import com.google.api.services.iam.v1.IamScopes; import com.google.api.services.iam.v1.model.CreateServiceAccountRequest; import com.google.api.services.iam.v1.model.ServiceAccount; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Collections; @@ -60,15 +61,15 @@ public static void createServiceAccount(String projectId) { private static Iam initService() throws GeneralSecurityException, IOException { // Use the Application Default Credentials strategy for authentication. For more info, see: // https://cloud.google.com/docs/authentication/production#finding_credentials_automatically - GoogleCredential credential = - GoogleCredential.getApplicationDefault() + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM)); // Initialize the IAM service, which can be used to send requests to the IAM API. Iam service = new Iam.Builder( GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), - credential) + new HttpCredentialsAdapter(credential)) .setApplicationName("service-accounts") .build(); return service; diff --git a/iam/api-client/src/main/java/iam/snippets/CreateServiceAccountKey.java b/iam/api-client/src/main/java/iam/snippets/CreateServiceAccountKey.java index 06db467d1e7..19a00119716 100644 --- a/iam/api-client/src/main/java/iam/snippets/CreateServiceAccountKey.java +++ b/iam/api-client/src/main/java/iam/snippets/CreateServiceAccountKey.java @@ -16,13 +16,14 @@ package iam.snippets; // [START iam_create_key] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.iam.v1.Iam; import com.google.api.services.iam.v1.IamScopes; import com.google.api.services.iam.v1.model.CreateServiceAccountKeyRequest; import com.google.api.services.iam.v1.model.ServiceAccountKey; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Collections; @@ -32,7 +33,7 @@ public class CreateServiceAccountKey { // Creates a key for a service account. public static void createKey(String projectId) { // String projectId = "my-project-id"; - + Iam service = null; try { service = initService(); @@ -63,15 +64,15 @@ public static void createKey(String projectId) { private static Iam initService() throws GeneralSecurityException, IOException { // Use the Application Default Credentials strategy for authentication. For more info, see: // https://cloud.google.com/docs/authentication/production#finding_credentials_automatically - GoogleCredential credential = - GoogleCredential.getApplicationDefault() + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM)); // Initialize the IAM service, which can be used to send requests to the IAM API. Iam service = new Iam.Builder( GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), - credential) + new HttpCredentialsAdapter(credential)) .setApplicationName("service-account-keys") .build(); return service; diff --git a/iam/api-client/src/main/java/iam/snippets/DeleteServiceAccount.java b/iam/api-client/src/main/java/iam/snippets/DeleteServiceAccount.java index bd3b94bee11..b7bf077fcce 100644 --- a/iam/api-client/src/main/java/iam/snippets/DeleteServiceAccount.java +++ b/iam/api-client/src/main/java/iam/snippets/DeleteServiceAccount.java @@ -16,11 +16,12 @@ package iam.snippets; // [START iam_delete_service_account] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.iam.v1.Iam; import com.google.api.services.iam.v1.IamScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Collections; @@ -30,8 +31,8 @@ public class DeleteServiceAccount { // Deletes a service account. public static void deleteServiceAccount(String projectId) { // String projectId = "my-project-id"; - - Iam service = null; + + Iam service = null; try { service = initService(); } catch (IOException | GeneralSecurityException e) { @@ -63,15 +64,15 @@ public static void deleteServiceAccount(String projectId) { private static Iam initService() throws GeneralSecurityException, IOException { // Use the Application Default Credentials strategy for authentication. For more info, see: // https://cloud.google.com/docs/authentication/production#finding_credentials_automatically - GoogleCredential credential = - GoogleCredential.getApplicationDefault() + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM)); // Initialize the IAM service, which can be used to send requests to the IAM API. Iam service = new Iam.Builder( GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), - credential) + new HttpCredentialsAdapter(credential)) .setApplicationName("service-accounts") .build(); return service; diff --git a/iam/api-client/src/main/java/iam/snippets/DeleteServiceAccountKey.java b/iam/api-client/src/main/java/iam/snippets/DeleteServiceAccountKey.java index a20d99299d7..38cae094bea 100644 --- a/iam/api-client/src/main/java/iam/snippets/DeleteServiceAccountKey.java +++ b/iam/api-client/src/main/java/iam/snippets/DeleteServiceAccountKey.java @@ -16,12 +16,13 @@ package iam.snippets; // [START iam_delete_key] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.iam.v1.Iam; import com.google.api.services.iam.v1.IamScopes; import com.google.api.services.iam.v1.model.ServiceAccountKey; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Collections; @@ -32,7 +33,7 @@ public class DeleteServiceAccountKey { // Deletes a service account key. public static void deleteKey(String projectId) { // String projectId = "my-project-id"; - + Iam service = null; try { service = initService(); @@ -69,15 +70,15 @@ public static void deleteKey(String projectId) { private static Iam initService() throws GeneralSecurityException, IOException { // Use the Application Default Credentials strategy for authentication. For more info, see: // https://cloud.google.com/docs/authentication/production#finding_credentials_automatically - GoogleCredential credential = - GoogleCredential.getApplicationDefault() + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM)); // Initialize the IAM service, which can be used to send requests to the IAM API. Iam service = new Iam.Builder( GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), - credential) + new HttpCredentialsAdapter(credential)) .setApplicationName("service-account-keys") .build(); return service; diff --git a/iam/api-client/src/main/java/iam/snippets/DisableServiceAccount.java b/iam/api-client/src/main/java/iam/snippets/DisableServiceAccount.java index c8e28a42949..ff8e949ced0 100644 --- a/iam/api-client/src/main/java/iam/snippets/DisableServiceAccount.java +++ b/iam/api-client/src/main/java/iam/snippets/DisableServiceAccount.java @@ -16,12 +16,13 @@ package iam.snippets; // [START iam_disable_service_account] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.iam.v1.Iam; import com.google.api.services.iam.v1.IamScopes; import com.google.api.services.iam.v1.model.DisableServiceAccountRequest; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Collections; @@ -66,15 +67,15 @@ public static void disableServiceAccount(String projectId) { private static Iam initService() throws GeneralSecurityException, IOException { // Use the Application Default Credentials strategy for authentication. For more info, see: // https://cloud.google.com/docs/authentication/production#finding_credentials_automatically - GoogleCredential credential = - GoogleCredential.getApplicationDefault() + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM)); // Initialize the IAM service, which can be used to send requests to the IAM API. Iam service = new Iam.Builder( GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), - credential) + new HttpCredentialsAdapter(credential)) .setApplicationName("service-accounts") .build(); return service; diff --git a/iam/api-client/src/main/java/iam/snippets/EnableServiceAccount.java b/iam/api-client/src/main/java/iam/snippets/EnableServiceAccount.java index 57778adb14d..bdccaab0ba1 100644 --- a/iam/api-client/src/main/java/iam/snippets/EnableServiceAccount.java +++ b/iam/api-client/src/main/java/iam/snippets/EnableServiceAccount.java @@ -16,12 +16,13 @@ package iam.snippets; // [START iam_enable_service_account] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.iam.v1.Iam; import com.google.api.services.iam.v1.IamScopes; import com.google.api.services.iam.v1.model.EnableServiceAccountRequest; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Collections; @@ -66,15 +67,15 @@ public static void enableServiceAccount(String projectId) { private static Iam initService() throws GeneralSecurityException, IOException { // Use the Application Default Credentials strategy for authentication. For more info, see: // https://cloud.google.com/docs/authentication/production#finding_credentials_automatically - GoogleCredential credential = - GoogleCredential.getApplicationDefault() + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM)); // Initialize the IAM service, which can be used to send requests to the IAM API. Iam service = new Iam.Builder( GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), - credential) + new HttpCredentialsAdapter(credential)) .setApplicationName("service-accounts") .build(); return service; diff --git a/iam/api-client/src/main/java/iam/snippets/GetPolicy.java b/iam/api-client/src/main/java/iam/snippets/GetPolicy.java index f3ecbc07759..d007fe48123 100644 --- a/iam/api-client/src/main/java/iam/snippets/GetPolicy.java +++ b/iam/api-client/src/main/java/iam/snippets/GetPolicy.java @@ -16,13 +16,14 @@ package iam.snippets; // [START iam_get_policy] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.cloudresourcemanager.CloudResourceManager; import com.google.api.services.cloudresourcemanager.model.GetIamPolicyRequest; import com.google.api.services.cloudresourcemanager.model.Policy; import com.google.api.services.iam.v1.IamScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Collections; @@ -58,15 +59,15 @@ public static CloudResourceManager createCloudResourceManagerService() throws IOException, GeneralSecurityException { // Use the Application Default Credentials strategy for authentication. For more info, see: // https://cloud.google.com/docs/authentication/production#finding_credentials_automatically - GoogleCredential credential = - GoogleCredential.getApplicationDefault() + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM)); CloudResourceManager service = new CloudResourceManager.Builder( GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), - credential) + new HttpCredentialsAdapter(credential)) .setApplicationName("service-accounts") .build(); return service; diff --git a/iam/api-client/src/main/java/iam/snippets/GrantableRoles.java b/iam/api-client/src/main/java/iam/snippets/GrantableRoles.java index f55f7ab274b..76528c070e0 100644 --- a/iam/api-client/src/main/java/iam/snippets/GrantableRoles.java +++ b/iam/api-client/src/main/java/iam/snippets/GrantableRoles.java @@ -15,7 +15,6 @@ package iam.snippets; -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.iam.v1.Iam; @@ -23,21 +22,23 @@ import com.google.api.services.iam.v1.model.QueryGrantableRolesRequest; import com.google.api.services.iam.v1.model.QueryGrantableRolesResponse; import com.google.api.services.iam.v1.model.Role; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.util.Collections; public class GrantableRoles { public static void main(String[] args) throws Exception { - GoogleCredential credential = - GoogleCredential.getApplicationDefault() + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM)); Iam service = new Iam.Builder( GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), - credential) + new HttpCredentialsAdapter(credential)) .setApplicationName("grantable-roles") .build(); diff --git a/iam/api-client/src/main/java/iam/snippets/ListServiceAccountKeys.java b/iam/api-client/src/main/java/iam/snippets/ListServiceAccountKeys.java index 62b14b57f99..42debf70c57 100644 --- a/iam/api-client/src/main/java/iam/snippets/ListServiceAccountKeys.java +++ b/iam/api-client/src/main/java/iam/snippets/ListServiceAccountKeys.java @@ -16,12 +16,13 @@ package iam.snippets; // [START iam_list_keys] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.iam.v1.Iam; import com.google.api.services.iam.v1.IamScopes; import com.google.api.services.iam.v1.model.ServiceAccountKey; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Collections; @@ -32,7 +33,7 @@ public class ListServiceAccountKeys { // Lists all keys for a service account. public static void listKeys(String projectId) { // String projectId = "my-project-id"; - + Iam service = null; try { service = initService(); @@ -66,15 +67,15 @@ public static void listKeys(String projectId) { private static Iam initService() throws GeneralSecurityException, IOException { // Use the Application Default Credentials strategy for authentication. For more info, see: // https://cloud.google.com/docs/authentication/production#finding_credentials_automatically - GoogleCredential credential = - GoogleCredential.getApplicationDefault() + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM)); // Initialize the IAM service, which can be used to send requests to the IAM API. Iam service = new Iam.Builder( GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), - credential) + new HttpCredentialsAdapter(credential)) .setApplicationName("service-account-keys") .build(); return service; diff --git a/iam/api-client/src/main/java/iam/snippets/ListServiceAccounts.java b/iam/api-client/src/main/java/iam/snippets/ListServiceAccounts.java index 1f8b4314f05..2aa36612824 100644 --- a/iam/api-client/src/main/java/iam/snippets/ListServiceAccounts.java +++ b/iam/api-client/src/main/java/iam/snippets/ListServiceAccounts.java @@ -16,13 +16,14 @@ package iam.snippets; // [START iam_list_service_accounts] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.iam.v1.Iam; import com.google.api.services.iam.v1.IamScopes; import com.google.api.services.iam.v1.model.ListServiceAccountsResponse; import com.google.api.services.iam.v1.model.ServiceAccount; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Collections; @@ -33,7 +34,7 @@ public class ListServiceAccounts { // Lists all service accounts for the current project. public static void listServiceAccounts(String projectId) { // String projectId = "my-project-id" - + Iam service = null; try { service = initService(); @@ -61,15 +62,15 @@ public static void listServiceAccounts(String projectId) { private static Iam initService() throws GeneralSecurityException, IOException { // Use the Application Default Credentials strategy for authentication. For more info, see: // https://cloud.google.com/docs/authentication/production#finding_credentials_automatically - GoogleCredential credential = - GoogleCredential.getApplicationDefault() + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM)); // Initialize the IAM service, which can be used to send requests to the IAM API. Iam service = new Iam.Builder( GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), - credential) + new HttpCredentialsAdapter(credential)) .setApplicationName("service-accounts") .build(); return service; diff --git a/iam/api-client/src/main/java/iam/snippets/Quickstart.java b/iam/api-client/src/main/java/iam/snippets/Quickstart.java index 9accc981da6..5167b07ea56 100644 --- a/iam/api-client/src/main/java/iam/snippets/Quickstart.java +++ b/iam/api-client/src/main/java/iam/snippets/Quickstart.java @@ -17,13 +17,14 @@ package iam.snippets; -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.iam.v1.Iam; import com.google.api.services.iam.v1.IamScopes; import com.google.api.services.iam.v1.model.ListRolesResponse; import com.google.api.services.iam.v1.model.Role; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.util.Collections; import java.util.List; @@ -31,8 +32,8 @@ public class Quickstart { public static void main(String[] args) throws Exception { // Get credentials - GoogleCredential credential = - GoogleCredential.getApplicationDefault() + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM)); // Create the Cloud IAM service object @@ -40,7 +41,7 @@ public static void main(String[] args) throws Exception { new Iam.Builder( GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), - credential) + new HttpCredentialsAdapter(credential)) .setApplicationName("quickstart") .build(); diff --git a/iam/api-client/src/main/java/iam/snippets/QuickstartV2.java b/iam/api-client/src/main/java/iam/snippets/QuickstartV2.java index 519356b56b3..b16379aaea2 100644 --- a/iam/api-client/src/main/java/iam/snippets/QuickstartV2.java +++ b/iam/api-client/src/main/java/iam/snippets/QuickstartV2.java @@ -14,9 +14,8 @@ */ package iam.snippets; -// [START iam_quickstart_v2] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +// [START iam_quickstart_v2] import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.cloudresourcemanager.CloudResourceManager; @@ -25,6 +24,8 @@ import com.google.api.services.cloudresourcemanager.model.Policy; import com.google.api.services.cloudresourcemanager.model.SetIamPolicyRequest; import com.google.api.services.iam.v1.IamScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Collections; @@ -76,8 +77,8 @@ public static CloudResourceManager initializeService() throws IOException, GeneralSecurityException { // Use the Application Default Credentials strategy for authentication. For more info, see: // https://cloud.google.com/docs/authentication/production#finding_credentials_automatically - GoogleCredential credential = - GoogleCredential.getApplicationDefault() + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM)); // Creates the Cloud Resource Manager service object. @@ -85,7 +86,7 @@ public static CloudResourceManager initializeService() new CloudResourceManager.Builder( GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), - credential) + new HttpCredentialsAdapter(credential)) .setApplicationName("service-accounts") .build(); return service; diff --git a/iam/api-client/src/main/java/iam/snippets/RenameServiceAccount.java b/iam/api-client/src/main/java/iam/snippets/RenameServiceAccount.java index 05a047ff4ab..780780a8fc2 100644 --- a/iam/api-client/src/main/java/iam/snippets/RenameServiceAccount.java +++ b/iam/api-client/src/main/java/iam/snippets/RenameServiceAccount.java @@ -16,12 +16,13 @@ package iam.snippets; // [START iam_rename_service_account] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.iam.v1.Iam; import com.google.api.services.iam.v1.IamScopes; import com.google.api.services.iam.v1.model.ServiceAccount; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Collections; @@ -31,7 +32,7 @@ public class RenameServiceAccount { // Changes a service account's display name. public static void renameServiceAccount(String projectId) { // String projectId = "my-project-id"; - + Iam service = null; try { service = initService(); @@ -75,15 +76,15 @@ public static void renameServiceAccount(String projectId) { private static Iam initService() throws GeneralSecurityException, IOException { // Use the Application Default Credentials strategy for authentication. For more info, see: // https://cloud.google.com/docs/authentication/production#finding_credentials_automatically - GoogleCredential credential = - GoogleCredential.getApplicationDefault() + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM)); // Initialize the IAM service, which can be used to send requests to the IAM API. Iam service = new Iam.Builder( GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), - credential) + new HttpCredentialsAdapter(credential)) .setApplicationName("service-accounts") .build(); return service; diff --git a/iam/api-client/src/main/java/iam/snippets/SetPolicy.java b/iam/api-client/src/main/java/iam/snippets/SetPolicy.java index 9ca0567e501..08119a2d9cc 100644 --- a/iam/api-client/src/main/java/iam/snippets/SetPolicy.java +++ b/iam/api-client/src/main/java/iam/snippets/SetPolicy.java @@ -16,13 +16,14 @@ package iam.snippets; // [START iam_set_policy] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.cloudresourcemanager.CloudResourceManager; import com.google.api.services.cloudresourcemanager.model.Policy; import com.google.api.services.cloudresourcemanager.model.SetIamPolicyRequest; import com.google.api.services.iam.v1.IamScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Collections; @@ -56,15 +57,15 @@ public static CloudResourceManager createCloudResourceManagerService() throws IOException, GeneralSecurityException { // Use the Application Default Credentials strategy for authentication. For more info, see: // https://cloud.google.com/docs/authentication/production#finding_credentials_automatically - GoogleCredential credential = - GoogleCredential.getApplicationDefault() + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM)); CloudResourceManager service = new CloudResourceManager.Builder( GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), - credential) + new HttpCredentialsAdapter(credential)) .setApplicationName("service-accounts") .build(); return service; diff --git a/iam/api-client/src/main/java/iam/snippets/TestPermissions.java b/iam/api-client/src/main/java/iam/snippets/TestPermissions.java index 70c9b420912..9bde6b2e10c 100644 --- a/iam/api-client/src/main/java/iam/snippets/TestPermissions.java +++ b/iam/api-client/src/main/java/iam/snippets/TestPermissions.java @@ -16,13 +16,14 @@ package iam.snippets; // [START iam_test_permissions] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.cloudresourcemanager.CloudResourceManager; import com.google.api.services.cloudresourcemanager.model.TestIamPermissionsRequest; import com.google.api.services.cloudresourcemanager.model.TestIamPermissionsResponse; import com.google.api.services.iam.v1.IamScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Arrays; @@ -64,15 +65,15 @@ public static CloudResourceManager createCloudResourceManagerService() throws IOException, GeneralSecurityException { // Use the Application Default Credentials strategy for authentication. For more info, see: // https://cloud.google.com/docs/authentication/production#finding_credentials_automatically - GoogleCredential credential = - GoogleCredential.getApplicationDefault() + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM)); CloudResourceManager service = new CloudResourceManager.Builder( GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), - credential) + new HttpCredentialsAdapter(credential)) .setApplicationName("service-accounts") .build(); return service; diff --git a/iam/api-client/src/test/java/iam/snippets/QuickstartV2Tests.java b/iam/api-client/src/test/java/iam/snippets/QuickstartV2Tests.java index 1567d82121e..2566ad84ee4 100644 --- a/iam/api-client/src/test/java/iam/snippets/QuickstartV2Tests.java +++ b/iam/api-client/src/test/java/iam/snippets/QuickstartV2Tests.java @@ -19,7 +19,6 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.cloudresourcemanager.CloudResourceManager; @@ -29,6 +28,8 @@ import com.google.api.services.iam.v1.IamScopes; import com.google.api.services.iam.v1.model.CreateServiceAccountRequest; import com.google.api.services.iam.v1.model.ServiceAccount; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Collections; @@ -65,15 +66,15 @@ public static void checkRequirements() { @Before public void setUp() { try { - GoogleCredential credential = - GoogleCredential.getApplicationDefault() + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM)); iamService = new Iam.Builder( GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), - credential) + new HttpCredentialsAdapter(credential)) .setApplicationName("service-accounts") .build(); } catch (IOException | GeneralSecurityException e) { diff --git a/iam/api-client/src/test/java/iam/snippets/ServiceAccountTests.java b/iam/api-client/src/test/java/iam/snippets/ServiceAccountTests.java index 88179f94625..dbb6a2917e4 100644 --- a/iam/api-client/src/test/java/iam/snippets/ServiceAccountTests.java +++ b/iam/api-client/src/test/java/iam/snippets/ServiceAccountTests.java @@ -37,7 +37,8 @@ public class ServiceAccountTests { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static void requireEnvVar(String varName) { - assertNotNull(System.getenv(varName), + assertNotNull( + System.getenv(varName), String.format("Environment variable '%s' is required to perform these tests.", varName)); } diff --git a/iap/pom.xml b/iap/pom.xml index 23dcae4671f..e1015b54393 100644 --- a/iap/pom.xml +++ b/iap/pom.xml @@ -29,7 +29,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -43,7 +43,7 @@ com.fasterxml.jackson.core jackson-core - 2.11.0 + 2.11.1 @@ -58,12 +58,7 @@ com.google.auth google-auth-library-oauth2-http - 0.20.0 - - - com.nimbusds - nimbus-jose-jwt - 8.17 + 0.21.0 diff --git a/iap/src/main/java/com/example/iap/BuildIapRequest.java b/iap/src/main/java/com/example/iap/BuildIapRequest.java index 2fae86f320c..b3d76ab10d0 100644 --- a/iap/src/main/java/com/example/iap/BuildIapRequest.java +++ b/iap/src/main/java/com/example/iap/BuildIapRequest.java @@ -25,29 +25,28 @@ import com.google.auth.oauth2.GoogleCredentials; import com.google.auth.oauth2.IdTokenCredentials; import com.google.auth.oauth2.IdTokenProvider; -import java.time.Clock; +import com.google.common.base.Preconditions; +import java.io.IOException; import java.util.Collections; public class BuildIapRequest { private static final String IAM_SCOPE = "https://www.googleapis.com/auth/iam"; - private static final String OAUTH_TOKEN_URI = "https://www.googleapis.com/oauth2/v4/token"; - private static final String JWT_BEARER_TOKEN_GRANT_TYPE = - "urn:ietf:params:oauth:grant-type:jwt-bearer"; - private static final long EXPIRATION_TIME_IN_SECONDS = 3600L; private static final HttpTransport httpTransport = new NetHttpTransport(); - private static Clock clock = Clock.systemUTC(); - private BuildIapRequest() {} - private static IdTokenProvider getIdTokenProvider() throws Exception { + private static IdTokenProvider getIdTokenProvider() throws IOException { GoogleCredentials credentials = GoogleCredentials.getApplicationDefault().createScoped(Collections.singleton(IAM_SCOPE)); - // service account credentials are required to sign the jwt token - if (credentials == null || !(credentials instanceof IdTokenProvider)) { - throw new Exception("Google credentials : credentials that can provide id tokens expected"); - } + + Preconditions.checkNotNull(credentials, "Expected to load credentials"); + Preconditions.checkState( + credentials instanceof IdTokenProvider, + String.format( + "Expected credentials that can provide id tokens, got %s instead", + credentials.getClass().getName())); + return (IdTokenProvider) credentials; } @@ -57,16 +56,17 @@ private static IdTokenProvider getIdTokenProvider() throws Exception { * @param request Request to add authorization header * @param iapClientId OAuth 2.0 client ID for IAP protected resource * @return Clone of request with Bearer style authorization header with signed jwt token. - * @throws Exception exception creating signed JWT + * @throws IOException exception creating signed JWT */ public static HttpRequest buildIapRequest(HttpRequest request, String iapClientId) - throws Exception { + throws IOException { IdTokenProvider idTokenProvider = getIdTokenProvider(); - IdTokenCredentials credentials = IdTokenCredentials.newBuilder() - .setIdTokenProvider(idTokenProvider) - .setTargetAudience(iapClientId) - .build(); + IdTokenCredentials credentials = + IdTokenCredentials.newBuilder() + .setIdTokenProvider(idTokenProvider) + .setTargetAudience(iapClientId) + .build(); HttpRequestInitializer httpRequestInitializer = new HttpCredentialsAdapter(credentials); diff --git a/iap/src/main/java/com/example/iap/VerifyIapRequestHeader.java b/iap/src/main/java/com/example/iap/VerifyIapRequestHeader.java index 61503264456..5f894187cd9 100644 --- a/iap/src/main/java/com/example/iap/VerifyIapRequestHeader.java +++ b/iap/src/main/java/com/example/iap/VerifyIapRequestHeader.java @@ -18,53 +18,14 @@ // [START iap_validate_jwt] import com.google.api.client.http.HttpRequest; -import com.google.common.base.Preconditions; -import com.nimbusds.jose.JWSHeader; -import com.nimbusds.jose.JWSVerifier; -import com.nimbusds.jose.crypto.ECDSAVerifier; -import com.nimbusds.jose.jwk.ECKey; -import com.nimbusds.jose.jwk.JWK; -import com.nimbusds.jose.jwk.JWKSet; -import com.nimbusds.jwt.JWTClaimsSet; -import com.nimbusds.jwt.SignedJWT; -import java.net.URL; -import java.security.interfaces.ECPublicKey; -import java.time.Clock; -import java.time.Instant; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; +import com.google.api.client.json.webtoken.JsonWebToken; +import com.google.auth.oauth2.TokenVerifier; /** Verify IAP authorization JWT token in incoming request. */ public class VerifyIapRequestHeader { - private static final String PUBLIC_KEY_VERIFICATION_URL = - "https://www.gstatic.com/iap/verify/public_key-jwk"; - private static final String IAP_ISSUER_URL = "https://cloud.google.com/iap"; - // using a simple cache with no eviction for this sample - private final Map keyCache = new HashMap<>(); - - private static Clock clock = Clock.systemUTC(); - - private ECPublicKey getKey(String kid, String alg) throws Exception { - JWK jwk = keyCache.get(kid); - if (jwk == null) { - // update cache loading jwk public key data from url - JWKSet jwkSet = JWKSet.load(new URL(PUBLIC_KEY_VERIFICATION_URL)); - for (JWK key : jwkSet.getKeys()) { - keyCache.put(key.getKeyID(), key); - } - jwk = keyCache.get(kid); - } - // confirm that algorithm matches - if (jwk != null && jwk.getAlgorithm().getName().equals(alg)) { - return ECKey.parse(jwk.toJSONString()).toECPublicKey(); - } - return null; - } - // Verify jwt tokens addressed to IAP protected resources on App Engine. // The project *number* for your Google Cloud project via 'gcloud projects describe $PROJECT_ID' // The project *number* can also be retrieved from the Project Info card in Cloud Console. @@ -96,38 +57,21 @@ boolean verifyJwtForComputeEngine( Long.toUnsignedString(projectNumber), Long.toUnsignedString(backendServiceId))); } - private boolean verifyJwt(String jwtToken, String expectedAudience) throws Exception { - - // parse signed token into header / claims - SignedJWT signedJwt = SignedJWT.parse(jwtToken); - JWSHeader jwsHeader = signedJwt.getHeader(); - - // header must have algorithm("alg") and "kid" - Preconditions.checkNotNull(jwsHeader.getAlgorithm()); - Preconditions.checkNotNull(jwsHeader.getKeyID()); - - JWTClaimsSet claims = signedJwt.getJWTClaimsSet(); - - // claims must have audience, issuer - Preconditions.checkArgument(claims.getAudience().contains(expectedAudience)); - Preconditions.checkArgument(claims.getIssuer().equals(IAP_ISSUER_URL)); - - // claim must have issued at time in the past - Date currentTime = Date.from(Instant.now(clock)); - Preconditions.checkArgument(claims.getIssueTime().before(currentTime)); - // claim must have expiration time in the future - Preconditions.checkArgument(claims.getExpirationTime().after(currentTime)); - - // must have subject, email - Preconditions.checkNotNull(claims.getSubject()); - Preconditions.checkNotNull(claims.getClaim("email")); - - // verify using public key : lookup with key id, algorithm name provided - ECPublicKey publicKey = getKey(jwsHeader.getKeyID(), jwsHeader.getAlgorithm().getName()); - - Preconditions.checkNotNull(publicKey); - JWSVerifier jwsVerifier = new ECDSAVerifier(publicKey); - return signedJwt.verify(jwsVerifier); + private boolean verifyJwt(String jwtToken, String expectedAudience) { + TokenVerifier tokenVerifier = TokenVerifier.newBuilder() + .setAudience(expectedAudience) + .setIssuer(IAP_ISSUER_URL) + .build(); + try { + JsonWebToken jsonWebToken = tokenVerifier.verify(jwtToken); + + // Verify that the token contain subject and email claims + JsonWebToken.Payload payload = jsonWebToken.getPayload(); + return payload.getSubject() != null && payload.get("email") != null; + } catch (TokenVerifier.VerificationException e) { + System.out.println(e.getMessage()); + return false; + } } } // [END iap_validate_jwt] diff --git a/iap/src/test/java/com/example/iap/BuildAndVerifyIapRequestIT.java b/iap/src/test/java/com/example/iap/BuildAndVerifyIapRequestIT.java index e5f52e86e67..3bef9a2824e 100644 --- a/iap/src/test/java/com/example/iap/BuildAndVerifyIapRequestIT.java +++ b/iap/src/test/java/com/example/iap/BuildAndVerifyIapRequestIT.java @@ -33,9 +33,9 @@ import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -//CHECKSTYLE OFF: AbbreviationAsWordInName +// CHECKSTYLE OFF: AbbreviationAsWordInName public class BuildAndVerifyIapRequestIT { - //CHECKSTYLE ON: AbbreviationAsWordInName + // CHECKSTYLE ON: AbbreviationAsWordInName // Update these fields to reflect your IAP protected App Engine credentials private static Long IAP_PROJECT_NUMBER = 320431926067L; diff --git a/iot/api-client/codelab/manager/pom.xml b/iot/api-client/codelab/manager/pom.xml index 36eb8376ad9..4e10e54c07e 100644 --- a/iot/api-client/codelab/manager/pom.xml +++ b/iot/api-client/codelab/manager/pom.xml @@ -30,11 +30,11 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 - 8 - 8 + 1.8 + 1.8 @@ -52,7 +52,7 @@ org.json json - 20190722 + 20200518 io.jsonwebtoken @@ -67,23 +67,17 @@ com.google.apis google-api-services-cloudiot - v1-rev20200414-1.30.9 + v1-rev20200519-1.30.9 com.google.cloud google-cloud-pubsub - 1.105.1 + 1.107.0 - com.google.oauth-client com.google.cloud google-cloud-core - 1.93.0 - - - com.google.oauth-client - google-oauth-client - 1.30.6 + 1.93.6 com.google.guava diff --git a/iot/api-client/codelab/manager/src/main/java/com/example/cloud/iot/examples/MqttCommandsDemo.java b/iot/api-client/codelab/manager/src/main/java/com/example/cloud/iot/examples/MqttCommandsDemo.java index 9fb00613f3b..30abca8937d 100644 --- a/iot/api-client/codelab/manager/src/main/java/com/example/cloud/iot/examples/MqttCommandsDemo.java +++ b/iot/api-client/codelab/manager/src/main/java/com/example/cloud/iot/examples/MqttCommandsDemo.java @@ -16,19 +16,6 @@ package com.example.cloud.iot.examples; -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; -import com.google.api.client.http.HttpRequestInitializer; -import com.google.api.client.json.JsonFactory; -import com.google.api.client.json.jackson2.JacksonFactory; -import com.google.api.services.cloudiot.v1.CloudIot; -import com.google.api.services.cloudiot.v1.CloudIotScopes; -import com.google.api.services.cloudiot.v1.model.Device; -import com.google.api.services.cloudiot.v1.model.DeviceCredential; -import com.google.api.services.cloudiot.v1.model.DeviceRegistry; -import com.google.api.services.cloudiot.v1.model.EventNotificationConfig; -import com.google.api.services.cloudiot.v1.model.PublicKeyCredential; -import com.google.common.base.Charsets; import com.googlecode.lanterna.Symbols; import com.googlecode.lanterna.TerminalPosition; import com.googlecode.lanterna.TerminalSize; @@ -38,28 +25,20 @@ import com.googlecode.lanterna.input.KeyStroke; import com.googlecode.lanterna.input.KeyType; import com.googlecode.lanterna.screen.Screen; -import com.googlecode.lanterna.screen.TabBehaviour; import com.googlecode.lanterna.screen.TerminalScreen; import com.googlecode.lanterna.terminal.DefaultTerminalFactory; import com.googlecode.lanterna.terminal.Terminal; import io.jsonwebtoken.JwtBuilder; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; - -import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; -import java.security.GeneralSecurityException; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; import java.util.Properties; - import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; import org.eclipse.paho.client.mqttv3.MqttCallback; import org.eclipse.paho.client.mqttv3.MqttClient; @@ -72,14 +51,12 @@ import org.json.JSONException; import org.json.JSONObject; - public class MqttCommandsDemo { static MqttCallback mCallback; static Thread mGUIthread; static final String APP_NAME = "MqttCommandsDemo"; - /** Create a Cloud IoT Core JWT for the given project id, signed with the given RSA key. */ private static String createJwtRsa(String projectId, String privateKeyFile) throws NoSuchAlgorithmException, IOException, InvalidKeySpecException { @@ -121,7 +98,6 @@ private static String createJwtEs(String projectId, String privateKeyFile) return jwtBuilder.signWith(SignatureAlgorithm.ES256, kf.generatePrivate(spec)).compact(); } - /** Attaches the callback used when configuration changes occur. */ public static void attachCallback(MqttClient client, String deviceId, Screen mainScreen) throws MqttException { @@ -153,7 +129,6 @@ public void messageArrived(String topic, MqttMessage message) throws Exception { // [end command respond code] } - } @Override @@ -202,7 +177,7 @@ TextColor getColor(String col) { public boolean isValidColor(JSONObject data, TextColor mainBgColor) throws JSONException { return data.get("color") instanceof String - && !mainBgColor.toColor().equals(getColor((String) data.get("color"))); + && !mainBgColor.toColor().equals(getColor((String) data.get("color"))); } }; @@ -217,10 +192,17 @@ public boolean isValidColor(JSONObject data, TextColor mainBgColor) throws JSONE client.setCallback(mCallback); } - public static void mqttDeviceDemo(String projectId, String cloudRegion, String registryId, - String deviceId, String privateKeyFile, String algorithm, - String mqttBridgeHostname, short mqttBridgePort, - String messageType, int waitTime) + public static void mqttDeviceDemo( + String projectId, + String cloudRegion, + String registryId, + String deviceId, + String privateKeyFile, + String algorithm, + String mqttBridgeHostname, + short mqttBridgePort, + String messageType, + int waitTime) throws NoSuchAlgorithmException, IOException, InvalidKeySpecException, MqttException, InterruptedException { @@ -254,11 +236,9 @@ public static void mqttDeviceDemo(String projectId, String cloudRegion, String r DateTime iat = new DateTime(); if (algorithm.equals("RS256")) { - connectOptions.setPassword( - createJwtRsa(projectId, privateKeyFile).toCharArray()); + connectOptions.setPassword(createJwtRsa(projectId, privateKeyFile).toCharArray()); } else if (algorithm.equals("ES256")) { - connectOptions.setPassword( - createJwtEs(projectId, privateKeyFile).toCharArray()); + connectOptions.setPassword(createJwtEs(projectId, privateKeyFile).toCharArray()); } else { throw new IllegalArgumentException( "Invalid algorithm " + algorithm + ". Should be one of 'RS256' or 'ES256'."); @@ -482,7 +462,6 @@ was done in the call to startScreen() screen.stopScreen(); } - public static boolean isJsonValid(String data) { try { new JSONObject(data); @@ -505,9 +484,16 @@ public static void main(String[] args) throws Exception { System.exit(1); } System.out.println("Starting mqtt demo:"); - mqttDeviceDemo(options.projectId, options.cloudRegion, options.registryId, options.deviceId, - options.privateKeyFile, options.algorithm, options.mqttBridgeHostname, - options.mqttBridgePort,"state", options.waitTime); - + mqttDeviceDemo( + options.projectId, + options.cloudRegion, + options.registryId, + options.deviceId, + options.privateKeyFile, + options.algorithm, + options.mqttBridgeHostname, + options.mqttBridgePort, + "state", + options.waitTime); } } diff --git a/iot/api-client/codelab/manager/src/main/java/com/example/cloud/iot/examples/RetryHttpInitializerWrapper.java b/iot/api-client/codelab/manager/src/main/java/com/example/cloud/iot/examples/RetryHttpInitializerWrapper.java index 47bb8d3e269..d565dcd41a3 100644 --- a/iot/api-client/codelab/manager/src/main/java/com/example/cloud/iot/examples/RetryHttpInitializerWrapper.java +++ b/iot/api-client/codelab/manager/src/main/java/com/example/cloud/iot/examples/RetryHttpInitializerWrapper.java @@ -26,7 +26,6 @@ import com.google.api.client.util.ExponentialBackOff; import com.google.api.client.util.Sleeper; import com.google.common.base.Preconditions; - import java.io.IOException; import java.util.logging.Logger; diff --git a/iot/api-client/end-to-end-example/pom.xml b/iot/api-client/end-to-end-example/pom.xml index 5aa3bc3eeb2..5b0cdb60d14 100644 --- a/iot/api-client/end-to-end-example/pom.xml +++ b/iot/api-client/end-to-end-example/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 1.8 @@ -43,7 +43,7 @@ org.json json - 20190722 + 20200518 io.jsonwebtoken @@ -58,17 +58,17 @@ com.google.apis google-api-services-cloudiot - v1-rev20200414-1.30.9 + v1-rev20200519-1.30.9 com.google.cloud google-cloud-pubsub - 1.105.1 + 1.107.0 - com.google.oauth-client - google-oauth-client - 1.30.6 + com.google.auth + google-auth-library-oauth2-http + 0.21.0 com.google.guava @@ -118,4 +118,4 @@ - \ No newline at end of file + diff --git a/iot/api-client/end-to-end-example/src/main/java/com/example/cloud/iot/endtoend/CloudiotPubsubExampleMqttDevice.java b/iot/api-client/end-to-end-example/src/main/java/com/example/cloud/iot/endtoend/CloudiotPubsubExampleMqttDevice.java index cf7ee6b5fff..1ba5398ef27 100644 --- a/iot/api-client/end-to-end-example/src/main/java/com/example/cloud/iot/endtoend/CloudiotPubsubExampleMqttDevice.java +++ b/iot/api-client/end-to-end-example/src/main/java/com/example/cloud/iot/endtoend/CloudiotPubsubExampleMqttDevice.java @@ -41,18 +41,18 @@ * turned on, its temperature decreases by one degree per second, and when the device's fan is * turned off, its temperature increases by one degree per second. * - *

Every second, the device publishes its temperature reading to Google Cloud IoT Core. The + * Every second, the device publishes its temperature reading to Google Cloud IoT Core. The * server meanwhile receives these temperature readings, and decides whether to re-configure the * device to turn its fan on or off. The server will instruct the device to turn the fan on when the * device's temperature exceeds 10 degrees, and to turn it off when the device's temperature is less * than 0 degrees. In a real system, one could use the cloud to compute the optimal thresholds for * turning on and off the fan, but for illustrative purposes we use a simple threshold model. * - *

To connect the device you must have downloaded Google's CA root certificates, and a copy of + * To connect the device you must have downloaded Google's CA root certificates, and a copy of * your private key file. See cloud.google.com/iot for instructions on how to do this. Run this * script with the corresponding algorithm flag. * - *

+ * * $ mvn clean compile assembly:single * * $ mvn exec:java \ @@ -64,7 +64,7 @@ * -algorithm=RS256|ES256" * * - *

With a single server, you can run multiple instances of the device with different device ids, + * With a single server, you can run multiple instances of the device with different device ids, * and the server will distinguish them. Try creating a few devices and running them all at the same * time. */ diff --git a/iot/api-client/end-to-end-example/src/main/java/com/example/cloud/iot/endtoend/CloudiotPubsubExampleServer.java b/iot/api-client/end-to-end-example/src/main/java/com/example/cloud/iot/endtoend/CloudiotPubsubExampleServer.java index 3a6f2a6eef8..903284ae3ec 100644 --- a/iot/api-client/end-to-end-example/src/main/java/com/example/cloud/iot/endtoend/CloudiotPubsubExampleServer.java +++ b/iot/api-client/end-to-end-example/src/main/java/com/example/cloud/iot/endtoend/CloudiotPubsubExampleServer.java @@ -16,7 +16,6 @@ package com.example.cloud.iot.endtoend; -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.json.JsonFactory; @@ -28,6 +27,8 @@ import com.google.api.services.cloudiot.v1.model.EventNotificationConfig; import com.google.api.services.cloudiot.v1.model.GatewayConfig; import com.google.api.services.cloudiot.v1.model.ModifyCloudToDeviceConfigRequest; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.pubsub.v1.AckReplyConsumer; import com.google.cloud.pubsub.v1.MessageReceiver; import com.google.cloud.pubsub.v1.Subscriber; @@ -46,16 +47,16 @@ /** * Sample server that pushes configuration to Google Cloud IoT devices. * - *

This example represents a server that consumes telemetry data from multiple Cloud IoT devices. + * This example represents a server that consumes telemetry data from multiple Cloud IoT devices. * The devices report telemetry data, which the server consumes from a Cloud Pub/Sub topic. The * server then decides whether to turn on or off individual devices fans. * - *

If you are running this example from a Compute Engine VM, you will have to enable the Cloud + * If you are running this example from a Compute Engine VM, you will have to enable the Cloud * Pub/Sub API for your project, which you can do from the Cloud Console. Create a pubsub topic, for * example projects/my-project-id/topics/my-topic-name, and a subscription, for example * projects/my-project-id/subscriptions/my-topic-subscription. * - *

You can then run the example with + * You can then run the example with * $ mvn clean compile assembly:single * * $ mvn exec:java \ @@ -73,10 +74,10 @@ public class CloudiotPubsubExampleServer { /** Represents the state of the server. */ public CloudiotPubsubExampleServer() throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); this.service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -87,10 +88,10 @@ public CloudiotPubsubExampleServer() throws GeneralSecurityException, IOExceptio public static void createRegistry( String cloudRegion, String projectId, String registryName, String pubsubTopicPath) throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -115,10 +116,10 @@ public static void createRegistry( /** Delete this registry from Cloud IoT. */ public static void deleteRegistry(String cloudRegion, String projectId, String registryName) throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -136,10 +137,10 @@ public static void deleteRegistry(String cloudRegion, String projectId, String r public static void deleteDevice( String deviceId, String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -159,10 +160,10 @@ public static void createDevice( String projectId, String cloudRegion, String registryName, String deviceId) throws GeneralSecurityException, IOException { // [START create_device] - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) diff --git a/iot/api-client/manager/pom.xml b/iot/api-client/manager/pom.xml index 0179b7299b0..f22fa3cadb4 100644 --- a/iot/api-client/manager/pom.xml +++ b/iot/api-client/manager/pom.xml @@ -30,7 +30,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -47,7 +47,7 @@ org.json json - 20190722 + 20200518 io.jsonwebtoken @@ -62,22 +62,22 @@ com.google.apis google-api-services-cloudiot - v1-rev20200414-1.30.9 + v1-rev20200519-1.30.9 com.google.cloud google-cloud-pubsub - 1.105.1 + 1.107.0 com.google.cloud google-cloud-core - 1.93.4 + 1.93.6 - com.google.oauth-client - google-oauth-client - 1.30.6 + com.google.auth + google-auth-library-oauth2-http + 0.21.0 com.google.guava @@ -164,7 +164,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 @@ -210,7 +210,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 @@ -252,7 +252,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 diff --git a/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/DeviceRegistryExample.java b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/DeviceRegistryExample.java index f0a82def619..88709af29d2 100644 --- a/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/DeviceRegistryExample.java +++ b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/DeviceRegistryExample.java @@ -16,7 +16,6 @@ package com.example.cloud.iot.examples; -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.json.JsonFactory; @@ -39,10 +38,11 @@ import com.google.api.services.cloudiot.v1.model.ModifyCloudToDeviceConfigRequest; import com.google.api.services.cloudiot.v1.model.PublicKeyCredential; import com.google.api.services.cloudiot.v1.model.SendCommandToDeviceRequest; -import com.google.api.services.cloudiot.v1.model.SendCommandToDeviceResponse; import com.google.api.services.cloudiot.v1.model.SetIamPolicyRequest; import com.google.api.services.cloudiot.v1.model.UnbindDeviceFromGatewayRequest; import com.google.api.services.cloudiot.v1.model.UnbindDeviceFromGatewayResponse; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.Role; import com.google.cloud.pubsub.v1.TopicAdminClient; import com.google.common.io.Files; @@ -55,29 +55,29 @@ import java.nio.file.Paths; import java.security.GeneralSecurityException; import java.util.ArrayList; -import java.util.Arrays; import java.util.Base64; import java.util.Collections; import java.util.List; import org.apache.commons.cli.HelpFormatter; + /** * Example of using Cloud IoT device manager API to administer devices, registries and projects. * - *

This example uses the Device Manager API to create, retrieve, disable, list and delete Cloud + * This example uses the Device Manager API to create, retrieve, disable, list and delete Cloud * IoT devices and registries, using both RSA and eliptic curve keys for authentication. * - *

To start, follow the instructions on the Developer Guide at cloud.google.com/iot to create a + * To start, follow the instructions on the Developer Guide at cloud.google.com/iot to create a * service_account.json file and Cloud Pub/Sub topic as discussed in the guide. You will then need * to point to the service_account.json file as described in * https://developers.google.com/identity/protocols/application-default-credentials#howtheywork * - *

Before running the example, we have to create private and public keys, as described in + * Before running the example, we have to create private and public keys, as described in * cloud.google.com/iot. Since we are interacting with the device manager, we will only use the * public keys. The private keys are used to sign JWTs to authenticate devices. See the MQTT * client example for more information. * - *

Finally, compile and run the example with: + * Finally, compile and run the example with: * *

  * 
@@ -90,7 +90,6 @@
  * 
  * 
*/ - public class DeviceRegistryExample { static final String APP_NAME = "DeviceRegistryExample"; @@ -105,14 +104,15 @@ protected static Topic createIotTopic(String projectId, String topicId) throws E final String topicString = topicName.toString(); // add role -> members binding // create updated policy - topicAdminClient.setIamPolicy(topicString, - com.google.iam.v1.Policy.newBuilder( - topicAdminClient.getIamPolicy(topicString)) - .addBindings( - Binding.newBuilder() - .addMembers("serviceAccount:cloud-iot@system.gserviceaccount.com") - .setRole(Role.owner().toString()) - .build()).build()); + topicAdminClient.setIamPolicy( + topicString, + com.google.iam.v1.Policy.newBuilder(topicAdminClient.getIamPolicy(topicString)) + .addBindings( + Binding.newBuilder() + .addMembers("serviceAccount:cloud-iot@system.gserviceaccount.com") + .setRole(Role.owner().toString()) + .build()) + .build()); System.out.println("Setup topic / policy for: " + topic.getName()); return topic; @@ -124,10 +124,10 @@ protected static Topic createIotTopic(String projectId, String topicId) throws E protected static void createRegistry( String cloudRegion, String projectId, String registryName, String pubsubTopicPath) throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -154,10 +154,10 @@ protected static void createRegistry( /** Delete this registry from Cloud IoT. */ protected static void deleteRegistry(String cloudRegion, String projectId, String registryName) throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -165,7 +165,7 @@ protected static void deleteRegistry(String cloudRegion, String projectId, Strin final String registryPath = String.format( - "projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName); + "projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName); System.out.println("Deleting: " + registryPath); service.projects().locations().registries().delete(registryPath).execute(); @@ -174,15 +174,15 @@ protected static void deleteRegistry(String cloudRegion, String projectId, Strin /** * clearRegistry - *
    - *
  • Registries can't be deleted if they contain devices,
  • - *
  • Gateways (a type of device) can't be deleted if they have bound devices
  • - *
  • Devices can't be deleted if bound to gateways...
  • - *
- * To completely remove a registry, you must unbind all devices from gateways, - * then remove all devices in a registry before removing the registry. - * As pseudocode: - * + * + *
    + *
  • Registries can't be deleted if they contain devices, + *
  • Gateways (a type of device) can't be deleted if they have bound devices + *
  • Devices can't be deleted if bound to gateways... + *
+ * + * To completely remove a registry, you must unbind all devices from gateways, then remove all + * devices in a registry before removing the registry. As pseudocode: * ForAll gateways * ForAll devicesBoundToGateway * unbindDeviceFromGateway @@ -194,10 +194,10 @@ protected static void deleteRegistry(String cloudRegion, String projectId, Strin // [START iot_clear_registry] protected static void clearRegistry(String cloudRegion, String projectId, String registryName) throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -206,16 +206,11 @@ protected static void clearRegistry(String cloudRegion, String projectId, String String.format( "projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName); - CloudIot.Projects.Locations.Registries regAlias = - service.projects().locations().registries(); - CloudIot.Projects.Locations.Registries.Devices devAlias = - regAlias.devices(); + CloudIot.Projects.Locations.Registries regAlias = service.projects().locations().registries(); + CloudIot.Projects.Locations.Registries.Devices devAlias = regAlias.devices(); ListDevicesResponse listGatewaysRes = - devAlias - .list(registryPath) - .setGatewayListOptionsGatewayType("GATEWAY") - .execute(); + devAlias.list(registryPath).setGatewayListOptionsGatewayType("GATEWAY").execute(); List gateways = listGatewaysRes.getDevices(); // Unbind all devices from all gateways @@ -242,9 +237,7 @@ protected static void clearRegistry(String cloudRegion, String projectId, String UnbindDeviceFromGatewayRequest request = new UnbindDeviceFromGatewayRequest(); request.setDeviceId(deviceId); request.setGatewayId(gatewayId); - regAlias - .unbindDeviceFromGateway(registryPath, request) - .execute(); + regAlias.unbindDeviceFromGateway(registryPath, request).execute(); } } else { System.out.println("Gateway has no bound devices."); @@ -253,37 +246,30 @@ protected static void clearRegistry(String cloudRegion, String projectId, String } // Remove all devices from the regsitry - List devices = - devAlias - .list(registryPath) - .execute() - .getDevices(); + List devices = devAlias.list(registryPath).execute().getDevices(); if (devices != null) { System.out.println("Found " + devices.size() + " devices"); for (Device d : devices) { String deviceId = d.getId(); - String devicePath = String.format( - "%s/devices/%s", registryPath, deviceId); + String devicePath = String.format("%s/devices/%s", registryPath, deviceId); service.projects().locations().registries().devices().delete(devicePath).execute(); } } // Delete the registry service.projects().locations().registries().delete(registryPath).execute(); - } // [END iot_clear_registry] - // [START iot_list_devices] /** Print all of the devices in this registry to standard out. */ protected static void listDevices(String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -328,10 +314,10 @@ protected static void createDeviceWithEs256( String cloudRegion, String registryName) throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -376,10 +362,10 @@ protected static void createDeviceWithRs256( String cloudRegion, String registryName) throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -418,16 +404,16 @@ protected static void createDeviceWithRs256( /** * Create a device that has no credentials. * - *

This is a valid way to construct a device, however until it is patched with a credential the + * This is a valid way to construct a device, however until it is patched with a credential the * device will not be able to connect to Cloud IoT. */ protected static void createDeviceWithNoAuth( String deviceId, String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -458,10 +444,10 @@ protected static void createDeviceWithNoAuth( protected static void deleteDevice( String deviceId, String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -482,10 +468,10 @@ protected static void deleteDevice( protected static Device getDevice( String deviceId, String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -506,10 +492,10 @@ protected static Device getDevice( protected static List getDeviceStates( String deviceId, String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -534,10 +520,10 @@ protected static List getDeviceStates( protected static DeviceRegistry getRegistry( String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -556,10 +542,10 @@ protected static DeviceRegistry getRegistry( protected static void listDeviceConfigs( String deviceId, String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -594,10 +580,10 @@ protected static void listDeviceConfigs( /** Lists all of the registries associated with the given project. */ protected static void listRegistries(String projectId, String cloudRegion) throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -639,10 +625,10 @@ protected static void patchEs256ForAuth( String cloudRegion, String registryName) throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -687,10 +673,10 @@ protected static void patchRsa256ForAuth( String cloudRegion, String registryName) throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -736,10 +722,10 @@ protected static void setDeviceConfiguration( String data, long version) throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -775,10 +761,10 @@ protected static void setDeviceConfiguration( /** Retrieves IAM permissions for the given registry. */ protected static void getIamPermissions(String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -817,10 +803,10 @@ protected static void getIamPermissions(String projectId, String cloudRegion, St protected static void setIamPermissions( String projectId, String cloudRegion, String registryName, String member, String role) throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -886,10 +872,10 @@ protected static void setIamPermissions( protected static void sendCommand( String deviceId, String projectId, String cloudRegion, String registryName, String data) throws GeneralSecurityException, IOException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -926,10 +912,10 @@ protected static void bindDeviceToGateway( // [START iot_bind_device_to_gateway] createDevice(projectId, cloudRegion, registryName, deviceId); - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -959,10 +945,10 @@ protected static void unbindDeviceFromGateway( String projectId, String cloudRegion, String registryName, String deviceId, String gatewayId) throws GeneralSecurityException, IOException { // [START iot_unbind_device_from_gateway] - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -993,10 +979,10 @@ protected static void createDevice( String projectId, String cloudRegion, String registryName, String deviceId) throws GeneralSecurityException, IOException { // [START iot_create_device] - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -1060,10 +1046,10 @@ protected static void createGateway( String algorithm) throws GeneralSecurityException, IOException { // [START iot_create_gateway] - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -1112,10 +1098,10 @@ protected static void createGateway( protected static void listGateways(String projectId, String cloudRegion, String registryName) throws IOException, GeneralSecurityException { // [START iot_list_gateways] - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -1157,10 +1143,10 @@ protected static void listDevicesForGateway( String projectId, String cloudRegion, String registryName, String gatewayId) throws IOException, GeneralSecurityException { // [START iot_list_devices_for_gateway] - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) .setApplicationName(APP_NAME) @@ -1200,8 +1186,11 @@ protected static void mainCreate(DeviceRegistryExampleOptions options) throws Ex } else if ("create-es".equals(options.command)) { System.out.println("Create ES Device:"); createDeviceWithEs256( - options.deviceId, options.ecPublicKeyFile, options.projectId, options.cloudRegion, - options.registryName); + options.deviceId, + options.ecPublicKeyFile, + options.projectId, + options.cloudRegion, + options.registryName); } else if ("create-rsa".equals(options.command)) { System.out.println("Create RSA Device:"); createDeviceWithRs256( @@ -1213,7 +1202,7 @@ protected static void mainCreate(DeviceRegistryExampleOptions options) throws Ex } else if ("create-unauth".equals(options.command)) { System.out.println("Create Unauth Device"); createDeviceWithNoAuth( - options.deviceId, options.projectId, options.cloudRegion, options.registryName); + options.deviceId, options.projectId, options.cloudRegion, options.registryName); } else if ("create-registry".equals(options.command)) { System.out.println("Create registry"); createRegistry( @@ -1228,8 +1217,12 @@ protected static void mainCreate(DeviceRegistryExampleOptions options) throws Ex } createGateway( - options.projectId, options.cloudRegion, options.registryName, options.gatewayId, - certificateFilePath, algorithm); + options.projectId, + options.cloudRegion, + options.registryName, + options.gatewayId, + certificateFilePath, + algorithm); } } @@ -1237,8 +1230,7 @@ protected static void mainGet(DeviceRegistryExampleOptions options) throws Excep if ("get-device".equals(options.command)) { System.out.println("Get device"); System.out.println( - getDevice( - options.deviceId, options.projectId, options.cloudRegion, options.registryName) + getDevice(options.deviceId, options.projectId, options.cloudRegion, options.registryName) .toPrettyString()); } else if ("get-iam-permissions".equals(options.command)) { System.out.println("Get iam permissions"); @@ -1253,8 +1245,7 @@ protected static void mainGet(DeviceRegistryExampleOptions options) throws Excep } } else if ("get-registry".equals(options.command)) { System.out.println("Get registry"); - System.out.println( - getRegistry(options.projectId, options.cloudRegion, options.registryName)); + System.out.println(getRegistry(options.projectId, options.cloudRegion, options.registryName)); } } @@ -1279,8 +1270,7 @@ public static void main(String[] args) throws Exception { clearRegistry(options.cloudRegion, options.projectId, options.registryName); } else if ("delete-device".equals(options.command)) { System.out.println("Delete device"); - deleteDevice( - options.deviceId, options.projectId, options.cloudRegion, options.registryName); + deleteDevice(options.deviceId, options.projectId, options.cloudRegion, options.registryName); } else if ("delete-registry".equals(options.command)) { System.out.println("Delete registry"); deleteRegistry(options.cloudRegion, options.projectId, options.registryName); @@ -1334,12 +1324,18 @@ public static void main(String[] args) throws Exception { } else if ("bind-device-to-gateway".equals(options.command)) { System.out.println("Bind device to gateway:"); bindDeviceToGateway( - options.projectId, options.cloudRegion, options.registryName, options.deviceId, + options.projectId, + options.cloudRegion, + options.registryName, + options.deviceId, options.gatewayId); } else if ("unbind-device-from-gateway".equals(options.command)) { System.out.println("Unbind device from gateway:"); unbindDeviceFromGateway( - options.projectId, options.cloudRegion, options.registryName, options.deviceId, + options.projectId, + options.cloudRegion, + options.registryName, + options.deviceId, options.gatewayId); } else if ("list-gateways".equals(options.command)) { System.out.println("Listing gateways: "); diff --git a/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/DeviceRegistryExampleOptions.java b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/DeviceRegistryExampleOptions.java index 63ee05ff8aa..71d367dbf68 100644 --- a/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/DeviceRegistryExampleOptions.java +++ b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/DeviceRegistryExampleOptions.java @@ -16,14 +16,12 @@ package com.example.cloud.iot.examples; -import java.util.Arrays; import javax.annotation.Nullable; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.DefaultParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Option; -import org.apache.commons.cli.Option.Builder; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; @@ -47,10 +45,10 @@ public class DeviceRegistryExampleOptions { long version = 0; /** Construct an DeviceRegistryExampleOptions class from command line flags. */ - public static @Nullable DeviceRegistryExampleOptions fromFlags(String...args) { + public static @Nullable DeviceRegistryExampleOptions fromFlags(String... args) { // Required arguments options.addOption( - Option.builder() + Option.builder() .type(String.class) .longOpt("command") .hasArg() @@ -255,8 +253,7 @@ public class DeviceRegistryExampleOptions { String footer = "\nhttps://cloud.google.com/iot-core"; HelpFormatter formatter = new HelpFormatter(); - formatter.printHelp( - "DeviceRegistryExample", header, options, footer, true); + formatter.printHelp("DeviceRegistryExample", header, options, footer, true); System.err.println(e.getMessage()); return null; diff --git a/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExample.java b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExample.java index 97fafd68534..b4ba15bd669 100644 --- a/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExample.java +++ b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExample.java @@ -39,8 +39,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; -import java.net.ProtocolException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; @@ -50,17 +48,18 @@ import org.joda.time.DateTime; import org.json.JSONException; import org.json.JSONObject; + // [END iot_http_includes] /** * Java sample of connecting to Google Cloud IoT Core vice via HTTP, using JWT. * - *

This example connects to Google Cloud IoT Core via HTTP Bridge, using a JWT for device + * This example connects to Google Cloud IoT Core via HTTP Bridge, using a JWT for device * authentication. After connecting, by default the device publishes 100 messages at a rate of one * per second, and then exits. You can change The behavior to set state instead of events by using * flag -message_type to 'state'. * - *

To run this example, follow the instructions in the README located in the sample's parent + * To run this example, follow the instructions in the README located in the sample's parent * folder. */ public class HttpExample { @@ -110,8 +109,14 @@ private static String createJwtEs(String projectId, String privateKeyFile) throw // [START iot_http_getconfig] /** Publish an event or state message using Cloud IoT Core via the HTTP API. */ - protected static void getConfig(String urlPath, String token, String projectId, - String cloudRegion, String registryId, String deviceId, String version) + protected static void getConfig( + String urlPath, + String token, + String projectId, + String cloudRegion, + String registryId, + String deviceId, + String version) throws IOException { // Build the resource path of the device that is going to be authenticated. String devicePath = @@ -121,12 +126,13 @@ protected static void getConfig(String urlPath, String token, String projectId, urlPath = urlPath + devicePath + "/config?local_version=" + version; HttpRequestFactory requestFactory = - HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() { - @Override - public void initialize(HttpRequest request) { - request.setParser(new JsonObjectParser(JSON_FACTORY)); - } - }); + HTTP_TRANSPORT.createRequestFactory( + new HttpRequestInitializer() { + @Override + public void initialize(HttpRequest request) { + request.setParser(new JsonObjectParser(JSON_FACTORY)); + } + }); final HttpRequest req = requestFactory.buildGetRequest(new GenericUrl(urlPath)); HttpHeaders heads = new HttpHeaders(); @@ -136,13 +142,14 @@ public void initialize(HttpRequest request) { heads.setCacheControl("no-cache"); req.setHeaders(heads); - ExponentialBackOff backoff = new ExponentialBackOff.Builder() - .setInitialIntervalMillis(500) - .setMaxElapsedTimeMillis(900000) - .setMaxIntervalMillis(6000) - .setMultiplier(1.5) - .setRandomizationFactor(0.5) - .build(); + ExponentialBackOff backoff = + new ExponentialBackOff.Builder() + .setInitialIntervalMillis(500) + .setMaxElapsedTimeMillis(900000) + .setMaxIntervalMillis(6000) + .setMultiplier(1.5) + .setRandomizationFactor(0.5) + .build(); req.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(backoff)); HttpResponse res = req.execute(); System.out.println(res.getStatusCode()); @@ -155,8 +162,15 @@ public void initialize(HttpRequest request) { // [START iot_http_publish] /** Publish an event or state message using Cloud IoT Core via the HTTP API. */ - protected static void publishMessage(String payload, String urlPath, String messageType, - String token, String projectId, String cloudRegion, String registryId, String deviceId) + protected static void publishMessage( + String payload, + String urlPath, + String messageType, + String token, + String projectId, + String cloudRegion, + String registryId, + String deviceId) throws IOException, JSONException { // Build the resource path of the device that is going to be authenticated. String devicePath = @@ -172,14 +186,14 @@ protected static void publishMessage(String payload, String urlPath, String mess urlPath = urlPath + devicePath + ":" + urlSuffix; - final HttpRequestFactory requestFactory = - HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() { - @Override - public void initialize(HttpRequest request) { - request.setParser(new JsonObjectParser(JSON_FACTORY)); - } - }); + HTTP_TRANSPORT.createRequestFactory( + new HttpRequestInitializer() { + @Override + public void initialize(HttpRequest request) { + request.setParser(new JsonObjectParser(JSON_FACTORY)); + } + }); HttpHeaders heads = new HttpHeaders(); heads.setAuthorization(String.format("Bearer %s", token)); @@ -196,20 +210,22 @@ public void initialize(HttpRequest request) { data.put("state", state); } - ByteArrayContent content = new ByteArrayContent( - "application/json", data.toString().getBytes(StandardCharsets.UTF_8.name())); + ByteArrayContent content = + new ByteArrayContent( + "application/json", data.toString().getBytes(StandardCharsets.UTF_8.name())); final HttpRequest req = requestFactory.buildGetRequest(new GenericUrl(urlPath)); req.setHeaders(heads); req.setContent(content); req.setRequestMethod("POST"); - ExponentialBackOff backoff = new ExponentialBackOff.Builder() - .setInitialIntervalMillis(500) - .setMaxElapsedTimeMillis(900000) - .setMaxIntervalMillis(6000) - .setMultiplier(1.5) - .setRandomizationFactor(0.5) - .build(); + ExponentialBackOff backoff = + new ExponentialBackOff.Builder() + .setInitialIntervalMillis(500) + .setMaxElapsedTimeMillis(900000) + .setMaxIntervalMillis(6000) + .setMultiplier(1.5) + .setRandomizationFactor(0.5) + .build(); req.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(backoff)); HttpResponse res = req.execute(); @@ -244,8 +260,14 @@ protected static void main(String[] args) throws Exception { System.out.format("Using URL: '%s'%n", urlPath); // Show the latest configuration - getConfig(urlPath, token, options.projectId, options.cloudRegion, options.registryId, - options.deviceId, "0"); + getConfig( + urlPath, + token, + options.projectId, + options.cloudRegion, + options.registryId, + options.deviceId, + "0"); // Publish numMessages messages to the HTTP bridge. for (int i = 1; i <= options.numMessages; ++i) { @@ -267,8 +289,15 @@ protected static void main(String[] args) throws Exception { } } - publishMessage(payload, urlPath, options.messageType, token, options.projectId, - options.cloudRegion, options.registryId, options.deviceId); + publishMessage( + payload, + urlPath, + options.messageType, + token, + options.projectId, + options.cloudRegion, + options.registryId, + options.deviceId); if ("event".equals(options.messageType)) { // Frequently send event payloads (every second) diff --git a/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExampleOptions.java b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExampleOptions.java index f1b309df855..0ab17364e18 100644 --- a/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExampleOptions.java +++ b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/HttpExampleOptions.java @@ -21,11 +21,9 @@ import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.DefaultParser; import org.apache.commons.cli.Option; -import org.apache.commons.cli.Option.Builder; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; - /** Command line options for the HTTP example. */ public class HttpExampleOptions { static final Options options = new Options(); @@ -42,7 +40,7 @@ public class HttpExampleOptions { String messageType = "event"; /** Construct an HttpExampleOptions class from command line flags. */ - public static @Nullable HttpExampleOptions fromFlags(String...args) { + public static @Nullable HttpExampleOptions fromFlags(String... args) { // Required arguments options.addOption( Option.builder() diff --git a/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/MqttExample.java b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/MqttExample.java index fff88777113..e79bf6333cb 100644 --- a/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/MqttExample.java +++ b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/MqttExample.java @@ -39,20 +39,21 @@ import org.eclipse.paho.client.mqttv3.MqttMessage; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; import org.joda.time.DateTime; + // [END iot_mqtt_includes] /** * Java sample of connecting to Google Cloud IoT Core vice via MQTT, using JWT. * - *

This example connects to Google Cloud IoT Core via MQTT, using a JWT for device + * This example connects to Google Cloud IoT Core via MQTT, using a JWT for device * authentication. After connecting, by default the device publishes 100 messages to the device's * MQTT topic at a rate of one per second, and then exits. To set state instead of publishing * telemetry events, set the `-message_type` flag to `state.` * - *

To run this example, first create your credentials and register your device as described in + * To run this example, first create your credentials and register your device as described in * the README located in the sample's parent folder. * - *

After you have registered your device and generated your credentials, compile and run with the + * After you have registered your device and generated your credentials, compile and run with the * corresponding algorithm flag, for example: * *

@@ -178,7 +179,7 @@ protected static MqttClient startMqtt(
     long retryIntervalMs = initialConnectIntervalMillis;
     long totalRetryTimeMs = 0;
 
-    while ((totalRetryTimeMs < maxConnectRetryTimeElapsedMillis) && !client.isConnected())  {
+    while ((totalRetryTimeMs < maxConnectRetryTimeElapsedMillis) && !client.isConnected()) {
       try {
         client.connect(connectOptions);
       } catch (MqttException e) {
diff --git a/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/MqttExampleOptions.java b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/MqttExampleOptions.java
index f043cfff6f8..cb7683e9524 100644
--- a/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/MqttExampleOptions.java
+++ b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/MqttExampleOptions.java
@@ -21,11 +21,9 @@
 import org.apache.commons.cli.CommandLineParser;
 import org.apache.commons.cli.DefaultParser;
 import org.apache.commons.cli.Option;
-import org.apache.commons.cli.Option.Builder;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
 
-
 /** Command line options for the MQTT example. */
 public class MqttExampleOptions {
   static final Options options = new Options();
@@ -47,7 +45,7 @@ public class MqttExampleOptions {
   int waitTime = 120;
 
   /** Construct an MqttExampleOptions class from command line flags. */
-  public static @Nullable MqttExampleOptions fromFlags(String...args) {
+  public static @Nullable MqttExampleOptions fromFlags(String... args) {
     // Required arguments
     options.addOption(
         Option.builder()
diff --git a/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/RetryHttpInitializerWrapper.java b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/RetryHttpInitializerWrapper.java
index 7f905ab6233..dcbb6df53e5 100644
--- a/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/RetryHttpInitializerWrapper.java
+++ b/iot/api-client/manager/src/main/java/com/example/cloud/iot/examples/RetryHttpInitializerWrapper.java
@@ -90,7 +90,7 @@ public boolean handleResponse(
               return true;
             } else if (backoffHandler.handleResponse(request, response, supportsRetry)) {
               // Otherwise, we defer to the judgment of our internal backoff handler.
-              LOG.info(request.getUrl().toString().replaceAll("[\r\n]",""));
+              LOG.info(request.getUrl().toString().replaceAll("[\r\n]", ""));
               return true;
             } else {
               return false;
diff --git a/iot/api-client/manager/src/test/java/com/example/cloud/iot/examples/ManagerIT.java b/iot/api-client/manager/src/test/java/com/example/cloud/iot/examples/ManagerIT.java
index db8b2a47c8b..15d2fcedf23 100644
--- a/iot/api-client/manager/src/test/java/com/example/cloud/iot/examples/ManagerIT.java
+++ b/iot/api-client/manager/src/test/java/com/example/cloud/iot/examples/ManagerIT.java
@@ -16,7 +16,6 @@
 
 package com.example.cloud.iot.examples;
 
-import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
 import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
 import com.google.api.client.http.HttpRequestInitializer;
 import com.google.api.client.json.JsonFactory;
@@ -24,14 +23,13 @@
 import com.google.api.services.cloudiot.v1.CloudIot;
 import com.google.api.services.cloudiot.v1.CloudIotScopes;
 import com.google.api.services.cloudiot.v1.model.DeviceRegistry;
+import com.google.auth.http.HttpCredentialsAdapter;
+import com.google.auth.oauth2.GoogleCredentials;
 import com.google.cloud.pubsub.v1.TopicAdminClient;
 import com.google.pubsub.v1.ProjectTopicName;
 import com.google.pubsub.v1.Topic;
 import java.io.ByteArrayOutputStream;
-import java.io.OutputStreamWriter;
 import java.io.PrintStream;
-import java.io.Writer;
-import java.lang.StringBuilder;
 import java.nio.charset.StandardCharsets;
 import java.util.List;
 import org.eclipse.paho.client.mqttv3.MqttClient;
@@ -43,7 +41,6 @@
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
 
-
 /** Tests for iot "Management" sample. */
 @RunWith(JUnit4.class)
 @SuppressWarnings("checkstyle:abbreviationaswordinname")
@@ -82,10 +79,10 @@ public void tearDown() throws Exception {
   }
 
   public void clearTestRegistries() throws Exception {
-    GoogleCredential credential =
-        GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
+    GoogleCredentials credential =
+        GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all());
     JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
-    HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
+    HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
     final CloudIot service =
         new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init)
             .setApplicationName("TEST")
@@ -107,8 +104,8 @@ public void clearTestRegistries() throws Exception {
         String registryId = r.getId();
         if (registryId.startsWith("java-reg-")) {
           long currSecs = System.currentTimeMillis() / 1000L;
-          long regSecs = Long.parseLong(registryId.substring(
-              "java-reg-".length(), registryId.length()));
+          long regSecs =
+              Long.parseLong(registryId.substring("java-reg-".length(), registryId.length()));
           long diffSecs = currSecs - regSecs;
           if (diffSecs > (60 * 60 * 24 * 7 * 10)) { // tests from last week or older
             System.out.println("Remove Id: " + r.getId());
diff --git a/jobs/v3/pom.xml b/jobs/v3/pom.xml
index ace406afd73..c868a0ff100 100644
--- a/jobs/v3/pom.xml
+++ b/jobs/v3/pom.xml
@@ -15,7 +15,7 @@
   
     com.google.cloud.samples
     shared-configuration
-    1.0.17
+    1.0.18
   
 
   
@@ -32,7 +32,12 @@
     
       com.google.apis
       google-api-services-jobs
-      v3-rev20200506-1.30.9
+      v3-rev20200604-1.30.9
+    
+    
+      com.google.auth
+      google-auth-library-oauth2-http
+      0.21.0
     
     
 
@@ -49,4 +54,4 @@
       4.13
     
   
-
\ No newline at end of file
+
diff --git a/jobs/v3/src/main/java/com/google/samples/AutoCompleteSample.java b/jobs/v3/src/main/java/com/google/samples/AutoCompleteSample.java
index 202cefaa6fd..fe282629097 100644
--- a/jobs/v3/src/main/java/com/google/samples/AutoCompleteSample.java
+++ b/jobs/v3/src/main/java/com/google/samples/AutoCompleteSample.java
@@ -35,16 +35,13 @@ public final class AutoCompleteSample {
   private static final String DEFAULT_PROJECT_ID =
       "projects/" + System.getenv("GOOGLE_CLOUD_PROJECT");
 
-  private static CloudTalentSolution talentSolutionClient = JobServiceQuickstart
-      .getTalentSolutionClient();
+  private static CloudTalentSolution talentSolutionClient =
+      JobServiceQuickstart.getTalentSolutionClient();
 
-  //[START auto_complete_job_title]
+  // [START auto_complete_job_title]
 
-  /**
-   * Auto completes job titles within given companyName.
-   */
-  public static void jobTitleAutoComplete(String companyName, String query)
-      throws IOException {
+  /** Auto completes job titles within given companyName. */
+  public static void jobTitleAutoComplete(String companyName, String query) throws IOException {
 
     Complete complete =
         talentSolutionClient
@@ -64,11 +61,8 @@ public static void jobTitleAutoComplete(String companyName, String query)
   }
   // [END auto_complete_default]
 
-  /**
-   * Auto completes job titles within given companyName.
-   */
-  public static void defaultAutoComplete(String companyName, String query)
-      throws IOException {
+  /** Auto completes job titles within given companyName. */
+  public static void defaultAutoComplete(String companyName, String query) throws IOException {
     Complete complete =
         talentSolutionClient
             .projects()
@@ -90,8 +84,8 @@ public static void main(String... args) throws Exception {
     Company companyToBeCreated = BasicCompanySample.generateCompany().setDisplayName("Google");
     String companyName = BasicCompanySample.createCompany(companyToBeCreated).getName();
 
-    Job jobToBeCreated = BasicJobSample.generateJobWithRequiredFields(companyName)
-        .setTitle("Software engineer");
+    Job jobToBeCreated =
+        BasicJobSample.generateJobWithRequiredFields(companyName).setTitle("Software engineer");
     final String jobName = BasicJobSample.createJob(jobToBeCreated).getName();
 
     // Wait several seconds for post processing
diff --git a/jobs/v3/src/main/java/com/google/samples/BasicCompanySample.java b/jobs/v3/src/main/java/com/google/samples/BasicCompanySample.java
index c68071518aa..052c82dd541 100644
--- a/jobs/v3/src/main/java/com/google/samples/BasicCompanySample.java
+++ b/jobs/v3/src/main/java/com/google/samples/BasicCompanySample.java
@@ -43,18 +43,15 @@ public final class BasicCompanySample {
   private static final String DEFAULT_PROJECT_ID =
       "projects/" + System.getenv("GOOGLE_CLOUD_PROJECT");
 
-  private static CloudTalentSolution talentSolutionClient = JobServiceQuickstart
-      .getTalentSolutionClient();
+  private static CloudTalentSolution talentSolutionClient =
+      JobServiceQuickstart.getTalentSolutionClient();
 
   // [START basic_company]
 
-  /**
-   * Generate a company
-   */
+  /** Generate a company */
   public static Company generateCompany() {
     // distributor company id should be a unique Id in your system.
-    String companyName =
-        "company:" + String.valueOf(new Random().nextLong());
+    String companyName = "company:" + String.valueOf(new Random().nextLong());
 
     Company company =
         new Company()
@@ -68,15 +65,14 @@ public static Company generateCompany() {
 
   // [START create_company]
 
-  /**
-   * Create a company.
-   */
+  /** Create a company. */
   public static Company createCompany(Company companyToBeCreated) throws IOException {
     try {
       CreateCompanyRequest createCompanyRequest =
           new CreateCompanyRequest().setCompany(companyToBeCreated);
       Company companyCreated =
-          talentSolutionClient.projects()
+          talentSolutionClient
+              .projects()
               .companies()
               .create(DEFAULT_PROJECT_ID, createCompanyRequest)
               .execute();
@@ -91,9 +87,7 @@ public static Company createCompany(Company companyToBeCreated) throws IOExcepti
 
   // [START get_company]
 
-  /**
-   * Get a company.
-   */
+  /** Get a company. */
   public static Company getCompany(String companyName) throws IOException {
     try {
       Company companyExisted =
@@ -109,9 +103,7 @@ public static Company getCompany(String companyName) throws IOException {
 
   // [START update_company]
 
-  /**
-   * Updates a company.
-   */
+  /** Updates a company. */
   public static Company updateCompany(String companyName, Company companyToBeUpdated)
       throws IOException {
     try {
@@ -136,12 +128,9 @@ public static Company updateCompany(String companyName, Company companyToBeUpdat
 
   // [START update_company_with_field_mask]
 
-  /**
-   * Updates a company.
-   */
-  public static Company updateCompanyWithFieldMask(String companyName, String fieldMask,
-      Company companyToBeUpdated)
-      throws IOException {
+  /** Updates a company. */
+  public static Company updateCompanyWithFieldMask(
+      String companyName, String fieldMask, Company companyToBeUpdated) throws IOException {
     try {
       // String foo = String.format("?updateCompanyFields=%s",fieldMask);
       UpdateCompanyRequest updateCompanyRequest =
@@ -165,9 +154,7 @@ public static Company updateCompanyWithFieldMask(String companyName, String fiel
 
   // [START delete_company]
 
-  /**
-   * Delete a company.
-   */
+  /** Delete a company. */
   public static void deleteCompany(String companyName) throws IOException {
     try {
       talentSolutionClient.projects().companies().delete(companyName).execute();
@@ -191,14 +178,14 @@ public static void main(String... args) throws Exception {
     getCompany(companyName);
 
     // Update a company
-    Company companyToBeUpdated = companyCreated
-        .setCareerSiteUri("https://elgoog.im/");
+    Company companyToBeUpdated = companyCreated.setCareerSiteUri("https://elgoog.im/");
     updateCompany(companyName, companyToBeUpdated);
 
     // Update a company with field mask
-    updateCompanyWithFieldMask(companyName, "displayName",
-        new Company().setDisplayName("changedTitle")
-            .setName(companyCreated.getName()));
+    updateCompanyWithFieldMask(
+        companyName,
+        "displayName",
+        new Company().setDisplayName("changedTitle").setName(companyCreated.getName()));
 
     // Delete a company
     deleteCompany(companyName);
diff --git a/jobs/v3/src/main/java/com/google/samples/BasicJobSample.java b/jobs/v3/src/main/java/com/google/samples/BasicJobSample.java
index 50fb7d2790b..a7266098bd8 100644
--- a/jobs/v3/src/main/java/com/google/samples/BasicJobSample.java
+++ b/jobs/v3/src/main/java/com/google/samples/BasicJobSample.java
@@ -46,18 +46,15 @@ public final class BasicJobSample {
   private static final String DEFAULT_PROJECT_ID =
       "projects/" + System.getenv("GOOGLE_CLOUD_PROJECT");
 
-  private static CloudTalentSolution talentSolutionClient = JobServiceQuickstart
-      .getTalentSolutionClient();
+  private static CloudTalentSolution talentSolutionClient =
+      JobServiceQuickstart.getTalentSolutionClient();
 
   // [START basic_job]
 
-  /**
-   * Generate a basic job with given companyName.
-   */
+  /** Generate a basic job with given companyName. */
   public static Job generateJobWithRequiredFields(String companyName) {
     // requisition id should be a unique Id in your system.
-    String requisitionId =
-        "jobWithRequiredFields:" + String.valueOf(new Random().nextLong());
+    String requisitionId = "jobWithRequiredFields:" + String.valueOf(new Random().nextLong());
     ApplicationInfo applicationInfo =
         new ApplicationInfo().setUris(Arrays.asList("http://careers.google.com"));
 
@@ -67,8 +64,7 @@ public static Job generateJobWithRequiredFields(String companyName) {
             .setTitle("Software Engineer")
             .setCompanyName(companyName)
             .setApplicationInfo(applicationInfo)
-            .setDescription(
-                "Design, develop, test, deploy, maintain and improve software.");
+            .setDescription("Design, develop, test, deploy, maintain and improve software.");
     System.out.println("Job generated: " + job);
     return job;
   }
@@ -76,16 +72,17 @@ public static Job generateJobWithRequiredFields(String companyName) {
 
   // [START create_job]
 
-  /**
-   * Create a job.
-   */
+  /** Create a job. */
   public static Job createJob(Job jobToBeCreated) throws IOException {
     try {
-      CreateJobRequest createJobRequest =
-          new CreateJobRequest().setJob(jobToBeCreated);
-
-      Job jobCreated = talentSolutionClient.projects().jobs()
-          .create(DEFAULT_PROJECT_ID, createJobRequest).execute();
+      CreateJobRequest createJobRequest = new CreateJobRequest().setJob(jobToBeCreated);
+
+      Job jobCreated =
+          talentSolutionClient
+              .projects()
+              .jobs()
+              .create(DEFAULT_PROJECT_ID, createJobRequest)
+              .execute();
       System.out.println("Job created: " + jobCreated);
       return jobCreated;
     } catch (IOException e) {
@@ -97,9 +94,7 @@ public static Job createJob(Job jobToBeCreated) throws IOException {
 
   // [START get_job]
 
-  /**
-   * Get a job.
-   */
+  /** Get a job. */
   public static Job getJob(String jobName) throws IOException {
     try {
       Job jobExisted = talentSolutionClient.projects().jobs().get(jobName).execute();
@@ -114,14 +109,10 @@ public static Job getJob(String jobName) throws IOException {
 
   // [START update_job]
 
-  /**
-   * Update a job.
-   */
-  public static Job updateJob(String jobName, Job jobToBeUpdated)
-      throws IOException {
+  /** Update a job. */
+  public static Job updateJob(String jobName, Job jobToBeUpdated) throws IOException {
     try {
-      UpdateJobRequest updateJobRequest =
-          new UpdateJobRequest().setJob(jobToBeUpdated);
+      UpdateJobRequest updateJobRequest = new UpdateJobRequest().setJob(jobToBeUpdated);
       Job jobUpdated =
           talentSolutionClient.projects().jobs().patch(jobName, updateJobRequest).execute();
       System.out.println("Job updated: " + jobUpdated);
@@ -136,9 +127,7 @@ public static Job updateJob(String jobName, Job jobToBeUpdated)
 
   // [START update_job_with_field_mask]
 
-  /**
-   * Update a job.
-   */
+  /** Update a job. */
   public static Job updateJobWithFieldMask(String jobName, String fieldMask, Job jobToBeUpdated)
       throws IOException {
     try {
@@ -157,9 +146,7 @@ public static Job updateJobWithFieldMask(String jobName, String fieldMask, Job j
 
   // [START delete_job]
 
-  /**
-   * Delete a job.
-   */
+  /** Delete a job. */
   public static void deleteJob(String jobName) throws IOException {
     try {
       talentSolutionClient.projects().jobs().delete(jobName).execute();
diff --git a/jobs/v3/src/main/java/com/google/samples/BatchOperationSample.java b/jobs/v3/src/main/java/com/google/samples/BatchOperationSample.java
index 62aa6bf8a5c..e9461a512a4 100644
--- a/jobs/v3/src/main/java/com/google/samples/BatchOperationSample.java
+++ b/jobs/v3/src/main/java/com/google/samples/BatchOperationSample.java
@@ -41,16 +41,16 @@
  *
  * - Delete job within batch.
  *
- * For simplicity, the samples always use the same kind of requests in each batch. In a real case ,
- * you might put different kinds of request in one batch.
+ * For simplicity, the samples always use the same kind of requests in each batch. In a real case
+ * , you might put different kinds of request in one batch.
  */
 public final class BatchOperationSample {
 
   private static final String DEFAULT_PROJECT_ID =
       "projects/" + System.getenv("GOOGLE_CLOUD_PROJECT");
 
-  private static CloudTalentSolution talentSolutionClient = JobServiceQuickstart
-      .getTalentSolutionClient();
+  private static CloudTalentSolution talentSolutionClient =
+      JobServiceQuickstart.getTalentSolutionClient();
 
   // [START batch_job_create]
   public static List batchCreateJobs(String companyName) throws IOException {
@@ -60,8 +60,7 @@ public static List batchCreateJobs(String companyName) throws IOException {
     JsonBatchCallback createCallback =
         new JsonBatchCallback() {
           @Override
-          public void onFailure(GoogleJsonError e,
-              HttpHeaders responseHeaders) {
+          public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) {
             System.out.println("Create Error Message: " + e.getMessage());
           }
 
@@ -81,8 +80,7 @@ public void onSuccess(Job response, HttpHeaders responseHeaders) {
             .setRequisitionId("123456")
             .setTitle("Software Engineer")
             .setApplicationInfo(applicationInfo)
-            .setDescription(
-                "Design, develop, test, deploy, maintain and improve software.");
+            .setDescription("Design, develop, test, deploy, maintain and improve software.");
     Job hardwareEngineerJob =
         new Job()
             .setCompanyName(companyName)
@@ -146,12 +144,13 @@ public void onSuccess(Job job, HttpHeaders responseHeaders) {
     }
     // Or just fill in part of field in Job entity and set the updateJobFields
     for (int i = 1; i < jobsToBeUpdate.size(); i += 2) {
-      Job toBeUpdated = new Job().setTitle("Engineer in Mountain View")
-          .setName(jobsToBeUpdate.get(i).getName());
+      Job toBeUpdated =
+          new Job().setTitle("Engineer in Mountain View").setName(jobsToBeUpdate.get(i).getName());
       talentSolutionClient
           .projects()
           .jobs()
-          .patch(toBeUpdated.getName(),
+          .patch(
+              toBeUpdated.getName(),
               new UpdateJobRequest().setJob(toBeUpdated).setUpdateMask("title"))
           .queue(batchUpdate, updateCallback);
     }
@@ -174,8 +173,7 @@ public static void batchDeleteJobs(List jobsToBeDeleted) throws IOException
               batchDelete,
               new JsonBatchCallback() {
                 @Override
-                public void onFailure(GoogleJsonError e,
-                    HttpHeaders responseHeaders) {
+                public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) {
                   System.out.println("Delete Error Message: " + e.getMessage());
                 }
 
@@ -190,8 +188,7 @@ public void onSuccess(Empty empty, HttpHeaders responseHeaders) {
   // [END batch_job_delete]
 
   public static void main(String... args) throws Exception {
-    Company company = BasicCompanySample
-        .createCompany(BasicCompanySample.generateCompany());
+    Company company = BasicCompanySample.createCompany(BasicCompanySample.generateCompany());
 
     // Batch create jobs
     List createdJobs = batchCreateJobs(company.getName());
diff --git a/jobs/v3/src/main/java/com/google/samples/CommuteSearchSample.java b/jobs/v3/src/main/java/com/google/samples/CommuteSearchSample.java
index fc8e512994c..4410512236a 100644
--- a/jobs/v3/src/main/java/com/google/samples/CommuteSearchSample.java
+++ b/jobs/v3/src/main/java/com/google/samples/CommuteSearchSample.java
@@ -39,8 +39,8 @@ public final class CommuteSearchSample {
   private static final String DEFAULT_PROJECT_ID =
       "projects/" + System.getenv("GOOGLE_CLOUD_PROJECT");
 
-  private static CloudTalentSolution talentSolutionClient = JobServiceQuickstart
-      .getTalentSolutionClient();
+  private static CloudTalentSolution talentSolutionClient =
+      JobServiceQuickstart.getTalentSolutionClient();
 
   // [START commute_search]
 
@@ -62,8 +62,7 @@ public static void commuteSearch(String companyName) throws IOException, Interru
                     .setCommuteMethod("TRANSIT")
                     .setTravelDuration("1000s")
                     .setStartCoordinates(
-                        new LatLng().setLatitude(37.422408)
-                            .setLongitude(-122.085609)));
+                        new LatLng().setLatitude(37.422408).setLongitude(-122.085609)));
     if (companyName != null) {
       jobQuery.setCompanyNames(Arrays.asList(companyName));
     }
@@ -75,7 +74,10 @@ public static void commuteSearch(String companyName) throws IOException, Interru
             .setJobView("JOB_VIEW_FULL")
             .setRequirePreciseResultSize(true);
     SearchJobsResponse response =
-        talentSolutionClient.projects().jobs().search(DEFAULT_PROJECT_ID, searchJobsRequest)
+        talentSolutionClient
+            .projects()
+            .jobs()
+            .search(DEFAULT_PROJECT_ID, searchJobsRequest)
             .execute();
     Thread.sleep(1000);
     System.out.println(response);
@@ -86,8 +88,9 @@ public static void main(String... args) throws Exception {
     Company companyToBeCreated = BasicCompanySample.generateCompany();
     String companyName = BasicCompanySample.createCompany(companyToBeCreated).getName();
 
-    Job jobToBeCreated = BasicJobSample.generateJobWithRequiredFields(companyName)
-        .setAddresses(Arrays.asList("1600 Amphitheatre Parkway, Mountain View, CA 94043"));
+    Job jobToBeCreated =
+        BasicJobSample.generateJobWithRequiredFields(companyName)
+            .setAddresses(Arrays.asList("1600 Amphitheatre Parkway, Mountain View, CA 94043"));
     String jobName = BasicJobSample.createJob(jobToBeCreated).getName();
 
     // Wait several seconds for post processing
diff --git a/jobs/v3/src/main/java/com/google/samples/CustomAttributeSample.java b/jobs/v3/src/main/java/com/google/samples/CustomAttributeSample.java
index cda82933ad8..a1313bf2314 100644
--- a/jobs/v3/src/main/java/com/google/samples/CustomAttributeSample.java
+++ b/jobs/v3/src/main/java/com/google/samples/CustomAttributeSample.java
@@ -31,8 +31,6 @@
 import java.util.Map;
 import java.util.Random;
 
-;
-
 /**
  * This file contains the samples about CustomAttribute, including:
  *
@@ -45,18 +43,15 @@ public final class CustomAttributeSample {
   private static final String DEFAULT_PROJECT_ID =
       "projects/" + System.getenv("GOOGLE_CLOUD_PROJECT");
 
-  private static CloudTalentSolution talentSolutionClient = JobServiceQuickstart
-      .getTalentSolutionClient();
+  private static CloudTalentSolution talentSolutionClient =
+      JobServiceQuickstart.getTalentSolutionClient();
 
   // [START custom_attribute_job]
 
-  /**
-   * Generate a job with a custom attribute.
-   */
+  /** Generate a job with a custom attribute. */
   public static Job generateJobWithACustomAttribute(String companyName) {
     // requisition id should be a unique Id in your system.
-    String requisitionId =
-        "jobWithACustomAttribute:" + String.valueOf(new Random().nextLong());
+    String requisitionId = "jobWithACustomAttribute:" + String.valueOf(new Random().nextLong());
     ApplicationInfo applicationInfo =
         new ApplicationInfo().setUris(Arrays.asList("http://careers.google.com"));
 
@@ -64,12 +59,10 @@ public static Job generateJobWithACustomAttribute(String companyName) {
     Map customAttributes = new HashMap<>();
     customAttributes.put(
         "someFieldName1",
-        new CustomAttribute()
-            .setStringValues(Arrays.asList("value1"))
-            .setFilterable(Boolean.TRUE));
-    customAttributes
-        .put("someFieldName2",
-            new CustomAttribute().setLongValues(Arrays.asList(256L)).setFilterable(true));
+        new CustomAttribute().setStringValues(Arrays.asList("value1")).setFilterable(Boolean.TRUE));
+    customAttributes.put(
+        "someFieldName2",
+        new CustomAttribute().setLongValues(Arrays.asList(256L)).setFilterable(true));
 
     // Creates job with custom attributes
     Job job =
@@ -87,9 +80,7 @@ public static Job generateJobWithACustomAttribute(String companyName) {
 
   // [START custom_attribute_filter_string_value]
 
-  /**
-   * CustomAttributeFilter on String value CustomAttribute
-   */
+  /** CustomAttributeFilter on String value CustomAttribute */
   public static void filtersOnStringValueCustomAttribute()
       throws IOException, InterruptedException {
     // Make sure to set the requestMetadata the same as the associated search request
@@ -100,12 +91,10 @@ public static void filtersOnStringValueCustomAttribute()
             // Make sure to hash the sessionID
             .setSessionId("HashedSessionID")
             // Domain of the website where the search is conducted
-            .setDomain(
-                "www.google.com");
+            .setDomain("www.google.com");
 
     String customAttributeFilter = "NOT EMPTY(someFieldName1)";
-    JobQuery jobQuery = new JobQuery()
-        .setCustomAttributeFilter(customAttributeFilter);
+    JobQuery jobQuery = new JobQuery().setCustomAttributeFilter(customAttributeFilter);
 
     SearchJobsRequest searchJobsRequest =
         new SearchJobsRequest()
@@ -125,9 +114,7 @@ public static void filtersOnStringValueCustomAttribute()
 
   // [START custom_attribute_filter_long_value]
 
-  /**
-   * CustomAttributeFilter on Long value CustomAttribute
-   */
+  /** CustomAttributeFilter on Long value CustomAttribute */
   public static void filtersOnLongValueCustomAttribute() throws IOException, InterruptedException {
     // Make sure to set the requestMetadata the same as the associated search request
     RequestMetadata requestMetadata =
@@ -140,8 +127,7 @@ public static void filtersOnLongValueCustomAttribute() throws IOException, Inter
             .setDomain("www.google.com");
 
     String customAttributeFilter = "(255 <= someFieldName2) AND (someFieldName2 <= 257)";
-    JobQuery jobQuery = new JobQuery()
-        .setCustomAttributeFilter(customAttributeFilter);
+    JobQuery jobQuery = new JobQuery().setCustomAttributeFilter(customAttributeFilter);
 
     SearchJobsRequest searchJobsRequest =
         new SearchJobsRequest()
@@ -162,9 +148,7 @@ public static void filtersOnLongValueCustomAttribute() throws IOException, Inter
 
   // [START custom_attribute_filter_multi_attributes]
 
-  /**
-   * CustomAttributeFilter on multiple CustomAttributes
-   */
+  /** CustomAttributeFilter on multiple CustomAttributes */
   public static void filtersOnMultiCustomAttributes() throws IOException, InterruptedException {
     // Make sure to set the requestMetadata the same as the associated search request
     RequestMetadata requestMetadata =
@@ -174,13 +158,12 @@ public static void filtersOnMultiCustomAttributes() throws IOException, Interrup
             // Make sure to hash the sessionID
             .setSessionId("HashedSessionID")
             // Domain of the website where the search is conducted
-            .setDomain(
-                "www.google.com");
+            .setDomain("www.google.com");
 
-    String customAttributeFilter = "(someFieldName1 = \"value1\") "
-        + "AND ((255 <= someFieldName2) OR (someFieldName2 <= 213))";
-    JobQuery jobQuery = new JobQuery()
-        .setCustomAttributeFilter(customAttributeFilter);
+    String customAttributeFilter =
+        "(someFieldName1 = \"value1\") "
+            + "AND ((255 <= someFieldName2) OR (someFieldName2 <= 213))";
+    JobQuery jobQuery = new JobQuery().setCustomAttributeFilter(customAttributeFilter);
 
     SearchJobsRequest searchJobsRequest =
         new SearchJobsRequest()
diff --git a/jobs/v3/src/main/java/com/google/samples/EmailAlertSearchSample.java b/jobs/v3/src/main/java/com/google/samples/EmailAlertSearchSample.java
index 6d8f3061770..48bb37be531 100644
--- a/jobs/v3/src/main/java/com/google/samples/EmailAlertSearchSample.java
+++ b/jobs/v3/src/main/java/com/google/samples/EmailAlertSearchSample.java
@@ -26,21 +26,17 @@
 import java.io.IOException;
 import java.util.Arrays;
 
-/**
- * The sample in this file introduce how to do a email alert search.
- */
+/** The sample in this file introduce how to do a email alert search. */
 public final class EmailAlertSearchSample {
 
   private static final String DEFAULT_PROJECT_ID =
       "projects/" + System.getenv("GOOGLE_CLOUD_PROJECT");
 
-  private static CloudTalentSolution talentSolutionClient = JobServiceQuickstart
-      .getTalentSolutionClient();
+  private static CloudTalentSolution talentSolutionClient =
+      JobServiceQuickstart.getTalentSolutionClient();
   // [START search_for_alerts]
 
-  /**
-   * Search jobs for alert.
-   */
+  /** Search jobs for alert. */
   public static void searchForAlerts(String companyName) throws IOException, InterruptedException {
     // Make sure to set the requestMetadata the same as the associated search request
     RequestMetadata requestMetadata =
diff --git a/jobs/v3/src/main/java/com/google/samples/FeaturedJobsSearchSample.java b/jobs/v3/src/main/java/com/google/samples/FeaturedJobsSearchSample.java
index e654033db6e..1957c406aea 100644
--- a/jobs/v3/src/main/java/com/google/samples/FeaturedJobsSearchSample.java
+++ b/jobs/v3/src/main/java/com/google/samples/FeaturedJobsSearchSample.java
@@ -31,9 +31,9 @@
 /**
  * The sample in this file introduce featured job, including:
  *
- * 

- Construct a featured job + * - Construct a featured job * - *

- Search featured job + * - Search featured job */ public final class FeaturedJobsSearchSample { diff --git a/jobs/v3/src/main/java/com/google/samples/GeneralSearchSample.java b/jobs/v3/src/main/java/com/google/samples/GeneralSearchSample.java index eb807412f54..3405aa34264 100644 --- a/jobs/v3/src/main/java/com/google/samples/GeneralSearchSample.java +++ b/jobs/v3/src/main/java/com/google/samples/GeneralSearchSample.java @@ -55,14 +55,12 @@ public final class GeneralSearchSample { private static final String DEFAULT_PROJECT_ID = "projects/" + System.getenv("GOOGLE_CLOUD_PROJECT"); - private static CloudTalentSolution talentSolutionClient = JobServiceQuickstart - .getTalentSolutionClient(); + private static CloudTalentSolution talentSolutionClient = + JobServiceQuickstart.getTalentSolutionClient(); - //[START basic_keyword_search] + // [START basic_keyword_search] - /** - * Simple search jobs with keyword. - */ + /** Simple search jobs with keyword. */ public static void basicSearcJobs(String companyName, String query) throws IOException, InterruptedException { // Make sure to set the requestMetadata the same as the associated search request @@ -97,13 +95,11 @@ public static void basicSearcJobs(String companyName, String query) System.out.println(searchJobsResponse); } - //[END basic_keyword_search] + // [END basic_keyword_search] // [START category_filter] - /** - * Search on category filter. - */ + /** Search on category filter. */ public static void categoryFilterSearch(String companyName, List categories) throws IOException, InterruptedException { // Make sure to set the requestMetadata the same as the associated search request @@ -141,9 +137,7 @@ public static void categoryFilterSearch(String companyName, List categor // [START employment_types_filter] - /** - * Search on employment types. - */ + /** Search on employment types. */ public static void employmentTypesSearch(String companyName, List employmentTypes) throws IOException, InterruptedException { // Make sure to set the requestMetadata the same as the associated search request @@ -202,7 +196,7 @@ public static void dateRangeSearch(String companyName, String startTime, String new TimestampRange().setStartTime(startTime).setEndTime(endTime); JobQuery jobQuery = new JobQuery().setPublishTimeRange(timestampRange); - //JobQuery jobQuery = new JobQuery().setPublishTimeRange(dateRange); + // JobQuery jobQuery = new JobQuery().setPublishTimeRange(dateRange); if (companyName != null) { jobQuery.setCompanyNames(Arrays.asList(companyName)); @@ -228,9 +222,7 @@ public static void dateRangeSearch(String companyName, String startTime, String // [START language_code_filter] - /** - * Search on language codes. - */ + /** Search on language codes. */ public static void languageCodeSearch(String companyName, List languageCodes) throws IOException, InterruptedException { // Make sure to set the requestMetadata the same as the associated search request @@ -268,9 +260,7 @@ public static void languageCodeSearch(String companyName, List languageC // [START company_display_name_filter] - /** - * Search on company display name. - */ + /** Search on company display name. */ public static void companyDisplayNameSearch(String companyName, List companyDisplayNames) throws IOException, InterruptedException { // Make sure to set the requestMetadata the same as the associated search request @@ -308,9 +298,7 @@ public static void companyDisplayNameSearch(String companyName, List com // [START compensation_filter] - /** - * Search on compensation. - */ + /** Search on compensation. */ public static void compensationSearch(String companyName) throws IOException, InterruptedException { // Make sure to set the requestMetadata the same as the associated search request @@ -332,8 +320,7 @@ public static void compensationSearch(String companyName) .setUnits(Arrays.asList("HOURLY")) .setRange( new CompensationRange() - .setMaxCompensation(new Money().setCurrencyCode("USD") - .setUnits(15L)) + .setMaxCompensation(new Money().setCurrencyCode("USD").setUnits(15L)) .setMinCompensation( new Money() .setCurrencyCode("USD") @@ -365,25 +352,26 @@ public static void main(String... args) throws Exception { Company companyToBeCreated = BasicCompanySample.generateCompany().setDisplayName("Google"); String companyName = BasicCompanySample.createCompany(companyToBeCreated).getName(); - Job jobToBeCreated = BasicJobSample.generateJobWithRequiredFields(companyName) - .setTitle("Systems Administrator") - .setEmploymentTypes(Arrays.asList("FULL_TIME")) - .setLanguageCode("en-US") - .setCompensationInfo( - new CompensationInfo().setEntries(Arrays.asList( - new CompensationEntry() - .setType("BASE") - .setUnit("HOURLY") - .setAmount(new Money().setCurrencyCode("USD").setUnits(12L))))); + Job jobToBeCreated = + BasicJobSample.generateJobWithRequiredFields(companyName) + .setTitle("Systems Administrator") + .setEmploymentTypes(Arrays.asList("FULL_TIME")) + .setLanguageCode("en-US") + .setCompensationInfo( + new CompensationInfo() + .setEntries( + Arrays.asList( + new CompensationEntry() + .setType("BASE") + .setUnit("HOURLY") + .setAmount(new Money().setCurrencyCode("USD").setUnits(12L))))); final String jobName = BasicJobSample.createJob(jobToBeCreated).getName(); // Wait several seconds for post processing Thread.sleep(10000); basicSearcJobs(companyName, "Systems Administrator"); categoryFilterSearch(companyName, Arrays.asList("COMPUTER_AND_IT")); - dateRangeSearch(companyName, - "1980-01-15T01:30:15.01Z", - "2099-01-15T01:30:15.01Z"); + dateRangeSearch(companyName, "1980-01-15T01:30:15.01Z", "2099-01-15T01:30:15.01Z"); employmentTypesSearch(companyName, Arrays.asList("FULL_TIME", "CONTRACTOR", "PER_DIEM")); companyDisplayNameSearch(companyName, Arrays.asList("Google")); compensationSearch(companyName); @@ -393,4 +381,3 @@ public static void main(String... args) throws Exception { BasicCompanySample.deleteCompany(companyName); } } - diff --git a/jobs/v3/src/main/java/com/google/samples/HistogramSample.java b/jobs/v3/src/main/java/com/google/samples/HistogramSample.java index 5a26b29367f..a8eaebf2764 100644 --- a/jobs/v3/src/main/java/com/google/samples/HistogramSample.java +++ b/jobs/v3/src/main/java/com/google/samples/HistogramSample.java @@ -28,22 +28,18 @@ import java.io.IOException; import java.util.Arrays; -/** - * The sample in this file introduce how to do a histogram search. - */ +/** The sample in this file introduce how to do a histogram search. */ public final class HistogramSample { private static final String DEFAULT_PROJECT_ID = "projects/" + System.getenv("GOOGLE_CLOUD_PROJECT"); - private static CloudTalentSolution talentSolutionClient = JobServiceQuickstart - .getTalentSolutionClient(); + private static CloudTalentSolution talentSolutionClient = + JobServiceQuickstart.getTalentSolutionClient(); // [START histogram_search] - /** - * Histogram search - */ + /** Histogram search */ public static void histogramSearch(String companyName) throws IOException, InterruptedException { // Make sure to set the requestMetadata the same as the associated search request RequestMetadata requestMetadata = @@ -53,8 +49,7 @@ public static void histogramSearch(String companyName) throws IOException, Inter // Make sure to hash the sessionID .setSessionId("HashedSessionID") // Domain of the website where the search is conducted - .setDomain( - "www.google.com"); + .setDomain("www.google.com"); HistogramFacets histogramFacets = new HistogramFacets() diff --git a/jobs/v3/src/main/java/com/google/samples/JobServiceQuickstart.java b/jobs/v3/src/main/java/com/google/samples/JobServiceQuickstart.java index 05836db9367..a50090326a3 100644 --- a/jobs/v3/src/main/java/com/google/samples/JobServiceQuickstart.java +++ b/jobs/v3/src/main/java/com/google/samples/JobServiceQuickstart.java @@ -16,7 +16,6 @@ package com.google.samples; -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; @@ -25,12 +24,12 @@ import com.google.api.services.jobs.v3.CloudTalentSolution; import com.google.api.services.jobs.v3.model.Company; import com.google.api.services.jobs.v3.model.ListCompaniesResponse; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.util.Collections; -/** - * The quickstart for Cloud Job Discovery - */ +/** The quickstart for Cloud Job Discovery */ public class JobServiceQuickstart { // [START quickstart] @@ -41,26 +40,25 @@ public class JobServiceQuickstart { private static final String DEFAULT_PROJECT_ID = "projects/" + System.getenv("GOOGLE_CLOUD_PROJECT"); - private static CloudTalentSolution talentSolutionClient = createTalentSolutionClient( - generateCredential()); + private static CloudTalentSolution talentSolutionClient = + createTalentSolutionClient(generateCredential()); - private static CloudTalentSolution createTalentSolutionClient(GoogleCredential credential) { + private static CloudTalentSolution createTalentSolutionClient(GoogleCredentials credential) { String url = "https://jobs.googleapis.com"; + HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credential); return new CloudTalentSolution.Builder( - NET_HTTP_TRANSPORT, JSON_FACTORY, setHttpTimeout(credential)) + NET_HTTP_TRANSPORT, JSON_FACTORY, setHttpTimeout(requestInitializer)) .setApplicationName("JobServiceClientSamples") .setRootUrl(url) .build(); } - private static GoogleCredential generateCredential() { + private static GoogleCredentials generateCredential() { try { // Credentials could be downloaded after creating service account // set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable, for example: // export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/key.json - return GoogleCredential - .getApplicationDefault(NET_HTTP_TRANSPORT, JSON_FACTORY) - .createScoped(Collections.singleton(SCOPES)); + return GoogleCredentials.getApplicationDefault().createScoped(Collections.singleton(SCOPES)); } catch (Exception e) { System.out.print("Error in generating credential"); throw new RuntimeException(e); @@ -83,9 +81,8 @@ public static CloudTalentSolution getTalentSolutionClient() { public static void main(String... args) throws Exception { try { - ListCompaniesResponse listCompaniesResponse = talentSolutionClient.projects().companies() - .list(DEFAULT_PROJECT_ID) - .execute(); + ListCompaniesResponse listCompaniesResponse = + talentSolutionClient.projects().companies().list(DEFAULT_PROJECT_ID).execute(); System.out.println("Request Id is " + listCompaniesResponse.getMetadata().getRequestId()); if (listCompaniesResponse.getCompanies() != null) { for (Company company : listCompaniesResponse.getCompanies()) { diff --git a/jobs/v3/src/main/java/com/google/samples/LocationSearchSample.java b/jobs/v3/src/main/java/com/google/samples/LocationSearchSample.java index efe81dc330b..f52b2974acd 100644 --- a/jobs/v3/src/main/java/com/google/samples/LocationSearchSample.java +++ b/jobs/v3/src/main/java/com/google/samples/LocationSearchSample.java @@ -45,14 +45,12 @@ public final class LocationSearchSample { private static final String DEFAULT_PROJECT_ID = "projects/" + System.getenv("GOOGLE_CLOUD_PROJECT"); - private static CloudTalentSolution talentSolutionClient = JobServiceQuickstart - .getTalentSolutionClient(); + private static CloudTalentSolution talentSolutionClient = + JobServiceQuickstart.getTalentSolutionClient(); // [START basic_location_search] - /** - * Basic location Search - */ + /** Basic location Search */ public static void basicLocationSearch(String companyName, String location, double distance) throws IOException, InterruptedException { // Make sure to set the requestMetadata the same as the associated search request @@ -65,10 +63,8 @@ public static void basicLocationSearch(String companyName, String location, doub // Domain of the website where the search is conducted .setDomain("www.google.com"); LocationFilter locationFilter = - new LocationFilter().setAddress(location) - .setDistanceInMiles(distance); - JobQuery jobQuery = new JobQuery() - .setLocationFilters(Arrays.asList(locationFilter)); + new LocationFilter().setAddress(location).setDistanceInMiles(distance); + JobQuery jobQuery = new JobQuery().setLocationFilters(Arrays.asList(locationFilter)); if (companyName != null) { jobQuery.setCompanyNames(Arrays.asList(companyName)); } @@ -86,11 +82,9 @@ public static void basicLocationSearch(String companyName, String location, doub // [START keyword_location_search] - /** - * Keyword location Search - */ - public static void keywordLocationSearch(String companyName, String location, double distance, - String keyword) + /** Keyword location Search */ + public static void keywordLocationSearch( + String companyName, String location, double distance, String keyword) throws IOException, InterruptedException { // Make sure to set the requestMetadata the same as the associated search request RequestMetadata requestMetadata = @@ -102,12 +96,9 @@ public static void keywordLocationSearch(String companyName, String location, do // Domain of the website where the search is conducted .setDomain("www.google.com"); LocationFilter locationFilter = - new LocationFilter().setAddress(location) - .setDistanceInMiles(distance); + new LocationFilter().setAddress(location).setDistanceInMiles(distance); JobQuery jobQuery = - new JobQuery() - .setQuery(keyword) - .setLocationFilters(Arrays.asList(locationFilter)); + new JobQuery().setQuery(keyword).setLocationFilters(Arrays.asList(locationFilter)); if (companyName != null) { jobQuery.setCompanyNames(Arrays.asList(companyName)); } @@ -125,9 +116,7 @@ public static void keywordLocationSearch(String companyName, String location, do // [START city_location_search] - /** - * City location Search - */ + /** City location Search */ public static void cityLocationSearch(String companyName, String location) throws IOException, InterruptedException { // Make sure to set the requestMetadata the same as the associated search request @@ -139,10 +128,8 @@ public static void cityLocationSearch(String companyName, String location) .setSessionId("HashedSessionID") // Domain of the website where the search is conducted .setDomain("www.google.com"); - LocationFilter locationFilter = new LocationFilter() - .setAddress(location); - JobQuery jobQuery = new JobQuery() - .setLocationFilters(Arrays.asList(locationFilter)); + LocationFilter locationFilter = new LocationFilter().setAddress(location); + JobQuery jobQuery = new JobQuery().setLocationFilters(Arrays.asList(locationFilter)); if (companyName != null) { jobQuery.setCompanyNames(Arrays.asList(companyName)); } @@ -160,11 +147,9 @@ public static void cityLocationSearch(String companyName, String location) // [START multi_locations_search] - /** - * Multiple locations Search - */ - public static void multiLocationsSearch(String companyName, String location1, double distance1, - String location2) + /** Multiple locations Search */ + public static void multiLocationsSearch( + String companyName, String location1, double distance1, String location2) throws IOException, InterruptedException { // Make sure to set the requestMetadata the same as the associated search request RequestMetadata requestMetadata = @@ -198,9 +183,7 @@ public static void multiLocationsSearch(String companyName, String location1, do // [START broadening_location_search] - /** - * Broadening location Search - */ + /** Broadening location Search */ public static void broadeningLocationsSearch(String companyName, String location) throws IOException, InterruptedException { // Make sure to set the requestMetadata the same as the associated search request @@ -213,9 +196,7 @@ public static void broadeningLocationsSearch(String companyName, String location // Domain of the website where the search is conducted .setDomain("www.google.com"); JobQuery jobQuery = - new JobQuery() - .setLocationFilters(Arrays - .asList(new LocationFilter().setAddress(location))); + new JobQuery().setLocationFilters(Arrays.asList(new LocationFilter().setAddress(location))); if (companyName != null) { jobQuery.setCompanyNames(Arrays.asList(companyName)); } @@ -241,11 +222,15 @@ public static void main(String... args) throws Exception { Company companyToBeCreated = BasicCompanySample.generateCompany(); String companyName = BasicCompanySample.createCompany(companyToBeCreated).getName(); - Job jobToBeCreated = BasicJobSample.generateJobWithRequiredFields(companyName) - .setAddresses(Arrays.asList(location)).setTitle(keyword); + Job jobToBeCreated = + BasicJobSample.generateJobWithRequiredFields(companyName) + .setAddresses(Arrays.asList(location)) + .setTitle(keyword); final String jobName = BasicJobSample.createJob(jobToBeCreated).getName(); - Job jobToBeCreated2 = BasicJobSample.generateJobWithRequiredFields(companyName) - .setAddresses(Arrays.asList(location2)).setTitle(keyword); + Job jobToBeCreated2 = + BasicJobSample.generateJobWithRequiredFields(companyName) + .setAddresses(Arrays.asList(location2)) + .setTitle(keyword); final String jobName2 = BasicJobSample.createJob(jobToBeCreated2).getName(); // Wait several seconds for post processing diff --git a/jobs/v3/src/test/java/SampleTests.java b/jobs/v3/src/test/java/SampleTests.java index 149a3a7fc77..9a53a3aac9e 100644 --- a/jobs/v3/src/test/java/SampleTests.java +++ b/jobs/v3/src/test/java/SampleTests.java @@ -104,7 +104,6 @@ public void batchOperationSampleTest() throws Exception { public void commuteSearchSampleTest() throws Exception { CommuteSearchSample.main(); String result = bout.toString(); - assertThat(result).containsMatch(".*matchingJobs.*commuteInfo.*"); assertThat(result).containsMatch(".*matchingJobs.*1600 Amphitheatre Pkwy.*"); bout.reset(); } @@ -169,7 +168,7 @@ public void jobServiceQuickStartTest() throws Exception { assertThat(bout.toString()).contains("Request Id is"); bout.reset(); } - + @Test public void locationSearchSampleTest() throws Exception { LocationSearchSample.main(); diff --git a/jobs/v4/pom.xml b/jobs/v4/pom.xml index d193d2ce329..210c2066a14 100644 --- a/jobs/v4/pom.xml +++ b/jobs/v4/pom.xml @@ -15,7 +15,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -24,7 +24,7 @@ - 3.5 + 3.5.4 @@ -32,7 +32,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -50,7 +50,7 @@ com.google.apis google-api-services-jobs - v3-rev20200506-1.30.9 + v3-rev20200604-1.30.9 diff --git a/jobs/v4/src/main/java/com/example/jobs/CommuteSearchJobs.java b/jobs/v4/src/main/java/com/example/jobs/CommuteSearchJobs.java index 2dad6176bcb..e8e8e04b094 100644 --- a/jobs/v4/src/main/java/com/example/jobs/CommuteSearchJobs.java +++ b/jobs/v4/src/main/java/com/example/jobs/CommuteSearchJobs.java @@ -42,6 +42,9 @@ public static void searchJobs() throws IOException { // Search Jobs with histogram queries. public static void searchJobs(String projectId, String tenantId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (JobServiceClient jobServiceClient = JobServiceClient.create()) { TenantName parent = TenantName.of(projectId, tenantId); String domain = "www.example.com"; @@ -80,11 +83,11 @@ public static void searchJobs(String projectId, String tenantId) throws IOExcept for (SearchJobsResponse.MatchingJob responseItem : jobServiceClient.searchJobs(request).iterateAll()) { - System.out.printf("Job summary: %s\n", responseItem.getJobSummary()); - System.out.printf("Job title snippet: %s\n", responseItem.getJobTitleSnippet()); + System.out.format("Job summary: %s%n", responseItem.getJobSummary()); + System.out.format("Job title snippet: %s%n", responseItem.getJobTitleSnippet()); Job job = responseItem.getJob(); - System.out.printf("Job name: %s\n", job.getName()); - System.out.printf("Job title: %s\n", job.getTitle()); + System.out.format("Job name: %s%n", job.getName()); + System.out.format("Job title: %s%n", job.getTitle()); } } } diff --git a/jobs/v4/src/main/java/com/example/jobs/CustomRankingSearchJobs.java b/jobs/v4/src/main/java/com/example/jobs/CustomRankingSearchJobs.java index dac2f276832..94899b282f4 100644 --- a/jobs/v4/src/main/java/com/example/jobs/CustomRankingSearchJobs.java +++ b/jobs/v4/src/main/java/com/example/jobs/CustomRankingSearchJobs.java @@ -37,6 +37,9 @@ public static void searchCustomRankingJobs() throws IOException { // Search Jobs using custom rankings. public static void searchCustomRankingJobs(String projectId, String tenantId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (JobServiceClient jobServiceClient = JobServiceClient.create()) { TenantName parent = TenantName.of(projectId, tenantId); String domain = "www.example.com"; @@ -66,11 +69,11 @@ public static void searchCustomRankingJobs(String projectId, String tenantId) th .build(); for (SearchJobsResponse.MatchingJob responseItem : jobServiceClient.searchJobs(request).iterateAll()) { - System.out.printf("Job summary: %s\n", responseItem.getJobSummary()); - System.out.printf("Job title snippet: %s\n", responseItem.getJobTitleSnippet()); + System.out.format("Job summary: %s%n", responseItem.getJobSummary()); + System.out.format("Job title snippet: %s%n", responseItem.getJobTitleSnippet()); Job job = responseItem.getJob(); - System.out.printf("Job name: %s\n", job.getName()); - System.out.printf("Job title: %s\n", job.getTitle()); + System.out.format("Job name: %s%n", job.getName()); + System.out.format("Job title: %s%n", job.getTitle()); } } } diff --git a/jobs/v4/src/main/java/com/example/jobs/HistogramSearchJobs.java b/jobs/v4/src/main/java/com/example/jobs/HistogramSearchJobs.java index 0ec97a2b957..57c34123392 100644 --- a/jobs/v4/src/main/java/com/example/jobs/HistogramSearchJobs.java +++ b/jobs/v4/src/main/java/com/example/jobs/HistogramSearchJobs.java @@ -40,6 +40,9 @@ public static void searchJobs() throws IOException { // Search Jobs with histogram queries. public static void searchJobs(String projectId, String tenantId, String query) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (JobServiceClient jobServiceClient = JobServiceClient.create()) { TenantName parent = TenantName.of(projectId, tenantId); @@ -63,11 +66,11 @@ public static void searchJobs(String projectId, String tenantId, String query) for (SearchJobsResponse.MatchingJob responseItem : jobServiceClient.searchJobs(request).iterateAll()) { - System.out.printf("Job summary: %s\n", responseItem.getJobSummary()); - System.out.printf("Job title snippet: %s\n", responseItem.getJobTitleSnippet()); + System.out.format("Job summary: %s%n", responseItem.getJobSummary()); + System.out.format("Job title snippet: %s%n", responseItem.getJobTitleSnippet()); Job job = responseItem.getJob(); - System.out.printf("Job name: %s\n", job.getName()); - System.out.printf("Job title: %s\n", job.getTitle()); + System.out.format("Job name: %s%n", job.getName()); + System.out.format("Job title: %s%n", job.getTitle()); } } } diff --git a/jobs/v4/src/main/java/com/example/jobs/JobSearchAutoCompleteJobTitle.java b/jobs/v4/src/main/java/com/example/jobs/JobSearchAutoCompleteJobTitle.java index 675f32a0002..842d77a05d1 100644 --- a/jobs/v4/src/main/java/com/example/jobs/JobSearchAutoCompleteJobTitle.java +++ b/jobs/v4/src/main/java/com/example/jobs/JobSearchAutoCompleteJobTitle.java @@ -37,6 +37,9 @@ public static void completeQuery() throws IOException { // Complete job title given partial text (autocomplete). public static void completeQuery(String projectId, String tenantId, String query) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (CompletionClient completionClient = CompletionClient.create()) { TenantName parent = TenantName.of(projectId, tenantId); CompleteQueryRequest request = @@ -48,9 +51,9 @@ public static void completeQuery(String projectId, String tenantId, String query .build(); CompleteQueryResponse response = completionClient.completeQuery(request); for (CompleteQueryResponse.CompletionResult result : response.getCompletionResultsList()) { - System.out.printf("Suggested title: %s\n", result.getSuggestion()); + System.out.format("Suggested title: %s%n", result.getSuggestion()); // Suggestion type is JOB_TITLE or COMPANY_TITLE - System.out.printf("Suggestion type: %s\n", result.getType()); + System.out.format("Suggestion type: %s%n", result.getType()); } } } diff --git a/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateClientEvent.java b/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateClientEvent.java index 5df585cf7e5..d7c835cf147 100644 --- a/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateClientEvent.java +++ b/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateClientEvent.java @@ -42,6 +42,9 @@ public static void createClientEvent() throws IOException { // Creates a client event. public static void createClientEvent( String projectId, String tenantId, String requestId, String eventId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (EventServiceClient eventServiceClient = EventServiceClient.create()) { TenantName parent = TenantName.of(projectId, tenantId); diff --git a/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateCompany.java b/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateCompany.java index f823259e3b9..baa890a51d9 100644 --- a/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateCompany.java +++ b/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateCompany.java @@ -38,6 +38,9 @@ public static void createCompany() throws IOException { // Create a company. public static void createCompany( String projectId, String tenantId, String displayName, String externalId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (CompanyServiceClient companyServiceClient = CompanyServiceClient.create()) { TenantName parent = TenantName.of(projectId, tenantId); Company company = @@ -51,9 +54,9 @@ public static void createCompany( Company response = companyServiceClient.createCompany(request); System.out.println("Created Company"); - System.out.printf("Name: %s\n", response.getName()); - System.out.printf("Display Name: %s\n", response.getDisplayName()); - System.out.printf("External ID: %s\n", response.getExternalId()); + System.out.format("Name: %s%n", response.getName()); + System.out.format("Display Name: %s%n", response.getDisplayName()); + System.out.format("External ID: %s%n", response.getExternalId()); } } } diff --git a/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateJob.java b/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateJob.java index e7652c5b8ce..ecbc0e9433e 100644 --- a/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateJob.java +++ b/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateJob.java @@ -23,8 +23,13 @@ import com.google.cloud.talent.v4beta1.JobServiceClient; import com.google.cloud.talent.v4beta1.TenantName; import java.io.IOException; +import java.sql.Timestamp; +import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.Arrays; +import java.util.Calendar; import java.util.List; +import java.util.Locale; public class JobSearchCreateJob { @@ -46,14 +51,21 @@ public static void createJob( String requisitionId, String jobApplicationUrl) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (JobServiceClient jobServiceClient = JobServiceClient.create()) { TenantName parent = TenantName.of(projectId, tenantId); Job.ApplicationInfo applicationInfo = Job.ApplicationInfo.newBuilder().addUris(jobApplicationUrl).build(); - List addresses = Arrays.asList("1600 Amphitheatre Parkway, Mountain View, CA 94043", + List addresses = + Arrays.asList( + "1600 Amphitheatre Parkway, Mountain View, CA 94043", "111 8th Avenue, New York, NY 10011"); + // By default, job will expire in 30 days. + // https://cloud.google.com/talent-solution/job-search/docs/jobs Job job = Job.newBuilder() .setCompany(companyId) @@ -69,7 +81,7 @@ public static void createJob( CreateJobRequest.newBuilder().setParent(parent.toString()).setJob(job).build(); Job response = jobServiceClient.createJob(request); - System.out.printf("Created job: %s\n", response.getName()); + System.out.format("Created job: %s%n", response.getName()); } } } diff --git a/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateJobCustomAttributes.java b/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateJobCustomAttributes.java index 5eeabce8f4e..8c8e1b9dcf5 100644 --- a/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateJobCustomAttributes.java +++ b/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateJobCustomAttributes.java @@ -43,6 +43,9 @@ public static void createJob( String companyId, String requisitionId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (JobServiceClient jobServiceClient = JobServiceClient.create()) { TenantName parent = TenantName.of(projectId, tenantId); diff --git a/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateTenant.java b/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateTenant.java index d5389406825..5d9b1a5f46c 100644 --- a/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateTenant.java +++ b/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateTenant.java @@ -35,6 +35,9 @@ public static void createTenant() throws IOException { // Create Tenant for scoping resources, e.g. companies and jobs. public static void createTenant(String projectId, String externalId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (TenantServiceClient tenantServiceClient = TenantServiceClient.create()) { ProjectName parent = ProjectName.of(projectId); Tenant tenant = Tenant.newBuilder().setExternalId(externalId).build(); @@ -44,8 +47,8 @@ public static void createTenant(String projectId, String externalId) throws IOEx Tenant response = tenantServiceClient.createTenant(request); System.out.println("Created Tenant"); - System.out.printf("Name: %s\n", response.getName()); - System.out.printf("External ID: %s\n", response.getExternalId()); + System.out.format("Name: %s%n", response.getName()); + System.out.format("External ID: %s%n", response.getExternalId()); } } } diff --git a/jobs/v4/src/main/java/com/example/jobs/JobSearchDeleteCompany.java b/jobs/v4/src/main/java/com/example/jobs/JobSearchDeleteCompany.java index 18599edc8b6..44d8b33aff6 100644 --- a/jobs/v4/src/main/java/com/example/jobs/JobSearchDeleteCompany.java +++ b/jobs/v4/src/main/java/com/example/jobs/JobSearchDeleteCompany.java @@ -36,6 +36,9 @@ public static void deleteCompany() throws IOException { // Delete Company. public static void deleteCompany(String projectId, String tenantId, String companyId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (CompanyServiceClient companyServiceClient = CompanyServiceClient.create()) { CompanyName name = CompanyName.ofProjectTenantCompanyName(projectId, tenantId, companyId); diff --git a/jobs/v4/src/main/java/com/example/jobs/JobSearchDeleteJob.java b/jobs/v4/src/main/java/com/example/jobs/JobSearchDeleteJob.java index 9fc5fd7b32b..82ff8b0a096 100644 --- a/jobs/v4/src/main/java/com/example/jobs/JobSearchDeleteJob.java +++ b/jobs/v4/src/main/java/com/example/jobs/JobSearchDeleteJob.java @@ -35,6 +35,9 @@ public static void deleteJob() throws IOException { // Delete Job. public static void deleteJob(String projectId, String tenantId, String jobId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (JobServiceClient jobServiceClient = JobServiceClient.create()) { JobName name = JobName.ofProjectTenantJobName(projectId, tenantId, jobId); diff --git a/jobs/v4/src/main/java/com/example/jobs/JobSearchDeleteTenant.java b/jobs/v4/src/main/java/com/example/jobs/JobSearchDeleteTenant.java index f984c48d8e6..5d112da04d8 100644 --- a/jobs/v4/src/main/java/com/example/jobs/JobSearchDeleteTenant.java +++ b/jobs/v4/src/main/java/com/example/jobs/JobSearchDeleteTenant.java @@ -34,6 +34,9 @@ public static void deleteTenant() throws IOException { // Delete Tenant. public static void deleteTenant(String projectId, String tenantId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (TenantServiceClient tenantServiceClient = TenantServiceClient.create()) { TenantName name = TenantName.of(projectId, tenantId); diff --git a/jobs/v4/src/main/java/com/example/jobs/JobSearchGetCompany.java b/jobs/v4/src/main/java/com/example/jobs/JobSearchGetCompany.java index 6aff009185b..639b15979cc 100644 --- a/jobs/v4/src/main/java/com/example/jobs/JobSearchGetCompany.java +++ b/jobs/v4/src/main/java/com/example/jobs/JobSearchGetCompany.java @@ -37,14 +37,17 @@ public static void getCompany() throws IOException { // Get Company. public static void getCompany(String projectId, String tenantId, String companyId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (CompanyServiceClient companyServiceClient = CompanyServiceClient.create()) { CompanyName name = CompanyName.ofProjectTenantCompanyName(projectId, tenantId, companyId); GetCompanyRequest request = GetCompanyRequest.newBuilder().setName(name.toString()).build(); Company response = companyServiceClient.getCompany(request); - System.out.printf("Company name: %s\n", response.getName()); - System.out.printf("Display name: %s\n", response.getDisplayName()); + System.out.format("Company name: %s%n", response.getName()); + System.out.format("Display name: %s%n", response.getDisplayName()); } } } diff --git a/jobs/v4/src/main/java/com/example/jobs/JobSearchGetJob.java b/jobs/v4/src/main/java/com/example/jobs/JobSearchGetJob.java index 8d6545a5d94..84a88801792 100644 --- a/jobs/v4/src/main/java/com/example/jobs/JobSearchGetJob.java +++ b/jobs/v4/src/main/java/com/example/jobs/JobSearchGetJob.java @@ -36,25 +36,28 @@ public static void getJob() throws IOException { // Get Job. public static void getJob(String projectId, String tenantId, String jobId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (JobServiceClient jobServiceClient = JobServiceClient.create()) { JobName name = JobName.ofProjectTenantJobName(projectId, tenantId, jobId); GetJobRequest request = GetJobRequest.newBuilder().setName(name.toString()).build(); Job response = jobServiceClient.getJob(request); - System.out.printf("Job name: %s\n", response.getName()); - System.out.printf("Requisition ID: %s\n", response.getRequisitionId()); - System.out.printf("Title: %s\n", response.getTitle()); - System.out.printf("Description: %s\n", response.getDescription()); - System.out.printf("Posting language: %s\n", response.getLanguageCode()); + System.out.format("Job name: %s%n", response.getName()); + System.out.format("Requisition ID: %s%n", response.getRequisitionId()); + System.out.format("Title: %s%n", response.getTitle()); + System.out.format("Description: %s%n", response.getDescription()); + System.out.format("Posting language: %s%n", response.getLanguageCode()); for (String address : response.getAddressesList()) { - System.out.printf("Address: %s\n", address); + System.out.format("Address: %s%n", address); } for (String email : response.getApplicationInfo().getEmailsList()) { - System.out.printf("Email: %s\n", email); + System.out.format("Email: %s%n", email); } for (String websiteUri : response.getApplicationInfo().getUrisList()) { - System.out.printf("Website: %s\n", websiteUri); + System.out.format("Website: %s%n", websiteUri); } } } diff --git a/jobs/v4/src/main/java/com/example/jobs/JobSearchGetTenant.java b/jobs/v4/src/main/java/com/example/jobs/JobSearchGetTenant.java index c59f90e27a8..527ba34d295 100644 --- a/jobs/v4/src/main/java/com/example/jobs/JobSearchGetTenant.java +++ b/jobs/v4/src/main/java/com/example/jobs/JobSearchGetTenant.java @@ -35,14 +35,17 @@ public static void getTenant() throws IOException { // Get Tenant by name. public static void getTenant(String projectId, String tenantId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (TenantServiceClient tenantServiceClient = TenantServiceClient.create()) { TenantName name = TenantName.of(projectId, tenantId); GetTenantRequest request = GetTenantRequest.newBuilder().setName(name.toString()).build(); Tenant response = tenantServiceClient.getTenant(request); - System.out.printf("Name: %s\n", response.getName()); - System.out.printf("External ID: %s\n", response.getExternalId()); + System.out.format("Name: %s%n", response.getName()); + System.out.format("External ID: %s%n", response.getExternalId()); } } } diff --git a/jobs/v4/src/main/java/com/example/jobs/JobSearchListCompanies.java b/jobs/v4/src/main/java/com/example/jobs/JobSearchListCompanies.java index 4e9878f7d9e..cef24dc2087 100644 --- a/jobs/v4/src/main/java/com/example/jobs/JobSearchListCompanies.java +++ b/jobs/v4/src/main/java/com/example/jobs/JobSearchListCompanies.java @@ -35,6 +35,9 @@ public static void listCompanies() throws IOException { // List Companies. public static void listCompanies(String projectId, String tenantId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (CompanyServiceClient companyServiceClient = CompanyServiceClient.create()) { TenantName parent = TenantName.of(projectId, tenantId); @@ -42,9 +45,9 @@ public static void listCompanies(String projectId, String tenantId) throws IOExc ListCompaniesRequest.newBuilder().setParent(parent.toString()).build(); for (Company responseItem : companyServiceClient.listCompanies(request).iterateAll()) { - System.out.printf("Company Name: %s\n", responseItem.getName()); - System.out.printf("Display Name: %s\n", responseItem.getDisplayName()); - System.out.printf("External ID: %s\n", responseItem.getExternalId()); + System.out.format("Company Name: %s%n", responseItem.getName()); + System.out.format("Display Name: %s%n", responseItem.getDisplayName()); + System.out.format("External ID: %s%n", responseItem.getExternalId()); } } } diff --git a/jobs/v4/src/main/java/com/example/jobs/JobSearchListJobs.java b/jobs/v4/src/main/java/com/example/jobs/JobSearchListJobs.java index df1342c843c..81f844226ea 100644 --- a/jobs/v4/src/main/java/com/example/jobs/JobSearchListJobs.java +++ b/jobs/v4/src/main/java/com/example/jobs/JobSearchListJobs.java @@ -36,15 +36,18 @@ public static void listJobs() throws IOException { // Search Jobs with histogram queries. public static void listJobs(String projectId, String tenantId, String filter) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (JobServiceClient jobServiceClient = JobServiceClient.create()) { TenantName parent = TenantName.of(projectId, tenantId); ListJobsRequest request = ListJobsRequest.newBuilder().setParent(parent.toString()).setFilter(filter).build(); for (Job responseItem : jobServiceClient.listJobs(request).iterateAll()) { - System.out.printf("Job name: %s\n", responseItem.getName()); - System.out.printf("Job requisition ID: %s\n", responseItem.getRequisitionId()); - System.out.printf("Job title: %s\n", responseItem.getTitle()); - System.out.printf("Job description: %s\n", responseItem.getDescription()); + System.out.format("Job name: %s%n", responseItem.getName()); + System.out.format("Job requisition ID: %s%n", responseItem.getRequisitionId()); + System.out.format("Job title: %s%n", responseItem.getTitle()); + System.out.format("Job description: %s%n", responseItem.getDescription()); } } } diff --git a/jobs/v4/src/main/java/com/example/jobs/JobSearchListTenants.java b/jobs/v4/src/main/java/com/example/jobs/JobSearchListTenants.java index 565e75e4714..4b0be36b2cf 100644 --- a/jobs/v4/src/main/java/com/example/jobs/JobSearchListTenants.java +++ b/jobs/v4/src/main/java/com/example/jobs/JobSearchListTenants.java @@ -34,6 +34,9 @@ public static void listTenants() throws IOException { // List Tenants. public static void listTenants(String projectId) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (TenantServiceClient tenantServiceClient = TenantServiceClient.create()) { ProjectName parent = ProjectName.of(projectId); @@ -41,8 +44,8 @@ public static void listTenants(String projectId) throws IOException { ListTenantsRequest.newBuilder().setParent(parent.toString()).build(); for (Tenant responseItem : tenantServiceClient.listTenants(request).iterateAll()) { - System.out.printf("Tenant Name: %s\n", responseItem.getName()); - System.out.printf("External ID: %s\n", responseItem.getExternalId()); + System.out.format("Tenant Name: %s%n", responseItem.getName()); + System.out.format("External ID: %s%n", responseItem.getExternalId()); } } } diff --git a/jobs/v4/src/test/java/JobSearchCreateCompanyTest.java b/jobs/v4/src/test/java/JobSearchCreateCompanyTest.java index 9a546b8e008..926dbe3556b 100644 --- a/jobs/v4/src/test/java/JobSearchCreateCompanyTest.java +++ b/jobs/v4/src/test/java/JobSearchCreateCompanyTest.java @@ -20,6 +20,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; +import java.util.UUID; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -28,7 +29,8 @@ public class JobSearchCreateCompanyTest { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String TENANT_ID = System.getenv("CTS_TENANT_ID"); - private static final String COMPANY_EXT_ID = "DO_NOT_DELETE_EXT_ID"; + private static final String COMPANY_EXT_ID = + String.format("COMP_EXT_ID_%s", UUID.randomUUID().toString().substring(0, 20)); private static final String COMPANY_DISPLAY_NAME = "DO_NOT_DELETE_COMPANY"; private String companyId; diff --git a/jobs/v4/src/test/java/JobSearchCreateTenantTest.java b/jobs/v4/src/test/java/JobSearchCreateTenantTest.java index ed0315e7391..96596b09971 100644 --- a/jobs/v4/src/test/java/JobSearchCreateTenantTest.java +++ b/jobs/v4/src/test/java/JobSearchCreateTenantTest.java @@ -21,14 +21,15 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; +import java.util.UUID; import org.junit.After; import org.junit.Before; import org.junit.Test; - public class JobSearchCreateTenantTest { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String TENANT_EXT_ID = "EXTERNAL_TEMP_TENANT_ID"; + private static final String TENANT_EXT_ID = + String.format("EXTERNAL_TEMP_TENANT_ID_%s", UUID.randomUUID().toString().substring(0, 20)); private String tenantId; private ByteArrayOutputStream bout; diff --git a/jobs/v4/src/test/java/JobSearchDeleteCompanyTest.java b/jobs/v4/src/test/java/JobSearchDeleteCompanyTest.java index 6f2ca284430..2f367a6755f 100644 --- a/jobs/v4/src/test/java/JobSearchDeleteCompanyTest.java +++ b/jobs/v4/src/test/java/JobSearchDeleteCompanyTest.java @@ -21,16 +21,17 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; +import java.util.UUID; import org.junit.After; import org.junit.Before; import org.junit.Test; - public class JobSearchDeleteCompanyTest { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String TENANT_ID = System.getenv("CTS_TENANT_ID"); - private static final String COMPANY_EXT_ID = "DO_NOT_DELETE_EXT_ID"; + private static final String COMPANY_EXT_ID = + String.format("COMP_EXT_ID_%s", UUID.randomUUID().toString().substring(0, 20)); private static final String COMPANY_DISPLAY_NAME = "DO_NOT_DELETE_COMPANY"; private String companyId; diff --git a/jobs/v4/src/test/java/JobSearchDeleteTenantTest.java b/jobs/v4/src/test/java/JobSearchDeleteTenantTest.java index 91e986203bf..5d111237acc 100644 --- a/jobs/v4/src/test/java/JobSearchDeleteTenantTest.java +++ b/jobs/v4/src/test/java/JobSearchDeleteTenantTest.java @@ -21,13 +21,15 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; +import java.util.UUID; import org.junit.After; import org.junit.Before; import org.junit.Test; public class JobSearchDeleteTenantTest { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String TENANT_EXT_ID = "EXTERNAL_TEMP_TENANT_ID"; + private static final String TENANT_EXT_ID = + String.format("EXTERNAL_TEMP_TENANT_ID_%s", UUID.randomUUID().toString().substring(0, 20)); private String tenantId; private ByteArrayOutputStream bout; diff --git a/kms/pom.xml b/kms/pom.xml index 58f221cbe38..642ed537f72 100644 --- a/kms/pom.xml +++ b/kms/pom.xml @@ -12,7 +12,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -25,7 +25,13 @@ com.google.cloud google-cloud-kms - 1.38.1 + 1.39.0 + + + + com.google.protobuf + protobuf-java-util + 3.12.2 diff --git a/language/analysis/pom.xml b/language/analysis/pom.xml index 6cdcf7fe306..4b56bb03dd7 100644 --- a/language/analysis/pom.xml +++ b/language/analysis/pom.xml @@ -27,7 +27,7 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 1.8 @@ -42,7 +42,7 @@ limitations under the License. com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -93,7 +93,7 @@ limitations under the License. org.apache.maven.plugins maven-failsafe-plugin - 3.0.0-M4 + 3.0.0-M5 @@ -107,19 +107,6 @@ limitations under the License. org.apache.maven.plugins maven-compiler-plugin 3.8.1 - - - -XDcompilePolicy=simple - -Xplugin:ErrorProne - - - - com.google.errorprone - error_prone_core - 2.3.4 - - - diff --git a/language/cloud-client/pom.xml b/language/cloud-client/pom.xml index 5543ca622cd..eb65d52bdb7 100644 --- a/language/cloud-client/pom.xml +++ b/language/cloud-client/pom.xml @@ -26,7 +26,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 1.8 diff --git a/logging/cloud-client/pom.xml b/logging/cloud-client/pom.xml index 598859f180a..544f665e242 100644 --- a/logging/cloud-client/pom.xml +++ b/logging/cloud-client/pom.xml @@ -26,7 +26,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -40,7 +40,7 @@ com.google.cloud google-cloud-logging - 1.101.1 + 1.101.2 @@ -63,7 +63,7 @@ org.apache.maven.plugins maven-failsafe-plugin - 3.0.0-M4 + 3.0.0-M5 ${project.build.outputDirectory} 3 diff --git a/logging/jul/pom.xml b/logging/jul/pom.xml index 769103ab408..17845d8e6ec 100644 --- a/logging/jul/pom.xml +++ b/logging/jul/pom.xml @@ -26,7 +26,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -39,7 +39,7 @@ com.google.cloud google-cloud-logging - 1.101.1 + 1.101.2 diff --git a/logging/logback/pom.xml b/logging/logback/pom.xml index 0164656b187..abb786c3beb 100644 --- a/logging/logback/pom.xml +++ b/logging/logback/pom.xml @@ -26,7 +26,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 diff --git a/memorystore/redis/pom.xml b/memorystore/redis/pom.xml index ea3d0860a47..59e5d96ab41 100644 --- a/memorystore/redis/pom.xml +++ b/memorystore/redis/pom.xml @@ -27,13 +27,13 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 1.8 1.8 - 9.4.29.v20200521 + 9.4.30.v20200611 false @@ -69,7 +69,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 diff --git a/mlengine/online-prediction/pom.xml b/mlengine/online-prediction/pom.xml index 54efe7a9a03..e74983f75ae 100644 --- a/mlengine/online-prediction/pom.xml +++ b/mlengine/online-prediction/pom.xml @@ -24,7 +24,7 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -37,7 +37,7 @@ limitations under the License. org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 @@ -64,9 +64,9 @@ limitations under the License. 2.10.6 - com.google.api-client - google-api-client - 1.30.9 + com.google.auth + google-auth-library-oauth2-http + 0.21.0 com.google.apis diff --git a/mlengine/online-prediction/src/main/java/OnlinePredictionSample.java b/mlengine/online-prediction/src/main/java/OnlinePredictionSample.java index fced73e444c..007f91896d2 100644 --- a/mlengine/online-prediction/src/main/java/OnlinePredictionSample.java +++ b/mlengine/online-prediction/src/main/java/OnlinePredictionSample.java @@ -14,7 +14,6 @@ * limitations under the License. */ -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.http.FileContent; import com.google.api.client.http.GenericUrl; @@ -29,6 +28,8 @@ import com.google.api.services.discovery.model.JsonSchema; import com.google.api.services.discovery.model.RestDescription; import com.google.api.services.discovery.model.RestMethod; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.File; import java.util.ArrayList; import java.util.List; @@ -63,16 +64,16 @@ public static void main(String[] args) throws Exception { File requestBodyFile = new File("input.txt"); HttpContent content = new FileContent(contentType, requestBodyFile); System.out.println(content.getLength()); - + List scopes = new ArrayList<>(); scopes.add("https://www.googleapis.com/auth/cloud-platform"); - - GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(scopes); - HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential); + + GoogleCredentials credential = GoogleCredentials.getApplicationDefault().createScoped(scopes); + HttpRequestFactory requestFactory = + httpTransport.createRequestFactory(new HttpCredentialsAdapter(credential)); HttpRequest request = requestFactory.buildRequest(method.getHttpMethod(), url, content); String response = request.execute().parseAsString(); System.out.println(response); } } - diff --git a/monitoring/cloud-client/pom.xml b/monitoring/cloud-client/pom.xml index 8884d9ec9af..54b1f63fd46 100644 --- a/monitoring/cloud-client/pom.xml +++ b/monitoring/cloud-client/pom.xml @@ -26,7 +26,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -45,7 +45,7 @@ com.google.auth google-auth-library-oauth2-http - 0.20.0 + 0.21.0 diff --git a/monitoring/cloud-client/src/main/java/com/example/monitoring/QuickstartSample.java b/monitoring/cloud-client/src/main/java/com/example/monitoring/QuickstartSample.java index fcfd392a43f..6b6e2579736 100644 --- a/monitoring/cloud-client/src/main/java/com/example/monitoring/QuickstartSample.java +++ b/monitoring/cloud-client/src/main/java/com/example/monitoring/QuickstartSample.java @@ -55,7 +55,7 @@ public static void main(String... args) throws Exception { TimeInterval.newBuilder() .setEndTime(Timestamps.fromMillis(System.currentTimeMillis())) .build(); - TypedValue value = TypedValue.newBuilder().setDoubleValue(123.45).build(); + TypedValue value = TypedValue.newBuilder().setDoubleValue(3.14).build(); Point point = Point.newBuilder().setInterval(interval).setValue(value).build(); List pointList = new ArrayList<>(); @@ -68,15 +68,16 @@ public static void main(String... args) throws Exception { metricLabels.put("store_id", "Pittsburg"); Metric metric = Metric.newBuilder() - .setType("custom.googleapis.com/stores/daily_sales") + .setType("custom.googleapis.com/my_metric") .putAllLabels(metricLabels) .build(); // Prepares the monitored resource descriptor Map resourceLabels = new HashMap(); - resourceLabels.put("project_id", projectId); + resourceLabels.put("instance_id", "1234567890123456789"); + resourceLabels.put("zone", "us-central1-f"); MonitoredResource resource = - MonitoredResource.newBuilder().setType("global").putAllLabels(resourceLabels).build(); + MonitoredResource.newBuilder().setType("gce_instance").putAllLabels(resourceLabels).build(); // Prepares the time series request TimeSeries timeSeries = diff --git a/monitoring/v3/pom.xml b/monitoring/v3/pom.xml index 4f3b5f79d91..31d824c0a30 100644 --- a/monitoring/v3/pom.xml +++ b/monitoring/v3/pom.xml @@ -27,7 +27,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 diff --git a/opencensus/pom.xml b/opencensus/pom.xml index c3b67ce483a..7427e9fc893 100644 --- a/opencensus/pom.xml +++ b/opencensus/pom.xml @@ -29,7 +29,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -64,7 +64,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 diff --git a/pubsub/cloud-client/README.md b/pubsub/cloud-client/README.md index 6b85c97ada6..26aa0bfb89d 100644 --- a/pubsub/cloud-client/README.md +++ b/pubsub/cloud-client/README.md @@ -1,56 +1,3 @@ # Getting Started with Cloud Pub/Sub and the Google Cloud Client libraries - -Open in Cloud Shell - -[Google Cloud Pub/Sub][pubsub] is a fully-managed real-time messaging service that allows you to -send and receive messages between independent applications. -This sample Java application demonstrates how to access the Pub/Sub API using -the [Google Cloud Client Library for Java][google-cloud-java]. - -[pubsub]: https://cloud.google.com/pubsub/ -[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java - -For more samples, see the samples in -[google-cloud-java](https://github.com/GoogleCloudPlatform/google-cloud-java/tree/master/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub). - -## Quickstart - -#### Setup -- Install [Maven](http://maven.apache.org/). -- [Enable](https://console.cloud.google.com/apis/api/pubsub.googleapis.com/overview) Pub/Sub API. -- Set up [authentication](https://cloud.google.com/docs/authentication/getting-started). - -#### Build -- Build your project with: -``` - mvn clean package -DskipTests -``` - -#### Create a new topic -``` - mvn exec:java -Dexec.mainClass=com.example.pubsub.CreateTopicExample -Dexec.args=my-topic -``` - -#### Create a subscription -``` - mvn exec:java -Dexec.mainClass=com.example.pubsub.CreatePullSubscriptionExample -Dexec.args="my-topic my-sub" -``` - -#### Publish messages -``` - mvn exec:java -Dexec.mainClass=com.example.pubsub.PublisherExample -Dexec.args="my-topic 5" -``` -Publishes 5 messages to the topic `my-topic`. - -#### Receive messages -``` - mvn exec:java -Dexec.mainClass=com.example.pubsub.SubscriberExample -Dexec.args=my-sub -``` -Subscriber will continue to listen on the topic and print out message id and data as messages are received. Press `Ctrl+C` to exit the application. - -#### Testing -Run the test with Maven. -``` - mvn verify -``` +The samples have been moved to live alongside the Java client library for Cloud Pub/Sub: https://github.com/googleapis/java-pubsub/tree/master/samples/snippets/src/main/java/pubsub/ \ No newline at end of file diff --git a/pubsub/cloud-client/pom.xml b/pubsub/cloud-client/pom.xml deleted file mode 100644 index fff92355864..00000000000 --- a/pubsub/cloud-client/pom.xml +++ /dev/null @@ -1,92 +0,0 @@ - - - 4.0.0 - com.example.pubsub - pubsub-google-cloud-samples - jar - - - - com.google.cloud.samples - shared-configuration - 1.0.17 - - - - 1.8 - 1.8 - - - - - - - - com.google.cloud - libraries-bom - 5.4.0 - pom - import - - - - - - - com.google.cloud - google-cloud-pubsub - - - com.google.cloud - google-cloud-core - - - - - - junit - junit - 4.13 - test - - - com.google.truth - truth - 1.0.1 - test - - - - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - false - - - - - diff --git a/pubsub/cloud-client/src/main/java/com/example/pubsub/CreatePullSubscriptionExample.java b/pubsub/cloud-client/src/main/java/com/example/pubsub/CreatePullSubscriptionExample.java deleted file mode 100644 index 4de91bcbdfe..00000000000 --- a/pubsub/cloud-client/src/main/java/com/example/pubsub/CreatePullSubscriptionExample.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2017 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.pubsub; - -// [START pubsub_quickstart_create_subscription] - -import com.google.api.gax.rpc.ApiException; -import com.google.cloud.ServiceOptions; -import com.google.cloud.pubsub.v1.SubscriptionAdminClient; -import com.google.pubsub.v1.ProjectSubscriptionName; -import com.google.pubsub.v1.ProjectTopicName; -import com.google.pubsub.v1.PushConfig; -import com.google.pubsub.v1.Subscription; - -public class CreatePullSubscriptionExample { - - /** - * Create a pull subscription. - * - * @param args topic subscriptionId - * @throws Exception exception thrown if operation is unsuccessful - */ - public static void main(String... args) throws Exception { - - // Your Google Cloud Platform project ID - String projectId = ServiceOptions.getDefaultProjectId(); - - // Your topic ID, eg. "my-topic" - String topicId = args[0]; - - // Your subscription ID eg. "my-sub" - String subscriptionId = args[1]; - - ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId); - - // Create a new subscription - ProjectSubscriptionName subscriptionName = - ProjectSubscriptionName.of(projectId, subscriptionId); - try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) { - // create a pull subscription with default acknowledgement deadline (= 10 seconds) - Subscription subscription = - subscriptionAdminClient.createSubscription( - subscriptionName, topicName, PushConfig.getDefaultInstance(), 0); - System.out.printf( - "Subscription %s:%s created.\n", - subscriptionName.getProject(), subscriptionName.getSubscription()); - } catch (ApiException e) { - // example : code = ALREADY_EXISTS(409) implies subscription already exists - System.out.print(e.getStatusCode().getCode()); - System.out.print(e.isRetryable()); - } - } -} -// [END pubsub_quickstart_create_subscription] diff --git a/pubsub/cloud-client/src/main/java/com/example/pubsub/CreateTopicExample.java b/pubsub/cloud-client/src/main/java/com/example/pubsub/CreateTopicExample.java deleted file mode 100644 index 66c6f8213d5..00000000000 --- a/pubsub/cloud-client/src/main/java/com/example/pubsub/CreateTopicExample.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2016 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.pubsub; - -// [START pubsub_quickstart_create_topic] -// Imports the Google Cloud client library - -import com.google.api.gax.rpc.ApiException; -import com.google.cloud.ServiceOptions; -import com.google.cloud.pubsub.v1.TopicAdminClient; -import com.google.pubsub.v1.ProjectTopicName; - -public class CreateTopicExample { - - /** - * Create a topic. - * - * @param args topicId - * @throws Exception exception thrown if operation is unsuccessful - */ - public static void main(String... args) throws Exception { - - // Your Google Cloud Platform project ID - String projectId = ServiceOptions.getDefaultProjectId(); - - // Your topic ID, eg. "my-topic" - String topicId = args[0]; - - // Create a new topic - ProjectTopicName topic = ProjectTopicName.of(projectId, topicId); - try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { - topicAdminClient.createTopic(topic); - System.out.printf("Topic %s:%s created.\n", topic.getProject(), topic.getTopic()); - } catch (ApiException e) { - // example : code = ALREADY_EXISTS(409) implies topic already exists - System.out.print(e.getStatusCode().getCode()); - System.out.print(e.isRetryable()); - } - } -} -// [END pubsub_quickstart_create_topic] diff --git a/pubsub/cloud-client/src/main/java/com/example/pubsub/PublisherExample.java b/pubsub/cloud-client/src/main/java/com/example/pubsub/PublisherExample.java deleted file mode 100644 index 1242eed6b23..00000000000 --- a/pubsub/cloud-client/src/main/java/com/example/pubsub/PublisherExample.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 2017 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.pubsub; -// [START pubsub_quickstart_publisher] - -import com.google.api.core.ApiFuture; -import com.google.api.core.ApiFutures; -import com.google.cloud.ServiceOptions; -import com.google.cloud.pubsub.v1.Publisher; -import com.google.protobuf.ByteString; -import com.google.pubsub.v1.ProjectTopicName; -import com.google.pubsub.v1.PubsubMessage; -import java.util.ArrayList; -import java.util.List; - -public class PublisherExample { - - // use the default project id - private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId(); - - /** - * Publish messages to a topic. - * - * @param args topic name, number of messages - */ - public static void main(String... args) throws Exception { - // topic id, eg. "my-topic" - String topicId = args[0]; - int messageCount = Integer.parseInt(args[1]); - ProjectTopicName topicName = ProjectTopicName.of(PROJECT_ID, topicId); - Publisher publisher = null; - List> futures = new ArrayList<>(); - - try { - // Create a publisher instance with default settings bound to the topic - publisher = Publisher.newBuilder(topicName).build(); - - for (int i = 0; i < messageCount; i++) { - String message = "message-" + i; - - // convert message to bytes - ByteString data = ByteString.copyFromUtf8(message); - PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); - - // Schedule a message to be published. Messages are automatically batched. - ApiFuture future = publisher.publish(pubsubMessage); - futures.add(future); - } - } finally { - // Wait on any pending requests - List messageIds = ApiFutures.allAsList(futures).get(); - - for (String messageId : messageIds) { - System.out.println(messageId); - } - - if (publisher != null) { - // When finished with the publisher, shutdown to free up resources. - publisher.shutdown(); - } - } - } -} -// [END pubsub_quickstart_quickstart] diff --git a/pubsub/cloud-client/src/main/java/com/example/pubsub/SubscriberExample.java b/pubsub/cloud-client/src/main/java/com/example/pubsub/SubscriberExample.java deleted file mode 100644 index e5bf2d831a0..00000000000 --- a/pubsub/cloud-client/src/main/java/com/example/pubsub/SubscriberExample.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2017 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.pubsub; - -// [START pubsub_quickstart_subscriber] - -import com.google.cloud.ServiceOptions; -import com.google.cloud.pubsub.v1.AckReplyConsumer; -import com.google.cloud.pubsub.v1.MessageReceiver; -import com.google.cloud.pubsub.v1.Subscriber; -import com.google.pubsub.v1.ProjectSubscriptionName; -import com.google.pubsub.v1.PubsubMessage; - -public class SubscriberExample { - // use the default project id - private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId(); - - static class MessageReceiverExample implements MessageReceiver { - - @Override - public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) { - System.out.println( - "Message Id: " + message.getMessageId() + " Data: " + message.getData().toStringUtf8()); - // Ack only after all work for the message is complete. - consumer.ack(); - } - } - - /** Receive messages over a subscription. */ - public static void main(String... args) throws Exception { - // set subscriber id, eg. my-sub - String subscriptionId = args[0]; - ProjectSubscriptionName subscriptionName = - ProjectSubscriptionName.of(PROJECT_ID, subscriptionId); - Subscriber subscriber = null; - try { - // create a subscriber bound to the asynchronous message receiver - subscriber = Subscriber.newBuilder(subscriptionName, new MessageReceiverExample()).build(); - subscriber.startAsync().awaitRunning(); - // Allow the subscriber to run indefinitely unless an unrecoverable error occurs. - subscriber.awaitTerminated(); - } catch (IllegalStateException e) { - System.out.println("Subscriber unexpectedly stopped: " + e); - } - } -} -// [END pubsub_quickstart_subscriber] diff --git a/pubsub/cloud-client/src/test/java/com/example/pubsub/QuickStartIT.java b/pubsub/cloud-client/src/test/java/com/example/pubsub/QuickStartIT.java deleted file mode 100644 index dfaf1045a4b..00000000000 --- a/pubsub/cloud-client/src/test/java/com/example/pubsub/QuickStartIT.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Copyright 2016 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.pubsub; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.cloud.ServiceOptions; -import com.google.cloud.pubsub.v1.SubscriptionAdminClient; -import com.google.cloud.pubsub.v1.TopicAdminClient; -import com.google.pubsub.v1.ProjectSubscriptionName; -import com.google.pubsub.v1.ProjectTopicName; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.PrintStream; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.Timeout; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Tests for quickstart sample. */ -@RunWith(JUnit4.class) -@SuppressWarnings("checkstyle:abbreviationaswordinname") -public class QuickStartIT { - - private ByteArrayOutputStream bout; - - private String projectId = ServiceOptions.getDefaultProjectId(); - private String topicId = formatForTest("my-topic"); - private String subscriptionId = formatForTest("my-sub"); - private int messageCount = 5; - - class SubscriberRunnable implements Runnable { - - private String subscriptionId; - - SubscriberRunnable(String subscriptionId) { - this.subscriptionId = subscriptionId; - } - - @Override - public void run() { - try { - SubscriberExample.main(subscriptionId); - } catch (Exception e) { - e.printStackTrace(); - } - } - } - - @Rule public Timeout globalTimeout = Timeout.seconds(300); // 5 minute timeout - - @Before - public void setUp() { - bout = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(bout); - System.setOut(out); - try { - deleteTestSubscription(); - deleteTestTopic(); - } catch (Exception e) { - // topic, subscription may not yet exist - } - } - - @After - public void tearDown() throws Exception { - System.setOut(null); - deleteTestSubscription(); - deleteTestTopic(); - } - - @Test - public void testQuickstart() throws Exception { - // create a topic - CreateTopicExample.main(topicId); - String got = bout.toString(); - assertThat(got).contains(topicId + " created."); - - // create a subscriber - CreatePullSubscriptionExample.main(topicId, subscriptionId); - got = bout.toString(); - assertThat(got).contains(subscriptionId + " created."); - - bout.reset(); - // publish messages - PublisherExample.main(topicId, String.valueOf(messageCount)); - String[] messageIds = bout.toString().split("\n"); - assertThat(messageIds).hasLength(messageCount); - - bout.reset(); - // receive messages - Thread subscriberThread = new Thread(new SubscriberRunnable(subscriptionId)); - subscriberThread.start(); - Set expectedMessageIds = new HashSet<>(); - List receivedMessageIds = new ArrayList<>(); - expectedMessageIds.addAll(Arrays.asList(messageIds)); - while (!expectedMessageIds.isEmpty()) { - for (String expectedId : expectedMessageIds) { - if (bout.toString().contains(expectedId)) { - receivedMessageIds.add(expectedId); - } - } - expectedMessageIds.removeAll(receivedMessageIds); - } - assertThat(expectedMessageIds).isEmpty(); - } - - private String formatForTest(String name) { - return name + "-" + java.util.UUID.randomUUID().toString(); - } - - private void deleteTestTopic() throws Exception { - try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { - topicAdminClient.deleteTopic(ProjectTopicName.of(projectId, topicId)); - } catch (IOException e) { - System.err.println("Error deleting topic " + e.getMessage()); - } - } - - private void deleteTestSubscription() throws Exception { - try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) { - subscriptionAdminClient.deleteSubscription( - ProjectSubscriptionName.of(projectId, subscriptionId)); - } catch (IOException e) { - System.err.println("Error deleting subscription " + e.getMessage()); - } - } -} diff --git a/pubsub/streaming-analytics/pom.xml b/pubsub/streaming-analytics/pom.xml index 4123fb13925..50204c6bf1e 100644 --- a/pubsub/streaming-analytics/pom.xml +++ b/pubsub/streaming-analytics/pom.xml @@ -28,12 +28,12 @@ 1.8 UTF-8 - 2.20.0 + 2.22.0 3.8.1 - 1.6.0 + 3.0.0 3.2.0 - 3.2.3 + 3.2.4 1.7.30 diff --git a/recommender/beta/cloud-client/pom.xml b/recommender/beta/cloud-client/pom.xml index ccd17e9ccd2..a18aaef6e15 100644 --- a/recommender/beta/cloud-client/pom.xml +++ b/recommender/beta/cloud-client/pom.xml @@ -23,7 +23,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -39,7 +39,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -50,13 +50,13 @@ com.google.cloud google-cloud-recommender - 0.4.1 + 1.1.0 com.google.api.grpc proto-google-cloud-recommender-v1beta1 - 0.4.1 + 0.6.0 @@ -82,7 +82,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 false diff --git a/run/authentication/pom.xml b/run/authentication/pom.xml index 24728db20ce..e6f46f99209 100644 --- a/run/authentication/pom.xml +++ b/run/authentication/pom.xml @@ -25,7 +25,7 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 diff --git a/run/hello-broken/pom.xml b/run/hello-broken/pom.xml index 7e1a8bd0f87..a3312438c76 100644 --- a/run/hello-broken/pom.xml +++ b/run/hello-broken/pom.xml @@ -23,7 +23,7 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 UTF-8 @@ -73,7 +73,7 @@ limitations under the License. com.google.cloud.tools jib-maven-plugin - 2.3.0 + 2.4.0 gcr.io/PROJECT_ID/hello-service diff --git a/run/helloworld/pom.xml b/run/helloworld/pom.xml index c2fdf10a185..2597ef11ab7 100644 --- a/run/helloworld/pom.xml +++ b/run/helloworld/pom.xml @@ -24,7 +24,7 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -32,7 +32,7 @@ limitations under the License. org.springframework.boot spring-boot-dependencies - 2.3.0.RELEASE + 2.3.1.RELEASE pom import @@ -71,7 +71,7 @@ limitations under the License. com.google.cloud.tools jib-maven-plugin - 2.3.0 + 2.4.0 gcr.io/PROJECT_ID/helloworld diff --git a/run/image-processing/pom.xml b/run/image-processing/pom.xml index 98e85af4d22..e00492cb5e8 100644 --- a/run/image-processing/pom.xml +++ b/run/image-processing/pom.xml @@ -23,7 +23,7 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 11 @@ -35,14 +35,14 @@ limitations under the License. org.springframework.boot spring-boot-dependencies - 2.3.0.RELEASE + 2.3.1.RELEASE pom import org.springframework.cloud spring-cloud-dependencies - Hoxton.SR4 + Hoxton.SR6 pom import @@ -50,7 +50,7 @@ limitations under the License. org.springframework.cloud spring-cloud-gcp-dependencies - 1.2.2.RELEASE + 1.2.3.RELEASE pom import @@ -82,7 +82,7 @@ limitations under the License. org.json json - 20190722 + 20200518 org.springframework.cloud @@ -95,7 +95,7 @@ limitations under the License. commons-io commons-io - 2.6 + 2.7 @@ -109,7 +109,7 @@ limitations under the License. com.google.cloud.tools jib-maven-plugin - 2.3.0 + 2.4.0 gcr.io/PROJECT_ID/imagemagick diff --git a/run/logging-manual/pom.xml b/run/logging-manual/pom.xml index 583f79a2ab6..db030794553 100644 --- a/run/logging-manual/pom.xml +++ b/run/logging-manual/pom.xml @@ -20,7 +20,7 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 UTF-8 @@ -43,7 +43,7 @@ limitations under the License. net.logstash.logback logstash-logback-encoder - 6.3 + 6.4 ch.qos.logback @@ -70,7 +70,7 @@ limitations under the License. com.google.cloud.tools jib-maven-plugin - 2.3.0 + 2.4.0 gcr.io/PROJECT_ID/logging-manual diff --git a/run/markdown-preview/editor/pom.xml b/run/markdown-preview/editor/pom.xml index 8cd9c4be6a5..e6841dffe32 100644 --- a/run/markdown-preview/editor/pom.xml +++ b/run/markdown-preview/editor/pom.xml @@ -24,7 +24,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 11 @@ -36,7 +36,7 @@ org.springframework.boot spring-boot-dependencies - 2.3.0.RELEASE + 2.3.1.RELEASE pom import @@ -83,7 +83,7 @@ com.google.cloud.tools jib-maven-plugin - 2.3.0 + 2.4.0 gcr.io/PROJECT_ID/editor diff --git a/run/markdown-preview/renderer/pom.xml b/run/markdown-preview/renderer/pom.xml index 341deedcee1..887cb3b3a12 100644 --- a/run/markdown-preview/renderer/pom.xml +++ b/run/markdown-preview/renderer/pom.xml @@ -24,7 +24,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 11 @@ -36,7 +36,7 @@ org.springframework.boot spring-boot-dependencies - 2.3.0.RELEASE + 2.3.1.RELEASE pom import @@ -50,22 +50,22 @@ com.atlassian.commonmark commonmark - 0.15.0 + 0.15.1 com.atlassian.commonmark commonmark-ext-gfm-tables - 0.15.0 + 0.15.1 com.atlassian.commonmark commonmark-ext-gfm-strikethrough - 0.15.0 + 0.15.1 com.googlecode.owasp-java-html-sanitizer owasp-java-html-sanitizer - 20191001.1 + 20200615.1 junit @@ -94,7 +94,7 @@ com.google.cloud.tools jib-maven-plugin - 2.3.0 + 2.4.0 gcr.io/PROJECT_ID/renderer diff --git a/run/pubsub/pom.xml b/run/pubsub/pom.xml index c815b06167f..c49dea4e50d 100644 --- a/run/pubsub/pom.xml +++ b/run/pubsub/pom.xml @@ -21,7 +21,7 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -35,14 +35,14 @@ limitations under the License. org.springframework.boot spring-boot-dependencies - 2.3.0.RELEASE + 2.3.1.RELEASE pom import org.springframework.cloud spring-cloud-dependencies - Hoxton.SR4 + Hoxton.SR6 pom import @@ -76,7 +76,7 @@ limitations under the License. com.google.cloud.tools jib-maven-plugin - 2.3.0 + 2.4.0 gcr.io/PROJECT_ID/pubsub diff --git a/run/system-package/pom.xml b/run/system-package/pom.xml index de037f9e44d..4c2711f7c21 100644 --- a/run/system-package/pom.xml +++ b/run/system-package/pom.xml @@ -25,7 +25,7 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -59,7 +59,7 @@ limitations under the License. com.google.cloud.tools jib-maven-plugin - 2.3.0 + 2.4.0 gcr.io/PROJECT_ID/graphviz-base diff --git a/secretmanager/pom.xml b/secretmanager/pom.xml index bf02b52e3cb..4bd02b26819 100644 --- a/secretmanager/pom.xml +++ b/secretmanager/pom.xml @@ -28,7 +28,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -41,7 +41,13 @@ com.google.cloud google-cloud-secretmanager - 1.0.1 + 1.1.0 + + + + com.google.protobuf + protobuf-java-util + 3.12.2 diff --git a/securitycenter/pom.xml b/securitycenter/pom.xml index 7d3ef16eafd..e407e80c8be 100644 --- a/securitycenter/pom.xml +++ b/securitycenter/pom.xml @@ -23,7 +23,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -36,12 +36,18 @@ com.google.cloud google-cloud-pubsub - 1.105.1 + 1.107.0 com.google.cloud google-cloud-securitycenter - 0.121.0 + 1.1.0 + + + + com.google.protobuf + protobuf-java-util + 3.12.2 diff --git a/securitycenter/src/main/java/com/google/cloud/examples/securitycenter/snippets/UpdateNotificationConfigSnippets.java b/securitycenter/src/main/java/com/google/cloud/examples/securitycenter/snippets/UpdateNotificationConfigSnippets.java index 5a511879a6f..dffc0dd9e2b 100644 --- a/securitycenter/src/main/java/com/google/cloud/examples/securitycenter/snippets/UpdateNotificationConfigSnippets.java +++ b/securitycenter/src/main/java/com/google/cloud/examples/securitycenter/snippets/UpdateNotificationConfigSnippets.java @@ -18,6 +18,7 @@ // [START scc_update_notification_config] import com.google.cloud.securitycenter.v1.NotificationConfig; +import com.google.cloud.securitycenter.v1.NotificationConfig.StreamingConfig; import com.google.cloud.securitycenter.v1.SecurityCenterClient; import com.google.protobuf.FieldMask; import java.io.IOException; @@ -48,9 +49,13 @@ public static NotificationConfig updateNotificationConfig( .setName(notificationConfigName) .setDescription("updated description") .setPubsubTopic(pubsubTopic) + .setStreamingConfig(StreamingConfig.newBuilder().setFilter("state = \"ACTIVE\"")) .build(); FieldMask fieldMask = - FieldMask.newBuilder().addPaths("description").addPaths("pubsub_topic").build(); + FieldMask.newBuilder() + .addPaths("description") + .addPaths("pubsub_topic") + .addPaths("streaming_config.filter").build(); try (SecurityCenterClient client = SecurityCenterClient.create()) { NotificationConfig updatedConfig = client.updateNotificationConfig(configToUpdate, fieldMask); @@ -60,4 +65,4 @@ public static NotificationConfig updateNotificationConfig( } } // [END scc_update_notification_config] -} +} \ No newline at end of file diff --git a/session-handling/pom.xml b/session-handling/pom.xml index 3ca794ee2e7..76ccc94b081 100644 --- a/session-handling/pom.xml +++ b/session-handling/pom.xml @@ -25,7 +25,7 @@ Copyright 2019 Google LLC com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -39,7 +39,7 @@ Copyright 2019 Google LLC true false false - 9.4.29.v20200521 + 9.4.30.v20200611 @@ -47,7 +47,7 @@ Copyright 2019 Google LLC com.google.cloud google-cloud-firestore - 1.33.0 + 1.35.0 @@ -104,7 +104,7 @@ Copyright 2019 Google LLC com.google.cloud.tools jib-maven-plugin - 2.3.0 + 2.4.0 gcr.io/${gcloud.appId}/session-handling diff --git a/spanner/cloud-client/pom.xml b/spanner/cloud-client/pom.xml index f57e7ba77f6..23080109976 100644 --- a/spanner/cloud-client/pom.xml +++ b/spanner/cloud-client/pom.xml @@ -26,7 +26,7 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -42,7 +42,7 @@ limitations under the License. com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -164,7 +164,7 @@ limitations under the License. org.apache.maven.plugins maven-failsafe-plugin - 3.0.0-M4 + 3.0.0-M5 default-instance diff --git a/spanner/cloud-client/src/main/java/com/example/spanner/CreateInstanceExample.java b/spanner/cloud-client/src/main/java/com/example/spanner/CreateInstanceExample.java new file mode 100644 index 00000000000..fe7e6ec4248 --- /dev/null +++ b/spanner/cloud-client/src/main/java/com/example/spanner/CreateInstanceExample.java @@ -0,0 +1,71 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.spanner; + +//[START spanner_create_instance] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.spanner.Instance; +import com.google.cloud.spanner.InstanceAdminClient; +import com.google.cloud.spanner.InstanceConfigId; +import com.google.cloud.spanner.InstanceId; +import com.google.cloud.spanner.InstanceInfo; +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; +import com.google.spanner.admin.instance.v1.CreateInstanceMetadata; +import java.util.concurrent.ExecutionException; + +class CreateInstanceExample { + + static void createInstance() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project"; + String instanceId = "my-instance"; + createInstance(projectId, instanceId); + } + + static void createInstance(String projectId, String instanceId) { + Spanner spanner = SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient(); + + // Set Instance configuration. + String configId = "regional-us-central1"; + int nodeCount = 2; + String displayName = "Descriptive name"; + + // Create an InstanceInfo object that will be used to create the instance. + InstanceInfo instanceInfo = + InstanceInfo.newBuilder(InstanceId.of(projectId, instanceId)) + .setInstanceConfigId(InstanceConfigId.of(projectId, configId)) + .setNodeCount(nodeCount) + .setDisplayName(displayName) + .build(); + OperationFuture operation = + instanceAdminClient.createInstance(instanceInfo); + try { + // Wait for the createInstance operation to finish. + Instance instance = operation.get(); + System.out.printf("Instance %s was successfully created%n", instance.getId()); + } catch (ExecutionException e) { + System.out.printf( + "Error: Creating instance %s failed with error message %s%n", + instanceInfo.getId(), e.getMessage()); + } catch (InterruptedException e) { + System.out.println("Error: Waiting for createInstance operation to finish was interrupted"); + } + } +} +//[END spanner_create_instance] diff --git a/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java b/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java index 6639ada47b9..0c4e0a28b85 100644 --- a/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java +++ b/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java @@ -37,6 +37,7 @@ import com.google.cloud.spanner.InstanceAdminClient; import com.google.cloud.spanner.InstanceId; import com.google.cloud.spanner.Key; +import com.google.cloud.spanner.KeyRange; import com.google.cloud.spanner.KeySet; import com.google.cloud.spanner.Mutation; import com.google.cloud.spanner.Options; @@ -353,15 +354,21 @@ static void writeExampleData(DatabaseClient dbClient) { static void deleteExampleData(DatabaseClient dbClient) { List mutations = new ArrayList<>(); - // KeySet.all() can be used to delete all the rows in a table. - mutations.add(Mutation.delete("Albums", KeySet.all())); + // KeySet.Builder can be used to delete a specific set of rows. + // Delete the Albums with the key values (2,1) and (2,3). + mutations.add( + Mutation.delete( + "Albums", KeySet.newBuilder().addKey(Key.of(2, 1)).addKey(Key.of(2, 3)).build())); - // KeySet.singleKey() can be used to delete one row at a time. - for (Singer singer : SINGERS) { - mutations.add( - Mutation.delete( - "Singers", KeySet.singleKey(Key.newBuilder().append(singer.singerId).build()))); - } + // KeyRange can be used to delete rows with a key in a specific range. + // Delete a range of rows where the column key is >=3 and <5 + mutations.add( + Mutation.delete("Singers", KeySet.range(KeyRange.closedOpen(Key.of(3), Key.of(5))))); + + // KeySet.all() can be used to delete all the rows in a table. + // Delete remaining Singers rows, which will also delete the remaining Albums rows since it was + // defined with ON DELETE CASCADE. + mutations.add(Mutation.delete("Singers", KeySet.all())); dbClient.write(mutations); System.out.printf("Records deleted.\n"); diff --git a/spanner/cloud-client/src/test/java/com/example/spanner/SpannerSampleIT.java b/spanner/cloud-client/src/test/java/com/example/spanner/SpannerSampleIT.java index 5360f2f1d13..21a307fe422 100644 --- a/spanner/cloud-client/src/test/java/com/example/spanner/SpannerSampleIT.java +++ b/spanner/cloud-client/src/test/java/com/example/spanner/SpannerSampleIT.java @@ -22,19 +22,18 @@ import com.google.cloud.spanner.DatabaseAdminClient; import com.google.cloud.spanner.DatabaseId; import com.google.cloud.spanner.ErrorCode; +import com.google.cloud.spanner.InstanceId; import com.google.cloud.spanner.Spanner; import com.google.cloud.spanner.SpannerException; import com.google.cloud.spanner.SpannerOptions; -import com.google.common.base.CharMatcher; import com.google.common.util.concurrent.Uninterruptibles; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.util.UUID; import java.util.concurrent.TimeUnit; -import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.junit.After; -import org.junit.Before; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -46,10 +45,12 @@ @SuppressWarnings("checkstyle:abbreviationaswordinname") public class SpannerSampleIT { // The instance needs to exist for tests to pass. - private final String instanceId = System.getProperty("spanner.test.instance"); - private final String databaseId = formatForTest(System.getProperty("spanner.sample.database")); - DatabaseId dbId; - DatabaseAdminClient dbClient; + private static final String instanceId = System.getProperty("spanner.test.instance"); + private static final String databaseId = + formatForTest(System.getProperty("spanner.sample.database")); + static Spanner spanner; + static DatabaseId dbId; + static DatabaseAdminClient dbClient; private long lastUpdateDataTimeInMillis; private String runSample(String command) throws Exception { @@ -62,10 +63,10 @@ private String runSample(String command) throws Exception { return bout.toString(); } - @Before - public void setUp() throws Exception { + @BeforeClass + public static void setUp() throws Exception { SpannerOptions options = SpannerOptions.newBuilder().build(); - Spanner spanner = options.getService(); + spanner = options.getService(); dbClient = spanner.getDatabaseAdminClient(); dbId = DatabaseId.of(options.getProjectId(), instanceId, databaseId); dbClient.dropDatabase(dbId.getInstanceId().getInstance(), dbId.getDatabase()); @@ -73,8 +74,8 @@ public void setUp() throws Exception { dbId.getInstanceId().getInstance(), SpannerSample.createRestoredSampleDbId(dbId)); } - @After - public void tearDown() throws Exception { + @AfterClass + public static void tearDown() throws Exception { dbClient.dropDatabase(dbId.getInstanceId().getInstance(), dbId.getDatabase()); dbClient.dropDatabase( dbId.getInstanceId().getInstance(), SpannerSample.createRestoredSampleDbId(dbId)); @@ -347,11 +348,44 @@ public void testSample() throws Exception { assertThat(out).contains("Deleted backup [" + backupId + "]"); } + private String runSampleRunnable(Runnable sample) { + PrintStream stdOut = System.out; + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + PrintStream out = new PrintStream(bout); + System.setOut(out); + sample.run(); + System.setOut(stdOut); + return bout.toString(); + } + + @Test + public void testCreateInstanceSample() { + String instanceId = formatForTest("sample-inst"); + String out = + runSampleRunnable( + new Runnable() { + @Override + public void run() { + try { + CreateInstanceExample.createInstance( + dbId.getInstanceId().getProject(), instanceId); + } finally { + spanner.getInstanceAdminClient().deleteInstance(instanceId); + } + } + }); + assertThat(out) + .contains( + String.format( + "Instance %s was successfully created", + InstanceId.of(dbId.getInstanceId().getProject(), instanceId))); + } + private static int countOccurrences(String input, String search) { return input.split(search).length - 1; } - private String formatForTest(String name) { + private static String formatForTest(String name) { return name + "-" + UUID.randomUUID().toString().substring(0, 20); } } diff --git a/spanner/hibernate/pom.xml b/spanner/hibernate/pom.xml index 2b5850fe032..a0c4959143a 100644 --- a/spanner/hibernate/pom.xml +++ b/spanner/hibernate/pom.xml @@ -17,7 +17,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 com.example.spanner @@ -44,7 +44,7 @@ org.hibernate hibernate-core - 5.4.16.Final + 5.4.18.Final @@ -54,7 +54,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 diff --git a/spanner/jdbc/pom.xml b/spanner/jdbc/pom.xml index 533e262f546..4f4e1b5556e 100644 --- a/spanner/jdbc/pom.xml +++ b/spanner/jdbc/pom.xml @@ -17,7 +17,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 com.example.spanner @@ -28,7 +28,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -64,7 +64,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 diff --git a/spanner/jdbc/src/main/java/com/example/spanner/jdbc/BatchDmlExample.java b/spanner/jdbc/src/main/java/com/example/spanner/jdbc/BatchDmlExample.java index 1d42bc38647..0e004951b78 100644 --- a/spanner/jdbc/src/main/java/com/example/spanner/jdbc/BatchDmlExample.java +++ b/spanner/jdbc/src/main/java/com/example/spanner/jdbc/BatchDmlExample.java @@ -16,6 +16,7 @@ package com.example.spanner.jdbc; +//[START spanner_jdbc_batch_transaction] import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.sql.Connection; import java.sql.DriverManager; @@ -41,18 +42,23 @@ static void batchDml(String projectId, String instanceId, String databaseId) thr String.format( "jdbc:cloudspanner:/projects/%s/instances/%s/databases/%s", projectId, instanceId, databaseId); - try (Connection connection = DriverManager.getConnection(connectionUrl); - Statement statement = connection.createStatement()) { - statement.addBatch( - "INSERT INTO Singers (SingerId, FirstName, LastName)\n" - + "VALUES (10, 'Marc', 'Richards')"); - statement.addBatch( - "INSERT INTO Singers (SingerId, FirstName, LastName)\n" - + "VALUES (11, 'Amirah', 'Finney')"); - statement.addBatch( - "INSERT INTO Singers (SingerId, FirstName, LastName)\n" + "VALUES (12, 'Reece', 'Dunn')"); - int[] updateCounts = statement.executeBatch(); - System.out.printf("Batch insert counts: %s%n", Arrays.toString(updateCounts)); + try (Connection connection = DriverManager.getConnection(connectionUrl)) { + connection.setAutoCommit(false); + try (Statement statement = connection.createStatement()) { + statement.addBatch( + "INSERT INTO Singers (SingerId, FirstName, LastName)\n" + + "VALUES (10, 'Marc', 'Richards')"); + statement.addBatch( + "INSERT INTO Singers (SingerId, FirstName, LastName)\n" + + "VALUES (11, 'Amirah', 'Finney')"); + statement.addBatch( + "INSERT INTO Singers (SingerId, FirstName, LastName)\n" + + "VALUES (12, 'Reece', 'Dunn')"); + int[] updateCounts = statement.executeBatch(); + connection.commit(); + System.out.printf("Batch insert counts: %s%n", Arrays.toString(updateCounts)); + } } } } +//[END spanner_jdbc_batch_transaction] diff --git a/spanner/jdbc/src/main/java/com/example/spanner/jdbc/CreateTableExample.java b/spanner/jdbc/src/main/java/com/example/spanner/jdbc/CreateTableExample.java index 3cd051004b7..f2d0bb1dd5b 100644 --- a/spanner/jdbc/src/main/java/com/example/spanner/jdbc/CreateTableExample.java +++ b/spanner/jdbc/src/main/java/com/example/spanner/jdbc/CreateTableExample.java @@ -16,6 +16,7 @@ package com.example.spanner.jdbc; +//[START spanner_jdbc_create_table] import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.sql.Connection; import java.sql.DriverManager; @@ -55,3 +56,4 @@ static void createTable(String projectId, String instanceId, String databaseId) System.out.println("Created table [Singers]"); } } +//[END spanner_jdbc_create_table] diff --git a/spanner/jdbc/src/main/java/com/example/spanner/jdbc/InsertDataExample.java b/spanner/jdbc/src/main/java/com/example/spanner/jdbc/InsertDataExample.java index 629a71f6ae3..7cf61578373 100644 --- a/spanner/jdbc/src/main/java/com/example/spanner/jdbc/InsertDataExample.java +++ b/spanner/jdbc/src/main/java/com/example/spanner/jdbc/InsertDataExample.java @@ -16,6 +16,7 @@ package com.example.spanner.jdbc; +//[START spanner_jdbc_insert] import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.sql.Connection; import java.sql.DriverManager; @@ -65,7 +66,6 @@ static void insertData(String projectId, String instanceId, String databaseId) "jdbc:cloudspanner:/projects/%s/instances/%s/databases/%s", projectId, instanceId, databaseId); try (Connection connection = DriverManager.getConnection(connectionUrl)) { - connection.setAutoCommit(false); try (PreparedStatement ps = connection.prepareStatement( "INSERT INTO Singers\n" @@ -80,9 +80,9 @@ static void insertData(String projectId, String instanceId, String databaseId) ps.addBatch(); } int[] updateCounts = ps.executeBatch(); - connection.commit(); System.out.printf("Insert counts: %s%n", Arrays.toString(updateCounts)); } } } } +//[END spanner_jdbc_insert] diff --git a/spanner/jdbc/src/main/java/com/example/spanner/jdbc/SingleUseReadOnlyExample.java b/spanner/jdbc/src/main/java/com/example/spanner/jdbc/SingleUseReadOnlyExample.java index 409eed9b233..4d2ffcc6b02 100644 --- a/spanner/jdbc/src/main/java/com/example/spanner/jdbc/SingleUseReadOnlyExample.java +++ b/spanner/jdbc/src/main/java/com/example/spanner/jdbc/SingleUseReadOnlyExample.java @@ -16,6 +16,7 @@ package com.example.spanner.jdbc; +//[START spanner_jdbc_query] import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.sql.Connection; import java.sql.DriverManager; @@ -57,3 +58,4 @@ static void singleUseReadOnly(String projectId, String instanceId, String databa } } } +//[END spanner_jdbc_query] diff --git a/spanner/leaderboard/complete/pom.xml b/spanner/leaderboard/complete/pom.xml index e3561a5de6e..be3cd2f06f9 100644 --- a/spanner/leaderboard/complete/pom.xml +++ b/spanner/leaderboard/complete/pom.xml @@ -25,7 +25,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -33,7 +33,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -90,12 +90,12 @@ org.apache.maven.plugins maven-failsafe-plugin - 3.0.0-M4 + 3.0.0-M5 org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 false diff --git a/spanner/leaderboard/step4/pom.xml b/spanner/leaderboard/step4/pom.xml index 71b7e9e325a..704e5874c26 100644 --- a/spanner/leaderboard/step4/pom.xml +++ b/spanner/leaderboard/step4/pom.xml @@ -25,7 +25,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -33,7 +33,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -84,12 +84,12 @@ org.apache.maven.plugins maven-failsafe-plugin - 3.0.0-M4 + 3.0.0-M5 org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 false diff --git a/spanner/leaderboard/step5/pom.xml b/spanner/leaderboard/step5/pom.xml index 71b7e9e325a..704e5874c26 100644 --- a/spanner/leaderboard/step5/pom.xml +++ b/spanner/leaderboard/step5/pom.xml @@ -25,7 +25,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -33,7 +33,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -84,12 +84,12 @@ org.apache.maven.plugins maven-failsafe-plugin - 3.0.0-M4 + 3.0.0-M5 org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 false diff --git a/spanner/leaderboard/step6/pom.xml b/spanner/leaderboard/step6/pom.xml index a7f894b15ee..cb4255a5b86 100644 --- a/spanner/leaderboard/step6/pom.xml +++ b/spanner/leaderboard/step6/pom.xml @@ -25,7 +25,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -33,7 +33,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -84,12 +84,12 @@ org.apache.maven.plugins maven-failsafe-plugin - 3.0.0-M4 + 3.0.0-M5 org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M4 + 3.0.0-M5 false diff --git a/spanner/spring-data/pom.xml b/spanner/spring-data/pom.xml index e4f5f5d4663..b15fec2dc42 100644 --- a/spanner/spring-data/pom.xml +++ b/spanner/spring-data/pom.xml @@ -28,7 +28,7 @@ limitations under the License. com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 com.example.spanner @@ -41,7 +41,7 @@ limitations under the License. org.springframework.cloud spring-cloud-gcp-dependencies - 1.2.2.RELEASE + 1.2.3.RELEASE pom import @@ -61,7 +61,7 @@ limitations under the License. org.springframework.boot spring-boot-maven-plugin - 2.3.0.RELEASE + 2.3.1.RELEASE com.example.spanner.QuickStartSample diff --git a/speech/beta/pom.xml b/speech/beta/pom.xml index 3cfd9c5e2fd..8ad8b8d00ba 100644 --- a/speech/beta/pom.xml +++ b/speech/beta/pom.xml @@ -26,7 +26,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -40,7 +40,7 @@ com.google.cloud google-cloud-speech - 1.23.0 + 1.24.0 @@ -90,7 +90,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 @@ -119,7 +119,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 diff --git a/speech/cloud-client/pom.xml b/speech/cloud-client/pom.xml index a4e0e13bf6c..4244366e124 100644 --- a/speech/cloud-client/pom.xml +++ b/speech/cloud-client/pom.xml @@ -26,7 +26,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -40,7 +40,7 @@ com.google.cloud google-cloud-speech - 1.23.0 + 1.24.0 @@ -94,7 +94,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 @@ -123,7 +123,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 @@ -151,7 +151,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 diff --git a/storage/cloud-client/pom.xml b/storage/cloud-client/pom.xml index b24c2c2f8c1..6d59d6160f3 100644 --- a/storage/cloud-client/pom.xml +++ b/storage/cloud-client/pom.xml @@ -26,7 +26,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -39,7 +39,7 @@ com.google.cloud google-cloud-storage - 1.108.0 + 1.111.0 diff --git a/storage/json-api/pom.xml b/storage/json-api/pom.xml index eaf8c07309d..442a16a6e55 100644 --- a/storage/json-api/pom.xml +++ b/storage/json-api/pom.xml @@ -26,7 +26,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -49,6 +49,11 @@ + + com.google.auth + google-auth-library-oauth2-http + 0.21.0 + com.google.guava guava diff --git a/storage/json-api/src/main/java/CustomerSuppliedEncryptionKeysSamples.java b/storage/json-api/src/main/java/CustomerSuppliedEncryptionKeysSamples.java index df3f5a57717..f5a9db0d596 100644 --- a/storage/json-api/src/main/java/CustomerSuppliedEncryptionKeysSamples.java +++ b/storage/json-api/src/main/java/CustomerSuppliedEncryptionKeysSamples.java @@ -25,15 +25,15 @@ /** * Demonstrates the use of GCS's CSEK features via the Java API client library * - * This program demonstrates some quick, basic examples of using GCS's CSEK functionality. + * This program demonstrates some quick, basic examples of using GCS's CSEK functionality. * - *

When run, it begins by uploading an object named "encrypted_file.txt" to the specified bucket - * that will be protected with a provided CSEK.

+ * When run, it begins by uploading an object named "encrypted_file.txt" to the specified bucket + * that will be protected with a provided CSEK. * - *

Next, it will fetch that object by providing that same CSEK to GCS.

+ * Next, it will fetch that object by providing that same CSEK to GCS. * - *

Finally, it will rotate that key to a new value.

- **/ + * Finally, it will rotate that key to a new value. + */ class CustomerSuppliedEncryptionKeysSamples { // You can (and should) generate your own CSEK Key! Try running this from the command line: @@ -62,9 +62,7 @@ class CustomerSuppliedEncryptionKeysSamples { * @param objectName The name of the destination object * @param base64CseKey An AES256 key, encoded as a base64 string. * @param base64CseKeyHash The SHA-256 hash of the above key, also encoded as a base64 string. - * * @return An InputStream that contains the decrypted contents of the object. - * * @throws IOException if there was some error download from GCS. */ public static InputStream downloadObject( @@ -148,12 +146,12 @@ public static void uploadObject( * @param storage A Storage object, ready for use * @param bucketName The name of the destination bucket * @param objectName The name of the destination object - * @param originalBase64Key The AES256 key currently associated with this object, - * encoded as a base64 string. - * @param originalBase64KeyHash The SHA-256 hash of the above key, - * also encoded as a base64 string. - * @param newBase64Key An AES256 key which will replace the existing key, - * encoded as a base64 string. + * @param originalBase64Key The AES256 key currently associated with this object, encoded as a + * base64 string. + * @param originalBase64KeyHash The SHA-256 hash of the above key, also encoded as a base64 + * string. + * @param newBase64Key An AES256 key which will replace the existing key, encoded as a base64 + * string. * @param newBase64KeyHash The SHA-256 hash of the above key, also encoded as a base64 string. * @throws IOException if there was some error download from GCS. */ @@ -203,10 +201,11 @@ public static void rotateKey( public static void main(String[] args) throws Exception { if (args.length != 1) { - System.out.println("\nPlease run this with one argument: " - + "the GCS bucket into which this program should upload an object.\n\n" - + "You can create a bucket using gsutil like this:\n\n\t" - + "gsutil mb gs://name-of-bucket\n\n"); + System.out.println( + "\nPlease run this with one argument: " + + "the GCS bucket into which this program should upload an object.\n\n" + + "You can create a bucket using gsutil like this:\n\n\t" + + "gsutil mb gs://name-of-bucket\n\n"); System.exit(1); } String bucketName = args[0]; @@ -223,10 +222,15 @@ public static void main(String[] args) throws Exception { StorageUtils.readStream(objectData); System.out.println("Rotating object to use a different CSEK."); - rotateKey(storage, bucketName, OBJECT_NAME, CSEK_KEY, CSEK_KEY_HASH, - ANOTHER_CESK_KEY, ANOTHER_CSEK_KEY_HASH); + rotateKey( + storage, + bucketName, + OBJECT_NAME, + CSEK_KEY, + CSEK_KEY_HASH, + ANOTHER_CESK_KEY, + ANOTHER_CSEK_KEY_HASH); System.out.println("Done"); } - } diff --git a/storage/json-api/src/main/java/StorageFactory.java b/storage/json-api/src/main/java/StorageFactory.java index fb47188d05f..caa9729006b 100644 --- a/storage/json-api/src/main/java/StorageFactory.java +++ b/storage/json-api/src/main/java/StorageFactory.java @@ -14,20 +14,19 @@ * limitations under the License. */ -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.http.HttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.storage.Storage; import com.google.api.services.storage.StorageScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Collection; -/** - * This class manages the details of creating a Storage service, including auth. - */ +/** This class manages the details of creating a Storage service, including auth. */ // [START authentication_application_default_credentials] public class StorageFactory { private static Storage instance = null; @@ -42,7 +41,7 @@ public static synchronized Storage getService() throws IOException, GeneralSecur private static Storage buildService() throws IOException, GeneralSecurityException { HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); JsonFactory jsonFactory = new JacksonFactory(); - GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory); + GoogleCredentials credential = GoogleCredentials.getApplicationDefault(); // Depending on the environment that provides the default credentials (for // example: Compute Engine, App Engine), the credentials may require us to @@ -53,7 +52,7 @@ private static Storage buildService() throws IOException, GeneralSecurityExcepti credential = credential.createScoped(scopes); } - return new Storage.Builder(transport, jsonFactory, credential) + return new Storage.Builder(transport, jsonFactory, new HttpCredentialsAdapter(credential)) .setApplicationName("GCS Samples") .build(); } diff --git a/storage/json-api/src/main/java/StorageSample.java b/storage/json-api/src/main/java/StorageSample.java index 9ea3a139065..9ce6f32a398 100644 --- a/storage/json-api/src/main/java/StorageSample.java +++ b/storage/json-api/src/main/java/StorageSample.java @@ -14,7 +14,7 @@ * limitations under the License. */ -//[START all] +// [START all] import com.google.api.client.http.InputStreamContent; import com.google.api.services.storage.Storage; @@ -98,24 +98,24 @@ public static Bucket getBucket(String bucketName) throws IOException, GeneralSec * @param file the file to upload. * @param bucketName the name of the bucket to create the object in. */ - public static void uploadFile( - String name, String contentType, File file, String bucketName) + public static void uploadFile(String name, String contentType, File file, String bucketName) throws IOException, GeneralSecurityException { - InputStreamContent contentStream = new InputStreamContent( - contentType, new FileInputStream(file)); + InputStreamContent contentStream = + new InputStreamContent(contentType, new FileInputStream(file)); // Setting the length improves upload performance contentStream.setLength(file.length()); - StorageObject objectMetadata = new StorageObject() - // Set the destination object name - .setName(name) - // Set the access control list to publicly read-only - .setAcl(Arrays.asList( - new ObjectAccessControl().setEntity("allUsers").setRole("READER"))); + StorageObject objectMetadata = + new StorageObject() + // Set the destination object name + .setName(name) + // Set the access control list to publicly read-only + .setAcl( + Arrays.asList(new ObjectAccessControl().setEntity("allUsers").setRole("READER"))); // Do the insert Storage client = StorageFactory.getService(); - Storage.Objects.Insert insertRequest = client.objects().insert( - bucketName, objectMetadata, contentStream); + Storage.Objects.Insert insertRequest = + client.objects().insert(bucketName, objectMetadata, contentStream); insertRequest.execute(); } @@ -156,7 +156,6 @@ public static void main(String[] args) { System.out.println("timeCreated: " + bucket.getTimeCreated()); System.out.println("owner: " + bucket.getOwner()); - // List the contents of the bucket. List bucketContents = listBucket(bucketName); if (null == bucketContents) { @@ -187,4 +186,4 @@ public static void main(String[] args) { } } } -//[END all] +// [END all] diff --git a/storage/json-api/src/main/java/StorageUtils.java b/storage/json-api/src/main/java/StorageUtils.java index 6ef874666ea..3a5edfb79c1 100644 --- a/storage/json-api/src/main/java/StorageUtils.java +++ b/storage/json-api/src/main/java/StorageUtils.java @@ -19,9 +19,7 @@ public class StorageUtils { - /** - * Reads the contents of an InputStream and does nothing with it. - */ + /** Reads the contents of an InputStream and does nothing with it. */ public static void readStream(InputStream is) throws IOException { byte[] inputBuffer = new byte[256]; while (is.read(inputBuffer) != -1) {} @@ -30,8 +28,8 @@ public static void readStream(InputStream is) throws IOException { } /** - * A helper class to provide input streams of any size. - * The input streams will be full of null bytes. + * A helper class to provide input streams of any size. The input streams will be full of null + * bytes. */ static class ArbitrarilyLargeInputStream extends InputStream { @@ -53,4 +51,3 @@ public int read() throws IOException { } } } - diff --git a/storage/s3-sdk/pom.xml b/storage/s3-sdk/pom.xml index 1f2538096f5..ed500287c17 100644 --- a/storage/s3-sdk/pom.xml +++ b/storage/s3-sdk/pom.xml @@ -26,7 +26,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -38,7 +38,7 @@ com.amazonaws aws-java-sdk-s3 - 1.11.788 + 1.11.811 diff --git a/storage/storage-transfer/pom.xml b/storage/storage-transfer/pom.xml index 1dd0e174b90..7ff3b44557b 100644 --- a/storage/storage-transfer/pom.xml +++ b/storage/storage-transfer/pom.xml @@ -30,7 +30,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -43,7 +43,7 @@ com.google.apis google-api-services-storagetransfer - v1-rev20200316-1.30.9 + v1-rev20200605-1.30.9 com.google.guava @@ -51,7 +51,11 @@ - + + com.google.auth + google-auth-library-oauth2-http + 0.21.0 + com.google.guava guava diff --git a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/AwsRequester.java b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/AwsRequester.java index e2c0835030a..0c6f067fa3b 100644 --- a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/AwsRequester.java +++ b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/AwsRequester.java @@ -30,24 +30,19 @@ import java.io.IOException; import java.io.PrintStream; -/** - * Creates a one-off transfer job from Amazon S3 to Google Cloud Storage. - */ +/** Creates a one-off transfer job from Amazon S3 to Google Cloud Storage. */ public final class AwsRequester { /** * Creates and executes a request for a TransferJob from Amazon S3 to Cloud Storage. * - *

The {@code startDate} and {@code startTime} parameters should be set according to the UTC + * The {@code startDate} and {@code startTime} parameters should be set according to the UTC * Time Zone. See: * https://developers.google.com/resources/api-libraries/documentation/storagetransfer/v1/java/latest/com/google/api/services/storagetransfer/v1/model/Schedule.html#getStartTimeOfDay() * * @return the response TransferJob if the request is successful - * @throws InstantiationException - * if instantiation fails when building the TransferJob - * @throws IllegalAccessException - * if an illegal access occurs when building the TransferJob - * @throws IOException - * if the client failed to complete the request + * @throws InstantiationException if instantiation fails when building the TransferJob + * @throws IllegalAccessException if an illegal access occurs when building the TransferJob + * @throws IOException if the client failed to complete the request */ public static TransferJob createAwsTransferJob( String projectId, @@ -110,9 +105,7 @@ public static void run(PrintStream out) out.println("Return transferJob: " + responseT.toPrettyString()); } - /** - * Output the contents of a successfully created TransferJob. - */ + /** Output the contents of a successfully created TransferJob. */ public static void main(String[] args) { try { run(System.out); @@ -121,4 +114,4 @@ public static void main(String[] args) { } } } -//[END all] +// [END all] diff --git a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/NearlineRequester.java b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/NearlineRequester.java index 87e98b3d2d6..8be0f3a085e 100644 --- a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/NearlineRequester.java +++ b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/NearlineRequester.java @@ -31,25 +31,22 @@ import java.io.PrintStream; /** - * Creates a daily transfer from a standard Cloud Storage bucket to a Cloud Storage Nearline - * bucket for files untouched for 30 days. + * Creates a daily transfer from a standard Cloud Storage bucket to a Cloud Storage Nearline bucket + * for files untouched for 30 days. */ public final class NearlineRequester { /** * Creates and executes a request for a TransferJob to Cloud Storage Nearline. * - *

The {@code startDate} and {@code startTime} parameters should be set according to the UTC + * The {@code startDate} and {@code startTime} parameters should be set according to the UTC * Time Zone. See: * https://developers.google.com/resources/api-libraries/documentation/storagetransfer/v1/java/latest/com/google/api/services/storagetransfer/v1/model/Schedule.html#getStartTimeOfDay() * * @return the response TransferJob if the request is successful - * @throws InstantiationException - * if instantiation fails when building the TransferJob - * @throws IllegalAccessException - * if an illegal access occurs when building the TransferJob - * @throws IOException - * if the client failed to complete the request + * @throws InstantiationException if instantiation fails when building the TransferJob + * @throws IllegalAccessException if an illegal access occurs when building the TransferJob + * @throws IOException if the client failed to complete the request */ public static TransferJob createNearlineTransferJob( String projectId, @@ -73,10 +70,8 @@ public static TransferJob createNearlineTransferJob( new ObjectConditions() .setMinTimeElapsedSinceLastModification("2592000s" /* 30 days */)) .setTransferOptions( - new TransferOptions() - .setDeleteObjectsFromSourceAfterTransfer(true))) - .setSchedule( - new Schedule().setScheduleStartDate(date).setStartTimeOfDay(time)) + new TransferOptions().setDeleteObjectsFromSourceAfterTransfer(true))) + .setSchedule(new Schedule().setScheduleStartDate(date).setStartTimeOfDay(time)) .setStatus("ENABLED"); Storagetransfer client = TransferClientCreator.createStorageTransferClient(); @@ -106,8 +101,7 @@ public static void run(PrintStream out) /** * Output the contents of a successfully created TransferJob. * - * @param args - * arguments from the command line + * @param args arguments from the command line */ public static void main(String[] args) { try { @@ -117,4 +111,4 @@ public static void main(String[] args) { } } } -//[END all] +// [END all] diff --git a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/RequestChecker.java b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/RequestChecker.java index f10257e8fb7..68e52c2b0a9 100644 --- a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/RequestChecker.java +++ b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/RequestChecker.java @@ -26,7 +26,6 @@ /** * Queries for TransferOperations associated with a specific TransferJob. A TransferJob is done when * all of its associated TransferOperations have completed. - * */ public final class RequestChecker { @@ -38,15 +37,11 @@ public final class RequestChecker { /** * Creates and executes a query for all associated TransferOperations. * - * @param client - * a Storagetransfer client, for interacting with the Storage Transfer API - * @param projectId - * the project to query within - * @param jobName - * the job Name of the relevant TransferJob + * @param client a Storagetransfer client, for interacting with the Storage Transfer API + * @param projectId the project to query within + * @param jobName the job Name of the relevant TransferJob * @return an object containing information on associated TransferOperations - * @throws IOException - * if the client failed to complete the request + * @throws IOException if the client failed to complete the request */ public static ListOperationsResponse checkTransfer( Storagetransfer client, String projectId, String jobName) throws IOException { @@ -60,8 +55,7 @@ public static ListOperationsResponse checkTransfer( /** * Output the returned list of TransferOperations. * - * @param args - * arguments from the command line + * @param args arguments from the command line */ public static void main(String[] args) { try { @@ -73,4 +67,4 @@ public static void main(String[] args) { } } } -//[END all] +// [END all] diff --git a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/RetryHttpInitializerWrapper.java b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/RetryHttpInitializerWrapper.java index 55b11ebb63b..cfb824dceb5 100644 --- a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/RetryHttpInitializerWrapper.java +++ b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/RetryHttpInitializerWrapper.java @@ -45,8 +45,8 @@ public class RetryHttpInitializerWrapper implements HttpRequestInitializer { /** * A constructor using the default Sleeper. * - * @param wrappedCredential - * the credential used to authenticate with a Google Cloud Platform project + * @param wrappedCredential the credential used to authenticate with a Google Cloud Platform + * project */ public RetryHttpInitializerWrapper(Credential wrappedCredential) { this(wrappedCredential, Sleeper.DEFAULT); @@ -55,10 +55,9 @@ public RetryHttpInitializerWrapper(Credential wrappedCredential) { /** * A constructor used only for testing. * - * @param wrappedCredential - * the credential used to authenticate with a Google Cloud Platform project - * @param sleeper - * a user-supplied Sleeper + * @param wrappedCredential the credential used to authenticate with a Google Cloud Platform + * project + * @param sleeper a user-supplied Sleeper */ RetryHttpInitializerWrapper(Credential wrappedCredential, Sleeper sleeper) { this.wrappedCredential = Preconditions.checkNotNull(wrappedCredential); @@ -68,8 +67,7 @@ public RetryHttpInitializerWrapper(Credential wrappedCredential) { /** * Initialize an HttpRequest. * - * @param request - * an HttpRequest that should be initialized + * @param request an HttpRequest that should be initialized */ public void initialize(HttpRequest request) { request.setReadTimeout(2 * MILLIS_PER_MINUTE); // 2 minutes read timeout @@ -98,4 +96,4 @@ public boolean handleResponse( new HttpBackOffIOExceptionHandler(new ExponentialBackOff()).setSleeper(sleeper)); } } -//[END all] +// [END all] diff --git a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/TransferClientCreator.java b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/TransferClientCreator.java index d64e0619fea..a74c044a56c 100644 --- a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/TransferClientCreator.java +++ b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/TransferClientCreator.java @@ -18,19 +18,18 @@ package com.google.cloud.storage.storagetransfer.samples; -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.util.Utils; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.HttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.services.storagetransfer.v1.Storagetransfer; import com.google.api.services.storagetransfer.v1.StoragetransferScopes; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import com.google.common.base.Preconditions; import java.io.IOException; -/** - * Create a client to make calls to Storage Transfer API. - */ +/** Create a client to make calls to Storage Transfer API. */ public final class TransferClientCreator { /** @@ -38,30 +37,25 @@ public final class TransferClientCreator { * settings. * * @return a Storage Transfer client - * @throws IOException - * there was an error obtaining application default credentials + * @throws IOException there was an error obtaining application default credentials */ public static Storagetransfer createStorageTransferClient() throws IOException { HttpTransport httpTransport = Utils.getDefaultTransport(); JsonFactory jsonFactory = Utils.getDefaultJsonFactory(); - GoogleCredential credential = - GoogleCredential.getApplicationDefault(httpTransport, jsonFactory); + GoogleCredentials credential = GoogleCredentials.getApplicationDefault(); return createStorageTransferClient(httpTransport, jsonFactory, credential); } /** * Create a Storage Transfer client using user-supplied credentials and other settings. * - * @param httpTransport - * a user-supplied HttpTransport - * @param jsonFactory - * a user-supplied JsonFactory - * @param credential - * a user-supplied Google credential + * @param httpTransport a user-supplied HttpTransport + * @param jsonFactory a user-supplied JsonFactory + * @param credential a user-supplied Google credential * @return a Storage Transfer client */ public static Storagetransfer createStorageTransferClient( - HttpTransport httpTransport, JsonFactory jsonFactory, GoogleCredential credential) { + HttpTransport httpTransport, JsonFactory jsonFactory, GoogleCredentials credential) { Preconditions.checkNotNull(httpTransport); Preconditions.checkNotNull(jsonFactory); Preconditions.checkNotNull(credential); @@ -73,10 +67,10 @@ public static Storagetransfer createStorageTransferClient( // Please use custom HttpRequestInitializer for automatic // retry upon failures. We provide a simple reference // implementation in the "Retry Handling" section. - HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential); + HttpRequestInitializer initializer = new HttpCredentialsAdapter(credential); return new Storagetransfer.Builder(httpTransport, jsonFactory, initializer) .setApplicationName("storagetransfer-sample") .build(); } } -//[END all] +// [END all] diff --git a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/TransferJobUtils.java b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/TransferJobUtils.java index ff3b5111250..80be773d46f 100644 --- a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/TransferJobUtils.java +++ b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/TransferJobUtils.java @@ -21,30 +21,21 @@ import com.google.api.services.storagetransfer.v1.model.Date; import com.google.api.services.storagetransfer.v1.model.TimeOfDay; -/** - * Utility methods for creating TransferJobs. - * - */ +/** Utility methods for creating TransferJobs. */ public final class TransferJobUtils { private static final int BASE_10 = 10; - /** - * A private constructor. - */ + /** A private constructor. */ private TransferJobUtils() {} /** * Parses a Date from a string of the form "YYYY-MM-DD". * - * @param dateString - * a string of the form "YYYY-MM-DD" + * @param dateString a string of the form "YYYY-MM-DD" * @return a Google Date representing the desired date - * @throws NumberFormatException - * if the input string has an incorrect format - * @throws InstantiationException - * if Date object instantiation failed - * @throws IllegalAccessException - * if Date object cannot be accessed + * @throws NumberFormatException if the input string has an incorrect format + * @throws InstantiationException if Date object instantiation failed + * @throws IllegalAccessException if Date object cannot be accessed */ public static Date createDate(String dateString) throws NumberFormatException, InstantiationException, IllegalAccessException { @@ -60,15 +51,11 @@ public static Date createDate(String dateString) /** * Parses a TimeOfDay from a string of the form "HH:MM:SS". * - * @param timeString - * a string of the form "HH:MM:SS" + * @param timeString a string of the form "HH:MM:SS" * @return a TimeOfDay representing the desired time - * @throws NumberFormatException - * if the input string has an incorrect format - * @throws InstantiationException - * if Date object instantiation failed - * @throws IllegalAccessException - * if Date object cannot be accessed + * @throws NumberFormatException if the input string has an incorrect format + * @throws InstantiationException if Date object instantiation failed + * @throws IllegalAccessException if Date object cannot be accessed */ public static TimeOfDay createTimeOfDay(String timeString) throws NumberFormatException, InstantiationException, IllegalAccessException { @@ -99,4 +86,4 @@ public static String getPropertyOrFail(String propertyName) { return propertyValue; } } -//[END all] +// [END all] diff --git a/storage/xml-api/cmdline-sample/pom.xml b/storage/xml-api/cmdline-sample/pom.xml index e5e68e0db92..4292143b0b7 100644 --- a/storage/xml-api/cmdline-sample/pom.xml +++ b/storage/xml-api/cmdline-sample/pom.xml @@ -26,7 +26,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -42,7 +42,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 @@ -78,6 +78,11 @@ + + com.google.auth + google-auth-library-oauth2-http + 0.21.0 + com.google.http-client google-http-client-jackson2 diff --git a/storage/xml-api/cmdline-sample/src/main/java/StorageSample.java b/storage/xml-api/cmdline-sample/src/main/java/StorageSample.java index b0382e5225e..593656f3c4d 100644 --- a/storage/xml-api/cmdline-sample/src/main/java/StorageSample.java +++ b/storage/xml-api/cmdline-sample/src/main/java/StorageSample.java @@ -14,7 +14,6 @@ * limitations under the License. */ -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.http.GenericUrl; import com.google.api.client.http.HttpRequest; @@ -22,6 +21,8 @@ import com.google.api.client.http.HttpResponse; import com.google.api.client.http.HttpTransport; import com.google.api.client.util.Preconditions; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; @@ -43,7 +44,7 @@ public final class StorageSample { /** This class is never instantiated. */ - private StorageSample() { } + private StorageSample() {} /** Global configuration of Google Cloud Storage OAuth 2.0 scope. */ private static final String STORAGE_SCOPE = @@ -53,31 +54,30 @@ private StorageSample() { } * Fetches the listing of the given bucket. * * @param bucketName the name of the bucket to list. - * * @return the raw XML containing the listing of the bucket. * @throws IOException if there's an error communicating with Cloud Storage. * @throws GeneralSecurityException for errors creating https connection. */ public static String listBucket(final String bucketName) throws IOException, GeneralSecurityException { - //[START snippet] + // [START snippet] // Build an account credential. - GoogleCredential credential = GoogleCredential.getApplicationDefault() - .createScoped(Collections.singleton(STORAGE_SCOPE)); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault() + .createScoped(Collections.singleton(STORAGE_SCOPE)); // Set up and execute a Google Cloud Storage request. - String uri = "https://storage.googleapis.com/" - + URLEncoder.encode(bucketName, "UTF-8"); + String uri = "https://storage.googleapis.com/" + URLEncoder.encode(bucketName, "UTF-8"); HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); - HttpRequestFactory requestFactory = httpTransport.createRequestFactory( - credential); + HttpRequestFactory requestFactory = + httpTransport.createRequestFactory(new HttpCredentialsAdapter(credential)); GenericUrl url = new GenericUrl(uri); HttpRequest request = requestFactory.buildGetRequest(url); HttpResponse response = request.execute(); String content = response.parseAsString(); - //[END snippet] + // [END snippet] return content; } @@ -88,21 +88,19 @@ public static String listBucket(final String bucketName) * @param bucketName the name of the bucket you're listing. * @param content the raw XML string. */ - private static void prettyPrintXml( - final String bucketName, final String content) { + private static void prettyPrintXml(final String bucketName, final String content) { // Instantiate transformer input. Source xmlInput = new StreamSource(new StringReader(content)); StreamResult xmlOutput = new StreamResult(new StringWriter()); // Configure transformer. try { - Transformer transformer = TransformerFactory.newInstance() - .newTransformer(); // An identity transformer + Transformer transformer = + TransformerFactory.newInstance().newTransformer(); // An identity transformer transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); - transformer.setOutputProperty( - "{http://xml.apache.org/xslt}indent-amount", "2"); + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(xmlInput, xmlOutput); // Pretty print the output XML. @@ -122,8 +120,7 @@ public static void main(final String[] args) { try { // Check for valid setup. Preconditions.checkArgument( - args.length == 1, - "Please pass in the Google Cloud Storage bucket name to display"); + args.length == 1, "Please pass in the Google Cloud Storage bucket name to display"); String bucketName = args[0]; String content = listBucket(bucketName); diff --git a/storage/xml-api/serviceaccount-appengine-sample/pom.xml b/storage/xml-api/serviceaccount-appengine-sample/pom.xml index 8e9608f1c6f..fafcead1a08 100644 --- a/storage/xml-api/serviceaccount-appengine-sample/pom.xml +++ b/storage/xml-api/serviceaccount-appengine-sample/pom.xml @@ -23,7 +23,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 com.google.apis-samples @@ -41,7 +41,7 @@ UTF-8 1.1 3.0.5 - 3.2.3 + 3.3.0 @@ -88,7 +88,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG gaeinfo diff --git a/tasks/pom.xml b/tasks/pom.xml index 45aa6c8d9b0..e1a8ed6cf8c 100644 --- a/tasks/pom.xml +++ b/tasks/pom.xml @@ -29,7 +29,7 @@ Copyright 2018 Google LLC com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -43,12 +43,12 @@ Copyright 2018 Google LLC com.google.cloud google-cloud-tasks - 1.29.1 + 1.30.0 com.google.protobuf protobuf-java - 3.12.1 + 3.12.2 diff --git a/tasks/src/main/java/com/example/task/CreateHttpTask.java b/tasks/src/main/java/com/example/task/CreateHttpTask.java index 858b439ae58..e86636bbc6b 100644 --- a/tasks/src/main/java/com/example/task/CreateHttpTask.java +++ b/tasks/src/main/java/com/example/task/CreateHttpTask.java @@ -23,19 +23,22 @@ import com.google.cloud.tasks.v2.QueueName; import com.google.cloud.tasks.v2.Task; import com.google.protobuf.ByteString; +import java.io.IOException; import java.nio.charset.Charset; public class CreateHttpTask { - /** - * Create a task with a HTTP target using the Cloud Tasks client. - * - * @param projectId the Id of the project. - * @param queueId the name of your Queue. - * @param locationId the GCP region of your queue. - * @throws Exception on Cloud Tasks Client errors. - */ + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String locationId = "us-central1"; + String queueId = "my-queue"; + createTask(projectId, locationId, queueId); + } + + // Create a task with a HTTP target using the Cloud Tasks client. public static void createTask(String projectId, String locationId, String queueId) - throws Exception { + throws IOException { // Instantiates a client. try (CloudTasksClient client = CloudTasksClient.create()) { diff --git a/tasks/src/main/java/com/example/task/CreateHttpTaskWithToken.java b/tasks/src/main/java/com/example/task/CreateHttpTaskWithToken.java index 72c4768ef35..a341f680fea 100644 --- a/tasks/src/main/java/com/example/task/CreateHttpTaskWithToken.java +++ b/tasks/src/main/java/com/example/task/CreateHttpTaskWithToken.java @@ -24,21 +24,25 @@ import com.google.cloud.tasks.v2.QueueName; import com.google.cloud.tasks.v2.Task; import com.google.protobuf.ByteString; +import java.io.IOException; import java.nio.charset.Charset; public class CreateHttpTaskWithToken { - /** - * Create a task with a HTTP target and authorization token using the Cloud Tasks client. - * - * @param projectId the Id of the project. - * @param queueId the name of your Queue. - * @param locationId the GCP region of your queue. - * @param serviceAccountEmail your Cloud IAM service account - * @throws Exception on Cloud Tasks Client errors. - */ + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String locationId = "us-central1"; + String queueId = "my-queue"; + String serviceAccountEmail = + "java-docs-samples-testing@java-docs-samples-testing.iam.gserviceaccount.com"; + createTask(projectId, locationId, queueId, serviceAccountEmail); + } + + // Create a task with a HTTP target and authorization token using the Cloud Tasks client. public static void createTask( String projectId, String locationId, String queueId, String serviceAccountEmail) - throws Exception { + throws IOException { // Instantiates a client. try (CloudTasksClient client = CloudTasksClient.create()) { diff --git a/tasks/src/main/java/com/example/task/CreateQueue.java b/tasks/src/main/java/com/example/task/CreateQueue.java new file mode 100644 index 00000000000..04aa81101ad --- /dev/null +++ b/tasks/src/main/java/com/example/task/CreateQueue.java @@ -0,0 +1,54 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.example.task; + +// [START cloud_tasks_create_queue] +import com.google.cloud.tasks.v2.CloudTasksClient; +import com.google.cloud.tasks.v2.LocationName; +import com.google.cloud.tasks.v2.Queue; +import com.google.cloud.tasks.v2.QueueName; +import java.io.IOException; + +public class CreateQueue { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String locationId = "us-central1"; + String queueId = "my-queue"; + createQueue(projectId, locationId, queueId); + } + + // Create a queue using the Cloud Tasks client. + public static void createQueue(String projectId, String locationId, String queueId) + throws IOException { + + // Instantiates a client. + try (CloudTasksClient client = CloudTasksClient.create()) { + + // Construct the fully qualified location. + String parent = LocationName.of(projectId, locationId).toString(); + + // Construct the fully qualified queue path. + String queuePath = QueueName.of(projectId, locationId, queueId).toString(); + + // Send create queue request. + Queue queue = client.createQueue(parent, Queue.newBuilder().setName(queuePath).build()); + + System.out.println("Queue created: " + queue.getName()); + } + } +} +// [END cloud_tasks_create_queue] diff --git a/tasks/src/main/java/com/example/task/DeleteQueue.java b/tasks/src/main/java/com/example/task/DeleteQueue.java new file mode 100644 index 00000000000..6f5d9898b48 --- /dev/null +++ b/tasks/src/main/java/com/example/task/DeleteQueue.java @@ -0,0 +1,49 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.example.task; + +// [START cloud_tasks_delete_queue] +import com.google.cloud.tasks.v2.CloudTasksClient; +import com.google.cloud.tasks.v2.QueueName; +import java.io.IOException; + +public class DeleteQueue { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String locationId = "us-central1"; + String queueId = "my-queue"; + deleteQueue(projectId, locationId, queueId); + } + + // Delete a queue using the Cloud Tasks client. + public static void deleteQueue(String projectId, String locationId, String queueId) + throws IOException { + + // Instantiates a client. + try (CloudTasksClient client = CloudTasksClient.create()) { + + // Construct the fully qualified queue path. + String queuePath = QueueName.of(projectId, locationId, queueId).toString(); + + // Send delete queue request. + client.deleteQueue(queuePath); + + System.out.println("Queue deleted: " + queueId); + } + } +} +// [END cloud_tasks_delete_queue] diff --git a/tasks/src/main/java/com/example/task/ListQueues.java b/tasks/src/main/java/com/example/task/ListQueues.java new file mode 100644 index 00000000000..85fd8c14cc6 --- /dev/null +++ b/tasks/src/main/java/com/example/task/ListQueues.java @@ -0,0 +1,58 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.example.task; + +// [START cloud_tasks_list_queues] +import com.google.cloud.tasks.v2.CloudTasksClient; +import com.google.cloud.tasks.v2.LocationName; +import com.google.cloud.tasks.v2.Queue; +import java.io.IOException; + +public class ListQueues { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String locationId = "us-central1"; + listQueues(projectId, locationId); + } + + // List queues using the Cloud Tasks client. + public static void listQueues(String projectId, String locationId) + throws IOException { + + // Instantiates a client. + try (CloudTasksClient client = CloudTasksClient.create()) { + + // Construct the fully qualified location path. + String parent = LocationName.of(projectId, locationId).toString(); + + // Send list queues request. + CloudTasksClient.ListQueuesPagedResponse response = client.listQueues(parent); + + // Iterate over results and print queue names + int total = 0; + for (Queue queue : response.iterateAll()) { + System.out.println(queue.getName()); + total++; + } + + if (total == 0) { + System.out.println("No queues found!"); + } + } + } +} +// [END cloud_tasks_list_queues] diff --git a/tasks/src/test/java/com/example/task/CreateHttpTaskIT.java b/tasks/src/test/java/com/example/task/CreateHttpTaskIT.java index e03d907a677..11fd89f0b12 100644 --- a/tasks/src/test/java/com/example/task/CreateHttpTaskIT.java +++ b/tasks/src/test/java/com/example/task/CreateHttpTaskIT.java @@ -17,6 +17,7 @@ package com.example.task; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNotNull; import com.google.cloud.tasks.v2.CloudTasksClient; import com.google.cloud.tasks.v2.QueueName; @@ -24,6 +25,7 @@ import java.io.PrintStream; import org.junit.After; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -31,14 +33,26 @@ /** Tests for creating Tasks with HTTP targets. */ @RunWith(JUnit4.class) public class CreateHttpTaskIT { - private static final String PROJECT_ID = "java-docs-samples-testing"; - private static final String LOCATION_ID = "us-east1"; + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String LOCATION_ID = System.getenv("LOCATION_ID"); private static final String QUEUE_ID = "default"; private static final String EMAIL = "java-docs-samples-testing@java-docs-samples-testing.iam.gserviceaccount.com"; private ByteArrayOutputStream bout; private PrintStream out; + private static void requireEnvVar(String varName) { + assertNotNull( + String.format("Environment variable '%s' must be set to perform these tests.", varName), + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + requireEnvVar("LOCATION_ID"); + } + @Before public void setUp() { bout = new ByteArrayOutputStream(); diff --git a/tasks/src/test/java/com/example/task/CreateQueueIT.java b/tasks/src/test/java/com/example/task/CreateQueueIT.java new file mode 100644 index 00000000000..8677ee30d09 --- /dev/null +++ b/tasks/src/test/java/com/example/task/CreateQueueIT.java @@ -0,0 +1,78 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.example.task; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.cloud.tasks.v2.CloudTasksClient; +import com.google.cloud.tasks.v2.QueueName; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for creating queues. */ +@RunWith(JUnit4.class) +public class CreateQueueIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String LOCATION_ID = System.getenv("LOCATION_ID"); + private static final String QUEUE_ID = "test-queue-" + UUID.randomUUID(); + + private ByteArrayOutputStream bout; + private PrintStream out; + + private static void requireEnvVar(String varName) { + assertNotNull( + String.format("Environment variable '%s' must be set to perform these tests.", varName), + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + requireEnvVar("LOCATION_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + try (CloudTasksClient client = CloudTasksClient.create()) { + String queuePath = QueueName.of(PROJECT_ID, LOCATION_ID, QUEUE_ID).toString(); + client.deleteQueue(queuePath); + } catch (Exception e) { + System.out.println("Error with queue deletion."); + } + System.setOut(null); + } + + @Test + public void testCreateQueue() throws Exception { + CreateQueue.createQueue(PROJECT_ID, LOCATION_ID, QUEUE_ID); + String got = bout.toString(); + assertThat(got).contains("Queue created:"); + } +} diff --git a/tasks/src/test/java/com/example/task/DeleteQueueIT.java b/tasks/src/test/java/com/example/task/DeleteQueueIT.java new file mode 100644 index 00000000000..90c3f74bf59 --- /dev/null +++ b/tasks/src/test/java/com/example/task/DeleteQueueIT.java @@ -0,0 +1,92 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.example.task; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.api.gax.rpc.NotFoundException; +import com.google.cloud.tasks.v2.CloudTasksClient; +import com.google.cloud.tasks.v2.LocationName; +import com.google.cloud.tasks.v2.Queue; +import com.google.cloud.tasks.v2.QueueName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for deleting queues. */ +@RunWith(JUnit4.class) +public class DeleteQueueIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String LOCATION_ID = System.getenv("LOCATION_ID"); + private static final String QUEUE_ID = "test-queue-" + UUID.randomUUID(); + + private ByteArrayOutputStream bout; + private PrintStream out; + private Queue queue; + + private static void requireEnvVar(String varName) { + assertNotNull( + String.format("Environment variable '%s' must be set to perform these tests.", varName), + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + requireEnvVar("LOCATION_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + + try (CloudTasksClient client = CloudTasksClient.create()) { + String parent = LocationName.of(PROJECT_ID, LOCATION_ID).toString(); + String queuePath = QueueName.of(PROJECT_ID, LOCATION_ID, QUEUE_ID).toString(); + queue = client.createQueue(parent, Queue.newBuilder().setName(queuePath).build()); + } catch (Exception e) { + System.out.println("Error with queue creation."); + } + } + + @After + public void tearDown() { + try (CloudTasksClient client = CloudTasksClient.create()) { + client.deleteQueue(queue.getName()); + } catch (IOException e) { + System.out.println("Error with queue deletion."); + } catch (NotFoundException e) { + System.out.println("Queue already successfully deleted"); + } + System.setOut(null); + } + + @Test + public void testDeleteQueue() throws Exception { + DeleteQueue.deleteQueue(PROJECT_ID, LOCATION_ID, QUEUE_ID); + String got = bout.toString(); + assertThat(got).contains("Queue deleted:"); + } +} diff --git a/tasks/src/test/java/com/example/task/ListQueuesIT.java b/tasks/src/test/java/com/example/task/ListQueuesIT.java new file mode 100644 index 00000000000..11e68b8e58e --- /dev/null +++ b/tasks/src/test/java/com/example/task/ListQueuesIT.java @@ -0,0 +1,90 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.example.task; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.api.gax.rpc.NotFoundException; +import com.google.cloud.tasks.v2.CloudTasksClient; +import com.google.cloud.tasks.v2.LocationName; +import com.google.cloud.tasks.v2.Queue; +import com.google.cloud.tasks.v2.QueueName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for listing queues. */ +@RunWith(JUnit4.class) +public class ListQueuesIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String LOCATION_ID = System.getenv("LOCATION_ID"); + private static final String QUEUE_ID = "test-queue-" + UUID.randomUUID(); + + private ByteArrayOutputStream bout; + private PrintStream out; + private Queue queue; + + private static void requireEnvVar(String varName) { + assertNotNull( + String.format("Environment variable '%s' must be set to perform these tests.", varName), + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + requireEnvVar("LOCATION_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + + try (CloudTasksClient client = CloudTasksClient.create()) { + String parent = LocationName.of(PROJECT_ID, LOCATION_ID).toString(); + String queuePath = QueueName.of(PROJECT_ID, LOCATION_ID, QUEUE_ID).toString(); + queue = client.createQueue(parent, Queue.newBuilder().setName(queuePath).build()); + } catch (Exception e) { + System.out.println("Error with queue creation."); + } + } + + @After + public void tearDown() { + try (CloudTasksClient client = CloudTasksClient.create()) { + client.deleteQueue(queue.getName()); + } catch (IOException e) { + System.out.println("Error with queue deletion."); + } + System.setOut(null); + } + + @Test + public void testListQueues() throws Exception { + ListQueues.listQueues(PROJECT_ID, LOCATION_ID); + String got = bout.toString(); + assertThat(got).contains(queue.getName()); + } +} diff --git a/texttospeech/beta/pom.xml b/texttospeech/beta/pom.xml index 42c552b0717..792fd4cec5e 100644 --- a/texttospeech/beta/pom.xml +++ b/texttospeech/beta/pom.xml @@ -23,7 +23,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -40,7 +40,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -91,7 +91,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 @@ -121,7 +121,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 @@ -151,7 +151,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 @@ -181,7 +181,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 diff --git a/texttospeech/cloud-client/pom.xml b/texttospeech/cloud-client/pom.xml index 7687aa72e02..1fdec203e20 100644 --- a/texttospeech/cloud-client/pom.xml +++ b/texttospeech/cloud-client/pom.xml @@ -23,7 +23,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -40,7 +40,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import diff --git a/trace/pom.xml b/trace/pom.xml index c22de75f538..0c7cbca20b5 100644 --- a/trace/pom.xml +++ b/trace/pom.xml @@ -28,7 +28,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 diff --git a/translate/automl/pom.xml b/translate/automl/pom.xml index c123f13aed9..ecc08778c19 100644 --- a/translate/automl/pom.xml +++ b/translate/automl/pom.xml @@ -26,7 +26,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -43,7 +43,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -58,7 +58,7 @@ com.google.cloud google-cloud-storage - 1.108.0 + 1.111.0 net.sourceforge.argparse4j @@ -95,7 +95,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 @@ -123,7 +123,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 @@ -151,7 +151,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 diff --git a/translate/cloud-client/pom.xml b/translate/cloud-client/pom.xml index d8f77d0eb0a..2180c98b267 100644 --- a/translate/cloud-client/pom.xml +++ b/translate/cloud-client/pom.xml @@ -26,7 +26,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -43,7 +43,7 @@ See https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import diff --git a/unittests/pom.xml b/unittests/pom.xml index 2924657c637..ec590eded4b 100644 --- a/unittests/pom.xml +++ b/unittests/pom.xml @@ -14,7 +14,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -81,7 +81,7 @@ org.apache.maven.plugins maven-war-plugin - 3.2.3 + 3.3.0 true @@ -98,7 +98,7 @@ com.google.cloud.tools appengine-maven-plugin - 2.2.0 + 2.3.0 GCLOUD_CONFIG gaeinfo diff --git a/video/beta/pom.xml b/video/beta/pom.xml index 60c56acc09f..ec0ce4b24a9 100644 --- a/video/beta/pom.xml +++ b/video/beta/pom.xml @@ -26,7 +26,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -43,7 +43,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -76,7 +76,7 @@ com.google.api.grpc proto-google-cloud-video-intelligence-v1p3beta1 - 0.84.1 + 0.86.0 @@ -111,7 +111,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 diff --git a/video/cloud-client/pom.xml b/video/cloud-client/pom.xml index 3203dba6fea..b848f612505 100644 --- a/video/cloud-client/pom.xml +++ b/video/cloud-client/pom.xml @@ -26,7 +26,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -39,7 +39,7 @@ com.google.cloud google-cloud-video-intelligence - 1.2.1 + 1.4.0 @@ -86,7 +86,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 diff --git a/video/cloud-client/src/test/java/com/example/video/QuickstartIT.java b/video/cloud-client/src/test/java/com/example/video/QuickstartIT.java index 5424752ba58..75fdac01658 100644 --- a/video/cloud-client/src/test/java/com/example/video/QuickstartIT.java +++ b/video/cloud-client/src/test/java/com/example/video/QuickstartIT.java @@ -51,6 +51,7 @@ public void test() throws Exception { String got = bout.toString(); // Test that the video with a cat has the whiskers label (may change). - assertThat(got.toUpperCase()).contains("WHISKERS"); + assertThat(got.toUpperCase()).contains("VIDEO LABEL DESCRIPTION"); + assertThat(got.toUpperCase()).contains("CONFIDENCE"); } } diff --git a/vision/automl/pom.xml b/vision/automl/pom.xml index 064a7d82922..01d8cc609ef 100644 --- a/vision/automl/pom.xml +++ b/vision/automl/pom.xml @@ -26,7 +26,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -43,7 +43,7 @@ com.google.cloud libraries-bom - 4.4.1 + 8.0.0 pom import @@ -92,7 +92,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 @@ -120,7 +120,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 @@ -148,7 +148,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 diff --git a/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/DatasetApi.java b/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/DatasetApi.java deleted file mode 100644 index bcf5ec74614..00000000000 --- a/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/DatasetApi.java +++ /dev/null @@ -1,356 +0,0 @@ -/* - * Copyright 2018 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.cloud.vision.samples.automl; - -// Imports the Google Cloud client library -import com.google.cloud.automl.v1beta1.AutoMlClient; -import com.google.cloud.automl.v1beta1.ClassificationProto.ClassificationType; -import com.google.cloud.automl.v1beta1.Dataset; -import com.google.cloud.automl.v1beta1.DatasetName; -import com.google.cloud.automl.v1beta1.GcsDestination; -import com.google.cloud.automl.v1beta1.GcsSource; -import com.google.cloud.automl.v1beta1.ImageClassificationDatasetMetadata; -import com.google.cloud.automl.v1beta1.InputConfig; -import com.google.cloud.automl.v1beta1.ListDatasetsRequest; -import com.google.cloud.automl.v1beta1.LocationName; -import com.google.cloud.automl.v1beta1.OutputConfig; -import com.google.protobuf.Empty; -import java.io.IOException; -import java.util.concurrent.ExecutionException; -import net.sourceforge.argparse4j.ArgumentParsers; -import net.sourceforge.argparse4j.inf.ArgumentParser; -import net.sourceforge.argparse4j.inf.ArgumentParserException; -import net.sourceforge.argparse4j.inf.Namespace; -import net.sourceforge.argparse4j.inf.Subparser; -import net.sourceforge.argparse4j.inf.Subparsers; - -/** - * Google Cloud AutoML Vision API sample application. Example usage: mvn package exec:java - * -Dexec.mainClass ='com.google.cloud.vision.samples.automl.DatasetAPI' -Dexec.args='create_dataset - * test_dataset' - */ -public class DatasetApi { - - // [START automl_vision_create_dataset] - /** - * Demonstrates using the AutoML client to create a dataset - * - * @param projectId the Google Cloud Project ID. - * @param computeRegion the Region name. (e.g., "us-central1") - * @param datasetName the name of the dataset to be created. - * @param multiLabel the type of classification problem. Set to FALSE by default. False - - * MULTICLASS , True - MULTILABEL - */ - static void createDataset( - String projectId, String computeRegion, String datasetName, boolean multiLabel) { - - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - // A resource that represents Google Cloud Platform location. - LocationName projectLocation = LocationName.of(projectId, computeRegion); - - // Classification type assigned based on multiLabel value. - ClassificationType classificationType = - multiLabel ? ClassificationType.MULTILABEL : ClassificationType.MULTICLASS; - - // Specify the image classification type for the dataset. - ImageClassificationDatasetMetadata imageClassificationDatasetMetadata = - ImageClassificationDatasetMetadata.newBuilder() - .setClassificationType(classificationType) - .build(); - - // Set dataset with dataset name and set the dataset metadata. - Dataset myDataset = - Dataset.newBuilder() - .setDisplayName(datasetName) - .setImageClassificationDatasetMetadata(imageClassificationDatasetMetadata) - .build(); - - // Create dataset with the dataset metadata in the region. - Dataset dataset = client.createDataset(projectLocation, myDataset); - - // Display the dataset information - System.out.println(String.format("Dataset name: %s", dataset.getName())); - System.out.println( - String.format( - "Dataset id: %s", - dataset.getName().split("/")[dataset.getName().split("/").length - 1])); - System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName())); - System.out.println("Image classification dataset specification:"); - System.out.print(String.format("\t%s", dataset.getImageClassificationDatasetMetadata())); - System.out.println(String.format("Dataset example count: %d", dataset.getExampleCount())); - System.out.println("Dataset create time:"); - System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); - System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); - } catch (IOException e) { - e.printStackTrace(); - } - } - // [END automl_vision_create_dataset] - - // [START automl_vision_list_datasets] - /** - * Demonstrates using the AutoML client to list all datasets. - * - * @param projectId the Id of the project. - * @param computeRegion the Region name. - * @param filter the Filter expression. - */ - static void listDatasets(String projectId, String computeRegion, String filter) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // A resource that represents Google Cloud Platform location. - LocationName projectLocation = LocationName.of(projectId, computeRegion); - - // Build the List datasets request - ListDatasetsRequest request = - ListDatasetsRequest.newBuilder() - .setParent(projectLocation.toString()) - .setFilter(filter) - .build(); - - // List all the datasets available in the region by applying the filter. - System.out.print("List of datasets:"); - for (Dataset dataset : client.listDatasets(request).iterateAll()) { - // Display the dataset information - System.out.println(String.format("\nDataset name: %s", dataset.getName())); - System.out.println( - String.format( - "Dataset id: %s", - dataset.getName().split("/")[dataset.getName().split("/").length - 1])); - System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName())); - System.out.println("Image classification dataset specification:"); - System.out.print(String.format("\t%s", dataset.getImageClassificationDatasetMetadata())); - System.out.println(String.format("Dataset example count: %d", dataset.getExampleCount())); - System.out.println("Dataset create time:"); - System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); - System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - // [END automl_vision_list_datasets] - - // [START automl_vision_get_dataset] - /** - * Demonstrates using the AutoML client to get a dataset by ID. - * - * @param projectId the Id of the project. - * @param computeRegion the Region name. - * @param datasetId the Id of the dataset. - */ - static void getDataset(String projectId, String computeRegion, String datasetId) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // Get the complete path of the dataset. - DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId); - - // Get all the information about a given dataset. - Dataset dataset = client.getDataset(datasetFullId); - - // Display the dataset information. - System.out.println(String.format("Dataset name: %s", dataset.getName())); - System.out.println( - String.format( - "Dataset id: %s", - dataset.getName().split("/")[dataset.getName().split("/").length - 1])); - System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName())); - System.out.println("Image classification dataset specification:"); - System.out.print(String.format("\t%s", dataset.getImageClassificationDatasetMetadata())); - System.out.println(String.format("Dataset example count: %d", dataset.getExampleCount())); - System.out.println("Dataset create time:"); - System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); - System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); - } catch (IOException e) { - e.printStackTrace(); - } - } - // [END automl_vision_get_dataset] - - // [START automl_vision_import_data] - /** - * Demonstrates using the AutoML client to import labeled images. - * - * @param projectId the Id of the project. - * @param computeRegion the Region name. - * @param datasetId the Id of the dataset to which the training data will be imported. - * @param path the Google Cloud Storage URIs. Target files must be in AutoML vision CSV format. - */ - static void importData(String projectId, String computeRegion, String datasetId, String path) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // Get the complete path of the dataset. - DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId); - - GcsSource.Builder gcsSource = GcsSource.newBuilder(); - - // Get multiple training data files to be imported - String[] inputUris = path.split(","); - for (String inputUri : inputUris) { - gcsSource.addInputUris(inputUri); - } - - // Import data from the input URI - InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build(); - System.out.println("Processing import..."); - Empty response = client.importDataAsync(datasetFullId.toString(), inputConfig).get(); - System.out.println(String.format("Dataset imported. %s", response)); - } catch (IOException | InterruptedException | ExecutionException e) { - e.printStackTrace(); - } - } - // [END automl_vision_import_data] - - // [START automl_vision_export_data] - /** - * Demonstrates using the AutoML client to export a dataset to a Google Cloud Storage bucket. - * - * @param projectId the Id of the project. - * @param computeRegion the Region name. - * @param datasetId the Id of the dataset. - * @param gcsUri the Destination URI (Google Cloud Storage) - */ - static void exportData(String projectId, String computeRegion, String datasetId, String gcsUri) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // Get the complete path of the dataset. - DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId); - - // Set the output URI - GcsDestination gcsDestination = - GcsDestination.newBuilder().setOutputUriPrefix(gcsUri).build(); - - // Export the dataset to the output URI. - OutputConfig outputConfig = - OutputConfig.newBuilder().setGcsDestination(gcsDestination).build(); - System.out.println("Processing export..."); - - Empty response = client.exportDataAsync(datasetFullId, outputConfig).get(); - System.out.println(String.format("Dataset exported. %s", response)); - } catch (IOException | InterruptedException | ExecutionException e) { - e.printStackTrace(); - } - } - // [END automl_vision_export_data] - - // [START automl_vision_delete_dataset] - /** - * Delete a dataset. - * - * @param projectId the Id of the project. - * @param computeRegion the Region name. - * @param datasetId the Id of the dataset. - */ - static void deleteDataset(String projectId, String computeRegion, String datasetId) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // Get the complete path of the dataset. - DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId); - - // Delete a dataset. - Empty response = client.deleteDatasetAsync(datasetFullId).get(); - - System.out.println(String.format("Dataset deleted. %s", response)); - } catch (IOException | InterruptedException | ExecutionException e) { - e.printStackTrace(); - } - } - // [END automl_vision_delete_dataset] - - public static void main(String[] args) { - argsHelper(args); - } - - static void argsHelper(String[] args) { - ArgumentParser parser = - ArgumentParsers.newFor("DatasetApi") - .build() - .defaultHelp(true) - .description("Dataset API operations."); - Subparsers subparsers = parser.addSubparsers().dest("command"); - - Subparser createDatasetParser = subparsers.addParser("create_dataset"); - createDatasetParser.addArgument("datasetName"); - createDatasetParser - .addArgument("multiLabel") - .nargs("?") - .type(Boolean.class) - .choices(Boolean.FALSE, Boolean.TRUE) - .setDefault(Boolean.FALSE); - - Subparser listDatasetsParser = subparsers.addParser("list_datasets"); - listDatasetsParser - .addArgument("filter") - .nargs("?") - .setDefault("imageClassificationDatasetMetadata:*"); - - Subparser getDatasetParser = subparsers.addParser("get_dataset"); - getDatasetParser.addArgument("datasetId"); - - Subparser importDataParser = subparsers.addParser("import_data"); - importDataParser.addArgument("datasetId"); - importDataParser.addArgument("path"); - - Subparser exportDataParser = subparsers.addParser("export_data"); - exportDataParser.addArgument("datasetId"); - exportDataParser.addArgument("gcsUri"); - - Subparser deleteDatasetParser = subparsers.addParser("delete_dataset"); - deleteDatasetParser.addArgument("datasetId"); - - String projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); - String computeRegion = System.getenv("REGION_NAME"); - - if (projectId == null || computeRegion == null) { - System.out.println("Set `GOOGLE_CLOUD_PROJECT` and `REGION_NAME` as specified in the README"); - System.exit(-1); - } - - try { - Namespace ns = parser.parseArgs(args); - - if (ns.get("command").equals("create_dataset")) { - createDataset( - projectId, computeRegion, ns.getString("datasetName"), ns.getBoolean("multiLabel")); - } - if (ns.get("command").equals("list_datasets")) { - listDatasets(projectId, computeRegion, ns.getString("filter")); - } - if (ns.get("command").equals("get_dataset")) { - getDataset(projectId, computeRegion, ns.getString("datasetId")); - } - if (ns.get("command").equals("import_data")) { - importData(projectId, computeRegion, ns.getString("datasetId"), ns.getString("path")); - } - if (ns.get("command").equals("export_data")) { - exportData(projectId, computeRegion, ns.getString("datasetId"), ns.getString("gcsUri")); - } - if (ns.get("command").equals("delete_dataset")) { - deleteDataset(projectId, computeRegion, ns.getString("datasetId")); - } - - } catch (ArgumentParserException e) { - parser.handleError(e); - } - } -} diff --git a/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ModelApi.java b/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ModelApi.java index 9a89c215d40..4b07977df3e 100644 --- a/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ModelApi.java +++ b/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ModelApi.java @@ -101,287 +101,6 @@ static void createModel( } // [END automl_vision_create_model] - // [START automl_vision_get_operation_status] - /** - * Demonstrates using the AutoML client to get operation status. - * - * @param operationFullId the complete name of a operation. For example, the name of your - * operation is projects/[projectId]/locations/us-central1/operations/[operationId]. - */ - static void getOperationStatus(String operationFullId) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // Get the latest state of a long-running operation. - Operation response = client.getOperationsClient().getOperation(operationFullId); - - System.out.println(String.format("Operation status: %s", response)); - } catch (IOException e) { - e.printStackTrace(); - } - } - // [END automl_vision_get_operation_status] - - // [START automl_vision_list_models] - /** - * Demonstrates using the AutoML client to list all models. - * - * @param projectId the Id of the project. - * @param computeRegion the Region name. - * @param filter - Filter expression. - */ - static void listModels(String projectId, String computeRegion, String filter) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // A resource that represents Google Cloud Platform location. - LocationName projectLocation = LocationName.of(projectId, computeRegion); - - // Create list models request - ListModelsRequest listModelsRequest = - ListModelsRequest.newBuilder() - .setParent(projectLocation.toString()) - .setFilter(filter) - .build(); - - System.out.println("List of models:"); - for (Model model : client.listModels(listModelsRequest).iterateAll()) { - // Display the model information. - System.out.println(String.format("Model name: %s", model.getName())); - System.out.println( - String.format( - "Model id: %s", model.getName().split("/")[model.getName().split("/").length - 1])); - System.out.println(String.format("Model display name: %s", model.getDisplayName())); - System.out.println("Image classification model metadata:"); - System.out.println( - "Tranning budget: " + model.getImageClassificationModelMetadata().getTrainBudget()); - System.out.println( - "Tranning cost: " + model.getImageClassificationModelMetadata().getTrainCost()); - System.out.println( - String.format( - "Stop reason: %s", model.getImageClassificationModelMetadata().getStopReason())); - System.out.println( - String.format( - "Base model id: %s", model.getImageClassificationModelMetadata().getBaseModelId())); - System.out.println("Model create time:"); - System.out.println(String.format("\tseconds: %s", model.getCreateTime().getSeconds())); - System.out.println(String.format("\tnanos: %s", model.getCreateTime().getNanos())); - System.out.println(String.format("Model deployment state: %s", model.getDeploymentState())); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - // [END automl_vision_list_models] - - // [START automl_vision_get_model] - /** - * Demonstrates using the AutoML client to get model details. - * - * @param projectId the Id of the project. - * @param computeRegion the Region name. - * @param modelId the Id of the model. - */ - static void getModel(String projectId, String computeRegion, String modelId) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // Get the full path of the model. - ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId); - - // Get complete detail of the model. - Model model = client.getModel(modelFullId); - - // Display the model information. - System.out.println(String.format("Model name: %s", model.getName())); - System.out.println( - String.format( - "Model id: %s", model.getName().split("/")[model.getName().split("/").length - 1])); - System.out.println(String.format("Model display name: %s", model.getDisplayName())); - System.out.println("Image classification model metadata:"); - System.out.println( - "Tranning budget: " + model.getImageClassificationModelMetadata().getTrainBudget()); - System.out.println( - "Tranning cost:" + model.getImageClassificationModelMetadata().getTrainCost()); - System.out.println( - String.format( - "Stop reason: %s", model.getImageClassificationModelMetadata().getStopReason())); - System.out.println( - String.format( - "Base model id: %s", model.getImageClassificationModelMetadata().getBaseModelId())); - System.out.println("Model create time:"); - System.out.println(String.format("\tseconds: %s", model.getCreateTime().getSeconds())); - System.out.println(String.format("\tnanos: %s", model.getCreateTime().getNanos())); - System.out.println(String.format("Model deployment state: %s", model.getDeploymentState())); - } catch (IOException e) { - e.printStackTrace(); - } - } - // [END automl_vision_get_model] - - // [START automl_vision_list_model_evaluations] - /** - * Demonstrates using the AutoML client to list model evaluations. - * - * @param projectId the Id of the project. - * @param computeRegion the Region name. - * @param modelId the Id of the model. - * @param filter the Filter expression. - */ - static void listModelEvaluations( - String projectId, String computeRegion, String modelId, String filter) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // Get the full path of the model. - ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId); - - // Create list model evaluations request - ListModelEvaluationsRequest modelEvaluationsrequest = - ListModelEvaluationsRequest.newBuilder() - .setParent(modelFullId.toString()) - .setFilter(filter) - .build(); - - System.out.println("List of model evaluations:"); - // List all the model evaluations in the model by applying filter. - for (ModelEvaluation element : - client.listModelEvaluations(modelEvaluationsrequest).iterateAll()) { - System.out.println(element); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - // [END automl_vision_list_model_evaluations] - - // [START automl_vision_get_model_evaluation] - /** - * Demonstrates using the AutoML client to get model evaluations. - * - * @param projectId the Id of the project. - * @param computeRegion the Region name. - * @param modelId the Id of the model. - * @param modelEvaluationId the Id of your model evaluation. - */ - static void getModelEvaluation( - String projectId, String computeRegion, String modelId, String modelEvaluationId) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // Get the full path of the model evaluation. - ModelEvaluationName modelEvaluationFullId = - ModelEvaluationName.of(projectId, computeRegion, modelId, modelEvaluationId); - // Perform the AutoML Model request to get Model Evaluation information - ModelEvaluation response = client.getModelEvaluation(modelEvaluationFullId); - - System.out.println(response); - } catch (IOException e) { - e.printStackTrace(); - } - } - // [END automl_vision_get_model_evaluation] - - // [START automl_vision_display_evaluation] - /** - * Demonstrates using the AutoML client to display model evaluation. - * - * @param projectId the Id of the project. - * @param computeRegion the Region name. - * @param modelId the Id of the model. - * @param filter the filter expression. - */ - static void displayEvaluation( - String projectId, String computeRegion, String modelId, String filter) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // Get the full path of the model. - ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId); - - // List all the model evaluations in the model by applying filter. - ListModelEvaluationsRequest modelEvaluationsrequest = - ListModelEvaluationsRequest.newBuilder() - .setParent(modelFullId.toString()) - .setFilter(filter) - .build(); - - // Iterate through the results. - String modelEvaluationId = ""; - for (ModelEvaluation element : - client.listModelEvaluations(modelEvaluationsrequest).iterateAll()) { - if (element.getAnnotationSpecId() != null) { - modelEvaluationId = element.getName().split("/")[element.getName().split("/").length - 1]; - } - } - - // Resource name for the model evaluation. - ModelEvaluationName modelEvaluationFullId = - ModelEvaluationName.of(projectId, computeRegion, modelId, modelEvaluationId); - - // Get a model evaluation. - ModelEvaluation modelEvaluation = client.getModelEvaluation(modelEvaluationFullId); - - ClassificationEvaluationMetrics classMetrics = - modelEvaluation.getClassificationEvaluationMetrics(); - List confidenceMetricsEntries = - classMetrics.getConfidenceMetricsEntryList(); - - // Showing model score based on threshold of 0.5 - for (ConfidenceMetricsEntry confidenceMetricsEntry : confidenceMetricsEntries) { - if (confidenceMetricsEntry.getConfidenceThreshold() == 0.5) { - System.out.println("Precision and recall are based on a score threshold of 0.5"); - System.out.println( - String.format("Model Precision: %.2f ", confidenceMetricsEntry.getPrecision() * 100) - + '%'); - System.out.println( - String.format("Model Recall: %.2f ", confidenceMetricsEntry.getRecall() * 100) + '%'); - System.out.println( - String.format("Model F1 score: %.2f ", confidenceMetricsEntry.getF1Score() * 100) - + '%'); - System.out.println( - String.format( - "Model Precision@1: %.2f ", confidenceMetricsEntry.getPrecisionAt1() * 100) - + '%'); - System.out.println( - String.format("Model Recall@1: %.2f ", confidenceMetricsEntry.getRecallAt1() * 100) - + '%'); - System.out.println( - String.format("Model F1 score@1: %.2f ", confidenceMetricsEntry.getF1ScoreAt1() * 100) - + '%'); - } - } - } catch (IOException e) { - e.printStackTrace(); - } - } - // [END automl_vision_display_evaluation] - - // [START automl_vision_delete_model] - /** - * Demonstrates using the AutoML client to delete a model. - * - * @param projectId the Id of the project. - * @param computeRegion the Region name. - * @param modelId the Id of the model. - */ - static void deleteModel(String projectId, String computeRegion, String modelId) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // Get the full path of the model. - ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId); - - // Delete a model. - Empty response = client.deleteModelAsync(modelFullId).get(); - - System.out.println("Model deletion started..."); - } catch (IOException | InterruptedException | ExecutionException e) { - e.printStackTrace(); - } - } - // [END automl_vision_delete_model] - public static void main(String[] args) { argsHelper(args); } @@ -399,33 +118,6 @@ static void argsHelper(String[] args) { createModelParser.addArgument("modelName"); createModelParser.addArgument("trainBudget"); - Subparser listModelParser = subparsers.addParser("list_models"); - listModelParser - .addArgument("filter") - .nargs("?") - .setDefault("imageClassificationModelMetadata:*"); - - Subparser getModelParser = subparsers.addParser("get_model"); - getModelParser.addArgument("modelId"); - - Subparser listModelEvaluationsParser = subparsers.addParser("list_model_evaluations"); - listModelEvaluationsParser.addArgument("modelId"); - listModelEvaluationsParser.addArgument("filter").nargs("?").setDefault(""); - - Subparser getModelEvaluationParser = subparsers.addParser("get_model_evaluation"); - getModelEvaluationParser.addArgument("modelId"); - getModelEvaluationParser.addArgument("modelEvaluationId"); - - Subparser displayEvaluationParser = subparsers.addParser("display_evaluation"); - displayEvaluationParser.addArgument("modelId"); - displayEvaluationParser.addArgument("filter").nargs("?").setDefault(""); - - Subparser deleteModelParser = subparsers.addParser("delete_model"); - deleteModelParser.addArgument("modelId"); - - Subparser getOperationStatusParser = subparsers.addParser("get_operation_status"); - getOperationStatusParser.addArgument("operationFullId"); - String projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); String computeRegion = System.getenv("REGION_NAME"); @@ -444,30 +136,6 @@ static void argsHelper(String[] args) { ns.getString("modelName"), ns.getString("trainBudget")); } - if (ns.get("command").equals("list_models")) { - listModels(projectId, computeRegion, ns.getString("filter")); - } - if (ns.get("command").equals("get_model")) { - getModel(projectId, computeRegion, ns.getString("modelId")); - } - if (ns.get("command").equals("list_model_evaluations")) { - listModelEvaluations( - projectId, computeRegion, ns.getString("modelId"), ns.getString("filter")); - } - if (ns.get("command").equals("get_model_evaluation")) { - getModelEvaluation( - projectId, computeRegion, ns.getString("modelId"), ns.getString("modelEvaluationId")); - } - if (ns.get("command").equals("display_evaluation")) { - displayEvaluation( - projectId, computeRegion, ns.getString("modelId"), ns.getString("filter")); - } - if (ns.get("command").equals("delete_model")) { - deleteModel(projectId, computeRegion, ns.getString("modelId")); - } - if (ns.get("command").equals("get_operation_status")) { - getOperationStatus(ns.getString("operationFullId")); - } } catch (ArgumentParserException e) { parser.handleError(e); } diff --git a/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/DatasetApiIT.java b/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/DatasetApiIT.java deleted file mode 100644 index 04cc0a6f4d2..00000000000 --- a/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/DatasetApiIT.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright 2018 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.cloud.vision.samples.automl; - -import static com.google.common.truth.Truth.assertThat; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.UUID; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Tests for Automl vision "Dataset API" sample. */ -@RunWith(JUnit4.class) -@SuppressWarnings("checkstyle:abbreviationaswordinname") -public class DatasetApiIT { - - private static final String PROJECT_ID = "java-docs-samples-testing"; - private static final String BUCKET = PROJECT_ID + "-vcm"; - private static final String COMPUTE_REGION = "us-central1"; - private ByteArrayOutputStream bout; - private PrintStream out; - private String datasetId; - - @Before - public void setUp() { - bout = new ByteArrayOutputStream(); - out = new PrintStream(bout); - System.setOut(out); - } - - @After - public void tearDown() { - System.setOut(null); - } - - @Test - public void testCreateImportDeleteDataset() { - // Create a random dataset name with a length of 32 characters (max allowed by AutoML) - // To prevent name collisions when running tests in multiple java versions at once. - // AutoML doesn't allow "-", but accepts "_" - String datasetName = - String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); - // Act - DatasetApi.createDataset(PROJECT_ID, COMPUTE_REGION, datasetName, false); - - // Assert - String got = bout.toString(); - datasetId = - bout.toString() - .split("\n")[0] - .split("/")[(bout.toString().split("\n")[0]).split("/").length - 1]; - assertThat(got).contains("Dataset id:"); - - // Act - DatasetApi.importData( - PROJECT_ID, COMPUTE_REGION, datasetId, "gs://" + BUCKET + "/flower_traindata.csv"); - - // Assert - got = bout.toString(); - assertThat(got).contains("Dataset id:"); - - // Act - DatasetApi.deleteDataset(PROJECT_ID, COMPUTE_REGION, datasetId); - - // Assert - got = bout.toString(); - assertThat(got).contains("Dataset deleted."); - } - - @Test - public void testListGetDatasets() { - // Act - DatasetApi.listDatasets(PROJECT_ID, COMPUTE_REGION, "imageClassificationDatasetMetadata:*"); - - // Assert - String got = bout.toString(); - datasetId = - bout.toString() - .split("\n")[1] - .split("/")[(bout.toString().split("\n")[1]).split("/").length - 1]; - assertThat(got).contains("Dataset id:"); - - // Act - DatasetApi.getDataset(PROJECT_ID, COMPUTE_REGION, datasetId); - - // Assert - got = bout.toString(); - - assertThat(got).contains("Dataset id:"); - } -} diff --git a/vision/beta/cloud-client/pom.xml b/vision/beta/cloud-client/pom.xml index 1d5971261c2..4386f7996be 100644 --- a/vision/beta/cloud-client/pom.xml +++ b/vision/beta/cloud-client/pom.xml @@ -26,7 +26,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -43,7 +43,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -91,7 +91,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 diff --git a/vision/cloud-client/pom.xml b/vision/cloud-client/pom.xml index 89ea28891da..cc25170b22c 100644 --- a/vision/cloud-client/pom.xml +++ b/vision/cloud-client/pom.xml @@ -26,7 +26,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -43,7 +43,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -91,7 +91,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 @@ -120,7 +120,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 @@ -129,7 +129,7 @@ - com.example.vision.QuickstartSample + com.example.vision.quickstart.QuickstartSample false diff --git a/vision/cloud-client/src/main/java/com/example/vision/Detect.java b/vision/cloud-client/src/main/java/com/example/vision/Detect.java index ae84a53ed70..5beb7d143a2 100644 --- a/vision/cloud-client/src/main/java/com/example/vision/Detect.java +++ b/vision/cloud-client/src/main/java/com/example/vision/Detect.java @@ -31,1222 +31,42 @@ import com.google.cloud.vision.v1.AsyncBatchAnnotateFilesResponse; import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; import com.google.cloud.vision.v1.Block; -import com.google.cloud.vision.v1.ColorInfo; -import com.google.cloud.vision.v1.CropHint; -import com.google.cloud.vision.v1.CropHintsAnnotation; -import com.google.cloud.vision.v1.DominantColorsAnnotation; -import com.google.cloud.vision.v1.EntityAnnotation; -import com.google.cloud.vision.v1.FaceAnnotation; import com.google.cloud.vision.v1.Feature; import com.google.cloud.vision.v1.Feature.Type; import com.google.cloud.vision.v1.GcsDestination; import com.google.cloud.vision.v1.GcsSource; import com.google.cloud.vision.v1.Image; import com.google.cloud.vision.v1.ImageAnnotatorClient; -import com.google.cloud.vision.v1.ImageContext; import com.google.cloud.vision.v1.ImageSource; import com.google.cloud.vision.v1.InputConfig; import com.google.cloud.vision.v1.LocalizedObjectAnnotation; -import com.google.cloud.vision.v1.LocationInfo; import com.google.cloud.vision.v1.OperationMetadata; import com.google.cloud.vision.v1.OutputConfig; import com.google.cloud.vision.v1.Page; import com.google.cloud.vision.v1.Paragraph; -import com.google.cloud.vision.v1.SafeSearchAnnotation; import com.google.cloud.vision.v1.Symbol; import com.google.cloud.vision.v1.TextAnnotation; -import com.google.cloud.vision.v1.WebDetection; -import com.google.cloud.vision.v1.WebDetection.WebEntity; -import com.google.cloud.vision.v1.WebDetection.WebImage; -import com.google.cloud.vision.v1.WebDetection.WebLabel; -import com.google.cloud.vision.v1.WebDetection.WebPage; -import com.google.cloud.vision.v1.WebDetectionParams; import com.google.cloud.vision.v1.Word; import com.google.protobuf.ByteString; import com.google.protobuf.util.JsonFormat; import java.io.FileInputStream; import java.io.IOException; -import java.io.PrintStream; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Detect { - - /** - * Detects entities, sentiment, and syntax in a document using the Vision API. - * - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - public static void main(String[] args) throws Exception, IOException { - argsHelper(args, System.out); - } - - /** - * Helper that handles the input passed to the program. - * - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - public static void argsHelper(String[] args, PrintStream out) throws Exception, IOException { - if (args.length < 1) { - out.println("Usage:"); - out.printf( - "\tmvn exec:java -DDetect -Dexec.args=\" \"\n" - + "\tmvn exec:java -DDetect -Dexec.args=\"ocr \"" - + "\n" - + "Commands:\n" - + "\tfaces | labels | landmarks | logos | text | safe-search | properties" - + "| web | web-entities | web-entities-include-geo | crop | ocr \n" - + "| object-localization \n" - + "Path:\n\tA file path (ex: ./resources/wakeupcat.jpg) or a URI for a Cloud Storage " - + "resource (gs://...)\n" - + "Path to File:\n\tA path to the remote file on Cloud Storage (gs://...)\n" - + "Path to Destination\n\tA path to the remote destination on Cloud Storage for the" - + " file to be saved. (gs://BUCKET_NAME/PREFIX/)\n"); - return; - } - String command = args[0]; - String path = args.length > 1 ? args[1] : ""; - - if (command.equals("faces")) { - if (path.startsWith("gs://")) { - detectFacesGcs(path, out); - } else { - detectFaces(path, out); - } - } else if (command.equals("labels")) { - if (path.startsWith("gs://")) { - detectLabelsGcs(path, out); - } else { - detectLabels(path, out); - } - } else if (command.equals("landmarks")) { - if (path.startsWith("http")) { - detectLandmarksUrl(path, out); - } else if (path.startsWith("gs://")) { - detectLandmarksGcs(path, out); - } else { - detectLandmarks(path, out); - } - } else if (command.equals("logos")) { - if (path.startsWith("gs://")) { - detectLogosGcs(path, out); - } else { - detectLogos(path, out); - } - } else if (command.equals("text")) { - if (path.startsWith("gs://")) { - detectTextGcs(path, out); - } else { - detectText(path, out); - } - } else if (command.equals("properties")) { - if (path.startsWith("gs://")) { - detectPropertiesGcs(path, out); - } else { - detectProperties(path, out); - } - } else if (command.equals("safe-search")) { - if (path.startsWith("gs://")) { - detectSafeSearchGcs(path, out); - } else { - detectSafeSearch(path, out); - } - } else if (command.equals("web")) { - if (path.startsWith("gs://")) { - detectWebDetectionsGcs(path, out); - } else { - detectWebDetections(path, out); - } - } else if (command.equals("web-entities")) { - if (path.startsWith("gs://")) { - detectWebEntitiesGcs(path, out); - } else { - detectWebEntities(path, out); - } - } else if (command.equals("web-entities-include-geo")) { - if (path.startsWith("gs://")) { - detectWebEntitiesIncludeGeoResultsGcs(path, out); - } else { - detectWebEntitiesIncludeGeoResults(path, out); - } - } else if (command.equals("crop")) { - if (path.startsWith("gs://")) { - detectCropHintsGcs(path, out); - } else { - detectCropHints(path, out); - } - } else if (command.equals("fulltext")) { - if (path.startsWith("gs://")) { - detectDocumentTextGcs(path, out); - } else { - detectDocumentText(path, out); - } - } else if (command.equals("ocr")) { - String destPath = args.length > 2 ? args[2] : ""; - detectDocumentsGcs(path, destPath); - } else if (command.equals("object-localization")) { - if (path.startsWith("gs://")) { - detectLocalizedObjectsGcs(path, out); - } else { - detectLocalizedObjects(path, out); - } - } - } - - /** - * Detects faces in the specified local image. - * - * @param filePath The path to the file to perform face detection on. - * @param out A {@link PrintStream} to write detected features to. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_face_detection] - public static void detectFaces(String filePath, PrintStream out) throws Exception, IOException { - List requests = new ArrayList<>(); - - ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); - - Image img = Image.newBuilder().setContent(imgBytes).build(); - Feature feat = Feature.newBuilder().setType(Type.FACE_DETECTION).build(); - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); - requests.add(request); - - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); - List responses = response.getResponsesList(); - - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } - - // For full list of available annotations, see http://g.co/cloud/vision/docs - for (FaceAnnotation annotation : res.getFaceAnnotationsList()) { - out.printf( - "anger: %s\njoy: %s\nsurprise: %s\nposition: %s", - annotation.getAngerLikelihood(), - annotation.getJoyLikelihood(), - annotation.getSurpriseLikelihood(), - annotation.getBoundingPoly()); - } - } - } - } - // [END vision_face_detection] - - /** - * Detects faces in the specified remote image on Google Cloud Storage. - * - * @param gcsPath The path to the remote file on Google Cloud Storage to perform face detection - * on. - * @param out A {@link PrintStream} to write detected features to. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_face_detection_gcs] - public static void detectFacesGcs(String gcsPath, PrintStream out) throws Exception, IOException { - List requests = new ArrayList<>(); - - ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); - Image img = Image.newBuilder().setSource(imgSource).build(); - Feature feat = Feature.newBuilder().setType(Type.FACE_DETECTION).build(); - - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); - requests.add(request); - - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); - List responses = response.getResponsesList(); - - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } - - // For full list of available annotations, see http://g.co/cloud/vision/docs - for (FaceAnnotation annotation : res.getFaceAnnotationsList()) { - out.printf( - "anger: %s\njoy: %s\nsurprise: %s\nposition: %s", - annotation.getAngerLikelihood(), - annotation.getJoyLikelihood(), - annotation.getSurpriseLikelihood(), - annotation.getBoundingPoly()); - } - } - } - } - // [END vision_face_detection_gcs] - - /** - * Detects labels in the specified local image. - * - * @param filePath The path to the file to perform label detection on. - * @param out A {@link PrintStream} to write detected labels to. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_label_detection] - public static void detectLabels(String filePath, PrintStream out) throws Exception, IOException { - List requests = new ArrayList<>(); - - ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); - - Image img = Image.newBuilder().setContent(imgBytes).build(); - Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build(); - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); - requests.add(request); - - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); - List responses = response.getResponsesList(); - - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } - - // For full list of available annotations, see http://g.co/cloud/vision/docs - for (EntityAnnotation annotation : res.getLabelAnnotationsList()) { - annotation.getAllFields().forEach((k, v) -> out.printf("%s : %s\n", k, v.toString())); - } - } - } - } - // [END vision_label_detection] - - /** - * Detects labels in the specified remote image on Google Cloud Storage. - * - * @param gcsPath The path to the remote file on Google Cloud Storage to perform label detection - * on. - * @param out A {@link PrintStream} to write detected features to. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_label_detection_gcs] - public static void detectLabelsGcs(String gcsPath, PrintStream out) - throws Exception, IOException { - List requests = new ArrayList<>(); - - ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); - Image img = Image.newBuilder().setSource(imgSource).build(); - Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build(); - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); - requests.add(request); - - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); - List responses = response.getResponsesList(); - - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } - - // For full list of available annotations, see http://g.co/cloud/vision/docs - for (EntityAnnotation annotation : res.getLabelAnnotationsList()) { - annotation.getAllFields().forEach((k, v) -> out.printf("%s : %s\n", k, v.toString())); - } - } - } - } - // [END vision_label_detection_gcs] - - /** - * Detects landmarks in the specified local image. - * - * @param filePath The path to the file to perform landmark detection on. - * @param out A {@link PrintStream} to write detected landmarks to. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_landmark_detection] - public static void detectLandmarks(String filePath, PrintStream out) - throws Exception, IOException { - List requests = new ArrayList<>(); - ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); - - Image img = Image.newBuilder().setContent(imgBytes).build(); - Feature feat = Feature.newBuilder().setType(Type.LANDMARK_DETECTION).build(); - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); - requests.add(request); - - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); - List responses = response.getResponsesList(); - - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } - - // For full list of available annotations, see http://g.co/cloud/vision/docs - for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) { - LocationInfo info = annotation.getLocationsList().listIterator().next(); - out.printf("Landmark: %s\n %s\n", annotation.getDescription(), info.getLatLng()); - } - } - } - } - // [END vision_landmark_detection] - - /** - * Detects landmarks in the specified URI. - * - * @param uri The path to the file to perform landmark detection on. - * @param out A {@link PrintStream} to write detected landmarks to. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - public static void detectLandmarksUrl(String uri, PrintStream out) throws Exception, IOException { - List requests = new ArrayList<>(); - - ImageSource imgSource = ImageSource.newBuilder().setImageUri(uri).build(); - Image img = Image.newBuilder().setSource(imgSource).build(); - Feature feat = Feature.newBuilder().setType(Type.LANDMARK_DETECTION).build(); - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); - requests.add(request); - - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); - List responses = response.getResponsesList(); - - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } - - // For full list of available annotations, see http://g.co/cloud/vision/docs - for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) { - LocationInfo info = annotation.getLocationsList().listIterator().next(); - out.printf("Landmark: %s\n %s\n", annotation.getDescription(), info.getLatLng()); - } - } - } - } - - /** - * Detects landmarks in the specified remote image on Google Cloud Storage. - * - * @param gcsPath The path to the remote file on Google Cloud Storage to perform landmark - * detection on. - * @param out A {@link PrintStream} to write detected landmarks to. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_landmark_detection_gcs] - public static void detectLandmarksGcs(String gcsPath, PrintStream out) - throws Exception, IOException { - List requests = new ArrayList<>(); - - ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); - Image img = Image.newBuilder().setSource(imgSource).build(); - Feature feat = Feature.newBuilder().setType(Type.LANDMARK_DETECTION).build(); - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); - requests.add(request); - - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); - List responses = response.getResponsesList(); - - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } - - // For full list of available annotations, see http://g.co/cloud/vision/docs - for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) { - LocationInfo info = annotation.getLocationsList().listIterator().next(); - out.printf("Landmark: %s\n %s\n", annotation.getDescription(), info.getLatLng()); - } - } - } - } - // [END vision_landmark_detection_gcs] - - /** - * Detects logos in the specified local image. - * - * @param filePath The path to the local file to perform logo detection on. - * @param out A {@link PrintStream} to write detected logos to. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_logo_detection] - public static void detectLogos(String filePath, PrintStream out) throws Exception, IOException { - List requests = new ArrayList<>(); - - ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); - - Image img = Image.newBuilder().setContent(imgBytes).build(); - Feature feat = Feature.newBuilder().setType(Type.LOGO_DETECTION).build(); - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); - requests.add(request); - - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); - List responses = response.getResponsesList(); - - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } - - // For full list of available annotations, see http://g.co/cloud/vision/docs - for (EntityAnnotation annotation : res.getLogoAnnotationsList()) { - out.println(annotation.getDescription()); - } - } - } - } - // [END vision_logo_detection] - - /** - * Detects logos in the specified remote image on Google Cloud Storage. - * - * @param gcsPath The path to the remote file on Google Cloud Storage to perform logo detection - * on. - * @param out A {@link PrintStream} to write detected logos to. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_logo_detection_gcs] - public static void detectLogosGcs(String gcsPath, PrintStream out) throws Exception, IOException { - List requests = new ArrayList<>(); - - ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); - Image img = Image.newBuilder().setSource(imgSource).build(); - Feature feat = Feature.newBuilder().setType(Type.LOGO_DETECTION).build(); - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); - requests.add(request); - - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); - List responses = response.getResponsesList(); - - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } - - // For full list of available annotations, see http://g.co/cloud/vision/docs - for (EntityAnnotation annotation : res.getLogoAnnotationsList()) { - out.println(annotation.getDescription()); - } - } - } - } - // [END vision_logo_detection_gcs] - - /** - * Detects text in the specified image. - * - * @param filePath The path to the file to detect text in. - * @param out A {@link PrintStream} to write the detected text to. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_text_detection] - public static void detectText(String filePath, PrintStream out) throws Exception, IOException { - List requests = new ArrayList<>(); - - ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); - - Image img = Image.newBuilder().setContent(imgBytes).build(); - Feature feat = Feature.newBuilder().setType(Type.TEXT_DETECTION).build(); - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); - requests.add(request); - - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); - List responses = response.getResponsesList(); - - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } - - // For full list of available annotations, see http://g.co/cloud/vision/docs - for (EntityAnnotation annotation : res.getTextAnnotationsList()) { - out.printf("Text: %s\n", annotation.getDescription()); - out.printf("Position : %s\n", annotation.getBoundingPoly()); - } - } - } - } - // [END vision_text_detection] - - /** - * Detects text in the specified remote image on Google Cloud Storage. - * - * @param gcsPath The path to the remote file on Google Cloud Storage to detect text in. - * @param out A {@link PrintStream} to write the detected text to. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_text_detection_gcs] - public static void detectTextGcs(String gcsPath, PrintStream out) throws Exception, IOException { - List requests = new ArrayList<>(); - - ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); - Image img = Image.newBuilder().setSource(imgSource).build(); - Feature feat = Feature.newBuilder().setType(Type.TEXT_DETECTION).build(); - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); - requests.add(request); - - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); - List responses = response.getResponsesList(); - - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } - - // For full list of available annotations, see http://g.co/cloud/vision/docs - for (EntityAnnotation annotation : res.getTextAnnotationsList()) { - out.printf("Text: %s\n", annotation.getDescription()); - out.printf("Position : %s\n", annotation.getBoundingPoly()); - } - } - } - } - // [END vision_text_detection_gcs] - - /** - * Detects image properties such as color frequency from the specified local image. - * - * @param filePath The path to the file to detect properties. - * @param out A {@link PrintStream} to write - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_image_property_detection] - public static void detectProperties(String filePath, PrintStream out) - throws Exception, IOException { - List requests = new ArrayList<>(); - - ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); - - Image img = Image.newBuilder().setContent(imgBytes).build(); - Feature feat = Feature.newBuilder().setType(Type.IMAGE_PROPERTIES).build(); - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); - requests.add(request); - - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); - List responses = response.getResponsesList(); - - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } - - // For full list of available annotations, see http://g.co/cloud/vision/docs - DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors(); - for (ColorInfo color : colors.getColorsList()) { - out.printf( - "fraction: %f\nr: %f, g: %f, b: %f\n", - color.getPixelFraction(), - color.getColor().getRed(), - color.getColor().getGreen(), - color.getColor().getBlue()); - } - } - } - } - // [END vision_image_property_detection] - - /** - * Detects image properties such as color frequency from the specified remote image on Google - * Cloud Storage. - * - * @param gcsPath The path to the remote file on Google Cloud Storage to detect properties on. - * @param out A {@link PrintStream} to write - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_image_property_detection_gcs] - public static void detectPropertiesGcs(String gcsPath, PrintStream out) - throws Exception, IOException { - List requests = new ArrayList<>(); - - ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); - Image img = Image.newBuilder().setSource(imgSource).build(); - Feature feat = Feature.newBuilder().setType(Type.IMAGE_PROPERTIES).build(); - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); - requests.add(request); - - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); - List responses = response.getResponsesList(); - - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } - - // For full list of available annotations, see http://g.co/cloud/vision/docs - DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors(); - for (ColorInfo color : colors.getColorsList()) { - out.printf( - "fraction: %f\nr: %f, g: %f, b: %f\n", - color.getPixelFraction(), - color.getColor().getRed(), - color.getColor().getGreen(), - color.getColor().getBlue()); - } - } - } - } - // [END vision_image_property_detection_gcs] - - /** - * Detects whether the specified image has features you would want to moderate. - * - * @param filePath The path to the local file used for safe search detection. - * @param out A {@link PrintStream} to write the results to. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_safe_search_detection] - public static void detectSafeSearch(String filePath, PrintStream out) - throws Exception, IOException { - List requests = new ArrayList<>(); - - ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); - - Image img = Image.newBuilder().setContent(imgBytes).build(); - Feature feat = Feature.newBuilder().setType(Type.SAFE_SEARCH_DETECTION).build(); - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); - requests.add(request); - - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); - List responses = response.getResponsesList(); - - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } - - // For full list of available annotations, see http://g.co/cloud/vision/docs - SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); - out.printf( - "adult: %s\nmedical: %s\nspoofed: %s\nviolence: %s\nracy: %s\n", - annotation.getAdult(), - annotation.getMedical(), - annotation.getSpoof(), - annotation.getViolence(), - annotation.getRacy()); - } - } - } - // [END vision_safe_search_detection] - - /** - * Detects whether the specified image on Google Cloud Storage has features you would want to - * moderate. - * - * @param gcsPath The path to the remote file on Google Cloud Storage to detect safe-search on. - * @param out A {@link PrintStream} to write the results to. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_safe_search_detection_gcs] - public static void detectSafeSearchGcs(String gcsPath, PrintStream out) - throws Exception, IOException { - List requests = new ArrayList<>(); - - ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); - Image img = Image.newBuilder().setSource(imgSource).build(); - Feature feat = Feature.newBuilder().setType(Type.SAFE_SEARCH_DETECTION).build(); - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); - requests.add(request); - - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); - List responses = response.getResponsesList(); - - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } - - // For full list of available annotations, see http://g.co/cloud/vision/docs - SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); - out.printf( - "adult: %s\nmedical: %s\nspoofed: %s\nviolence: %s\nracy: %s\n", - annotation.getAdult(), - annotation.getMedical(), - annotation.getSpoof(), - annotation.getViolence(), - annotation.getRacy()); - } - } - } - // [END vision_safe_search_detection_gcs] - - // [START vision_web_detection] - /** - * Finds references to the specified image on the web. - * - * @param filePath The path to the local file used for web annotation detection. - * @param out A {@link PrintStream} to write the results to. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - public static void detectWebDetections(String filePath, PrintStream out) - throws Exception, IOException { - List requests = new ArrayList<>(); - - ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); - - Image img = Image.newBuilder().setContent(imgBytes).build(); - Feature feat = Feature.newBuilder().setType(Type.WEB_DETECTION).build(); - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); - requests.add(request); - - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); - List responses = response.getResponsesList(); - - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } - - // Search the web for usages of the image. You could use these signals later - // for user input moderation or linking external references. - // For a full list of available annotations, see http://g.co/cloud/vision/docs - WebDetection annotation = res.getWebDetection(); - out.println("Entity:Id:Score"); - out.println("==============="); - for (WebEntity entity : annotation.getWebEntitiesList()) { - out.println( - entity.getDescription() + " : " + entity.getEntityId() + " : " + entity.getScore()); - } - for (WebLabel label : annotation.getBestGuessLabelsList()) { - out.format("\nBest guess label: %s", label.getLabel()); - } - out.println("\nPages with matching images: Score\n=="); - for (WebPage page : annotation.getPagesWithMatchingImagesList()) { - out.println(page.getUrl() + " : " + page.getScore()); - } - out.println("\nPages with partially matching images: Score\n=="); - for (WebImage image : annotation.getPartialMatchingImagesList()) { - out.println(image.getUrl() + " : " + image.getScore()); - } - out.println("\nPages with fully matching images: Score\n=="); - for (WebImage image : annotation.getFullMatchingImagesList()) { - out.println(image.getUrl() + " : " + image.getScore()); - } - out.println("\nPages with visually similar images: Score\n=="); - for (WebImage image : annotation.getVisuallySimilarImagesList()) { - out.println(image.getUrl() + " : " + image.getScore()); - } - } - } - } - // [END vision_web_detection] - - // [START vision_web_detection_gcs] - /** - * Detects whether the remote image on Google Cloud Storage has features you would want to - * moderate. - * - * @param gcsPath The path to the remote on Google Cloud Storage file to detect web annotations. - * @param out A {@link PrintStream} to write the results to. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - public static void detectWebDetectionsGcs(String gcsPath, PrintStream out) - throws Exception, IOException { - List requests = new ArrayList<>(); - - ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); - Image img = Image.newBuilder().setSource(imgSource).build(); - Feature feat = Feature.newBuilder().setType(Type.WEB_DETECTION).build(); - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); - requests.add(request); - - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); - List responses = response.getResponsesList(); - - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } - - // Search the web for usages of the image. You could use these signals later - // for user input moderation or linking external references. - // For a full list of available annotations, see http://g.co/cloud/vision/docs - WebDetection annotation = res.getWebDetection(); - out.println("Entity:Id:Score"); - out.println("==============="); - for (WebEntity entity : annotation.getWebEntitiesList()) { - out.println( - entity.getDescription() + " : " + entity.getEntityId() + " : " + entity.getScore()); - } - for (WebLabel label : annotation.getBestGuessLabelsList()) { - out.format("\nBest guess label: %s", label.getLabel()); - } - out.println("\nPages with matching images: Score\n=="); - for (WebPage page : annotation.getPagesWithMatchingImagesList()) { - out.println(page.getUrl() + " : " + page.getScore()); - } - out.println("\nPages with partially matching images: Score\n=="); - for (WebImage image : annotation.getPartialMatchingImagesList()) { - out.println(image.getUrl() + " : " + image.getScore()); - } - out.println("\nPages with fully matching images: Score\n=="); - for (WebImage image : annotation.getFullMatchingImagesList()) { - out.println(image.getUrl() + " : " + image.getScore()); - } - out.println("\nPages with visually similar images: Score\n=="); - for (WebImage image : annotation.getVisuallySimilarImagesList()) { - out.println(image.getUrl() + " : " + image.getScore()); - } - } - } - } - // [END vision_web_detection_gcs] - - /** - * Find web entities given a local image. - * - * @param filePath The path of the image to detect. - * @param out A {@link PrintStream} to write the results to. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - public static void detectWebEntities(String filePath, PrintStream out) - throws Exception, IOException { - // Instantiates a client - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - // Read in the local image - ByteString contents = ByteString.readFrom(new FileInputStream(filePath)); - - // Build the image - Image image = Image.newBuilder().setContent(contents).build(); - - // Create the request with the image and the specified feature: web detection - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder() - .addFeatures(Feature.newBuilder().setType(Type.WEB_DETECTION)) - .setImage(image) - .build(); - - // Perform the request - BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request)); - - // Display the results - response - .getResponsesList() - .stream() - .forEach( - r -> - r.getWebDetection() - .getWebEntitiesList() - .stream() - .forEach( - entity -> { - out.format("Description: %s\n", entity.getDescription()); - out.format("Score: %f\n", entity.getScore()); - })); - } - } - - /** - * Find web entities given the remote image on Google Cloud Storage. - * - * @param gcsPath The path to the remote file on Google Cloud Storage to detect web entities. - * @param out A {@link PrintStream} to write the results to. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - public static void detectWebEntitiesGcs(String gcsPath, PrintStream out) - throws Exception, IOException { - // Instantiates a client - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - // Set the image source to the given gs uri - ImageSource imageSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); - // Build the image - Image image = Image.newBuilder().setSource(imageSource).build(); - - // Create the request with the image and the specified feature: web detection - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder() - .addFeatures(Feature.newBuilder().setType(Type.WEB_DETECTION)) - .setImage(image) - .build(); - - // Perform the request - BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request)); - - // Display the results - response - .getResponsesList() - .stream() - .forEach( - r -> - r.getWebDetection() - .getWebEntitiesList() - .stream() - .forEach( - entity -> { - System.out.format("Description: %s\n", entity.getDescription()); - System.out.format("Score: %f\n", entity.getScore()); - })); - } - } - - // [START vision_web_detection_include_geo] - /** - * Find web entities given a local image. - * - * @param filePath The path of the image to detect. - * @param out A {@link PrintStream} to write the results to. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - public static void detectWebEntitiesIncludeGeoResults(String filePath, PrintStream out) - throws Exception, IOException { - // Instantiates a client - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - // Read in the local image - ByteString contents = ByteString.readFrom(new FileInputStream(filePath)); - - // Build the image - Image image = Image.newBuilder().setContent(contents).build(); - - // Enable `IncludeGeoResults` - WebDetectionParams webDetectionParams = - WebDetectionParams.newBuilder().setIncludeGeoResults(true).build(); - - // Set the parameters for the image - ImageContext imageContext = - ImageContext.newBuilder().setWebDetectionParams(webDetectionParams).build(); - - // Create the request with the image, imageContext, and the specified feature: web detection - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder() - .addFeatures(Feature.newBuilder().setType(Type.WEB_DETECTION)) - .setImage(image) - .setImageContext(imageContext) - .build(); - - // Perform the request - BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request)); - - // Display the results - response - .getResponsesList() - .stream() - .forEach( - r -> - r.getWebDetection() - .getWebEntitiesList() - .stream() - .forEach( - entity -> { - out.format("Description: %s\n", entity.getDescription()); - out.format("Score: %f\n", entity.getScore()); - })); - } - } - // [END vision_web_detection_include_geo] - - // [START vision_web_detection_include_geo_gcs] - /** - * Find web entities given the remote image on Google Cloud Storage. - * - * @param gcsPath The path to the remote file on Google Cloud Storage to detect web entities with - * geo results. - * @param out A {@link PrintStream} to write the results to. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - public static void detectWebEntitiesIncludeGeoResultsGcs(String gcsPath, PrintStream out) - throws Exception, IOException { - // Instantiates a client - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - // Set the image source to the given gs uri - ImageSource imageSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); - // Build the image - Image image = Image.newBuilder().setSource(imageSource).build(); - - // Enable `IncludeGeoResults` - WebDetectionParams webDetectionParams = - WebDetectionParams.newBuilder().setIncludeGeoResults(true).build(); - - // Set the parameters for the image - ImageContext imageContext = - ImageContext.newBuilder().setWebDetectionParams(webDetectionParams).build(); - - // Create the request with the image, imageContext, and the specified feature: web detection - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder() - .addFeatures(Feature.newBuilder().setType(Type.WEB_DETECTION)) - .setImage(image) - .setImageContext(imageContext) - .build(); - - // Perform the request - BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request)); - - // Display the results - response - .getResponsesList() - .stream() - .forEach( - r -> - r.getWebDetection() - .getWebEntitiesList() - .stream() - .forEach( - entity -> { - out.format("Description: %s\n", entity.getDescription()); - out.format("Score: %f\n", entity.getScore()); - })); - } - } - // [END vision_web_detection_include_geo_gcs] - - /** - * Suggests a region to crop to for a local file. - * - * @param filePath The path to the local file used for web annotation detection. - * @param out A {@link PrintStream} to write the results to. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_crop_hint_detection] - public static void detectCropHints(String filePath, PrintStream out) - throws Exception, IOException { - List requests = new ArrayList<>(); - - ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); - - Image img = Image.newBuilder().setContent(imgBytes).build(); - Feature feat = Feature.newBuilder().setType(Type.CROP_HINTS).build(); - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); - requests.add(request); - - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); - List responses = response.getResponsesList(); - - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } - - // For full list of available annotations, see http://g.co/cloud/vision/docs - CropHintsAnnotation annotation = res.getCropHintsAnnotation(); - for (CropHint hint : annotation.getCropHintsList()) { - out.println(hint.getBoundingPoly()); - } - } - } - } - // [END vision_crop_hint_detection] - - /** - * Suggests a region to crop to for a remote file on Google Cloud Storage. - * - * @param gcsPath The path to the remote file on Google Cloud Storage to detect safe-search on. - * @param out A {@link PrintStream} to write the results to. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_crop_hint_detection_gcs] - public static void detectCropHintsGcs(String gcsPath, PrintStream out) - throws Exception, IOException { - List requests = new ArrayList<>(); - - ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); - Image img = Image.newBuilder().setSource(imgSource).build(); - Feature feat = Feature.newBuilder().setType(Type.CROP_HINTS).build(); - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); - requests.add(request); - - try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { - BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); - List responses = response.getResponsesList(); - - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } - - // For full list of available annotations, see http://g.co/cloud/vision/docs - CropHintsAnnotation annotation = res.getCropHintsAnnotation(); - for (CropHint hint : annotation.getCropHintsList()) { - out.println(hint.getBoundingPoly()); - } - } - } - } - // [END vision_crop_hint_detection_gcs] - /** * Performs document text detection on a local image file. * * @param filePath The path to the local file to detect document text on. - * @param out A {@link PrintStream} to write the results to. * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ // [START vision_fulltext_detection] - public static void detectDocumentText(String filePath, PrintStream out) - throws Exception, IOException { + public static void detectDocumentText(String filePath) throws IOException { List requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); @@ -1257,6 +77,9 @@ public static void detectDocumentText(String filePath, PrintStream out) AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); List responses = response.getResponsesList(); @@ -1264,7 +87,7 @@ public static void detectDocumentText(String filePath, PrintStream out) for (AnnotateImageResponse res : responses) { if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); + System.out.format("Error: %s%n", res.getError().getMessage()); return; } @@ -1280,23 +103,24 @@ public static void detectDocumentText(String filePath, PrintStream out) String wordText = ""; for (Symbol symbol : word.getSymbolsList()) { wordText = wordText + symbol.getText(); - out.format( - "Symbol text: %s (confidence: %f)\n", + System.out.format( + "Symbol text: %s (confidence: %f)%n", symbol.getText(), symbol.getConfidence()); } - out.format("Word text: %s (confidence: %f)\n\n", wordText, word.getConfidence()); + System.out.format( + "Word text: %s (confidence: %f)%n%n", wordText, word.getConfidence()); paraText = String.format("%s %s", paraText, wordText); } // Output Example using Paragraph: - out.println("\nParagraph: \n" + paraText); - out.format("Paragraph Confidence: %f\n", para.getConfidence()); + System.out.println("%nParagraph: %n" + paraText); + System.out.format("Paragraph Confidence: %f%n", para.getConfidence()); blockText = blockText + paraText; } pageText = pageText + blockText; } } - out.println("\nComplete annotation:"); - out.println(annotation.getText()); + System.out.println("%nComplete annotation:"); + System.out.println(annotation.getText()); } } } @@ -1306,13 +130,11 @@ public static void detectDocumentText(String filePath, PrintStream out) * Performs document text detection on a remote image on Google Cloud Storage. * * @param gcsPath The path to the remote file on Google Cloud Storage to detect document text on. - * @param out A {@link PrintStream} to write the results to. * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ // [START vision_fulltext_detection_gcs] - public static void detectDocumentTextGcs(String gcsPath, PrintStream out) - throws Exception, IOException { + public static void detectDocumentTextGcs(String gcsPath) throws IOException { List requests = new ArrayList<>(); ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); @@ -1322,6 +144,9 @@ public static void detectDocumentTextGcs(String gcsPath, PrintStream out) AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); List responses = response.getResponsesList(); @@ -1329,7 +154,7 @@ public static void detectDocumentTextGcs(String gcsPath, PrintStream out) for (AnnotateImageResponse res : responses) { if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); + System.out.format("Error: %s%n", res.getError().getMessage()); return; } // For full list of available annotations, see http://g.co/cloud/vision/docs @@ -1344,23 +169,24 @@ public static void detectDocumentTextGcs(String gcsPath, PrintStream out) String wordText = ""; for (Symbol symbol : word.getSymbolsList()) { wordText = wordText + symbol.getText(); - out.format( - "Symbol text: %s (confidence: %f)\n", + System.out.format( + "Symbol text: %s (confidence: %f)%n", symbol.getText(), symbol.getConfidence()); } - out.format("Word text: %s (confidence: %f)\n\n", wordText, word.getConfidence()); + System.out.format( + "Word text: %s (confidence: %f)%n%n", wordText, word.getConfidence()); paraText = String.format("%s %s", paraText, wordText); } // Output Example using Paragraph: - out.println("\nParagraph: \n" + paraText); - out.format("Paragraph Confidence: %f\n", para.getConfidence()); + System.out.println("%nParagraph: %n" + paraText); + System.out.format("Paragraph Confidence: %f%n", para.getConfidence()); blockText = blockText + paraText; } pageText = pageText + blockText; } } - out.println("\nComplete annotation:"); - out.println(annotation.getText()); + System.out.println("%nComplete annotation:"); + System.out.println(annotation.getText()); } } } @@ -1378,6 +204,10 @@ public static void detectDocumentTextGcs(String gcsPath, PrintStream out) */ public static void detectDocumentsGcs(String gcsSourcePath, String gcsDestinationPath) throws Exception { + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { List requests = new ArrayList<>(); @@ -1397,8 +227,8 @@ public static void detectDocumentsGcs(String gcsSourcePath, String gcsDestinatio GcsDestination gcsDestination = GcsDestination.newBuilder().setUri(gcsDestinationPath).build(); - // Create the configuration for the output with the batch size. - // The batch size sets how many pages should be grouped into each json output file. + // Create the configuration for the System.output with the batch size. + // The batch size sets how many pages should be grouped into each json System.output file. OutputConfig outputConfig = OutputConfig.newBuilder().setBatchSize(2).setGcsDestination(gcsDestination).build(); @@ -1426,8 +256,8 @@ public static void detectDocumentsGcs(String gcsSourcePath, String gcsDestinatio List result = response.get(180, TimeUnit.SECONDS).getResponsesList(); - // Once the request has completed and the output has been - // written to GCS, we can list all the output files. + // Once the request has completed and the System.output has been + // written to GCS, we can list all the System.output files. Storage storage = StorageOptions.getDefaultInstance().getService(); // Get the destination location from the gcsDestinationPath @@ -1449,7 +279,7 @@ public static void detectDocumentsGcs(String gcsSourcePath, String gcsDestinatio for (Blob blob : pageList.iterateAll()) { System.out.println(blob.getName()); - // Process the first output file from GCS. + // Process the first System.output file from GCS. // Since we specified batch size = 2, the first response contains // the first two pages of the input file. if (firstOutputFile == null) { @@ -1475,7 +305,7 @@ public static void detectDocumentsGcs(String gcsSourcePath, String gcsDestinatio // The response contains more information: // annotation/pages/blocks/paragraphs/words/symbols // including confidence score and bounding boxes - System.out.format("\nText: %s\n", annotateImageResponse.getFullTextAnnotation().getText()); + System.out.format("%nText: %s%n", annotateImageResponse.getFullTextAnnotation().getText()); } else { System.out.println("No MATCH"); } @@ -1488,12 +318,10 @@ public static void detectDocumentsGcs(String gcsSourcePath, String gcsDestinatio * Detects localized objects in the specified local image. * * @param filePath The path to the file to perform localized object detection on. - * @param out A {@link PrintStream} to write detected objects to. * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectLocalizedObjects(String filePath, PrintStream out) - throws Exception, IOException { + public static void detectLocalizedObjects(String filePath) throws IOException { List requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); @@ -1506,21 +334,24 @@ public static void detectLocalizedObjects(String filePath, PrintStream out) .build(); requests.add(request); - // Perform the request + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + // Perform the request BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); List responses = response.getResponsesList(); // Display the results for (AnnotateImageResponse res : responses) { for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) { - out.format("Object name: %s\n", entity.getName()); - out.format("Confidence: %s\n", entity.getScore()); - out.format("Normalized Vertices:\n"); + System.out.format("Object name: %s%n", entity.getName()); + System.out.format("Confidence: %s%n", entity.getScore()); + System.out.format("Normalized Vertices:%n"); entity .getBoundingPoly() .getNormalizedVerticesList() - .forEach(vertex -> out.format("- (%s, %s)\n", vertex.getX(), vertex.getY())); + .forEach(vertex -> System.out.format("- (%s, %s)%n", vertex.getX(), vertex.getY())); } } } @@ -1533,12 +364,10 @@ public static void detectLocalizedObjects(String filePath, PrintStream out) * * @param gcsPath The path to the remote file on Google Cloud Storage to detect localized objects * on. - * @param out A {@link PrintStream} to write detected objects to. * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectLocalizedObjectsGcs(String gcsPath, PrintStream out) - throws Exception, IOException { + public static void detectLocalizedObjectsGcs(String gcsPath) throws IOException { List requests = new ArrayList<>(); ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); @@ -1551,21 +380,24 @@ public static void detectLocalizedObjectsGcs(String gcsPath, PrintStream out) .build(); requests.add(request); - // Perform the request + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + // Perform the request BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); List responses = response.getResponsesList(); client.close(); // Display the results for (AnnotateImageResponse res : responses) { for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) { - out.format("Object name: %s\n", entity.getName()); - out.format("Confidence: %s\n", entity.getScore()); - out.format("Normalized Vertices:\n"); + System.out.format("Object name: %s%n", entity.getName()); + System.out.format("Confidence: %s%n", entity.getScore()); + System.out.format("Normalized Vertices:%n"); entity .getBoundingPoly() .getNormalizedVerticesList() - .forEach(vertex -> out.format("- (%s, %s)\n", vertex.getX(), vertex.getY())); + .forEach(vertex -> System.out.format("- (%s, %s)%n", vertex.getX(), vertex.getY())); } } } diff --git a/vision/cloud-client/src/main/java/com/example/vision/QuickstartSample.java b/vision/cloud-client/src/main/java/com/example/vision/quickstart/QuickstartSample.java similarity index 85% rename from vision/cloud-client/src/main/java/com/example/vision/QuickstartSample.java rename to vision/cloud-client/src/main/java/com/example/vision/quickstart/QuickstartSample.java index b88233612d9..6e04f722e2f 100644 --- a/vision/cloud-client/src/main/java/com/example/vision/QuickstartSample.java +++ b/vision/cloud-client/src/main/java/com/example/vision/quickstart/QuickstartSample.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.example.vision; +package com.example.vision.quickstart; // [START vision_quickstart] // Imports the Google Cloud client library @@ -36,7 +36,9 @@ public class QuickstartSample { public static void main(String... args) throws Exception { - // Instantiates a client + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. try (ImageAnnotatorClient vision = ImageAnnotatorClient.create()) { // The path to the image file to annotate @@ -61,14 +63,14 @@ public static void main(String... args) throws Exception { for (AnnotateImageResponse res : responses) { if (res.hasError()) { - System.out.printf("Error: %s\n", res.getError().getMessage()); + System.out.format("Error: %s%n", res.getError().getMessage()); return; } for (EntityAnnotation annotation : res.getLabelAnnotationsList()) { annotation .getAllFields() - .forEach((k, v) -> System.out.printf("%s : %s\n", k, v.toString())); + .forEach((k, v) -> System.out.format("%s : %s%n", k, v.toString())); } } } diff --git a/vision/cloud-client/src/main/java/com/example/vision/AsyncBatchAnnotateImages.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/AsyncBatchAnnotateImages.java similarity index 97% rename from vision/cloud-client/src/main/java/com/example/vision/AsyncBatchAnnotateImages.java rename to vision/cloud-client/src/main/java/com/example/vision/snippets/AsyncBatchAnnotateImages.java index e9b31582051..025abc30594 100644 --- a/vision/cloud-client/src/main/java/com/example/vision/AsyncBatchAnnotateImages.java +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/AsyncBatchAnnotateImages.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.example.vision; +package com.example.vision.snippets; // [START vision_async_batch_annotate_images] import com.google.cloud.vision.v1.AnnotateImageRequest; @@ -82,7 +82,7 @@ public static void asyncBatchAnnotateImages(String inputImageUri, String outputU // The output is written to GCS with the provided output_uri as prefix String gcsOutputUri = response.getOutputConfig().getGcsDestination().getUri(); - System.out.printf("Output written to GCS with prefix: %s\n", gcsOutputUri); + System.out.format("Output written to GCS with prefix: %s%n", gcsOutputUri); } } } diff --git a/vision/cloud-client/src/main/java/com/example/vision/BatchAnnotateFiles.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/BatchAnnotateFiles.java similarity index 92% rename from vision/cloud-client/src/main/java/com/example/vision/BatchAnnotateFiles.java rename to vision/cloud-client/src/main/java/com/example/vision/snippets/BatchAnnotateFiles.java index 0895ca7aa00..15a864ff5f3 100644 --- a/vision/cloud-client/src/main/java/com/example/vision/BatchAnnotateFiles.java +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/BatchAnnotateFiles.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.example.vision; +package com.example.vision.snippets; // [START vision_batch_annotate_files] import com.google.cloud.vision.v1.AnnotateFileRequest; @@ -90,17 +90,17 @@ public static void batchAnnotateFiles(String filePath) throws IOException { // sample. for (AnnotateImageResponse imageResponse : response.getResponsesList().get(0).getResponsesList()) { - System.out.printf("Full text: %s\n", imageResponse.getFullTextAnnotation().getText()); + System.out.format("Full text: %s%n", imageResponse.getFullTextAnnotation().getText()); for (Page page : imageResponse.getFullTextAnnotation().getPagesList()) { for (Block block : page.getBlocksList()) { - System.out.printf("\nBlock confidence: %s\n", block.getConfidence()); + System.out.format("%nBlock confidence: %s%n", block.getConfidence()); for (Paragraph par : block.getParagraphsList()) { - System.out.printf("\tParagraph confidence: %s\n", par.getConfidence()); + System.out.format("\tParagraph confidence: %s%n", par.getConfidence()); for (Word word : par.getWordsList()) { - System.out.printf("\t\tWord confidence: %s\n", word.getConfidence()); + System.out.format("\t\tWord confidence: %s%n", word.getConfidence()); for (Symbol symbol : word.getSymbolsList()) { - System.out.printf( - "\t\t\tSymbol: %s, (confidence: %s)\n", + System.out.format( + "\t\t\tSymbol: %s, (confidence: %s)%n", symbol.getText(), symbol.getConfidence()); } } diff --git a/vision/cloud-client/src/main/java/com/example/vision/BatchAnnotateFilesGcs.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/BatchAnnotateFilesGcs.java similarity index 92% rename from vision/cloud-client/src/main/java/com/example/vision/BatchAnnotateFilesGcs.java rename to vision/cloud-client/src/main/java/com/example/vision/snippets/BatchAnnotateFilesGcs.java index dce825fd025..f7d1ca4e52a 100644 --- a/vision/cloud-client/src/main/java/com/example/vision/BatchAnnotateFilesGcs.java +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/BatchAnnotateFilesGcs.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.example.vision; +package com.example.vision.snippets; // [START vision_batch_annotate_files_gcs] import com.google.cloud.vision.v1.AnnotateFileRequest; @@ -85,17 +85,17 @@ public static void batchAnnotateFilesGcs(String gcsUri) throws IOException { // sample. for (AnnotateImageResponse imageResponse : response.getResponsesList().get(0).getResponsesList()) { - System.out.printf("Full text: %s\n", imageResponse.getFullTextAnnotation().getText()); + System.out.format("Full text: %s%n", imageResponse.getFullTextAnnotation().getText()); for (Page page : imageResponse.getFullTextAnnotation().getPagesList()) { for (Block block : page.getBlocksList()) { - System.out.printf("\nBlock confidence: %s\n", block.getConfidence()); + System.out.format("%nBlock confidence: %s%n", block.getConfidence()); for (Paragraph par : block.getParagraphsList()) { - System.out.printf("\tParagraph confidence: %s\n", par.getConfidence()); + System.out.format("\tParagraph confidence: %s%n", par.getConfidence()); for (Word word : par.getWordsList()) { - System.out.printf("\t\tWord confidence: %s\n", word.getConfidence()); + System.out.format("\t\tWord confidence: %s%n", word.getConfidence()); for (Symbol symbol : word.getSymbolsList()) { - System.out.printf( - "\t\t\tSymbol: %s, (confidence: %s)\n", + System.out.format( + "\t\t\tSymbol: %s, (confidence: %s)%n", symbol.getText(), symbol.getConfidence()); } } diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectCropHints.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectCropHints.java new file mode 100644 index 00000000000..705a87ce0f8 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectCropHints.java @@ -0,0 +1,76 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +// [START vision_crop_hint_detection] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.AnnotateImageResponse; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.CropHint; +import com.google.cloud.vision.v1.CropHintsAnnotation; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectCropHints { + public static void detectCropHints() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "path/to/your/image/file.jpg"; + detectCropHints(filePath); + } + + // Suggests a region to crop to for a local file. + public static void detectCropHints(String filePath) throws IOException { + List requests = new ArrayList<>(); + + ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); + + Image img = Image.newBuilder().setContent(imgBytes).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.CROP_HINTS).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + System.out.format("Error: %s%n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + CropHintsAnnotation annotation = res.getCropHintsAnnotation(); + for (CropHint hint : annotation.getCropHintsList()) { + System.out.println(hint.getBoundingPoly()); + } + } + } + } +} +// [END vision_crop_hint_detection] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectCropHintsGcs.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectCropHintsGcs.java new file mode 100644 index 00000000000..9448a4612c7 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectCropHintsGcs.java @@ -0,0 +1,75 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +// [START vision_crop_hint_detection_gcs] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.AnnotateImageResponse; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.CropHint; +import com.google.cloud.vision.v1.CropHintsAnnotation; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.cloud.vision.v1.ImageSource; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectCropHintsGcs { + + public static void detectCropHintsGcs() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; + detectCropHintsGcs(filePath); + } + + // Suggests a region to crop to for a remote file on Google Cloud Storage. + public static void detectCropHintsGcs(String gcsPath) throws IOException { + List requests = new ArrayList<>(); + + ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); + Image img = Image.newBuilder().setSource(imgSource).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.CROP_HINTS).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + System.out.format("Error: %s%n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + CropHintsAnnotation annotation = res.getCropHintsAnnotation(); + for (CropHint hint : annotation.getCropHintsList()) { + System.out.println(hint.getBoundingPoly()); + } + } + } + } +} +// [END vision_crop_hint_detection_gcs] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectFaces.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectFaces.java new file mode 100644 index 00000000000..4c07fa08080 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectFaces.java @@ -0,0 +1,80 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +// [START vision_face_detection] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.AnnotateImageResponse; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.FaceAnnotation; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectFaces { + + public static void detectFaces() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "path/to/your/image/file.jpg"; + detectFaces(filePath); + } + + // Detects faces in the specified local image. + public static void detectFaces(String filePath) throws IOException { + List requests = new ArrayList<>(); + + ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); + + Image img = Image.newBuilder().setContent(imgBytes).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.FACE_DETECTION).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + System.out.format("Error: %s%n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (FaceAnnotation annotation : res.getFaceAnnotationsList()) { + System.out.format( + "anger: %s%njoy: %s%nsurprise: %s%nposition: %s", + annotation.getAngerLikelihood(), + annotation.getJoyLikelihood(), + annotation.getSurpriseLikelihood(), + annotation.getBoundingPoly()); + } + } + } + } +} +// [END vision_face_detection] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectFacesGcs.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectFacesGcs.java new file mode 100644 index 00000000000..b325623e761 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectFacesGcs.java @@ -0,0 +1,79 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +// [START vision_face_detection_gcs] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.AnnotateImageResponse; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.FaceAnnotation; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.cloud.vision.v1.ImageSource; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectFacesGcs { + + public static void detectFacesGcs() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; + detectFacesGcs(filePath); + } + + // Detects faces in the specified remote image on Google Cloud Storage. + public static void detectFacesGcs(String gcsPath) throws IOException { + List requests = new ArrayList<>(); + + ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); + Image img = Image.newBuilder().setSource(imgSource).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.FACE_DETECTION).build(); + + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + System.out.format("Error: %s%n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (FaceAnnotation annotation : res.getFaceAnnotationsList()) { + System.out.format( + "anger: %s%njoy: %s%nsurprise: %s%nposition: %s", + annotation.getAngerLikelihood(), + annotation.getJoyLikelihood(), + annotation.getSurpriseLikelihood(), + annotation.getBoundingPoly()); + } + } + } + } +} +// [END vision_face_detection_gcs] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLabels.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLabels.java new file mode 100644 index 00000000000..87563bb3ca6 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLabels.java @@ -0,0 +1,77 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +// [START vision_label_detection] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.AnnotateImageResponse; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.EntityAnnotation; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectLabels { + + public static void detectLabels() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "path/to/your/image/file.jpg"; + detectLabels(filePath); + } + + // Detects labels in the specified local image. + public static void detectLabels(String filePath) throws IOException { + List requests = new ArrayList<>(); + + ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); + + Image img = Image.newBuilder().setContent(imgBytes).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.LABEL_DETECTION).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + System.out.format("Error: %s%n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getLabelAnnotationsList()) { + annotation + .getAllFields() + .forEach((k, v) -> System.out.format("%s : %s%n", k, v.toString())); + } + } + } + } +} +// [END vision_label_detection] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLabelsGcs.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLabelsGcs.java new file mode 100644 index 00000000000..53e8dfe5351 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLabelsGcs.java @@ -0,0 +1,75 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +// [START vision_label_detection_gcs] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.AnnotateImageResponse; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.EntityAnnotation; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.cloud.vision.v1.ImageSource; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectLabelsGcs { + + public static void detectLabelsGcs() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; + detectLabelsGcs(filePath); + } + + // Detects labels in the specified remote image on Google Cloud Storage. + public static void detectLabelsGcs(String gcsPath) throws IOException { + List requests = new ArrayList<>(); + + ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); + Image img = Image.newBuilder().setSource(imgSource).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.LABEL_DETECTION).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + System.out.format("Error: %s%n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getLabelAnnotationsList()) { + annotation + .getAllFields() + .forEach((k, v) -> System.out.format("%s : %s%n", k, v.toString())); + } + } + } + } +} +// [END vision_label_detection_gcs] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLandmarks.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLandmarks.java new file mode 100644 index 00000000000..5147e1e11dc --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLandmarks.java @@ -0,0 +1,75 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +// [START vision_landmark_detection] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.AnnotateImageResponse; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.EntityAnnotation; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.cloud.vision.v1.LocationInfo; +import com.google.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectLandmarks { + public static void detectLandmarks() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "path/to/your/image/file.jpg"; + detectLandmarks(filePath); + } + + // Detects landmarks in the specified local image. + public static void detectLandmarks(String filePath) throws IOException { + List requests = new ArrayList<>(); + ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); + + Image img = Image.newBuilder().setContent(imgBytes).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.LANDMARK_DETECTION).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + System.out.format("Error: %s%n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) { + LocationInfo info = annotation.getLocationsList().listIterator().next(); + System.out.format("Landmark: %s%n %s%n", annotation.getDescription(), info.getLatLng()); + } + } + } + } +} +// [END vision_landmark_detection] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLandmarksGcs.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLandmarksGcs.java new file mode 100644 index 00000000000..c5ff0c1e129 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLandmarksGcs.java @@ -0,0 +1,75 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +// [START vision_landmark_detection_gcs] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.AnnotateImageResponse; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.EntityAnnotation; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.cloud.vision.v1.ImageSource; +import com.google.cloud.vision.v1.LocationInfo; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectLandmarksGcs { + + public static void detectLandmarksGcs() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; + detectLandmarksGcs(filePath); + } + + // Detects landmarks in the specified remote image on Google Cloud Storage. + public static void detectLandmarksGcs(String gcsPath) throws IOException { + List requests = new ArrayList<>(); + + ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); + Image img = Image.newBuilder().setSource(imgSource).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.LANDMARK_DETECTION).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + System.out.format("Error: %s%n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) { + LocationInfo info = annotation.getLocationsList().listIterator().next(); + System.out.format("Landmark: %s%n %s%n", annotation.getDescription(), info.getLatLng()); + } + } + } + } +} +// [END vision_landmark_detection_gcs] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLandmarksUrl.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLandmarksUrl.java new file mode 100644 index 00000000000..ff52931863e --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLandmarksUrl.java @@ -0,0 +1,72 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.AnnotateImageResponse; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.EntityAnnotation; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.cloud.vision.v1.ImageSource; +import com.google.cloud.vision.v1.LocationInfo; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectLandmarksUrl { + + public static void detectLandmarksUrl() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; + detectLandmarksUrl(filePath); + } + + // Detects landmarks in the specified URI. + public static void detectLandmarksUrl(String uri) throws IOException { + List requests = new ArrayList<>(); + + ImageSource imgSource = ImageSource.newBuilder().setImageUri(uri).build(); + Image img = Image.newBuilder().setSource(imgSource).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.LANDMARK_DETECTION).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + System.out.format("Error: %s%n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) { + LocationInfo info = annotation.getLocationsList().listIterator().next(); + System.out.format("Landmark: %s%n %s%n", annotation.getDescription(), info.getLatLng()); + } + } + } + } +} diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLogos.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLogos.java new file mode 100644 index 00000000000..22785a21dd0 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLogos.java @@ -0,0 +1,75 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +// [START vision_logo_detection] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.AnnotateImageResponse; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.EntityAnnotation; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectLogos { + public static void detectLogos() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "path/to/your/image/file.jpg"; + detectLogos(filePath); + } + + // Detects logos in the specified local image. + + public static void detectLogos(String filePath) throws IOException { + List requests = new ArrayList<>(); + + ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); + + Image img = Image.newBuilder().setContent(imgBytes).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.LOGO_DETECTION).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + System.out.format("Error: %s%n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getLogoAnnotationsList()) { + System.out.println(annotation.getDescription()); + } + } + } + } +} +// [END vision_logo_detection] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLogosGcs.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLogosGcs.java new file mode 100644 index 00000000000..e7e0c2002bd --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLogosGcs.java @@ -0,0 +1,73 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +// [START vision_logo_detection_gcs] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.AnnotateImageResponse; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.EntityAnnotation; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.cloud.vision.v1.ImageSource; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectLogosGcs { + + public static void detectLogosGcs() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; + detectLogosGcs(filePath); + } + + // Detects logos in the specified remote image on Google Cloud Storage. + public static void detectLogosGcs(String gcsPath) throws IOException { + List requests = new ArrayList<>(); + + ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); + Image img = Image.newBuilder().setSource(imgSource).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.LOGO_DETECTION).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + System.out.format("Error: %s%n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getLogoAnnotationsList()) { + System.out.println(annotation.getDescription()); + } + } + } + } +} +// [END vision_logo_detection_gcs] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectProperties.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectProperties.java new file mode 100644 index 00000000000..9fd022b6469 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectProperties.java @@ -0,0 +1,81 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +// [START vision_image_property_detection] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.AnnotateImageResponse; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.ColorInfo; +import com.google.cloud.vision.v1.DominantColorsAnnotation; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectProperties { + public static void detectProperties() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "path/to/your/image/file.jpg"; + detectProperties(filePath); + } + + // Detects image properties such as color frequency from the specified local image. + public static void detectProperties(String filePath) throws IOException { + List requests = new ArrayList<>(); + + ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); + + Image img = Image.newBuilder().setContent(imgBytes).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.IMAGE_PROPERTIES).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + System.out.format("Error: %s%n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors(); + for (ColorInfo color : colors.getColorsList()) { + System.out.format( + "fraction: %f%nr: %f, g: %f, b: %f%n", + color.getPixelFraction(), + color.getColor().getRed(), + color.getColor().getGreen(), + color.getColor().getBlue()); + } + } + } + } +} +// [END vision_image_property_detection] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectPropertiesGcs.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectPropertiesGcs.java new file mode 100644 index 00000000000..2a974c63296 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectPropertiesGcs.java @@ -0,0 +1,81 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +// [START vision_image_property_detection_gcs] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.AnnotateImageResponse; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.ColorInfo; +import com.google.cloud.vision.v1.DominantColorsAnnotation; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.cloud.vision.v1.ImageSource; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectPropertiesGcs { + + public static void detectPropertiesGcs() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; + detectPropertiesGcs(filePath); + } + + // Detects image properties such as color frequency from the specified remote image on Google + // Cloud Storage. + public static void detectPropertiesGcs(String gcsPath) throws IOException { + List requests = new ArrayList<>(); + + ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); + Image img = Image.newBuilder().setSource(imgSource).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.IMAGE_PROPERTIES).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + System.out.format("Error: %s%n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors(); + for (ColorInfo color : colors.getColorsList()) { + System.out.format( + "fraction: %f%nr: %f, g: %f, b: %f%n", + color.getPixelFraction(), + color.getColor().getRed(), + color.getColor().getGreen(), + color.getColor().getBlue()); + } + } + } + } +} +// [END vision_image_property_detection_gcs] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectSafeSearch.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectSafeSearch.java new file mode 100644 index 00000000000..351869f8172 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectSafeSearch.java @@ -0,0 +1,79 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +// [START vision_safe_search_detection] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.AnnotateImageResponse; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.cloud.vision.v1.SafeSearchAnnotation; +import com.google.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectSafeSearch { + public static void detectSafeSearch() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "path/to/your/image/file.jpg"; + detectSafeSearch(filePath); + } + + // Detects whether the specified image has features you would want to moderate. + public static void detectSafeSearch(String filePath) throws IOException { + List requests = new ArrayList<>(); + + ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); + + Image img = Image.newBuilder().setContent(imgBytes).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.SAFE_SEARCH_DETECTION).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + System.out.format("Error: %s%n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); + System.out.format( + "adult: %s%nmedical: %s%nspoofed: %s%nviolence: %s%nracy: %s%n", + annotation.getAdult(), + annotation.getMedical(), + annotation.getSpoof(), + annotation.getViolence(), + annotation.getRacy()); + } + } + } +} +// [END vision_safe_search_detection] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectSafeSearchGcs.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectSafeSearchGcs.java new file mode 100644 index 00000000000..619404b6753 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectSafeSearchGcs.java @@ -0,0 +1,80 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +// [START vision_safe_search_detection_gcs] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.AnnotateImageResponse; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Feature.Type; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.cloud.vision.v1.ImageSource; +import com.google.cloud.vision.v1.SafeSearchAnnotation; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectSafeSearchGcs { + + public static void detectSafeSearchGcs() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; + detectSafeSearchGcs(filePath); + } + + // Detects whether the specified image on Google Cloud Storage has features you would want to + // moderate. + public static void detectSafeSearchGcs(String gcsPath) throws IOException { + List requests = new ArrayList<>(); + + ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); + Image img = Image.newBuilder().setSource(imgSource).build(); + Feature feat = Feature.newBuilder().setType(Type.SAFE_SEARCH_DETECTION).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + System.out.format("Error: %s%n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); + System.out.format( + "adult: %s%nmedical: %s%nspoofed: %s%nviolence: %s%nracy: %s%n", + annotation.getAdult(), + annotation.getMedical(), + annotation.getSpoof(), + annotation.getViolence(), + annotation.getRacy()); + } + } + } +} +// [END vision_safe_search_detection_gcs] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectText.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectText.java new file mode 100644 index 00000000000..8cac7288a5e --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectText.java @@ -0,0 +1,75 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +// [START vision_text_detection] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.AnnotateImageResponse; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.EntityAnnotation; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectText { + public static void detectText() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "path/to/your/image/file.jpg"; + detectText(filePath); + } + + // Detects text in the specified image. + public static void detectText(String filePath) throws IOException { + List requests = new ArrayList<>(); + + ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); + + Image img = Image.newBuilder().setContent(imgBytes).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.TEXT_DETECTION).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + System.out.format("Error: %s%n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getTextAnnotationsList()) { + System.out.format("Text: %s%n", annotation.getDescription()); + System.out.format("Position : %s%n", annotation.getBoundingPoly()); + } + } + } + } +} +// [END vision_text_detection] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectTextGcs.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectTextGcs.java new file mode 100644 index 00000000000..18bedcdcf5c --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectTextGcs.java @@ -0,0 +1,74 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +// [START vision_text_detection_gcs] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.AnnotateImageResponse; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.EntityAnnotation; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.cloud.vision.v1.ImageSource; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectTextGcs { + + public static void detectTextGcs() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; + detectTextGcs(filePath); + } + + // Detects text in the specified remote image on Google Cloud Storage. + public static void detectTextGcs(String gcsPath) throws IOException { + List requests = new ArrayList<>(); + + ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); + Image img = Image.newBuilder().setSource(imgSource).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.TEXT_DETECTION).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + System.out.format("Error: %s%n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getTextAnnotationsList()) { + System.out.format("Text: %s%n", annotation.getDescription()); + System.out.format("Position : %s%n", annotation.getBoundingPoly()); + } + } + } + } +} +// [END vision_text_detection_gcs] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebDetections.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebDetections.java new file mode 100644 index 00000000000..98a74f4d691 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebDetections.java @@ -0,0 +1,105 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +// [START vision_web_detection] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.AnnotateImageResponse; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Feature.Type; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.cloud.vision.v1.WebDetection; +import com.google.cloud.vision.v1.WebDetection.WebEntity; +import com.google.cloud.vision.v1.WebDetection.WebImage; +import com.google.cloud.vision.v1.WebDetection.WebLabel; +import com.google.cloud.vision.v1.WebDetection.WebPage; +import com.google.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectWebDetections { + + public static void detectWebDetections() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "path/to/your/image/file.jpg"; + detectWebDetections(filePath); + } + + // Finds references to the specified image on the web. + public static void detectWebDetections(String filePath) throws IOException { + List requests = new ArrayList<>(); + + ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); + + Image img = Image.newBuilder().setContent(imgBytes).build(); + Feature feat = Feature.newBuilder().setType(Type.WEB_DETECTION).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + System.out.format("Error: %s%n", res.getError().getMessage()); + return; + } + + // Search the web for usages of the image. You could use these signals later + // for user input moderation or linking external references. + // For a full list of available annotations, see http://g.co/cloud/vision/docs + WebDetection annotation = res.getWebDetection(); + System.out.println("Entity:Id:Score"); + System.out.println("==============="); + for (WebEntity entity : annotation.getWebEntitiesList()) { + System.out.println( + entity.getDescription() + " : " + entity.getEntityId() + " : " + entity.getScore()); + } + for (WebLabel label : annotation.getBestGuessLabelsList()) { + System.out.format("%nBest guess label: %s", label.getLabel()); + } + System.out.println("%nPages with matching images: Score%n=="); + for (WebPage page : annotation.getPagesWithMatchingImagesList()) { + System.out.println(page.getUrl() + " : " + page.getScore()); + } + System.out.println("%nPages with partially matching images: Score%n=="); + for (WebImage image : annotation.getPartialMatchingImagesList()) { + System.out.println(image.getUrl() + " : " + image.getScore()); + } + System.out.println("%nPages with fully matching images: Score%n=="); + for (WebImage image : annotation.getFullMatchingImagesList()) { + System.out.println(image.getUrl() + " : " + image.getScore()); + } + System.out.println("%nPages with visually similar images: Score%n=="); + for (WebImage image : annotation.getVisuallySimilarImagesList()) { + System.out.println(image.getUrl() + " : " + image.getScore()); + } + } + } + } +} +// [END vision_web_detection] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebDetectionsGcs.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebDetectionsGcs.java new file mode 100644 index 00000000000..484a6711cc6 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebDetectionsGcs.java @@ -0,0 +1,99 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +// [START vision_web_detection_gcs] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.AnnotateImageResponse; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.cloud.vision.v1.ImageSource; +import com.google.cloud.vision.v1.WebDetection; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectWebDetectionsGcs { + + public static void detectWebDetectionsGcs() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; + detectWebDetectionsGcs(filePath); + } + + // Detects whether the remote image on Google Cloud Storage has features you would want to + // moderate. + public static void detectWebDetectionsGcs(String gcsPath) throws IOException { + List requests = new ArrayList<>(); + + ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); + Image img = Image.newBuilder().setSource(imgSource).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.WEB_DETECTION).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + System.out.format("Error: %s%n", res.getError().getMessage()); + return; + } + + // Search the web for usages of the image. You could use these signals later + // for user input moderation or linking external references. + // For a full list of available annotations, see http://g.co/cloud/vision/docs + WebDetection annotation = res.getWebDetection(); + System.out.println("Entity:Id:Score"); + System.out.println("==============="); + for (WebDetection.WebEntity entity : annotation.getWebEntitiesList()) { + System.out.println( + entity.getDescription() + " : " + entity.getEntityId() + " : " + entity.getScore()); + } + for (WebDetection.WebLabel label : annotation.getBestGuessLabelsList()) { + System.out.format("%nBest guess label: %s", label.getLabel()); + } + System.out.println("%nPages with matching images: Score%n=="); + for (WebDetection.WebPage page : annotation.getPagesWithMatchingImagesList()) { + System.out.println(page.getUrl() + " : " + page.getScore()); + } + System.out.println("%nPages with partially matching images: Score%n=="); + for (WebDetection.WebImage image : annotation.getPartialMatchingImagesList()) { + System.out.println(image.getUrl() + " : " + image.getScore()); + } + System.out.println("%nPages with fully matching images: Score%n=="); + for (WebDetection.WebImage image : annotation.getFullMatchingImagesList()) { + System.out.println(image.getUrl() + " : " + image.getScore()); + } + System.out.println("%nPages with visually similar images: Score%n=="); + for (WebDetection.WebImage image : annotation.getVisuallySimilarImagesList()) { + System.out.println(image.getUrl() + " : " + image.getScore()); + } + } + } + } +} +// [END vision_web_detection_gcs] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntities.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntities.java new file mode 100644 index 00000000000..c8e99065754 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntities.java @@ -0,0 +1,75 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +// [START vision_web_detection] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Feature.Type; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Arrays; + +public class DetectWebEntities { + + public static void detectWebEntities() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "path/to/your/image/file.jpg"; + detectWebEntities(filePath); + } + + // Find web entities given a local image. + public static void detectWebEntities(String filePath) throws IOException { + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + // Read in the local image + ByteString contents = ByteString.readFrom(new FileInputStream(filePath)); + + // Build the image + Image image = Image.newBuilder().setContent(contents).build(); + + // Create the request with the image and the specified feature: web detection + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder() + .addFeatures(Feature.newBuilder().setType(Type.WEB_DETECTION)) + .setImage(image) + .build(); + + // Perform the request + BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request)); + + // Display the results + response.getResponsesList().stream() + .forEach( + r -> + r.getWebDetection().getWebEntitiesList().stream() + .forEach( + entity -> { + System.out.format("Description: %s%n", entity.getDescription()); + System.out.format("Score: %f%n", entity.getScore()); + })); + } + } +} diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntitiesGcs.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntitiesGcs.java new file mode 100644 index 00000000000..5e4fab05bc9 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntitiesGcs.java @@ -0,0 +1,70 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.cloud.vision.v1.ImageSource; +import java.io.IOException; +import java.util.Arrays; + +public class DetectWebEntitiesGcs { + + public static void detectWebEntitiesGcs() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; + detectWebEntitiesGcs(filePath); + } + + // Find web entities given the remote image on Google Cloud Storage. + public static void detectWebEntitiesGcs(String gcsPath) throws IOException { + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + // Set the image source to the given gs uri + ImageSource imageSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); + // Build the image + Image image = Image.newBuilder().setSource(imageSource).build(); + + // Create the request with the image and the specified feature: web detection + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder() + .addFeatures(Feature.newBuilder().setType(Feature.Type.WEB_DETECTION)) + .setImage(image) + .build(); + + // Perform the request + BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request)); + + // Display the results + response.getResponsesList().stream() + .forEach( + r -> + r.getWebDetection().getWebEntitiesList().stream() + .forEach( + entity -> { + System.out.format("Description: %s%n", entity.getDescription()); + System.out.format("Score: %f%n", entity.getScore()); + })); + } + } +} diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntitiesIncludeGeoResults.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntitiesIncludeGeoResults.java new file mode 100644 index 00000000000..f1a0f58c969 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntitiesIncludeGeoResults.java @@ -0,0 +1,87 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +// [START vision_web_detection_include_geo] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Feature.Type; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.cloud.vision.v1.ImageContext; +import com.google.cloud.vision.v1.WebDetectionParams; +import com.google.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Arrays; + +public class DetectWebEntitiesIncludeGeoResults { + + public static void detectWebEntitiesIncludeGeoResults() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "path/to/your/image/file.jpg"; + detectWebEntitiesIncludeGeoResults(filePath); + } + + // Find web entities given a local image. + public static void detectWebEntitiesIncludeGeoResults(String filePath) throws IOException { + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + // Read in the local image + ByteString contents = ByteString.readFrom(new FileInputStream(filePath)); + + // Build the image + Image image = Image.newBuilder().setContent(contents).build(); + + // Enable `IncludeGeoResults` + WebDetectionParams webDetectionParams = + WebDetectionParams.newBuilder().setIncludeGeoResults(true).build(); + + // Set the parameters for the image + ImageContext imageContext = + ImageContext.newBuilder().setWebDetectionParams(webDetectionParams).build(); + + // Create the request with the image, imageContext, and the specified feature: web detection + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder() + .addFeatures(Feature.newBuilder().setType(Type.WEB_DETECTION)) + .setImage(image) + .setImageContext(imageContext) + .build(); + + // Perform the request + BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request)); + + // Display the results + response.getResponsesList().stream() + .forEach( + r -> + r.getWebDetection().getWebEntitiesList().stream() + .forEach( + entity -> { + System.out.format("Description: %s%n", entity.getDescription()); + System.out.format("Score: %f%n", entity.getScore()); + })); + } + } +} +// [END vision_web_detection_include_geo] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntitiesIncludeGeoResultsGcs.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntitiesIncludeGeoResultsGcs.java new file mode 100644 index 00000000000..d6dc1941ea3 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntitiesIncludeGeoResultsGcs.java @@ -0,0 +1,84 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision.snippets; + +// [START vision_web_detection_include_geo_gcs] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.Feature; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.cloud.vision.v1.ImageContext; +import com.google.cloud.vision.v1.ImageSource; +import com.google.cloud.vision.v1.WebDetectionParams; +import java.io.IOException; +import java.util.Arrays; + +public class DetectWebEntitiesIncludeGeoResultsGcs { + + public static void detectWebEntitiesIncludeGeoResultsGcs() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; + detectWebEntitiesIncludeGeoResultsGcs(filePath); + } + + // Find web entities given the remote image on Google Cloud Storage. + public static void detectWebEntitiesIncludeGeoResultsGcs(String gcsPath) throws IOException { + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + // Set the image source to the given gs uri + ImageSource imageSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); + // Build the image + Image image = Image.newBuilder().setSource(imageSource).build(); + + // Enable `IncludeGeoResults` + WebDetectionParams webDetectionParams = + WebDetectionParams.newBuilder().setIncludeGeoResults(true).build(); + + // Set the parameters for the image + ImageContext imageContext = + ImageContext.newBuilder().setWebDetectionParams(webDetectionParams).build(); + + // Create the request with the image, imageContext, and the specified feature: web detection + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder() + .addFeatures(Feature.newBuilder().setType(Feature.Type.WEB_DETECTION)) + .setImage(image) + .setImageContext(imageContext) + .build(); + + // Perform the request + BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request)); + + // Display the results + response.getResponsesList().stream() + .forEach( + r -> + r.getWebDetection().getWebEntitiesList().stream() + .forEach( + entity -> { + System.out.format("Description: %s%n", entity.getDescription()); + System.out.format("Score: %f%n", entity.getScore()); + })); + } + } +} +// [END vision_web_detection_include_geo_gcs] diff --git a/vision/cloud-client/src/main/java/com/example/vision/SetEndpoint.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/SetEndpoint.java similarity index 91% rename from vision/cloud-client/src/main/java/com/example/vision/SetEndpoint.java rename to vision/cloud-client/src/main/java/com/example/vision/snippets/SetEndpoint.java index 22eed6813d4..7aeb05abb70 100644 --- a/vision/cloud-client/src/main/java/com/example/vision/SetEndpoint.java +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/SetEndpoint.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.example.vision; +package com.example.vision.snippets; import com.google.cloud.vision.v1.AnnotateImageRequest; import com.google.cloud.vision.v1.AnnotateImageResponse; @@ -29,10 +29,10 @@ import java.util.ArrayList; import java.util.List; -class SetEndpoint { +public class SetEndpoint { // Change your endpoint - static void setEndpoint() throws IOException { + public static void setEndpoint() throws IOException { // [START vision_set_endpoint] ImageAnnotatorSettings settings = ImageAnnotatorSettings.newBuilder().setEndpoint("eu-vision.googleapis.com:443").build(); @@ -58,9 +58,9 @@ static void setEndpoint() throws IOException { for (AnnotateImageResponse response : batchResponse.getResponsesList()) { for (EntityAnnotation annotation : response.getTextAnnotationsList()) { - System.out.printf("Text: %s\n", annotation.getDescription()); + System.out.format("Text: %s%n", annotation.getDescription()); System.out.println("Position:"); - System.out.printf("%s\n", annotation.getBoundingPoly()); + System.out.format("%s%n", annotation.getBoundingPoly()); } } client.close(); diff --git a/vision/cloud-client/src/test/java/com/example/vision/AsyncBatchAnnotateImagesTest.java b/vision/cloud-client/src/test/java/com/example/vision/AsyncBatchAnnotateImagesTest.java index 8edc89828fb..aa05841ed45 100644 --- a/vision/cloud-client/src/test/java/com/example/vision/AsyncBatchAnnotateImagesTest.java +++ b/vision/cloud-client/src/test/java/com/example/vision/AsyncBatchAnnotateImagesTest.java @@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat; import static junit.framework.TestCase.assertNotNull; +import com.example.vision.snippets.AsyncBatchAnnotateImages; import com.google.api.gax.paging.Page; import com.google.cloud.storage.Blob; import com.google.cloud.storage.Storage; diff --git a/vision/cloud-client/src/test/java/com/example/vision/BatchAnnotateFilesGcsTest.java b/vision/cloud-client/src/test/java/com/example/vision/BatchAnnotateFilesGcsTest.java index 5f1c1e043a3..f8ff793559d 100644 --- a/vision/cloud-client/src/test/java/com/example/vision/BatchAnnotateFilesGcsTest.java +++ b/vision/cloud-client/src/test/java/com/example/vision/BatchAnnotateFilesGcsTest.java @@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat; +import com.example.vision.snippets.BatchAnnotateFilesGcs; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; diff --git a/vision/cloud-client/src/test/java/com/example/vision/BatchAnnotateFilesTest.java b/vision/cloud-client/src/test/java/com/example/vision/BatchAnnotateFilesTest.java index 621aba76798..f08718981ca 100644 --- a/vision/cloud-client/src/test/java/com/example/vision/BatchAnnotateFilesTest.java +++ b/vision/cloud-client/src/test/java/com/example/vision/BatchAnnotateFilesTest.java @@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat; +import com.example.vision.snippets.BatchAnnotateFiles; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; diff --git a/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ModelApiIT.java b/vision/cloud-client/src/test/java/com/example/vision/DetectCropHintsGcsTest.java similarity index 51% rename from vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ModelApiIT.java rename to vision/cloud-client/src/test/java/com/example/vision/DetectCropHintsGcsTest.java index 164c5e9586e..acbf5503d44 100644 --- a/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ModelApiIT.java +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectCropHintsGcsTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 Google Inc. + * Copyright 2017 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,32 +14,31 @@ * limitations under the License. */ -package com.google.cloud.vision.samples.automl; +package com.example.vision; import static com.google.common.truth.Truth.assertThat; +import com.example.vision.snippets.DetectCropHintsGcs; import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import java.util.regex.Pattern; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -/** Tests for vision "Model API" sample. */ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") -public class ModelApiIT { - private static final String PROJECT_ID = "java-docs-samples-testing"; - private static final String COMPUTE_REGION = "us-central1"; +public class DetectCropHintsGcsTest { + + private static final String ASSET_BUCKET = "cloud-samples-data"; + private ByteArrayOutputStream bout; private PrintStream out; - private String modelId; - private String modelEvaluationId; @Before public void setUp() { - bout = new ByteArrayOutputStream(); out = new PrintStream(bout); System.setOut(out); @@ -51,35 +50,14 @@ public void tearDown() { } @Test - public void testModelApi() { + public void testCropHintsGcs() throws Exception { // Act - ModelApi.listModels(PROJECT_ID, COMPUTE_REGION, ""); + DetectCropHintsGcs.detectCropHintsGcs("gs://" + ASSET_BUCKET + "/vision/label/wakeupcat.jpg"); // Assert String got = bout.toString(); - modelId = got.split("\n")[1].split("/")[got.split("\n")[1].split("/").length - 1]; - assertThat(got).contains("Model id:"); - - // Act - ModelApi.getModel(PROJECT_ID, COMPUTE_REGION, modelId); - - // Assert - got = bout.toString(); - assertThat(got).contains("Model name:"); - - // Act - ModelApi.listModelEvaluations(PROJECT_ID, COMPUTE_REGION, modelId, ""); - - // Assert - got = bout.toString(); - modelEvaluationId = got.split("List of model evaluations:")[1].split("\"")[1].split("/")[7]; - assertThat(got).contains("name:"); - - // Act - ModelApi.getModelEvaluation(PROJECT_ID, COMPUTE_REGION, modelId, modelEvaluationId); - - // Assert - got = bout.toString(); - assertThat(got).contains("name:"); + assertThat(got).contains("vertices {"); + assertThat(got).containsMatch(Pattern.compile("x: 2\\d{2}")); + assertThat(got).containsMatch(Pattern.compile("y: 4\\d{2}")); } } diff --git a/bigquery/bigquerystorage/src/test/java/com/example/bigquerystorage/QuickstartArrowSampleIT.java b/vision/cloud-client/src/test/java/com/example/vision/DetectCropHintsTest.java similarity index 71% rename from bigquery/bigquerystorage/src/test/java/com/example/bigquerystorage/QuickstartArrowSampleIT.java rename to vision/cloud-client/src/test/java/com/example/vision/DetectCropHintsTest.java index faffde73304..2a6b11c2b6d 100644 --- a/bigquery/bigquerystorage/src/test/java/com/example/bigquerystorage/QuickstartArrowSampleIT.java +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectCropHintsTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Google Inc. + * Copyright 2017 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,24 +14,23 @@ * limitations under the License. */ -package com.example.bigquerystorage; +package com.example.vision; import static com.google.common.truth.Truth.assertThat; +import com.example.vision.snippets.DetectCropHints; import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import java.util.regex.Pattern; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -/** Tests for quickstart sample. */ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") -public class QuickstartArrowSampleIT { - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - +public class DetectCropHintsTest { private ByteArrayOutputStream bout; private PrintStream out; @@ -48,11 +47,14 @@ public void tearDown() { } @Test - public void testQuickstart() throws Exception { - StorageArrowSample.main(PROJECT_ID); + public void testCropHints() throws Exception { + // Act + DetectCropHints.detectCropHints("./resources/wakeupcat.jpg"); + + // Assert String got = bout.toString(); - // Ensure at least 1k of output generated and a specific token was present in the output. - assertThat(bout.size()).isGreaterThan(1024); - assertThat(got).contains("Zayvion"); + assertThat(got).contains("vertices {"); + assertThat(got).containsMatch(Pattern.compile("x: 2\\d{2}")); + assertThat(got).containsMatch(Pattern.compile("y: 4\\d{2}")); } } diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectFacesGcsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectFacesGcsTest.java new file mode 100644 index 00000000000..8de0dac2d22 --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectFacesGcsTest.java @@ -0,0 +1,62 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectFacesGcs; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectFacesGcsTest { + + private static final String ASSET_BUCKET = "cloud-samples-data"; + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testFaces() throws Exception { + // Act + DetectFacesGcs.detectFacesGcs("gs://" + ASSET_BUCKET + "/vision/face/face_no_surprise.jpg"); + + // Assert + String got = bout.toString(); + assertThat(got).contains("anger:"); + assertThat(got).contains("joy:"); + assertThat(got).contains("surprise:"); + } +} diff --git a/bigquery/bigquerystorage/src/test/java/com/example/bigquerystorage/QuickstartSampleIT.java b/vision/cloud-client/src/test/java/com/example/vision/DetectFacesTest.java similarity index 71% rename from bigquery/bigquerystorage/src/test/java/com/example/bigquerystorage/QuickstartSampleIT.java rename to vision/cloud-client/src/test/java/com/example/vision/DetectFacesTest.java index a3a18e72201..d009212739d 100644 --- a/bigquery/bigquerystorage/src/test/java/com/example/bigquerystorage/QuickstartSampleIT.java +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectFacesTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Google Inc. + * Copyright 2017 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,10 +14,11 @@ * limitations under the License. */ -package com.example.bigquerystorage; +package com.example.vision; import static com.google.common.truth.Truth.assertThat; +import com.example.vision.snippets.DetectFaces; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import org.junit.After; @@ -26,12 +27,9 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -/** Tests for quickstart sample. */ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") -public class QuickstartSampleIT { - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - +public class DetectFacesTest { private ByteArrayOutputStream bout; private PrintStream out; @@ -48,11 +46,14 @@ public void tearDown() { } @Test - public void testQuickstart() throws Exception { - StorageSample.main(PROJECT_ID); + public void testFaces() throws Exception { + // Act + DetectFaces.detectFaces("./resources/face_no_surprise.jpg"); + + // Assert String got = bout.toString(); - // Ensure at least 1k of output generated and a specific token was present in the output. - assertThat(bout.size()).isGreaterThan(1024); - assertThat(got).contains("Zayvion"); + assertThat(got).contains("anger:"); + assertThat(got).contains("joy:"); + assertThat(got).contains("surprise:"); } } diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectIT.java b/vision/cloud-client/src/test/java/com/example/vision/DetectIT.java index ad3c19d85a5..6ccdcf7eb6b 100644 --- a/vision/cloud-client/src/test/java/com/example/vision/DetectIT.java +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectIT.java @@ -26,7 +26,6 @@ import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.util.UUID; -import java.util.regex.Pattern; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -39,8 +38,8 @@ public class DetectIT { private ByteArrayOutputStream bout; private PrintStream out; - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String ASSET_BUCKET = "cloud-samples-data"; + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String OUTPUT_BUCKET = PROJECT_ID; private static final String OUTPUT_PREFIX = "OCR_PDF_TEST_OUTPUT_" + UUID.randomUUID().toString(); @@ -56,285 +55,10 @@ public void tearDown() { System.setOut(null); } - @Test - public void testFaces() throws Exception { - // Act - String[] args = {"faces", "./resources/face_no_surprise.jpg"}; - Detect.argsHelper(args, out); - - // Assert - String got = bout.toString(); - assertThat(got).contains("anger:"); - assertThat(got).contains("joy:"); - assertThat(got).contains("surprise:"); - } - - @Test - public void testFacesGcs() throws Exception { - // Act - String[] args = {"faces", "gs://" + ASSET_BUCKET + "/vision/face/face_no_surprise.jpg"}; - Detect.argsHelper(args, out); - - // Assert - String got = bout.toString(); - assertThat(got).contains("anger:"); - assertThat(got).contains("joy:"); - assertThat(got).contains("surprise:"); - } - - @Test - public void testLabels() throws Exception { - // Act - String[] args = {"labels", "./resources/wakeupcat.jpg"}; - Detect.argsHelper(args, out); - - // Assert - String got = bout.toString().toLowerCase(); - assertThat(got).contains("whiskers"); - } - - @Test - public void testLabelsGcs() throws Exception { - // Act - String[] args = {"labels", "gs://" + ASSET_BUCKET + "/vision/label/wakeupcat.jpg"}; - Detect.argsHelper(args, out); - - // Assert - String got = bout.toString().toLowerCase(); - assertThat(got).contains("whiskers"); - } - - @Test - public void testLandmarks() throws Exception { - // Act - String[] args = {"landmarks", "./resources/landmark.jpg"}; - Detect.argsHelper(args, out); - - // Assert - String got = bout.toString().toLowerCase(); - assertThat(got).contains("palace of fine arts"); - } - - @Test - public void testLandmarksGcs() throws Exception { - // Act - String[] args = {"landmarks", "gs://" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"}; - Detect.argsHelper(args, out); - - // Assert - String got = bout.toString().toLowerCase(); - assertThat(got).contains("palace of fine arts"); - } - - @Test - public void testLandmarksUrl() throws Exception { - // Act - String uri = - "https://storage-download.googleapis.com/" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"; - String[] args = {"landmarks", uri}; - Detect.argsHelper(args, out); - - // Assert - String got = bout.toString().toLowerCase(); - assertThat(got).contains("palace of fine arts"); - } - - @Test - public void testLogos() throws Exception { - // Act - String[] args = {"logos", "./resources/logos.png"}; - Detect.argsHelper(args, out); - - // Assert - String got = bout.toString().toLowerCase(); - assertThat(got).contains("google"); - } - - @Test - public void testLogosGcs() throws Exception { - // Act - String[] args = {"logos", "gs://" + ASSET_BUCKET + "/vision/logo/logo_google.png"}; - Detect.argsHelper(args, out); - - // Assert - String got = bout.toString().toLowerCase(); - assertThat(got).contains("google"); - } - - @Test - public void testText() throws Exception { - // Act - String[] args = {"text", "./resources/text.jpg"}; - Detect.argsHelper(args, out); - - // Assert - String got = bout.toString(); - assertThat(got).contains("37%"); - } - - @Test - public void testTextGcs() throws Exception { - // Act - String[] args = {"text", "gs://" + ASSET_BUCKET + "/vision/text/screen.jpg"}; - Detect.argsHelper(args, out); - - // Assert - String got = bout.toString(); - assertThat(got).contains("37%"); - } - - @Test - public void testSafeSearch() throws Exception { - // Act - String[] args = {"safe-search", "./resources/wakeupcat.jpg"}; - Detect.argsHelper(args, out); - - // Assert - String got = bout.toString(); - assertThat(got).contains("adult:"); - assertThat(got).contains("racy:"); - } - - @Test - public void testSafeSearchGcs() throws Exception { - // Act - String[] args = {"safe-search", "gs://" + ASSET_BUCKET + "/vision/label/wakeupcat.jpg"}; - Detect.argsHelper(args, out); - - // Assert - String got = bout.toString(); - assertThat(got).contains("adult:"); - assertThat(got).contains("racy:"); - } - - @Test - public void testProperties() throws Exception { - // Act - String[] args = {"properties", "./resources/landmark.jpg"}; - Detect.argsHelper(args, out); - - // Assert - String got = bout.toString(); - assertThat(got).contains("fraction:"); - assertThat(got).contains("r:"); - assertThat(got).contains("g:"); - assertThat(got).contains("b:"); - } - - @Test - public void testPropertiesGcs() throws Exception { - // Act - String[] args = {"properties", "gs://" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"}; - Detect.argsHelper(args, out); - - // Assert - String got = bout.toString(); - assertThat(got).contains("fraction:"); - assertThat(got).contains("r:"); - assertThat(got).contains("g:"); - assertThat(got).contains("b:"); - } - - @Test - public void detectWebAnnotations() throws Exception { - // Act - String[] args = {"web", "./resources/landmark.jpg"}; - Detect.argsHelper(args, out); - - // Assert - String got = bout.toString().toLowerCase(); - assertThat(got).contains("entity:id:score"); - assertThat(got).contains("best guess label"); - } - - @Test - public void detectWebAnnotationsGcs() throws Exception { - // Act - String[] args = {"web", "gs://" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"}; - Detect.argsHelper(args, out); - - // Assert - String got = bout.toString().toLowerCase(); - assertThat(got).contains("entity:id:score"); - assertThat(got).contains("best guess label"); - } - - @Test - public void testDetectWebEntities() throws Exception { - // Act - String[] args = {"web-entities", "./resources/city.jpg"}; - Detect.argsHelper(args, out); - - // Assert - String got = bout.toString().toLowerCase(); - assertThat(got).doesNotContain("zepra"); - } - - @Test - public void testDetectWebEntitiesGcs() throws Exception { - // Act - String[] args = {"web-entities", "gs://" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"}; - Detect.argsHelper(args, out); - - String got = bout.toString().toLowerCase(); - assertThat(got).contains("description"); - } - - @Test - public void testDetectWebEntitiesIncludeGeoResults() throws Exception { - // Act - String[] args = {"web-entities-include-geo", "./resources/city.jpg"}; - Detect.argsHelper(args, out); - - // Assert - String got = bout.toString().toLowerCase(); - // Note: entities and labels can change over time. - assertThat(got).doesNotContain("error"); - } - - @Test - public void testDetectWebEntitiesIncludeGeoResultsGcs() throws Exception { - // Act - String[] args = { - "web-entities-include-geo", "gs://" + ASSET_BUCKET + "/vision/landmark/pofa.jpg" - }; - Detect.argsHelper(args, out); - - String got = bout.toString().toLowerCase(); - assertThat(got).contains("description"); - } - - @Test - public void testCropHints() throws Exception { - // Act - String[] args = {"crop", "./resources/wakeupcat.jpg"}; - Detect.argsHelper(args, out); - - // Assert - String got = bout.toString(); - assertThat(got).contains("vertices {"); - assertThat(got).containsMatch(Pattern.compile("x: 2\\d{2}")); - assertThat(got).containsMatch(Pattern.compile("y: 4\\d{2}")); - } - - @Test - public void testCropHintsGcs() throws Exception { - // Act - String[] args = {"crop", "gs://" + ASSET_BUCKET + "/vision/label/wakeupcat.jpg"}; - Detect.argsHelper(args, out); - - // Assert - String got = bout.toString(); - assertThat(got).contains("vertices {"); - assertThat(got).containsMatch(Pattern.compile("x: 2\\d{2}")); - assertThat(got).containsMatch(Pattern.compile("y: 4\\d{2}")); - } - @Test public void testDocumentText() throws Exception { // Act - String[] args = {"fulltext", "./resources/text.jpg"}; - Detect.argsHelper(args, out); + Detect.detectDocumentText("./resources/text.jpg"); // Assert String got = bout.toString(); @@ -346,8 +70,7 @@ public void testDocumentText() throws Exception { @Test public void testDocumentTextGcs() throws Exception { // Act - String[] args = {"fulltext", "gs://" + ASSET_BUCKET + "/vision/text/screen.jpg"}; - Detect.argsHelper(args, out); + Detect.detectDocumentTextGcs("gs://" + ASSET_BUCKET + "/vision/text/screen.jpg"); // Assert String got = bout.toString(); @@ -359,12 +82,9 @@ public void testDocumentTextGcs() throws Exception { @Test public void testDetectDocumentsGcs() throws Exception { // Act - String[] args = { - "ocr", - "gs://" + ASSET_BUCKET + "/vision/document/custom_0773375000.pdf", - "gs://" + OUTPUT_BUCKET + "/" + OUTPUT_PREFIX + "/" - }; - Detect.argsHelper(args, out); + Detect.detectDocumentsGcs( + "gs://" + ASSET_BUCKET + "/vision/document/custom_0773375000.pdf", + "gs://" + OUTPUT_BUCKET + "/" + OUTPUT_PREFIX + "/"); // Assert String got = bout.toString(); @@ -386,8 +106,7 @@ public void testDetectDocumentsGcs() throws Exception { @Test public void testDetectLocalizedObjects() throws Exception { // Act - String[] args = {"object-localization", "./resources/puppies.jpg"}; - Detect.argsHelper(args, out); + Detect.detectLocalizedObjects("./resources/puppies.jpg"); // Assert String got = bout.toString().toLowerCase(); @@ -397,10 +116,8 @@ public void testDetectLocalizedObjects() throws Exception { @Test public void testDetectLocalizedObjectsGcs() throws Exception { // Act - String[] args = { - "object-localization", "gs://cloud-samples-data/vision/object_localization/puppies.jpg" - }; - Detect.argsHelper(args, out); + Detect.detectLocalizedObjectsGcs( + "gs://cloud-samples-data/vision/object_localization/puppies.jpg"); // Assert String got = bout.toString().toLowerCase(); diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectLabelsGcsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectLabelsGcsTest.java new file mode 100644 index 00000000000..9ddb0e2e515 --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectLabelsGcsTest.java @@ -0,0 +1,59 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectLabelsGcs; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectLabelsGcsTest { + private static final String ASSET_BUCKET = "cloud-samples-data"; + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testLabelsGcs() throws Exception { + // Act + DetectLabelsGcs.detectLabelsGcs("gs://" + ASSET_BUCKET + "/vision/label/wakeupcat.jpg"); + + // Assert + String got = bout.toString().toLowerCase(); + assertThat(got).contains("whiskers"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectLabelsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectLabelsTest.java new file mode 100644 index 00000000000..741b2f63142 --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectLabelsTest.java @@ -0,0 +1,57 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectLabels; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectLabelsTest { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testLabels() throws Exception { + // Act + DetectLabels.detectLabels("./resources/wakeupcat.jpg"); + + // Assert + String got = bout.toString().toLowerCase(); + assertThat(got).contains("whiskers"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectLandmarksGcsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectLandmarksGcsTest.java new file mode 100644 index 00000000000..ef9e4c5ca9e --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectLandmarksGcsTest.java @@ -0,0 +1,73 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectLandmarksGcs; +import com.example.vision.snippets.DetectLandmarksUrl; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectLandmarksGcsTest { + + private static final String ASSET_BUCKET = "cloud-samples-data"; + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testLandmarksUrl() throws Exception { + // Act + String uri = + "https://storage-download.googleapis.com/" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"; + DetectLandmarksUrl.detectLandmarksUrl(uri); + + // Assert + String got = bout.toString().toLowerCase(); + assertThat(got).contains("palace of fine arts"); + } + + @Test + public void testLandmarksGcs() throws Exception { + // Act + DetectLandmarksGcs.detectLandmarksGcs("gs://" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"); + + // Assert + String got = bout.toString().toLowerCase(); + assertThat(got).contains("palace of fine arts"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectLandmarksTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectLandmarksTest.java new file mode 100644 index 00000000000..d937a7f11c8 --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectLandmarksTest.java @@ -0,0 +1,57 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectLandmarks; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectLandmarksTest { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testLandmarks() throws Exception { + // Act + DetectLandmarks.detectLandmarks("./resources/landmark.jpg"); + + // Assert + String got = bout.toString().toLowerCase(); + assertThat(got).contains("palace of fine arts"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectLogosGcsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectLogosGcsTest.java new file mode 100644 index 00000000000..1f6cd1c5201 --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectLogosGcsTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectLogosGcs; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectLogosGcsTest { + + private static final String ASSET_BUCKET = "cloud-samples-data"; + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testLogosGcs() throws Exception { + // Act + DetectLogosGcs.detectLogosGcs("gs://" + ASSET_BUCKET + "/vision/logo/logo_google.png"); + + // Assert + String got = bout.toString().toLowerCase(); + assertThat(got).contains("google"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectLogosTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectLogosTest.java new file mode 100644 index 00000000000..ed3cf7f41ce --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectLogosTest.java @@ -0,0 +1,57 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectLogos; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectLogosTest { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testLogos() throws Exception { + // Act + DetectLogos.detectLogos("./resources/logos.png"); + + // Assert + String got = bout.toString().toLowerCase(); + assertThat(got).contains("google"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectPropertiesGcsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectPropertiesGcsTest.java new file mode 100644 index 00000000000..c0ff61caa05 --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectPropertiesGcsTest.java @@ -0,0 +1,63 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectPropertiesGcs; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectPropertiesGcsTest { + + private static final String ASSET_BUCKET = "cloud-samples-data"; + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testPropertiesGcs() throws Exception { + // Act + DetectPropertiesGcs.detectPropertiesGcs("gs://" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"); + + // Assert + String got = bout.toString(); + assertThat(got).contains("fraction:"); + assertThat(got).contains("r:"); + assertThat(got).contains("g:"); + assertThat(got).contains("b:"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectPropertiesTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectPropertiesTest.java new file mode 100644 index 00000000000..e591bf01128 --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectPropertiesTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectProperties; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectPropertiesTest { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testProperties() throws Exception { + // Act + DetectProperties.detectProperties("./resources/landmark.jpg"); + + // Assert + String got = bout.toString(); + assertThat(got).contains("fraction:"); + assertThat(got).contains("r:"); + assertThat(got).contains("g:"); + assertThat(got).contains("b:"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectSafeSearchGcsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectSafeSearchGcsTest.java new file mode 100644 index 00000000000..6939b7a1c9b --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectSafeSearchGcsTest.java @@ -0,0 +1,61 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectSafeSearchGcs; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectSafeSearchGcsTest { + + private static final String ASSET_BUCKET = "cloud-samples-data"; + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testSafeSearchGcs() throws Exception { + // Act + DetectSafeSearchGcs.detectSafeSearchGcs("gs://" + ASSET_BUCKET + "/vision/label/wakeupcat.jpg"); + + // Assert + String got = bout.toString(); + assertThat(got).contains("adult:"); + assertThat(got).contains("racy:"); + } +} diff --git a/bigquery/datatransfer/cloud-client/src/test/java/com/example/bigquerydatatransfer/QuickstartSampleIT.java b/vision/cloud-client/src/test/java/com/example/vision/DetectSafeSearchTest.java similarity index 76% rename from bigquery/datatransfer/cloud-client/src/test/java/com/example/bigquerydatatransfer/QuickstartSampleIT.java rename to vision/cloud-client/src/test/java/com/example/vision/DetectSafeSearchTest.java index bc0c457c5d2..abdd1d99bf7 100644 --- a/bigquery/datatransfer/cloud-client/src/test/java/com/example/bigquerydatatransfer/QuickstartSampleIT.java +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectSafeSearchTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 Google Inc. + * Copyright 2017 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,10 +14,11 @@ * limitations under the License. */ -package com.example.bigquerydatatransfer; +package com.example.vision; import static com.google.common.truth.Truth.assertThat; +import com.example.vision.snippets.DetectSafeSearch; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import org.junit.After; @@ -26,12 +27,9 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -/** Tests for quickstart sample. */ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") -public class QuickstartSampleIT { - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - +public class DetectSafeSearchTest { private ByteArrayOutputStream bout; private PrintStream out; @@ -48,9 +46,13 @@ public void tearDown() { } @Test - public void testQuickstart() throws Exception { - QuickstartSample.main(PROJECT_ID); + public void testSafeSearch() throws Exception { + // Act + DetectSafeSearch.detectSafeSearch("./resources/wakeupcat.jpg"); + + // Assert String got = bout.toString(); - assertThat(got).contains("Supported Data Sources:"); + assertThat(got).contains("adult:"); + assertThat(got).contains("racy:"); } } diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectTextGcsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectTextGcsTest.java new file mode 100644 index 00000000000..7ec54a14fa8 --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectTextGcsTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectTextGcs; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectTextGcsTest { + + private static final String ASSET_BUCKET = "cloud-samples-data"; + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testTextGcs() throws Exception { + // Act + DetectTextGcs.detectTextGcs("gs://" + ASSET_BUCKET + "/vision/text/screen.jpg"); + + // Assert + String got = bout.toString(); + assertThat(got).contains("37%"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectTextTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectTextTest.java new file mode 100644 index 00000000000..c55c0f4e24e --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectTextTest.java @@ -0,0 +1,57 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectText; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectTextTest { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testText() throws Exception { + // Act + DetectText.detectText("./resources/text.jpg"); + + // Assert + String got = bout.toString(); + assertThat(got).contains("37%"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectWebDetectionsGcsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectWebDetectionsGcsTest.java new file mode 100644 index 00000000000..9829294257a --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectWebDetectionsGcsTest.java @@ -0,0 +1,62 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectWebDetectionsGcs; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectWebDetectionsGcsTest { + + private static final String ASSET_BUCKET = "cloud-samples-data"; + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testDetectWebAnnotationsGcs() throws Exception { + // Act + DetectWebDetectionsGcs.detectWebDetectionsGcs( + "gs://" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"); + + // Assert + String got = bout.toString().toLowerCase(); + assertThat(got).contains("entity:id:score"); + assertThat(got).contains("best guess label"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectWebDetectionsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectWebDetectionsTest.java new file mode 100644 index 00000000000..d0cc10b53d7 --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectWebDetectionsTest.java @@ -0,0 +1,58 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectWebDetections; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectWebDetectionsTest { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void detectWebAnnotations() throws Exception { + // Act + DetectWebDetections.detectWebDetections("./resources/landmark.jpg"); + + // Assert + String got = bout.toString().toLowerCase(); + assertThat(got).contains("entity:id:score"); + assertThat(got).contains("best guess label"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesGcsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesGcsTest.java new file mode 100644 index 00000000000..596451bf79a --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesGcsTest.java @@ -0,0 +1,59 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectWebEntitiesGcs; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectWebEntitiesGcsTest { + + private static final String ASSET_BUCKET = "cloud-samples-data"; + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testDetectWebEntitiesGcs() throws Exception { + // Act + DetectWebEntitiesGcs.detectWebEntitiesGcs("gs://" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"); + + String got = bout.toString().toLowerCase(); + assertThat(got).contains("description"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesIncludeGeoResultsGcsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesIncludeGeoResultsGcsTest.java new file mode 100644 index 00000000000..bedd5bde23f --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesIncludeGeoResultsGcsTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectWebEntitiesIncludeGeoResultsGcs; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectWebEntitiesIncludeGeoResultsGcsTest { + + private static final String ASSET_BUCKET = "cloud-samples-data"; + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testDetectWebEntitiesIncludeGeoResultsGcs() throws Exception { + // Act + DetectWebEntitiesIncludeGeoResultsGcs.detectWebEntitiesIncludeGeoResultsGcs( + "gs://" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"); + + String got = bout.toString().toLowerCase(); + assertThat(got).contains("description"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesIncludeGeoResultsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesIncludeGeoResultsTest.java new file mode 100644 index 00000000000..b455c761d96 --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesIncludeGeoResultsTest.java @@ -0,0 +1,58 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectWebEntitiesIncludeGeoResults; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectWebEntitiesIncludeGeoResultsTest { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testDetectWebEntitiesIncludeGeoResults() throws Exception { + // Act + DetectWebEntitiesIncludeGeoResults.detectWebEntitiesIncludeGeoResults("./resources/city.jpg"); + + // Assert + String got = bout.toString().toLowerCase(); + // Note: entities and labels can change over time. + assertThat(got).doesNotContain("error"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesTest.java new file mode 100644 index 00000000000..f2a37676bdc --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesTest.java @@ -0,0 +1,57 @@ +/* + * Copyright 2017 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.vision; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectWebEntities; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectWebEntitiesTest { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testDetectWebEntities() throws Exception { + // Act + DetectWebEntities.detectWebEntities("./resources/city.jpg"); + + // Assert + String got = bout.toString().toLowerCase(); + assertThat(got).doesNotContain("zepra"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/SetEndpointIT.java b/vision/cloud-client/src/test/java/com/example/vision/SetEndpointIT.java index 9b27ea7d7de..1f648f54494 100644 --- a/vision/cloud-client/src/test/java/com/example/vision/SetEndpointIT.java +++ b/vision/cloud-client/src/test/java/com/example/vision/SetEndpointIT.java @@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat; +import com.example.vision.snippets.SetEndpoint; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; diff --git a/vision/face-detection/pom.xml b/vision/face-detection/pom.xml index 5fa31ffc081..c5664cc7ddc 100644 --- a/vision/face-detection/pom.xml +++ b/vision/face-detection/pom.xml @@ -28,7 +28,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -41,18 +41,12 @@ com.google.apis google-api-services-vision - v1-rev20200405-1.30.9 + v1-rev20200612-1.30.9 - com.google.api-client - google-api-client - 1.30.9 - - - com.google.guava - guava-jdk5 - - + com.google.auth + google-auth-library-oauth2-http + 0.21.0 com.google.guava diff --git a/vision/face-detection/src/main/java/com/google/cloud/vision/samples/facedetect/FaceDetectApp.java b/vision/face-detection/src/main/java/com/google/cloud/vision/samples/facedetect/FaceDetectApp.java index d3bc08e3f1c..9296355e192 100644 --- a/vision/face-detection/src/main/java/com/google/cloud/vision/samples/facedetect/FaceDetectApp.java +++ b/vision/face-detection/src/main/java/com/google/cloud/vision/samples/facedetect/FaceDetectApp.java @@ -17,7 +17,6 @@ package com.google.cloud.vision.samples.facedetect; // [START vision_face_detection_tutorial_imports] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; @@ -31,6 +30,8 @@ import com.google.api.services.vision.v1.model.Feature; import com.google.api.services.vision.v1.model.Image; import com.google.api.services.vision.v1.model.Vertex; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; import com.google.common.collect.ImmutableList; import java.awt.BasicStroke; import java.awt.Color; @@ -85,10 +86,13 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept // [START vision_face_detection_tutorial_client] /** Connects to the Vision API using Application Default Credentials. */ public static Vision getVisionService() throws IOException, GeneralSecurityException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(VisionScopes.all()); + GoogleCredentials credential = + GoogleCredentials.getApplicationDefault().createScoped(VisionScopes.all()); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - return new Vision.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, credential) + return new Vision.Builder( + GoogleNetHttpTransport.newTrustedTransport(), + jsonFactory, + new HttpCredentialsAdapter(credential)) .setApplicationName(APPLICATION_NAME) .build(); } diff --git a/vision/product-search/cloud-client/pom.xml b/vision/product-search/cloud-client/pom.xml index 7d787d1e3d7..5b1831cde00 100644 --- a/vision/product-search/cloud-client/pom.xml +++ b/vision/product-search/cloud-client/pom.xml @@ -26,7 +26,7 @@ com.google.cloud.samples shared-configuration - 1.0.17 + 1.0.18 @@ -43,7 +43,7 @@ com.google.cloud libraries-bom - 5.4.0 + 8.0.0 pom import @@ -101,7 +101,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 @@ -129,7 +129,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 @@ -157,7 +157,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 @@ -185,7 +185,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 @@ -213,7 +213,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 @@ -241,7 +241,7 @@ org.codehaus.mojo exec-maven-plugin - 1.6.0 + 3.0.0 diff --git a/vision/spring-framework/pom.xml b/vision/spring-framework/pom.xml index 607eaebac0a..5529f1e69f3 100644 --- a/vision/spring-framework/pom.xml +++ b/vision/spring-framework/pom.xml @@ -19,7 +19,7 @@ limitations under the License. 1.8 1.8 - 2.3.0.RELEASE + 2.3.1.RELEASE