merge new tools (#244)
Co-authored-by: Adam Bem <adam.bem@zoho.eu> Co-authored-by: Adam Bem <bema@noreply.example.com> Reviewed-on: #244
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package com.r11.tools.controller;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.r11.tools.model.MockedMessageDto;
|
||||
import com.r11.tools.service.KlausService;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -9,12 +8,12 @@ import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Returns the homepage and provides the api for javascript async requests.
|
||||
@@ -36,14 +35,12 @@ public class MockController {
|
||||
|
||||
/**
|
||||
* Updates queried message with given set of data
|
||||
* @param body {@link MockedMessageDto} json representation
|
||||
* @param message {@link MockedMessageDto} json representation
|
||||
* @return confirmation and 200 OK
|
||||
*/
|
||||
@SneakyThrows
|
||||
@PutMapping
|
||||
public ResponseEntity<String> updateMessage(@RequestBody String body){
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
MockedMessageDto message = mapper.readValue(body, MockedMessageDto.class);
|
||||
public ResponseEntity<String> updateMessage(@RequestBody MockedMessageDto message){
|
||||
return klausService.setMockedResponse(message);
|
||||
}
|
||||
|
||||
@@ -59,40 +56,10 @@ public class MockController {
|
||||
MockedMessageDto message ;
|
||||
if(uuidValue == null || uuidValue.equals("")) clientUUID = UUID.randomUUID();
|
||||
else clientUUID = UUID.fromString(uuidValue);
|
||||
message = klausService
|
||||
.getMockedMessageByClientUUID(clientUUID)
|
||||
.orElse( buildDefaultMessage(clientUUID) );
|
||||
|
||||
message = klausService.getMockedResponse(clientUUID.toString());
|
||||
return message;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Constructs message with default set of data
|
||||
* @param uuid the key-uuid of given set of messages
|
||||
* @return message with default dataset
|
||||
*/
|
||||
private MockedMessageDto buildDefaultMessage(UUID uuid){
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("Keep-Alive", "timeout=60");
|
||||
headers.put("Connection", "keep-alive");
|
||||
headers.put("Date", LocalDateTime.now().toString());
|
||||
MockedMessageDto mockedMessageDto = MockedMessageDto.builder()
|
||||
.clientUUID(uuid)
|
||||
.contentType(MediaType.APPLICATION_XML_VALUE)
|
||||
.messageBody("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
|
||||
"<note>\n" +
|
||||
" <to>Tove</to>\n" +
|
||||
" <from>Jani</from>\n" +
|
||||
" <heading>Reminder</heading>\n" +
|
||||
" <body>Don't forget me this weekend!</body>\n" +
|
||||
"</note>")
|
||||
.httpHeaders(headers)
|
||||
.httpStatus(200)
|
||||
.build();
|
||||
klausService.setMockedResponse(mockedMessageDto);
|
||||
return mockedMessageDto;
|
||||
}
|
||||
/**
|
||||
* It's one of the most important features - the bread and butter of the Mocked Service. It's link that allows
|
||||
* to receive mocked response from the server and use it to mock!
|
||||
@@ -101,7 +68,7 @@ public class MockController {
|
||||
*/
|
||||
@RequestMapping(value = "/r/{clientUUID}")
|
||||
public ResponseEntity getMockedResponse(
|
||||
@PathVariable UUID clientUUID) {
|
||||
@PathVariable String clientUUID) {
|
||||
MockedMessageDto mockedMessageDto = klausService.getMockedResponse(clientUUID);
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
if (mockedMessageDto.getHttpHeaders() != null) mockedMessageDto.getHttpHeaders().forEach(httpHeaders::set);
|
||||
|
||||
@@ -26,7 +26,7 @@ import java.util.UUID;
|
||||
public class MockedMessage implements Serializable {
|
||||
@Indexed
|
||||
@Id
|
||||
private UUID clientUUID;
|
||||
private String clientUUID;
|
||||
private String contentType;
|
||||
private String messageBody;
|
||||
private Map<String, String> httpHeaders;
|
||||
|
||||
@@ -18,7 +18,7 @@ import java.util.UUID;
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class MockedMessageDto implements Serializable{
|
||||
private UUID clientUUID;
|
||||
private String clientUUID;
|
||||
private String contentType;
|
||||
private String messageBody;
|
||||
private Map<String, String> httpHeaders;
|
||||
|
||||
@@ -1,24 +1,16 @@
|
||||
package com.r11.tools.repository;
|
||||
|
||||
import com.r11.tools.model.MockedMessage;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Spring repository that allows to retrieve message list by key-uuid from redis database
|
||||
* @author Rafał Żukowicz
|
||||
*/
|
||||
@Repository
|
||||
@Transactional
|
||||
public interface MockedResponseRepository extends CrudRepository<MockedMessage, UUID> {
|
||||
/**
|
||||
* Finds all messages by their uuid
|
||||
* @param clientUUID the key-uuid of given set of messages
|
||||
* @return Optional of list of {@link com.r11.tools.model.MockedMessage}
|
||||
*/
|
||||
Optional<MockedMessage> findAllByClientUUID(UUID clientUUID);
|
||||
}
|
||||
public interface MockedResponseRepository extends CrudRepository<MockedMessage, String> {}
|
||||
|
||||
@@ -13,7 +13,6 @@ import org.springframework.stereotype.Service;
|
||||
*/
|
||||
@Service
|
||||
public interface KlausService {
|
||||
Optional<MockedMessageDto> getMockedMessageByClientUUID(UUID clientUUID);
|
||||
MockedMessageDto getMockedResponse(UUID clientUUID);
|
||||
MockedMessageDto getMockedResponse(String clientUUID);
|
||||
ResponseEntity<String> setMockedResponse(MockedMessageDto mockedMessageDto);
|
||||
}
|
||||
|
||||
@@ -10,11 +10,14 @@ import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Service for {@link com.r11.tools.controller.MockController} and {@link com.r11.tools.controller.MockController}
|
||||
@@ -31,18 +34,6 @@ public class KlausServiceImpl implements KlausService {
|
||||
private final MockedResponseRepository mockedResponseRepository;
|
||||
|
||||
|
||||
/**
|
||||
* Returns all messages of given key-uuid
|
||||
* @param clientUUID the key-uuid of given set of messages
|
||||
* @return List of {@link MockedMessageDto}
|
||||
*/
|
||||
@Override
|
||||
public Optional<MockedMessageDto> getMockedMessageByClientUUID(UUID clientUUID){
|
||||
Optional<MockedMessage> mockedMessageOptional = mockedResponseRepository.findAllByClientUUID(clientUUID);
|
||||
log.info("Message for UUID: "+clientUUID+" has been fetched from DB.");
|
||||
return mockedMessageMapper.optionalMockedMessageToOptionalMockedMessageDTO(mockedMessageOptional);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@link MockedMessageDto} of given id and key-uuid. If message doesn't then empty message is returned
|
||||
* @param clientUUID the key-uuid of given set of messages
|
||||
@@ -50,18 +41,44 @@ public class KlausServiceImpl implements KlausService {
|
||||
*/
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public MockedMessageDto getMockedResponse(UUID clientUUID){
|
||||
public MockedMessageDto getMockedResponse(String clientUUID){
|
||||
Optional<MockedMessage> optionalMockedMessage = mockedResponseRepository.findById(clientUUID);
|
||||
MockedMessageDto mockedMessageDto = MockedMessageDto.builder()
|
||||
.clientUUID(clientUUID)
|
||||
.build();
|
||||
MockedMessageDto mockedMessageDto;
|
||||
if (optionalMockedMessage.isPresent()) {
|
||||
mockedMessageDto = mockedMessageMapper.mockedMessageToMockedMessageDto(optionalMockedMessage.get());
|
||||
//log.info(mockedMessageDto.toString().replaceAll("\"", "\\\\\""));
|
||||
return mockedMessageDto;
|
||||
} else {
|
||||
MockedMessageDto defaultMessage = buildDefaultMessage(clientUUID);
|
||||
setMockedResponse(defaultMessage);
|
||||
return defaultMessage;
|
||||
}
|
||||
//log.info(mockedMessageDto.toString().replaceAll("\"", "\\\\\""));
|
||||
return mockedMessageDto;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs message with default set of data
|
||||
* @param uuid the key-uuid of given set of messages
|
||||
* @return message with default dataset
|
||||
*/
|
||||
|
||||
|
||||
private MockedMessageDto buildDefaultMessage(String uuid){
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("Keep-Alive", "timeout=60");
|
||||
headers.put("Connection", "keep-alive");
|
||||
headers.put("Date", LocalDateTime.now().toString());
|
||||
return MockedMessageDto.builder()
|
||||
.clientUUID(uuid)
|
||||
.contentType(MediaType.APPLICATION_XML_VALUE)
|
||||
.messageBody("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
|
||||
"<note>\n" +
|
||||
" <to>Tove</to>\n" +
|
||||
" <from>Jani</from>\n" +
|
||||
" <heading>Reminder</heading>\n" +
|
||||
" <body>Don't forget me this weekend!</body>\n" +
|
||||
"</note>")
|
||||
.httpHeaders(headers)
|
||||
.httpStatus(200)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -72,8 +89,10 @@ public class KlausServiceImpl implements KlausService {
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public ResponseEntity<String> setMockedResponse(MockedMessageDto mockedMessageDto) {
|
||||
mockedResponseRepository.save(mockedMessageMapper.mockedMessageDtoToMockedMessage(mockedMessageDto));
|
||||
//log.info(mockedMessageDto.toString().replaceAll("\"", "\\\\\""));
|
||||
MockedMessage message = mockedMessageMapper.mockedMessageDtoToMockedMessage(mockedMessageDto);
|
||||
message.setCreatedAt(LocalDateTime.now());
|
||||
log.info("SAVE:"+message.toString().replace("\n"," "));
|
||||
mockedResponseRepository.save(message);
|
||||
return new ResponseEntity<>("MockedResponse has been setup successfully!", new HttpHeaders(),
|
||||
HttpStatus.ACCEPTED);
|
||||
}
|
||||
|
||||
@@ -2,11 +2,7 @@ package com.r11.tools;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.r11.tools.controller.JsonController;
|
||||
import com.r11.tools.controller.ProcessorInfoController;
|
||||
import com.r11.tools.controller.XPathController;
|
||||
import com.r11.tools.controller.XsdController;
|
||||
import com.r11.tools.controller.XsltController;
|
||||
import com.r11.tools.controller.*;
|
||||
import com.r11.tools.controller.internal.RestControllerRegistry;
|
||||
import com.r11.tools.xml.Saxon;
|
||||
import com.r11.tools.xml.Xalan;
|
||||
@@ -48,6 +44,7 @@ public class SparkApplication {
|
||||
registry.registerController(new XPathController(gson, logger, saxon, xalan));
|
||||
registry.registerController(new XsltController(gson, logger, saxon, xalan));
|
||||
registry.registerController(new JsonController(gson, jsongson, logger));
|
||||
registry.registerController(new XQueryController(gson, logger, saxon));
|
||||
|
||||
registry.register();
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package com.r11.tools.controller;
|
||||
|
||||
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.model.XPathQueryResult;
|
||||
import com.r11.tools.xml.XmlEngine;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import spark.Request;
|
||||
@@ -24,109 +26,69 @@ public class XPathController implements RestController {
|
||||
this.xalan = xalan;
|
||||
}
|
||||
|
||||
private XMLResponseBody errorResponse(String message, String processor) {
|
||||
return new XMLResponseBody(message, "ERR", processor, -1);
|
||||
}
|
||||
|
||||
private void nonValidEngineSelectedResponse(Response response) {
|
||||
XMLResponseBody responseBody =
|
||||
errorResponse("Valid engines are: saxon, xalan", "N/A");
|
||||
response.body(this.gson.toJson(responseBody));
|
||||
response.status(400);
|
||||
}
|
||||
|
||||
@ScopedControllerManifest(method = HandlerType.POST, path = "/xpath")
|
||||
public void transform(Request request, Response response) {
|
||||
String body = request.body();
|
||||
|
||||
JsonObject requestJson;
|
||||
public void acceptRequest(Request request, Response response) {
|
||||
XMLRequestBody requestBody;
|
||||
try {
|
||||
requestJson = this.gson.fromJson(body, JsonObject.class);
|
||||
requestBody = this.gson.fromJson(request.body(), XMLRequestBody.class);
|
||||
} catch (Exception e) {
|
||||
JsonObject responseJson = new JsonObject();
|
||||
responseJson.addProperty("result", e.getMessage());
|
||||
responseJson.addProperty("processor", "N/A");
|
||||
responseJson.addProperty("status", "ERR");
|
||||
responseJson.addProperty("time", "N/A");
|
||||
|
||||
XMLResponseBody responseBody = errorResponse(e.getMessage(), "N/A");
|
||||
response.status(400);
|
||||
response.body(this.gson.toJson(responseJson));
|
||||
response.body(this.gson.toJson(responseBody));
|
||||
return;
|
||||
}
|
||||
|
||||
String data = requestJson.get("data").getAsString();
|
||||
String query = requestJson.get("process").getAsString();
|
||||
String processor = requestJson.get("processor").getAsString();
|
||||
String version = requestJson.get("version").getAsString();
|
||||
|
||||
if (processor == null) {
|
||||
response.body("saxon, xalan");
|
||||
if (requestBody.getProcessor() == null) {
|
||||
nonValidEngineSelectedResponse(response);
|
||||
return;
|
||||
}
|
||||
|
||||
JsonObject responseJson = new JsonObject();
|
||||
switch (processor) {
|
||||
switch (requestBody.getProcessor()) {
|
||||
case "saxon":
|
||||
processWithSaxon(response, data, query, version, responseJson);
|
||||
process(response, requestBody, saxon);
|
||||
break;
|
||||
case "xalan":
|
||||
processWithXalan(response, data, query, responseJson);
|
||||
process(response, requestBody, xalan);
|
||||
break;
|
||||
default:
|
||||
response.body("saxon, xalan");
|
||||
nonValidEngineSelectedResponse(response);
|
||||
}
|
||||
}
|
||||
|
||||
private void processWithXalan(Response response, String data, String query, JsonObject responseJson) {
|
||||
long timeStart;
|
||||
long duration;
|
||||
response.header("processor", xalan.getVersion());
|
||||
timeStart = System.currentTimeMillis();
|
||||
|
||||
private void process(Response response, XMLRequestBody requestBody, XmlEngine engine) {
|
||||
long timeStart = System.currentTimeMillis();
|
||||
XMLResponseBody responseBody = null;
|
||||
try {
|
||||
XPathQueryResult xPathQueryResult = xalan.processXPath(data, query, "");
|
||||
XPathQueryResult xPathQueryResult =
|
||||
engine.processXPath(requestBody.getData(), requestBody.getProcess(), requestBody.getVersion());
|
||||
|
||||
response.status(200);
|
||||
|
||||
responseJson.addProperty("result", xPathQueryResult.getData().trim());
|
||||
responseJson.addProperty("status", "OK");
|
||||
responseJson.addProperty("type", xPathQueryResult.getType());
|
||||
long duration = System.currentTimeMillis() - timeStart;
|
||||
responseBody = new XMLResponseBody(xPathQueryResult.getData().trim(),
|
||||
"OK", engine.getVersion(),duration);
|
||||
|
||||
responseBody.setType(xPathQueryResult.getType());
|
||||
this.logger.info("Request (XPath, " + engine.getVersion() + ") processed in " + duration + " ms.");
|
||||
} catch (Exception ex) {
|
||||
this.logger.error("Error on processing XPath using Xalan. " + ex);
|
||||
|
||||
responseBody = errorResponse(ex.getMessage(), engine.getVersion());
|
||||
response.status(400);
|
||||
|
||||
responseJson.addProperty("result", ex.getMessage());
|
||||
responseJson.addProperty("status", "ERR");
|
||||
this.logger.error("Error on processing XPath using " + engine.getVersion() + ". " + ex);
|
||||
} finally {
|
||||
response.body(this.gson.toJson(responseBody));
|
||||
}
|
||||
|
||||
duration = System.currentTimeMillis() - timeStart;
|
||||
this.logger.info("Request (XPath, Xalan) processed in " + duration + " ms.");
|
||||
|
||||
responseJson.addProperty("processor", xalan.getVersion());
|
||||
responseJson.addProperty("time", duration);
|
||||
|
||||
response.body(this.gson.toJson(responseJson));
|
||||
}
|
||||
|
||||
private void processWithSaxon(Response response, String data, String query, String version, JsonObject responseJson) {
|
||||
long timeStart;
|
||||
String tmp;
|
||||
long duration;
|
||||
response.header("processor", "Saxon " + saxon.getVersion() + " " + version + " over s9api");
|
||||
timeStart = System.currentTimeMillis();
|
||||
|
||||
try {
|
||||
tmp = saxon.processXPath(data, query, version).getData().trim();
|
||||
|
||||
response.status(200);
|
||||
|
||||
responseJson.addProperty("result", tmp);
|
||||
responseJson.addProperty("status", "OK");
|
||||
} catch (Exception ex) {
|
||||
this.logger.error("Error on processing XPath using Saxon. " + ex);
|
||||
|
||||
response.status(400);
|
||||
|
||||
responseJson.addProperty("result", ex.getMessage());
|
||||
responseJson.addProperty("status", "ERR");
|
||||
}
|
||||
|
||||
duration = System.currentTimeMillis() - timeStart;
|
||||
this.logger.info("Request (XPath, Saxon) processed in " + duration + " ms.");
|
||||
|
||||
responseJson.addProperty("processor", "Saxon " + saxon.getVersion() + " " + version + " over s9api");
|
||||
responseJson.addProperty("time", duration);
|
||||
|
||||
response.body(this.gson.toJson(responseJson));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.r11.tools.controller;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
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;
|
||||
import spark.Response;
|
||||
|
||||
/**
|
||||
* Controller used to handle XQuery tool. Currently, it supports Saxon engine
|
||||
* @author Adam Bem
|
||||
*/
|
||||
@GlobalControllerManifest
|
||||
public class XQueryController implements RestController {
|
||||
|
||||
private final Gson gson;
|
||||
private final Logger logger;
|
||||
private final XmlEngine saxon;
|
||||
|
||||
public XQueryController(Gson gson, Logger logger, XmlEngine saxon) {
|
||||
this.gson = gson;
|
||||
this.logger = logger;
|
||||
this.saxon = saxon;
|
||||
}
|
||||
|
||||
private XMLResponseBody prepareErrorResponse(String message, String processor) {
|
||||
return new XMLResponseBody(message, "ERR", processor, -1);
|
||||
}
|
||||
|
||||
private void nonValidEngineSelectedResponse(Response response) {
|
||||
XMLResponseBody responseBody =
|
||||
prepareErrorResponse("Valid engines are: saxon", "N/A");
|
||||
response.body(this.gson.toJson(responseBody));
|
||||
response.status(400);
|
||||
}
|
||||
|
||||
@ScopedControllerManifest(method = HandlerType.POST, path = "/xquery")
|
||||
public void acceptRequest(Request request, Response response) {
|
||||
XMLRequestBody requestBody;
|
||||
try {
|
||||
requestBody = this.gson.fromJson(request.body(), XMLRequestBody.class);
|
||||
} catch (Exception e) {
|
||||
XMLResponseBody responseBody = prepareErrorResponse(e.getMessage(), "N/A");
|
||||
|
||||
response.status(400);
|
||||
response.body(this.gson.toJson(responseBody));
|
||||
return;
|
||||
}
|
||||
if (requestBody.getProcessor() == null) {
|
||||
nonValidEngineSelectedResponse(response);
|
||||
return;
|
||||
}
|
||||
|
||||
if (requestBody.getProcessor().equalsIgnoreCase("saxon"))
|
||||
process(response, requestBody, saxon);
|
||||
else
|
||||
nonValidEngineSelectedResponse(response);
|
||||
}
|
||||
|
||||
private void process(Response response, XMLRequestBody requestBody, XmlEngine engine) {
|
||||
XMLResponseBody responseBody = null;
|
||||
long timeStart = System.currentTimeMillis();
|
||||
try {
|
||||
String result = engine.executeXQuery(requestBody.getData(), requestBody.getProcess(), requestBody.getVersion());
|
||||
|
||||
response.status(200);
|
||||
|
||||
long duration = System.currentTimeMillis() - timeStart;
|
||||
responseBody = new XMLResponseBody(result, "OK", engine.getVersion(), duration);
|
||||
|
||||
this.logger.info("Request (XQuery, " + engine.getVersion() + ") processed in " + duration + " ms.");
|
||||
} catch (Exception ex) {
|
||||
response.status(400);
|
||||
responseBody = prepareErrorResponse(ex.getMessage(), engine.getVersion());
|
||||
|
||||
this.logger.error("Error on processing XQuery using " + engine.getVersion() + ". " + ex);
|
||||
}
|
||||
finally {
|
||||
response.body(this.gson.toJson(responseBody));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +1,9 @@
|
||||
package com.r11.tools.controller;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.r11.tools.controller.internal.GlobalControllerManifest;
|
||||
import com.r11.tools.controller.internal.HandlerType;
|
||||
import com.r11.tools.controller.internal.RestController;
|
||||
import com.r11.tools.controller.internal.ScopedControllerManifest;
|
||||
import com.r11.tools.xml.Xalan;
|
||||
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,57 +23,63 @@ public class XsdController implements RestController {
|
||||
this.xalan = xalan;
|
||||
}
|
||||
|
||||
@ScopedControllerManifest(method = HandlerType.POST, path = "/xsd")
|
||||
public Response transform(Request request, Response response) {
|
||||
String body = request.body();
|
||||
private XMLResponseBody prepareErrorResponse(String message, String processor) {
|
||||
return new XMLResponseBody(message, "ERR", processor, -1);
|
||||
}
|
||||
|
||||
JsonObject requestJson;
|
||||
private void nonValidEngineSelectedResponse(Response response) {
|
||||
XMLResponseBody responseBody =
|
||||
prepareErrorResponse("Valid engines is: xalan", "N/A");
|
||||
response.body(this.gson.toJson(responseBody));
|
||||
response.status(400);
|
||||
}
|
||||
@ScopedControllerManifest(method = HandlerType.POST, path = "/xsd")
|
||||
public void acceptRequest(Request request, Response response) {
|
||||
XMLRequestBody requestBody;
|
||||
try {
|
||||
requestJson = this.gson.fromJson(body, JsonObject.class);
|
||||
requestBody = this.gson.fromJson(request.body(), XMLRequestBody.class);
|
||||
} catch (Exception e) {
|
||||
JsonObject responseJson = new JsonObject();
|
||||
responseJson.addProperty("result", e.getMessage());
|
||||
responseJson.addProperty("processor", "N/A");
|
||||
responseJson.addProperty("status", "ERR");
|
||||
responseJson.addProperty("time", "N/A");
|
||||
XMLResponseBody responseBody = prepareErrorResponse(e.getMessage(), "N/A");
|
||||
|
||||
response.status(400);
|
||||
response.body(this.gson.toJson(responseJson));
|
||||
return response;
|
||||
response.body(this.gson.toJson(responseBody));
|
||||
return;
|
||||
}
|
||||
|
||||
String data = requestJson.get("data").getAsString();
|
||||
String xsd = requestJson.get("process").getAsString();
|
||||
if (requestBody.getProcessor() == null) {
|
||||
nonValidEngineSelectedResponse(response);
|
||||
return;
|
||||
}
|
||||
if (requestBody.getProcessor().equalsIgnoreCase("xalan"))
|
||||
process(response, requestBody, xalan);
|
||||
else
|
||||
nonValidEngineSelectedResponse(response);
|
||||
|
||||
response.header("processor", xalan.getVersion());
|
||||
}
|
||||
|
||||
long timeStart = System.currentTimeMillis();
|
||||
String tmp;
|
||||
|
||||
JsonObject responseJson = new JsonObject();
|
||||
private void process(Response response, XMLRequestBody requestBody, XmlEngine engine) {
|
||||
XMLResponseBody responseBody = null;
|
||||
try {
|
||||
tmp = xalan.validate(data, xsd).trim();
|
||||
long timeStart = System.currentTimeMillis();
|
||||
String result = engine.validate(requestBody.getData(), requestBody.getProcess()).trim();
|
||||
|
||||
response.status(200);
|
||||
|
||||
responseJson.addProperty("result", tmp);
|
||||
responseJson.addProperty("status", "OK");
|
||||
} catch (Exception ex) {
|
||||
this.logger.error("Error on validation against XSD using Xalan. " + ex);
|
||||
long duration = System.currentTimeMillis() - timeStart;
|
||||
responseBody = new XMLResponseBody(result, "OK", engine.getVersion(), duration);
|
||||
|
||||
this.logger.info("Request (XSD, " + engine.getVersion() + ") processed in " + duration + " ms.");
|
||||
} catch (Exception ex) {
|
||||
responseBody = prepareErrorResponse(ex.getMessage(), engine.getVersion());
|
||||
response.status(400);
|
||||
|
||||
responseJson.addProperty("result", ex.getMessage());
|
||||
responseJson.addProperty("status", "ERR");
|
||||
this.logger.error("Error on validation against XSD using " + engine.getVersion() + ". " + ex);
|
||||
}
|
||||
finally {
|
||||
response.body(this.gson.toJson(responseBody));
|
||||
}
|
||||
|
||||
long duration = System.currentTimeMillis() - timeStart;
|
||||
this.logger.info("Request (XSD, Xalan) processed in " + duration + " ms.");
|
||||
|
||||
responseJson.addProperty("processor", xalan.getVersion());
|
||||
responseJson.addProperty("time", duration);
|
||||
|
||||
response.body(this.gson.toJson(responseJson));
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
package com.r11.tools.controller;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.r11.tools.controller.internal.GlobalControllerManifest;
|
||||
import com.r11.tools.controller.internal.HandlerType;
|
||||
import com.r11.tools.controller.internal.RestController;
|
||||
import com.r11.tools.controller.internal.ScopedControllerManifest;
|
||||
import com.r11.tools.xml.Saxon;
|
||||
import com.r11.tools.xml.Xalan;
|
||||
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;
|
||||
@@ -29,106 +25,69 @@ public class XsltController implements RestController {
|
||||
this.xalan = xalan;
|
||||
}
|
||||
|
||||
@ScopedControllerManifest(method = HandlerType.POST, path = "/xslt")
|
||||
public void transform(Request request, Response response) {
|
||||
String body = request.body();
|
||||
private XMLResponseBody prepareErrorResponse(String message, String processor) {
|
||||
return new XMLResponseBody(message, "ERR", processor, -1);
|
||||
}
|
||||
|
||||
JsonObject requestJson;
|
||||
private void nonValidEngineSelectedResponse(Response response) {
|
||||
XMLResponseBody responseBody =
|
||||
prepareErrorResponse("Valid engines are: saxon, xalan", "N/A");
|
||||
response.body(this.gson.toJson(responseBody));
|
||||
response.status(400);
|
||||
}
|
||||
|
||||
@ScopedControllerManifest(method = HandlerType.POST, path = "/xslt")
|
||||
public void acceptRequest(Request request, Response response) {
|
||||
XMLRequestBody requestBody;
|
||||
try {
|
||||
requestJson = this.gson.fromJson(body, JsonObject.class);
|
||||
requestBody = this.gson.fromJson(request.body(), XMLRequestBody.class);
|
||||
} catch (Exception e) {
|
||||
JsonObject responseJson = new JsonObject();
|
||||
responseJson.addProperty("result", e.getMessage());
|
||||
responseJson.addProperty("processor", "N/A");
|
||||
responseJson.addProperty("status", "ERR");
|
||||
responseJson.addProperty("time", "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;
|
||||
}
|
||||
|
||||
String data = requestJson.get("data").getAsString();
|
||||
String query = requestJson.get("process").getAsString();
|
||||
String processor = requestJson.get("processor").getAsString();
|
||||
String version = requestJson.get("version").getAsString();
|
||||
|
||||
if (processor == null) {
|
||||
response.body("saxon, xalan");
|
||||
if (requestBody.getProcessor() == null) {
|
||||
nonValidEngineSelectedResponse(response);
|
||||
return;
|
||||
}
|
||||
JsonObject responseJson = new JsonObject();
|
||||
switch (processor) {
|
||||
|
||||
switch (requestBody.getProcessor()) {
|
||||
case "saxon":
|
||||
processWithSaxon(response, data, query, version, responseJson);
|
||||
process(response, requestBody, saxon);
|
||||
return;
|
||||
|
||||
case "xalan":
|
||||
processWithXalan(response, data, query, responseJson);
|
||||
process(response, requestBody, xalan);
|
||||
return;
|
||||
|
||||
default:
|
||||
response.body("saxon, xalan");
|
||||
nonValidEngineSelectedResponse(response);
|
||||
}
|
||||
}
|
||||
|
||||
private void processWithXalan(Response response, String data, String query, JsonObject responseJson) {
|
||||
long duration;
|
||||
long timeStart;
|
||||
String tmp;
|
||||
timeStart = System.currentTimeMillis();
|
||||
private void process(Response response, XMLRequestBody requestBody, XmlEngine engine) {
|
||||
XMLResponseBody responseBody = null;
|
||||
long timeStart = System.currentTimeMillis();
|
||||
try {
|
||||
tmp = xalan.processXSLT(data, query);
|
||||
|
||||
String result = engine.processXSLT(requestBody.getData(), requestBody.getProcess());
|
||||
response.status(200);
|
||||
|
||||
responseJson.addProperty("result", tmp);
|
||||
responseJson.addProperty("status", "OK");
|
||||
long duration = System.currentTimeMillis() - timeStart;
|
||||
responseBody = new XMLResponseBody(result, "OK", engine.getVersion(), duration);
|
||||
|
||||
this.logger.info("Request (XSLT, " + engine.getVersion() + ") processed in " + duration + " ms.");
|
||||
} catch (Exception ex) {
|
||||
this.logger.error("Error on processing XSLT using Xalan. " + ex);
|
||||
|
||||
responseBody = prepareErrorResponse(ex.getMessage(), engine.getVersion());
|
||||
response.status(400);
|
||||
this.logger.error("Error on processing XSLT using " + engine.getVersion() + ". " + ex);
|
||||
|
||||
responseJson.addProperty("result", ex.getMessage());
|
||||
responseJson.addProperty("status", "ERR");
|
||||
} finally {
|
||||
response.body(this.gson.toJson(responseBody));
|
||||
}
|
||||
|
||||
duration = System.currentTimeMillis() - timeStart;
|
||||
this.logger.info("Request (XSLT, Xalan) processed in " + duration + " ms.");
|
||||
|
||||
responseJson.addProperty("processor", xalan.getVersion());
|
||||
responseJson.addProperty("time", duration);
|
||||
|
||||
response.body(this.gson.toJson(responseJson));
|
||||
}
|
||||
|
||||
private void processWithSaxon(Response response, String data, String query, String version, JsonObject responseJson) {
|
||||
long duration;
|
||||
String tmp;
|
||||
long timeStart;
|
||||
timeStart = System.currentTimeMillis();
|
||||
try {
|
||||
tmp = saxon.processXSLT(data, query);
|
||||
|
||||
response.status(200);
|
||||
|
||||
responseJson.addProperty("result", tmp);
|
||||
responseJson.addProperty("status", "OK");
|
||||
} catch (Exception ex) {
|
||||
this.logger.error("Error on processing XSLT using Saxon. " + ex);
|
||||
|
||||
response.status(400);
|
||||
|
||||
responseJson.addProperty("result", ex.getMessage());
|
||||
responseJson.addProperty("status", "ERR");
|
||||
}
|
||||
|
||||
duration = System.currentTimeMillis() - timeStart;
|
||||
this.logger.info("Request (XSLT, Saxon) processed in " + duration + " ms.");
|
||||
|
||||
responseJson.addProperty("processor", "Saxon " + saxon.getVersion() + " " + version);
|
||||
responseJson.addProperty("time", duration);
|
||||
|
||||
response.body(this.gson.toJson(responseJson));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.r11.tools.model;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
/**
|
||||
* POJO class used to contain body of XML related requests
|
||||
* @author Adam
|
||||
*/
|
||||
public class XMLRequestBody {
|
||||
@SerializedName("data")
|
||||
private String data;
|
||||
@SerializedName("process")
|
||||
private String process;
|
||||
@SerializedName("processor")
|
||||
private String processor;
|
||||
@SerializedName("version")
|
||||
private String version;
|
||||
|
||||
public String getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public String getProcess() {
|
||||
return process;
|
||||
}
|
||||
|
||||
public String getProcessor() {
|
||||
return processor;
|
||||
}
|
||||
|
||||
public String getVersion() {
|
||||
return version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.r11.tools.model;
|
||||
|
||||
public class XMLResponseBody {
|
||||
|
||||
private String result;
|
||||
private String status;
|
||||
private String processor;
|
||||
private long duration;
|
||||
|
||||
// Optional
|
||||
private String type;
|
||||
|
||||
public XMLResponseBody(String result, String status, String processor, long duration) {
|
||||
this.result = result;
|
||||
this.status = status;
|
||||
this.processor = processor;
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
public String getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setResult(String result) {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getProcessor() {
|
||||
return processor;
|
||||
}
|
||||
|
||||
public void setProcessor(String processor) {
|
||||
this.processor = processor;
|
||||
}
|
||||
|
||||
public long getDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
public void setDuration(long duration) {
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.r11.tools.controller.internal;
|
||||
package com.r11.tools.model;
|
||||
|
||||
/**
|
||||
* Class used to store data received from parser and type of that data (node, string, etc.)
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.r11.tools.xml;
|
||||
|
||||
import com.r11.tools.controller.internal.XPathQueryResult;
|
||||
import com.r11.tools.model.XPathQueryResult;
|
||||
import net.sf.saxon.s9api.*;
|
||||
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
@@ -39,6 +39,29 @@ public class Saxon implements XmlEngine{
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method evaluates XQuery exporession on given xml
|
||||
* @param data xml
|
||||
* @param xquery expression
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public String executeXQuery(String data, String xquery, String version) throws Exception {
|
||||
Processor processor = new Processor(false);
|
||||
|
||||
XQueryCompiler compiler = processor.newXQueryCompiler();
|
||||
compiler.setLanguageVersion(version);
|
||||
|
||||
XQueryExecutable executable = compiler.compile(xquery);
|
||||
|
||||
XQueryEvaluator evaluator = executable.load();
|
||||
evaluator.setSource(new StreamSource(new StringReader(data)));
|
||||
|
||||
XdmValue result = evaluator.evaluate();
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Process xpath and return either node or wrapped atomic value
|
||||
* @param data xml to be querried
|
||||
@@ -76,6 +99,6 @@ public class Saxon implements XmlEngine{
|
||||
* @return version of the processor
|
||||
*/
|
||||
public String getVersion() {
|
||||
return new Processor(false).getSaxonProductVersion();
|
||||
return "Saxon " + new Processor(false).getSaxonProductVersion();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package com.r11.tools.xml;
|
||||
|
||||
import com.r11.tools.controller.internal.XPathQueryResult;
|
||||
import com.r11.tools.model.XPathQueryResult;
|
||||
import org.apache.xpath.XPathAPI;
|
||||
import org.apache.xpath.objects.XObject;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.traversal.NodeIterator;
|
||||
@@ -133,4 +132,9 @@ public class Xalan implements XmlEngine{
|
||||
validator.validate(dataSource);
|
||||
return "XML file is valid";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String executeXQuery(String data, String xquery, String version) throws Exception {
|
||||
throw new UnsupportedOperationException("Xalan doesn't support XQuery evaluation");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
package com.r11.tools.xml;
|
||||
|
||||
import com.r11.tools.controller.internal.XPathQueryResult;
|
||||
import com.r11.tools.model.XPathQueryResult;
|
||||
|
||||
public interface XmlEngine {
|
||||
XPathQueryResult processXPath(String data, String query, String version) throws Exception;
|
||||
String processXSLT(String data, String transform) throws Exception;
|
||||
String validate(String data, String xsd) throws Exception;
|
||||
|
||||
String executeXQuery(String data, String xquery, String version) throws Exception;
|
||||
|
||||
public String getVersion();
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user