Search in sources :

Example 1 with ReplaceValueConfig

use of com.google.privacy.dlp.v2.ReplaceValueConfig in project java-dlp by googleapis.

the class DeIdentifyWithReplacement method deIdentifyWithReplacement.

// Inspects the provided text.
public static void deIdentifyWithReplacement(String projectId, String textToRedact) {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (DlpServiceClient dlp = DlpServiceClient.create()) {
        // Specify the content to be inspected.
        ContentItem item = ContentItem.newBuilder().setValue(textToRedact).build();
        // Specify the type of info the inspection will look for.
        // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types
        InfoType infoType = InfoType.newBuilder().setName("EMAIL_ADDRESS").build();
        InspectConfig inspectConfig = InspectConfig.newBuilder().addInfoTypes(infoType).build();
        // Specify replacement string to be used for the finding.
        ReplaceValueConfig replaceValueConfig = ReplaceValueConfig.newBuilder().setNewValue(Value.newBuilder().setStringValue("[email-address]").build()).build();
        // Define type of deidentification as replacement.
        PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder().setReplaceConfig(replaceValueConfig).build();
        // Associate deidentification type with info type.
        InfoTypeTransformation transformation = InfoTypeTransformation.newBuilder().addInfoTypes(infoType).setPrimitiveTransformation(primitiveTransformation).build();
        // Construct the configuration for the Redact request and list all desired transformations.
        DeidentifyConfig redactConfig = DeidentifyConfig.newBuilder().setInfoTypeTransformations(InfoTypeTransformations.newBuilder().addTransformations(transformation)).build();
        // Construct the Redact request to be sent by the client.
        DeidentifyContentRequest request = DeidentifyContentRequest.newBuilder().setParent(LocationName.of(projectId, "global").toString()).setItem(item).setDeidentifyConfig(redactConfig).setInspectConfig(inspectConfig).build();
        // Use the client to send the API request.
        DeidentifyContentResponse response = dlp.deidentifyContent(request);
        // Parse the response and process results
        System.out.println("Text after redaction: " + response.getItem().getValue());
    } catch (Exception e) {
        System.out.println("Error during inspectString: \n" + e.toString());
    }
}
Also used : DeidentifyContentRequest(com.google.privacy.dlp.v2.DeidentifyContentRequest) PrimitiveTransformation(com.google.privacy.dlp.v2.PrimitiveTransformation) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) DeidentifyConfig(com.google.privacy.dlp.v2.DeidentifyConfig) ReplaceValueConfig(com.google.privacy.dlp.v2.ReplaceValueConfig) InfoTypeTransformation(com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation) InfoType(com.google.privacy.dlp.v2.InfoType) InspectConfig(com.google.privacy.dlp.v2.InspectConfig) ContentItem(com.google.privacy.dlp.v2.ContentItem) DeidentifyContentResponse(com.google.privacy.dlp.v2.DeidentifyContentResponse)

Aggregations

DlpServiceClient (com.google.cloud.dlp.v2.DlpServiceClient)1 ContentItem (com.google.privacy.dlp.v2.ContentItem)1 DeidentifyConfig (com.google.privacy.dlp.v2.DeidentifyConfig)1 DeidentifyContentRequest (com.google.privacy.dlp.v2.DeidentifyContentRequest)1 DeidentifyContentResponse (com.google.privacy.dlp.v2.DeidentifyContentResponse)1 InfoType (com.google.privacy.dlp.v2.InfoType)1 InfoTypeTransformation (com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation)1 InspectConfig (com.google.privacy.dlp.v2.InspectConfig)1 PrimitiveTransformation (com.google.privacy.dlp.v2.PrimitiveTransformation)1 ReplaceValueConfig (com.google.privacy.dlp.v2.ReplaceValueConfig)1