Finished implementing backend
This commit is contained in:
@@ -2,11 +2,7 @@ package com.r11.tools;
|
|||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import com.r11.tools.controller.JsonController;
|
import com.r11.tools.controller.*;
|
||||||
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.internal.RestControllerRegistry;
|
import com.r11.tools.controller.internal.RestControllerRegistry;
|
||||||
import com.r11.tools.xml.Saxon;
|
import com.r11.tools.xml.Saxon;
|
||||||
import com.r11.tools.xml.Xalan;
|
import com.r11.tools.xml.Xalan;
|
||||||
@@ -48,6 +44,7 @@ public class SparkApplication {
|
|||||||
registry.registerController(new XPathController(gson, logger, saxon, xalan));
|
registry.registerController(new XPathController(gson, logger, saxon, xalan));
|
||||||
registry.registerController(new XsltController(gson, logger, saxon, xalan));
|
registry.registerController(new XsltController(gson, logger, saxon, xalan));
|
||||||
registry.registerController(new JsonController(gson, jsongson, logger));
|
registry.registerController(new JsonController(gson, jsongson, logger));
|
||||||
|
registry.registerController(new XQueryController(gson, logger, saxon));
|
||||||
|
|
||||||
registry.register();
|
registry.register();
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,103 @@
|
|||||||
|
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.XmlEngine;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
import spark.Request;
|
||||||
|
import spark.Response;
|
||||||
|
|
||||||
|
@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 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 = "/xquery")
|
||||||
|
public void transform(Request request, Response response) {
|
||||||
|
|
||||||
|
JsonObject requestBody;
|
||||||
|
JsonObject responseJson;
|
||||||
|
try {
|
||||||
|
requestBody = this.gson.fromJson(request.body(), JsonObject.class);
|
||||||
|
} catch (Exception e) {
|
||||||
|
responseJson = prepareErrorResponse(e.getMessage(), "N/A");
|
||||||
|
|
||||||
|
response.status(400);
|
||||||
|
response.body(this.gson.toJson(responseJson));
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
response.body("saxon");
|
||||||
|
} catch (Exception e) {
|
||||||
|
responseJson = prepareErrorResponse(e.getMessage(), "Saxon");
|
||||||
|
|
||||||
|
response.status(400);
|
||||||
|
response.body(this.gson.toJson(responseJson));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processWithSaxon(Response response, String data, String query, String version) {
|
||||||
|
JsonObject responseJson = new JsonObject();
|
||||||
|
long duration;
|
||||||
|
String result;
|
||||||
|
long timeStart;
|
||||||
|
timeStart = System.currentTimeMillis();
|
||||||
|
try {
|
||||||
|
result = saxon.executeXQuery(data, query, version);
|
||||||
|
|
||||||
|
response.status(200);
|
||||||
|
|
||||||
|
responseJson.addProperty("result", result);
|
||||||
|
responseJson.addProperty("status", "OK");
|
||||||
|
} 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");
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -6,8 +6,6 @@ import com.r11.tools.controller.internal.GlobalControllerManifest;
|
|||||||
import com.r11.tools.controller.internal.HandlerType;
|
import com.r11.tools.controller.internal.HandlerType;
|
||||||
import com.r11.tools.controller.internal.RestController;
|
import com.r11.tools.controller.internal.RestController;
|
||||||
import com.r11.tools.controller.internal.ScopedControllerManifest;
|
import com.r11.tools.controller.internal.ScopedControllerManifest;
|
||||||
import com.r11.tools.xml.Saxon;
|
|
||||||
import com.r11.tools.xml.Xalan;
|
|
||||||
import com.r11.tools.xml.XmlEngine;
|
import com.r11.tools.xml.XmlEngine;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import spark.Request;
|
import spark.Request;
|
||||||
@@ -126,7 +124,7 @@ public class XsltController implements RestController {
|
|||||||
duration = System.currentTimeMillis() - timeStart;
|
duration = System.currentTimeMillis() - timeStart;
|
||||||
this.logger.info("Request (XSLT, Saxon) processed in " + duration + " ms.");
|
this.logger.info("Request (XSLT, Saxon) processed in " + duration + " ms.");
|
||||||
|
|
||||||
responseJson.addProperty("processor", "Saxon " + saxon.getVersion() + " " + version);
|
responseJson.addProperty("processor", "Saxon " + saxon.getVersion());
|
||||||
responseJson.addProperty("time", duration);
|
responseJson.addProperty("time", duration);
|
||||||
|
|
||||||
response.body(this.gson.toJson(responseJson));
|
response.body(this.gson.toJson(responseJson));
|
||||||
|
|||||||
@@ -47,10 +47,11 @@ public class Saxon implements XmlEngine{
|
|||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String executeXQuery(String data, String xquery) throws Exception {
|
public String executeXQuery(String data, String xquery, String version) throws Exception {
|
||||||
Processor processor = new Processor(false);
|
Processor processor = new Processor(false);
|
||||||
|
|
||||||
XQueryCompiler compiler = processor.newXQueryCompiler();
|
XQueryCompiler compiler = processor.newXQueryCompiler();
|
||||||
|
compiler.setLanguageVersion(version);
|
||||||
|
|
||||||
XQueryExecutable executable = compiler.compile(xquery);
|
XQueryExecutable executable = compiler.compile(xquery);
|
||||||
|
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ public class Xalan implements XmlEngine{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String executeXQuery(String data, String xquery) throws Exception {
|
public String executeXQuery(String data, String xquery, String version) throws Exception {
|
||||||
throw new UnsupportedOperationException("Xalan doesn't support XQuery evaluation");
|
throw new UnsupportedOperationException("Xalan doesn't support XQuery evaluation");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ public interface XmlEngine {
|
|||||||
String processXSLT(String data, String transform) throws Exception;
|
String processXSLT(String data, String transform) throws Exception;
|
||||||
String validate(String data, String xsd) throws Exception;
|
String validate(String data, String xsd) throws Exception;
|
||||||
|
|
||||||
String executeXQuery(String data, String xquery) throws Exception;
|
String executeXQuery(String data, String xquery, String version) throws Exception;
|
||||||
|
|
||||||
public String getVersion();
|
public String getVersion();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user