Added XQuery Tool and refactored tools-service (#220)
Co-authored-by: Adam Bem <adam.bem@zoho.eu> Reviewed-on: #220 Reviewed-by: Mikolaj Widla <widlam@noreply.example.com>
This commit is contained in:
		| @@ -2,11 +2,7 @@ package com.r11.tools; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
| import com.google.gson.GsonBuilder; | ||||
| import com.r11.tools.controller.JsonController; | ||||
| import com.r11.tools.controller.ProcessorInfoController; | ||||
| import com.r11.tools.controller.XPathController; | ||||
| import com.r11.tools.controller.XsdController; | ||||
| import com.r11.tools.controller.XsltController; | ||||
| import com.r11.tools.controller.*; | ||||
| import com.r11.tools.controller.internal.RestControllerRegistry; | ||||
| import com.r11.tools.xml.Saxon; | ||||
| import com.r11.tools.xml.Xalan; | ||||
| @@ -48,6 +44,7 @@ public class SparkApplication { | ||||
|         registry.registerController(new XPathController(gson, logger, saxon, xalan)); | ||||
|         registry.registerController(new XsltController(gson, logger, saxon, xalan)); | ||||
|         registry.registerController(new JsonController(gson, jsongson, logger)); | ||||
|         registry.registerController(new XQueryController(gson, logger, saxon)); | ||||
|  | ||||
|         registry.register(); | ||||
|  | ||||
|   | ||||
| @@ -1,8 +1,10 @@ | ||||
| package com.r11.tools.controller; | ||||
|  | ||||
| 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.model.XPathQueryResult; | ||||
| import com.r11.tools.xml.XmlEngine; | ||||
| import org.apache.logging.log4j.Logger; | ||||
| import spark.Request; | ||||
| @@ -24,109 +26,69 @@ public class XPathController implements RestController { | ||||
|         this.xalan = xalan; | ||||
|     } | ||||
|  | ||||
|     private XMLResponseBody errorResponse(String message, String processor) { | ||||
|         return new XMLResponseBody(message, "ERR", processor, -1); | ||||
|     } | ||||
|  | ||||
|     private void nonValidEngineSelectedResponse(Response response) { | ||||
|         XMLResponseBody responseBody = | ||||
|                 errorResponse("Valid engines are: saxon, xalan", "N/A"); | ||||
|         response.body(this.gson.toJson(responseBody)); | ||||
|         response.status(400); | ||||
|     } | ||||
|  | ||||
|     @ScopedControllerManifest(method = HandlerType.POST, path = "/xpath") | ||||
|     public void transform(Request request, Response response) { | ||||
|         String body = request.body(); | ||||
|  | ||||
|         JsonObject requestJson; | ||||
|     public void acceptRequest(Request request, Response response) { | ||||
|         XMLRequestBody requestBody; | ||||
|         try { | ||||
|             requestJson = this.gson.fromJson(body, JsonObject.class); | ||||
|             requestBody = this.gson.fromJson(request.body(), XMLRequestBody.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"); | ||||
|  | ||||
|             XMLResponseBody responseBody = errorResponse(e.getMessage(), "N/A"); | ||||
|             response.status(400); | ||||
|             response.body(this.gson.toJson(responseJson)); | ||||
|             response.body(this.gson.toJson(responseBody)); | ||||
|             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) { | ||||
|             response.body("saxon, xalan"); | ||||
|         if (requestBody.getProcessor() == null) { | ||||
|             nonValidEngineSelectedResponse(response); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         JsonObject responseJson = new JsonObject(); | ||||
|         switch (processor) { | ||||
|         switch (requestBody.getProcessor()) { | ||||
|             case "saxon": | ||||
|                 processWithSaxon(response, data, query, version, responseJson); | ||||
|                 process(response, requestBody, saxon); | ||||
|                 break; | ||||
|             case "xalan": | ||||
|                 processWithXalan(response, data, query, responseJson); | ||||
|                 process(response, requestBody, xalan); | ||||
|                 break; | ||||
|             default: | ||||
|                 response.body("saxon, xalan"); | ||||
|                 nonValidEngineSelectedResponse(response); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private void processWithXalan(Response response, String data, String query, JsonObject responseJson) { | ||||
|         long timeStart; | ||||
|         long duration; | ||||
|         response.header("processor", xalan.getVersion()); | ||||
|         timeStart = System.currentTimeMillis(); | ||||
|  | ||||
|     private void process(Response response, XMLRequestBody requestBody, XmlEngine engine) { | ||||
|         long timeStart = System.currentTimeMillis(); | ||||
|         XMLResponseBody responseBody = null; | ||||
|         try { | ||||
|             XPathQueryResult xPathQueryResult = xalan.processXPath(data, query, ""); | ||||
|             XPathQueryResult xPathQueryResult = | ||||
|                     engine.processXPath(requestBody.getData(), requestBody.getProcess(), requestBody.getVersion()); | ||||
|  | ||||
|             response.status(200); | ||||
|  | ||||
|             responseJson.addProperty("result", xPathQueryResult.getData().trim()); | ||||
|             responseJson.addProperty("status", "OK"); | ||||
|             responseJson.addProperty("type", xPathQueryResult.getType()); | ||||
|             long duration = System.currentTimeMillis() - timeStart; | ||||
|             responseBody = new XMLResponseBody(xPathQueryResult.getData().trim(), | ||||
|                     "OK", engine.getVersion(),duration); | ||||
|              | ||||
|             responseBody.setType(xPathQueryResult.getType()); | ||||
|             this.logger.info("Request (XPath, " + engine.getVersion() + ") processed in " + duration + " ms."); | ||||
|         } catch (Exception ex) { | ||||
|             this.logger.error("Error on processing XPath using Xalan. " + ex); | ||||
|  | ||||
|             responseBody = errorResponse(ex.getMessage(), engine.getVersion()); | ||||
|             response.status(400); | ||||
|  | ||||
|             responseJson.addProperty("result", ex.getMessage()); | ||||
|             responseJson.addProperty("status", "ERR"); | ||||
|             this.logger.error("Error on processing XPath using " + engine.getVersion() + ". " + ex); | ||||
|         } finally { | ||||
|             response.body(this.gson.toJson(responseBody)); | ||||
|         } | ||||
|  | ||||
|         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(); | ||||
|  | ||||
|         try { | ||||
|             tmp = saxon.processXPath(data, query, version).getData().trim(); | ||||
|  | ||||
|             response.status(200); | ||||
|  | ||||
|             responseJson.addProperty("result", tmp); | ||||
|             responseJson.addProperty("status", "OK"); | ||||
|         } catch (Exception ex) { | ||||
|             this.logger.error("Error on processing XPath using Saxon. " + ex); | ||||
|  | ||||
|             response.status(400); | ||||
|  | ||||
|             responseJson.addProperty("result", ex.getMessage()); | ||||
|             responseJson.addProperty("status", "ERR"); | ||||
|         } | ||||
|  | ||||
|         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)); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -0,0 +1,87 @@ | ||||
| package com.r11.tools.controller; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
| 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; | ||||
| import spark.Response; | ||||
|  | ||||
| /** | ||||
|  * Controller used to handle XQuery tool. Currently, it supports Saxon engine | ||||
|  * @author Adam Bem | ||||
|  */ | ||||
| @GlobalControllerManifest | ||||
| public class XQueryController implements RestController { | ||||
|  | ||||
|     private final Gson gson; | ||||
|     private final Logger logger; | ||||
|     private final XmlEngine saxon; | ||||
|  | ||||
|     public XQueryController(Gson gson, Logger logger, XmlEngine saxon) { | ||||
|         this.gson = gson; | ||||
|         this.logger = logger; | ||||
|         this.saxon = saxon; | ||||
|     } | ||||
|  | ||||
|     private XMLResponseBody prepareErrorResponse(String message, String processor) { | ||||
|         return new XMLResponseBody(message, "ERR", processor, -1); | ||||
|     } | ||||
|  | ||||
|     private void nonValidEngineSelectedResponse(Response response) { | ||||
|         XMLResponseBody responseBody = | ||||
|                 prepareErrorResponse("Valid engines are: saxon", "N/A"); | ||||
|         response.body(this.gson.toJson(responseBody)); | ||||
|         response.status(400); | ||||
|     } | ||||
|  | ||||
|     @ScopedControllerManifest(method = HandlerType.POST, path = "/xquery") | ||||
|     public void acceptRequest(Request request, Response response) { | ||||
|         XMLRequestBody requestBody; | ||||
|         try { | ||||
|             requestBody = this.gson.fromJson(request.body(), XMLRequestBody.class); | ||||
|         } catch (Exception e) { | ||||
|             XMLResponseBody responseBody = prepareErrorResponse(e.getMessage(), "N/A"); | ||||
|  | ||||
|             response.status(400); | ||||
|             response.body(this.gson.toJson(responseBody)); | ||||
|             return; | ||||
|         } | ||||
|         if (requestBody.getProcessor() == null) { | ||||
|             nonValidEngineSelectedResponse(response); | ||||
|             return; | ||||
|         } | ||||
|          | ||||
|         if (requestBody.getProcessor().equalsIgnoreCase("saxon")) | ||||
|             process(response, requestBody, saxon); | ||||
|         else | ||||
|             nonValidEngineSelectedResponse(response); | ||||
|     } | ||||
|  | ||||
|     private void process(Response response, XMLRequestBody requestBody, XmlEngine engine) { | ||||
|         XMLResponseBody responseBody = null; | ||||
|         long timeStart = System.currentTimeMillis(); | ||||
|         try { | ||||
|             String result = engine.executeXQuery(requestBody.getData(), requestBody.getProcess(), requestBody.getVersion()); | ||||
|  | ||||
|             response.status(200); | ||||
|  | ||||
|             long duration = System.currentTimeMillis() - timeStart; | ||||
|             responseBody = new XMLResponseBody(result, "OK", engine.getVersion(), duration); | ||||
|  | ||||
|             this.logger.info("Request (XQuery, " + engine.getVersion() + ") processed in " + duration + " ms."); | ||||
|         } catch (Exception ex) { | ||||
|             response.status(400); | ||||
|             responseBody = prepareErrorResponse(ex.getMessage(), engine.getVersion()); | ||||
|  | ||||
|             this.logger.error("Error on processing XQuery using " + engine.getVersion() + ". " + ex); | ||||
|         } | ||||
|         finally { | ||||
|             response.body(this.gson.toJson(responseBody)); | ||||
|         } | ||||
|  | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -1,12 +1,9 @@ | ||||
| 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.xml.Xalan; | ||||
| 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; | ||||
| @@ -26,57 +23,63 @@ public class XsdController implements RestController { | ||||
|         this.xalan = xalan; | ||||
|     } | ||||
|  | ||||
|     @ScopedControllerManifest(method = HandlerType.POST, path = "/xsd") | ||||
|     public Response transform(Request request, Response response) { | ||||
|         String body = request.body(); | ||||
|     private XMLResponseBody prepareErrorResponse(String message, String processor) { | ||||
|         return new XMLResponseBody(message, "ERR", processor, -1); | ||||
|     } | ||||
|  | ||||
|         JsonObject requestJson; | ||||
|     private void nonValidEngineSelectedResponse(Response response) { | ||||
|         XMLResponseBody responseBody = | ||||
|                 prepareErrorResponse("Valid engines is: xalan", "N/A"); | ||||
|         response.body(this.gson.toJson(responseBody)); | ||||
|         response.status(400); | ||||
|     } | ||||
|     @ScopedControllerManifest(method = HandlerType.POST, path = "/xsd") | ||||
|     public void acceptRequest(Request request, Response response) { | ||||
|         XMLRequestBody requestBody; | ||||
|         try { | ||||
|             requestJson = this.gson.fromJson(body, JsonObject.class); | ||||
|             requestBody = this.gson.fromJson(request.body(), XMLRequestBody.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"); | ||||
|             XMLResponseBody responseBody = prepareErrorResponse(e.getMessage(), "N/A"); | ||||
|  | ||||
|             response.status(400); | ||||
|             response.body(this.gson.toJson(responseJson)); | ||||
|             return response; | ||||
|             response.body(this.gson.toJson(responseBody)); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         String data = requestJson.get("data").getAsString(); | ||||
|         String xsd = requestJson.get("process").getAsString(); | ||||
|         if (requestBody.getProcessor() == null) { | ||||
|             nonValidEngineSelectedResponse(response); | ||||
|             return; | ||||
|         } | ||||
|         if (requestBody.getProcessor().equalsIgnoreCase("xalan")) | ||||
|             process(response, requestBody, xalan); | ||||
|         else | ||||
|             nonValidEngineSelectedResponse(response); | ||||
|  | ||||
|         response.header("processor", xalan.getVersion()); | ||||
|     } | ||||
|  | ||||
|         long timeStart = System.currentTimeMillis(); | ||||
|         String tmp; | ||||
|  | ||||
|         JsonObject responseJson = new JsonObject(); | ||||
|     private void process(Response response, XMLRequestBody requestBody, XmlEngine engine) { | ||||
|         XMLResponseBody responseBody = null; | ||||
|         try { | ||||
|             tmp = xalan.validate(data, xsd).trim(); | ||||
|             long timeStart = System.currentTimeMillis(); | ||||
|             String result = engine.validate(requestBody.getData(), requestBody.getProcess()).trim(); | ||||
|  | ||||
|             response.status(200); | ||||
|  | ||||
|             responseJson.addProperty("result", tmp); | ||||
|             responseJson.addProperty("status", "OK"); | ||||
|         } catch (Exception ex) { | ||||
|             this.logger.error("Error on validation against XSD using Xalan. " + ex); | ||||
|             long duration = System.currentTimeMillis() - timeStart; | ||||
|             responseBody = new XMLResponseBody(result, "OK", engine.getVersion(), duration); | ||||
|  | ||||
|             this.logger.info("Request (XSD, " +  engine.getVersion() + ") processed in " + duration + " ms."); | ||||
|         } catch (Exception ex) { | ||||
|             responseBody = prepareErrorResponse(ex.getMessage(), engine.getVersion()); | ||||
|             response.status(400); | ||||
|  | ||||
|             responseJson.addProperty("result", ex.getMessage()); | ||||
|             responseJson.addProperty("status", "ERR"); | ||||
|             this.logger.error("Error on validation against XSD using " + engine.getVersion() + ". " + ex); | ||||
|         } | ||||
|         finally { | ||||
|             response.body(this.gson.toJson(responseBody)); | ||||
|         } | ||||
|  | ||||
|         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; | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -1,13 +1,9 @@ | ||||
| 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.xml.Saxon; | ||||
| import com.r11.tools.xml.Xalan; | ||||
| 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; | ||||
| @@ -29,106 +25,69 @@ public class XsltController implements RestController { | ||||
|         this.xalan = xalan; | ||||
|     } | ||||
|  | ||||
|     @ScopedControllerManifest(method = HandlerType.POST, path = "/xslt") | ||||
|     public void transform(Request request, Response response) { | ||||
|         String body = request.body(); | ||||
|     private XMLResponseBody prepareErrorResponse(String message, String processor) { | ||||
|         return new XMLResponseBody(message, "ERR", processor, -1); | ||||
|     } | ||||
|  | ||||
|         JsonObject requestJson; | ||||
|     private void nonValidEngineSelectedResponse(Response response) { | ||||
|         XMLResponseBody responseBody = | ||||
|                 prepareErrorResponse("Valid engines are: saxon, xalan", "N/A"); | ||||
|         response.body(this.gson.toJson(responseBody)); | ||||
|         response.status(400); | ||||
|     } | ||||
|  | ||||
|     @ScopedControllerManifest(method = HandlerType.POST, path = "/xslt") | ||||
|     public void acceptRequest(Request request, Response response) { | ||||
|         XMLRequestBody requestBody; | ||||
|         try { | ||||
|             requestJson = this.gson.fromJson(body, JsonObject.class); | ||||
|             requestBody = this.gson.fromJson(request.body(), XMLRequestBody.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"); | ||||
|             XMLResponseBody responseBody = prepareErrorResponse(e.getMessage(), "N/A"); | ||||
|  | ||||
|             response.status(400); | ||||
|             response.body(this.gson.toJson(responseJson)); | ||||
|             response.body(this.gson.toJson(responseBody)); | ||||
|             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) { | ||||
|             response.body("saxon, xalan"); | ||||
|         if (requestBody.getProcessor() == null) { | ||||
|             nonValidEngineSelectedResponse(response); | ||||
|             return; | ||||
|         } | ||||
|         JsonObject responseJson = new JsonObject(); | ||||
|         switch (processor) { | ||||
|  | ||||
|         switch (requestBody.getProcessor()) { | ||||
|             case "saxon": | ||||
|                 processWithSaxon(response, data, query, version, responseJson); | ||||
|                 process(response, requestBody, saxon); | ||||
|                 return; | ||||
|  | ||||
|             case "xalan": | ||||
|                 processWithXalan(response, data, query, responseJson); | ||||
|                 process(response, requestBody, xalan); | ||||
|                 return; | ||||
|  | ||||
|             default: | ||||
|                 response.body("saxon, xalan"); | ||||
|                 nonValidEngineSelectedResponse(response); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private void processWithXalan(Response response, String data, String query, JsonObject responseJson) { | ||||
|         long duration; | ||||
|         long timeStart; | ||||
|         String tmp; | ||||
|         timeStart = System.currentTimeMillis(); | ||||
|     private void process(Response response, XMLRequestBody requestBody, XmlEngine engine) { | ||||
|         XMLResponseBody responseBody = null; | ||||
|         long timeStart = System.currentTimeMillis(); | ||||
|         try { | ||||
|             tmp = xalan.processXSLT(data, query); | ||||
|  | ||||
|             String result = engine.processXSLT(requestBody.getData(), requestBody.getProcess()); | ||||
|             response.status(200); | ||||
|  | ||||
|             responseJson.addProperty("result", tmp); | ||||
|             responseJson.addProperty("status", "OK"); | ||||
|             long duration = System.currentTimeMillis() - timeStart; | ||||
|             responseBody = new XMLResponseBody(result, "OK", engine.getVersion(), duration); | ||||
|  | ||||
|             this.logger.info("Request (XSLT, " + engine.getVersion() + ") processed in " + duration + " ms."); | ||||
|         } catch (Exception ex) { | ||||
|             this.logger.error("Error on processing XSLT using Xalan. " + ex); | ||||
|  | ||||
|             responseBody = prepareErrorResponse(ex.getMessage(), engine.getVersion()); | ||||
|             response.status(400); | ||||
|             this.logger.error("Error on processing XSLT using " + engine.getVersion() + ". " + ex); | ||||
|  | ||||
|             responseJson.addProperty("result", ex.getMessage()); | ||||
|             responseJson.addProperty("status", "ERR"); | ||||
|         } finally { | ||||
|             response.body(this.gson.toJson(responseBody)); | ||||
|         } | ||||
|  | ||||
|         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(); | ||||
|         try { | ||||
|             tmp = saxon.processXSLT(data, query); | ||||
|  | ||||
|             response.status(200); | ||||
|  | ||||
|             responseJson.addProperty("result", tmp); | ||||
|             responseJson.addProperty("status", "OK"); | ||||
|         } catch (Exception ex) { | ||||
|             this.logger.error("Error on processing XSLT using Saxon. " + ex); | ||||
|  | ||||
|             response.status(400); | ||||
|  | ||||
|             responseJson.addProperty("result", ex.getMessage()); | ||||
|             responseJson.addProperty("status", "ERR"); | ||||
|         } | ||||
|  | ||||
|         duration = System.currentTimeMillis() - timeStart; | ||||
|         this.logger.info("Request (XSLT, Saxon) processed in " + duration + " ms."); | ||||
|  | ||||
|         responseJson.addProperty("processor", "Saxon " + saxon.getVersion() + " " + version); | ||||
|         responseJson.addProperty("time", duration); | ||||
|  | ||||
|         response.body(this.gson.toJson(responseJson)); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -0,0 +1,34 @@ | ||||
| package com.r11.tools.model; | ||||
|  | ||||
| import com.google.gson.annotations.SerializedName; | ||||
|  | ||||
| /** | ||||
|  * POJO class used to contain body of XML related requests | ||||
|  * @author Adam | ||||
|  */ | ||||
| public class XMLRequestBody { | ||||
|     @SerializedName("data") | ||||
|     private String data; | ||||
|     @SerializedName("process") | ||||
|     private String process; | ||||
|     @SerializedName("processor") | ||||
|     private String processor; | ||||
|     @SerializedName("version") | ||||
|     private String version; | ||||
|  | ||||
|     public String getData() { | ||||
|         return data; | ||||
|     } | ||||
|  | ||||
|     public String getProcess() { | ||||
|         return process; | ||||
|     } | ||||
|  | ||||
|     public String getProcessor() { | ||||
|         return processor; | ||||
|     } | ||||
|  | ||||
|     public String getVersion() { | ||||
|         return version; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,59 @@ | ||||
| package com.r11.tools.model; | ||||
|  | ||||
| public class XMLResponseBody { | ||||
|  | ||||
|     private String result; | ||||
|     private String status; | ||||
|     private String processor; | ||||
|     private long duration; | ||||
|  | ||||
|     // Optional | ||||
|     private String type; | ||||
|  | ||||
|     public XMLResponseBody(String result, String status, String processor, long duration) { | ||||
|         this.result = result; | ||||
|         this.status = status; | ||||
|         this.processor = processor; | ||||
|         this.duration = duration; | ||||
|     } | ||||
|  | ||||
|     public String getResult() { | ||||
|         return result; | ||||
|     } | ||||
|  | ||||
|     public void setResult(String result) { | ||||
|         this.result = result; | ||||
|     } | ||||
|  | ||||
|     public String getStatus() { | ||||
|         return status; | ||||
|     } | ||||
|  | ||||
|     public void setStatus(String status) { | ||||
|         this.status = status; | ||||
|     } | ||||
|  | ||||
|     public String getProcessor() { | ||||
|         return processor; | ||||
|     } | ||||
|  | ||||
|     public void setProcessor(String processor) { | ||||
|         this.processor = processor; | ||||
|     } | ||||
|  | ||||
|     public long getDuration() { | ||||
|         return duration; | ||||
|     } | ||||
|  | ||||
|     public void setDuration(long duration) { | ||||
|         this.duration = duration; | ||||
|     } | ||||
|  | ||||
|     public String getType() { | ||||
|         return type; | ||||
|     } | ||||
|  | ||||
|     public void setType(String type) { | ||||
|         this.type = type; | ||||
|     } | ||||
| } | ||||
| @@ -1,4 +1,4 @@ | ||||
| package com.r11.tools.controller.internal; | ||||
| package com.r11.tools.model; | ||||
| 
 | ||||
| /** | ||||
|  * Class used to store data received from parser and type of that data (node, string, etc.) | ||||
| @@ -1,6 +1,6 @@ | ||||
| package com.r11.tools.xml; | ||||
|  | ||||
| import com.r11.tools.controller.internal.XPathQueryResult; | ||||
| import com.r11.tools.model.XPathQueryResult; | ||||
| import net.sf.saxon.s9api.*; | ||||
|  | ||||
| import javax.xml.transform.stream.StreamSource; | ||||
| @@ -39,6 +39,29 @@ public class Saxon implements XmlEngine{ | ||||
|         throw new UnsupportedOperationException(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * This method evaluates XQuery exporession on given xml | ||||
|      * @param data xml | ||||
|      * @param xquery expression | ||||
|      * @return | ||||
|      * @throws Exception | ||||
|      */ | ||||
|     @Override | ||||
|     public String executeXQuery(String data, String xquery, String version) throws Exception { | ||||
|         Processor processor = new Processor(false); | ||||
|  | ||||
|         XQueryCompiler compiler = processor.newXQueryCompiler(); | ||||
|         compiler.setLanguageVersion(version); | ||||
|  | ||||
|         XQueryExecutable executable = compiler.compile(xquery); | ||||
|  | ||||
|         XQueryEvaluator evaluator = executable.load(); | ||||
|         evaluator.setSource(new StreamSource(new StringReader(data))); | ||||
|  | ||||
|         XdmValue result = evaluator.evaluate(); | ||||
|         return result.toString(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Process xpath and return either node or wrapped atomic value | ||||
|      * @param data xml to be querried | ||||
| @@ -76,6 +99,6 @@ public class Saxon implements XmlEngine{ | ||||
|      * @return version of the processor | ||||
|      */ | ||||
|     public String getVersion() { | ||||
|         return new Processor(false).getSaxonProductVersion(); | ||||
|         return "Saxon " + new Processor(false).getSaxonProductVersion(); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -1,8 +1,7 @@ | ||||
| package com.r11.tools.xml; | ||||
|  | ||||
| import com.r11.tools.controller.internal.XPathQueryResult; | ||||
| import com.r11.tools.model.XPathQueryResult; | ||||
| import org.apache.xpath.XPathAPI; | ||||
| import org.apache.xpath.objects.XObject; | ||||
| import org.w3c.dom.Document; | ||||
| import org.w3c.dom.Node; | ||||
| import org.w3c.dom.traversal.NodeIterator; | ||||
| @@ -133,4 +132,9 @@ public class Xalan implements XmlEngine{ | ||||
|         validator.validate(dataSource); | ||||
|         return "XML file is valid"; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public String executeXQuery(String data, String xquery, String version) throws Exception { | ||||
|         throw new UnsupportedOperationException("Xalan doesn't support XQuery evaluation"); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -1,12 +1,14 @@ | ||||
| package com.r11.tools.xml; | ||||
|  | ||||
| import com.r11.tools.controller.internal.XPathQueryResult; | ||||
| import com.r11.tools.model.XPathQueryResult; | ||||
|  | ||||
| public interface XmlEngine { | ||||
|     XPathQueryResult processXPath(String data, String query, String version) throws Exception; | ||||
|     String processXSLT(String data, String transform) throws Exception; | ||||
|     String validate(String data, String xsd) throws Exception; | ||||
|  | ||||
|     String executeXQuery(String data, String xquery, String version) throws Exception; | ||||
|  | ||||
|     public String getVersion(); | ||||
|  | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user