Removed duplicate code and adjusted captions #249
@@ -1,10 +1,7 @@
|
|||||||
package com.r11.tools.controller;
|
package com.r11.tools.controller;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
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.model.XMLRequestBody;
|
import com.r11.tools.model.XMLRequestBody;
|
||||||
import com.r11.tools.model.XMLResponseBody;
|
import com.r11.tools.model.XMLResponseBody;
|
||||||
import com.r11.tools.model.XPathQueryResult;
|
import com.r11.tools.model.XPathQueryResult;
|
||||||
@@ -54,40 +51,34 @@ public class XmlController implements RestController {
|
|||||||
|
|
||||||
switch (requestBody.getProcessor()) {
|
switch (requestBody.getProcessor()) {
|
||||||
case "saxon":
|
case "saxon":
|
||||||
processXPath(response, requestBody, saxon);
|
process(response, requestBody, saxon, XmlJobType.XPath);
|
||||||
break;
|
break;
|
||||||
case "xalan":
|
case "xalan":
|
||||||
processXPath(response, requestBody, xalan);
|
process(response, requestBody, xalan, XmlJobType.XPath);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
nonValidEngineSelectedResponse(response);
|
nonValidEngineSelectedResponse(response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processXPath(Response response, XMLRequestBody requestBody, XmlEngine engine) {
|
// private void processXPath(Response response, XMLRequestBody requestBody, XmlEngine engine) {
|
||||||
long timeStart = System.currentTimeMillis();
|
// long timeStart = System.currentTimeMillis();
|
||||||
XMLResponseBody responseBody = null;
|
// XMLResponseBody responseBody = null;
|
||||||
try {
|
// try {
|
||||||
XPathQueryResult xPathQueryResult =
|
//
|
||||||
engine.processXPath(requestBody.getData(), requestBody.getProcess(), requestBody.getVersion());
|
//
|
||||||
|
//
|
||||||
response.status(200);
|
// this.logger.info("Request (XPath, " + engine.getVersion() + ") processed in " + duration + " ms.");
|
||||||
long duration = System.currentTimeMillis() - timeStart;
|
// } catch (Exception ex) {
|
||||||
responseBody = new XMLResponseBody(xPathQueryResult.getData().trim(),
|
// responseBody = prepareErrorResponse(ex.getMessage(), engine.getVersion());
|
||||||
"OK", engine.getVersion(),duration);
|
// response.status(400);
|
||||||
|
//
|
||||||
responseBody.setType(xPathQueryResult.getType());
|
// this.logger.error("Error on processing XPath using " + engine.getVersion() + ". " + ex);
|
||||||
this.logger.info("Request (XPath, " + engine.getVersion() + ") processed in " + duration + " ms.");
|
// } finally {
|
||||||
} catch (Exception ex) {
|
// response.body(this.gson.toJson(responseBody));
|
||||||
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")
|
@ScopedControllerManifest(method = HandlerType.POST, path = "/xquery")
|
||||||
public void acceptRequestXQuery(Request request, Response response) {
|
public void acceptRequestXQuery(Request request, Response response) {
|
||||||
@@ -204,11 +195,11 @@ public class XmlController implements RestController {
|
|||||||
|
|
||||||
switch (requestBody.getProcessor()) {
|
switch (requestBody.getProcessor()) {
|
||||||
case "saxon":
|
case "saxon":
|
||||||
processXslt(response, requestBody, saxon);
|
process(response, requestBody, saxon, XmlJobType.XSLT);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case "xalan":
|
case "xalan":
|
||||||
processXslt(response, requestBody, xalan);
|
process(response, requestBody, xalan, XmlJobType.XSLT);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
default:
|
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;
|
XMLResponseBody responseBody = null;
|
||||||
long timeStart = System.currentTimeMillis();
|
long timeStart = System.currentTimeMillis();
|
||||||
|
long duration;
|
||||||
try {
|
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);
|
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) {
|
} catch (Exception ex) {
|
||||||
responseBody = prepareErrorResponse(ex.getMessage(), engine.getVersion());
|
responseBody = prepareErrorResponse(ex.getMessage(), engine.getVersion());
|
||||||
response.status(400);
|
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 {
|
} finally {
|
||||||
response.body(this.gson.toJson(responseBody));
|
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