Skip to content

Commit

Permalink
added execution context when emitting events.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsilaghi committed Nov 27, 2024
1 parent edffea9 commit ae1c3e3
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>edu.stanford.protege</groupId>
<artifactId>webprotege-ipc</artifactId>
<version>1.0.7</version>
<version>1.0.8</version>
<name>webprotege-ipc</name>
<description>Inter Process Communication framework</description>
<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
* 2022-01-31
*/
public interface EventDispatcher {
void dispatchEvent(Event event, ExecutionContext executionContext);

void dispatchEvent(Event event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ public interface EventHandler<E extends Event> {
* @param event The event to be handled
*/
void handleEvent(E event);

default void handleEvent(E event, ExecutionContext executionContext) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import edu.stanford.protege.webprotege.common.Event;
import edu.stanford.protege.webprotege.common.ProjectEvent;
import edu.stanford.protege.webprotege.ipc.EventDispatcher;
import edu.stanford.protege.webprotege.ipc.ExecutionContext;
import edu.stanford.protege.webprotege.ipc.Headers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
Expand Down Expand Up @@ -40,12 +42,18 @@ public RabbitMQEventDispatcher(ObjectMapper objectMapper,
}

@Override
public void dispatchEvent(Event event) {
public void dispatchEvent(Event event, ExecutionContext executionContext) {
try {
var value = objectMapper.writeValueAsBytes(event);
Message message = MessageBuilder.withBody(value).build();
getJsonTypeName(event).ifPresent(typeName ->message.getMessageProperties().getHeaders().put(EVENT_TYPE, typeName));
message.getMessageProperties().getHeaders().put(CHANNEL, event.getChannel());

if(executionContext != null) {
message.getMessageProperties().getHeaders().put(Headers.ACCESS_TOKEN, executionContext.jwt());
message.getMessageProperties().getHeaders().put(Headers.USER_ID, executionContext.userId().id());
}

if(event instanceof ProjectEvent) {
var projectId = ((ProjectEvent) event).projectId().value();
message.getMessageProperties().getHeaders().put(PROJECT_ID, projectId);
Expand All @@ -54,7 +62,11 @@ public void dispatchEvent(Event event) {
} catch (JsonProcessingException | AmqpException e) {
logger.info("Could not serialize event: {}", e.getMessage(), e);
}
}

@Override
public void dispatchEvent(Event event) {
dispatchEvent(event, null);
}
/*
TODO remove this after everything regarding events is clear
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import edu.stanford.protege.webprotege.common.Event;
import edu.stanford.protege.webprotege.common.UserId;
import edu.stanford.protege.webprotege.ipc.EventHandler;
import edu.stanford.protege.webprotege.ipc.ExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
Expand All @@ -11,7 +13,7 @@
import java.io.IOException;
import java.util.List;

import static edu.stanford.protege.webprotege.ipc.Headers.CHANNEL;
import static edu.stanford.protege.webprotege.ipc.Headers.*;

public class RabbitMQEventHandlerWrapper<T extends Event> implements MessageListener {

Expand All @@ -37,7 +39,16 @@ public void onMessage(Message message) {
if(eventHandler != null) {
try {
T event = (T) objectMapper.readValue(message.getBody(), eventHandler.getEventClass());
eventHandler.handleEvent(event);
var accessToken = String.valueOf(message.getMessageProperties().getHeaders().get(ACCESS_TOKEN));
var userId = (String) message.getMessageProperties().getHeaders().get(USER_ID);

if(accessToken != null && !accessToken.isEmpty()){
ExecutionContext executionContext = new ExecutionContext(UserId.valueOf(userId), accessToken);
eventHandler.handleEvent(event, executionContext);
} else {
eventHandler.handleEvent(event);
}

} catch (IOException e) {
logger.error("Error when handling event "+ message.getMessageProperties().getMessageId(), e);
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public void handleEvent(TestEvent event) {
logger.info("Handling event " + event);
EventHandler_TestCase.countDownLatch.countDown();
}

@Override
public void handleEvent(TestEvent event, ExecutionContext executionContext) {

}
}

0 comments on commit ae1c3e3

Please sign in to comment.