Skip to content

Commit b604f7f

Browse files
authored
Create Java code sample for omit names that overlap with custom detector (GoogleCloudPlatform#3064)
Fixes internal bug b/156971892 - [X] I have followed [Sample Format Guide](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/SAMPLE_FORMAT.md) - [X] `pom.xml` parent set to latest `shared-configuration` - [X] Appropriate changes to README are included in PR - [X] API's need to be enabled to test (tell us) (**Nothing new**) - [X] Environment Variables need to be set (ask us to set them) (**Nothing new**) - [X] Tests pass (`mvn -P lint clean verify`) * (Note- `Checkstyle` passing is required; `Spotbugs`, `ErrorProne`, `PMD`, etc. `ERROR`'s are advisory only) - [X] Please **merge** this PR for me once it is approved.
1 parent 786d733 commit b604f7f

File tree

2 files changed

+126
-3
lines changed

2 files changed

+126
-3
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dlp.snippets;
18+
19+
// [START dlp_inspect_string_custom_omit_overlap]
20+
21+
import com.google.cloud.dlp.v2.DlpServiceClient;
22+
import com.google.privacy.dlp.v2.ByteContentItem;
23+
import com.google.privacy.dlp.v2.ByteContentItem.BytesType;
24+
import com.google.privacy.dlp.v2.ContentItem;
25+
import com.google.privacy.dlp.v2.CustomInfoType;
26+
import com.google.privacy.dlp.v2.CustomInfoType.ExclusionType;
27+
import com.google.privacy.dlp.v2.CustomInfoType.Regex;
28+
import com.google.privacy.dlp.v2.ExcludeInfoTypes;
29+
import com.google.privacy.dlp.v2.ExclusionRule;
30+
import com.google.privacy.dlp.v2.Finding;
31+
import com.google.privacy.dlp.v2.InfoType;
32+
import com.google.privacy.dlp.v2.InspectConfig;
33+
import com.google.privacy.dlp.v2.InspectContentRequest;
34+
import com.google.privacy.dlp.v2.InspectContentResponse;
35+
import com.google.privacy.dlp.v2.InspectionRule;
36+
import com.google.privacy.dlp.v2.InspectionRuleSet;
37+
import com.google.privacy.dlp.v2.LocationName;
38+
import com.google.privacy.dlp.v2.MatchingType;
39+
import com.google.protobuf.ByteString;
40+
import java.io.IOException;
41+
42+
public class InspectStringCustomOmitOverlap {
43+
44+
public static void inspectStringCustomOmitOverlap() throws IOException {
45+
// TODO(developer): Replace these variables before running the sample.
46+
String projectId = "your-project-id";
47+
String textToInspect = "Name: Jane Doe. Name: Larry Page.";
48+
inspectStringCustomOmitOverlap(projectId, textToInspect);
49+
}
50+
51+
// Inspects the provided text, avoiding matches specified in the exclusion list.
52+
public static void inspectStringCustomOmitOverlap(String projectId, String textToInspect)
53+
throws IOException {
54+
// Initialize client that will be used to send requests. This client only needs to be created
55+
// once, and can be reused for multiple requests. After completing all of your requests, call
56+
// the "close" method on the client to safely clean up any remaining background resources.
57+
try (DlpServiceClient dlp = DlpServiceClient.create()) {
58+
// Specify the type and content to be inspected.
59+
ByteContentItem byteItem =
60+
ByteContentItem.newBuilder()
61+
.setType(BytesType.TEXT_UTF8)
62+
.setData(ByteString.copyFromUtf8(textToInspect))
63+
.build();
64+
ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();
65+
66+
// Construct the custom infotype.
67+
CustomInfoType customInfoType = CustomInfoType.newBuilder()
68+
.setInfoType(InfoType.newBuilder().setName("VIP_DETECTOR"))
69+
.setRegex(Regex.newBuilder().setPattern("Larry Page|Sergey Brin"))
70+
.setExclusionType(ExclusionType.EXCLUSION_TYPE_EXCLUDE)
71+
.build();
72+
73+
// Exclude matches that also match the custom infotype.
74+
ExclusionRule exclusionRule = ExclusionRule.newBuilder()
75+
.setExcludeInfoTypes(
76+
ExcludeInfoTypes.newBuilder().addInfoTypes(customInfoType.getInfoType()))
77+
.setMatchingType(MatchingType.MATCHING_TYPE_FULL_MATCH)
78+
.build();
79+
80+
// Construct a ruleset that applies the exclusion rule to the PERSON_NAME infotype.
81+
InspectionRuleSet ruleSet = InspectionRuleSet.newBuilder()
82+
.addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME"))
83+
.addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule))
84+
.build();
85+
86+
// Construct the configuration for the Inspect request, including the ruleset.
87+
InspectConfig config =
88+
InspectConfig.newBuilder()
89+
.addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME"))
90+
.addCustomInfoTypes(customInfoType)
91+
.setIncludeQuote(true)
92+
.addRuleSet(ruleSet)
93+
.build();
94+
95+
// Construct the Inspect request to be sent by the client.
96+
InspectContentRequest request =
97+
InspectContentRequest.newBuilder()
98+
.setParent(LocationName.of(projectId, "global").toString())
99+
.setItem(item)
100+
.setInspectConfig(config)
101+
.build();
102+
103+
// Use the client to send the API request.
104+
InspectContentResponse response = dlp.inspectContent(request);
105+
106+
// Parse the response and process results
107+
System.out.println("Findings: " + response.getResult().getFindingsCount());
108+
for (Finding f : response.getResult().getFindingsList()) {
109+
System.out.println("\tQuote: " + f.getQuote());
110+
System.out.println("\tInfo type: " + f.getInfoType().getName());
111+
System.out.println("\tLikelihood: " + f.getLikelihood());
112+
}
113+
}
114+
}
115+
}
116+
// [END dlp_inspect_string_custom_omit_overlap]

dlp/src/test/java/dlp/snippets/InspectTests.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@
2121
import static org.hamcrest.core.StringContains.containsString;
2222
import static org.junit.Assert.assertNotNull;
2323

24-
import com.google.cloud.dlp.v2.DlpServiceClient;
25-
import com.google.privacy.dlp.v2.CancelDlpJobRequest;
2624
import java.io.ByteArrayOutputStream;
2725
import java.io.PrintStream;
2826
import java.util.Arrays;
29-
import java.util.concurrent.TimeUnit;
3027
import org.junit.After;
3128
import org.junit.Before;
3229
import org.junit.Test;
@@ -138,6 +135,16 @@ public void testInspectStringCustomExcludingSubstring() throws Exception {
138135
assertThat(output, not(containsString("Example, Jimmy")));
139136
}
140137

138+
@Test
139+
public void testInspectStringCustomOmitOverlap() throws Exception {
140+
InspectStringCustomOmitOverlap.inspectStringCustomOmitOverlap(PROJECT_ID,
141+
"Name: Jane Doe. Name: Larry Page.");
142+
143+
String output = bout.toString();
144+
assertThat(output, containsString("Jane Doe"));
145+
assertThat(output, not(containsString("Larry Page")));
146+
}
147+
141148
@Test
142149
public void textInspectTestFile() throws Exception {
143150
InspectTextFile.inspectTextFile(PROJECT_ID, "src/test/resources/test.txt");

0 commit comments

Comments
 (0)