34
34
import com .google .privacy .dlp .v2 .InfoType ;
35
35
import com .google .privacy .dlp .v2 .InfoTypeStats ;
36
36
import com .google .privacy .dlp .v2 .InspectConfig ;
37
+ import com .google .privacy .dlp .v2 .InspectConfig .FindingLimits ;
37
38
import com .google .privacy .dlp .v2 .InspectContentRequest ;
38
39
import com .google .privacy .dlp .v2 .InspectContentResponse ;
39
40
import com .google .privacy .dlp .v2 .InspectDataSourceDetails ;
65
66
import org .apache .commons .cli .Options ;
66
67
import org .apache .commons .cli .ParseException ;
67
68
69
+
68
70
public class Inspect {
69
71
70
72
/**
@@ -86,8 +88,8 @@ private static void inspectString(
86
88
String projectId ) {
87
89
// instantiate a client
88
90
try (DlpServiceClient dlpServiceClient = DlpServiceClient .create ()) {
89
- InspectConfig . FindingLimits findingLimits =
90
- InspectConfig . FindingLimits .newBuilder ().setMaxFindingsPerRequest (maxFindings ).build ();
91
+ FindingLimits findingLimits =
92
+ FindingLimits .newBuilder ().setMaxFindingsPerRequest (maxFindings ).build ();
91
93
InspectConfig inspectConfig =
92
94
InspectConfig .newBuilder ()
93
95
.addAllInfoTypes (infoTypes )
@@ -155,16 +157,23 @@ private static void inspectFile(
155
157
mimeType = MimetypesFileTypeMap .getDefaultFileTypeMap ().getContentType (filePath );
156
158
}
157
159
158
- ByteContentItem .BytesType bytesType = ByteContentItem .BytesType .BYTES_TYPE_UNSPECIFIED ;
159
-
160
- if (mimeType .equals ("image/jpeg" )) {
161
- bytesType = ByteContentItem .BytesType .IMAGE_JPEG ;
162
- } else if (mimeType .equals ("image/bmp" )) {
163
- bytesType = ByteContentItem .BytesType .IMAGE_BMP ;
164
- } else if (mimeType .equals ("image/png" )) {
165
- bytesType = ByteContentItem .BytesType .IMAGE_PNG ;
166
- } else if (mimeType .equals ("image/svg" )) {
167
- bytesType = ByteContentItem .BytesType .IMAGE_SVG ;
160
+ ByteContentItem .BytesType bytesType ;
161
+ switch (mimeType ) {
162
+ case "image/jpeg" :
163
+ bytesType = ByteContentItem .BytesType .IMAGE_JPEG ;
164
+ break ;
165
+ case "image/bmp" :
166
+ bytesType = ByteContentItem .BytesType .IMAGE_BMP ;
167
+ break ;
168
+ case "image/png" :
169
+ bytesType = ByteContentItem .BytesType .IMAGE_PNG ;
170
+ break ;
171
+ case "image/svg" :
172
+ bytesType = ByteContentItem .BytesType .IMAGE_SVG ;
173
+ break ;
174
+ default :
175
+ bytesType = ByteContentItem .BytesType .BYTES_TYPE_UNSPECIFIED ;
176
+ break ;
168
177
}
169
178
170
179
byte [] data = Files .readAllBytes (Paths .get (filePath ));
@@ -174,8 +183,8 @@ private static void inspectFile(
174
183
.build ();
175
184
ContentItem contentItem = ContentItem .newBuilder ().setByteItem (byteContentItem ).build ();
176
185
177
- InspectConfig . FindingLimits findingLimits =
178
- InspectConfig . FindingLimits .newBuilder ().setMaxFindingsPerRequest (maxFindings ).build ();
186
+ FindingLimits findingLimits =
187
+ FindingLimits .newBuilder ().setMaxFindingsPerRequest (maxFindings ).build ();
179
188
180
189
InspectConfig inspectConfig =
181
190
InspectConfig .newBuilder ()
@@ -253,8 +262,8 @@ private static void inspectGcsFile(
253
262
StorageConfig storageConfig =
254
263
StorageConfig .newBuilder ().setCloudStorageOptions (cloudStorageOptions ).build ();
255
264
256
- InspectConfig . FindingLimits findingLimits =
257
- InspectConfig . FindingLimits .newBuilder ().setMaxFindingsPerRequest (maxFindings ).build ();
265
+ FindingLimits findingLimits =
266
+ FindingLimits .newBuilder ().setMaxFindingsPerRequest (maxFindings ).build ();
258
267
259
268
InspectConfig inspectConfig =
260
269
InspectConfig .newBuilder ()
@@ -287,7 +296,30 @@ private static void inspectGcsFile(
287
296
288
297
System .out .println ("Job created with ID:" + dlpJob .getName ());
289
298
290
- waitOnJobCompletion (projectId , subscriptionId , dlpJob .getName ());
299
+ final SettableApiFuture <Boolean > done = SettableApiFuture .create ();
300
+
301
+ // setup a Pub/Sub subscriber to listen on the job completion status
302
+ Subscriber subscriber =
303
+ Subscriber .newBuilder (
304
+ ProjectSubscriptionName .of (projectId , subscriptionId ),
305
+ (pubsubMessage , ackReplyConsumer ) -> {
306
+ if (pubsubMessage .getAttributesCount () > 0
307
+ && pubsubMessage .getAttributesMap ().get ("DlpJobName" ).equals (dlpJob .getName ())) {
308
+ // notify job completion
309
+ done .set (true );
310
+ ackReplyConsumer .ack ();
311
+ }
312
+ })
313
+ .build ();
314
+ subscriber .startAsync ();
315
+
316
+ // wait for job completion
317
+ try {
318
+ done .get (1 , TimeUnit .MINUTES );
319
+ Thread .sleep (500 );
320
+ } catch (Exception e ){
321
+ System .out .println ("Unable to verify job completion." );
322
+ }
291
323
292
324
DlpJob completedJob =
293
325
dlpServiceClient .getDlpJob (
@@ -309,36 +341,6 @@ private static void inspectGcsFile(
309
341
// [END dlp_inspect_gcs]
310
342
}
311
343
312
- // [START wait_on_dlp_job_completion]
313
- // wait on receiving a job status update over a Google Cloud Pub/Sub subscriber
314
- private static void waitOnJobCompletion (
315
- String projectId , String subscriptionId , String dlpJobName ) throws Exception {
316
- // wait for job completion
317
- final SettableApiFuture <Boolean > done = SettableApiFuture .create ();
318
-
319
- // setup a Pub/Sub subscriber to listen on the job completion status
320
- Subscriber subscriber =
321
- Subscriber .newBuilder (
322
- ProjectSubscriptionName .of (projectId , subscriptionId ),
323
- (pubsubMessage , ackReplyConsumer ) -> {
324
- if (pubsubMessage .getAttributesCount () > 0
325
- && pubsubMessage .getAttributesMap ().get ("DlpJobName" ).equals (dlpJobName )) {
326
- // notify job completion
327
- done .set (true );
328
- ackReplyConsumer .ack ();
329
- }
330
- })
331
- .build ();
332
- subscriber .startAsync ();
333
- // wait for job completion
334
- try {
335
- done .get (30 , TimeUnit .SECONDS );
336
- } catch (Exception e ){
337
- System .out .println ("Unable to verify job completion." );
338
- }
339
- }
340
- // [END wait_on_dlp_job_completion]
341
-
342
344
// [START dlp_inspect_datastore]
343
345
/**
344
346
* Inspect a Datastore kind
@@ -378,8 +380,8 @@ private static void inspectDatastore(
378
380
StorageConfig storageConfig =
379
381
StorageConfig .newBuilder ().setDatastoreOptions (datastoreOptions ).build ();
380
382
381
- InspectConfig . FindingLimits findingLimits =
382
- InspectConfig . FindingLimits .newBuilder ().setMaxFindingsPerRequest (maxFindings ).build ();
383
+ FindingLimits findingLimits =
384
+ FindingLimits .newBuilder ().setMaxFindingsPerRequest (maxFindings ).build ();
383
385
384
386
InspectConfig inspectConfig =
385
387
InspectConfig .newBuilder ()
@@ -411,8 +413,32 @@ private static void inspectDatastore(
411
413
DlpJob dlpJob = dlpServiceClient .createDlpJob (createDlpJobRequest );
412
414
413
415
System .out .println ("Job created with ID:" + dlpJob .getName ());
414
- // asynchronously submit an inspect job, and wait on results
415
- waitOnJobCompletion (projectId , subscriptionId , dlpJob .getName ());
416
+
417
+ final SettableApiFuture <Boolean > done = SettableApiFuture .create ();
418
+
419
+ // setup a Pub/Sub subscriber to listen on the job completion status
420
+ Subscriber subscriber =
421
+ Subscriber .newBuilder (
422
+ ProjectSubscriptionName .of (projectId , subscriptionId ),
423
+ (pubsubMessage , ackReplyConsumer ) -> {
424
+ if (pubsubMessage .getAttributesCount () > 0
425
+ && pubsubMessage .getAttributesMap ().get ("DlpJobName" ).equals (dlpJob .getName ())) {
426
+ // notify job completion
427
+ done .set (true );
428
+ ackReplyConsumer .ack ();
429
+ }
430
+ })
431
+ .build ();
432
+ subscriber .startAsync ();
433
+
434
+ // wait for job completion
435
+ try {
436
+ done .get (1 , TimeUnit .MINUTES );
437
+ Thread .sleep (500 );
438
+ } catch (Exception e ){
439
+ System .out .println ("Unable to verify job completion." );
440
+ }
441
+
416
442
417
443
DlpJob completedJob =
418
444
dlpServiceClient .getDlpJob (
@@ -473,8 +499,8 @@ private static void inspectBigquery(
473
499
StorageConfig storageConfig =
474
500
StorageConfig .newBuilder ().setBigQueryOptions (bigQueryOptions ).build ();
475
501
476
- InspectConfig . FindingLimits findingLimits =
477
- InspectConfig . FindingLimits .newBuilder ().setMaxFindingsPerRequest (maxFindings ).build ();
502
+ FindingLimits findingLimits =
503
+ FindingLimits .newBuilder ().setMaxFindingsPerRequest (maxFindings ).build ();
478
504
479
505
InspectConfig inspectConfig =
480
506
InspectConfig .newBuilder ()
@@ -509,8 +535,31 @@ private static void inspectBigquery(
509
535
510
536
System .out .println ("Job created with ID:" + dlpJob .getName ());
511
537
512
- // wait on completion
513
- waitOnJobCompletion (projectId , subscriptionId , dlpJob .getName ());
538
+ // wait on job completion
539
+ final SettableApiFuture <Boolean > done = SettableApiFuture .create ();
540
+
541
+ // setup a Pub/Sub subscriber to listen on the job completion status
542
+ Subscriber subscriber =
543
+ Subscriber .newBuilder (
544
+ ProjectSubscriptionName .of (projectId , subscriptionId ),
545
+ (pubsubMessage , ackReplyConsumer ) -> {
546
+ if (pubsubMessage .getAttributesCount () > 0
547
+ && pubsubMessage .getAttributesMap ().get ("DlpJobName" ).equals (dlpJob .getName ())) {
548
+ // notify job completion
549
+ done .set (true );
550
+ ackReplyConsumer .ack ();
551
+ }
552
+ })
553
+ .build ();
554
+ subscriber .startAsync ();
555
+
556
+ try {
557
+ done .get (1 , TimeUnit .MINUTES );
558
+ Thread .sleep (500 );
559
+ } catch (Exception e ){
560
+ System .out .println ("Unable to verify job completion." );
561
+ }
562
+
514
563
515
564
DlpJob completedJob =
516
565
dlpServiceClient .getDlpJob (
0 commit comments