Skip to content

Commit 52e25a4

Browse files
committed
Seperate ByteType into two different samples.
1 parent c4ba531 commit 52e25a4

File tree

3 files changed

+109
-6
lines changed

3 files changed

+109
-6
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2018 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_inspect_image_file]
20+
import com.google.cloud.dlp.v2.DlpServiceClient;
21+
import com.google.privacy.dlp.v2.ByteContentItem;
22+
import com.google.privacy.dlp.v2.ByteContentItem.BytesType;
23+
import com.google.privacy.dlp.v2.ContentItem;
24+
import com.google.privacy.dlp.v2.Finding;
25+
import com.google.privacy.dlp.v2.InfoType;
26+
import com.google.privacy.dlp.v2.InspectConfig;
27+
import com.google.privacy.dlp.v2.InspectContentRequest;
28+
import com.google.privacy.dlp.v2.InspectContentResponse;
29+
import com.google.privacy.dlp.v2.ProjectName;
30+
import com.google.protobuf.ByteString;
31+
import java.io.FileInputStream;
32+
import java.util.ArrayList;
33+
import java.util.List;
34+
35+
public class InspectImageFile {
36+
37+
// Inspects the specified image file.
38+
public static void inspectImageFile(String projectId, String filePath) {
39+
// String projectId = "my-project-id";
40+
// String filePath = "path/to/image.png";
41+
// String fileType = "IMAGE"
42+
43+
// Initialize client with try-with-resources for automatic cleanup of background resources
44+
try (DlpServiceClient dlp = DlpServiceClient.create()) {
45+
// Set project for request
46+
ProjectName project = ProjectName.of(projectId);
47+
48+
// Set content for request
49+
ByteString fileBytes = ByteString.readFrom(new FileInputStream(filePath));
50+
ByteContentItem byteItem = ByteContentItem.newBuilder()
51+
.setType(BytesType.IMAGE)
52+
.setData(fileBytes)
53+
.build();
54+
ContentItem item = ContentItem.newBuilder()
55+
.setByteItem(byteItem)
56+
.build();
57+
58+
// Set required InfoTypes for inspection config
59+
List<InfoType> infoTypes = new ArrayList<>();
60+
// See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types
61+
for (String typeName : new String[] {"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) {
62+
infoTypes.add(InfoType.newBuilder().setName(typeName).build());
63+
}
64+
65+
// Set the inspect configuration for request
66+
InspectConfig config = InspectConfig.newBuilder()
67+
.addAllInfoTypes(infoTypes)
68+
.setIncludeQuote(true)
69+
.build();
70+
71+
// Construct the request to be sent by the client
72+
InspectContentRequest request = InspectContentRequest.newBuilder()
73+
.setParent(project.toString())
74+
.setItem(item)
75+
.setInspectConfig(config)
76+
.build();
77+
78+
// Use the client to send the API request
79+
InspectContentResponse response = dlp.inspectContent(request);
80+
81+
// Parse the response and process results
82+
System.out.println("Findings: " + response.getResult().getFindingsCount());
83+
for (Finding f : response.getResult().getFindingsList()) {
84+
System.out.println("\tQuote: " + f.getQuote());
85+
System.out.println("\tInfo type: " + f.getInfoType().getName());
86+
System.out.println("\tLikelihood: " + f.getLikelihood());
87+
}
88+
} catch (Exception e) {
89+
System.out.println("Error during inspectFile: \n" + e.toString());
90+
}
91+
}
92+
}
93+
// [END dlp_inspect_image_file]

dlp/src/main/java/dlp/snippets/InspectFile.java renamed to dlp/src/main/java/dlp/snippets/InspectTextFile.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
import java.util.ArrayList;
3333
import java.util.List;
3434

35-
public class InspectFile {
35+
public class InspectTextFile {
3636

37-
// Inspects the specified file.
38-
public static void inspectFile(String projectId, String filePath, String fileType) {
37+
// Inspects the specified text file.
38+
public static void inspectTextFile(String projectId, String filePath) {
3939
// String projectId = "my-project-id";
4040
// String filePath = "path/to/image.png";
4141
// String fileType = "IMAGE"
@@ -48,7 +48,7 @@ public static void inspectFile(String projectId, String filePath, String fileTyp
4848
// Set content for request
4949
ByteString fileBytes = ByteString.readFrom(new FileInputStream(filePath));
5050
ByteContentItem byteItem = ByteContentItem.newBuilder()
51-
.setType(BytesType.valueOf(fileType))
51+
.setType(BytesType.TEXT_UTF8)
5252
.setData(fileBytes)
5353
.build();
5454
ContentItem item = ContentItem.newBuilder()

dlp/src/test/java/dlp/snippets/InspectTests.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,18 @@ public void testInspectString() {
7070
}
7171

7272
@Test
73-
public void testInspectFile() {
74-
InspectFile.inspectFile(PROJECT_ID, "src/test/resources/test.png", "IMAGE");
73+
public void textInspectTestFile() {
74+
InspectTextFile.inspectTextFile(PROJECT_ID, "src/test/resources/test.txt");
75+
76+
String output = bout.toString();
77+
assertThat(output, CoreMatchers.containsString("Info type: PHONE_NUMBER"));
78+
assertThat(output, CoreMatchers.containsString("Info type: EMAIL_ADDRESS"));
79+
}
80+
81+
82+
@Test
83+
public void testInspectImageFile() {
84+
InspectImageFile.inspectImageFile(PROJECT_ID, "src/test/resources/test.png");
7585

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

0 commit comments

Comments
 (0)