Skip to content

Commit 3bcfd86

Browse files
authored
Create Java code sample for custom regex (GoogleCloudPlatform#3111)
To be linked from https://cloud.google.com/dlp/docs/creating-custom-infotypes-regex#regex-example Fixes #issue It's a good idea to open an issue first for discussion. [yes] I have followed Sample Format Guide [yes] pom.xml parent set to latest shared-configuration [nothing new] Appropriate changes to README are included in PR [nothing new] API's need to be enabled to test (tell us) [nothing new] Environment Variables need to be set (ask us to set them) [yes] Tests pass (mvn -P lint clean verify) (Note- Checkstyle passing is required; Spotbugs, ErrorProne, PMD, etc. ERROR's are advisory only) [yes] Please merge this PR for me once it is approved.
1 parent 8558a50 commit 3bcfd86

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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_custom_regex]
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.Regex;
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.Likelihood;
33+
import com.google.privacy.dlp.v2.ProjectName;
34+
import com.google.protobuf.ByteString;
35+
import java.io.IOException;
36+
37+
public class InspectWithCustomRegex {
38+
39+
public static void inspectWithCustomRegex() throws IOException {
40+
// TODO(developer): Replace these variables before running the sample.
41+
String projectId = "your-project-id";
42+
String textToInspect = "Patients MRN 444-5-22222";
43+
String customRegexPattern = "[1-9]{3}-[1-9]{1}-[1-9]{5}";
44+
inspectWithCustomRegex(projectId, textToInspect, customRegexPattern);
45+
}
46+
47+
// Inspects a BigQuery Table
48+
public static void inspectWithCustomRegex(
49+
String projectId, String textToInspect, String customRegexPattern) throws IOException {
50+
// Initialize client that will be used to send requests. This client only needs to be created
51+
// once, and can be reused for multiple requests. After completing all of your requests, call
52+
// the "close" method on the client to safely clean up any remaining background resources.
53+
try (DlpServiceClient dlp = DlpServiceClient.create()) {
54+
// Specify the project used for request.
55+
ProjectName project = ProjectName.of(projectId);
56+
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 regex pattern the inspection will look for.
66+
Regex regex = Regex.newBuilder().setPattern(customRegexPattern).build();
67+
68+
// Construct the custom regex detector.
69+
InfoType infoType = InfoType.newBuilder().setName("C_MRN").build();
70+
CustomInfoType customInfoType =
71+
CustomInfoType.newBuilder().setInfoType(infoType).setRegex(regex).build();
72+
73+
// Construct the configuration for the Inspect request.
74+
InspectConfig config =
75+
InspectConfig.newBuilder()
76+
.addCustomInfoTypes(customInfoType)
77+
.setIncludeQuote(true)
78+
.setMinLikelihood(Likelihood.POSSIBLE)
79+
.build();
80+
81+
// Construct the Inspect request to be sent by the client.
82+
InspectContentRequest request =
83+
InspectContentRequest.newBuilder()
84+
.setParent(project.toString())
85+
.setItem(item)
86+
.setInspectConfig(config)
87+
.build();
88+
89+
// Use the client to send the API request.
90+
InspectContentResponse response = dlp.inspectContent(request);
91+
92+
// Parse the response and process results
93+
System.out.println("Findings: " + response.getResult().getFindingsCount());
94+
for (Finding f : response.getResult().getFindingsList()) {
95+
System.out.println("\tQuote: " + f.getQuote());
96+
System.out.println("\tInfo type: " + f.getInfoType().getName());
97+
System.out.println("\tLikelihood: " + f.getLikelihood());
98+
}
99+
}
100+
}
101+
}
102+
// [END dlp_inspect_custom_regex]

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ public void testInspectString() throws Exception {
143143
assertThat(output, containsString("Info type: EMAIL_ADDRESS"));
144144
}
145145

146+
@Test
147+
public void testInspectWithCustomRegex() throws Exception {
148+
InspectWithCustomRegex.inspectWithCustomRegex(
149+
PROJECT_ID, "Patients MRN 444-5-22222", "[1-9]{3}-[1-9]{1}-[1-9]{5}");
150+
151+
String output = bout.toString();
152+
assertThat(output, containsString("Info type: C_MRN"));
153+
}
146154

147155
@Test
148156
public void testInspectStringWithExclusionDict() throws Exception {

0 commit comments

Comments
 (0)