99import com .engflow .notification .v1 .PullNotificationRequest ;
1010import com .engflow .notification .v1 .PullNotificationResponse ;
1111import com .engflow .notification .v1 .QueueId ;
12+ import com .engflow .notificationqueue .demoserver .EngFlowRequest ;
13+ import com .engflow .notificationqueue .demoserver .EngFlowResponse ;
14+ import com .engflow .notificationqueue .demoserver .ForwardingGrpc ;
1215import com .google .common .base .Preconditions ;
1316import com .google .common .base .Strings ;
1417import com .google .protobuf .Any ;
1518import com .google .protobuf .InvalidProtocolBufferException ;
1619import io .grpc .ManagedChannel ;
1720import io .grpc .Metadata ;
21+ import io .grpc .StatusRuntimeException ;
1822import io .grpc .netty .GrpcSslContexts ;
1923import io .grpc .netty .NegotiationType ;
2024import io .grpc .netty .NettyChannelBuilder ;
@@ -38,7 +42,9 @@ public static void main(String[] args) throws Exception {
3842 System .err .println (e );
3943 System .err .println (" --notification_queue_endpoint" );
4044 System .err .println (" --queue_name" );
41- System .err .println ("Please provide also authentication credentials" );
45+ System .err .println (
46+ "Please provide also authentication credentials "
47+ + "and if you want to forward then add another external server endpoint" );
4248 return ;
4349 }
4450
@@ -62,17 +68,31 @@ public static void main(String[] args) throws Exception {
6268 throw new IllegalStateException (e );
6369 }
6470
71+ ManagedChannel forwardChannel = null ;
72+ if (!Strings .isNullOrEmpty (clientOptions .getOption ("forward" ))) {
73+ try {
74+ forwardChannel = createChannel (clientOptions .getOption ("forward" ), null , null );
75+ } catch (IllegalArgumentException e ) {
76+ System .err .println ("Could not open forwarding channel" );
77+ } catch (IllegalStateException e ) {
78+ System .err .println ("Could not open forwarding channel" );
79+ } catch (IOException e ) {
80+ System .err .println ("Could not open forwarding channel" );
81+ }
82+ }
6583 try {
6684 final Metadata header = new Metadata ();
6785 Metadata .Key <String > userKey =
6886 Metadata .Key .of ("Authorization" , Metadata .ASCII_STRING_MARSHALLER );
6987 header .put (userKey , "Bearer " + clientOptions .getOption ("token" ));
70- pull (channel , clientOptions .getOption ("queue_name" ), header );
88+ pull (channel , clientOptions .getOption ("queue_name" ), header , forwardChannel );
7189 } finally {
7290 if (channel != null ) {
7391 channel .shutdownNow ();
92+ forwardChannel .shutdownNow ();
7493 try {
7594 channel .awaitTermination (10 , TimeUnit .SECONDS );
95+ forwardChannel .awaitTermination (10 , TimeUnit .SECONDS );
7696 } catch (InterruptedException e ) {
7797 System .out .println ("Could not shut down channel within timeout" );
7898 }
@@ -89,18 +109,30 @@ public static void main(String[] args) throws Exception {
89109 * @param header metadata for token authentication (if needed)
90110 * @throws InterruptedException
91111 */
92- private static void pull (ManagedChannel channel , String queueName , Metadata header )
112+ private static void pull (
113+ ManagedChannel channel , String queueName , Metadata header , ManagedChannel forwardChannel )
93114 throws InterruptedException {
115+
94116 NotificationQueueGrpc .NotificationQueueStub asyncStub = NotificationQueueGrpc .newStub (channel );
95117 asyncStub = MetadataUtils .attachHeaders (asyncStub , header );
96118 final CountDownLatch finishLatch = new CountDownLatch (1 );
119+ System .out .println ("Listening for build events..." );
97120 StreamObserver <PullNotificationRequest > requestObserver =
98121 asyncStub .pull (
99122 new StreamObserver <PullNotificationResponse >() {
100123 @ Override
101124 public void onNext (PullNotificationResponse response ) {
102125 Notification streamedNotification = response .getNotification ().getNotification ();
103- System .out .println ("Notification: " + streamedNotification );
126+ System .out .println ("Notification: " + streamedNotification .toString ());
127+ try {
128+ /** Forward notification data to external server */
129+ forwardToBESStub (
130+ forwardChannel ,
131+ streamedNotification .getId ().toString (),
132+ streamedNotification .getPayload ().toString ());
133+ } catch (Exception e ) {
134+ System .err .println ("Could not forward notification to external sever..." );
135+ }
104136 Any notificationContent = streamedNotification .getPayload ();
105137 try {
106138 BuildLifecycleEventNotification lifeCycleEvent =
@@ -116,7 +148,7 @@ public void onNext(PullNotificationResponse response) {
116148 * Fetch the invocation using the grpc {@link EventStoreGrpc} stub using the
117149 * acquired invocation id
118150 */
119- getInvocations (channel , invocation , header );
151+ getInvocations (channel , invocation , header , forwardChannel );
120152 } catch (InterruptedException e ) {
121153 System .err .println ("Could not get invocation with uuid " + invocation );
122154 }
@@ -162,7 +194,8 @@ public void onCompleted() {
162194 * @param header metadata for token authentication (if needed)
163195 * @throws InterruptedException
164196 */
165- public static void getInvocations (ManagedChannel channel , String invocationId , Metadata header )
197+ private static void getInvocations (
198+ ManagedChannel channel , String invocationId , Metadata header , ManagedChannel forwardChannel )
166199 throws InterruptedException {
167200 EventStoreGrpc .EventStoreStub asyncStub = EventStoreGrpc .newStub (channel );
168201 asyncStub = MetadataUtils .attachHeaders (asyncStub , header );
@@ -172,6 +205,13 @@ public static void getInvocations(ManagedChannel channel, String invocationId, M
172205 @ Override
173206 public void onNext (StreamedBuildEvent response ) {
174207 System .out .println ("Invocation: " + response .toString ());
208+ String buildEvent = response .getEvent ().toString ();
209+ try {
210+ /** Forward invocation data to external server */
211+ forwardToBESStub (forwardChannel , invocationId , buildEvent );
212+ } catch (Exception e ) {
213+ System .err .println ("Could not forward invocation to external sever..." );
214+ }
175215 }
176216
177217 @ Override
@@ -186,6 +226,30 @@ public void onCompleted() {
186226 });
187227 }
188228
229+ /**
230+ * Forwards data to an external grpc stub.
231+ *
232+ * @param channel a grpc channel for connection
233+ * @param id the id of the data to send
234+ * @param payload the payload
235+ */
236+ private static void forwardToBESStub (ManagedChannel channel , String id , String payload ) {
237+ if (channel == null ) {
238+ return ;
239+ }
240+ final ForwardingGrpc .ForwardingBlockingStub blockingStub =
241+ ForwardingGrpc .newBlockingStub (channel );
242+ EngFlowRequest request = EngFlowRequest .newBuilder ().setId (id ).setPayload (payload ).build ();
243+ EngFlowResponse response ;
244+ try {
245+ response = blockingStub .forwardStream (request );
246+ System .out .println ("Forwarding: " + response .getMessage ());
247+ } catch (StatusRuntimeException e ) {
248+ System .out .println ("Could not forward data to external server." );
249+ return ;
250+ }
251+ }
252+
189253 private static ManagedChannel createChannel (
190254 String endpoint , @ Nullable String clientCertificate , @ Nullable String clientKey )
191255 throws IOException {
0 commit comments