Implemented response POJO in XSD controller

This commit is contained in:
2023-06-07 11:28:30 +02:00
parent aead1109c7
commit 2f8e1dd052
2 changed files with 9 additions and 17 deletions

View File

@@ -97,7 +97,7 @@ public class XPathController implements RestController {
response.status(200);
long duration = System.currentTimeMillis() - timeStart;
String processor = "Saxon " + saxon.getVersion() + " " + requestBody.getVersion() + " over s9api";
String processor = "Saxon " + saxon.getVersion() + " over s9api";
responseBody = new XMLResponseBody(result, "OK", processor, duration);
this.logger.info("Request (XPath, Saxon) processed in " + duration + " ms.");

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.xml.XmlEngine;
import org.apache.logging.log4j.Logger;
import spark.Request;
@@ -23,13 +24,8 @@ public class XsdController 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, -1);
}
@ScopedControllerManifest(method = HandlerType.POST, path = "/xsd")
public Response transform(Request request, Response response) {
@@ -38,36 +34,32 @@ public class XsdController implements RestController {
try {
requestBody = this.gson.fromJson(request.body(), XMLRequestBody.class);
} catch (Exception e) {
JsonObject responseJson = prepareErrorResponse(e.getMessage(), "N/A");
XMLResponseBody responseJson = prepareErrorResponse(e.getMessage(), "N/A");
response.status(400);
response.body(this.gson.toJson(responseJson));
return response;
}
JsonObject responseJson = new JsonObject();
XMLResponseBody responseBody;
try {
long timeStart = System.currentTimeMillis();
String result = xalan.validate(requestBody.getData(), requestBody.getProcess()).trim();
response.status(200);
responseJson.addProperty("result", result);
responseJson.addProperty("status", "OK");
responseJson.addProperty("processor", xalan.getVersion());
long duration = System.currentTimeMillis() - timeStart;
responseJson.addProperty("time", duration);
responseBody = new XMLResponseBody(result, "OK", xalan.getVersion(), duration);
this.logger.info("Request (XSD, 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 validation against XSD using Xalan. " + ex);
}
response.body(this.gson.toJson(responseJson));
response.body(this.gson.toJson(responseBody));
return response;
}
}