Removed duplicate code and adjusted captions #249
@@ -38,14 +38,14 @@ public class XmlController implements RestController {
 | 
			
		||||
        try {
 | 
			
		||||
            requestBody = this.gson.fromJson(request.body(), XMLRequestBody.class);
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            XMLResponseBody responseBody = prepareErrorResponse(e.getMessage(), "N/A");
 | 
			
		||||
            XMLResponseBody responseBody = prepareErrorResponseBody(e.getMessage(), "N/A");
 | 
			
		||||
            response.status(400);
 | 
			
		||||
            response.body(this.gson.toJson(responseBody));
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (requestBody.getProcessor() == null) {
 | 
			
		||||
            nonValidEngineSelectedResponse(response);
 | 
			
		||||
            invalidEngineSelectedResponse(response);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -57,73 +57,31 @@ public class XmlController implements RestController {
 | 
			
		||||
                process(response, requestBody, xalan, XmlJobType.XPath);
 | 
			
		||||
                break;
 | 
			
		||||
            default:
 | 
			
		||||
                nonValidEngineSelectedResponse(response);
 | 
			
		||||
                invalidEngineSelectedResponse(response);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
//    private void processXPath(Response response, XMLRequestBody requestBody, XmlEngine engine) {
 | 
			
		||||
//        long timeStart = System.currentTimeMillis();
 | 
			
		||||
//        XMLResponseBody responseBody = null;
 | 
			
		||||
//        try {
 | 
			
		||||
//
 | 
			
		||||
//
 | 
			
		||||
//
 | 
			
		||||
//            this.logger.info("Request (XPath, " + engine.getVersion() + ") processed in " + duration + " ms.");
 | 
			
		||||
//        } catch (Exception ex) {
 | 
			
		||||
//            responseBody = prepareErrorResponse(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));
 | 
			
		||||
//        }
 | 
			
		||||
//
 | 
			
		||||
//    }
 | 
			
		||||
 | 
			
		||||
    @ScopedControllerManifest(method = HandlerType.POST, path = "/xquery")
 | 
			
		||||
    public void acceptRequestXQuery(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");
 | 
			
		||||
            XMLResponseBody responseBody = prepareErrorResponseBody(e.getMessage(), "N/A");
 | 
			
		||||
 | 
			
		||||
            response.status(400);
 | 
			
		||||
            response.body(this.gson.toJson(responseBody));
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        if (requestBody.getProcessor() == null) {
 | 
			
		||||
            nonValidEngineSelectedResponse(response);
 | 
			
		||||
            invalidEngineSelectedResponse(response);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (requestBody.getProcessor().equalsIgnoreCase("saxon"))
 | 
			
		||||
            processXQuery(response, requestBody, saxon);
 | 
			
		||||
            process(response, requestBody, saxon, XmlJobType.XQuery);
 | 
			
		||||
        else
 | 
			
		||||
            nonValidEngineSelectedResponse(response);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void processXQuery(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));
 | 
			
		||||
        }
 | 
			
		||||
            invalidEngineSelectedResponse(response);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @ScopedControllerManifest(method = HandlerType.POST, path = "/xsd")
 | 
			
		||||
@@ -132,7 +90,7 @@ public class XmlController implements RestController {
 | 
			
		||||
        try {
 | 
			
		||||
            requestBody = this.gson.fromJson(request.body(), XMLRequestBody.class);
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            XMLResponseBody responseBody = prepareErrorResponse(e.getMessage(), "N/A");
 | 
			
		||||
            XMLResponseBody responseBody = prepareErrorResponseBody(e.getMessage(), "N/A");
 | 
			
		||||
 | 
			
		||||
            response.status(400);
 | 
			
		||||
            response.body(this.gson.toJson(responseBody));
 | 
			
		||||
@@ -140,38 +98,13 @@ public class XmlController implements RestController {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (requestBody.getProcessor() == null) {
 | 
			
		||||
            nonValidEngineSelectedResponse(response);
 | 
			
		||||
            invalidEngineSelectedResponse(response);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        if (requestBody.getProcessor().equalsIgnoreCase("xalan"))
 | 
			
		||||
            processXsd(response, requestBody, xalan);
 | 
			
		||||
            process(response, requestBody, xalan, XmlJobType.XSD);
 | 
			
		||||
        else
 | 
			
		||||
            nonValidEngineSelectedResponse(response);
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void processXsd(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));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
            invalidEngineSelectedResponse(response);
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -181,7 +114,7 @@ public class XmlController implements RestController {
 | 
			
		||||
        try {
 | 
			
		||||
            requestBody = this.gson.fromJson(request.body(), XMLRequestBody.class);
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            XMLResponseBody responseBody = prepareErrorResponse(e.getMessage(), "N/A");
 | 
			
		||||
            XMLResponseBody responseBody = prepareErrorResponseBody(e.getMessage(), "N/A");
 | 
			
		||||
 | 
			
		||||
            response.status(400);
 | 
			
		||||
            response.body(this.gson.toJson(responseBody));
 | 
			
		||||
@@ -189,7 +122,7 @@ public class XmlController implements RestController {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (requestBody.getProcessor() == null) {
 | 
			
		||||
            nonValidEngineSelectedResponse(response);
 | 
			
		||||
            invalidEngineSelectedResponse(response);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -203,7 +136,7 @@ public class XmlController implements RestController {
 | 
			
		||||
                return;
 | 
			
		||||
 | 
			
		||||
            default:
 | 
			
		||||
                nonValidEngineSelectedResponse(response);
 | 
			
		||||
                invalidEngineSelectedResponse(response);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -211,31 +144,18 @@ public class XmlController implements RestController {
 | 
			
		||||
        XMLResponseBody responseBody = null;
 | 
			
		||||
        long timeStart = System.currentTimeMillis();
 | 
			
		||||
        long duration;
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            if (xmlJobType == XmlJobType.XPath) {
 | 
			
		||||
                XPathQueryResult xPathQueryResult =
 | 
			
		||||
                        engine.processXPath(requestBody.getData(), requestBody.getProcess(), requestBody.getVersion());
 | 
			
		||||
            responseBody = selectProcessMethod(response, requestBody, engine, xmlJobType);
 | 
			
		||||
 | 
			
		||||
                duration = System.currentTimeMillis() - timeStart;
 | 
			
		||||
                responseBody = new XMLResponseBody(xPathQueryResult.getData().trim(),
 | 
			
		||||
                        "OK", engine.getVersion(), duration);
 | 
			
		||||
                responseBody.setType(xPathQueryResult.getType());
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                String result = engine.processXSLT(requestBody.getData(), requestBody.getProcess());
 | 
			
		||||
            duration = System.currentTimeMillis() - timeStart;
 | 
			
		||||
            responseBody.setDuration(duration);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
                duration = System.currentTimeMillis() - timeStart;
 | 
			
		||||
                responseBody = new XMLResponseBody(result, "OK", engine.getVersion(), duration);
 | 
			
		||||
            }
 | 
			
		||||
            response.status(200);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
            this.logger.info("Request (" + xmlJobType + ", " + engine.getVersion() + ") processed in " + duration + " ms.");
 | 
			
		||||
        } catch (Exception ex) {
 | 
			
		||||
            responseBody = prepareErrorResponse(ex.getMessage(), engine.getVersion());
 | 
			
		||||
            response.status(400);
 | 
			
		||||
            this.logger.error("Error on processing " + xmlJobType + " using " + engine.getVersion() + ". " + ex);
 | 
			
		||||
            responseBody = processingErrorResponse(response, ex, engine, xmlJobType);
 | 
			
		||||
 | 
			
		||||
        } finally {
 | 
			
		||||
            response.body(this.gson.toJson(responseBody));
 | 
			
		||||
@@ -243,15 +163,61 @@ public class XmlController implements RestController {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private XMLResponseBody prepareErrorResponse(String message, String processor) {
 | 
			
		||||
        return new XMLResponseBody(message, "ERR", processor, -1);
 | 
			
		||||
    private XMLResponseBody selectProcessMethod(Response response, XMLRequestBody requestBody, XmlEngine engine, XmlJobType xmlJobType) throws Exception {
 | 
			
		||||
        if (xmlJobType == XmlJobType.XPath)
 | 
			
		||||
            return processXPath(response, requestBody, engine, xmlJobType);
 | 
			
		||||
        else
 | 
			
		||||
            return processOther(response, requestBody, engine, xmlJobType);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void nonValidEngineSelectedResponse(Response response) {
 | 
			
		||||
    private XMLResponseBody processXPath(Response response, XMLRequestBody requestBody, XmlEngine engine, XmlJobType xmlJobType) throws Exception {
 | 
			
		||||
        XPathQueryResult xPathQueryResult =
 | 
			
		||||
                engine.processXPath(requestBody.getData(), requestBody.getProcess(), requestBody.getVersion());
 | 
			
		||||
 | 
			
		||||
        XMLResponseBody responseBody = new XMLResponseBody(xPathQueryResult.getData().trim(),
 | 
			
		||||
                "OK", engine.getVersion());
 | 
			
		||||
        responseBody.setType(xPathQueryResult.getType());
 | 
			
		||||
 | 
			
		||||
        return responseBody;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private XMLResponseBody processOther(Response response, XMLRequestBody requestBody, XmlEngine engine, XmlJobType xmlJobType) throws Exception {
 | 
			
		||||
        String result = null;
 | 
			
		||||
        switch (xmlJobType) {
 | 
			
		||||
            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", engine.getVersion());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private XMLResponseBody processingErrorResponse(Response response, Exception ex, XmlEngine engine, XmlJobType xmlJobType) {
 | 
			
		||||
        XMLResponseBody responseBody = prepareErrorResponseBody(ex.getMessage(), engine.getVersion());
 | 
			
		||||
        response.status(400);
 | 
			
		||||
        this.logger.error("Error on processing " + xmlJobType + " using " + engine.getVersion() + ". " + ex);
 | 
			
		||||
        return responseBody;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void invalidEngineSelectedResponse(Response response) {
 | 
			
		||||
        XMLResponseBody responseBody =
 | 
			
		||||
                prepareErrorResponse("Valid engines are: saxon, xalan", "N/A");
 | 
			
		||||
                prepareErrorResponseBody("Valid engines are: saxon, xalan", "N/A");
 | 
			
		||||
        response.body(this.gson.toJson(responseBody));
 | 
			
		||||
        response.status(400);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void requestErrorResponse(Response response) {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private XMLResponseBody prepareErrorResponseBody(String message, String processor) {
 | 
			
		||||
        return new XMLResponseBody(message, "ERR", processor, -1);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user