Implemented response POJO in XSD controller
This commit is contained in:
@@ -97,7 +97,7 @@ public class XPathController implements RestController {
|
|||||||
response.status(200);
|
response.status(200);
|
||||||
|
|
||||||
long duration = System.currentTimeMillis() - timeStart;
|
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);
|
responseBody = new XMLResponseBody(result, "OK", processor, duration);
|
||||||
|
|
||||||
this.logger.info("Request (XPath, Saxon) processed in " + duration + " ms.");
|
this.logger.info("Request (XPath, Saxon) processed in " + duration + " ms.");
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import com.google.gson.Gson;
|
|||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.r11.tools.controller.internal.*;
|
import com.r11.tools.controller.internal.*;
|
||||||
import com.r11.tools.model.XMLRequestBody;
|
import com.r11.tools.model.XMLRequestBody;
|
||||||
|
import com.r11.tools.model.XMLResponseBody;
|
||||||
import com.r11.tools.xml.XmlEngine;
|
import com.r11.tools.xml.XmlEngine;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import spark.Request;
|
import spark.Request;
|
||||||
@@ -23,13 +24,8 @@ public class XsdController implements RestController {
|
|||||||
this.xalan = xalan;
|
this.xalan = xalan;
|
||||||
}
|
}
|
||||||
|
|
||||||
private JsonObject prepareErrorResponse(String message, String processor) {
|
private XMLResponseBody prepareErrorResponse(String message, String processor) {
|
||||||
JsonObject result = new JsonObject();
|
return new XMLResponseBody(message, "ERR", processor, -1);
|
||||||
result.addProperty("result", message);
|
|
||||||
result.addProperty("processor", processor);
|
|
||||||
result.addProperty("status", "ERR");
|
|
||||||
result.addProperty("time", "N/A");
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
@ScopedControllerManifest(method = HandlerType.POST, path = "/xsd")
|
@ScopedControllerManifest(method = HandlerType.POST, path = "/xsd")
|
||||||
public Response transform(Request request, Response response) {
|
public Response transform(Request request, Response response) {
|
||||||
@@ -38,36 +34,32 @@ public class XsdController implements RestController {
|
|||||||
try {
|
try {
|
||||||
requestBody = this.gson.fromJson(request.body(), XMLRequestBody.class);
|
requestBody = this.gson.fromJson(request.body(), XMLRequestBody.class);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
JsonObject responseJson = prepareErrorResponse(e.getMessage(), "N/A");
|
XMLResponseBody responseJson = prepareErrorResponse(e.getMessage(), "N/A");
|
||||||
|
|
||||||
response.status(400);
|
response.status(400);
|
||||||
response.body(this.gson.toJson(responseJson));
|
response.body(this.gson.toJson(responseJson));
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonObject responseJson = new JsonObject();
|
XMLResponseBody responseBody;
|
||||||
try {
|
try {
|
||||||
long timeStart = System.currentTimeMillis();
|
long timeStart = System.currentTimeMillis();
|
||||||
String result = xalan.validate(requestBody.getData(), requestBody.getProcess()).trim();
|
String result = xalan.validate(requestBody.getData(), requestBody.getProcess()).trim();
|
||||||
|
|
||||||
response.status(200);
|
response.status(200);
|
||||||
|
|
||||||
responseJson.addProperty("result", result);
|
|
||||||
responseJson.addProperty("status", "OK");
|
|
||||||
responseJson.addProperty("processor", xalan.getVersion());
|
|
||||||
|
|
||||||
long duration = System.currentTimeMillis() - timeStart;
|
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.");
|
this.logger.info("Request (XSD, Xalan) processed in " + duration + " ms.");
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
responseJson = prepareErrorResponse(ex.getMessage(), xalan.getVersion());
|
responseBody = prepareErrorResponse(ex.getMessage(), xalan.getVersion());
|
||||||
response.status(400);
|
response.status(400);
|
||||||
|
|
||||||
this.logger.error("Error on validation against XSD using Xalan. " + ex);
|
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;
|
return response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user