From 753650f81cccc592b7de5d98842c51e4093fa57d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20Ko=C5=82ecki?= Date: Wed, 1 Mar 2023 16:02:38 +0100 Subject: [PATCH] Working json formatter with xml tags support. --- Backend/tools-services/pom.xml | 6 + .../r11/tools/controller/JsonController.java | 44 ++++- .../internal/RestControllerRegistry.java | 7 +- .../controller/internal/path/PathBuilder.java | 30 ++++ Frontend/assets/scripts/tools/json.js | 35 ++-- Frontend/tools/jsonFormatter.html | 161 ++++++++++++++++++ 6 files changed, 268 insertions(+), 15 deletions(-) create mode 100644 Backend/tools-services/src/main/java/com/r11/tools/controller/internal/path/PathBuilder.java diff --git a/Backend/tools-services/pom.xml b/Backend/tools-services/pom.xml index 9ac56d7..6e02cfc 100644 --- a/Backend/tools-services/pom.xml +++ b/Backend/tools-services/pom.xml @@ -15,6 +15,7 @@ 2.14.1 2.0.6 2.19.0 + 2.10.1 @@ -68,6 +69,11 @@ + + com.google.code.gson + gson + ${gson.version} + com.fasterxml.jackson.core jackson-core diff --git a/Backend/tools-services/src/main/java/com/r11/tools/controller/JsonController.java b/Backend/tools-services/src/main/java/com/r11/tools/controller/JsonController.java index 1e68949..8648ed8 100644 --- a/Backend/tools-services/src/main/java/com/r11/tools/controller/JsonController.java +++ b/Backend/tools-services/src/main/java/com/r11/tools/controller/JsonController.java @@ -1,5 +1,8 @@ package com.r11.tools.controller; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; import com.r11.tools.controller.internal.GlobalControllerManifest; import com.r11.tools.controller.internal.HandlerType; import com.r11.tools.controller.internal.RestController; @@ -7,11 +10,46 @@ import com.r11.tools.controller.internal.ScopedControllerManifest; import spark.Request; import spark.Response; -@GlobalControllerManifest +@GlobalControllerManifest(path = "/json") public class JsonController implements RestController { - @ScopedControllerManifest(method = HandlerType.GET, path = "/json") + private final Gson prettyGson = new GsonBuilder() + .disableHtmlEscaping() + .setPrettyPrinting() + .create(); + + private final Gson gson = new GsonBuilder() + .disableHtmlEscaping() + .create(); + + @ScopedControllerManifest(method = HandlerType.POST, path = "/formatting") public void formatting(Request request, Response response) { - response.body("Hello World!"); + try { + JsonObject jsonObject = this.gson.fromJson(request.body(), JsonObject.class); + response.status(200); + response.body(this.prettyGson.toJson(jsonObject)); + System.out.printf(response.body()); + } catch (Exception e) { + response.status(500); + Throwable cause = e.getCause(); + if (cause == null) { + response.body(e.getMessage()); + } else { + response.body(cause.getMessage()); + } + System.out.printf(response.body()); + } + } + + @ScopedControllerManifest(method = HandlerType.POST, path = "/minimize") + public void minimize(Request request, Response response) { + try { + JsonObject jsonObject = this.prettyGson.fromJson(request.body(), JsonObject.class); + response.status(200); + response.body(this.gson.toJson(jsonObject)); + } catch (Exception e) { + response.status(500); + response.body(e.getMessage()); + } } } diff --git a/Backend/tools-services/src/main/java/com/r11/tools/controller/internal/RestControllerRegistry.java b/Backend/tools-services/src/main/java/com/r11/tools/controller/internal/RestControllerRegistry.java index f27bacd..88bd537 100644 --- a/Backend/tools-services/src/main/java/com/r11/tools/controller/internal/RestControllerRegistry.java +++ b/Backend/tools-services/src/main/java/com/r11/tools/controller/internal/RestControllerRegistry.java @@ -1,5 +1,6 @@ package com.r11.tools.controller.internal; +import com.r11.tools.controller.internal.path.PathBuilder; import java.lang.reflect.Method; import java.util.HashSet; import java.util.Set; @@ -33,7 +34,11 @@ public class RestControllerRegistry { (method.isAnnotationPresent(ScopedControllerManifest.class)) ) { HandlerType handlerType = method.getAnnotation(ScopedControllerManifest.class).method(); - String path = method.getAnnotation(ScopedControllerManifest.class).path(); + + String path = PathBuilder.resolvePathOf( + parent.getAnnotation(GlobalControllerManifest.class).path(), + method.getAnnotation(ScopedControllerManifest.class).path() + ); switch (handlerType) { case GET: diff --git a/Backend/tools-services/src/main/java/com/r11/tools/controller/internal/path/PathBuilder.java b/Backend/tools-services/src/main/java/com/r11/tools/controller/internal/path/PathBuilder.java new file mode 100644 index 0000000..9f6c10f --- /dev/null +++ b/Backend/tools-services/src/main/java/com/r11/tools/controller/internal/path/PathBuilder.java @@ -0,0 +1,30 @@ +package com.r11.tools.controller.internal.path; + +public final class PathBuilder { + + private static final String PATH_SEPARATOR = "/"; + + private PathBuilder() { + + } + + public static String resolvePathOf(String globalPath, String scopedPath) { + String resolvedPath = + PathBuilder.removeTrailingPathSeparator(globalPath) + + PathBuilder.removeTrailingPathSeparator(scopedPath); + + if (resolvedPath.endsWith(PATH_SEPARATOR)) { + resolvedPath = resolvedPath.substring(0, resolvedPath.length() - 1); + } + + return PATH_SEPARATOR + resolvedPath; + } + + private static String removeTrailingPathSeparator(String path) { + if (path.endsWith(PATH_SEPARATOR)) { + return path.substring(0, path.length() - 1); + } + + return path; + } +} \ No newline at end of file diff --git a/Frontend/assets/scripts/tools/json.js b/Frontend/assets/scripts/tools/json.js index 6e448b1..1ce3473 100644 --- a/Frontend/assets/scripts/tools/json.js +++ b/Frontend/assets/scripts/tools/json.js @@ -1,21 +1,33 @@ function formatAndValidateJson(errorElement) { const input = document.querySelector('#jsonBlock'); const processInfo = document.getElementById(errorElement); + const start = new Date(); - try { - const start = new Date(); + const address = window.location.protocol + "//" + window.location.hostname + ":" + 8081 + "/json/formatting" - const obj = JSON.parse(input.textContent); - input.innerHTML = JSON.stringify(obj, null, 2); + fetch(address, { + method: 'POST', + body: input.textContent + }) + .then(async (response) => { + if (!response.ok) { + throw Error(await response.text()); + } + + return response.text(); + }) + .then((data) => { + input.innerText = data; processInfo.innerText = ""; hljs.highlightElement(input); - - const end = new Date(); - processInfo.innerHTML = "Validation and formatting time: " + (end.getMilliseconds() - start.getMilliseconds()) + "ms"; - } catch (error) { + }) + .catch((error) => { processInfo.innerHTML = "" + error + ""; - console.error("Error: ", error) - } + console.error('Error:', error); + }); + + const end = new Date(); + processInfo.innerHTML = "Validation and formatting time: " + (end.getMilliseconds() - start.getMilliseconds()) + "ms"; } function minimizeJson(errorElement) { @@ -31,7 +43,8 @@ function minimizeJson(errorElement) { hljs.highlightElement(input); const end = new Date(); - processInfo.innerHTML = "Validation and formatting time: " + (end.getMilliseconds() - start.getMilliseconds()) + "ms"; + processInfo.innerHTML = "Validation and formatting time: " + + (end.getMilliseconds() - start.getMilliseconds()) + "ms"; } catch (error) { processInfo.innerHTML = "" + error + ""; console.error("Error: ", error) diff --git a/Frontend/tools/jsonFormatter.html b/Frontend/tools/jsonFormatter.html index 4935d5c..8fe6ee2 100644 --- a/Frontend/tools/jsonFormatter.html +++ b/Frontend/tools/jsonFormatter.html @@ -52,6 +52,167 @@