Skip to content

Commit

Permalink
migrated websocket to api gateway. bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsilaghi committed May 8, 2024
1 parent 434a02f commit 2efaf74
Show file tree
Hide file tree
Showing 13 changed files with 875 additions and 6 deletions.
42 changes: 37 additions & 5 deletions 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-gwt-api-gateway</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
<name>webprotege-gwt-api-gateway</name>
<description>The API Gateway for the WebProtégé GWT User Interface</description>
<properties>
Expand Down Expand Up @@ -39,6 +39,11 @@
<artifactId>testcontainers-keycloak</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.4</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand All @@ -52,10 +57,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>edu.stanford.protege</groupId>
<artifactId>webprotege-ipc</artifactId>
Expand All @@ -67,6 +68,11 @@
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.0-jre</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
Expand Down Expand Up @@ -122,6 +128,32 @@
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-guava</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>edu.stanford.protege</groupId>
<artifactId>webprotege-backend-api</artifactId>
<version>1.0.2</version>
</dependency>

<dependency>
<groupId>edu.stanford.protege</groupId>
<artifactId>webprotege-entity-frames</artifactId>
<version>0.9.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.4</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public WebSecurityCustomizer webSecurityCustomizer() {
public SecurityFilterChain resourceServerFilterChain(HttpSecurity http) throws Exception {
http.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(auth -> auth
.requestMatchers("/api/execute")
.requestMatchers("/api/execute", "/wsapps")
.permitAll()
.anyRequest()
.authenticated()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package edu.stanford.protege.webprotege.gateway.websocket;


import edu.stanford.protege.webprotege.authorization.*;
import edu.stanford.protege.webprotege.gateway.websocket.dto.BuiltInAction;
import edu.stanford.protege.webprotege.ipc.CommandExecutor;
import edu.stanford.protege.webprotege.ipc.ExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Nonnull;
import java.util.concurrent.ExecutionException;

@Service
public class AccessManager {

private final static Logger LOGGER = LoggerFactory.getLogger(AccessManager.class);

private final CommandExecutor<GetAuthorizationStatusRequest, GetAuthorizationStatusResponse> getAuthorizationStatusExecutor;

public AccessManager(CommandExecutor<GetAuthorizationStatusRequest, GetAuthorizationStatusResponse> getAuthorizationStatusExecutor) {
this.getAuthorizationStatusExecutor = getAuthorizationStatusExecutor;
}

public boolean hasPermission(@Nonnull Subject subject,
@Nonnull Resource resource,
@Nonnull BuiltInAction builtInAction,
ExecutionContext executionContext) {
try {
GetAuthorizationStatusResponse response = getAuthorizationStatusExecutor.execute(new GetAuthorizationStatusRequest(resource, subject, builtInAction.getActionId()),
executionContext)
.get();
return response.authorizationStatus().equals(AuthorizationStatus.AUTHORIZED);

} catch (InterruptedException | ExecutionException e) {
LOGGER.error("Error when getting authorization status", e);
return false;
}

}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package edu.stanford.protege.webprotege.gateway.websocket;

import com.fasterxml.jackson.databind.ObjectMapper;
import edu.stanford.protege.webprotege.event.EventTag;
import edu.stanford.protege.webprotege.gateway.websocket.dto.EventList;
import edu.stanford.protege.webprotege.gateway.websocket.dto.PackagedProjectChangeEvent;
import edu.stanford.protege.webprotege.gateway.websocket.dto.ProjectEventsQueryResponse;
import edu.stanford.protege.webprotege.ipc.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;

@Component
public class ProjectChangedEmitterHandler implements EventHandler<PackagedProjectChangeEvent> {

private final static Logger LOGGER = LoggerFactory.getLogger(ProjectChangedEmitterHandler.class);

private final SimpMessagingTemplate simpMessagingTemplate;

private final ObjectMapper objectMapper;

public ProjectChangedEmitterHandler(SimpMessagingTemplate simpMessagingTemplate, ObjectMapper objectMapper) {
this.simpMessagingTemplate = simpMessagingTemplate;
this.objectMapper = objectMapper;
}


@Nonnull
@Override
public String getChannelName() {
return "webprotege.events.projects.PackagedProjectChange";
}

@Nonnull
@Override
public String getHandlerName() {
return this.getClass().getName();
}

@Override
public Class<PackagedProjectChangeEvent> getEventClass() {
return PackagedProjectChangeEvent.class;
}

@Override
public void handleEvent(PackagedProjectChangeEvent event) {
try {
ProjectEventsQueryResponse response = new ProjectEventsQueryResponse();
response.events = new EventList(EventTag.getFirst(), event.projectEvents(), EventTag.get(1));
simpMessagingTemplate.send("/topic/project-events/" + event.projectId().id(), new GenericMessage<>(objectMapper.writeValueAsBytes(response)));

} catch (Exception e) {
LOGGER.error("Error forwarding the events through websocket");
}
}
}
Loading

0 comments on commit 2efaf74

Please sign in to comment.