Skip to content

Commit eb0e6d2

Browse files
author
Ace Nassri
committed
Address PR feedback
1 parent d11d705 commit eb0e6d2

File tree

11 files changed

+288
-156
lines changed

11 files changed

+288
-156
lines changed

dlp/src/main/java/com/example/dlp/DeIdentification.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ private static void deIdentifyWithFpe(
161161

162162
ByteContentItem byteContentItem =
163163
ByteContentItem.newBuilder()
164-
.setType(ByteContentItem.BytesType.TEXT_UTF8)
165164
.setData(ByteString.copyFrom(string, StandardCharsets.UTF_8))
166165
.build();
167166

@@ -224,8 +223,8 @@ private static void deIdentifyWithFpe(
224223
}
225224
// [END dlp_deidentify_fpe]
226225

226+
// [START dlp_deidentify_date_shift]
227227
/**
228-
* [START dlp_deidentify_date_shift]
229228
*
230229
* @param inputCsvPath The path to the CSV file to deidentify
231230
* @param outputCsvPath (Optional) path to the output CSV file
@@ -340,7 +339,6 @@ private static void deidentifyWithDateShift(
340339

341340
File outputFile = outputCsvPath.toFile();
342341
if (!outputFile.exists()) {
343-
outputFile.getParentFile().mkdirs();
344342
outputFile.createNewFile();
345343
}
346344
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(outputFile));

dlp/src/main/java/com/example/dlp/Inspect.java

Lines changed: 104 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.google.privacy.dlp.v2.InfoType;
3535
import com.google.privacy.dlp.v2.InfoTypeStats;
3636
import com.google.privacy.dlp.v2.InspectConfig;
37+
import com.google.privacy.dlp.v2.InspectConfig.FindingLimits;
3738
import com.google.privacy.dlp.v2.InspectContentRequest;
3839
import com.google.privacy.dlp.v2.InspectContentResponse;
3940
import com.google.privacy.dlp.v2.InspectDataSourceDetails;
@@ -65,6 +66,7 @@
6566
import org.apache.commons.cli.Options;
6667
import org.apache.commons.cli.ParseException;
6768

69+
6870
public class Inspect {
6971

7072
/**
@@ -86,8 +88,8 @@ private static void inspectString(
8688
String projectId) {
8789
// instantiate a client
8890
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();
9193
InspectConfig inspectConfig =
9294
InspectConfig.newBuilder()
9395
.addAllInfoTypes(infoTypes)
@@ -155,16 +157,23 @@ private static void inspectFile(
155157
mimeType = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(filePath);
156158
}
157159

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;
168177
}
169178

170179
byte[] data = Files.readAllBytes(Paths.get(filePath));
@@ -174,8 +183,8 @@ private static void inspectFile(
174183
.build();
175184
ContentItem contentItem = ContentItem.newBuilder().setByteItem(byteContentItem).build();
176185

177-
InspectConfig.FindingLimits findingLimits =
178-
InspectConfig.FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
186+
FindingLimits findingLimits =
187+
FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
179188

180189
InspectConfig inspectConfig =
181190
InspectConfig.newBuilder()
@@ -253,8 +262,8 @@ private static void inspectGcsFile(
253262
StorageConfig storageConfig =
254263
StorageConfig.newBuilder().setCloudStorageOptions(cloudStorageOptions).build();
255264

256-
InspectConfig.FindingLimits findingLimits =
257-
InspectConfig.FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
265+
FindingLimits findingLimits =
266+
FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
258267

259268
InspectConfig inspectConfig =
260269
InspectConfig.newBuilder()
@@ -287,7 +296,30 @@ private static void inspectGcsFile(
287296

288297
System.out.println("Job created with ID:" + dlpJob.getName());
289298

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+
}
291323

292324
DlpJob completedJob =
293325
dlpServiceClient.getDlpJob(
@@ -309,36 +341,6 @@ private static void inspectGcsFile(
309341
// [END dlp_inspect_gcs]
310342
}
311343

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-
342344
// [START dlp_inspect_datastore]
343345
/**
344346
* Inspect a Datastore kind
@@ -378,8 +380,8 @@ private static void inspectDatastore(
378380
StorageConfig storageConfig =
379381
StorageConfig.newBuilder().setDatastoreOptions(datastoreOptions).build();
380382

381-
InspectConfig.FindingLimits findingLimits =
382-
InspectConfig.FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
383+
FindingLimits findingLimits =
384+
FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
383385

384386
InspectConfig inspectConfig =
385387
InspectConfig.newBuilder()
@@ -411,8 +413,32 @@ private static void inspectDatastore(
411413
DlpJob dlpJob = dlpServiceClient.createDlpJob(createDlpJobRequest);
412414

413415
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+
416442

417443
DlpJob completedJob =
418444
dlpServiceClient.getDlpJob(
@@ -473,8 +499,8 @@ private static void inspectBigquery(
473499
StorageConfig storageConfig =
474500
StorageConfig.newBuilder().setBigQueryOptions(bigQueryOptions).build();
475501

476-
InspectConfig.FindingLimits findingLimits =
477-
InspectConfig.FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
502+
FindingLimits findingLimits =
503+
FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
478504

479505
InspectConfig inspectConfig =
480506
InspectConfig.newBuilder()
@@ -509,8 +535,31 @@ private static void inspectBigquery(
509535

510536
System.out.println("Job created with ID:" + dlpJob.getName());
511537

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+
514563

515564
DlpJob completedJob =
516565
dlpServiceClient.getDlpJob(

dlp/src/main/java/com/example/dlp/QuickStart.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ public class QuickStart {
3939
public static void main(String[] args) throws Exception {
4040

4141
// string to inspect
42-
String text = "Robert Frost";
42+
String text = "His name was Robert Frost";
4343

4444
// The minimum likelihood required before returning a match:
4545
// LIKELIHOOD_UNSPECIFIED, VERY_UNLIKELY, UNLIKELY, POSSIBLE, LIKELY, VERY_LIKELY, UNRECOGNIZED
46-
Likelihood minLikelihood = Likelihood.VERY_LIKELY;
46+
Likelihood minLikelihood = Likelihood.POSSIBLE;
4747

4848
// The maximum number of findings to report (0 = server maximum)
4949
int maxFindings = 0;

dlp/src/main/java/com/example/dlp/Redact.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,24 @@ private static void redactImage(
6868
if (mimeType == null) {
6969
mimeType = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(filePath);
7070
}
71-
ByteContentItem.BytesType bytesType = ByteContentItem.BytesType.BYTES_TYPE_UNSPECIFIED;
72-
73-
if (mimeType.equals("image/jpeg")) {
74-
bytesType = ByteContentItem.BytesType.IMAGE_JPEG;
75-
} else if (mimeType.equals("image/bmp")) {
76-
bytesType = ByteContentItem.BytesType.IMAGE_BMP;
77-
} else if (mimeType.equals("image/png")) {
78-
bytesType = ByteContentItem.BytesType.IMAGE_PNG;
79-
} else if (mimeType.equals("image/svg")) {
80-
bytesType = ByteContentItem.BytesType.IMAGE_SVG;
71+
72+
ByteContentItem.BytesType bytesType;
73+
switch (mimeType) {
74+
case "image/jpeg":
75+
bytesType = ByteContentItem.BytesType.IMAGE_JPEG;
76+
break;
77+
case "image/bmp":
78+
bytesType = ByteContentItem.BytesType.IMAGE_BMP;
79+
break;
80+
case "image/png":
81+
bytesType = ByteContentItem.BytesType.IMAGE_PNG;
82+
break;
83+
case "image/svg":
84+
bytesType = ByteContentItem.BytesType.IMAGE_SVG;
85+
break;
86+
default:
87+
bytesType = ByteContentItem.BytesType.BYTES_TYPE_UNSPECIFIED;
88+
break;
8189
}
8290

8391
byte[] data = Files.readAllBytes(Paths.get(filePath));

0 commit comments

Comments
 (0)