diff --git a/dlp/pom.xml b/dlp/pom.xml
index 9be90886dc1..98085dfb5e2 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/dlp.snippets/InspectFile.java b/dlp/src/main/java/dlp.snippets/InspectFile.java
new file mode 100644
index 00000000000..88735b54273
--- /dev/null
+++ b/dlp/src/main/java/dlp.snippets/InspectFile.java
@@ -0,0 +1,91 @@
+/*
+ * Copyright 2018 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 InspectFile {
+
+ // Inspects the specified file.
+ public static void inspectFile(String projectId, String filePath, String fileType) {
+ // String projectId = "my-project-id";
+ // String filePath = "path/to/image.png";
+ // String fileType = "IMAGE"
+
+ // Initialize client with try-with-resources for automatic cleanup of background resources
+ try (DlpServiceClient dlp = DlpServiceClient.create()) {
+ // Set project for request
+ ProjectName project = ProjectName.of(projectId);
+
+ // Set content for request
+ ByteString fileBytes = ByteString.readFrom(new FileInputStream(filePath));
+ ByteContentItem byteItem = ByteContentItem.newBuilder()
+ .setType(BytesType.valueOf(fileType))
+ .setData(fileBytes)
+ .build();
+ ContentItem item = ContentItem.newBuilder()
+ .setByteItem(byteItem)
+ .build();
+
+ // Set required InfoTypes for inspection config
+ 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());
+ }
+
+ // Set the inspect configuration for request
+ InspectConfig config = InspectConfig.newBuilder()
+ .addAllInfoTypes(infoTypes)
+ .setIncludeQuote(true)
+ .build();
+
+ // Construct request
+ InspectContentRequest request = InspectContentRequest.newBuilder()
+ .setParent(project.toString())
+ .setItem(item)
+ .setInspectConfig(config)
+ .build();
+
+ // Run request and parse response
+ InspectContentResponse response = dlp.inspectContent(request);
+ 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());
+ System.out.println("\tLikelihood: " + f.getLikelihood());
+ }
+ } catch (Exception e) {
+ System.out.println("Error during inspectFile: \n" + e.toString());
+ }
+ }
+}
+// [START dlp_inspect_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..f2fb1a70ece
--- /dev/null
+++ b/dlp/src/main/java/dlp.snippets/InspectString.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2018 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 with try-with-resources for automatic cleanup of background resources
+ try (DlpServiceClient dlp = DlpServiceClient.create()) {
+ // Set project for request
+ ProjectName project = ProjectName.of(projectId);
+
+ // Set content for request
+ ByteContentItem byteItem = ByteContentItem.newBuilder()
+ .setType(BytesType.TEXT_UTF8)
+ .setData(ByteString.copyFromUtf8(textToInspect))
+ .build();
+ ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();
+
+ // Set required InfoTypes for inspection config
+ 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());
+ }
+
+ // Set the inspect configuration for request
+ InspectConfig config = InspectConfig.newBuilder()
+ .addAllInfoTypes(infoTypes)
+ .setIncludeQuote(true)
+ .build();
+
+ // Construct request
+ InspectContentRequest request = InspectContentRequest.newBuilder()
+ .setParent(project.toString())
+ .setItem(item)
+ .setInspectConfig(config)
+ .build();
+
+ // Run request and parse response
+ InspectContentResponse response = dlp.inspectContent(request);
+ 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());
+ 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/test/java/dlp/snippets/InspectTests.java b/dlp/src/test/java/dlp/snippets/InspectTests.java
new file mode 100644
index 00000000000..94aed84958d
--- /dev/null
+++ b/dlp/src/test/java/dlp/snippets/InspectTests.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2018 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: PHONE_NUMBER"));
+ assertThat(output, CoreMatchers.containsString("Info type: EMAIL_ADDRESS"));
+ }
+
+ @Test
+ public void testInspectFile() {
+ InspectFile.inspectFile(PROJECT_ID, "src/test/resources/test.png", "IMAGE");
+
+ String output = bout.toString();
+ assertThat(output, CoreMatchers.containsString("Info type: PHONE_NUMBER"));
+ assertThat(output, CoreMatchers.containsString("Info type: EMAIL_ADDRESS"));
+ }
+
+}