From 4d9ef6d3c7997da5eb75c5026f638bd4ead1a0cf Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Wed, 7 Jun 2023 10:39:40 +0200 Subject: [PATCH] Implemented new POJO in all XML controllers --- .../tools/controller/XQueryController.java | 51 ++++++++----------- .../r11/tools/controller/XsdController.java | 14 ++--- .../r11/tools/controller/XsltController.java | 30 ++++------- 3 files changed, 35 insertions(+), 60 deletions(-) diff --git a/Backend/tools-services/src/main/java/com/r11/tools/controller/XQueryController.java b/Backend/tools-services/src/main/java/com/r11/tools/controller/XQueryController.java index befe5ce..eb3499a 100644 --- a/Backend/tools-services/src/main/java/com/r11/tools/controller/XQueryController.java +++ b/Backend/tools-services/src/main/java/com/r11/tools/controller/XQueryController.java @@ -2,10 +2,7 @@ package com.r11.tools.controller; import com.google.gson.Gson; 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; -import com.r11.tools.controller.internal.ScopedControllerManifest; +import com.r11.tools.controller.internal.*; import com.r11.tools.xml.XmlEngine; import org.apache.logging.log4j.Logger; import spark.Request; @@ -40,10 +37,10 @@ public class XQueryController implements RestController { @ScopedControllerManifest(method = HandlerType.POST, path = "/xquery") public void transform(Request request, Response response) { - JsonObject requestBody; + XMLRequestBody requestBody; JsonObject responseJson; try { - requestBody = this.gson.fromJson(request.body(), JsonObject.class); + requestBody = this.gson.fromJson(request.body(), XMLRequestBody.class); } catch (Exception e) { responseJson = prepareErrorResponse(e.getMessage(), "N/A"); @@ -52,14 +49,9 @@ public class XQueryController implements RestController { return; } - String data = requestBody.get("data").getAsString(); - String query = requestBody.get("process").getAsString(); - String processor = requestBody.get("processor").getAsString(); - String version = requestBody.get("version").getAsString(); - try { - if (processor.equals("saxon")) { - processWithSaxon(response, data, query, version); + if (requestBody.getProcessor().equals("saxon")) { + processWithSaxon(response, requestBody); } else response.body("saxon"); @@ -73,35 +65,32 @@ public class XQueryController implements RestController { } - private void processWithSaxon(Response response, String data, String query, String version) { + private void processWithSaxon(Response response, XMLRequestBody requestBody) { JsonObject responseJson = new JsonObject(); - long duration; - String result; - long timeStart; - timeStart = System.currentTimeMillis(); + long timeStart = System.currentTimeMillis(); try { - result = saxon.executeXQuery(data, query, version); + String result = saxon.executeXQuery(requestBody.getData(), requestBody.getProcess(), requestBody.getVersion()); response.status(200); responseJson.addProperty("result", result); responseJson.addProperty("status", "OK"); + responseJson.addProperty("processor", "Saxon " + saxon.getVersion()); + + long duration = System.currentTimeMillis() - timeStart; + responseJson.addProperty("time", duration); + + this.logger.info("Request (XQuery, Saxon) processed in " + duration + " ms."); } catch (Exception ex) { - this.logger.error("Error on processing XQuery using Saxon. " + ex); - response.status(400); - - responseJson.addProperty("result", ex.getMessage()); - responseJson.addProperty("status", "ERR"); + responseJson = prepareErrorResponse(ex.getMessage(), saxon.getVersion()); + + this.logger.error("Error on processing XQuery using Saxon. " + ex); + } + finally { + response.body(this.gson.toJson(responseJson)); } - duration = System.currentTimeMillis() - timeStart; - this.logger.info("Request (XQuery, Saxon) processed in " + duration + " ms."); - - responseJson.addProperty("processor", "Saxon " + saxon.getVersion()); - responseJson.addProperty("time", duration); - - response.body(this.gson.toJson(responseJson)); } } diff --git a/Backend/tools-services/src/main/java/com/r11/tools/controller/XsdController.java b/Backend/tools-services/src/main/java/com/r11/tools/controller/XsdController.java index db801d6..854684d 100644 --- a/Backend/tools-services/src/main/java/com/r11/tools/controller/XsdController.java +++ b/Backend/tools-services/src/main/java/com/r11/tools/controller/XsdController.java @@ -2,10 +2,7 @@ package com.r11.tools.controller; import com.google.gson.Gson; 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; -import com.r11.tools.controller.internal.ScopedControllerManifest; +import com.r11.tools.controller.internal.*; import com.r11.tools.xml.Xalan; import com.r11.tools.xml.XmlEngine; import org.apache.logging.log4j.Logger; @@ -38,9 +35,9 @@ public class XsdController implements RestController { public Response transform(Request request, Response response) { String body = request.body(); - JsonObject requestJson; + XMLRequestBody requestBody; try { - requestJson = this.gson.fromJson(body, JsonObject.class); + requestBody = this.gson.fromJson(body, XMLRequestBody.class); } catch (Exception e) { JsonObject responseJson = prepareErrorResponse(e.getMessage(), "N/A"); @@ -49,13 +46,10 @@ public class XsdController implements RestController { return response; } - String data = requestJson.get("data").getAsString(); - String xsd = requestJson.get("process").getAsString(); - JsonObject responseJson = new JsonObject(); try { long timeStart = System.currentTimeMillis(); - String result = xalan.validate(data, xsd).trim(); + String result = xalan.validate(requestBody.getData(), requestBody.getProcess()).trim(); response.status(200); diff --git a/Backend/tools-services/src/main/java/com/r11/tools/controller/XsltController.java b/Backend/tools-services/src/main/java/com/r11/tools/controller/XsltController.java index f1f1c8a..0e15753 100644 --- a/Backend/tools-services/src/main/java/com/r11/tools/controller/XsltController.java +++ b/Backend/tools-services/src/main/java/com/r11/tools/controller/XsltController.java @@ -2,10 +2,7 @@ package com.r11.tools.controller; import com.google.gson.Gson; 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; -import com.r11.tools.controller.internal.ScopedControllerManifest; +import com.r11.tools.controller.internal.*; import com.r11.tools.xml.XmlEngine; import org.apache.logging.log4j.Logger; import spark.Request; @@ -39,9 +36,9 @@ public class XsltController implements RestController { public void transform(Request request, Response response) { String body = request.body(); - JsonObject requestJson; + XMLRequestBody requestBody; try { - requestJson = this.gson.fromJson(body, JsonObject.class); + requestBody = this.gson.fromJson(body, XMLRequestBody.class); } catch (Exception e) { JsonObject responseJson = prepareErrorResponse(e.getMessage(), "N/A"); @@ -50,23 +47,18 @@ public class XsltController implements RestController { return; } - String data = requestJson.get("data").getAsString(); - String query = requestJson.get("process").getAsString(); - String processor = requestJson.get("processor").getAsString(); - String version = requestJson.get("version").getAsString(); - - if (processor == null) { + if (requestBody.getProcessor() == null) { response.body("saxon, xalan"); return; } JsonObject responseJson = new JsonObject(); - switch (processor) { + switch (requestBody.getProcessor()) { case "saxon": - processWithSaxon(response, data, query, version, responseJson); + processWithSaxon(response, requestBody, responseJson); return; case "xalan": - processWithXalan(response, data, query, responseJson); + processWithXalan(response, requestBody, responseJson); return; default: @@ -74,10 +66,10 @@ public class XsltController implements RestController { } } - private void processWithXalan(Response response, String data, String query, JsonObject responseJson) { + private void processWithXalan(Response response, XMLRequestBody requestBody, JsonObject responseJson) { long timeStart = System.currentTimeMillis(); try { - String result = xalan.processXSLT(data, query); + String result = xalan.processXSLT(requestBody.getData(), requestBody.getProcess()); response.status(200); @@ -100,10 +92,10 @@ public class XsltController implements RestController { } - private void processWithSaxon(Response response, String data, String query, String version, JsonObject responseJson) { + private void processWithSaxon(Response response, XMLRequestBody requestBody, JsonObject responseJson) { long timeStart = System.currentTimeMillis(); try { - String result = saxon.processXSLT(data, query); + String result = saxon.processXSLT(requestBody.getData(), requestBody.getProcess()); response.status(200);