Removed duplicate code and adjusted captions (#249)
This pull request completes issues: #235 and #247 Reviewed-on: #249 Reviewed-by: Mikolaj Widla <widlam@noreply.example.com> Co-authored-by: Adam Bem <adam.bem@zoho.eu> Co-committed-by: Adam Bem <adam.bem@zoho.eu>
This commit is contained in:
		| @@ -40,11 +40,9 @@ public class SparkApplication { | ||||
|  | ||||
|         RestControllerRegistry registry = new RestControllerRegistry(); | ||||
|         registry.registerController(new ProcessorInfoController(logger, saxon, xalan)); | ||||
|         registry.registerController(new XsdController(gson, logger, xalan)); | ||||
|         registry.registerController(new XPathController(gson, logger, saxon, xalan)); | ||||
|         registry.registerController(new XsltController(gson, logger, saxon, xalan)); | ||||
|  | ||||
|         registry.registerController(new XmlController(gson, logger, saxon, xalan)); | ||||
|         registry.registerController(new JsonController(gson, jsongson, logger)); | ||||
|         registry.registerController(new XQueryController(gson, logger, saxon)); | ||||
|  | ||||
|         registry.register(); | ||||
|  | ||||
|   | ||||
| @@ -1,94 +0,0 @@ | ||||
| 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.model.XPathQueryResult; | ||||
| import com.r11.tools.xml.XmlEngine; | ||||
| import org.apache.logging.log4j.Logger; | ||||
| import spark.Request; | ||||
| import spark.Response; | ||||
|  | ||||
| @GlobalControllerManifest | ||||
| public class XPathController implements RestController { | ||||
|  | ||||
|     private final Gson gson; | ||||
|     private final Logger logger; | ||||
|  | ||||
|     private final XmlEngine saxon; | ||||
|     private final XmlEngine xalan; | ||||
|  | ||||
|     public XPathController(Gson gson, Logger logger, XmlEngine saxon, XmlEngine xalan) { | ||||
|         this.gson = gson; | ||||
|         this.logger = logger; | ||||
|         this.saxon = saxon; | ||||
|         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 acceptRequest(Request request, Response response) { | ||||
|         XMLRequestBody requestBody; | ||||
|         try { | ||||
|             requestBody = this.gson.fromJson(request.body(), XMLRequestBody.class); | ||||
|         } catch (Exception e) { | ||||
|             XMLResponseBody responseBody = errorResponse(e.getMessage(), "N/A"); | ||||
|             response.status(400); | ||||
|             response.body(this.gson.toJson(responseBody)); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         if (requestBody.getProcessor() == null) { | ||||
|             nonValidEngineSelectedResponse(response); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         switch (requestBody.getProcessor()) { | ||||
|             case "saxon": | ||||
|                 process(response, requestBody, saxon); | ||||
|                 break; | ||||
|             case "xalan": | ||||
|                 process(response, requestBody, xalan); | ||||
|                 break; | ||||
|             default: | ||||
|                 nonValidEngineSelectedResponse(response); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private void process(Response response, XMLRequestBody requestBody, XmlEngine engine) { | ||||
|         long timeStart = System.currentTimeMillis(); | ||||
|         XMLResponseBody responseBody = null; | ||||
|         try { | ||||
|             XPathQueryResult xPathQueryResult = | ||||
|                     engine.processXPath(requestBody.getData(), requestBody.getProcess(), requestBody.getVersion()); | ||||
|  | ||||
|             response.status(200); | ||||
|             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) { | ||||
|             responseBody = errorResponse(ex.getMessage(), engine.getVersion()); | ||||
|             response.status(400); | ||||
|  | ||||
|             this.logger.error("Error on processing XPath using " + engine.getVersion() + ". " + ex); | ||||
|         } finally { | ||||
|             response.body(this.gson.toJson(responseBody)); | ||||
|         } | ||||
|  | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -1,87 +0,0 @@ | ||||
| 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)); | ||||
|         } | ||||
|  | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,173 @@ | ||||
| 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.model.XPathQueryResult; | ||||
| import com.r11.tools.xml.XmlEngine; | ||||
| import org.apache.logging.log4j.Logger; | ||||
| import spark.Request; | ||||
| import spark.Response; | ||||
|  | ||||
| /** | ||||
|  * Controller used to handle XML tools: XPath, XSD validation, XQuery and XSLT | ||||
|  * @author Adam Bem | ||||
|  */ | ||||
| @GlobalControllerManifest | ||||
| public class XmlController implements RestController { | ||||
|  | ||||
|     private final Gson gson; | ||||
|     private final Logger logger; | ||||
|  | ||||
|     private final XmlEngine saxon; | ||||
|     private final XmlEngine xalan; | ||||
|  | ||||
|     public XmlController(Gson gson, Logger logger, XmlEngine saxon, XmlEngine xalan) { | ||||
|         this.gson = gson; | ||||
|         this.logger = logger; | ||||
|         this.saxon = saxon; | ||||
|         this.xalan = xalan; | ||||
|     } | ||||
|  | ||||
|     @ScopedControllerManifest(method = HandlerType.POST, path = "/xpath") | ||||
|     public void acceptRequestXPath(Request request, Response response) { | ||||
|         acceptRequest(request, response, XmlJobType.XPath); | ||||
|     } | ||||
|  | ||||
|     @ScopedControllerManifest(method = HandlerType.POST, path = "/xquery") | ||||
|     public void acceptRequestXQuery(Request request, Response response) { | ||||
|         acceptRequest(request, response, XmlJobType.XQuery); | ||||
|     } | ||||
|  | ||||
|     @ScopedControllerManifest(method = HandlerType.POST, path = "/xsd") | ||||
|     public void acceptRequestXsd(Request request, Response response) { | ||||
|         acceptRequest(request, response, XmlJobType.XSD); | ||||
|  | ||||
|     } | ||||
|  | ||||
|     @ScopedControllerManifest(method = HandlerType.POST, path = "/xslt") | ||||
|     public void acceptRequestXslt(Request request, Response response) { | ||||
|         acceptRequest(request, response, XmlJobType.XSLT); | ||||
|     } | ||||
|  | ||||
|     private void acceptRequest(Request request, Response response, XmlJobType xmlJobType) { | ||||
|         XMLRequestBody requestBody; | ||||
|         try { | ||||
|             requestBody = this.gson.fromJson(request.body(), XMLRequestBody.class); | ||||
|         } catch (Exception e) { | ||||
|             requestErrorResponse(response, e); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         if (requestBody.getProcessor() == null) { | ||||
|             invalidEngineSelectedResponse(response); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         switch (requestBody.getProcessor().toLowerCase()) { | ||||
|             case "saxon": | ||||
|                 processRequest(new XmlJob(response, requestBody, saxon, xmlJobType)); | ||||
|                 return; | ||||
|  | ||||
|             case "xalan": | ||||
|                 processRequest(new XmlJob(response, requestBody, xalan, xmlJobType)); | ||||
|                 return; | ||||
|  | ||||
|             default: | ||||
|                 invalidEngineSelectedResponse(response); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private void processRequest(XmlJob xmlJob) { | ||||
|         XMLResponseBody responseBody = null; | ||||
|         long timeStart = System.currentTimeMillis(); | ||||
|         long duration; | ||||
|  | ||||
|         try { | ||||
|             responseBody = processData(xmlJob); | ||||
|  | ||||
|             duration = System.currentTimeMillis() - timeStart; | ||||
|             responseBody.setDuration(duration); | ||||
|  | ||||
|             xmlJob.getResponse().status(200); | ||||
|  | ||||
|             this.logger.info("Request (" + xmlJob.getXmlJobType() + ", " + | ||||
|                     xmlJob.getEngine().getVersion() + | ||||
|                     ") processed in " + duration + " ms."); | ||||
|  | ||||
|         } catch (Exception ex) { | ||||
|             responseBody = processingErrorResponse(ex, xmlJob); | ||||
|  | ||||
|         } finally { | ||||
|             xmlJob.getResponse().body(this.gson.toJson(responseBody)); | ||||
|         } | ||||
|  | ||||
|     } | ||||
|  | ||||
|     private XMLResponseBody processData(XmlJob xmlJob) throws Exception { | ||||
|         if (xmlJob.getXmlJobType() == XmlJobType.XPath) | ||||
|             return processXPath(xmlJob); | ||||
|         else | ||||
|             return processOther(xmlJob); | ||||
|     } | ||||
|  | ||||
|     private XMLResponseBody processXPath(XmlJob xmlJob) throws Exception { | ||||
|         XmlEngine engine = xmlJob.getEngine(); | ||||
|         XMLRequestBody requestBody = xmlJob.getRequestBody(); | ||||
|  | ||||
|         XPathQueryResult xPathQueryResult = | ||||
|                 engine.processXPath(requestBody.getData(), requestBody.getProcess(), requestBody.getVersion()); | ||||
|  | ||||
|         return new XMLResponseBody(xPathQueryResult.getData().trim(), | ||||
|                 "OK", engine.getVersion(), xPathQueryResult.getType()); | ||||
|     } | ||||
|  | ||||
|     private XMLResponseBody processOther(XmlJob xmlJob) throws Exception { | ||||
|         XmlEngine engine = xmlJob.getEngine(); | ||||
|         XMLRequestBody requestBody = xmlJob.getRequestBody(); | ||||
|  | ||||
|         String result = null; | ||||
|         switch (xmlJob.getXmlJobType()) { | ||||
|             case XSLT: | ||||
|                 result = engine.processXSLT(requestBody.getData(), requestBody.getProcess()); | ||||
|                 break; | ||||
|             case XSD: | ||||
|                 result = engine.validate(requestBody.getData(), requestBody.getProcess()).trim(); | ||||
|                 break; | ||||
|             case XQuery: | ||||
|                 result = engine.executeXQuery(requestBody.getData(), | ||||
|                         requestBody.getProcess(), | ||||
|                         requestBody.getVersion()); | ||||
|                 break; | ||||
|         } | ||||
|         return new XMLResponseBody(result, "OK", requestBody.getVersion()); | ||||
|     } | ||||
|  | ||||
|     private XMLResponseBody processingErrorResponse(Exception ex, XmlJob xmlJob) { | ||||
|         XmlEngine engine = xmlJob.getEngine(); | ||||
|         XmlJobType xmlJobType = xmlJob.getXmlJobType(); | ||||
|         Response response = xmlJob.getResponse(); | ||||
|  | ||||
|         XMLResponseBody responseBody = | ||||
|                 new XMLResponseBody(ex.getMessage(), "ERR", engine.getVersion(), -1); | ||||
|  | ||||
|         response.status(400); | ||||
|         this.logger.error("Error on processing " + xmlJobType + " using " + engine.getVersion() + ". " + ex); | ||||
|  | ||||
|         return responseBody; | ||||
|     } | ||||
|  | ||||
|     private void invalidEngineSelectedResponse(Response response) { | ||||
|         XMLResponseBody responseBody = | ||||
|                 new XMLResponseBody("Valid engines are: saxon, xalan", "ERR", "N/A", -1); | ||||
|         response.body(this.gson.toJson(responseBody)); | ||||
|         response.status(400); | ||||
|     } | ||||
|  | ||||
|     private void requestErrorResponse(Response response, Exception ex) { | ||||
|         XMLResponseBody responseBody = new XMLResponseBody(ex.getMessage(), "ERR", "N/A", -1); | ||||
|         response.status(400); | ||||
|         response.body(this.gson.toJson(responseBody)); | ||||
|     } | ||||
| } | ||||
| @@ -1,85 +0,0 @@ | ||||
| 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; | ||||
|  | ||||
| @GlobalControllerManifest | ||||
| public class XsdController implements RestController { | ||||
|  | ||||
|     private final Gson gson; | ||||
|     private final Logger logger; | ||||
|  | ||||
|     private final XmlEngine xalan; | ||||
|  | ||||
|     public XsdController(Gson gson, Logger logger, XmlEngine xalan) { | ||||
|         this.gson = gson; | ||||
|         this.logger = logger; | ||||
|         this.xalan = xalan; | ||||
|     } | ||||
|  | ||||
|     private XMLResponseBody prepareErrorResponse(String message, String processor) { | ||||
|         return new XMLResponseBody(message, "ERR", processor, -1); | ||||
|     } | ||||
|  | ||||
|     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 { | ||||
|             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("xalan")) | ||||
|             process(response, requestBody, xalan); | ||||
|         else | ||||
|             nonValidEngineSelectedResponse(response); | ||||
|  | ||||
|     } | ||||
|  | ||||
|     private void process(Response response, XMLRequestBody requestBody, XmlEngine engine) { | ||||
|         XMLResponseBody responseBody = null; | ||||
|         try { | ||||
|             long timeStart = System.currentTimeMillis(); | ||||
|             String result = engine.validate(requestBody.getData(), requestBody.getProcess()).trim(); | ||||
|  | ||||
|             response.status(200); | ||||
|  | ||||
|             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); | ||||
|  | ||||
|             this.logger.error("Error on validation against XSD using " + engine.getVersion() + ". " + ex); | ||||
|         } | ||||
|         finally { | ||||
|             response.body(this.gson.toJson(responseBody)); | ||||
|         } | ||||
|  | ||||
|  | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -1,93 +0,0 @@ | ||||
| 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; | ||||
|  | ||||
| @GlobalControllerManifest | ||||
| public class XsltController implements RestController { | ||||
|  | ||||
|     private final Gson gson; | ||||
|     private final Logger logger; | ||||
|  | ||||
|     private final XmlEngine saxon; | ||||
|     private final XmlEngine xalan; | ||||
|  | ||||
|     public XsltController(Gson gson, Logger logger, XmlEngine saxon, XmlEngine xalan) { | ||||
|         this.gson = gson; | ||||
|         this.logger = logger; | ||||
|         this.saxon = saxon; | ||||
|         this.xalan = xalan; | ||||
|     } | ||||
|  | ||||
|     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, 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 { | ||||
|             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; | ||||
|         } | ||||
|  | ||||
|         switch (requestBody.getProcessor()) { | ||||
|             case "saxon": | ||||
|                 process(response, requestBody, saxon); | ||||
|                 return; | ||||
|  | ||||
|             case "xalan": | ||||
|                 process(response, requestBody, xalan); | ||||
|                 return; | ||||
|  | ||||
|             default: | ||||
|                 nonValidEngineSelectedResponse(response); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private void process(Response response, XMLRequestBody requestBody, XmlEngine engine) { | ||||
|         XMLResponseBody responseBody = null; | ||||
|         long timeStart = System.currentTimeMillis(); | ||||
|         try { | ||||
|             String result = engine.processXSLT(requestBody.getData(), requestBody.getProcess()); | ||||
|             response.status(200); | ||||
|  | ||||
|             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) { | ||||
|             responseBody = prepareErrorResponse(ex.getMessage(), engine.getVersion()); | ||||
|             response.status(400); | ||||
|             this.logger.error("Error on processing XSLT using " + engine.getVersion() + ". " + ex); | ||||
|  | ||||
|         } finally { | ||||
|             response.body(this.gson.toJson(responseBody)); | ||||
|         } | ||||
|  | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,35 @@ | ||||
| package com.r11.tools.controller.internal; | ||||
|  | ||||
| import com.r11.tools.model.XMLRequestBody; | ||||
| import com.r11.tools.xml.XmlEngine; | ||||
| import spark.Response; | ||||
|  | ||||
| public class XmlJob { | ||||
|     private final Response response; | ||||
|     private final XMLRequestBody requestBody; | ||||
|     private final XmlEngine engine; | ||||
|     private final XmlJobType xmlJobType; | ||||
|  | ||||
|     public XmlJob(Response response, XMLRequestBody requestBody, XmlEngine engine, XmlJobType xmlJobType) { | ||||
|         this.response = response; | ||||
|         this.requestBody = requestBody; | ||||
|         this.engine = engine; | ||||
|         this.xmlJobType = xmlJobType; | ||||
|     } | ||||
|  | ||||
|     public Response getResponse() { | ||||
|         return response; | ||||
|     } | ||||
|  | ||||
|     public XMLRequestBody getRequestBody() { | ||||
|         return requestBody; | ||||
|     } | ||||
|  | ||||
|     public XmlEngine getEngine() { | ||||
|         return engine; | ||||
|     } | ||||
|  | ||||
|     public XmlJobType getXmlJobType() { | ||||
|         return xmlJobType; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,8 @@ | ||||
| package com.r11.tools.controller.internal; | ||||
|  | ||||
| public enum XmlJobType { | ||||
|     XPath("XPath"), XSD("XSD"), XQuery("XQuery"), XSLT("XSLT"); | ||||
|  | ||||
|     XmlJobType(String type) { | ||||
|     } | ||||
| } | ||||
| @@ -10,6 +10,11 @@ public class XMLResponseBody { | ||||
|     // Optional | ||||
|     private String type; | ||||
|  | ||||
|     public XMLResponseBody(String result, String status, String processor) { | ||||
|         this.result = result; | ||||
|         this.status = status; | ||||
|         this.processor = processor; | ||||
|     } | ||||
|     public XMLResponseBody(String result, String status, String processor, long duration) { | ||||
|         this.result = result; | ||||
|         this.status = status; | ||||
| @@ -17,6 +22,13 @@ public class XMLResponseBody { | ||||
|         this.duration = duration; | ||||
|     } | ||||
|  | ||||
|     public XMLResponseBody(String result, String status, String processor, String type) { | ||||
|         this.result = result; | ||||
|         this.status = status; | ||||
|         this.processor = processor; | ||||
|         this.type = type; | ||||
|     } | ||||
|  | ||||
|     public String getResult() { | ||||
|         return result; | ||||
|     } | ||||
|   | ||||
| @@ -40,7 +40,7 @@ public class Saxon implements XmlEngine{ | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * This method evaluates XQuery exporession on given xml | ||||
|      * This method evaluates XQuery expression on given xml | ||||
|      * @param data xml | ||||
|      * @param xquery expression | ||||
|      * @return | ||||
| @@ -64,8 +64,8 @@ public class Saxon implements XmlEngine{ | ||||
|  | ||||
|     /** | ||||
|      * Process xpath and return either node or wrapped atomic value | ||||
|      * @param data xml to be querried | ||||
|      * @param query xpath queryy | ||||
|      * @param data xml to be processed | ||||
|      * @param query xpath query | ||||
|      * @param version processor version | ||||
|      * @return string xml representation of the node | ||||
|      * @throws Exception thrown on node building errors or invalid xpath | ||||
|   | ||||
		Reference in New Issue
	
	Block a user