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 89a3376a13fccbd420fbdd17a75189cbc9f6776e..b1bc60252566348c11ee2c9ddf44c75345717ac4 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 24b6646c8b93cb795ba8fedb04e9ade785d65788..f06244b57324e733e376f8a3b6cf0328092cb5a7 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 13adf62cb5605971ede3ef9379057e80e752ea34..7db3d5c5965506fce8bc78fbe3f3c7f675e38e64 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 0000000000000000000000000000000000000000..d93b25c067f91e33cc538c4f9394043cea1969e3 --- /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 0000000000000000000000000000000000000000..57801ed6e5bb670ff240c21ba202875fcd5eb223 --- /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 d3155489b3a8e6948fdb4e45e1dc43c90c2d6ab0..707a59fc951ab821160f71d35f766bc68ff39eff 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 cbf48fb7cc6d97a50bc105c2d2e18d1f47507fbd..61f0520808162f345ac2c6f4645d0247a480ee60 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 0000000000000000000000000000000000000000..c282f83aaf997c430a6e41083bf2104196a8d0d8 --- /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 b8126d6c1e863bcf085a889d7e217d9e876ce785..8a11fb3b511b5754f9f83ce63950a0829b511413 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();