Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Java code sample for table inspection #3062

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions dlp/src/main/java/dlp/snippets/InspectTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dlp.snippets;

// [START dlp_inspect_table]

import com.google.cloud.dlp.v2.DlpServiceClient;
import com.google.privacy.dlp.v2.ByteContentItem;
import com.google.privacy.dlp.v2.ByteContentItem.BytesType;
import com.google.privacy.dlp.v2.ContentItem;
import com.google.privacy.dlp.v2.FieldId;
import com.google.privacy.dlp.v2.Finding;
import com.google.privacy.dlp.v2.InfoType;
import com.google.privacy.dlp.v2.InspectConfig;
import com.google.privacy.dlp.v2.InspectContentRequest;
import com.google.privacy.dlp.v2.InspectContentResponse;
import com.google.privacy.dlp.v2.Likelihood;
import com.google.privacy.dlp.v2.LocationName;
import com.google.privacy.dlp.v2.Table;
import com.google.privacy.dlp.v2.Table.Row;
import com.google.privacy.dlp.v2.Value;
import com.google.protobuf.ByteString;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class InspectTable {

public static void inspectTable() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "your-project-id";
Table tableToInspect = Table.newBuilder()
.addHeaders(FieldId.newBuilder().setName("name").build())
.addHeaders(FieldId.newBuilder().setName("phone").build())
.addRows(Row.newBuilder()
.addValues(Value.newBuilder().setStringValue("John Doe").build())
.addValues(Value.newBuilder().setStringValue("(206) 555-0123").build()))
.build();

inspectTable(projectId, tableToInspect);
}

// Inspects the provided text.
public static void inspectTable(String projectId, Table tableToInspect) {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (DlpServiceClient dlp = DlpServiceClient.create()) {
// Specify the table to be inspected.
ContentItem item = ContentItem.newBuilder().setTable(tableToInspect).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("PHONE_NUMBER").build();

// Construct the configuration for the Inspect request.
InspectConfig config =
InspectConfig.newBuilder()
.addInfoTypes(infoType)
.setIncludeQuote(true)
.build();

// Construct the Inspect request to be sent by the client.
InspectContentRequest request =
InspectContentRequest.newBuilder()
.setParent(LocationName.of(projectId, "global").toString())
.setItem(item)
.setInspectConfig(config)
.build();

// Use the client to send the API request.
InspectContentResponse response = dlp.inspectContent(request);

// Parse the response and process results
System.out.println("Findings: " + response.getResult().getFindingsCount());
for (Finding f : response.getResult().getFindingsList()) {
System.out.println("\tQuote: " + f.getQuote());
System.out.println("\tInfo type: " + f.getInfoType().getName());
System.out.println("\tLikelihood: " + f.getLikelihood());
}
} catch (Exception e) {
System.out.println("Error during inspectString: \n" + e.toString());
}
}
}
// [END dlp_inspect_table]
19 changes: 19 additions & 0 deletions dlp/src/test/java/dlp/snippets/InspectTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
import com.google.api.gax.rpc.ApiException;
import com.google.cloud.pubsub.v1.SubscriptionAdminClient;
import com.google.cloud.pubsub.v1.TopicAdminClient;
import com.google.privacy.dlp.v2.FieldId;
import com.google.privacy.dlp.v2.Table;
import com.google.privacy.dlp.v2.Table.Row;
import com.google.privacy.dlp.v2.Value;
import com.google.pubsub.v1.ProjectSubscriptionName;
import com.google.pubsub.v1.PushConfig;
import com.google.pubsub.v1.TopicName;
Expand Down Expand Up @@ -212,6 +216,21 @@ public void testInspectStringWithoutOverlap() throws Exception {
assertThat(output, containsString("example.com"));
assertThat(output, not(containsString("example.org")));
}

@Test
public void testInspectTable() {
Table tableToInspect = Table.newBuilder()
.addHeaders(FieldId.newBuilder().setName("name").build())
.addHeaders(FieldId.newBuilder().setName("phone").build())
.addRows(Row.newBuilder()
.addValues(Value.newBuilder().setStringValue("John Doe").build())
.addValues(Value.newBuilder().setStringValue("(206) 555-0123").build()))
.build();
InspectTable.inspectTable(PROJECT_ID, tableToInspect);

String output = bout.toString();
assertThat(output, containsString("Info type: PHONE_NUMBER"));
}

@Test
public void testInspectStringCustomHotword() throws Exception {
Expand Down