Added XMLRequestBody class and implemented it in XPath controller

This commit is contained in:
2023-06-07 10:31:34 +02:00
parent 424bb57c42
commit 800c88fc62
2 changed files with 44 additions and 16 deletions

View File

@@ -37,9 +37,9 @@ public class XPathController 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");
response.status(400);
@@ -47,33 +47,29 @@ public class XPathController 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);
break;
case "xalan":
processWithXalan(response, data, query, responseJson);
processWithXalan(response, requestBody, responseJson);
break;
default:
response.body("saxon, xalan");
}
}
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 {
XPathQueryResult xPathQueryResult = xalan.processXPath(data, query, "");
XPathQueryResult xPathQueryResult =
xalan.processXPath(requestBody.getData(), requestBody.getProcess(), "");
response.status(200);
@@ -97,16 +93,18 @@ public class XPathController 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.processXPath(data, query, version).getData().trim();
String result =
saxon.processXPath(requestBody.getData(), requestBody.getProcess(), requestBody.getVersion())
.getData().trim();
response.status(200);
responseJson.addProperty("result", result);
responseJson.addProperty("status", "OK");
responseJson.addProperty("processor", "Saxon " + saxon.getVersion() + " " + version + " over s9api");
responseJson.addProperty("processor", "Saxon " + saxon.getVersion() + " " + requestBody.getVersion() + " over s9api");
long duration = System.currentTimeMillis() - timeStart;
responseJson.addProperty("time", duration);

View File

@@ -0,0 +1,30 @@
package com.r11.tools.controller.internal;
import com.google.gson.annotations.SerializedName;
public class XMLRequestBody {
@SerializedName("data")
private String data;
@SerializedName("process")
private String process;
@SerializedName("processor")
private String processor;
@SerializedName("version")
private String version;
public String getData() {
return data;
}
public String getProcess() {
return process;
}
public String getProcessor() {
return processor;
}
public String getVersion() {
return version;
}
}