|
| 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_infotype] |
| 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.ReplaceWithInfoTypeConfig; |
| 33 | +import java.io.IOException; |
| 34 | + |
| 35 | +public class DeIdentifyWithInfoType { |
| 36 | + |
| 37 | + public static void deIdentifyWithInfoType() throws IOException { |
| 38 | + // TODO(developer): Replace these variables before running the sample. |
| 39 | + String projectId = "your-project-id"; |
| 40 | + String textToInspect = |
| 41 | + "My email is test@example.com"; |
| 42 | + deIdentifyWithInfoType(projectId, textToInspect); |
| 43 | + } |
| 44 | + |
| 45 | + public static void deIdentifyWithInfoType(String projectId, String textToRedact) |
| 46 | + throws IOException { |
| 47 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 48 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 49 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 50 | + try (DlpServiceClient dlp = DlpServiceClient.create()) { |
| 51 | + // Specify the content to be inspected. |
| 52 | + ContentItem item = ContentItem.newBuilder() |
| 53 | + .setValue(textToRedact).build(); |
| 54 | + |
| 55 | + // Specify the type of info the inspection will look for. |
| 56 | + // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types |
| 57 | + InfoType infoType = InfoType.newBuilder().setName("EMAIL_ADDRESS").build(); |
| 58 | + InspectConfig inspectConfig = InspectConfig.newBuilder().addInfoTypes(infoType).build(); |
| 59 | + // Specify replacement string to be used for the finding. |
| 60 | + ReplaceWithInfoTypeConfig replaceWithInfoTypeConfig = |
| 61 | + ReplaceWithInfoTypeConfig.newBuilder().build(); |
| 62 | + // Define type of deidentification as replacement with info type. |
| 63 | + PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder() |
| 64 | + .setReplaceWithInfoTypeConfig(replaceWithInfoTypeConfig) |
| 65 | + .build(); |
| 66 | + // Associate deidentification type with info type. |
| 67 | + InfoTypeTransformation transformation = InfoTypeTransformation.newBuilder() |
| 68 | + .addInfoTypes(infoType) |
| 69 | + .setPrimitiveTransformation(primitiveTransformation) |
| 70 | + .build(); |
| 71 | + // Construct the configuration for the Redact request and list all desired transformations. |
| 72 | + DeidentifyConfig redactConfig = DeidentifyConfig.newBuilder() |
| 73 | + .setInfoTypeTransformations(InfoTypeTransformations.newBuilder() |
| 74 | + .addTransformations(transformation)) |
| 75 | + .build(); |
| 76 | + |
| 77 | + // Construct the Redact request to be sent by the client. |
| 78 | + DeidentifyContentRequest request = |
| 79 | + DeidentifyContentRequest.newBuilder() |
| 80 | + .setParent(LocationName.of(projectId, "global").toString()) |
| 81 | + .setItem(item) |
| 82 | + .setDeidentifyConfig(redactConfig) |
| 83 | + .setInspectConfig(inspectConfig) |
| 84 | + .build(); |
| 85 | + |
| 86 | + // Use the client to send the API request. |
| 87 | + DeidentifyContentResponse response = dlp.deidentifyContent(request); |
| 88 | + |
| 89 | + // Parse the response and process results |
| 90 | + System.out.println("Text after redaction: " + response.getItem().getValue()); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | +// [END dlp_deidentify_replace] |
0 commit comments