Skip to content

Commit b4d608a

Browse files
authored
Create Java code sample for omit domain names that are an email (GoogleCloudPlatform#3066)
Fixes internal bug b/156972858 - [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 dec4945 commit b4d608a

File tree

2 files changed

+136
-0
lines changed

2 files changed

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

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,16 @@ public void testInspectStringOmitOverlap() throws Exception {
154154
assertThat(output, not(containsString("PERSON_NAME")));
155155
}
156156

157+
@Test
158+
public void testInspectStringWithoutOverlap() throws Exception {
159+
InspectStringWithoutOverlap.inspectStringWithoutOverlap(PROJECT_ID,
160+
"example.com is a domain, james@example.org is an email.");
161+
162+
String output = bout.toString();
163+
assertThat(output, containsString("example.com"));
164+
assertThat(output, not(containsString("example.org")));
165+
}
166+
157167
@Test
158168
public void textInspectTestFile() throws Exception {
159169
InspectTextFile.inspectTextFile(PROJECT_ID, "src/test/resources/test.txt");

0 commit comments

Comments
 (0)