From 424bb57c424b76a5f2ffc37e7af854ec252b9097 Mon Sep 17 00:00:00 2001 From: Adam Bem Date: Wed, 7 Jun 2023 10:17:47 +0200 Subject: [PATCH] Tidied up code --- .../r11/tools/controller/XPathController.java | 76 +++++++++---------- .../tools/controller/XQueryController.java | 4 + .../r11/tools/controller/XsdController.java | 42 +++++----- .../r11/tools/controller/XsltController.java | 75 +++++++++--------- 4 files changed, 94 insertions(+), 103 deletions(-) diff --git a/Backend/tools-services/src/main/java/com/r11/tools/controller/XPathController.java b/Backend/tools-services/src/main/java/com/r11/tools/controller/XPathController.java index a188566..14e4847 100644 --- a/Backend/tools-services/src/main/java/com/r11/tools/controller/XPathController.java +++ b/Backend/tools-services/src/main/java/com/r11/tools/controller/XPathController.java @@ -24,6 +24,15 @@ public class XPathController implements RestController { this.xalan = xalan; } + private JsonObject prepareErrorResponse(String message, String processor) { + JsonObject result = new JsonObject(); + result.addProperty("result", message); + result.addProperty("processor", processor); + result.addProperty("status", "ERR"); + result.addProperty("time", "N/A"); + return result; + } + @ScopedControllerManifest(method = HandlerType.POST, path = "/xpath") public void transform(Request request, Response response) { String body = request.body(); @@ -32,12 +41,7 @@ public class XPathController implements RestController { try { requestJson = this.gson.fromJson(body, JsonObject.class); } catch (Exception e) { - JsonObject responseJson = new JsonObject(); - responseJson.addProperty("result", e.getMessage()); - responseJson.addProperty("processor", "N/A"); - responseJson.addProperty("status", "ERR"); - responseJson.addProperty("time", "N/A"); - + JsonObject responseJson = prepareErrorResponse(e.getMessage(), "N/A"); response.status(400); response.body(this.gson.toJson(responseJson)); return; @@ -67,11 +71,7 @@ public class XPathController implements RestController { } private void processWithXalan(Response response, String data, String query, JsonObject responseJson) { - long timeStart; - long duration; - response.header("processor", xalan.getVersion()); - timeStart = System.currentTimeMillis(); - + long timeStart = System.currentTimeMillis(); try { XPathQueryResult xPathQueryResult = xalan.processXPath(data, query, ""); @@ -80,53 +80,45 @@ public class XPathController implements RestController { responseJson.addProperty("result", xPathQueryResult.getData().trim()); responseJson.addProperty("status", "OK"); responseJson.addProperty("type", xPathQueryResult.getType()); - } catch (Exception ex) { - this.logger.error("Error on processing XPath using Xalan. " + ex); + responseJson.addProperty("processor", xalan.getVersion()); + long duration = System.currentTimeMillis() - timeStart; + responseJson.addProperty("time", duration); + + this.logger.info("Request (XPath, Xalan) processed in " + duration + " ms."); + } catch (Exception ex) { + responseJson = prepareErrorResponse(ex.getMessage(), xalan.getVersion()); response.status(400); - responseJson.addProperty("result", ex.getMessage()); - responseJson.addProperty("status", "ERR"); + this.logger.error("Error on processing XPath using Xalan. " + ex); + } finally { + response.body(this.gson.toJson(responseJson)); } - duration = System.currentTimeMillis() - timeStart; - this.logger.info("Request (XPath, Xalan) processed in " + duration + " ms."); - - responseJson.addProperty("processor", xalan.getVersion()); - responseJson.addProperty("time", duration); - - response.body(this.gson.toJson(responseJson)); } private void processWithSaxon(Response response, String data, String query, String version, JsonObject responseJson) { - long timeStart; - String tmp; - long duration; - response.header("processor", "Saxon " + saxon.getVersion() + " " + version + " over s9api"); - timeStart = System.currentTimeMillis(); - + long timeStart = System.currentTimeMillis(); try { - tmp = saxon.processXPath(data, query, version).getData().trim(); + String result = saxon.processXPath(data, query, version).getData().trim(); response.status(200); - responseJson.addProperty("result", tmp); + responseJson.addProperty("result", result); responseJson.addProperty("status", "OK"); + responseJson.addProperty("processor", "Saxon " + saxon.getVersion() + " " + version + " over s9api"); + + long duration = System.currentTimeMillis() - timeStart; + responseJson.addProperty("time", duration); + + this.logger.info("Request (XPath, Saxon) processed in " + duration + " ms."); } catch (Exception ex) { - this.logger.error("Error on processing XPath using Saxon. " + ex); - + responseJson = prepareErrorResponse(ex.getMessage(), saxon.getVersion()); response.status(400); - - responseJson.addProperty("result", ex.getMessage()); - responseJson.addProperty("status", "ERR"); + this.logger.error("Error on processing XPath using Saxon. " + ex); + } finally { + response.body(this.gson.toJson(responseJson)); } - duration = System.currentTimeMillis() - timeStart; - this.logger.info("Request (XPath, Saxon) processed in " + duration + " ms."); - - responseJson.addProperty("processor", "Saxon " + saxon.getVersion() + " " + version + " over s9api"); - responseJson.addProperty("time", duration); - - response.body(this.gson.toJson(responseJson)); } } 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 5bf7206..befe5ce 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 @@ -11,6 +11,10 @@ import org.apache.logging.log4j.Logger; import spark.Request; import spark.Response; +/** + * Controller used to handle XQuery tool. Currently, it supports Saxon engine + * @author Adam Bem + */ @GlobalControllerManifest public class XQueryController implements RestController { 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 3a04ce6..db801d6 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 @@ -26,6 +26,14 @@ public class XsdController implements RestController { this.xalan = xalan; } + private JsonObject prepareErrorResponse(String message, String processor) { + JsonObject result = new JsonObject(); + result.addProperty("result", message); + result.addProperty("processor", processor); + result.addProperty("status", "ERR"); + result.addProperty("time", "N/A"); + return result; + } @ScopedControllerManifest(method = HandlerType.POST, path = "/xsd") public Response transform(Request request, Response response) { String body = request.body(); @@ -34,11 +42,7 @@ public class XsdController implements RestController { try { requestJson = this.gson.fromJson(body, JsonObject.class); } catch (Exception e) { - JsonObject responseJson = new JsonObject(); - responseJson.addProperty("result", e.getMessage()); - responseJson.addProperty("processor", "N/A"); - responseJson.addProperty("status", "ERR"); - responseJson.addProperty("time", "N/A"); + JsonObject responseJson = prepareErrorResponse(e.getMessage(), "N/A"); response.status(400); response.body(this.gson.toJson(responseJson)); @@ -48,34 +52,28 @@ public class XsdController implements RestController { String data = requestJson.get("data").getAsString(); String xsd = requestJson.get("process").getAsString(); - response.header("processor", xalan.getVersion()); - - long timeStart = System.currentTimeMillis(); - String tmp; - JsonObject responseJson = new JsonObject(); try { - tmp = xalan.validate(data, xsd).trim(); + long timeStart = System.currentTimeMillis(); + String result = xalan.validate(data, xsd).trim(); response.status(200); - responseJson.addProperty("result", tmp); + responseJson.addProperty("result", result); responseJson.addProperty("status", "OK"); - } catch (Exception ex) { - this.logger.error("Error on validation against XSD using Xalan. " + ex); + responseJson.addProperty("processor", xalan.getVersion()); + long duration = System.currentTimeMillis() - timeStart; + responseJson.addProperty("time", duration); + + this.logger.info("Request (XSD, Xalan) processed in " + duration + " ms."); + } catch (Exception ex) { + responseJson = prepareErrorResponse(ex.getMessage(), xalan.getVersion()); response.status(400); - responseJson.addProperty("result", ex.getMessage()); - responseJson.addProperty("status", "ERR"); + this.logger.error("Error on validation against XSD using Xalan. " + ex); } - long duration = System.currentTimeMillis() - timeStart; - this.logger.info("Request (XSD, Xalan) processed in " + duration + " ms."); - - responseJson.addProperty("processor", xalan.getVersion()); - responseJson.addProperty("time", duration); - response.body(this.gson.toJson(responseJson)); return response; } 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 d318b0f..f1f1c8a 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 @@ -27,6 +27,14 @@ public class XsltController implements RestController { this.xalan = xalan; } + private JsonObject prepareErrorResponse(String message, String processor) { + JsonObject result = new JsonObject(); + result.addProperty("result", message); + result.addProperty("processor", processor); + result.addProperty("status", "ERR"); + result.addProperty("time", "N/A"); + return result; + } @ScopedControllerManifest(method = HandlerType.POST, path = "/xslt") public void transform(Request request, Response response) { String body = request.body(); @@ -35,11 +43,7 @@ public class XsltController implements RestController { try { requestJson = this.gson.fromJson(body, JsonObject.class); } catch (Exception e) { - JsonObject responseJson = new JsonObject(); - responseJson.addProperty("result", e.getMessage()); - responseJson.addProperty("processor", "N/A"); - responseJson.addProperty("status", "ERR"); - responseJson.addProperty("time", "N/A"); + JsonObject responseJson = prepareErrorResponse(e.getMessage(), "N/A"); response.status(400); response.body(this.gson.toJson(responseJson)); @@ -71,62 +75,55 @@ public class XsltController implements RestController { } private void processWithXalan(Response response, String data, String query, JsonObject responseJson) { - long duration; - long timeStart; - String tmp; - timeStart = System.currentTimeMillis(); + long timeStart = System.currentTimeMillis(); try { - tmp = xalan.processXSLT(data, query); + String result = xalan.processXSLT(data, query); response.status(200); - responseJson.addProperty("result", tmp); + responseJson.addProperty("result", result); responseJson.addProperty("status", "OK"); + responseJson.addProperty("processor", xalan.getVersion()); + + long duration = System.currentTimeMillis() - timeStart; + responseJson.addProperty("time", duration); + + this.logger.info("Request (XSLT, Xalan) processed in " + duration + " ms."); } catch (Exception ex) { + responseJson = prepareErrorResponse(ex.getMessage(), xalan.getVersion()); + response.status(400); this.logger.error("Error on processing XSLT using Xalan. " + ex); - response.status(400); - - responseJson.addProperty("result", ex.getMessage()); - responseJson.addProperty("status", "ERR"); + } finally { + response.body(this.gson.toJson(responseJson)); } - duration = System.currentTimeMillis() - timeStart; - this.logger.info("Request (XSLT, Xalan) processed in " + duration + " ms."); - - responseJson.addProperty("processor", xalan.getVersion()); - responseJson.addProperty("time", duration); - - response.body(this.gson.toJson(responseJson)); } private void processWithSaxon(Response response, String data, String query, String version, JsonObject responseJson) { - long duration; - String tmp; - long timeStart; - timeStart = System.currentTimeMillis(); + long timeStart = System.currentTimeMillis(); try { - tmp = saxon.processXSLT(data, query); + String result = saxon.processXSLT(data, query); response.status(200); - responseJson.addProperty("result", tmp); + responseJson.addProperty("result", result); responseJson.addProperty("status", "OK"); - } catch (Exception ex) { - this.logger.error("Error on processing XSLT using Saxon. " + ex); + responseJson.addProperty("processor", "Saxon " + saxon.getVersion()); + long duration = System.currentTimeMillis() - timeStart; + responseJson.addProperty("time", duration); + this.logger.info("Request (XSLT, Saxon) processed in " + duration + " ms."); + + } catch (Exception ex) { + responseJson = prepareErrorResponse(ex.getMessage(), saxon.getVersion()); response.status(400); - responseJson.addProperty("result", ex.getMessage()); - responseJson.addProperty("status", "ERR"); + this.logger.error("Error on processing XSLT using Saxon. " + ex); + } + finally { + response.body(this.gson.toJson(responseJson)); } - duration = System.currentTimeMillis() - timeStart; - this.logger.info("Request (XSLT, Saxon) processed in " + duration + " ms."); - - responseJson.addProperty("processor", "Saxon " + saxon.getVersion()); - responseJson.addProperty("time", duration); - - response.body(this.gson.toJson(responseJson)); } }