|
72 | 72 |
|
73 | 73 | public class Inspect { |
74 | 74 |
|
75 | | - /** |
76 | | - * [START dlp_inspect_string] Inspect a text for given InfoTypes |
77 | | - * |
78 | | - * @param string String to instpect |
79 | | - * @param minLikelihood The minimum likelihood required before returning a match |
80 | | - * @param maxFindings The maximum number of findings to report (0 = server maximum) |
81 | | - * @param infoTypes The infoTypes of information to match |
82 | | - * @param includeQuote Whether to include the matching string |
83 | | - * @param projectId Google Cloud project ID |
84 | | - */ |
85 | | - private static void inspectString( |
86 | | - String string, |
87 | | - Likelihood minLikelihood, |
88 | | - int maxFindings, |
89 | | - List<InfoType> infoTypes, |
90 | | - List<CustomInfoType> customInfoTypes, |
91 | | - boolean includeQuote, |
92 | | - String projectId) { |
93 | | - // instantiate a client |
94 | | - try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) { |
95 | | - FindingLimits findingLimits = |
96 | | - FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build(); |
97 | | - InspectConfig inspectConfig = |
98 | | - InspectConfig.newBuilder() |
99 | | - .addAllInfoTypes(infoTypes) |
100 | | - .addAllCustomInfoTypes(customInfoTypes) |
101 | | - .setMinLikelihood(minLikelihood) |
102 | | - .setLimits(findingLimits) |
103 | | - .setIncludeQuote(includeQuote) |
104 | | - .build(); |
105 | | - |
106 | | - ByteContentItem byteContentItem = |
107 | | - ByteContentItem.newBuilder() |
108 | | - .setType(ByteContentItem.BytesType.TEXT_UTF8) |
109 | | - .setData(ByteString.copyFromUtf8(string)) |
110 | | - .build(); |
111 | | - |
112 | | - ContentItem contentItem = ContentItem.newBuilder().setByteItem(byteContentItem).build(); |
113 | | - |
114 | | - InspectContentRequest request = |
115 | | - InspectContentRequest.newBuilder() |
116 | | - .setParent(ProjectName.of(projectId).toString()) |
117 | | - .setInspectConfig(inspectConfig) |
118 | | - .setItem(contentItem) |
119 | | - .build(); |
120 | | - InspectContentResponse response = dlpServiceClient.inspectContent(request); |
121 | | - |
122 | | - if (response.getResult().getFindingsCount() > 0) { |
123 | | - System.out.println("Findings: "); |
124 | | - for (Finding finding : response.getResult().getFindingsList()) { |
125 | | - if (includeQuote) { |
126 | | - System.out.print("\tQuote: " + finding.getQuote()); |
127 | | - } |
128 | | - System.out.print("\tInfo type: " + finding.getInfoType().getName()); |
129 | | - System.out.println("\tLikelihood: " + finding.getLikelihood()); |
130 | | - } |
131 | | - } else { |
132 | | - System.out.println("No findings."); |
133 | | - } |
134 | | - } catch (Exception e) { |
135 | | - System.out.println("Error in inspectString: " + e.getMessage()); |
136 | | - } |
137 | | - } |
138 | | - // [END dlp_inspect_string] |
139 | | - |
140 | | - // [START dlp_inspect_file] |
141 | | - /** |
142 | | - * Inspect a local file |
143 | | - * |
144 | | - * @param filePath The path to a local file to inspect. Can be a text, JPG, or PNG file. |
145 | | - * @param minLikelihood The minimum likelihood required before returning a match |
146 | | - * @param maxFindings The maximum number of findings to report (0 = server maximum) |
147 | | - * @param infoTypes The infoTypes of information to match |
148 | | - * @param includeQuote Whether to include the matching string |
149 | | - * @param projectId Google Cloud project ID |
150 | | - */ |
151 | | - private static void inspectFile( |
152 | | - String filePath, |
153 | | - Likelihood minLikelihood, |
154 | | - int maxFindings, |
155 | | - List<InfoType> infoTypes, |
156 | | - List<CustomInfoType> customInfoTypes, |
157 | | - boolean includeQuote, |
158 | | - String projectId) { |
159 | | - // Instantiates a client |
160 | | - try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) { |
161 | | - // detect file mime type, default to application/octet-stream |
162 | | - String mimeType = URLConnection.guessContentTypeFromName(filePath); |
163 | | - if (mimeType == null) { |
164 | | - mimeType = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(filePath); |
165 | | - } |
166 | | - |
167 | | - ByteContentItem.BytesType bytesType; |
168 | | - switch (mimeType) { |
169 | | - case "image/jpeg": |
170 | | - bytesType = ByteContentItem.BytesType.IMAGE_JPEG; |
171 | | - break; |
172 | | - case "image/bmp": |
173 | | - bytesType = ByteContentItem.BytesType.IMAGE_BMP; |
174 | | - break; |
175 | | - case "image/png": |
176 | | - bytesType = ByteContentItem.BytesType.IMAGE_PNG; |
177 | | - break; |
178 | | - case "image/svg": |
179 | | - bytesType = ByteContentItem.BytesType.IMAGE_SVG; |
180 | | - break; |
181 | | - default: |
182 | | - bytesType = ByteContentItem.BytesType.BYTES_TYPE_UNSPECIFIED; |
183 | | - break; |
184 | | - } |
185 | | - |
186 | | - byte[] data = Files.readAllBytes(Paths.get(filePath)); |
187 | | - ByteContentItem byteContentItem = |
188 | | - ByteContentItem.newBuilder() |
189 | | - .setType(bytesType) |
190 | | - .setData(ByteString.copyFrom(data)) |
191 | | - .build(); |
192 | | - ContentItem contentItem = ContentItem.newBuilder().setByteItem(byteContentItem).build(); |
193 | | - |
194 | | - FindingLimits findingLimits = |
195 | | - FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build(); |
196 | | - |
197 | | - InspectConfig inspectConfig = |
198 | | - InspectConfig.newBuilder() |
199 | | - .addAllInfoTypes(infoTypes) |
200 | | - .addAllCustomInfoTypes(customInfoTypes) |
201 | | - .setMinLikelihood(minLikelihood) |
202 | | - .setLimits(findingLimits) |
203 | | - .setIncludeQuote(includeQuote) |
204 | | - .build(); |
205 | | - |
206 | | - InspectContentRequest request = |
207 | | - InspectContentRequest.newBuilder() |
208 | | - .setParent(ProjectName.of(projectId).toString()) |
209 | | - .setInspectConfig(inspectConfig) |
210 | | - .setItem(contentItem) |
211 | | - .build(); |
212 | | - |
213 | | - InspectContentResponse response = dlpServiceClient.inspectContent(request); |
214 | | - |
215 | | - InspectResult result = response.getResult(); |
216 | | - if (result.getFindingsCount() > 0) { |
217 | | - System.out.println("Findings: "); |
218 | | - for (Finding finding : result.getFindingsList()) { |
219 | | - if (includeQuote) { |
220 | | - System.out.print("\tQuote: " + finding.getQuote()); |
221 | | - } |
222 | | - System.out.print("\tInfo type: " + finding.getInfoType().getName()); |
223 | | - System.out.println("\tLikelihood: " + finding.getLikelihood()); |
224 | | - } |
225 | | - } else { |
226 | | - System.out.println("No findings."); |
227 | | - } |
228 | | - } catch (Exception e) { |
229 | | - System.out.println("Error in inspectFile: " + e.getMessage()); |
230 | | - } |
231 | | - } |
232 | | - // [END dlp_inspect_file] |
233 | | - |
234 | 75 | // [START dlp_inspect_gcs] |
235 | 76 | /** |
236 | 77 | * Inspect GCS file for Info types and wait on job completion using Google Cloud Pub/Sub |
@@ -756,28 +597,7 @@ public static void main(String[] args) throws Exception { |
756 | 597 | } |
757 | 598 |
|
758 | 599 | // string inspection |
759 | | - if (cmd.hasOption("s")) { |
760 | | - String val = cmd.getOptionValue(stringOption.getOpt()); |
761 | | - inspectString( |
762 | | - val, |
763 | | - minLikelihood, |
764 | | - maxFindings, |
765 | | - infoTypesList, |
766 | | - customInfoTypesList, |
767 | | - includeQuote, |
768 | | - projectId); |
769 | | - } else if (cmd.hasOption("f")) { |
770 | | - String filePath = cmd.getOptionValue(fileOption.getOpt()); |
771 | | - inspectFile( |
772 | | - filePath, |
773 | | - minLikelihood, |
774 | | - maxFindings, |
775 | | - infoTypesList, |
776 | | - customInfoTypesList, |
777 | | - includeQuote, |
778 | | - projectId); |
779 | | - // gcs file inspection |
780 | | - } else if (cmd.hasOption("gcs")) { |
| 600 | + if (cmd.hasOption("gcs")) { |
781 | 601 | String bucketName = cmd.getOptionValue(bucketNameOption.getOpt()); |
782 | 602 | String fileName = cmd.getOptionValue(gcsFileNameOption.getOpt()); |
783 | 603 | inspectGcsFile( |
|
0 commit comments