From eb8eedb442d062778140ca1b78ad545409c5ad1b Mon Sep 17 00:00:00 2001 From: komakkar <komakkar@microsoft.com> Date: Sun, 22 Nov 2020 01:44:23 +0530 Subject: [PATCH] adding stack trace for debugging. --- .../osdu/notification/api/PubsubEndpoint.java | 104 +++++++++--------- .../api/TestPubsubEndpointHMAC.java | 2 +- 2 files changed, 55 insertions(+), 51 deletions(-) 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 2b4be7d15..085b98078 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 @@ -86,57 +86,61 @@ public class PubsubEndpoint { @PostMapping("/records-changed") @PreAuthorize("@authorizationFilter.hasAnyPermission('" + Config.OPS + "', '" + Config.PUBSUB + "')") public ResponseEntity recordChanged() throws Exception { - this.log.info("komakkar recieved recoreds changed request " ); - 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(); - - Subscription subscription = getSubscriptionFromCache(notificationId); - Secret secret = subscription.getSecret(); - String endpoint = subscription.getPushEndpoint(); - - String secretType = secret.getSecretType(); - String pushUrl = ""; - Map<String, String> requestHeader = new HashMap<>(); - - if (secretType.equalsIgnoreCase(HMAC_TYPE)) { - this.log.info("receiving pubsub message, will send out hmac type request, pubsub message: " + pubsubMessage); - HmacSecret hmacSecret = (HmacSecret) secret; - String signedjwt = this.signatureService.getSignedSignature(endpoint, hmacSecret.getValue()); - pushUrl = endpoint + "?hmac=" + signedjwt; - } else if (secretType.equalsIgnoreCase(GSA_TYPE)) { - this.log.info("receiving pubsub message, will send out gsa type request, pubsub message: " + pubsubMessage); - GsaSecret gsaSecret = (GsaSecret) secret; - GsaSecretValue gsaSecretValue = gsaSecret.getValue(); - - JsonParser jsonParser = new JsonParser(); - JsonElement root = jsonParser.parse(gsaSecretValue.getKey()); - String keyString = root.getAsJsonObject().toString(); - - String idToken = this.gsaTokenProvider.getIdToken(keyString, gsaSecretValue.getAudience()); - pushUrl = endpoint; - requestHeader.put("Authorization", idToken); - } - - this.log.info("komakkar sending out notification to endpoint: " + pushUrl); - 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)); - this.log.info("komakkar sending out notification to endpoint: " + headers.toString()); - - HttpRequest request = HttpRequest.post().url(pushUrl).headers(requestHeader).body(pubsubMessage).connectionTimeout(WAITING_TIME).build(); - HttpResponse response = httpClient.send(request); - if (!response.isSuccessCode()) { - this.log.error(NOT_ACKNOWLEDGE); - return ResponseEntity.badRequest().body(NOT_ACKNOWLEDGE); + try { + 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(); + + Subscription subscription = getSubscriptionFromCache(notificationId); + Secret secret = subscription.getSecret(); + String endpoint = subscription.getPushEndpoint(); + + String secretType = secret.getSecretType(); + String pushUrl = ""; + Map<String, String> requestHeader = new HashMap<>(); + + if (secretType.equalsIgnoreCase(HMAC_TYPE)) { + this.log.info("receiving pubsub message, will send out hmac type request, pubsub message: " + pubsubMessage); + HmacSecret hmacSecret = (HmacSecret) secret; + String signedjwt = this.signatureService.getSignedSignature(endpoint, hmacSecret.getValue()); + pushUrl = endpoint + "?hmac=" + signedjwt; + } else if (secretType.equalsIgnoreCase(GSA_TYPE)) { + this.log.info("receiving pubsub message, will send out gsa type request, pubsub message: " + pubsubMessage); + GsaSecret gsaSecret = (GsaSecret) secret; + GsaSecretValue gsaSecretValue = gsaSecret.getValue(); + + JsonParser jsonParser = new JsonParser(); + JsonElement root = jsonParser.parse(gsaSecretValue.getKey()); + String keyString = root.getAsJsonObject().toString(); + + String idToken = this.gsaTokenProvider.getIdToken(keyString, gsaSecretValue.getAudience()); + pushUrl = endpoint; + requestHeader.put("Authorization", idToken); + } + + this.log.info("sending out notification to endpoint: " + endpoint); + 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)); + this.log.info("komakkar sending out notification to endpoint: " + headers.toString()); + + HttpRequest request = HttpRequest.post().url(pushUrl).headers(requestHeader).body(pubsubMessage).connectionTimeout(WAITING_TIME).build(); + HttpResponse response = httpClient.send(request); + if (!response.isSuccessCode()) { + this.log.error(NOT_ACKNOWLEDGE); + return ResponseEntity.badRequest().body(NOT_ACKNOWLEDGE); + } + this.log.info(ACKNOWLEDGE); + return ResponseEntity.ok(ACKNOWLEDGE); + } catch (Exception e) { + e.printStackTrace(); } - this.log.info(ACKNOWLEDGE); - return ResponseEntity.ok(ACKNOWLEDGE); + return ResponseEntity.badRequest().body(NOT_ACKNOWLEDGE); } private Subscription getSubscriptionFromCache(String notificationId) throws Exception { diff --git a/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/TestPubsubEndpointHMAC.java b/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/TestPubsubEndpointHMAC.java index 54e98c9da..20319e1e8 100644 --- a/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/TestPubsubEndpointHMAC.java +++ b/testing/notification-test-azure/src/test/java/org/opengroup/osdu/notification/api/TestPubsubEndpointHMAC.java @@ -59,7 +59,7 @@ public class TestPubsubEndpointHMAC extends PubsubEndpointHMACTests { return; } - // For the following 403 is the expceted result. + // For the following 403 is the expected result. // Tracking in Issue: https://community.opengroup.org/osdu/platform/system/notification/-/issues/17 @Test @Override -- GitLab