Skip to content

Commit 9a4816e

Browse files
authored
Create Java code sample for multiple rules (GoogleCloudPlatform#3112)
* Create Java code sample for multiple rules * add more tests * fix formatting * More formatting fix * one more format fix
1 parent be82695 commit 9a4816e

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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_multiple_rules]
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.DetectionRule.HotwordRule;
26+
import com.google.privacy.dlp.v2.CustomInfoType.DetectionRule.LikelihoodAdjustment;
27+
import com.google.privacy.dlp.v2.CustomInfoType.DetectionRule.Proximity;
28+
import com.google.privacy.dlp.v2.CustomInfoType.Dictionary;
29+
import com.google.privacy.dlp.v2.CustomInfoType.Dictionary.WordList;
30+
import com.google.privacy.dlp.v2.CustomInfoType.Regex;
31+
import com.google.privacy.dlp.v2.ExclusionRule;
32+
import com.google.privacy.dlp.v2.Finding;
33+
import com.google.privacy.dlp.v2.InfoType;
34+
import com.google.privacy.dlp.v2.InspectConfig;
35+
import com.google.privacy.dlp.v2.InspectContentRequest;
36+
import com.google.privacy.dlp.v2.InspectContentResponse;
37+
import com.google.privacy.dlp.v2.InspectionRule;
38+
import com.google.privacy.dlp.v2.InspectionRuleSet;
39+
import com.google.privacy.dlp.v2.Likelihood;
40+
import com.google.privacy.dlp.v2.LocationName;
41+
import com.google.privacy.dlp.v2.MatchingType;
42+
import com.google.protobuf.ByteString;
43+
import java.io.IOException;
44+
45+
public class InspectStringMultipleRules {
46+
47+
public static void inspectStringMultipleRules() throws IOException {
48+
// TODO(developer): Replace these variables before running the sample.
49+
String projectId = "your-project-id";
50+
String textToInspect = "patient: Jane Doe";
51+
inspectStringMultipleRules(projectId, textToInspect);
52+
}
53+
54+
// Inspects the provided text, avoiding matches specified in the exclusion list.
55+
public static void inspectStringMultipleRules(String projectId, String textToInspect)
56+
throws IOException {
57+
// Initialize client that will be used to send requests. This client only needs to be created
58+
// once, and can be reused for multiple requests. After completing all of your requests, call
59+
// the "close" method on the client to safely clean up any remaining background resources.
60+
try (DlpServiceClient dlp = DlpServiceClient.create()) {
61+
// Specify the type and content to be inspected.
62+
ByteContentItem byteItem =
63+
ByteContentItem.newBuilder()
64+
.setType(BytesType.TEXT_UTF8)
65+
.setData(ByteString.copyFromUtf8(textToInspect))
66+
.build();
67+
ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();
68+
69+
// Construct hotword rules
70+
HotwordRule patientRule = HotwordRule.newBuilder()
71+
.setHotwordRegex(Regex.newBuilder().setPattern("patient"))
72+
.setProximity(Proximity.newBuilder().setWindowBefore(10))
73+
.setLikelihoodAdjustment(
74+
LikelihoodAdjustment.newBuilder().setFixedLikelihood(Likelihood.VERY_LIKELY))
75+
.build();
76+
77+
HotwordRule doctorRule = HotwordRule.newBuilder()
78+
.setHotwordRegex(Regex.newBuilder().setPattern("doctor"))
79+
.setProximity(Proximity.newBuilder().setWindowBefore(10))
80+
.setLikelihoodAdjustment(
81+
LikelihoodAdjustment.newBuilder().setFixedLikelihood(Likelihood.UNLIKELY))
82+
.build();
83+
84+
// Construct exclusion rules
85+
ExclusionRule quasimodoRule = ExclusionRule.newBuilder()
86+
.setDictionary(
87+
Dictionary.newBuilder().setWordList(WordList.newBuilder().addWords("Quasimodo")))
88+
.setMatchingType(MatchingType.MATCHING_TYPE_PARTIAL_MATCH)
89+
.build();
90+
91+
ExclusionRule redactedRule = ExclusionRule.newBuilder()
92+
.setRegex(Regex.newBuilder().setPattern("REDACTED"))
93+
.setMatchingType(MatchingType.MATCHING_TYPE_PARTIAL_MATCH)
94+
.build();
95+
96+
// Construct a ruleset that applies the rules to the PERSON_NAME infotype.
97+
InspectionRuleSet ruleSet = InspectionRuleSet.newBuilder()
98+
.addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME"))
99+
.addRules(InspectionRule.newBuilder().setHotwordRule(patientRule))
100+
.addRules(InspectionRule.newBuilder().setHotwordRule(doctorRule))
101+
.addRules(InspectionRule.newBuilder().setExclusionRule(quasimodoRule))
102+
.addRules(InspectionRule.newBuilder().setExclusionRule(redactedRule))
103+
.build();
104+
105+
// Construct the configuration for the Inspect request, including the ruleset.
106+
InspectConfig config =
107+
InspectConfig.newBuilder()
108+
.addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME"))
109+
.setIncludeQuote(true)
110+
.addRuleSet(ruleSet)
111+
.build();
112+
113+
// Construct the Inspect request to be sent by the client.
114+
InspectContentRequest request =
115+
InspectContentRequest.newBuilder()
116+
.setParent(LocationName.of(projectId, "global").toString())
117+
.setItem(item)
118+
.setInspectConfig(config)
119+
.build();
120+
121+
// Use the client to send the API request.
122+
InspectContentResponse response = dlp.inspectContent(request);
123+
124+
// Parse the response and process results
125+
System.out.println("Findings: " + response.getResult().getFindingsCount());
126+
for (Finding f : response.getResult().getFindingsList()) {
127+
System.out.println("\tQuote: " + f.getQuote());
128+
System.out.println("\tInfo type: " + f.getInfoType().getName());
129+
System.out.println("\tLikelihood: " + f.getLikelihood());
130+
}
131+
}
132+
}
133+
}
134+
// [END dlp_inspect_string_multiple_rules]

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,42 @@ public void testInspectStringCustomHotwordNegativeExample() throws Exception {
259259
assertThat(output, not(containsString("John Doe")));
260260
}
261261

262+
@Test
263+
public void testInspectStringMultipleRulesPatientRule() throws Exception {
264+
InspectStringMultipleRules.inspectStringMultipleRules(PROJECT_ID,
265+
"patient: Jane Doe");
266+
267+
String output = bout.toString();
268+
assertThat(output, containsString("VERY_LIKELY"));
269+
}
270+
271+
@Test
272+
public void testInspectStringMultipleRulesDoctorRule() throws Exception {
273+
InspectStringMultipleRules.inspectStringMultipleRules(PROJECT_ID,
274+
"doctor: Jane Doe");
275+
276+
String output = bout.toString();
277+
assertThat(output, containsString("Findings: 0"));
278+
}
279+
280+
@Test
281+
public void testInspectStringMultipleRulesQuasimodoRule() throws Exception {
282+
InspectStringMultipleRules.inspectStringMultipleRules(PROJECT_ID,
283+
"patient: Quasimodo");
284+
285+
String output = bout.toString();
286+
assertThat(output, containsString("Findings: 0"));
287+
}
288+
289+
@Test
290+
public void testInspectStringMultipleRulesRedactedRule() throws Exception {
291+
InspectStringMultipleRules.inspectStringMultipleRules(PROJECT_ID,
292+
"name of patient: REDACTED");
293+
294+
String output = bout.toString();
295+
assertThat(output, containsString("Findings: 0"));
296+
}
297+
262298
@Test
263299
public void textInspectTestFile() throws Exception {
264300
InspectTextFile.inspectTextFile(PROJECT_ID, "src/test/resources/test.txt");

0 commit comments

Comments
 (0)