Implemented reponse POJO in XQuery controller
This commit is contained in:
@@ -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;
|
||||
@@ -26,26 +27,20 @@ public class XQueryController implements RestController {
|
||||
this.saxon = saxon;
|
||||
}
|
||||
|
||||
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 = "/xquery")
|
||||
public void transform(Request request, Response response) {
|
||||
XMLRequestBody requestBody;
|
||||
JsonObject responseJson;
|
||||
try {
|
||||
requestBody = this.gson.fromJson(request.body(), XMLRequestBody.class);
|
||||
} catch (Exception e) {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -55,39 +50,35 @@ public class XQueryController implements RestController {
|
||||
else
|
||||
response.body("saxon");
|
||||
} catch (Exception e) {
|
||||
responseJson = prepareErrorResponse(e.getMessage(), "Saxon");
|
||||
XMLResponseBody responseBody = prepareErrorResponse(e.getMessage(), "Saxon");
|
||||
|
||||
response.status(400);
|
||||
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 = saxon.executeXQuery(requestBody.getData(), requestBody.getProcess(), requestBody.getVersion());
|
||||
|
||||
response.status(200);
|
||||
|
||||
responseJson.addProperty("result", result);
|
||||
responseJson.addProperty("status", "OK");
|
||||
responseJson.addProperty("processor", "Saxon " + saxon.getVersion());
|
||||
|
||||
long duration = System.currentTimeMillis() - timeStart;
|
||||
responseJson.addProperty("time", duration);
|
||||
responseBody = new XMLResponseBody(result, "OK", saxon.getVersion(), duration);
|
||||
|
||||
this.logger.info("Request (XQuery, Saxon) processed in " + duration + " ms.");
|
||||
} catch (Exception ex) {
|
||||
response.status(400);
|
||||
responseJson = prepareErrorResponse(ex.getMessage(), saxon.getVersion());
|
||||
responseBody = prepareErrorResponse(ex.getMessage(), saxon.getVersion());
|
||||
|
||||
this.logger.error("Error on processing XQuery using Saxon. " + ex);
|
||||
}
|
||||
finally {
|
||||
response.body(this.gson.toJson(responseJson));
|
||||
response.body(this.gson.toJson(responseBody));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user