Skip to content

Commit dec4945

Browse files
authored
Create Java code sample for omit names that are also an email (GoogleCloudPlatform#3065)
* Create Java code sample for omit names that are also an email * add test annotation dropped during merge
1 parent b604f7f commit dec4945

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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_with_exclusion_dict]
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.ExcludeInfoTypes;
26+
import com.google.privacy.dlp.v2.ExclusionRule;
27+
import com.google.privacy.dlp.v2.Finding;
28+
import com.google.privacy.dlp.v2.InfoType;
29+
import com.google.privacy.dlp.v2.InspectConfig;
30+
import com.google.privacy.dlp.v2.InspectContentRequest;
31+
import com.google.privacy.dlp.v2.InspectContentResponse;
32+
import com.google.privacy.dlp.v2.InspectionRule;
33+
import com.google.privacy.dlp.v2.InspectionRuleSet;
34+
import com.google.privacy.dlp.v2.LocationName;
35+
import com.google.privacy.dlp.v2.MatchingType;
36+
import com.google.protobuf.ByteString;
37+
import java.io.IOException;
38+
import java.util.ArrayList;
39+
import java.util.List;
40+
41+
public class InspectStringOmitOverlap {
42+
43+
public static void inspectStringOmitOverlap() throws IOException {
44+
// TODO(developer): Replace these variables before running the sample.
45+
String projectId = "your-project-id";
46+
String textToInspect = "james@example.com";
47+
inspectStringOmitOverlap(projectId, textToInspect);
48+
}
49+
50+
// Inspects the provided text, avoiding matches specified in the exclusion list.
51+
public static void inspectStringOmitOverlap(String projectId, String textToInspect)
52+
throws IOException {
53+
// Initialize client that will be used to send requests. This client only needs to be created
54+
// once, and can be reused for multiple requests. After completing all of your requests, call
55+
// the "close" method on the client to safely clean up any remaining background resources.
56+
try (DlpServiceClient dlp = DlpServiceClient.create()) {
57+
// Specify the type and content to be inspected.
58+
ByteContentItem byteItem =
59+
ByteContentItem.newBuilder()
60+
.setType(BytesType.TEXT_UTF8)
61+
.setData(ByteString.copyFromUtf8(textToInspect))
62+
.build();
63+
ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build();
64+
65+
// Specify the type of info the inspection will look for.
66+
// See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types.
67+
List<InfoType> infoTypes = new ArrayList<>();
68+
for (String typeName : new String[]{"PERSON_NAME", "EMAIL_ADDRESS"}) {
69+
infoTypes.add(InfoType.newBuilder().setName(typeName).build());
70+
}
71+
72+
// Exclude EMAIL_ADDRESS matches
73+
ExclusionRule exclusionRule = ExclusionRule.newBuilder()
74+
.setExcludeInfoTypes(
75+
ExcludeInfoTypes.newBuilder()
76+
.addInfoTypes(InfoType.newBuilder().setName("EMAIL_ADDRESS")))
77+
.setMatchingType(MatchingType.MATCHING_TYPE_PARTIAL_MATCH)
78+
.build();
79+
80+
// Construct a ruleset that applies the exclusion rule to the PERSON_NAME infotype.
81+
// If a PERSON_NAME match overlaps with an EMAIL_ADDRESS match, the PERSON_NAME match will
82+
// be excluded.
83+
InspectionRuleSet ruleSet = InspectionRuleSet.newBuilder()
84+
.addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME"))
85+
.addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule))
86+
.build();
87+
88+
// Construct the configuration for the Inspect request, including the ruleset.
89+
InspectConfig config =
90+
InspectConfig.newBuilder()
91+
.addAllInfoTypes(infoTypes)
92+
.setIncludeQuote(true)
93+
.addRuleSet(ruleSet)
94+
.build();
95+
96+
// Construct the Inspect request to be sent by the client.
97+
InspectContentRequest request =
98+
InspectContentRequest.newBuilder()
99+
.setParent(LocationName.of(projectId, "global").toString())
100+
.setItem(item)
101+
.setInspectConfig(config)
102+
.build();
103+
104+
// Use the client to send the API request.
105+
InspectContentResponse response = dlp.inspectContent(request);
106+
107+
// Parse the response and process results
108+
System.out.println("Findings: " + response.getResult().getFindingsCount());
109+
for (Finding f : response.getResult().getFindingsList()) {
110+
System.out.println("\tQuote: " + f.getQuote());
111+
System.out.println("\tInfo type: " + f.getInfoType().getName());
112+
System.out.println("\tLikelihood: " + f.getLikelihood());
113+
}
114+
}
115+
}
116+
}
117+
// [END dlp_inspect_string_with_exclusion_dict]

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ public void testInspectStringCustomOmitOverlap() throws Exception {
145145
assertThat(output, not(containsString("Larry Page")));
146146
}
147147

148+
@Test
149+
public void testInspectStringOmitOverlap() throws Exception {
150+
InspectStringOmitOverlap.inspectStringOmitOverlap(PROJECT_ID, "james@example.com");
151+
152+
String output = bout.toString();
153+
assertThat(output, containsString("EMAIL_ADDRESS"));
154+
assertThat(output, not(containsString("PERSON_NAME")));
155+
}
156+
148157
@Test
149158
public void textInspectTestFile() throws Exception {
150159
InspectTextFile.inspectTextFile(PROJECT_ID, "src/test/resources/test.txt");

0 commit comments

Comments
 (0)