Removed duplicate code and adjusted captions #249
@@ -1,10 +1,7 @@
 | 
			
		||||
package com.r11.tools.controller;
 | 
			
		||||
 | 
			
		||||
import com.google.gson.Gson;
 | 
			
		||||
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.model.XMLRequestBody;
 | 
			
		||||
import com.r11.tools.model.XMLResponseBody;
 | 
			
		||||
import com.r11.tools.model.XPathQueryResult;
 | 
			
		||||
@@ -54,40 +51,34 @@ public class XmlController implements RestController {
 | 
			
		||||
 | 
			
		||||
        switch (requestBody.getProcessor()) {
 | 
			
		||||
            case "saxon":
 | 
			
		||||
                processXPath(response, requestBody, saxon);
 | 
			
		||||
                process(response, requestBody, saxon, XmlJobType.XPath);
 | 
			
		||||
                break;
 | 
			
		||||
            case "xalan":
 | 
			
		||||
                processXPath(response, requestBody, xalan);
 | 
			
		||||
                process(response, requestBody, xalan, XmlJobType.XPath);
 | 
			
		||||
                break;
 | 
			
		||||
            default:
 | 
			
		||||
                nonValidEngineSelectedResponse(response);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void processXPath(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 = 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));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
//    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) {
 | 
			
		||||
@@ -204,11 +195,11 @@ public class XmlController implements RestController {
 | 
			
		||||
 | 
			
		||||
        switch (requestBody.getProcessor()) {
 | 
			
		||||
            case "saxon":
 | 
			
		||||
                processXslt(response, requestBody, saxon);
 | 
			
		||||
                process(response, requestBody, saxon, XmlJobType.XSLT);
 | 
			
		||||
                return;
 | 
			
		||||
 | 
			
		||||
            case "xalan":
 | 
			
		||||
                processXslt(response, requestBody, xalan);
 | 
			
		||||
                process(response, requestBody, xalan, XmlJobType.XSLT);
 | 
			
		||||
                return;
 | 
			
		||||
 | 
			
		||||
            default:
 | 
			
		||||
@@ -216,21 +207,35 @@ public class XmlController implements RestController {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void processXslt(Response response, XMLRequestBody requestBody, XmlEngine engine) {
 | 
			
		||||
    private void process(Response response, XMLRequestBody requestBody, XmlEngine engine, XmlJobType xmlJobType) {
 | 
			
		||||
        XMLResponseBody responseBody = null;
 | 
			
		||||
        long timeStart = System.currentTimeMillis();
 | 
			
		||||
        long duration;
 | 
			
		||||
        try {
 | 
			
		||||
            String result = engine.processXSLT(requestBody.getData(), requestBody.getProcess());
 | 
			
		||||
            if (xmlJobType == XmlJobType.XPath) {
 | 
			
		||||
                XPathQueryResult xPathQueryResult =
 | 
			
		||||
                        engine.processXPath(requestBody.getData(), requestBody.getProcess(), requestBody.getVersion());
 | 
			
		||||
 | 
			
		||||
                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 = new XMLResponseBody(result, "OK", engine.getVersion(), duration);
 | 
			
		||||
            }
 | 
			
		||||
            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.");
 | 
			
		||||
            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 XSLT using " + engine.getVersion() + ". " + ex);
 | 
			
		||||
            this.logger.error("Error on processing " + xmlJobType + " using " + engine.getVersion() + ". " + ex);
 | 
			
		||||
 | 
			
		||||
        } finally {
 | 
			
		||||
            response.body(this.gson.toJson(responseBody));
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,8 @@
 | 
			
		||||
package com.r11.tools.controller.internal;
 | 
			
		||||
 | 
			
		||||
public enum XmlJobType {
 | 
			
		||||
    XPath("XPath"), XSD("XSD"), XQuery("XQuery"), XSLT("XSLT");
 | 
			
		||||
 | 
			
		||||
    XmlJobType(String type) {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user