Implement response POJO in XPath controller
This commit is contained in:
@@ -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.model.XPathQueryResult;
|
import com.r11.tools.model.XPathQueryResult;
|
||||||
import com.r11.tools.xml.XmlEngine;
|
import com.r11.tools.xml.XmlEngine;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
@@ -26,26 +27,20 @@ public class XPathController 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, "N/A");
|
||||||
result.addProperty("result", message);
|
|
||||||
result.addProperty("processor", processor);
|
|
||||||
result.addProperty("status", "ERR");
|
|
||||||
result.addProperty("time", "N/A");
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ScopedControllerManifest(method = HandlerType.POST, path = "/xpath")
|
@ScopedControllerManifest(method = HandlerType.POST, path = "/xpath")
|
||||||
public void transform(Request request, Response response) {
|
public void transform(Request request, Response response) {
|
||||||
String body = request.body();
|
|
||||||
|
|
||||||
XMLRequestBody requestBody;
|
XMLRequestBody requestBody;
|
||||||
try {
|
try {
|
||||||
requestBody = this.gson.fromJson(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 responseBody = prepareErrorResponse(e.getMessage(), "N/A");
|
||||||
response.status(400);
|
response.status(400);
|
||||||
response.body(this.gson.toJson(responseJson));
|
response.body(this.gson.toJson(responseBody));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,36 +62,32 @@ public class XPathController implements RestController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void processWithXalan(Response response, XMLRequestBody requestBody) {
|
private void processWithXalan(Response response, XMLRequestBody requestBody) {
|
||||||
JsonObject responseJson = new JsonObject();
|
|
||||||
long timeStart = System.currentTimeMillis();
|
long timeStart = System.currentTimeMillis();
|
||||||
|
XMLResponseBody responseBody = null;
|
||||||
try {
|
try {
|
||||||
XPathQueryResult xPathQueryResult =
|
XPathQueryResult xPathQueryResult =
|
||||||
xalan.processXPath(requestBody.getData(), requestBody.getProcess(), "");
|
xalan.processXPath(requestBody.getData(), requestBody.getProcess(), "");
|
||||||
|
|
||||||
response.status(200);
|
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;
|
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.");
|
this.logger.info("Request (XPath, 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 processing XPath using Xalan. " + ex);
|
this.logger.error("Error on processing XPath using Xalan. " + ex);
|
||||||
} finally {
|
} finally {
|
||||||
response.body(this.gson.toJson(responseJson));
|
response.body(this.gson.toJson(responseBody));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processWithSaxon(Response response, XMLRequestBody requestBody) {
|
private void processWithSaxon(Response response, XMLRequestBody requestBody) {
|
||||||
JsonObject responseJson = new JsonObject();
|
XMLResponseBody responseBody = null;
|
||||||
long timeStart = System.currentTimeMillis();
|
long timeStart = System.currentTimeMillis();
|
||||||
try {
|
try {
|
||||||
String result =
|
String result =
|
||||||
@@ -105,20 +96,17 @@ public class XPathController implements RestController {
|
|||||||
|
|
||||||
response.status(200);
|
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;
|
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.");
|
this.logger.info("Request (XPath, Saxon) processed in " + duration + " ms.");
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
responseJson = prepareErrorResponse(ex.getMessage(), saxon.getVersion());
|
responseBody = prepareErrorResponse(ex.getMessage(), saxon.getVersion());
|
||||||
response.status(400);
|
response.status(400);
|
||||||
this.logger.error("Error on processing XPath using Saxon. " + ex);
|
this.logger.error("Error on processing XPath using Saxon. " + ex);
|
||||||
} finally {
|
} finally {
|
||||||
response.body(this.gson.toJson(responseJson));
|
response.body(this.gson.toJson(responseBody));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,12 +5,12 @@ public class XMLResponseBody {
|
|||||||
private String result;
|
private String result;
|
||||||
private String status;
|
private String status;
|
||||||
private String processor;
|
private String processor;
|
||||||
private long duration;
|
private String duration;
|
||||||
|
|
||||||
// Optional
|
// Optional
|
||||||
private String type;
|
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.result = result;
|
||||||
this.status = status;
|
this.status = status;
|
||||||
this.processor = processor;
|
this.processor = processor;
|
||||||
@@ -41,11 +41,11 @@ public class XMLResponseBody {
|
|||||||
this.processor = processor;
|
this.processor = processor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getDuration() {
|
public String getDuration() {
|
||||||
return duration;
|
return duration;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDuration(long duration) {
|
public void setDuration(String duration) {
|
||||||
this.duration = duration;
|
this.duration = duration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user