diff --git a/dlp/pom.xml b/dlp/pom.xml
index 4c533803dfb..654bfc88610 100644
--- a/dlp/pom.xml
+++ b/dlp/pom.xml
@@ -29,14 +29,12 @@
com.google.cloud.samples
shared-configuration
- 1.0.9
-
+ 1.0.10
1.8
1.8
- 0.7.1
UTF-8
diff --git a/dlp/src/main/java/com/example/dlp/Inspect.java b/dlp/src/main/java/com/example/dlp/Inspect.java
index 8defeb657c7..7c1ab137178 100644
--- a/dlp/src/main/java/com/example/dlp/Inspect.java
+++ b/dlp/src/main/java/com/example/dlp/Inspect.java
@@ -72,165 +72,6 @@
public class Inspect {
- /**
- * [START dlp_inspect_string] Inspect a text for given InfoTypes
- *
- * @param string String to instpect
- * @param minLikelihood The minimum likelihood required before returning a match
- * @param maxFindings The maximum number of findings to report (0 = server maximum)
- * @param infoTypes The infoTypes of information to match
- * @param includeQuote Whether to include the matching string
- * @param projectId Google Cloud project ID
- */
- private static void inspectString(
- String string,
- Likelihood minLikelihood,
- int maxFindings,
- List infoTypes,
- List customInfoTypes,
- boolean includeQuote,
- String projectId) {
- // instantiate a client
- try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
- FindingLimits findingLimits =
- FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
- InspectConfig inspectConfig =
- InspectConfig.newBuilder()
- .addAllInfoTypes(infoTypes)
- .addAllCustomInfoTypes(customInfoTypes)
- .setMinLikelihood(minLikelihood)
- .setLimits(findingLimits)
- .setIncludeQuote(includeQuote)
- .build();
-
- ByteContentItem byteContentItem =
- ByteContentItem.newBuilder()
- .setType(ByteContentItem.BytesType.TEXT_UTF8)
- .setData(ByteString.copyFromUtf8(string))
- .build();
-
- ContentItem contentItem = ContentItem.newBuilder().setByteItem(byteContentItem).build();
-
- InspectContentRequest request =
- InspectContentRequest.newBuilder()
- .setParent(ProjectName.of(projectId).toString())
- .setInspectConfig(inspectConfig)
- .setItem(contentItem)
- .build();
- InspectContentResponse response = dlpServiceClient.inspectContent(request);
-
- if (response.getResult().getFindingsCount() > 0) {
- System.out.println("Findings: ");
- for (Finding finding : response.getResult().getFindingsList()) {
- if (includeQuote) {
- System.out.print("\tQuote: " + finding.getQuote());
- }
- System.out.print("\tInfo type: " + finding.getInfoType().getName());
- System.out.println("\tLikelihood: " + finding.getLikelihood());
- }
- } else {
- System.out.println("No findings.");
- }
- } catch (Exception e) {
- System.out.println("Error in inspectString: " + e.getMessage());
- }
- }
- // [END dlp_inspect_string]
-
- // [START dlp_inspect_file]
- /**
- * Inspect a local file
- *
- * @param filePath The path to a local file to inspect. Can be a text, JPG, or PNG file.
- * @param minLikelihood The minimum likelihood required before returning a match
- * @param maxFindings The maximum number of findings to report (0 = server maximum)
- * @param infoTypes The infoTypes of information to match
- * @param includeQuote Whether to include the matching string
- * @param projectId Google Cloud project ID
- */
- private static void inspectFile(
- String filePath,
- Likelihood minLikelihood,
- int maxFindings,
- List infoTypes,
- List customInfoTypes,
- boolean includeQuote,
- String projectId) {
- // Instantiates a client
- try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
- // detect file mime type, default to application/octet-stream
- String mimeType = URLConnection.guessContentTypeFromName(filePath);
- if (mimeType == null) {
- mimeType = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(filePath);
- }
-
- ByteContentItem.BytesType bytesType;
- switch (mimeType) {
- case "image/jpeg":
- bytesType = ByteContentItem.BytesType.IMAGE_JPEG;
- break;
- case "image/bmp":
- bytesType = ByteContentItem.BytesType.IMAGE_BMP;
- break;
- case "image/png":
- bytesType = ByteContentItem.BytesType.IMAGE_PNG;
- break;
- case "image/svg":
- bytesType = ByteContentItem.BytesType.IMAGE_SVG;
- break;
- default:
- bytesType = ByteContentItem.BytesType.BYTES_TYPE_UNSPECIFIED;
- break;
- }
-
- byte[] data = Files.readAllBytes(Paths.get(filePath));
- ByteContentItem byteContentItem =
- ByteContentItem.newBuilder()
- .setType(bytesType)
- .setData(ByteString.copyFrom(data))
- .build();
- ContentItem contentItem = ContentItem.newBuilder().setByteItem(byteContentItem).build();
-
- FindingLimits findingLimits =
- FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
-
- InspectConfig inspectConfig =
- InspectConfig.newBuilder()
- .addAllInfoTypes(infoTypes)
- .addAllCustomInfoTypes(customInfoTypes)
- .setMinLikelihood(minLikelihood)
- .setLimits(findingLimits)
- .setIncludeQuote(includeQuote)
- .build();
-
- InspectContentRequest request =
- InspectContentRequest.newBuilder()
- .setParent(ProjectName.of(projectId).toString())
- .setInspectConfig(inspectConfig)
- .setItem(contentItem)
- .build();
-
- InspectContentResponse response = dlpServiceClient.inspectContent(request);
-
- InspectResult result = response.getResult();
- if (result.getFindingsCount() > 0) {
- System.out.println("Findings: ");
- for (Finding finding : result.getFindingsList()) {
- if (includeQuote) {
- System.out.print("\tQuote: " + finding.getQuote());
- }
- System.out.print("\tInfo type: " + finding.getInfoType().getName());
- System.out.println("\tLikelihood: " + finding.getLikelihood());
- }
- } else {
- System.out.println("No findings.");
- }
- } catch (Exception e) {
- System.out.println("Error in inspectFile: " + e.getMessage());
- }
- }
- // [END dlp_inspect_file]
-
// [START dlp_inspect_gcs]
/**
* Inspect GCS file for Info types and wait on job completion using Google Cloud Pub/Sub
@@ -756,28 +597,7 @@ public static void main(String[] args) throws Exception {
}
// string inspection
- if (cmd.hasOption("s")) {
- String val = cmd.getOptionValue(stringOption.getOpt());
- inspectString(
- val,
- minLikelihood,
- maxFindings,
- infoTypesList,
- customInfoTypesList,
- includeQuote,
- projectId);
- } else if (cmd.hasOption("f")) {
- String filePath = cmd.getOptionValue(fileOption.getOpt());
- inspectFile(
- filePath,
- minLikelihood,
- maxFindings,
- infoTypesList,
- customInfoTypesList,
- includeQuote,
- projectId);
- // gcs file inspection
- } else if (cmd.hasOption("gcs")) {
+ if (cmd.hasOption("gcs")) {
String bucketName = cmd.getOptionValue(bucketNameOption.getOpt());
String fileName = cmd.getOptionValue(gcsFileNameOption.getOpt());
inspectGcsFile(
diff --git a/dlp/src/main/java/com/example/dlp/Redact.java b/dlp/src/main/java/com/example/dlp/Redact.java
deleted file mode 100644
index 1ffd49fc90c..00000000000
--- a/dlp/src/main/java/com/example/dlp/Redact.java
+++ /dev/null
@@ -1,189 +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.dlp;
-
-import com.google.cloud.ServiceOptions;
-import com.google.cloud.dlp.v2.DlpServiceClient;
-import com.google.privacy.dlp.v2.ByteContentItem;
-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.RedactImageRequest;
-import com.google.privacy.dlp.v2.RedactImageResponse;
-import com.google.protobuf.ByteString;
-import java.io.FileOutputStream;
-import java.net.URLConnection;
-import java.nio.file.Files;
-import java.nio.file.Paths;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.stream.Collectors;
-import javax.activation.MimetypesFileTypeMap;
-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.Options;
-import org.apache.commons.cli.ParseException;
-
-public class Redact {
-
- // [START dlp_redact_image]
- /*
- * Redact sensitive data from an image using the Data Loss Prevention API.
- *
- * @param filePath The path to a local file to inspect. Can be a JPG or PNG image file.
- * @param minLikelihood The minimum likelihood required before redacting a match.
- * @param infoTypes The infoTypes of information to redact.
- * @param outputPath The local path to save the resulting image to.
- * @param projectId The project ID to run the API call under.
- */
- private static void redactImage(
- String filePath,
- Likelihood minLikelihood,
- List infoTypes,
- String outputPath,
- String projectId)
- throws Exception {
-
- // Instantiate the DLP client
- try (DlpServiceClient dlpClient = DlpServiceClient.create()) {
- String mimeType = URLConnection.guessContentTypeFromName(filePath);
- if (mimeType == null) {
- mimeType = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(filePath);
- }
-
- ByteContentItem.BytesType bytesType;
- switch (mimeType) {
- case "image/jpeg":
- bytesType = ByteContentItem.BytesType.IMAGE_JPEG;
- break;
- case "image/bmp":
- bytesType = ByteContentItem.BytesType.IMAGE_BMP;
- break;
- case "image/png":
- bytesType = ByteContentItem.BytesType.IMAGE_PNG;
- break;
- case "image/svg":
- bytesType = ByteContentItem.BytesType.IMAGE_SVG;
- break;
- default:
- bytesType = ByteContentItem.BytesType.BYTES_TYPE_UNSPECIFIED;
- break;
- }
-
- byte[] data = Files.readAllBytes(Paths.get(filePath));
-
- InspectConfig inspectConfig =
- InspectConfig.newBuilder()
- .addAllInfoTypes(infoTypes)
- .setMinLikelihood(minLikelihood)
- .build();
-
- ByteContentItem byteContentItem =
- ByteContentItem.newBuilder()
- .setType(bytesType)
- .setData(ByteString.copyFrom(data))
- .build();
-
- List imageRedactionConfigs =
- infoTypes
- .stream()
- .map(
- infoType ->
- RedactImageRequest.ImageRedactionConfig.newBuilder()
- .setInfoType(infoType)
- .build())
- .collect(Collectors.toList());
-
- RedactImageRequest redactImageRequest =
- RedactImageRequest.newBuilder()
- .setParent(ProjectName.of(projectId).toString())
- .addAllImageRedactionConfigs(imageRedactionConfigs)
- .setByteItem(byteContentItem)
- .setInspectConfig(inspectConfig)
- .build();
-
- RedactImageResponse redactImageResponse = dlpClient.redactImage(redactImageRequest);
-
- // redacted image data
- ByteString redactedImageData = redactImageResponse.getRedactedImage();
- FileOutputStream outputStream = new FileOutputStream(outputPath);
- outputStream.write(redactedImageData.toByteArray());
- outputStream.close();
- }
- }
- // [END dlp_redact_image]
-
- /** Command line application to redact strings, images using the Data Loss Prevention API. */
- public static void main(String[] args) throws Exception {
-
- Options commandLineOptions = new Options();
-
- Option minLikelihoodOption =
- Option.builder("minLikelihood").hasArg(true).required(false).build();
-
- commandLineOptions.addOption(minLikelihoodOption);
-
- Option infoTypesOption = Option.builder("infoTypes").hasArg(true).required(false).build();
- infoTypesOption.setArgs(Option.UNLIMITED_VALUES);
- commandLineOptions.addOption(infoTypesOption);
-
- Option inputFilePathOption =
- Option.builder("f").hasArg(true).longOpt("inputFilePath").required(false).build();
- commandLineOptions.addOption(inputFilePathOption);
-
- Option outputFilePathOption =
- Option.builder("o").hasArg(true).longOpt("outputFilePath").required(false).build();
-
- commandLineOptions.addOption(outputFilePathOption);
-
- Option projectIdOption = Option.builder("projectId").hasArg(true).required(false).build();
- CommandLineParser parser = new DefaultParser();
- HelpFormatter formatter = new HelpFormatter();
- CommandLine cmd;
-
- try {
- cmd = parser.parse(commandLineOptions, args);
- } catch (ParseException e) {
- System.out.println(e.getMessage());
- formatter.printHelp(Redact.class.getName(), commandLineOptions);
- System.exit(1);
- return;
- }
-
- List infoTypesList = new ArrayList<>();
- String[] infoTypes = cmd.getOptionValues(infoTypesOption.getOpt());
- if (infoTypes != null) {
- for (String infoType : infoTypes) {
- infoTypesList.add(InfoType.newBuilder().setName(infoType).build());
- }
- }
- Likelihood minLikelihood =
- Likelihood.valueOf(
- cmd.getOptionValue(
- minLikelihoodOption.getOpt(), Likelihood.LIKELIHOOD_UNSPECIFIED.name()));
-
- String inputFilePath = cmd.getOptionValue(inputFilePathOption.getOpt());
- String outputFilePath = cmd.getOptionValue(outputFilePathOption.getOpt());
- String projectId =
- cmd.getOptionValue(projectIdOption.getOpt(), ServiceOptions.getDefaultProjectId());
- redactImage(inputFilePath, minLikelihood, infoTypesList, outputFilePath, projectId);
- }
-}
diff --git a/dlp/src/main/java/com/example/dlp/Templates.java b/dlp/src/main/java/com/example/dlp/Templates.java
index da4c5eb88c6..72e2cb6cf6d 100644
--- a/dlp/src/main/java/com/example/dlp/Templates.java
+++ b/dlp/src/main/java/com/example/dlp/Templates.java
@@ -218,7 +218,7 @@ public static void main(String[] args) throws Exception {
cmd = parser.parse(commandLineOptions, args);
} catch (ParseException e) {
System.out.println(e.getMessage());
- formatter.printHelp(Redact.class.getName(), commandLineOptions);
+ formatter.printHelp("dlp_snippets", commandLineOptions);
System.exit(1);
return;
}
diff --git a/dlp/src/main/java/dlp/snippets/InspectImageFile.java b/dlp/src/main/java/dlp/snippets/InspectImageFile.java
new file mode 100644
index 00000000000..7c8dd6d0bd5
--- /dev/null
+++ b/dlp/src/main/java/dlp/snippets/InspectImageFile.java
@@ -0,0 +1,94 @@
+/*
+ * 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_image_file]
+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.ProjectName;
+import com.google.protobuf.ByteString;
+import java.io.FileInputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+public class InspectImageFile {
+
+ // Inspects the specified image file.
+ public static void inspectImageFile(String projectId, String filePath) {
+ // String projectId = "my-project-id";
+ // String filePath = "path/to/image.png";
+
+ // 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 = ByteContentItem.newBuilder()
+ .setType(BytesType.IMAGE)
+ .setData(fileBytes)
+ .build();
+ ContentItem item = ContentItem.newBuilder()
+ .setByteItem(byteItem)
+ .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[] {"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();
+
+ // Construct the Inspect request to be sent by the client.
+ InspectContentRequest request = InspectContentRequest.newBuilder()
+ .setParent(project.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 inspectImageFile: \n" + e.toString());
+ }
+ }
+}
+// [END dlp_inspect_image_file]
diff --git a/dlp/src/main/java/dlp/snippets/InspectString.java b/dlp/src/main/java/dlp/snippets/InspectString.java
new file mode 100644
index 00000000000..a6a178dd529
--- /dev/null
+++ b/dlp/src/main/java/dlp/snippets/InspectString.java
@@ -0,0 +1,90 @@
+/*
+ * 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]
+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.ProjectName;
+import com.google.protobuf.ByteString;
+import java.util.ArrayList;
+import java.util.List;
+
+public class InspectString {
+
+ // Inspects the provided text.
+ public static void inspectString(String projectId, String textToInspect) {
+ // String projectId = "my-project-id";
+ // String textToInspect = "My name is Gary and my email is gary@example.com";
+
+ // 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()
+ .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.
+ 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"}) {
+ infoTypes.add(InfoType.newBuilder().setName(typeName).build());
+ }
+
+ // Construct the configuration for the Inspect request.
+ InspectConfig config = InspectConfig.newBuilder()
+ .addAllInfoTypes(infoTypes)
+ .setIncludeQuote(true)
+ .build();
+
+ // Construct the Inspect request to be sent by the client.
+ InspectContentRequest request = InspectContentRequest.newBuilder()
+ .setParent(project.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_string]
diff --git a/dlp/src/main/java/dlp/snippets/InspectTextFile.java b/dlp/src/main/java/dlp/snippets/InspectTextFile.java
new file mode 100644
index 00000000000..335e2fcbe47
--- /dev/null
+++ b/dlp/src/main/java/dlp/snippets/InspectTextFile.java
@@ -0,0 +1,94 @@
+/*
+ * 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_file]
+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.ProjectName;
+import com.google.protobuf.ByteString;
+import java.io.FileInputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+public class InspectTextFile {
+
+ // Inspects the specified text file.
+ public static void inspectTextFile(String projectId, String filePath) {
+ // String projectId = "my-project-id";
+ // String filePath = "path/to/image.png";
+
+ // 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 = ByteContentItem.newBuilder()
+ .setType(BytesType.TEXT_UTF8)
+ .setData(fileBytes)
+ .build();
+ ContentItem item = ContentItem.newBuilder()
+ .setByteItem(byteItem)
+ .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[] {"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();
+
+ // Construct the Inspect request to be sent by the client.
+ InspectContentRequest request = InspectContentRequest.newBuilder()
+ .setParent(project.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 inspectFile: \n" + e.toString());
+ }
+ }
+}
+// [END dlp_inspect_file]
diff --git a/dlp/src/main/java/dlp/snippets/RedactImageFile.java b/dlp/src/main/java/dlp/snippets/RedactImageFile.java
new file mode 100644
index 00000000000..bbf4bc53184
--- /dev/null
+++ b/dlp/src/main/java/dlp/snippets/RedactImageFile.java
@@ -0,0 +1,88 @@
+/*
+ * 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_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.RedactImageRequest;
+import com.google.privacy.dlp.v2.RedactImageResponse;
+import com.google.protobuf.ByteString;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+class RedactImageFile {
+
+ static void redactImageFile(String projectId, String filePath) {
+ // String projectId = "my-project-id";
+ // String filePath = "path/to/image.png";
+
+ // 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));
+ ByteContentItem byteItem =
+ ByteContentItem.newBuilder().setType(BytesType.IMAGE).setData(fileBytes).build();
+
+ // Specify the type of info and likelihood 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[] {"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) {
+ infoTypes.add(InfoType.newBuilder().setName(typeName).build());
+ }
+ InspectConfig config =
+ InspectConfig.newBuilder()
+ .addAllInfoTypes(infoTypes)
+ .setMinLikelihood(Likelihood.LIKELY)
+ .build();
+
+ // Construct the Redact request to be sent by the client.
+ RedactImageRequest request =
+ RedactImageRequest.newBuilder()
+ .setParent(project.toString())
+ .setByteItem(byteItem)
+ .setInspectConfig(config)
+ .build();
+
+ // Use the client to send the API request.
+ 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());
+ }
+ }
+}
+// [END dlp_redact_image]
diff --git a/dlp/src/test/java/com/example/dlp/InspectIT.java b/dlp/src/test/java/com/example/dlp/InspectIT.java
index 17814cfc395..ae66bacf5d6 100644
--- a/dlp/src/test/java/com/example/dlp/InspectIT.java
+++ b/dlp/src/test/java/com/example/dlp/InspectIT.java
@@ -53,74 +53,6 @@ public void setUp() {
assertNotNull(System.getenv("GOOGLE_APPLICATION_CREDENTIALS"));
}
- @Test
- public void testStringInspectionReturnsInfoTypes() throws Exception {
- String text =
- "\"My phone number is (234) 456-7890 and my email address is gary@somedomain.com\"";
- Inspect.main(new String[] {"-s", text, "-infoTypes", "PHONE_NUMBER", "EMAIL_ADDRESS"});
- String output = bout.toString();
-
- assertThat(output, containsString("PHONE_NUMBER"));
- assertThat(output, containsString("EMAIL_ADDRESS"));
- }
-
- @Test
- public void testStringInspectionReturnsCustomInfoTypes() throws Exception {
- String text =
- "\"My phone number is (234) 456-7890 and my email address is gary@somedomain.com\"";
- Inspect.main(
- new String[] {
- "-s",
- text,
- "-customDictionaries",
- "gary@somedomain.com",
- "-customRegexes",
- "\\(\\d{3}\\) \\d{3}-\\d{4}"
- });
- String output = bout.toString();
-
- assertThat(output, containsString("CUSTOM_DICTIONARY_0"));
- assertThat(output, containsString("CUSTOM_REGEX_0"));
- }
-
- @Test
- public void testTextFileInspectionReturnsInfoTypes() throws Exception {
- Inspect.main(
- new String[] {
- "-f", "src/test/resources/test.txt", "-infoTypes", "PHONE_NUMBER", "EMAIL_ADDRESS"
- });
- String output = bout.toString();
- assertThat(output, containsString("PHONE_NUMBER"));
- assertThat(output, containsString("EMAIL_ADDRESS"));
- }
-
- @Test
- public void testTextFileInspectionReturnsCustomInfoTypes() throws Exception {
- Inspect.main(
- new String[] {
- "-f",
- "src/test/resources/test.txt",
- "-customDictionaries",
- "gary@somedomain.com",
- "-customRegexes",
- "\\(\\d{3}\\) \\d{3}-\\d{4}"
- });
- String output = bout.toString();
- assertThat(output, containsString("CUSTOM_DICTIONARY_0"));
- assertThat(output, containsString("CUSTOM_REGEX_0"));
- }
-
- @Test
- public void testImageFileInspectionReturnsInfoTypes() throws Exception {
- Inspect.main(
- new String[] {
- "-f", "src/test/resources/test.png", "-infoTypes", "PHONE_NUMBER", "EMAIL_ADDRESS"
- });
- String output = bout.toString();
- assertThat(output, containsString("PHONE_NUMBER"));
- assertThat(output, containsString("EMAIL_ADDRESS"));
- }
-
// Requires that bucket by the specified name exists
@Test
public void testGcsFileInspectionReturnsInfoTypes() throws Exception {
diff --git a/dlp/src/test/java/com/example/dlp/RedactIT.java b/dlp/src/test/java/com/example/dlp/RedactIT.java
deleted file mode 100644
index 5708fae6211..00000000000
--- a/dlp/src/test/java/com/example/dlp/RedactIT.java
+++ /dev/null
@@ -1,75 +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.dlp;
-
-import static org.hamcrest.CoreMatchers.containsString;
-import static org.hamcrest.CoreMatchers.not;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertThat;
-
-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)
-// CHECKSTYLE OFF: AbbreviationAsWordInName
-public class RedactIT {
-
- // CHECKSTYLE ON: AbbreviationAsWordInName
- private ByteArrayOutputStream bout;
- private PrintStream out;
-
- @Before
- public void setUp() {
- bout = new ByteArrayOutputStream();
- out = new PrintStream(bout);
- System.setOut(out);
- assertNotNull(System.getenv("GOOGLE_APPLICATION_CREDENTIALS"));
- }
-
- @Test
- public void testRedactImage() throws Exception {
- // InspectIT Tests verify original has PII present
- String outputFilePath = "src/test/resources/output.png";
-
- // Restrict phone number, but not email
- Redact.main(
- new String[] {
- "-f", "src/test/resources/test.png",
- "-infoTypes", "PHONE_NUMBER",
- "-o", outputFilePath
- });
- bout.reset();
-
- // Verify that phone_number is missing but email is present
- Inspect.main(
- new String[] {"-f", outputFilePath, "-infoTypes", "PHONE_NUMBER", "EMAIL_ADDRESS"});
- String output = bout.toString();
- assertThat(output, not(containsString("PHONE_NUMBER")));
- assertThat(output, containsString("EMAIL_ADDRESS"));
- }
-
- @After
- public void tearDown() {
- System.setOut(null);
- bout.reset();
- }
-}
diff --git a/dlp/src/test/java/dlp/snippets/InspectTests.java b/dlp/src/test/java/dlp/snippets/InspectTests.java
new file mode 100644
index 00000000000..6c64a19b9c5
--- /dev/null
+++ b/dlp/src/test/java/dlp/snippets/InspectTests.java
@@ -0,0 +1,91 @@
+/*
+ * 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;
+
+import static junit.framework.TestCase.assertNotNull;
+import static org.junit.Assert.assertThat;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+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 InspectTests {
+
+ private ByteArrayOutputStream bout;
+
+ private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
+
+ private static void requireEnvVar(String varName) {
+ assertNotNull(
+ System.getenv(varName),
+ "Environment variable '%s' is required to perform these tests.".format(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();
+ }
+
+ @Test
+ public void testInspectString() {
+ InspectString.inspectString(PROJECT_ID, "I'm Gary and my email is gary@example.com");
+
+ String output = bout.toString();
+ assertThat(output, CoreMatchers.containsString("Info type: EMAIL_ADDRESS"));
+ }
+
+ @Test
+ public void textInspectTestFile() {
+ InspectTextFile.inspectTextFile(PROJECT_ID, "src/test/resources/test.txt");
+
+ String output = bout.toString();
+ assertThat(output, CoreMatchers.containsString("Info type: PHONE_NUMBER"));
+ assertThat(output, CoreMatchers.containsString("Info type: EMAIL_ADDRESS"));
+ }
+
+
+ @Test
+ public void testInspectImageFile() {
+ InspectImageFile.inspectImageFile(PROJECT_ID, "src/test/resources/test.png");
+
+ String output = bout.toString();
+ assertThat(output, CoreMatchers.containsString("Info type: PHONE_NUMBER"));
+ assertThat(output, CoreMatchers.containsString("Info type: EMAIL_ADDRESS"));
+ }
+
+}
diff --git a/dlp/src/test/java/dlp/snippets/RedactTests.java b/dlp/src/test/java/dlp/snippets/RedactTests.java
new file mode 100644
index 00000000000..475b9485a8d
--- /dev/null
+++ b/dlp/src/test/java/dlp/snippets/RedactTests.java
@@ -0,0 +1,70 @@
+/*
+ * 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;
+
+import static junit.framework.TestCase.assertNotNull;
+import static org.junit.Assert.assertThat;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+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 RedactTests {
+
+ private ByteArrayOutputStream bout;
+
+ private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
+
+ private static void requireEnvVar(String varName) {
+ assertNotNull(
+ System.getenv(varName),
+ "Environment variable '%s' is required to perform these tests.".format(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();
+ }
+
+ @Test
+ public void testRedactImage() {
+ RedactImageFile.redactImageFile(PROJECT_ID, "src/test/resources/test.png");
+
+ String output = bout.toString();
+ assertThat(output, CoreMatchers.containsString("Redacted image written"));
+ }
+}
diff --git a/dlp/src/test/resources/test.png b/dlp/src/test/resources/test.png
index 8f32c825884..748f46cdcb5 100644
Binary files a/dlp/src/test/resources/test.png and b/dlp/src/test/resources/test.png differ
diff --git a/dlp/src/test/resources/test.txt b/dlp/src/test/resources/test.txt
index c2ee3815bc9..f30af240c72 100644
--- a/dlp/src/test/resources/test.txt
+++ b/dlp/src/test/resources/test.txt
@@ -1 +1 @@
-My phone number is (223) 456-7890 and my email address is gary@somedomain.com.
\ No newline at end of file
+My phone number is (223) 456-7890 and my email address is gary@example.com.
\ No newline at end of file