Unified intendations to 4 spaces in tools-services

This commit is contained in:
2023-03-02 09:38:06 +01:00
parent 7e63afd07f
commit b5c85a0074
11 changed files with 320 additions and 320 deletions

View File

@@ -12,28 +12,28 @@ import spark.Spark;
public class SparkApplication { public class SparkApplication {
public static void run() { public static void run() {
// TODO: read port from config // TODO: read port from config
Spark.port(8081); Spark.port(8081);
Spark.after((request, response) -> { Spark.after((request, response) -> {
response.header("Access-Control-Allow-Origin", "*"); response.header("Access-Control-Allow-Origin", "*");
response.header("access-control-allow-headers", "*"); response.header("access-control-allow-headers", "*");
response.header("access-control-expose-headers", "*"); response.header("access-control-expose-headers", "*");
response.header("Access-Control-Allow-Methods", "POST"); response.header("Access-Control-Allow-Methods", "POST");
}); });
Logger logger = LogManager.getLogger(SparkApplication.class); Logger logger = LogManager.getLogger(SparkApplication.class);
RestControllerRegistry registry = new RestControllerRegistry(); RestControllerRegistry registry = new RestControllerRegistry();
registry.registerController(new ProcessorInfoController(logger)); registry.registerController(new ProcessorInfoController(logger));
registry.registerController(new XsdController(logger)); registry.registerController(new XsdController(logger));
registry.registerController(new XPathController(logger)); registry.registerController(new XPathController(logger));
registry.registerController(new XsltController(logger)); registry.registerController(new XsltController(logger));
registry.registerController(new JsonController()); registry.registerController(new JsonController());
registry.register(); registry.register();
logger.info("Server is online at port: " + Spark.port()); logger.info("Server is online at port: " + Spark.port());
} }
} }

View File

@@ -1,7 +1,7 @@
package com.r11.tools; package com.r11.tools;
public class SparkInitializer { public class SparkInitializer {
public static void main(String[] args) { public static void main(String[] args) {
SparkApplication.run(); SparkApplication.run();
} }
} }

View File

@@ -13,46 +13,46 @@ import spark.Response;
@GlobalControllerManifest(path = "/json") @GlobalControllerManifest(path = "/json")
public class JsonController implements RestController { public class JsonController implements RestController {
private final Gson prettyGson = new GsonBuilder() private final Gson prettyGson = new GsonBuilder()
.disableHtmlEscaping() .disableHtmlEscaping()
.setPrettyPrinting() .setPrettyPrinting()
.create(); .create();
private final Gson gson = new GsonBuilder() private final Gson gson = new GsonBuilder()
.disableHtmlEscaping() .disableHtmlEscaping()
.create(); .create();
@ScopedControllerManifest(method = HandlerType.POST, path = "/formatting") @ScopedControllerManifest(method = HandlerType.POST, path = "/formatting")
public void formatting(Request request, Response response) { public void formatting(Request request, Response response) {
try { try {
JsonObject jsonObject = this.gson.fromJson(request.body(), JsonObject.class); JsonObject jsonObject = this.gson.fromJson(request.body(), JsonObject.class);
response.status(200); response.status(200);
response.body(this.prettyGson.toJson(jsonObject)); response.body(this.prettyGson.toJson(jsonObject));
} catch (Exception e) { } catch (Exception e) {
response.status(500); response.status(500);
Throwable cause = e.getCause(); Throwable cause = e.getCause();
if (cause == null) { if (cause == null) {
response.body(e.getMessage()); response.body(e.getMessage());
} else { } else {
response.body(cause.getMessage()); response.body(cause.getMessage());
} }
}
} }
}
@ScopedControllerManifest(method = HandlerType.POST, path = "/minimize") @ScopedControllerManifest(method = HandlerType.POST, path = "/minimize")
public void minimize(Request request, Response response) { public void minimize(Request request, Response response) {
try { try {
JsonObject jsonObject = this.prettyGson.fromJson(request.body(), JsonObject.class); JsonObject jsonObject = this.prettyGson.fromJson(request.body(), JsonObject.class);
response.status(200); response.status(200);
response.body(this.gson.toJson(jsonObject)); response.body(this.gson.toJson(jsonObject));
} catch (Exception e) { } catch (Exception e) {
response.status(500); response.status(500);
Throwable cause = e.getCause(); Throwable cause = e.getCause();
if (cause == null) { if (cause == null) {
response.body(e.getMessage()); response.body(e.getMessage());
} else { } else {
response.body(cause.getMessage()); response.body(cause.getMessage());
} }
}
} }
}
} }

View File

@@ -12,23 +12,23 @@ import spark.Response;
@GlobalControllerManifest @GlobalControllerManifest
public class ProcessorInfoController implements RestController { public class ProcessorInfoController implements RestController {
private final Logger logger; private final Logger logger;
public ProcessorInfoController(Logger logger) { public ProcessorInfoController(Logger logger) {
this.logger = logger; this.logger = logger;
} }
/** /**
* Handler that returns processor version * Handler that returns processor version
*/ */
@ScopedControllerManifest(method = HandlerType.GET, path = "/procinfo") @ScopedControllerManifest(method = HandlerType.GET, path = "/procinfo")
public void processorInfo(Request request, Response response) { public void processorInfo(Request request, Response response) {
try { try {
response.header("processor", "Saxon " + Saxon.getVersion() + " over s9api"); response.header("processor", "Saxon " + Saxon.getVersion() + " over s9api");
response.body(Saxon.getVersion()); response.body(Saxon.getVersion());
} catch (Exception ex) { } catch (Exception ex) {
this.logger.error("Error on retrieving engine version. " + ex); this.logger.error("Error on retrieving engine version. " + ex);
response.body(ex.getMessage()); response.body(ex.getMessage());
}
} }
}
} }

View File

@@ -17,88 +17,88 @@ import spark.Response;
@GlobalControllerManifest @GlobalControllerManifest
public class XPathController implements RestController { public class XPathController implements RestController {
private final Logger logger; private final Logger logger;
public XPathController(Logger logger) { public XPathController(Logger logger) {
this.logger = logger; this.logger = logger;
}
@ScopedControllerManifest(method = HandlerType.POST, path = "/xpath")
public void transform(Request request, Response response) throws JsonProcessingException {
String body = request.body();
ObjectMapper mapper = new ObjectMapper();
Map<String, String> requestMap = new HashMap<>();
Map<String, String> responseMap = new HashMap<>();
try {
requestMap = mapper.readValue(body, Map.class);
} catch (JsonProcessingException ex) {
this.logger.error("Request JSON error. " + ex);
responseMap.put("result", ex.getMessage());
responseMap.put("processor", "N/A");
responseMap.put("status", "ERR");
responseMap.put("time", "N/A");
response.status(400);
response.body(mapper.writeValueAsString(responseMap));
return;
} }
String data = requestMap.get("data"); @ScopedControllerManifest(method = HandlerType.POST, path = "/xpath")
String query = requestMap.get("process"); public void transform(Request request, Response response) throws JsonProcessingException {
String processor = requestMap.get("processor"); String body = request.body();
String version = requestMap.get("version"); ObjectMapper mapper = new ObjectMapper();
Map<String, String> requestMap = new HashMap<>();
Map<String, String> responseMap = new HashMap<>();
String tmp = "";
long timeStart;
long duration;
if (processor == null) {
response.body("saxon, xalan");
return;
}
switch (processor) {
case "saxon":
response.header("processor", "Saxon " + Saxon.getVersion() + " " + version + " over s9api");
timeStart = System.currentTimeMillis();
try { try {
tmp = Saxon.processXPath(data, query, version).trim(); requestMap = mapper.readValue(body, Map.class);
responseMap.put("result", tmp); } catch (JsonProcessingException ex) {
responseMap.put("status", "OK"); this.logger.error("Request JSON error. " + ex);
} catch (Exception ex) { responseMap.put("result", ex.getMessage());
this.logger.error("Error on processing XPath using Saxon. " + ex); responseMap.put("processor", "N/A");
responseMap.put("result", ex.getMessage()); responseMap.put("status", "ERR");
responseMap.put("status", "ERR"); responseMap.put("time", "N/A");
response.status(400); response.status(400);
response.body(mapper.writeValueAsString(responseMap));
return;
} }
duration = System.currentTimeMillis() - timeStart;
this.logger.info("Request" + body + " processed in " + duration + " ms.");
responseMap.put("processor", "Saxon " + Saxon.getVersion() + " " + version + " over s9api");
responseMap.put("time", "" + duration);
response.body(mapper.writeValueAsString(responseMap));
return;
case "xalan": String data = requestMap.get("data");
response.header("processor", Xalan.getVersion()); String query = requestMap.get("process");
timeStart = System.currentTimeMillis(); String processor = requestMap.get("processor");
try { String version = requestMap.get("version");
tmp = Xalan.processXPath(data, query).trim();
responseMap.put("result", tmp);
responseMap.put("status", "OK"); String tmp = "";
} catch (Exception ex) { long timeStart;
this.logger.error("Error on processing XPath using Xalan. " + ex); long duration;
responseMap.put("result", ex.getMessage());
responseMap.put("status", "ERR"); if (processor == null) {
response.status(400); response.body("saxon, xalan");
return;
}
switch (processor) {
case "saxon":
response.header("processor", "Saxon " + Saxon.getVersion() + " " + version + " over s9api");
timeStart = System.currentTimeMillis();
try {
tmp = Saxon.processXPath(data, query, version).trim();
responseMap.put("result", tmp);
responseMap.put("status", "OK");
} catch (Exception ex) {
this.logger.error("Error on processing XPath using Saxon. " + ex);
responseMap.put("result", ex.getMessage());
responseMap.put("status", "ERR");
response.status(400);
}
duration = System.currentTimeMillis() - timeStart;
this.logger.info("Request" + body + " processed in " + duration + " ms.");
responseMap.put("processor", "Saxon " + Saxon.getVersion() + " " + version + " over s9api");
responseMap.put("time", "" + duration);
response.body(mapper.writeValueAsString(responseMap));
return;
case "xalan":
response.header("processor", Xalan.getVersion());
timeStart = System.currentTimeMillis();
try {
tmp = Xalan.processXPath(data, query).trim();
responseMap.put("result", tmp);
responseMap.put("status", "OK");
} catch (Exception ex) {
this.logger.error("Error on processing XPath using Xalan. " + ex);
responseMap.put("result", ex.getMessage());
responseMap.put("status", "ERR");
response.status(400);
}
duration = System.currentTimeMillis() - timeStart;
this.logger.info("Request: " + body + " processed in " + duration + " ms.");
responseMap.put("processor", Xalan.getVersion());
responseMap.put("time", Long.toString(duration));
response.body(mapper.writeValueAsString(responseMap));
return;
default:
response.body("saxon, xalan");
} }
duration = System.currentTimeMillis() - timeStart;
this.logger.info("Request: " + body + " processed in " + duration + " ms.");
responseMap.put("processor", Xalan.getVersion());
responseMap.put("time", Long.toString(duration));
response.body(mapper.writeValueAsString(responseMap));
return;
default:
response.body("saxon, xalan");
} }
}
} }

View File

@@ -16,55 +16,55 @@ import spark.Response;
@GlobalControllerManifest @GlobalControllerManifest
public class XsdController implements RestController { public class XsdController implements RestController {
private final Logger logger; private final Logger logger;
public XsdController(Logger logger) { public XsdController(Logger logger) {
this.logger = logger; this.logger = logger;
}
@ScopedControllerManifest(method = HandlerType.POST, path = "/xsd")
public Response transform(Request req, Response resp) throws JsonProcessingException {
String body = req.body();
ObjectMapper mapper = new ObjectMapper();
Map<String, String> requestMap = new HashMap<>();
Map<String, String> responseMap = new HashMap<>();
try {
requestMap = mapper.readValue(body, Map.class);
} catch (JsonProcessingException ex) {
this.logger.error("Request JSON error. " + ex);
responseMap.put("result", ex.getMessage());
responseMap.put("processor", "N/A");
responseMap.put("status", "ERR");
responseMap.put("time", "N/A");
resp.status(400);
resp.body(mapper.writeValueAsString(responseMap));
return resp;
} }
String data = requestMap.get("data"); @ScopedControllerManifest(method = HandlerType.POST, path = "/xsd")
String xsd = requestMap.get("process"); public Response transform(Request req, Response resp) throws JsonProcessingException {
String body = req.body();
resp.header("processor", Xalan.getVersion()); ObjectMapper mapper = new ObjectMapper();
long timeStart = System.currentTimeMillis(); Map<String, String> requestMap = new HashMap<>();
String tmp; Map<String, String> responseMap = new HashMap<>();
try {
tmp = Xalan.validate(data, xsd).trim(); try {
responseMap.put("result", tmp); requestMap = mapper.readValue(body, Map.class);
responseMap.put("status", "OK"); } catch (JsonProcessingException ex) {
} catch (Exception ex) { this.logger.error("Request JSON error. " + ex);
this.logger.error("Error on validation against XSD using Xalan. " + ex); responseMap.put("result", ex.getMessage());
responseMap.put("result", ex.getMessage()); responseMap.put("processor", "N/A");
responseMap.put("status", "ERR"); responseMap.put("status", "ERR");
resp.status(400); responseMap.put("time", "N/A");
resp.status(400);
resp.body(mapper.writeValueAsString(responseMap));
return resp;
}
String data = requestMap.get("data");
String xsd = requestMap.get("process");
resp.header("processor", Xalan.getVersion());
long timeStart = System.currentTimeMillis();
String tmp;
try {
tmp = Xalan.validate(data, xsd).trim();
responseMap.put("result", tmp);
responseMap.put("status", "OK");
} catch (Exception ex) {
this.logger.error("Error on validation against XSD using Xalan. " + ex);
responseMap.put("result", ex.getMessage());
responseMap.put("status", "ERR");
resp.status(400);
}
long duration = System.currentTimeMillis() - timeStart;
this.logger.info("Request: " + body + " processed in " + duration + " ms.");
responseMap.put("processor", Xalan.getVersion());
responseMap.put("time", "" + duration);
resp.body(mapper.writeValueAsString(responseMap));
return resp;
} }
long duration = System.currentTimeMillis() - timeStart;
this.logger.info("Request: " + body + " processed in " + duration + " ms.");
responseMap.put("processor", Xalan.getVersion());
responseMap.put("time", "" + duration);
resp.body(mapper.writeValueAsString(responseMap));
return resp;
}
} }

View File

@@ -19,87 +19,87 @@ import spark.Response;
@GlobalControllerManifest @GlobalControllerManifest
public class XsltController implements RestController { public class XsltController implements RestController {
private final Logger logger; private final Logger logger;
public XsltController(Logger logger) { public XsltController(Logger logger) {
this.logger = logger; this.logger = logger;
}
@ScopedControllerManifest(method = HandlerType.POST, path = "/xslt")
public void transform(Request request, Response response) throws JsonProcessingException {
String body = request.body();
ObjectMapper mapper = new ObjectMapper();
Map<String, String> requestMap = new HashMap<>();
Map<String, String> responseMap = new HashMap<>();
try {
requestMap = mapper.readValue(body, Map.class);
} catch (JsonMappingException | JsonParseException ex) {
this.logger.error("Request JSON error. " + ex);
responseMap.put("result", ex.getMessage());
responseMap.put("processor", "N/A");
responseMap.put("status", "ERR");
responseMap.put("time", "N/A");
response.status(400);
response.body(mapper.writeValueAsString(responseMap));
return;
} }
String data = requestMap.get("data"); @ScopedControllerManifest(method = HandlerType.POST, path = "/xslt")
String query = requestMap.get("process"); public void transform(Request request, Response response) throws JsonProcessingException {
String processor = requestMap.get("processor"); String body = request.body();
String version = requestMap.get("version"); ObjectMapper mapper = new ObjectMapper();
Map<String, String> requestMap = new HashMap<>();
if (processor == null) { Map<String, String> responseMap = new HashMap<>();
response.body("saxon, xalan");
return;
}
String tmp;
long timeStart;
long duration;
switch (processor) {
case "saxon":
timeStart = System.currentTimeMillis();
try { try {
tmp = Saxon.processXSLT(data, query); requestMap = mapper.readValue(body, Map.class);
responseMap.put("result", tmp); } catch (JsonMappingException | JsonParseException ex) {
responseMap.put("status", "OK"); this.logger.error("Request JSON error. " + ex);
} catch (Exception ex) { responseMap.put("result", ex.getMessage());
this.logger.error("Error on processing XSLT using Saxon. " + ex); responseMap.put("processor", "N/A");
responseMap.put("result", ex.getMessage()); responseMap.put("status", "ERR");
responseMap.put("status", "ERR"); responseMap.put("time", "N/A");
response.status(400); response.status(400);
response.body(mapper.writeValueAsString(responseMap));
return;
} }
duration = System.currentTimeMillis() - timeStart; String data = requestMap.get("data");
this.logger.info("Request: " + body + " processed in " + duration + " ms."); String query = requestMap.get("process");
responseMap.put("processor", "Saxon " + Saxon.getVersion() + " " + version); String processor = requestMap.get("processor");
responseMap.put("time", Long.toString(duration)); String version = requestMap.get("version");
response.body(mapper.writeValueAsString(responseMap));
return;
case "xalan": if (processor == null) {
timeStart = System.currentTimeMillis(); response.body("saxon, xalan");
try { return;
tmp = Xalan.processXSLT(data, query);
responseMap.put("result", tmp);
responseMap.put("status", "OK");
} catch (Exception ex) {
this.logger.error("Error on processing XSLT using Xalan. " + ex);
responseMap.put("result", ex.getMessage());
responseMap.put("status", "ERR");
response.status(400);
} }
duration = System.currentTimeMillis() - timeStart; String tmp;
this.logger.info("Request: " + body + " processed in " + duration + " ms."); long timeStart;
responseMap.put("processor", Xalan.getVersion()); long duration;
responseMap.put("time", Long.toString(duration)); switch (processor) {
response.body(mapper.writeValueAsString(responseMap)); case "saxon":
return; timeStart = System.currentTimeMillis();
try {
tmp = Saxon.processXSLT(data, query);
responseMap.put("result", tmp);
responseMap.put("status", "OK");
} catch (Exception ex) {
this.logger.error("Error on processing XSLT using Saxon. " + ex);
responseMap.put("result", ex.getMessage());
responseMap.put("status", "ERR");
response.status(400);
}
default: duration = System.currentTimeMillis() - timeStart;
response.body("saxon, xalan"); this.logger.info("Request: " + body + " processed in " + duration + " ms.");
responseMap.put("processor", "Saxon " + Saxon.getVersion() + " " + version);
responseMap.put("time", Long.toString(duration));
response.body(mapper.writeValueAsString(responseMap));
return;
case "xalan":
timeStart = System.currentTimeMillis();
try {
tmp = Xalan.processXSLT(data, query);
responseMap.put("result", tmp);
responseMap.put("status", "OK");
} catch (Exception ex) {
this.logger.error("Error on processing XSLT using Xalan. " + ex);
responseMap.put("result", ex.getMessage());
responseMap.put("status", "ERR");
response.status(400);
}
duration = System.currentTimeMillis() - timeStart;
this.logger.info("Request: " + body + " processed in " + duration + " ms.");
responseMap.put("processor", Xalan.getVersion());
responseMap.put("time", Long.toString(duration));
response.body(mapper.writeValueAsString(responseMap));
return;
default:
response.body("saxon, xalan");
}
} }
}
} }

View File

@@ -10,5 +10,5 @@ import java.lang.annotation.Target;
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
public @interface GlobalControllerManifest { public @interface GlobalControllerManifest {
String path() default ""; String path() default "";
} }

View File

@@ -2,6 +2,6 @@ package com.r11.tools.controller.internal;
public enum HandlerType { public enum HandlerType {
GET, POST, PUT, DELETE GET, POST, PUT, DELETE
} }

View File

@@ -8,52 +8,52 @@ import spark.Spark;
public class RestControllerRegistry { public class RestControllerRegistry {
private final Set<RestController> registeredControllers; private final Set<RestController> registeredControllers;
public RestControllerRegistry() { public RestControllerRegistry() {
this.registeredControllers = new HashSet<>(); this.registeredControllers = new HashSet<>();
} }
public void registerController(RestController restController) { public void registerController(RestController restController) {
this.registeredControllers.add(restController); this.registeredControllers.add(restController);
} }
public void register() { public void register() {
this.registeredControllers.forEach(controller -> { this.registeredControllers.forEach(controller -> {
if (controller.getClass().isAnnotationPresent(GlobalControllerManifest.class)) { if (controller.getClass().isAnnotationPresent(GlobalControllerManifest.class)) {
for (Method method : controller.getClass().getMethods()) { for (Method method : controller.getClass().getMethods()) {
this.registerAssignableHandlers(controller.getClass(), controller, method); this.registerAssignableHandlers(controller.getClass(), controller, method);
} }
} }
}); });
} }
private void registerAssignableHandlers(Class<? extends RestController> parent, RestController parentValue, Method method) { private void registerAssignableHandlers(Class<? extends RestController> parent, RestController parentValue, Method method) {
if ( if (
(parent.isAnnotationPresent(GlobalControllerManifest.class)) && (parent.isAnnotationPresent(GlobalControllerManifest.class)) &&
(method.isAnnotationPresent(ScopedControllerManifest.class)) (method.isAnnotationPresent(ScopedControllerManifest.class))
) { ) {
HandlerType handlerType = method.getAnnotation(ScopedControllerManifest.class).method(); HandlerType handlerType = method.getAnnotation(ScopedControllerManifest.class).method();
String path = PathBuilder.resolvePathOf( String path = PathBuilder.resolvePathOf(
parent.getAnnotation(GlobalControllerManifest.class).path(), parent.getAnnotation(GlobalControllerManifest.class).path(),
method.getAnnotation(ScopedControllerManifest.class).path() method.getAnnotation(ScopedControllerManifest.class).path()
); );
switch (handlerType) { switch (handlerType) {
case GET: case GET:
Spark.get(path, (request, response) -> method.invoke(parentValue, request, response)); Spark.get(path, (request, response) -> method.invoke(parentValue, request, response));
break; break;
case PUT: case PUT:
Spark.put(path, (request, response) -> method.invoke(parentValue, request, response)); Spark.put(path, (request, response) -> method.invoke(parentValue, request, response));
break; break;
case POST: case POST:
Spark.post(path, (request, response) -> method.invoke(parentValue, request, response)); Spark.post(path, (request, response) -> method.invoke(parentValue, request, response));
break; break;
case DELETE: case DELETE:
Spark.delete(path, (request, response) -> method.invoke(parentValue, request, response)); Spark.delete(path, (request, response) -> method.invoke(parentValue, request, response));
break; break;
} }
}
} }
}
} }

View File

@@ -9,7 +9,7 @@ import java.lang.annotation.Target;
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
public @interface ScopedControllerManifest { public @interface ScopedControllerManifest {
HandlerType method(); HandlerType method();
String path(); String path();
} }