Skip to content

Commit b3803be

Browse files
authored
Add regional endpoints code samples (#9431)
1 parent 3b38fe8 commit b3803be

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2024 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_rep]
20+
21+
import com.google.cloud.dlp.v2.DlpServiceClient;
22+
import com.google.cloud.dlp.v2.DlpServiceSettings;
23+
import com.google.privacy.dlp.v2.ByteContentItem;
24+
import com.google.privacy.dlp.v2.ByteContentItem.BytesType;
25+
import com.google.privacy.dlp.v2.ContentItem;
26+
import com.google.privacy.dlp.v2.Finding;
27+
import com.google.privacy.dlp.v2.InfoType;
28+
import com.google.privacy.dlp.v2.InspectConfig;
29+
import com.google.privacy.dlp.v2.InspectContentRequest;
30+
import com.google.privacy.dlp.v2.InspectContentResponse;
31+
import com.google.privacy.dlp.v2.LocationName;
32+
import com.google.protobuf.ByteString;
33+
import java.io.IOException;
34+
import java.util.ArrayList;
35+
import java.util.List;
36+
37+
public class InspectStringRep {
38+
39+
public static void main(String[] args) throws Exception {
40+
// TODO(developer): Replace these variables before running the sample.
41+
String projectId = "your-project-id";
42+
String repLocation = "regional-endpoint-location-to-use";
43+
String textToInspect = "My name is Gary and my email is [email protected]";
44+
inspectString(projectId, repLocation, textToInspect);
45+
}
46+
47+
// Inspects the provided text.
48+
public static void inspectString(String projectId, String repLocation, String textToInspect)
49+
throws IOException {
50+
// Assemble the regional endpoint url using provided rep location
51+
String repEndpoint = String.format("dlp.%s.rep.googleapis.com:443", repLocation);
52+
DlpServiceSettings settings = DlpServiceSettings.newBuilder()
53+
.setEndpoint(repEndpoint)
54+
.build();
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(settings)) {
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+
List<InfoType> infoTypes = new ArrayList<>();
69+
// See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types
70+
for (String typeName : new String[] {"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) {
71+
infoTypes.add(InfoType.newBuilder().setName(typeName).build());
72+
}
73+
74+
// Construct the configuration for the Inspect request.
75+
InspectConfig config =
76+
InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setIncludeQuote(true).build();
77+
78+
// Construct the Inspect request to be sent by the client.
79+
InspectContentRequest request =
80+
InspectContentRequest.newBuilder()
81+
.setParent(LocationName.of(projectId, repLocation).toString())
82+
.setItem(item)
83+
.setInspectConfig(config)
84+
.build();
85+
86+
// Use the client to send the API request.
87+
InspectContentResponse response = dlp.inspectContent(request);
88+
89+
// Parse the response and process results
90+
System.out.println("Findings: " + response.getResult().getFindingsCount());
91+
for (Finding f : response.getResult().getFindingsList()) {
92+
System.out.println("\tQuote: " + f.getQuote());
93+
System.out.println("\tInfo type: " + f.getInfoType().getName());
94+
System.out.println("\tLikelihood: " + f.getLikelihood());
95+
}
96+
}
97+
}
98+
}
99+
// [END dlp_inspect_string_rep]

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

+9
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ public void testInspectString() throws Exception {
146146
assertThat(output).contains("Info type: EMAIL_ADDRESS");
147147
}
148148

149+
@Test
150+
public void testInspectStringRep() throws Exception {
151+
InspectStringRep.inspectString(
152+
PROJECT_ID, "us-east1", "I'm Gary and my email is [email protected]");
153+
154+
String output = bout.toString();
155+
assertThat(output).contains("Info type: EMAIL_ADDRESS");
156+
}
157+
149158
@Test
150159
public void testInspectWithCustomRegex() throws Exception {
151160
InspectWithCustomRegex.inspectWithCustomRegex(

0 commit comments

Comments
 (0)