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 be93500..76fab58 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 @@ -4,6 +4,7 @@ import com.google.gson.Gson; import com.google.gson.JsonObject; import com.r11.tools.controller.internal.*; import com.r11.tools.model.XMLRequestBody; +import com.r11.tools.model.XMLResponseBody; import com.r11.tools.xml.XmlEngine; import org.apache.logging.log4j.Logger; import spark.Request; @@ -25,13 +26,8 @@ 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; + private XMLResponseBody prepareErrorResponse(String message, String processor) { + return new XMLResponseBody(message, "ERR", processor, -1); } @ScopedControllerManifest(method = HandlerType.POST, path = "/xslt") public void transform(Request request, Response response) { @@ -41,10 +37,10 @@ public class XsltController implements RestController { try { requestBody = this.gson.fromJson(body, XMLRequestBody.class); } catch (Exception e) { - JsonObject responseJson = prepareErrorResponse(e.getMessage(), "N/A"); + XMLResponseBody responseBody = prepareErrorResponse(e.getMessage(), "N/A"); response.status(400); - response.body(this.gson.toJson(responseJson)); + response.body(this.gson.toJson(responseBody)); return; } @@ -68,56 +64,46 @@ public class XsltController implements RestController { } private void processWithXalan(Response response, XMLRequestBody requestBody) { - JsonObject responseJson = new JsonObject(); + XMLResponseBody responseBody = null; long timeStart = System.currentTimeMillis(); try { String result = xalan.processXSLT(requestBody.getData(), requestBody.getProcess()); - response.status(200); - responseJson.addProperty("result", result); - responseJson.addProperty("status", "OK"); - responseJson.addProperty("processor", xalan.getVersion()); - long duration = System.currentTimeMillis() - timeStart; - responseJson.addProperty("time", duration); + responseBody = new XMLResponseBody(result, "OK", xalan.getVersion(), duration); this.logger.info("Request (XSLT, Xalan) processed in " + duration + " ms."); } catch (Exception ex) { - responseJson = prepareErrorResponse(ex.getMessage(), xalan.getVersion()); + responseBody = prepareErrorResponse(ex.getMessage(), xalan.getVersion()); response.status(400); this.logger.error("Error on processing XSLT using Xalan. " + ex); } finally { - response.body(this.gson.toJson(responseJson)); + response.body(this.gson.toJson(responseBody)); } } private void processWithSaxon(Response response, XMLRequestBody requestBody) { - JsonObject responseJson = new JsonObject(); + XMLResponseBody responseBody = null; long timeStart = System.currentTimeMillis(); try { String result = saxon.processXSLT(requestBody.getData(), requestBody.getProcess()); - 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); + responseBody = new XMLResponseBody(result, "OK", saxon.getVersion(), duration); this.logger.info("Request (XSLT, Saxon) processed in " + duration + " ms."); } catch (Exception ex) { - responseJson = prepareErrorResponse(ex.getMessage(), saxon.getVersion()); + responseBody = prepareErrorResponse(ex.getMessage(), saxon.getVersion()); response.status(400); this.logger.error("Error on processing XSLT using Saxon. " + ex); } finally { - response.body(this.gson.toJson(responseJson)); + response.body(this.gson.toJson(responseBody)); } }