Search in sources :

Example 1 with InspectContentRequest

use of com.google.privacy.dlp.v2.InspectContentRequest in project java-docs-samples by GoogleCloudPlatform.

the class Inspect method inspectString.

/**
 * [START dlp_inspect_string] Inspect a text for given InfoTypes
 *
 * @param string String to instpect
 * @param minLikelihood The minimum likelihood required before returning a match
 * @param maxFindings The maximum number of findings to report (0 = server maximum)
 * @param infoTypes The infoTypes of information to match
 * @param includeQuote Whether to include the matching string
 * @param projectId Google Cloud project ID
 */
private static void inspectString(String string, Likelihood minLikelihood, int maxFindings, List<InfoType> infoTypes, boolean includeQuote, String projectId) {
    // instantiate a client
    try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
        FindingLimits findingLimits = FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
        InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setMinLikelihood(minLikelihood).setLimits(findingLimits).setIncludeQuote(includeQuote).build();
        ByteContentItem byteContentItem = ByteContentItem.newBuilder().setType(ByteContentItem.BytesType.TEXT_UTF8).setData(ByteString.copyFromUtf8(string)).build();
        ContentItem contentItem = ContentItem.newBuilder().setByteItem(byteContentItem).build();
        InspectContentRequest request = InspectContentRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).setInspectConfig(inspectConfig).setItem(contentItem).build();
        InspectContentResponse response = dlpServiceClient.inspectContent(request);
        if (response.getResult().getFindingsCount() > 0) {
            System.out.println("Findings: ");
            for (Finding finding : response.getResult().getFindingsList()) {
                if (includeQuote) {
                    System.out.print("\tQuote: " + finding.getQuote());
                }
                System.out.print("\tInfo type: " + finding.getInfoType().getName());
                System.out.println("\tLikelihood: " + finding.getLikelihood());
            }
        } else {
            System.out.println("No findings.");
        }
    } catch (Exception e) {
        System.out.println("Error in inspectString: " + e.getMessage());
    }
}
Also used : FindingLimits(com.google.privacy.dlp.v2.InspectConfig.FindingLimits) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) InspectContentResponse(com.google.privacy.dlp.v2.InspectContentResponse) Finding(com.google.privacy.dlp.v2.Finding) ByteContentItem(com.google.privacy.dlp.v2.ByteContentItem) InspectContentRequest(com.google.privacy.dlp.v2.InspectContentRequest) InspectConfig(com.google.privacy.dlp.v2.InspectConfig) ByteContentItem(com.google.privacy.dlp.v2.ByteContentItem) ContentItem(com.google.privacy.dlp.v2.ContentItem) ParseException(org.apache.commons.cli.ParseException)

Example 2 with InspectContentRequest

use of com.google.privacy.dlp.v2.InspectContentRequest in project java-docs-samples by GoogleCloudPlatform.

the class QuickStart method main.

/**
 * Quick start to DLP API : inspects a given string for an InfoType.
 */
public static void main(String[] args) throws Exception {
    // string to inspect
    String text = "His name was Robert Frost";
    // The minimum likelihood required before returning a match:
    // LIKELIHOOD_UNSPECIFIED, VERY_UNLIKELY, UNLIKELY, POSSIBLE, LIKELY, VERY_LIKELY, UNRECOGNIZED
    Likelihood minLikelihood = Likelihood.POSSIBLE;
    // The maximum number of findings to report (0 = server maximum)
    int maxFindings = 0;
    // The infoTypes of information to match
    List<InfoType> infoTypes = Arrays.asList(InfoType.newBuilder().setName("PERSON_NAME").build(), InfoType.newBuilder().setName("US_STATE").build());
    // Whether to include the matching string
    boolean includeQuote = true;
    // instantiate a client
    try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
        InspectConfig.FindingLimits findingLimits = InspectConfig.FindingLimits.newBuilder().setMaxFindingsPerItem(maxFindings).build();
        InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setMinLikelihood(minLikelihood).setLimits(findingLimits).setIncludeQuote(includeQuote).build();
        ByteContentItem byteContentItem = ByteContentItem.newBuilder().setType(ByteContentItem.BytesType.TEXT_UTF8).setData(ByteString.copyFromUtf8(text)).build();
        ContentItem contentItem = ContentItem.newBuilder().setByteItem(byteContentItem).build();
        String projectId = ServiceOptions.getDefaultProjectId();
        InspectContentRequest request = InspectContentRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).setInspectConfig(inspectConfig).setItem(contentItem).build();
        // Inspect the text for info types
        InspectContentResponse response = dlpServiceClient.inspectContent(request);
        InspectResult result = response.getResult();
        if (result.getFindingsCount() > 0) {
            System.out.println("Findings: ");
            for (Finding finding : result.getFindingsList()) {
                if (includeQuote) {
                    System.out.print("\tQuote: " + finding.getQuote());
                }
                System.out.print("\tInfo type: " + finding.getInfoType().getName());
                System.out.println("\tLikelihood: " + finding.getLikelihood());
            }
        } else {
            System.out.println("No findings.");
        }
    } catch (Exception e) {
        System.out.println("Error in inspectString: " + e.getMessage());
    }
}
Also used : Likelihood(com.google.privacy.dlp.v2.Likelihood) ByteContentItem(com.google.privacy.dlp.v2.ByteContentItem) ByteString(com.google.protobuf.ByteString) InspectContentRequest(com.google.privacy.dlp.v2.InspectContentRequest) InspectConfig(com.google.privacy.dlp.v2.InspectConfig) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) InspectContentResponse(com.google.privacy.dlp.v2.InspectContentResponse) Finding(com.google.privacy.dlp.v2.Finding) InspectResult(com.google.privacy.dlp.v2.InspectResult) InfoType(com.google.privacy.dlp.v2.InfoType) ByteContentItem(com.google.privacy.dlp.v2.ByteContentItem) ContentItem(com.google.privacy.dlp.v2.ContentItem)

Example 3 with InspectContentRequest

use of com.google.privacy.dlp.v2.InspectContentRequest in project java-docs-samples by GoogleCloudPlatform.

the class Inspect method inspectFile.

// [END dlp_inspect_string]
// [START dlp_inspect_file]
/**
 * Inspect a local file
 *
 * @param filePath The path to a local file to inspect. Can be a text, JPG, or PNG file.
 * @param minLikelihood The minimum likelihood required before returning a match
 * @param maxFindings The maximum number of findings to report (0 = server maximum)
 * @param infoTypes The infoTypes of information to match
 * @param includeQuote Whether to include the matching string
 * @param projectId Google Cloud project ID
 */
private static void inspectFile(String filePath, Likelihood minLikelihood, int maxFindings, List<InfoType> infoTypes, boolean includeQuote, String projectId) {
    // Instantiates a client
    try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
        // detect file mime type, default to application/octet-stream
        String mimeType = URLConnection.guessContentTypeFromName(filePath);
        if (mimeType == null) {
            mimeType = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(filePath);
        }
        ByteContentItem.BytesType bytesType;
        switch(mimeType) {
            case "image/jpeg":
                bytesType = ByteContentItem.BytesType.IMAGE_JPEG;
                break;
            case "image/bmp":
                bytesType = ByteContentItem.BytesType.IMAGE_BMP;
                break;
            case "image/png":
                bytesType = ByteContentItem.BytesType.IMAGE_PNG;
                break;
            case "image/svg":
                bytesType = ByteContentItem.BytesType.IMAGE_SVG;
                break;
            default:
                bytesType = ByteContentItem.BytesType.BYTES_TYPE_UNSPECIFIED;
                break;
        }
        byte[] data = Files.readAllBytes(Paths.get(filePath));
        ByteContentItem byteContentItem = ByteContentItem.newBuilder().setType(bytesType).setData(ByteString.copyFrom(data)).build();
        ContentItem contentItem = ContentItem.newBuilder().setByteItem(byteContentItem).build();
        FindingLimits findingLimits = FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
        InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setMinLikelihood(minLikelihood).setLimits(findingLimits).setIncludeQuote(includeQuote).build();
        InspectContentRequest request = InspectContentRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).setInspectConfig(inspectConfig).setItem(contentItem).build();
        InspectContentResponse response = dlpServiceClient.inspectContent(request);
        InspectResult result = response.getResult();
        if (result.getFindingsCount() > 0) {
            System.out.println("Findings: ");
            for (Finding finding : result.getFindingsList()) {
                if (includeQuote) {
                    System.out.print("\tQuote: " + finding.getQuote());
                }
                System.out.print("\tInfo type: " + finding.getInfoType().getName());
                System.out.println("\tLikelihood: " + finding.getLikelihood());
            }
        } else {
            System.out.println("No findings.");
        }
    } catch (Exception e) {
        System.out.println("Error in inspectFile: " + e.getMessage());
    }
}
Also used : FindingLimits(com.google.privacy.dlp.v2.InspectConfig.FindingLimits) ByteContentItem(com.google.privacy.dlp.v2.ByteContentItem) ByteString(com.google.protobuf.ByteString) InspectContentRequest(com.google.privacy.dlp.v2.InspectContentRequest) InspectConfig(com.google.privacy.dlp.v2.InspectConfig) ParseException(org.apache.commons.cli.ParseException) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) InspectContentResponse(com.google.privacy.dlp.v2.InspectContentResponse) Finding(com.google.privacy.dlp.v2.Finding) InspectResult(com.google.privacy.dlp.v2.InspectResult) ByteContentItem(com.google.privacy.dlp.v2.ByteContentItem) ContentItem(com.google.privacy.dlp.v2.ContentItem)

Aggregations

DlpServiceClient (com.google.cloud.dlp.v2.DlpServiceClient)3 ByteContentItem (com.google.privacy.dlp.v2.ByteContentItem)3 ContentItem (com.google.privacy.dlp.v2.ContentItem)3 Finding (com.google.privacy.dlp.v2.Finding)3 InspectConfig (com.google.privacy.dlp.v2.InspectConfig)3 InspectContentRequest (com.google.privacy.dlp.v2.InspectContentRequest)3 InspectContentResponse (com.google.privacy.dlp.v2.InspectContentResponse)3 FindingLimits (com.google.privacy.dlp.v2.InspectConfig.FindingLimits)2 InspectResult (com.google.privacy.dlp.v2.InspectResult)2 ByteString (com.google.protobuf.ByteString)2 ParseException (org.apache.commons.cli.ParseException)2 InfoType (com.google.privacy.dlp.v2.InfoType)1 Likelihood (com.google.privacy.dlp.v2.Likelihood)1