Skip to content

Commit 68834cc

Browse files
authored
Add Java code sample for replacement (GoogleCloudPlatform#3072)
Fixes #issue > It's a good idea to open an issue first for discussion. - [yes] I have followed [Sample Format Guide](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/SAMPLE_FORMAT.md) - [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 ab69fb5 commit 68834cc

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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_deidentify_replace]
20+
21+
import com.google.cloud.dlp.v2.DlpServiceClient;
22+
import com.google.privacy.dlp.v2.ContentItem;
23+
import com.google.privacy.dlp.v2.DeidentifyConfig;
24+
import com.google.privacy.dlp.v2.DeidentifyContentRequest;
25+
import com.google.privacy.dlp.v2.DeidentifyContentResponse;
26+
import com.google.privacy.dlp.v2.InfoType;
27+
import com.google.privacy.dlp.v2.InfoTypeTransformations;
28+
import com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation;
29+
import com.google.privacy.dlp.v2.InspectConfig;
30+
import com.google.privacy.dlp.v2.LocationName;
31+
import com.google.privacy.dlp.v2.PrimitiveTransformation;
32+
import com.google.privacy.dlp.v2.RedactConfig;
33+
import com.google.privacy.dlp.v2.ReplaceValueConfig;
34+
import com.google.privacy.dlp.v2.Value;
35+
36+
public class DeIdentifyWithReplacement {
37+
38+
public static void deIdentifyWithReplacement() {
39+
// TODO(developer): Replace these variables before running the sample.
40+
String projectId = "your-project-id";
41+
String textToInspect =
42+
"My name is Alicia Abernathy, and my email address is aabernathy@example.com.";
43+
deIdentifyWithReplacement(projectId, textToInspect);
44+
}
45+
46+
// Inspects the provided text.
47+
public static void deIdentifyWithReplacement(String projectId, String textToRedact) {
48+
// Initialize client that will be used to send requests. This client only needs to be created
49+
// once, and can be reused for multiple requests. After completing all of your requests, call
50+
// the "close" method on the client to safely clean up any remaining background resources.
51+
try (DlpServiceClient dlp = DlpServiceClient.create()) {
52+
// Specify the content to be inspected.
53+
ContentItem item = ContentItem.newBuilder()
54+
.setValue(textToRedact).build();
55+
56+
// Specify the type of info the inspection will look for.
57+
// See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types
58+
InfoType infoType = InfoType.newBuilder().setName("EMAIL_ADDRESS").build();
59+
InspectConfig inspectConfig = InspectConfig.newBuilder().addInfoTypes(infoType).build();
60+
// Specify replacement string to be used for the finding.
61+
ReplaceValueConfig replaceValueConfig = ReplaceValueConfig.newBuilder()
62+
.setNewValue(Value.newBuilder().setStringValue("[email-address]").build())
63+
.build();
64+
// Define type of deidentification as replacement.
65+
PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder()
66+
.setReplaceConfig(replaceValueConfig)
67+
.build();
68+
// Associate deidentification type with info type.
69+
InfoTypeTransformation transformation = InfoTypeTransformation.newBuilder()
70+
.addInfoTypes(infoType)
71+
.setPrimitiveTransformation(primitiveTransformation)
72+
.build();
73+
// Construct the configuration for the Redact request and list all desired transformations.
74+
DeidentifyConfig redactConfig = DeidentifyConfig.newBuilder()
75+
.setInfoTypeTransformations(InfoTypeTransformations.newBuilder()
76+
.addTransformations(transformation))
77+
.build();
78+
79+
// Construct the Redact request to be sent by the client.
80+
DeidentifyContentRequest request =
81+
DeidentifyContentRequest.newBuilder()
82+
.setParent(LocationName.of(projectId, "global").toString())
83+
.setItem(item)
84+
.setDeidentifyConfig(redactConfig)
85+
.setInspectConfig(inspectConfig)
86+
.build();
87+
88+
// Use the client to send the API request.
89+
DeidentifyContentResponse response = dlp.deidentifyContent(request);
90+
91+
// Parse the response and process results
92+
System.out.println("Text after redaction: " + response.getItem().getValue());
93+
} catch (Exception e) {
94+
System.out.println("Error during inspectString: \n" + e.toString());
95+
}
96+
}
97+
}
98+
// [END dlp_deidentify_replace]

dlp/src/test/java/dlp/snippets/DeIdentificationTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,15 @@ public void testDeIdentifyWithDateShift() throws IOException {
107107
// Clean up test output
108108
Files.delete(outputFile);
109109
}
110+
111+
@Test
112+
public void testDeIdentifyWithReplacement() throws IOException {
113+
DeIdentifyWithReplacement.deIdentifyWithReplacement(
114+
PROJECT_ID,
115+
"My name is Alicia Abernathy, and my email address is aabernathy@example.com.");
116+
117+
String output = bout.toString();
118+
assertThat(output, containsString("Text after redaction: "
119+
+ "My name is Alicia Abernathy, and my email address is [email-address]."));
120+
}
110121
}

0 commit comments

Comments
 (0)