Commit 17503fa0 authored by Komal Makkar's avatar Komal Makkar
Browse files

intermediate changes

parent 12fb9693
......@@ -23,7 +23,9 @@ import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import java.util.Map;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.annotation.RequestScope;
......
......@@ -31,19 +31,4 @@ public class CredentialHeadersProviderTest {
when(httpRequest.getMethod()).thenReturn(RequestMethod.GET.toString());
assertNotNull(headersProvider.getObject());
}
@Test
public void testHandshake() throws Exception {
// set up
when(httpRequest.getMethod()).thenReturn(RequestMethod.GET.toString());
when(pubsubRequestBodyExtractor.isHandshakeRequest()).thenReturn(true);
// Act
DpsHeaders headers = headersProvider.getObject();
// Assert
assertNotNull(headers);
assertNull(headers.getCorrelationId());
assertNull(headers.getPartitionId());
}
}
\ No newline at end of file
......@@ -25,7 +25,7 @@ import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class NotificationData {
public class NotificationRecordsChangedData {
private JsonArray data;
......
......@@ -23,7 +23,7 @@ import lombok.SneakyThrows;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.notification.provider.azure.models.HandshakeRequestData;
import org.opengroup.osdu.notification.provider.azure.models.NotificationData;
import org.opengroup.osdu.notification.provider.azure.models.NotificationRecordsChangedData;
import org.opengroup.osdu.notification.provider.azure.models.NotificationRequest;
import org.opengroup.osdu.notification.provider.interfaces.IPubsubRequestBodyExtractor;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -45,8 +45,7 @@ import java.util.Map;
@Component
@RequestScope
public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtractor {
public static final String AUTHORIZATION = "Authorization";
private static final String INVALID_EVENTGRID_MESSAGE = "Invalid Event Grid Message";
private static final String SUBSCRIPTION_ID = "Aeg-Subscription-Name";
private static final String EVENTGRID_VALIDATION_EVENT = "Microsoft.EventGrid.SubscriptionValidationEvent";
private static final Gson GSON = new Gson();
......@@ -57,7 +56,7 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto
private final JaxRsDpsLog logger;
private final NotificationRequest notificationRequest;
private NotificationData notificationData;
private NotificationRecordsChangedData notificationRecordsChangedData;
private HandshakeRequestData handshakeRequestData;
private boolean isHandshakeRequest;
......@@ -71,41 +70,40 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto
/**
* Extracts the attributes from the request that are filled in by publisher of the message.
*
* @throws AppException
* @return Request Attributes Map
* @throws AppException
*/
public Map<String, String> extractAttributesFromRequestBody() {
if(isHandshakeRequest) {
if (isHandshakeRequest) {
logger.error("Invalid Event Grid Message. Is a handshake request");
return null;
}
Map<String, String> attributes= new HashMap<>();
attributes.put("correlation-id", this.notificationData.getCorrelationId());
attributes.put("data-partition-id", this.notificationData.getDataPartitionId());
attributes.put("account-id", this.notificationData.getAccountId());
attributes.put("authorization", extractAuthHeaderFromRequestBody());
Map<String, String> attributes = new HashMap<>();
attributes.put("correlation-id", this.notificationRecordsChangedData.getCorrelationId());
attributes.put("data-partition-id", this.notificationRecordsChangedData.getDataPartitionId());
attributes.put("account-id", this.notificationRecordsChangedData.getAccountId());
return attributes;
}
/**
* Extracts the data from the request that are filled in by publisher of the message,
*
* @throws AppException
* @return Request Data String
* @throws AppException
*/
public String extractDataFromRequestBody() {
if(isHandshakeRequest) {
if (isHandshakeRequest) {
logger.error("Invalid Event Grid Message. Is a handshake request");
return null;
}
return notificationData.getData().toString();
return notificationRecordsChangedData.getData().toString();
}
/**
* Extracts the notificationId from the request that are filled in by EventGrid.
*
* @throws AppException
* @return Request NotificationId String.
* @throws AppException
*/
public String extractNotificationIdFromRequestBody() {
String subscriptionId = httpServletRequest.getHeader(SUBSCRIPTION_ID);
......@@ -113,36 +111,28 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto
logger.error("Invalid Event Grid Message. Subscription Id is null or empty");
throw new AppException(HttpStatus.BAD_REQUEST.value(), "Invalid Event Grid Message", "Subscription ID not found");
}
return subscriptionId.substring(0, subscriptionId.length() - 7);
}
private String extractAuthHeaderFromRequestBody() {
String authorization = httpServletRequest.getHeader(AUTHORIZATION);
if (Strings.isNullOrEmpty(authorization)) {
logger.error("Invalid Event Grid Message. Subscription Id is null or empty");
throw new AppException(HttpStatus.BAD_REQUEST.value(), "Invalid Event Grid Message", "Subscription ID not found");
}
return authorization;
return subscriptionId.toLowerCase();
}
/**
* Checks if the request is for handshake.
*
* @throws AppException
* @return Request Type Boolean
* @throws AppException
*/
public boolean isHandshakeRequest() {
return this.isHandshakeRequest ;
return this.isHandshakeRequest;
}
/**
* Return ValidationCode
*
* @throws AppException
* @return Request Type Boolean
* @throws AppException
*/
public String getValidationCodeForHandshake() {
if(!isHandshakeRequest) {
if (!isHandshakeRequest) {
logger.error("Invalid Event Grid Message. Is not a handshake request");
return null;
}
......@@ -152,13 +142,14 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto
/**
* Utility method that extracts the core content in the request.
* This is what the publisher will publish.
*
* <p>
* The attributes are validated.
*
* @throws AppException
* @return NotificationRequest Object
* @throws AppException
*/
@SneakyThrows
// TODO : @komakkar sanitize the exceptions to match the SpringExceptionMapper and throw ValidationException
private NotificationRequest extractNotificationRequestFromHttpRequest() {
NotificationRequest notificationRequest = null;
if (this.notificationRequest == null) {
......@@ -183,13 +174,13 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto
private void extractHandshakeData(NotificationRequest notificationRequest) {
this.handshakeRequestData = GSON.fromJson(notificationRequest.getData(), HandshakeRequestData.class);
Preconditions.checkNotNull(this.handshakeRequestData.getValidationCode(), "Request payload parsing error handshkae" );
Preconditions.checkNotNull(this.handshakeRequestData.getValidationCode(), "Request payload parsing error handshkae");
}
private void extractNotificationData(NotificationRequest notificationRequest) {
NotificationData notificationData = GSON.fromJson(notificationRequest.getData(), NotificationData.class);
verifyNotificationData(notificationData);
this.notificationData = notificationData;
NotificationRecordsChangedData notificationRecordsChangedData = GSON.fromJson(notificationRequest.getData(), NotificationRecordsChangedData.class);
verifyNotificationData(notificationRecordsChangedData);
this.notificationRecordsChangedData = notificationRecordsChangedData;
}
// TODO: Clean up for using @NonNull Lombok.
......@@ -198,15 +189,15 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto
//
// Making Gson use a custom constructor, involves boilerplating, hence adding precondition checks. https://github.com/google/gson/blob/master/UserGuide.md#TOC-Writing-a-Deserializer
// The required condition is not operation as of now.https://github.com/google/gson/issues/61
private void verifyNotificationData(NotificationData notificationData) {
Preconditions.checkNotNull(notificationData, "Request payload parsing error Data" );
Preconditions.checkNotNull(notificationData.getData(), "Request payload parsing error data data" );
Preconditions.checkNotNull(notificationData.getCorrelationId() , "Request payload parsing error coorelation" );
Preconditions.checkNotNull(notificationData.getDataPartitionId() , "Request payload parsing error partition" );
private void verifyNotificationData(NotificationRecordsChangedData notificationRecordsChangedData) {
Preconditions.checkNotNull(notificationRecordsChangedData, "Request payload parsing error");
Preconditions.checkNotNull(notificationRecordsChangedData.getData(), "Request payload parsing error");
Preconditions.checkNotNull(notificationRecordsChangedData.getCorrelationId(), "Request payload parsing error");
Preconditions.checkNotNull(notificationRecordsChangedData.getDataPartitionId(), "Request payload parsing error");
}
private String getBody(HttpServletRequest request) throws IOException {
String body = null;
String body;
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;
try {
......@@ -221,15 +212,9 @@ public class EventGridRequestBodyExtractor implements IPubsubRequestBodyExtracto
} else {
stringBuilder.append("");
}
} catch (IOException ex) {
throw ex;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
throw ex;
}
bufferedReader.close();
}
}
body = stringBuilder.toString();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment