From ab2cdc135ad2fbf5076600dd0fdbdf028d53981f Mon Sep 17 00:00:00 2001 From: Gayathri Date: Tue, 7 May 2024 13:55:59 +0530 Subject: [PATCH] Enable CORS requests for REPLGridServer --- .../legend-engine-repl-relational/pom.xml | 2 +- .../relational/httpServer/ReplGridServer.java | 26 ++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/legend-engine-config/legend-engine-repl/legend-engine-repl-relational/pom.xml b/legend-engine-config/legend-engine-repl/legend-engine-repl-relational/pom.xml index 7d84d7224f..e28c8a957f 100644 --- a/legend-engine-config/legend-engine-repl/legend-engine-repl-relational/pom.xml +++ b/legend-engine-config/legend-engine-repl/legend-engine-repl-relational/pom.xml @@ -28,7 +28,7 @@ false - 11.37.0 + 11.42.0 ${npm.registry.url}/@finos/legend-application-repl-deployment/-/legend-application-repl-deployment-${repl.web-application.version}.tgz diff --git a/legend-engine-config/legend-engine-repl/legend-engine-repl-relational/src/main/java/org/finos/legend/engine/repl/relational/httpServer/ReplGridServer.java b/legend-engine-config/legend-engine-repl/legend-engine-repl-relational/src/main/java/org/finos/legend/engine/repl/relational/httpServer/ReplGridServer.java index bb0f4c2f23..1bf3ddb811 100644 --- a/legend-engine-config/legend-engine-repl/legend-engine-repl-relational/src/main/java/org/finos/legend/engine/repl/relational/httpServer/ReplGridServer.java +++ b/legend-engine-config/legend-engine-repl/legend-engine-repl-relational/src/main/java/org/finos/legend/engine/repl/relational/httpServer/ReplGridServer.java @@ -17,6 +17,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectMapper; +import com.sun.net.httpserver.Headers; import org.eclipse.collections.api.RichIterable; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; @@ -118,6 +119,7 @@ public void initializeServer() throws Exception @Override public void handle(HttpExchange exchange) throws IOException { + handleCORS(exchange); if ("GET".equals(exchange.getRequestMethod())) { try @@ -143,6 +145,7 @@ public void handle(HttpExchange exchange) throws IOException @Override public void handle(HttpExchange exchange) throws IOException { + handleCORS(exchange); if ("GET".equals(exchange.getRequestMethod())) { try @@ -172,6 +175,7 @@ public void handle(HttpExchange exchange) throws IOException @Override public void handle(HttpExchange exchange) throws IOException { + handleCORS(exchange); if ("GET".equals(exchange.getRequestMethod())) { try @@ -195,6 +199,7 @@ public void handle(HttpExchange exchange) throws IOException @Override public void handle(HttpExchange exchange) throws IOException { + handleCORS(exchange); if ("POST".equals(exchange.getRequestMethod())) { try @@ -226,6 +231,7 @@ public void handle(HttpExchange exchange) throws IOException @Override public void handle(HttpExchange exchange) throws IOException { + handleCORS(exchange); if ("POST".equals(exchange.getRequestMethod())) { try @@ -257,6 +263,7 @@ public void handle(HttpExchange exchange) throws IOException @Override public void handle(HttpExchange exchange) throws IOException { + handleCORS(exchange); if ("POST".equals(exchange.getRequestMethod())) { try @@ -280,6 +287,7 @@ public void handle(HttpExchange exchange) throws IOException @Override public void handle(HttpExchange exchange) throws IOException { + handleCORS(exchange); if ("GET".equals(exchange.getRequestMethod())) { ValueSpecification funcBody = null; @@ -429,4 +437,20 @@ private static boolean checkIfPaginationIsEnabled(String queryParamsString) } return queryParams.get("isPaginationEnabled").equals("true") ? true : false; } -} + + private static void handleCORS(HttpExchange exchange) throws IOException + { + Headers headers = exchange.getResponseHeaders(); + headers.add("Access-Control-Allow-Origin", "http://localhost:9005"); + headers.add("allowedMethods", "GET, POST, OPTIONS"); + headers.add("Access-Control-Allow-Credentials", "true"); + headers.add("allowedTimingOrigins", "true"); + headers.add("chainPreflight", "false"); + headers.add("Access-Control-Allow-Headers", "Access-Control-Allow-Headers, Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers"); + + if ("OPTIONS".equals(exchange.getRequestMethod())) + { + exchange.sendResponseHeaders(204, -1); + } + } + }