Skip to content

Commit 93bb351

Browse files
Sita04anguillanneuf
authored andcommitted
added sample
1 parent ca5d664 commit 93bb351

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2020 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+
// [START securitycenter_receive_notifications]
18+
19+
import com.google.cloud.pubsub.v1.AckReplyConsumer;
20+
import com.google.cloud.pubsub.v1.MessageReceiver;
21+
import com.google.cloud.pubsub.v1.Subscriber;
22+
import com.google.cloud.securitycenter.v1.NotificationMessage;
23+
import com.google.protobuf.InvalidProtocolBufferException;
24+
import com.google.protobuf.util.JsonFormat;
25+
import com.google.pubsub.v1.ProjectSubscriptionName;
26+
import com.google.pubsub.v1.PubsubMessage;
27+
import java.util.concurrent.TimeUnit;
28+
import java.util.concurrent.TimeoutException;
29+
30+
public class NotificationReceiver {
31+
32+
private NotificationReceiver() {
33+
}
34+
35+
public static void receiveNotificationMessages(String projectId, String subscriptionId) {
36+
// String projectId = "{your-project}";
37+
// String subscriptionId = "{your-subscription}";
38+
ProjectSubscriptionName subscriptionName =
39+
ProjectSubscriptionName.of(projectId, subscriptionId);
40+
41+
try {
42+
Subscriber subscriber =
43+
Subscriber.newBuilder(subscriptionName, new NotificationMessageReceiver()).build();
44+
subscriber.startAsync().awaitRunning();
45+
46+
// This sets the timeout value of the subscriber to 10s.
47+
subscriber.awaitTerminated(10_000, TimeUnit.MILLISECONDS);
48+
} catch (IllegalStateException | TimeoutException e) {
49+
System.out.println("Subscriber stopped: " + e);
50+
}
51+
}
52+
53+
static class NotificationMessageReceiver implements MessageReceiver {
54+
55+
@Override
56+
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
57+
NotificationMessage.Builder notificationMessageBuilder = NotificationMessage.newBuilder();
58+
59+
try {
60+
String jsonString = message.getData().toStringUtf8();
61+
JsonFormat.parser().merge(jsonString, notificationMessageBuilder);
62+
63+
NotificationMessage notificationMessage = notificationMessageBuilder.build();
64+
System.out.println(
65+
String.format("Config id: %s", notificationMessage.getNotificationConfigName()));
66+
System.out.println(String.format("Finding: %s", notificationMessage.getFinding()));
67+
} catch (InvalidProtocolBufferException e) {
68+
System.out.println("Could not parse message: " + e);
69+
} finally {
70+
consumer.ack();
71+
}
72+
}
73+
}
74+
}
75+
// [END securitycenter_receive_notifications]

0 commit comments

Comments
 (0)