Skip to content

Commit

Permalink
[PAGOPA-1446] feat: subscription validation event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-ang committed Jan 25, 2024
1 parent 27caa3b commit 3a9df23
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
<artifactId>azure-functions-java-library</artifactId>
<version>${azure.functions.java.library.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.azure/azure-messaging-eventgrid -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-messaging-eventgrid</artifactId>
<version>4.20.1</version>
</dependency>


<!-- End Azure -->

Expand Down
43 changes: 43 additions & 0 deletions src/main/java/it/gov/pagopa/gpd/upload/BlobEventFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package it.gov.pagopa.gpd.upload;

import com.azure.core.util.BinaryData;
import com.azure.messaging.eventgrid.EventGridEvent;
import com.azure.messaging.eventgrid.systemevents.SubscriptionValidationEventData;
import com.microsoft.azure.functions.*;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class BlobEventFunction {

@FunctionName("BlobCreatedSubscriber")
public HttpResponseMessage run (
@HttpTrigger(name = "BlobCreatedSubscriber",
methods = {HttpMethod.POST, HttpMethod.GET},
route = "upload",
authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage request,
final ExecutionContext context) {
BinaryData events = BinaryData.fromString(String.valueOf(request.getBody()));
Logger logger = context.getLogger();
logger.log(Level.INFO, () -> "Request body: " + request.getBody());
logger.log(Level.INFO, () -> "Events: " + events);

List<EventGridEvent> eventGridEvents = EventGridEvent.fromString(events.toString());

for (EventGridEvent eventGridEvent : eventGridEvents) {
if(eventGridEvent.getEventType().equals("SubscriptionValidationEventData")){
SubscriptionValidationEventData s = SubscriptionValidationEventData.class.cast(eventGridEvent);
return request.createResponseBuilder(HttpStatus.OK)
.header("Content-Type", "application/json")
.body(s.getValidationCode())
.build();
}
}
return request.createResponseBuilder(HttpStatus.NOT_FOUND)
.build();
}
}

0 comments on commit 3a9df23

Please sign in to comment.