libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

commit 832dd5979a83dc75a272efbccec0af085084fa1e
parent 7d33776d78129e3f4f3bbcfbcff0843fb4972bae
Author: Marcello Stanisci <stanisci.m@gmail.com>
Date:   Mon, 30 Sep 2019 15:10:21 +0200

avoid double parsing

Diffstat:
Msrc/main/kotlin/tech/libeufin/Main.kt | 22++++++++++------------
Msrc/main/kotlin/tech/libeufin/XMLTransform.kt | 17++++++++++++++++-
2 files changed, 26 insertions(+), 13 deletions(-)

diff --git a/src/main/kotlin/tech/libeufin/Main.kt b/src/main/kotlin/tech/libeufin/Main.kt @@ -141,28 +141,26 @@ fun main() { post("/ebicsweb") { val body: String = call.receiveText() logger.debug("Body: $body") + val bodyDocument: Document? = xmlProcess.parseStringIntoDom(body) - val isValid = xmlProcess.validateFromString(body) - - if (!isValid) { - logger.error("Invalid request received") + if (bodyDocument == null) { call.respondText( contentType = ContentType.Application.Xml, status = HttpStatusCode.BadRequest - ) { "Bad request" } + ) { "Bad request / Could not parse the body" } return@post + } - val bodyDocument: Document? = xmlProcess.parseStringIntoDom(body) - if (null == bodyDocument) { - /* Should never happen. */ - logger.error("A valid document failed to parse into DOM!") + if (!xmlProcess.validateFromDom(bodyDocument)) { + logger.error("Invalid request received") call.respondText( contentType = ContentType.Application.Xml, - status = HttpStatusCode.InternalServerError - ) { "Internal server error" } + status = HttpStatusCode.BadRequest + ) { "Bad request / invalid document" } return@post } + logger.info(bodyDocument.documentElement.localName) when (bodyDocument.documentElement.localName) { @@ -178,7 +176,7 @@ fun main() { val jaxbHEV: JAXBElement<HEVResponseDataType> = hevResponse.makeHEVResponse() val responseText: String? = xmlProcess.getStringFromJaxb(jaxbHEV) - // FIXME: check if String is actually non-NULL! + call.respondText( contentType = ContentType.Application.Xml, status = HttpStatusCode.OK diff --git a/src/main/kotlin/tech/libeufin/XMLTransform.kt b/src/main/kotlin/tech/libeufin/XMLTransform.kt @@ -116,6 +116,21 @@ class XMLTransform { } /** + * Validates the DOM against the Schema(s) of this object. + * @param domDocument DOM to validate + * @return true/false if the document is valid/invalid + */ + fun validateFromDom(domDocument: Document): Boolean { + try { + validator?.validate(DOMSource(domDocument)) + } catch (e: SAXException) { + e.printStackTrace() + return false + } + return true + } + + /** * Craft object to be passed to the XML validator. * @param xmlString XML body, as read from the POST body. * @return InputStream object, as wanted by the validator. @@ -205,7 +220,7 @@ class XMLTransform { } catch (e: JAXBException) { e.printStackTrace() - return null + return "Bank fatal error." } return sw.toString()