From 88be5edafc61cf10c390ca336d2a4f39ee7ce878 Mon Sep 17 00:00:00 2001
From: Komal Makkar <komakkar@microsoft.com>
Date: Tue, 25 Aug 2020 09:02:41 -0400
Subject: [PATCH] SPI for PubSubRequestExtractor and handshake request handler.
 Added interfaces 1. IPubsubRequestBodyExtractor 2. IPubsubHandshakehandler

---
 .../osdu/notification/api/PubsubEndpoint.java    | 12 ++++++++++--
 .../di/CredentialHeadersProvider.java            |  5 +++--
 .../osdu/notification/di/RequestInfoExt.java     |  6 ++----
 .../interfaces/IPubsubHandshakeHandler.java      |  5 +++++
 .../interfaces/IPubsubRequestBodyExtractor.java  | 16 ++++++++++++++++
 .../notification/api/PubsubEndpointTests.java    |  4 ++--
 .../auth/AuthorizationFilterTest.java            |  4 ++--
 .../gcp/pubsub/PubsubHandshakeHandler.java       | 15 +++++++++++++++
 .../gcp}/pubsub/PubsubRequestBodyExtractor.java  |  9 +++++++--
 9 files changed, 62 insertions(+), 14 deletions(-)
 create mode 100644 notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IPubsubHandshakeHandler.java
 create mode 100644 notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IPubsubRequestBodyExtractor.java
 create mode 100644 provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/pubsub/PubsubHandshakeHandler.java
 rename {notification-core/src/main/java/org/opengroup/osdu/notification => provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp}/pubsub/PubsubRequestBodyExtractor.java (94%)

diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpoint.java b/notification-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpoint.java
index 89a3376a1..b1bc60252 100644
--- a/notification-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpoint.java
+++ b/notification-core/src/main/java/org/opengroup/osdu/notification/api/PubsubEndpoint.java
@@ -34,7 +34,8 @@ import org.opengroup.osdu.core.common.notification.ISubscriptionFactory;
 import org.opengroup.osdu.core.common.notification.ISubscriptionService;
 import org.opengroup.osdu.core.common.notification.SubscriptionException;
 import org.opengroup.osdu.notification.di.SubscriptionCacheFactory;
-import org.opengroup.osdu.notification.pubsub.PubsubRequestBodyExtractor;
+import org.opengroup.osdu.notification.provider.interfaces.IPubsubHandshakeHandler;
+import org.opengroup.osdu.notification.provider.interfaces.IPubsubRequestBodyExtractor;
 import org.opengroup.osdu.notification.utils.Config;
 import org.opengroup.osdu.notification.provider.interfaces.IGoogleServiceAccount;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -56,7 +57,9 @@ import java.util.Map;
 @RequestMapping("/push-handlers")
 public class PubsubEndpoint {
     @Autowired
-    private PubsubRequestBodyExtractor pubsubRequestBodyExtractor;
+    private IPubsubRequestBodyExtractor pubsubRequestBodyExtractor;
+    @Autowired
+    private IPubsubHandshakeHandler pubsubHandshakeHandler;
     @Autowired
     private ISignatureService signatureService;
     @Autowired
@@ -83,6 +86,11 @@ public class PubsubEndpoint {
     @PostMapping("/records-changed")
     @PreAuthorize("@authorizationFilter.hasAnyPermission('" + Config.OPS + "', '" + Config.PUBSUB + "')")
     public ResponseEntity recordChanged() throws Exception {
+        if(this.pubsubRequestBodyExtractor.isHandshakeRequest()) {
+            String handshakeResponse = this.pubsubHandshakeHandler.getHandshakeResponse();
+            return ResponseEntity.ok(handshakeResponse);
+        }
+
         String notificationId = this.pubsubRequestBodyExtractor.extractNotificationIdFromRequestBody();
         String pubsubMessage = this.pubsubRequestBodyExtractor.extractDataFromRequestBody();
         Map<String, String> headerAttributes = this.pubsubRequestBodyExtractor.extractAttributesFromRequestBody();
diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/di/CredentialHeadersProvider.java b/notification-core/src/main/java/org/opengroup/osdu/notification/di/CredentialHeadersProvider.java
index 24b6646c8..f06244b57 100644
--- a/notification-core/src/main/java/org/opengroup/osdu/notification/di/CredentialHeadersProvider.java
+++ b/notification-core/src/main/java/org/opengroup/osdu/notification/di/CredentialHeadersProvider.java
@@ -15,9 +15,10 @@
  */
 
 package org.opengroup.osdu.notification.di;
+
 import org.opengroup.osdu.core.common.model.http.DpsHeaders;
 import org.opengroup.osdu.core.common.util.IServiceAccountJwtClient;
-import org.opengroup.osdu.notification.pubsub.PubsubRequestBodyExtractor;
+import org.opengroup.osdu.notification.provider.interfaces.IPubsubRequestBodyExtractor;
 import org.springframework.beans.factory.FactoryBean;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Primary;
@@ -38,7 +39,7 @@ public class CredentialHeadersProvider implements FactoryBean<DpsHeaders> {
     private HttpServletRequest httpRequest;
 
     @Autowired
-    private PubsubRequestBodyExtractor pubsubRequestBodyExtractor;
+    private IPubsubRequestBodyExtractor pubsubRequestBodyExtractor;
 
     @Autowired
     private IServiceAccountJwtClient serviceAccountJwtClient;
diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/di/RequestInfoExt.java b/notification-core/src/main/java/org/opengroup/osdu/notification/di/RequestInfoExt.java
index 13adf62cb..7db3d5c59 100644
--- a/notification-core/src/main/java/org/opengroup/osdu/notification/di/RequestInfoExt.java
+++ b/notification-core/src/main/java/org/opengroup/osdu/notification/di/RequestInfoExt.java
@@ -19,7 +19,7 @@ package org.opengroup.osdu.notification.di;
 import com.google.api.client.util.Strings;
 import org.opengroup.osdu.core.common.model.http.DpsHeaders;
 import org.opengroup.osdu.core.common.model.http.RequestInfo;
-import org.opengroup.osdu.notification.pubsub.PubsubRequestBodyExtractor;
+import org.opengroup.osdu.notification.provider.interfaces.IPubsubRequestBodyExtractor;
 import org.springframework.context.annotation.Primary;
 import org.springframework.stereotype.Component;
 import org.springframework.web.context.annotation.RequestScope;
@@ -27,8 +27,6 @@ import org.springframework.web.context.annotation.RequestScope;
 import javax.annotation.PostConstruct;
 import javax.inject.Inject;
 import javax.servlet.http.HttpServletRequest;
-import java.io.BufferedReader;
-import java.io.IOException;
 import java.util.Collections;
 import java.util.Map;
 import java.util.stream.Collectors;
@@ -40,7 +38,7 @@ public class RequestInfoExt extends RequestInfo {
     private HttpServletRequest httpRequest;
 
     @Inject
-    private PubsubRequestBodyExtractor requestBodyExtractor;
+    private IPubsubRequestBodyExtractor requestBodyExtractor;
 
     @Inject
     public RequestInfoExt(HttpServletRequest request) {
diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IPubsubHandshakeHandler.java b/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IPubsubHandshakeHandler.java
new file mode 100644
index 000000000..d93b25c06
--- /dev/null
+++ b/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IPubsubHandshakeHandler.java
@@ -0,0 +1,5 @@
+package org.opengroup.osdu.notification.provider.interfaces;
+
+public interface IPubsubHandshakeHandler {
+    String getHandshakeResponse();
+}
diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IPubsubRequestBodyExtractor.java b/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IPubsubRequestBodyExtractor.java
new file mode 100644
index 000000000..57801ed6e
--- /dev/null
+++ b/notification-core/src/main/java/org/opengroup/osdu/notification/provider/interfaces/IPubsubRequestBodyExtractor.java
@@ -0,0 +1,16 @@
+package org.opengroup.osdu.notification.provider.interfaces;
+
+import org.springframework.stereotype.Component;
+import org.springframework.http.ResponseEntity;
+
+import java.util.Map;
+
+public interface IPubsubRequestBodyExtractor {
+    Map<String, String> extractAttributesFromRequestBody() ;
+
+    String extractDataFromRequestBody();
+
+    String extractNotificationIdFromRequestBody() ;
+
+    boolean isHandshakeRequest();
+}
diff --git a/notification-core/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointTests.java b/notification-core/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointTests.java
index d3155489b..707a59fc9 100644
--- a/notification-core/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointTests.java
+++ b/notification-core/src/test/java/org/opengroup/osdu/notification/api/PubsubEndpointTests.java
@@ -36,8 +36,8 @@ import org.opengroup.osdu.core.common.notification.SubscriptionFactory;
 import org.opengroup.osdu.core.common.notification.SubscriptionService;
 import org.opengroup.osdu.notification.di.CredentialHeadersProvider;
 import org.opengroup.osdu.notification.di.SubscriptionCacheFactory;
-import org.opengroup.osdu.notification.pubsub.PubsubRequestBodyExtractor;
 import org.opengroup.osdu.notification.provider.interfaces.IGoogleServiceAccount;
+import org.opengroup.osdu.notification.provider.interfaces.IPubsubRequestBodyExtractor;
 import org.powermock.modules.junit4.PowerMockRunner;
 import org.springframework.http.ResponseEntity;
 
@@ -54,7 +54,7 @@ import static org.mockito.Mockito.when;
 @RunWith(PowerMockRunner.class)
 public class PubsubEndpointTests {
     @Mock
-    private PubsubRequestBodyExtractor pubsubRequestBodyExtractor;
+    private IPubsubRequestBodyExtractor pubsubRequestBodyExtractor;
     @Mock
     private ISignatureService signatureService;
     @Mock
diff --git a/notification-core/src/test/java/org/opengroup/osdu/notification/auth/AuthorizationFilterTest.java b/notification-core/src/test/java/org/opengroup/osdu/notification/auth/AuthorizationFilterTest.java
index cbf48fb7c..61f052080 100644
--- a/notification-core/src/test/java/org/opengroup/osdu/notification/auth/AuthorizationFilterTest.java
+++ b/notification-core/src/test/java/org/opengroup/osdu/notification/auth/AuthorizationFilterTest.java
@@ -26,7 +26,7 @@ import org.opengroup.osdu.core.common.model.http.AppException;
 import org.opengroup.osdu.core.common.model.http.DpsHeaders;
 import org.opengroup.osdu.core.common.provider.interfaces.IAuthorizationService;
 import org.opengroup.osdu.notification.di.RequestInfoExt;
-import org.opengroup.osdu.notification.pubsub.PubsubRequestBodyExtractor;
+import org.opengroup.osdu.notification.provider.interfaces.IPubsubRequestBodyExtractor;
 import org.opengroup.osdu.notification.utils.Config;
 import org.opengroup.osdu.notification.provider.interfaces.IServiceAccountValidator;
 import org.powermock.modules.junit4.PowerMockRunner;
@@ -62,7 +62,7 @@ public class AuthorizationFilterTest {
     @Mock
     private IServiceAccountValidator validator;
     @Mock
-    private PubsubRequestBodyExtractor extractor;
+    private IPubsubRequestBodyExtractor extractor;
     @InjectMocks
     private AuthorizationFilter sut;
 
diff --git a/provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/pubsub/PubsubHandshakeHandler.java b/provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/pubsub/PubsubHandshakeHandler.java
new file mode 100644
index 000000000..c282f83aa
--- /dev/null
+++ b/provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/pubsub/PubsubHandshakeHandler.java
@@ -0,0 +1,15 @@
+package org.opengroup.osdu.notification.provider.gcp.pubsub;
+
+import org.opengroup.osdu.notification.provider.interfaces.IPubsubHandshakeHandler;
+import org.springframework.context.annotation.Lazy;
+import org.springframework.stereotype.Component;
+
+@Component
+@Lazy
+public class PubsubHandshakeHandler implements IPubsubHandshakeHandler {
+
+    @Override
+    public String getHandshakeResponse() {
+        return null;
+    }
+}
\ No newline at end of file
diff --git a/notification-core/src/main/java/org/opengroup/osdu/notification/pubsub/PubsubRequestBodyExtractor.java b/provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/pubsub/PubsubRequestBodyExtractor.java
similarity index 94%
rename from notification-core/src/main/java/org/opengroup/osdu/notification/pubsub/PubsubRequestBodyExtractor.java
rename to provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/pubsub/PubsubRequestBodyExtractor.java
index b8126d6c1..8a11fb3b5 100644
--- a/notification-core/src/main/java/org/opengroup/osdu/notification/pubsub/PubsubRequestBodyExtractor.java
+++ b/provider/notification-gcp/src/main/java/org/opengroup/osdu/notification/provider/gcp/pubsub/PubsubRequestBodyExtractor.java
@@ -14,7 +14,7 @@
  *   limitations under the License.
  */
 
-package org.opengroup.osdu.notification.pubsub;
+package org.opengroup.osdu.notification.provider.gcp.pubsub;
 
 import com.google.common.base.Strings;
 import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
@@ -25,6 +25,7 @@ import com.google.gson.Gson;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
+import org.opengroup.osdu.notification.provider.interfaces.IPubsubRequestBodyExtractor;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.stereotype.Component;
@@ -41,7 +42,7 @@ import java.util.stream.Stream;
 
 @Component
 @RequestScope
-public class PubsubRequestBodyExtractor {
+public class PubsubRequestBodyExtractor implements IPubsubRequestBodyExtractor {
     private static final String INVALID_PUBSUB_MESSAGE = "Invalid pubsub message";
     private static final Gson GSON = new Gson();
     private MessageContent messageContent;
@@ -80,6 +81,10 @@ public class PubsubRequestBodyExtractor {
         return fullNotificationId[fullNotificationId.length - 1];
     }
 
+    public boolean isHandshakeRequest() {
+        return false;
+    }
+
     private MessageContent extractPubsubMessageFromRequestBody() {
         if (this.root == null) {
             this.root = this.extractRootJsonElementFromRequestBody();
-- 
GitLab