Added XQuery Tool and refactored tools-service #220
@@ -2,10 +2,7 @@ package com.r11.tools.controller;
|
|||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.r11.tools.controller.internal.GlobalControllerManifest;
|
import com.r11.tools.controller.internal.*;
|
||||||
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 com.r11.tools.xml.XmlEngine;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import spark.Request;
|
import spark.Request;
|
||||||
@@ -40,10 +37,10 @@ public class XQueryController implements RestController {
|
|||||||
@ScopedControllerManifest(method = HandlerType.POST, path = "/xquery")
|
@ScopedControllerManifest(method = HandlerType.POST, path = "/xquery")
|
||||||
public void transform(Request request, Response response) {
|
public void transform(Request request, Response response) {
|
||||||
|
|
||||||
JsonObject requestBody;
|
XMLRequestBody requestBody;
|
||||||
JsonObject responseJson;
|
JsonObject responseJson;
|
||||||
try {
|
try {
|
||||||
requestBody = this.gson.fromJson(request.body(), JsonObject.class);
|
requestBody = this.gson.fromJson(request.body(), XMLRequestBody.class);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
responseJson = prepareErrorResponse(e.getMessage(), "N/A");
|
responseJson = prepareErrorResponse(e.getMessage(), "N/A");
|
||||||
|
|
||||||
@@ -52,14 +49,9 @@ public class XQueryController implements RestController {
|
|||||||
return;
|
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 {
|
try {
|
||||||
if (processor.equals("saxon")) {
|
if (requestBody.getProcessor().equals("saxon")) {
|
||||||
processWithSaxon(response, data, query, version);
|
processWithSaxon(response, requestBody);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
response.body("saxon");
|
response.body("saxon");
|
||||||
@@ -73,35 +65,32 @@ public class XQueryController implements RestController {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processWithSaxon(Response response, String data, String query, String version) {
|
private void processWithSaxon(Response response, XMLRequestBody requestBody) {
|
||||||
JsonObject responseJson = new JsonObject();
|
JsonObject responseJson = new JsonObject();
|
||||||
long duration;
|
long timeStart = System.currentTimeMillis();
|
||||||
String result;
|
|
||||||
long timeStart;
|
|
||||||
timeStart = System.currentTimeMillis();
|
|
||||||
try {
|
try {
|
||||||
result = saxon.executeXQuery(data, query, version);
|
String result = saxon.executeXQuery(requestBody.getData(), requestBody.getProcess(), requestBody.getVersion());
|
||||||
|
|
||||||
response.status(200);
|
response.status(200);
|
||||||
|
|
||||||
responseJson.addProperty("result", result);
|
responseJson.addProperty("result", result);
|
||||||
responseJson.addProperty("status", "OK");
|
responseJson.addProperty("status", "OK");
|
||||||
|
responseJson.addProperty("processor", "Saxon " + saxon.getVersion());
|
||||||
|
|
||||||
|
long duration = System.currentTimeMillis() - timeStart;
|
||||||
|
responseJson.addProperty("time", duration);
|
||||||
|
|
||||||
|
this.logger.info("Request (XQuery, Saxon) processed in " + duration + " ms.");
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
this.logger.error("Error on processing XQuery using Saxon. " + ex);
|
|
||||||
|
|
||||||
response.status(400);
|
response.status(400);
|
||||||
|
responseJson = prepareErrorResponse(ex.getMessage(), saxon.getVersion());
|
||||||
|
|
||||||
responseJson.addProperty("result", ex.getMessage());
|
this.logger.error("Error on processing XQuery using Saxon. " + ex);
|
||||||
responseJson.addProperty("status", "ERR");
|
}
|
||||||
|
finally {
|
||||||
|
response.body(this.gson.toJson(responseJson));
|
||||||
}
|
}
|
||||||
|
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,7 @@ package com.r11.tools.controller;
|
|||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.r11.tools.controller.internal.GlobalControllerManifest;
|
import com.r11.tools.controller.internal.*;
|
||||||
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.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;
|
||||||
@@ -38,9 +35,9 @@ public class XsdController implements RestController {
|
|||||||
public Response transform(Request request, Response response) {
|
public Response transform(Request request, Response response) {
|
||||||
String body = request.body();
|
String body = request.body();
|
||||||
|
|
||||||
JsonObject requestJson;
|
XMLRequestBody requestBody;
|
||||||
try {
|
try {
|
||||||
requestJson = this.gson.fromJson(body, JsonObject.class);
|
requestBody = this.gson.fromJson(body, XMLRequestBody.class);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
JsonObject responseJson = prepareErrorResponse(e.getMessage(), "N/A");
|
JsonObject responseJson = prepareErrorResponse(e.getMessage(), "N/A");
|
||||||
|
|
||||||
@@ -49,13 +46,10 @@ public class XsdController implements RestController {
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
String data = requestJson.get("data").getAsString();
|
|
||||||
String xsd = requestJson.get("process").getAsString();
|
|
||||||
|
|
||||||
JsonObject responseJson = new JsonObject();
|
JsonObject responseJson = new JsonObject();
|
||||||
try {
|
try {
|
||||||
long timeStart = System.currentTimeMillis();
|
long timeStart = System.currentTimeMillis();
|
||||||
String result = xalan.validate(data, xsd).trim();
|
String result = xalan.validate(requestBody.getData(), requestBody.getProcess()).trim();
|
||||||
|
|
||||||
response.status(200);
|
response.status(200);
|
||||||
|
|
||||||
|
|||||||
@@ -2,10 +2,7 @@ package com.r11.tools.controller;
|
|||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.r11.tools.controller.internal.GlobalControllerManifest;
|
import com.r11.tools.controller.internal.*;
|
||||||
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 com.r11.tools.xml.XmlEngine;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import spark.Request;
|
import spark.Request;
|
||||||
@@ -39,9 +36,9 @@ public class XsltController implements RestController {
|
|||||||
public void transform(Request request, Response response) {
|
public void transform(Request request, Response response) {
|
||||||
String body = request.body();
|
String body = request.body();
|
||||||
|
|
||||||
JsonObject requestJson;
|
XMLRequestBody requestBody;
|
||||||
try {
|
try {
|
||||||
requestJson = this.gson.fromJson(body, JsonObject.class);
|
requestBody = this.gson.fromJson(body, XMLRequestBody.class);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
JsonObject responseJson = prepareErrorResponse(e.getMessage(), "N/A");
|
JsonObject responseJson = prepareErrorResponse(e.getMessage(), "N/A");
|
||||||
|
|
||||||
@@ -50,23 +47,18 @@ public class XsltController implements RestController {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String data = requestJson.get("data").getAsString();
|
if (requestBody.getProcessor() == null) {
|
||||||
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");
|
response.body("saxon, xalan");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
JsonObject responseJson = new JsonObject();
|
JsonObject responseJson = new JsonObject();
|
||||||
switch (processor) {
|
switch (requestBody.getProcessor()) {
|
||||||
case "saxon":
|
case "saxon":
|
||||||
processWithSaxon(response, data, query, version, responseJson);
|
processWithSaxon(response, requestBody, responseJson);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case "xalan":
|
case "xalan":
|
||||||
processWithXalan(response, data, query, responseJson);
|
processWithXalan(response, requestBody, responseJson);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@@ -74,10 +66,10 @@ public class XsltController implements RestController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processWithXalan(Response response, String data, String query, JsonObject responseJson) {
|
private void processWithXalan(Response response, XMLRequestBody requestBody, JsonObject responseJson) {
|
||||||
long timeStart = System.currentTimeMillis();
|
long timeStart = System.currentTimeMillis();
|
||||||
try {
|
try {
|
||||||
String result = xalan.processXSLT(data, query);
|
String result = xalan.processXSLT(requestBody.getData(), requestBody.getProcess());
|
||||||
|
|
||||||
response.status(200);
|
response.status(200);
|
||||||
|
|
||||||
@@ -100,10 +92,10 @@ public class XsltController implements RestController {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processWithSaxon(Response response, String data, String query, String version, JsonObject responseJson) {
|
private void processWithSaxon(Response response, XMLRequestBody requestBody, JsonObject responseJson) {
|
||||||
long timeStart = System.currentTimeMillis();
|
long timeStart = System.currentTimeMillis();
|
||||||
try {
|
try {
|
||||||
String result = saxon.processXSLT(data, query);
|
String result = saxon.processXSLT(requestBody.getData(), requestBody.getProcess());
|
||||||
|
|
||||||
response.status(200);
|
response.status(200);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user