Implemented POJO in XSLT 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.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;
|
||||||
@@ -25,13 +26,8 @@ public class XsltController 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 = "/xslt")
|
@ScopedControllerManifest(method = HandlerType.POST, path = "/xslt")
|
||||||
public void transform(Request request, Response response) {
|
public void transform(Request request, Response response) {
|
||||||
@@ -41,10 +37,10 @@ public class XsltController implements RestController {
|
|||||||
try {
|
try {
|
||||||
requestBody = this.gson.fromJson(body, XMLRequestBody.class);
|
requestBody = this.gson.fromJson(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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,56 +64,46 @@ public class XsltController implements RestController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void processWithXalan(Response response, XMLRequestBody requestBody) {
|
private void processWithXalan(Response response, XMLRequestBody requestBody) {
|
||||||
JsonObject responseJson = new JsonObject();
|
XMLResponseBody responseBody = null;
|
||||||
long timeStart = System.currentTimeMillis();
|
long timeStart = System.currentTimeMillis();
|
||||||
try {
|
try {
|
||||||
String result = xalan.processXSLT(requestBody.getData(), requestBody.getProcess());
|
String result = xalan.processXSLT(requestBody.getData(), requestBody.getProcess());
|
||||||
|
|
||||||
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 (XSLT, Xalan) processed in " + duration + " ms.");
|
this.logger.info("Request (XSLT, 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 XSLT using Xalan. " + ex);
|
this.logger.error("Error on processing XSLT 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 = saxon.processXSLT(requestBody.getData(), requestBody.getProcess());
|
String result = saxon.processXSLT(requestBody.getData(), requestBody.getProcess());
|
||||||
|
|
||||||
response.status(200);
|
response.status(200);
|
||||||
|
|
||||||
responseJson.addProperty("result", result);
|
|
||||||
responseJson.addProperty("status", "OK");
|
|
||||||
|
|
||||||
responseJson.addProperty("processor", "Saxon " + saxon.getVersion());
|
|
||||||
long duration = System.currentTimeMillis() - timeStart;
|
long duration = System.currentTimeMillis() - timeStart;
|
||||||
responseJson.addProperty("time", duration);
|
responseBody = new XMLResponseBody(result, "OK", saxon.getVersion(), duration);
|
||||||
this.logger.info("Request (XSLT, Saxon) processed in " + duration + " ms.");
|
this.logger.info("Request (XSLT, 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 XSLT using Saxon. " + ex);
|
this.logger.error("Error on processing XSLT using Saxon. " + ex);
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
response.body(this.gson.toJson(responseJson));
|
response.body(this.gson.toJson(responseBody));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user