Skip to content

Commit

Permalink
Seperate ByteType into two different samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtisvg committed Nov 7, 2018
1 parent 89cec0b commit f233ede
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 6 deletions.
93 changes: 93 additions & 0 deletions dlp/src/main/java/dlp/snippets/InspectImageFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2018 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_image_file]
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.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.ProjectName;
import com.google.protobuf.ByteString;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

public class InspectImageFile {

// Inspects the specified image file.
public static void inspectImageFile(String projectId, String filePath) {
// String projectId = "my-project-id";
// String filePath = "path/to/image.png";
// String fileType = "IMAGE"

// Initialize client with try-with-resources for automatic cleanup of background resources
try (DlpServiceClient dlp = DlpServiceClient.create()) {
// Set project for request
ProjectName project = ProjectName.of(projectId);

// Set content for request
ByteString fileBytes = ByteString.readFrom(new FileInputStream(filePath));
ByteContentItem byteItem = ByteContentItem.newBuilder()
.setType(BytesType.IMAGE)
.setData(fileBytes)
.build();
ContentItem item = ContentItem.newBuilder()
.setByteItem(byteItem)
.build();

// Set required InfoTypes for inspection config
List<InfoType> infoTypes = new ArrayList<>();
// See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types
for (String typeName : new String[] {"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) {
infoTypes.add(InfoType.newBuilder().setName(typeName).build());
}

// Set the inspect configuration for request
InspectConfig config = InspectConfig.newBuilder()
.addAllInfoTypes(infoTypes)
.setIncludeQuote(true)
.build();

// Construct the request to be sent by the client
InspectContentRequest request = InspectContentRequest.newBuilder()
.setParent(project.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 inspectFile: \n" + e.toString());
}
}
}
// [END dlp_inspect_image_file]
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
import java.util.ArrayList;
import java.util.List;

public class InspectFile {
public class InspectTextFile {

// Inspects the specified file.
public static void inspectFile(String projectId, String filePath, String fileType) {
// Inspects the specified text file.
public static void inspectTextFile(String projectId, String filePath) {
// String projectId = "my-project-id";
// String filePath = "path/to/image.png";
// String fileType = "IMAGE"
Expand All @@ -48,7 +48,7 @@ public static void inspectFile(String projectId, String filePath, String fileTyp
// Set content for request
ByteString fileBytes = ByteString.readFrom(new FileInputStream(filePath));
ByteContentItem byteItem = ByteContentItem.newBuilder()
.setType(BytesType.valueOf(fileType))
.setType(BytesType.TEXT_UTF8)
.setData(fileBytes)
.build();
ContentItem item = ContentItem.newBuilder()
Expand Down
14 changes: 12 additions & 2 deletions dlp/src/test/java/dlp/snippets/InspectTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,18 @@ public void testInspectString() {
}

@Test
public void testInspectFile() {
InspectFile.inspectFile(PROJECT_ID, "src/test/resources/test.png", "IMAGE");
public void textInspectTestFile() {
InspectTextFile.inspectTextFile(PROJECT_ID, "src/test/resources/test.txt");

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


@Test
public void testInspectImageFile() {
InspectImageFile.inspectImageFile(PROJECT_ID, "src/test/resources/test.png");

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

0 comments on commit f233ede

Please sign in to comment.