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