Implement response POJO in XPath controller

This commit is contained in:
2023-06-07 11:16:12 +02:00
parent a6593bcf1d
commit 3c8f3f4d41
2 changed files with 22 additions and 34 deletions

View File

@@ -4,6 +4,7 @@ import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.r11.tools.controller.internal.*;
import com.r11.tools.model.XMLRequestBody;
import com.r11.tools.model.XMLResponseBody;
import com.r11.tools.model.XPathQueryResult;
import com.r11.tools.xml.XmlEngine;
import org.apache.logging.log4j.Logger;
@@ -26,26 +27,20 @@ public class XPathController implements RestController {
this.xalan = xalan;
}
private JsonObject prepareErrorResponse(String message, String processor) {
JsonObject result = new JsonObject();
result.addProperty("result", message);
result.addProperty("processor", processor);
result.addProperty("status", "ERR");
result.addProperty("time", "N/A");
return result;
private XMLResponseBody prepareErrorResponse(String message, String processor) {
return new XMLResponseBody(message, "ERR", processor, "N/A");
}
@ScopedControllerManifest(method = HandlerType.POST, path = "/xpath")
public void transform(Request request, Response response) {
String body = request.body();
XMLRequestBody requestBody;
try {
requestBody = this.gson.fromJson(body, XMLRequestBody.class);
requestBody = this.gson.fromJson(request.body(), XMLRequestBody.class);
} catch (Exception e) {
JsonObject responseJson = prepareErrorResponse(e.getMessage(), "N/A");
XMLResponseBody responseBody = prepareErrorResponse(e.getMessage(), "N/A");
response.status(400);
response.body(this.gson.toJson(responseJson));
response.body(this.gson.toJson(responseBody));
return;
}
@@ -67,36 +62,32 @@ public class XPathController implements RestController {
}
private void processWithXalan(Response response, XMLRequestBody requestBody) {
JsonObject responseJson = new JsonObject();
long timeStart = System.currentTimeMillis();
XMLResponseBody responseBody = null;
try {
XPathQueryResult xPathQueryResult =
xalan.processXPath(requestBody.getData(), requestBody.getProcess(), "");
response.status(200);
responseJson.addProperty("result", xPathQueryResult.getData().trim());
responseJson.addProperty("status", "OK");
responseJson.addProperty("type", xPathQueryResult.getType());
responseJson.addProperty("processor", xalan.getVersion());
long duration = System.currentTimeMillis() - timeStart;
responseJson.addProperty("time", duration);
responseBody = new XMLResponseBody(xPathQueryResult.getData().trim(),
"OK", xalan.getVersion(), String.valueOf(duration));
responseBody.setType(xPathQueryResult.getType());
this.logger.info("Request (XPath, Xalan) processed in " + duration + " ms.");
} catch (Exception ex) {
responseJson = prepareErrorResponse(ex.getMessage(), xalan.getVersion());
responseBody = prepareErrorResponse(ex.getMessage(), xalan.getVersion());
response.status(400);
this.logger.error("Error on processing XPath using Xalan. " + ex);
} finally {
response.body(this.gson.toJson(responseJson));
response.body(this.gson.toJson(responseBody));
}
}
private void processWithSaxon(Response response, XMLRequestBody requestBody) {
JsonObject responseJson = new JsonObject();
XMLResponseBody responseBody = null;
long timeStart = System.currentTimeMillis();
try {
String result =
@@ -105,20 +96,17 @@ public class XPathController implements RestController {
response.status(200);
responseJson.addProperty("result", result);
responseJson.addProperty("status", "OK");
responseJson.addProperty("processor", "Saxon " + saxon.getVersion() + " " + requestBody.getVersion() + " over s9api");
long duration = System.currentTimeMillis() - timeStart;
responseJson.addProperty("time", duration);
String processor = "Saxon " + saxon.getVersion() + " " + requestBody.getVersion() + " over s9api";
responseBody = new XMLResponseBody(result, "OK", processor, String.valueOf(duration));
this.logger.info("Request (XPath, Saxon) processed in " + duration + " ms.");
} catch (Exception ex) {
responseJson = prepareErrorResponse(ex.getMessage(), saxon.getVersion());
responseBody = prepareErrorResponse(ex.getMessage(), saxon.getVersion());
response.status(400);
this.logger.error("Error on processing XPath using Saxon. " + ex);
} finally {
response.body(this.gson.toJson(responseJson));
response.body(this.gson.toJson(responseBody));
}
}

View File

@@ -5,12 +5,12 @@ public class XMLResponseBody {
private String result;
private String status;
private String processor;
private long duration;
private String duration;
// Optional
private String type;
public XMLResponseBody(String result, String status, String processor, long duration) {
public XMLResponseBody(String result, String status, String processor, String duration) {
this.result = result;
this.status = status;
this.processor = processor;
@@ -41,11 +41,11 @@ public class XMLResponseBody {
this.processor = processor;
}
public long getDuration() {
public String getDuration() {
return duration;
}
public void setDuration(long duration) {
public void setDuration(String duration) {
this.duration = duration;
}