From ae1c3e365682e1fd9a3233c72186251169ef052f Mon Sep 17 00:00:00 2001 From: alexsilaghi Date: Wed, 27 Nov 2024 13:11:10 +0200 Subject: [PATCH 1/3] added execution context when emitting events. --- pom.xml | 2 +- .../protege/webprotege/ipc/EventDispatcher.java | 2 ++ .../protege/webprotege/ipc/EventHandler.java | 4 ++++ .../ipc/impl/RabbitMQEventDispatcher.java | 14 +++++++++++++- .../ipc/impl/RabbitMQEventHandlerWrapper.java | 15 +++++++++++++-- .../protege/webprotege/ipc/TestEventHandler.java | 5 +++++ 6 files changed, 38 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 7729009..9f9cd85 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ edu.stanford.protege webprotege-ipc - 1.0.7 + 1.0.8 webprotege-ipc Inter Process Communication framework diff --git a/src/main/java/edu/stanford/protege/webprotege/ipc/EventDispatcher.java b/src/main/java/edu/stanford/protege/webprotege/ipc/EventDispatcher.java index 7604c8f..d067b7e 100644 --- a/src/main/java/edu/stanford/protege/webprotege/ipc/EventDispatcher.java +++ b/src/main/java/edu/stanford/protege/webprotege/ipc/EventDispatcher.java @@ -8,5 +8,7 @@ * 2022-01-31 */ public interface EventDispatcher { + void dispatchEvent(Event event, ExecutionContext executionContext); + void dispatchEvent(Event event); } diff --git a/src/main/java/edu/stanford/protege/webprotege/ipc/EventHandler.java b/src/main/java/edu/stanford/protege/webprotege/ipc/EventHandler.java index 815618f..070508f 100644 --- a/src/main/java/edu/stanford/protege/webprotege/ipc/EventHandler.java +++ b/src/main/java/edu/stanford/protege/webprotege/ipc/EventHandler.java @@ -40,4 +40,8 @@ public interface EventHandler { * @param event The event to be handled */ void handleEvent(E event); + + default void handleEvent(E event, ExecutionContext executionContext) { + + } } diff --git a/src/main/java/edu/stanford/protege/webprotege/ipc/impl/RabbitMQEventDispatcher.java b/src/main/java/edu/stanford/protege/webprotege/ipc/impl/RabbitMQEventDispatcher.java index 5b33419..12e5d9a 100644 --- a/src/main/java/edu/stanford/protege/webprotege/ipc/impl/RabbitMQEventDispatcher.java +++ b/src/main/java/edu/stanford/protege/webprotege/ipc/impl/RabbitMQEventDispatcher.java @@ -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; @@ -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); @@ -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 diff --git a/src/main/java/edu/stanford/protege/webprotege/ipc/impl/RabbitMQEventHandlerWrapper.java b/src/main/java/edu/stanford/protege/webprotege/ipc/impl/RabbitMQEventHandlerWrapper.java index ea1417b..2e41a07 100644 --- a/src/main/java/edu/stanford/protege/webprotege/ipc/impl/RabbitMQEventHandlerWrapper.java +++ b/src/main/java/edu/stanford/protege/webprotege/ipc/impl/RabbitMQEventHandlerWrapper.java @@ -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; @@ -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 implements MessageListener { @@ -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); diff --git a/src/test/java/edu/stanford/protege/webprotege/ipc/TestEventHandler.java b/src/test/java/edu/stanford/protege/webprotege/ipc/TestEventHandler.java index 89f2888..1510ff6 100644 --- a/src/test/java/edu/stanford/protege/webprotege/ipc/TestEventHandler.java +++ b/src/test/java/edu/stanford/protege/webprotege/ipc/TestEventHandler.java @@ -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) { + + } } From 5490786f4c07651ec04123bcfeb4b3d0ecaa131b Mon Sep 17 00:00:00 2001 From: alexsilaghi Date: Wed, 27 Nov 2024 13:28:44 +0200 Subject: [PATCH 2/3] fixed failing test. added test --- .../ipc/impl/RabbitMQEventHandlerWrapper.java | 2 +- .../webprotege/ipc/EventHandleWrapperTest.java | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/edu/stanford/protege/webprotege/ipc/impl/RabbitMQEventHandlerWrapper.java b/src/main/java/edu/stanford/protege/webprotege/ipc/impl/RabbitMQEventHandlerWrapper.java index 2e41a07..ba58b20 100644 --- a/src/main/java/edu/stanford/protege/webprotege/ipc/impl/RabbitMQEventHandlerWrapper.java +++ b/src/main/java/edu/stanford/protege/webprotege/ipc/impl/RabbitMQEventHandlerWrapper.java @@ -42,7 +42,7 @@ public void onMessage(Message message) { var accessToken = String.valueOf(message.getMessageProperties().getHeaders().get(ACCESS_TOKEN)); var userId = (String) message.getMessageProperties().getHeaders().get(USER_ID); - if(accessToken != null && !accessToken.isEmpty()){ + if(accessToken != null && !accessToken.isEmpty() && !"null".equalsIgnoreCase(accessToken)){ ExecutionContext executionContext = new ExecutionContext(UserId.valueOf(userId), accessToken); eventHandler.handleEvent(event, executionContext); } else { diff --git a/src/test/java/edu/stanford/protege/webprotege/ipc/EventHandleWrapperTest.java b/src/test/java/edu/stanford/protege/webprotege/ipc/EventHandleWrapperTest.java index 66027f6..55aa54f 100644 --- a/src/test/java/edu/stanford/protege/webprotege/ipc/EventHandleWrapperTest.java +++ b/src/test/java/edu/stanford/protege/webprotege/ipc/EventHandleWrapperTest.java @@ -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; @@ -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"); From bb8a909da8e17f4f1ae502084601940327855e54 Mon Sep 17 00:00:00 2001 From: alexsilaghi Date: Wed, 27 Nov 2024 14:06:31 +0200 Subject: [PATCH 3/3] fixed not implemented method. --- .../edu/stanford/protege/webprotege/ipc/EventHandler.java | 4 ++-- .../ipc/impl/EventHandlerMethodNotImplemented.java | 8 ++++++++ .../webprotege/ipc/impl/RabbitMQEventHandlerWrapper.java | 7 ++++++- 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 src/main/java/edu/stanford/protege/webprotege/ipc/impl/EventHandlerMethodNotImplemented.java diff --git a/src/main/java/edu/stanford/protege/webprotege/ipc/EventHandler.java b/src/main/java/edu/stanford/protege/webprotege/ipc/EventHandler.java index 070508f..5796097 100644 --- a/src/main/java/edu/stanford/protege/webprotege/ipc/EventHandler.java +++ b/src/main/java/edu/stanford/protege/webprotege/ipc/EventHandler.java @@ -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; @@ -42,6 +42,6 @@ public interface EventHandler { void handleEvent(E event); default void handleEvent(E event, ExecutionContext executionContext) { - + throw new EventHandlerMethodNotImplemented("Method not implemented"); } } diff --git a/src/main/java/edu/stanford/protege/webprotege/ipc/impl/EventHandlerMethodNotImplemented.java b/src/main/java/edu/stanford/protege/webprotege/ipc/impl/EventHandlerMethodNotImplemented.java new file mode 100644 index 0000000..87033fe --- /dev/null +++ b/src/main/java/edu/stanford/protege/webprotege/ipc/impl/EventHandlerMethodNotImplemented.java @@ -0,0 +1,8 @@ +package edu.stanford.protege.webprotege.ipc.impl; + +public class EventHandlerMethodNotImplemented extends RuntimeException { + + public EventHandlerMethodNotImplemented(String message) { + super(message); + } +} diff --git a/src/main/java/edu/stanford/protege/webprotege/ipc/impl/RabbitMQEventHandlerWrapper.java b/src/main/java/edu/stanford/protege/webprotege/ipc/impl/RabbitMQEventHandlerWrapper.java index ba58b20..2b73d84 100644 --- a/src/main/java/edu/stanford/protege/webprotege/ipc/impl/RabbitMQEventHandlerWrapper.java +++ b/src/main/java/edu/stanford/protege/webprotege/ipc/impl/RabbitMQEventHandlerWrapper.java @@ -44,7 +44,12 @@ public void onMessage(Message message) { if(accessToken != null && !accessToken.isEmpty() && !"null".equalsIgnoreCase(accessToken)){ ExecutionContext executionContext = new ExecutionContext(UserId.valueOf(userId), accessToken); - eventHandler.handleEvent(event, executionContext); + try { + eventHandler.handleEvent(event, executionContext); + + } catch (EventHandlerMethodNotImplemented e){ + eventHandler.handleEvent(event); + } } else { eventHandler.handleEvent(event); }