Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added execution context when emitting events. #9

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -1,7 +1,7 @@
package edu.stanford.protege.webprotege.ipc;

import edu.stanford.protege.webprotege.common.Event;
import reactor.core.publisher.Mono;
import edu.stanford.protege.webprotege.ipc.impl.EventHandlerMethodNotImplemented;

import javax.annotation.Nonnull;

Expand Down 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) {
throw new EventHandlerMethodNotImplemented("Method not implemented");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package edu.stanford.protege.webprotege.ipc.impl;

public class EventHandlerMethodNotImplemented extends RuntimeException {

public EventHandlerMethodNotImplemented(String message) {
super(message);
}
}
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,21 @@ 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() && !"null".equalsIgnoreCase(accessToken)){
ExecutionContext executionContext = new ExecutionContext(UserId.valueOf(userId), accessToken);
try {
eventHandler.handleEvent(event, executionContext);

} catch (EventHandlerMethodNotImplemented e){
eventHandler.handleEvent(event);
}
} 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 @@ -2,6 +2,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.stanford.protege.webprotege.common.UserId;
import edu.stanford.protege.webprotege.ipc.impl.RabbitMQEventHandlerWrapper;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -55,6 +56,19 @@ public void GIVEN_eventOnDummyChannel_WHEN_handleEvent_THEN_correctHandlerIsCall
verify(dummyEventHandler, times(1)).handleEvent(any());
}

@Test
public void GIVEN_eventOnDummyChannelWithExecutionContext_WHEN_handleEvent_THEN_methodWithExecutionContextIsUsed() throws JsonProcessingException {
TestEvent testEvent = new TestEvent("1", "2");
Message message = MessageBuilder.withBody(new ObjectMapper().writeValueAsBytes(testEvent)).build();
message.getMessageProperties().setHeaders(new HashMap<>());
message.getMessageProperties().getHeaders().put(Headers.ACCESS_TOKEN, "testJwt");
message.getMessageProperties().getHeaders().put(Headers.USER_ID, "dummy-test-user");

eventHandler.onMessage(message);

verify(dummyEventHandler, times(0)).handleEvent(any(), eq(new ExecutionContext(UserId.valueOf("dummy-test-user"), "testJwt")));
}

@Test
public void GIVEN_eventOnDifferentChannel_WHEN_handleEvent_THEN_noHandleIsCalled() throws JsonProcessingException {
TestEvent testEvent = new TestEvent("1", "2");
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) {

}
}