From 3bc055011fc1060dd05c8ab75ed99fde07cd9cac Mon Sep 17 00:00:00 2001 From: komakkar <komakkar@microsoft.com> Date: Wed, 4 Nov 2020 17:57:50 +0530 Subject: [PATCH] Extracting the core logic in a method --- .../di/CredentialHeadersProvider.java | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) 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 30252493e..fb568311c 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 @@ -57,27 +57,30 @@ public class CredentialHeadersProvider implements FactoryBean<DpsHeaders> { switch (requestMethod) { case POST: case PUT: - case PATCH: { - if(this.pubsubRequestBodyExtractor.isHandshakeRequest()) { - // The headers are not needed for the handshake requests. - return new DpsHeaders(); - } - - Map<String, String> attributes = this.pubsubRequestBodyExtractor.extractAttributesFromRequestBody(); - try { - //extract headers from pubsub message - String dataPartitionId = attributes.get(DpsHeaders.DATA_PARTITION_ID); - String authToken = this.serviceAccountJwtClient.getIdToken(dataPartitionId); - attributes.put(DpsHeaders.AUTHORIZATION, authToken); - return DpsHeaders.createFromMap(attributes); - } catch (AppException e) { - throw new Exception("Failed to generate headers for register service."); - } - } + case PATCH: + return getDpsHeadersForPostPutPatch(); default: { return new DpsHeaders(); } } } + + private DpsHeaders getDpsHeadersForPostPutPatch() throws Exception { + if(this.pubsubRequestBodyExtractor.isHandshakeRequest()) { + // The headers are not needed for the handshake requests. + return new DpsHeaders(); + } + + Map<String, String> attributes = this.pubsubRequestBodyExtractor.extractAttributesFromRequestBody(); + try { + //extract headers from pubsub message + String dataPartitionId = attributes.get(DpsHeaders.DATA_PARTITION_ID); + String authToken = this.serviceAccountJwtClient.getIdToken(dataPartitionId); + attributes.put(DpsHeaders.AUTHORIZATION, authToken); + return DpsHeaders.createFromMap(attributes); + } catch (AppException e) { + throw new Exception("Failed to generate headers for register service."); + } + } } \ No newline at end of file -- GitLab