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_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 .Regex ;
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 .util .ArrayList ;
38+ import java .util .List ;
39+
40+ public class InspectStringWithExclusionRegex {
41+
42+ public static void inspectStringWithExclusionRegex () {
43+ // TODO(developer): Replace these variables before running the sample.
44+ String projectId = "your-project-id" ;
45+ String textToInspect = "Some email addresses: gary@example.com, bob@example.org" ;
46+ String excludedRegex = ".+@example.com" ;
47+ inspectStringWithExclusionRegex (projectId , textToInspect , excludedRegex );
48+ }
49+
50+ // Inspects the provided text, avoiding matches specified in the exclusion list.
51+ public static void inspectStringWithExclusionRegex (String projectId , String textToInspect ,
52+ String excludedRegex ) {
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 []{"PHONE_NUMBER" , "EMAIL_ADDRESS" , "CREDIT_CARD_NUMBER" }) {
69+ infoTypes .add (InfoType .newBuilder ().setName (typeName ).build ());
70+ }
71+
72+ // Exclude matches from the specified excludedMatchList.
73+ ExclusionRule exclusionRule = ExclusionRule .newBuilder ()
74+ .setMatchingType (MatchingType .MATCHING_TYPE_FULL_MATCH )
75+ .setRegex (Regex .newBuilder ().setPattern (excludedRegex ))
76+ .build ();
77+
78+ // Construct a ruleset that applies the exclusion rule to the EMAIL_ADDRESSES infotype.
79+ InspectionRuleSet ruleSet = InspectionRuleSet .newBuilder ()
80+ .addInfoTypes (InfoType .newBuilder ().setName ("EMAIL_ADDRESS" ))
81+ .addRules (InspectionRule .newBuilder ().setExclusionRule (exclusionRule ))
82+ .build ();
83+
84+ // Construct the configuration for the Inspect request, including the ruleset.
85+ InspectConfig config =
86+ InspectConfig .newBuilder ()
87+ .addAllInfoTypes (infoTypes )
88+ .setIncludeQuote (true )
89+ .addRuleSet (ruleSet )
90+ .build ();
91+
92+ // Construct the Inspect request to be sent by the client.
93+ InspectContentRequest request =
94+ InspectContentRequest .newBuilder ()
95+ .setParent (LocationName .of (projectId , "global" ).toString ())
96+ .setItem (item )
97+ .setInspectConfig (config )
98+ .build ();
99+
100+ // Use the client to send the API request.
101+ InspectContentResponse response = dlp .inspectContent (request );
102+
103+ // Parse the response and process results
104+ System .out .println ("Findings: " + response .getResult ().getFindingsCount ());
105+ for (Finding f : response .getResult ().getFindingsList ()) {
106+ System .out .println ("\t Quote: " + f .getQuote ());
107+ System .out .println ("\t Info type: " + f .getInfoType ().getName ());
108+ System .out .println ("\t Likelihood: " + f .getLikelihood ());
109+ }
110+ } catch (Exception e ) {
111+ System .out .println ("Error during inspectString: \n " + e .toString ());
112+ }
113+ }
114+ }
115+ // [END dlp_inspect_string_with_exclusion_regex]
0 commit comments