From 5fc62fab7dd78aefe968a10c387d0b8ebe0fb54b Mon Sep 17 00:00:00 2001
From: Anfal Fadhil <afadhil2@slb.com>
Date: Mon, 30 Jan 2023 21:23:47 +0000
Subject: [PATCH] added x-collaboration property to the message header.

---
 docs/tutorial/DataNotification.md             | 30 ++++++++++++++++---
 .../service/NotificationHandler.java          | 17 ++++++++---
 2 files changed, 39 insertions(+), 8 deletions(-)

diff --git a/docs/tutorial/DataNotification.md b/docs/tutorial/DataNotification.md
index 2b2877779..0e4861e75 100644
--- a/docs/tutorial/DataNotification.md
+++ b/docs/tutorial/DataNotification.md
@@ -705,9 +705,8 @@ properties:
 [Back to Table of Contents](#TOC)
 
 ## Support for Collaboration Context <a name="collaboration-support"></a>
-Register service and Notification service are collaboration aware. For now, to enable collaboration context support you have to enable collaboration feature flag in the services. Refer to this [Wiki](https://community.opengroup.org/groups/osdu/platform/system/-/wikis/Feature-Flag) for more details on how to do that. 
-That means when the consumer provides the "x-collaboration" header the list of topics returned will have a new topic "recordstopic-v2" which will receive notifications when "x-collaboration" header is provided. 
-
+Register service and Notification service are collaboration aware. For now, to enable collaboration context support you have to enable collaboration feature flag in the services. Refer to this [Wiki](https://community.opengroup.org/groups/osdu/platform/system/-/wikis/Feature-Flag) for more details on how to do that.
+That means when the collaboration context feature flag is enabled the list of topics returned will have a new topic "recordstopic-v2" which will receive notifications when "x-collaboration" header is provided in the request.
 
 <details><summary>curl for a collaboration context header provided request</summary>
 
@@ -721,7 +720,7 @@ curl --request GET \
 ```
 </details>
 
-A sample output is shown below when the collaboration context header is provided.
+A sample output is shown below when the collaboration context feature flag is set to true.
 
 <details><summary>Sample response</summary>
 
@@ -827,3 +826,26 @@ A sample output is shown below when the collaboration context header is provided
 ```
 
 </details>
+
+
+When the feature flag is set to true and the consumer provides "x-collaboration" header in the request for creating, updating, and deleting a record. the message sent will contain the collaboration context header as an attribute.
+
+#### Example of a message when the x-collaboration header is provided:
+```json
+{
+   "message": {
+      "data": [
+         {
+            "id": "<message-id>",
+            "kind": "common:welldb:wellbore:1.0.0",
+            "op": "create"
+         }
+      ],
+      "account-id": "opendes",
+      "data-partition-id": "opendes",
+      "correlation-id": "<corrilation-id>",
+      "x-collaboration": "id=<collaboration-id>,application=<app-name>"
+   }
+}
+```
+
diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/service/NotificationHandler.java b/notification-core/src/main/java/org/opengroup/osdu/notification/service/NotificationHandler.java
index 9ba4a823a..1f92d0cdb 100644
--- a/notification-core/src/main/java/org/opengroup/osdu/notification/service/NotificationHandler.java
+++ b/notification-core/src/main/java/org/opengroup/osdu/notification/service/NotificationHandler.java
@@ -16,10 +16,10 @@
 
 package org.opengroup.osdu.notification.service;
 
+import com.google.common.base.Strings;
 import org.opengroup.osdu.core.common.http.HttpClient;
 import org.opengroup.osdu.core.common.http.HttpRequest;
 import org.opengroup.osdu.core.common.http.HttpResponse;
-import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
 import org.opengroup.osdu.core.common.model.http.DpsHeaders;
 import org.opengroup.osdu.core.common.model.notification.*;
 import org.opengroup.osdu.notification.auth.factory.AuthFactory;
@@ -29,7 +29,6 @@ import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
-import org.springframework.web.context.annotation.RequestScope;
 
 import java.util.HashMap;
 import java.util.Map;
@@ -37,6 +36,7 @@ import java.util.Map;
 @Component
 public class NotificationHandler {
     private final static Logger LOGGER = LoggerFactory.getLogger(NotificationHandler.class);
+    private static final String X_COLLABORATION_HEADER =  "x-collaboration";
     @Autowired
     private HttpClient httpClient;
     @Autowired
@@ -52,6 +52,7 @@ public class NotificationHandler {
         String endpoint = subscription.getPushEndpoint();
         String secretType = secret.getSecretType();
         String pushUrl = "";
+        String collaborationContext = "";
         Map<String, String> requestHeader = new HashMap<String, String>();
 
         // Authentication Secret
@@ -60,13 +61,21 @@ public class NotificationHandler {
         pushUrl = secretAuth.getPushUrl(endpoint);
         requestHeader = secretAuth.getRequestHeaders();
 
+
+        if (headerAttributes.containsKey(X_COLLABORATION_HEADER))
+            collaborationContext = headerAttributes.get(X_COLLABORATION_HEADER);
+
         requestHeader.put(DpsHeaders.CONTENT_TYPE, "application/json");
         requestHeader.put(DpsHeaders.CORRELATION_ID, headerAttributes.get(DpsHeaders.CORRELATION_ID));
         requestHeader.put(DpsHeaders.DATA_PARTITION_ID, headerAttributes.get(DpsHeaders.DATA_PARTITION_ID));
 
+        if (!Strings.isNullOrEmpty(collaborationContext)) {
+            requestHeader.put(X_COLLABORATION_HEADER, headerAttributes.get(X_COLLABORATION_HEADER));
+        }
+
         HttpRequest request = HttpRequest.post().url(pushUrl).headers(requestHeader).body(pubsubMessage).connectionTimeout(WAITING_TIME).build();
-        HttpResponse response = httpClient.send(request);
         this.LOGGER.debug("Sending out notification to endpoint: " + endpoint);
+        HttpResponse response = httpClient.send(request);
         return response;
     }
-}
+}
\ No newline at end of file
-- 
GitLab