Skip to content

Commit 7cc9546

Browse files
authored
1 parent 513e751 commit 7cc9546

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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_phone_number]
20+
21+
import com.google.cloud.dlp.v2.DlpServiceClient;
22+
import com.google.privacy.dlp.v2.ContentItem;
23+
import com.google.privacy.dlp.v2.Finding;
24+
import com.google.privacy.dlp.v2.InfoType;
25+
import com.google.privacy.dlp.v2.InspectConfig;
26+
import com.google.privacy.dlp.v2.InspectContentRequest;
27+
import com.google.privacy.dlp.v2.InspectContentResponse;
28+
import com.google.privacy.dlp.v2.Likelihood;
29+
import com.google.privacy.dlp.v2.ProjectName;
30+
31+
public class InspectPhoneNumber {
32+
33+
public static void inspectString() {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String projectId = "your-project-id";
36+
String textToInspect = "My name is Gary and my email is gary@example.com";
37+
inspectString(projectId, textToInspect);
38+
}
39+
40+
// Inspects the provided text.
41+
public static void inspectString(String projectId, String textToInspect) {
42+
// Initialize client that will be used to send requests. This client only needs to be created
43+
// once, and can be reused for multiple requests. After completing all of your requests, call
44+
// the "close" method on the client to safely clean up any remaining background resources.
45+
try (DlpServiceClient dlp = DlpServiceClient.create()) {
46+
// Specify the project used for request.
47+
ProjectName project = ProjectName.of(projectId);
48+
49+
// Specify the type and content to be inspected.
50+
ContentItem item = ContentItem.newBuilder()
51+
.setValue(textToInspect)
52+
.build();
53+
54+
// Specify the type of info the inspection will look for.
55+
// See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types
56+
InfoType infoType = InfoType.newBuilder().setName("PHONE_NUMBER").build();
57+
58+
// Construct the configuration for the Inspect request.
59+
InspectConfig config =
60+
InspectConfig.newBuilder()
61+
.setIncludeQuote(true)
62+
.setMinLikelihood(Likelihood.POSSIBLE)
63+
.addInfoTypes(infoType)
64+
.build();
65+
66+
// Construct the Inspect request to be sent by the client.
67+
InspectContentRequest request =
68+
InspectContentRequest.newBuilder()
69+
.setParent(project.toString())
70+
.setItem(item)
71+
.setInspectConfig(config)
72+
.build();
73+
74+
// Use the client to send the API request.
75+
InspectContentResponse response = dlp.inspectContent(request);
76+
77+
// Parse the response and process results
78+
System.out.println("Findings: " + response.getResult().getFindingsCount());
79+
for (Finding f : response.getResult().getFindingsList()) {
80+
System.out.println("\tQuote: " + f.getQuote());
81+
System.out.println("\tInfo type: " + f.getInfoType().getName());
82+
System.out.println("\tLikelihood: " + f.getLikelihood());
83+
}
84+
} catch (Exception e) {
85+
System.out.println("Error during inspectString: \n" + e.toString());
86+
}
87+
}
88+
}
89+
// [END dlp_inspect_phone_number]

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ public void releaseOut() {
7676
bout.reset();
7777
}
7878

79+
@Test
80+
public void testInspectPhoneNumber() {
81+
InspectString.inspectString(PROJECT_ID, "My phone number is (415) 555-0890");
82+
83+
String output = bout.toString();
84+
assertThat(output, containsString("Info type: PHONE_NUMBER"));
85+
}
86+
7987
@Test
8088
public void testInspectString() {
8189
InspectString.inspectString(PROJECT_ID, "I'm Gary and my email is gary@example.com");

0 commit comments

Comments
 (0)