Skip to content

Commit 532e145

Browse files
authored
Create Java code sample for omit finding with substring using custom detector (GoogleCloudPlatform#3055)
* Add InspectStringCustomExcludingSubstring * add test for InspectStringCustomExcludingSubstring * Fix sample annotation name
1 parent 4f4c8ed commit 532e145

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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_excluding_substring]
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.Dictionary;
27+
import com.google.privacy.dlp.v2.CustomInfoType.Dictionary.WordList;
28+
import com.google.privacy.dlp.v2.CustomInfoType.Regex;
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.util.Arrays;
41+
import java.util.List;
42+
43+
public class InspectStringCustomExcludingSubstring {
44+
45+
public static void inspectStringCustomExcludingSubstring() {
46+
// TODO(developer): Replace these variables before running the sample.
47+
String projectId = "your-project-id";
48+
String textToInspect = "Name: Doe, John. Name: Example, Jimmy";
49+
String customDetectorPattern = "[A-Z][a-z]{1,15}, [A-Z][a-z]{1,15}";
50+
List<String> excludedSubstringList = Arrays.asList("Jimmy");
51+
inspectStringCustomExcludingSubstring(projectId, textToInspect, customDetectorPattern,
52+
excludedSubstringList);
53+
}
54+
55+
// Inspects the provided text, avoiding matches specified in the exclusion list.
56+
public static void inspectStringCustomExcludingSubstring(String projectId, String textToInspect,
57+
String customDetectorPattern, List<String> excludedSubstringList) {
58+
// Initialize client that will be used to send requests. This client only needs to be created
59+
// once, and can be reused for multiple requests. After completing all of your requests, call
60+
// the "close" method on the client to safely clean up any remaining background resources.
61+
try (DlpServiceClient dlp = DlpServiceClient.create()) {
62+
// Specify the type and content to be inspected.
63+
ByteContentItem byteItem =
64+
ByteContentItem.newBuilder()
65+
.setType(BytesType.TEXT_UTF8)
66+
.setData(ByteString.copyFromUtf8(textToInspect))
67+
.build();
68+
ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();
69+
70+
// Specify the type of info the inspection will look for.
71+
InfoType infoType = InfoType.newBuilder().setName("CUSTOM_NAME_DETECTOR").build();
72+
CustomInfoType customInfoType = CustomInfoType.newBuilder()
73+
.setInfoType(infoType).setRegex(
74+
Regex.newBuilder().setPattern(customDetectorPattern)).build();
75+
76+
// Exclude partial matches from the specified excludedSubstringList.
77+
ExclusionRule exclusionRule = ExclusionRule.newBuilder()
78+
.setMatchingType(MatchingType.MATCHING_TYPE_PARTIAL_MATCH)
79+
.setDictionary(Dictionary.newBuilder()
80+
.setWordList(WordList.newBuilder().addAllWords(excludedSubstringList)))
81+
.build();
82+
83+
// Construct a ruleset that applies the exclusion rule to the EMAIL_ADDRESSES infotype.
84+
InspectionRuleSet ruleSet = InspectionRuleSet.newBuilder()
85+
.addInfoTypes(infoType)
86+
.addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule))
87+
.build();
88+
89+
// Construct the configuration for the Inspect request, including the ruleset.
90+
InspectConfig config =
91+
InspectConfig.newBuilder()
92+
.addCustomInfoTypes(customInfoType)
93+
.setIncludeQuote(true)
94+
.addRuleSet(ruleSet)
95+
.build();
96+
97+
// Construct the Inspect request to be sent by the client.
98+
InspectContentRequest request =
99+
InspectContentRequest.newBuilder()
100+
.setParent(LocationName.of(projectId, "global").toString())
101+
.setItem(item)
102+
.setInspectConfig(config)
103+
.build();
104+
105+
// Use the client to send the API request.
106+
InspectContentResponse response = dlp.inspectContent(request);
107+
108+
// Parse the response and process results
109+
System.out.println("Findings: " + response.getResult().getFindingsCount());
110+
for (Finding f : response.getResult().getFindingsList()) {
111+
System.out.println("\tQuote: " + f.getQuote());
112+
System.out.println("\tInfo type: " + f.getInfoType().getName());
113+
System.out.println("\tLikelihood: " + f.getLikelihood());
114+
}
115+
} catch (Exception e) {
116+
System.out.println("Error during inspectString: \n" + e.toString());
117+
}
118+
}
119+
}
120+
// [END dlp_inspect_string_custom_excluding_substring]

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ public void testInspectStringWithExclusionRegex() {
128128
assertThat(output, not(containsString("gary@example.com")));
129129
}
130130

131+
@Test
132+
public void testInspectStringCustomExcludingSubstring() {
133+
InspectStringCustomExcludingSubstring.inspectStringCustomExcludingSubstring(PROJECT_ID,
134+
"Name: Doe, John. Name: Example, Jimmy",
135+
"[A-Z][a-z]{1,15}, [A-Z][a-z]{1,15}",
136+
Arrays.asList("Jimmy"));
137+
138+
String output = bout.toString();
139+
assertThat(output, containsString("Doe, John"));
140+
assertThat(output, not(containsString("Example, Jimmy")));
141+
}
142+
131143
@Test
132144
public void textInspectTestFile() {
133145
InspectTextFile.inspectTextFile(PROJECT_ID, "src/test/resources/test.txt");

0 commit comments

Comments
 (0)