commit e4b2bc79f192b31ab2450a89b4b65ff1c9c5ebb9
parent da813ed4892f2ee33495dba89453efeb8bb17255
Author: Marcello Stanisci <stanisci.m@gmail.com>
Date: Tue, 24 Sep 2019 14:50:53 +0200
Coding conventions.
Diffstat:
7 files changed, 213 insertions(+), 218 deletions(-)
diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt
@@ -26,15 +26,15 @@ import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
+import org.w3c.dom.Document
import tech.libeufin.messages.HEVResponse
import tech.libeufin.messages.HEVResponseDataType
import tech.libeufin.messages.ProtocolAndVersion
-import tech.libeufin.tech.libeufin.db
import javax.xml.bind.JAXBElement
-fun main(args: Array<String>) {
+fun main( ) {
- var xmlProcess = XMLManagement();
+ var xmlProcess = XMLTransform()
var logger = getLogger()
val server = embeddedServer(Netty, port = 5000) {
@@ -48,29 +48,27 @@ fun main(args: Array<String>) {
val body: String = call.receiveText()
logger.debug("Body: $body")
- val isValid = xmlProcess.validateFromString(body as java.lang.String)
+ val isValid = xmlProcess.validateFromString(body)
if (!isValid) {
logger.error("Invalid request received")
call.respondText(contentType = ContentType.Application.Xml,
- status = HttpStatusCode.BadRequest) {"Bad request"};
+ status = HttpStatusCode.BadRequest) {"Bad request"}
return@post
}
- val bodyDocument = xmlProcess.parseStringIntoDom(body) as org.w3c.dom.Document
- if (null == bodyDocument)
- {
+ val bodyDocument: Document? = xmlProcess.parseStringIntoDom(body)
+ if (null == bodyDocument) {
/* Should never happen. */
logger.error("A valid document failed to parse into DOM!")
call.respondText(contentType = ContentType.Application.Xml,
- status = HttpStatusCode.InternalServerError) {"Internal server error"};
+ status = HttpStatusCode.InternalServerError) {"Internal server error"}
return@post
}
logger.info(bodyDocument.documentElement.localName)
- if ("ebicsHEVRequest" == bodyDocument.documentElement.localName)
- {
- val hevResponse: HEVResponse = HEVResponse(
+ if ("ebicsHEVRequest" == bodyDocument.documentElement.localName) {
+ val hevResponse = HEVResponse(
"000000",
"EBICS_OK",
arrayOf(
@@ -83,14 +81,14 @@ fun main(args: Array<String>) {
val responseText: String? = xmlProcess.getStringFromJaxb(jaxbHEV)
// FIXME: check if String is actually non-NULL!
call.respondText(contentType = ContentType.Application.Xml,
- status = HttpStatusCode.OK) {responseText.toString()};
+ status = HttpStatusCode.OK) {responseText.toString()}
return@post
}
/* Log to console and return "unknown type" */
logger.info("Unknown message, just logging it!")
call.respondText(contentType = ContentType.Application.Xml,
- status = HttpStatusCode.NotFound) {"Not found"};
+ status = HttpStatusCode.NotFound) {"Not found"}
return@post
}
diff --git a/src/main/kotlin/tech/libeufin/DB.kt b/src/main/kotlin/tech/libeufin/DB.kt
@@ -10,6 +10,7 @@ object SignKeys: IntIdTable(){
fun db() {
Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")
+
transaction {
addLogger(StdOutSqlLogger)
SchemaUtils.create(SignKeys)
diff --git a/src/main/kotlin/tech/libeufin/GetLogger.kt b/src/main/kotlin/tech/libeufin/GetLogger.kt
@@ -15,7 +15,7 @@ fun getLogger(): Logger {
fa.context = lc
fa.file = "server.log"
fa.start()
- logger.addAppender(fa);
+ logger.addAppender(fa)
logger.level = Level.DEBUG
- return logger;
- }
+ return logger
+}
diff --git a/src/main/kotlin/tech/libeufin/XMLManagement.kt b/src/main/kotlin/tech/libeufin/XMLManagement.kt
@@ -1,191 +0,0 @@
-package tech.libeufin;
-
-import org.xml.sax.InputSource;
-import org.xml.sax.SAXException;
-
-import java.io.*;
-import javax.xml.XMLConstants;
-import javax.xml.bind.JAXBContext;
-import javax.xml.bind.JAXBElement;
-import javax.xml.bind.JAXBException;
-import javax.xml.bind.Marshaller;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.transform.*;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
-import javax.xml.transform.stream.StreamSource;
-import javax.xml.validation.*; // has SchemaFactory()
-import javax.xml.parsers.DocumentBuilderFactory;
-import org.w3c.dom.Document;
-
-/**
- * This class takes care of importing XSDs and validate
- * XMLs against those.
- */
-
-public class XMLManagement() {
-
- /**
- * Bundle of all the XSDs loaded in memory, from disk.
- */
- private val bundle = {
- val classLoader = ClassLoader.getSystemClassLoader()
- val ebicsHevPath = classLoader.getResourceAsStream("ebics_hev.xsd");
- val schemas = arrayOf(StreamSource(ebicsHevPath)
- // other StreamSources for other schemas here ..
- )
-
- try {
- val sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
- sf.newSchema(schemas)
- } catch (e: SAXException) {
- e.printStackTrace();
- null
- }
- }()
- private val validator = bundle?.newValidator()
-
- /**
- * Parse string into XML DOM.
- * @param xmlString the string to parse.
- * @return the DOM representing @a xmlString
- */
- fun parseStringIntoDom(xmlString: String): Document? {
-
- val factory = DocumentBuilderFactory.newInstance()
- factory.isNamespaceAware = true
-
- try {
- val xmlInputStream = ByteArrayInputStream(xmlString.toByteArray())
- val builder = factory.newDocumentBuilder()
- val document = builder.parse(InputSource(xmlInputStream));
-
- return document;
-
- } catch (e: ParserConfigurationException) {
- e.printStackTrace()
- } catch (e: SAXException) {
- e.printStackTrace()
- } catch (e: IOException) {
- e.printStackTrace()
- }
- return null;
- }
-
- /**
- *
- * @param xmlDoc the XML document to validate
- * @return true when validation passes, false otherwise
- */
- // public boolean validate(Source xmlDoc){
- private fun validate(xmlDoc: StreamSource): Boolean {
- try {
- validator?.validate(xmlDoc)
-
-
- } catch (e: SAXException) {
- e.printStackTrace()
- return false;
- } catch (e: IOException) {
- 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.
- */
- fun validateFromString(xmlString: java.lang.String): Boolean {
- val xmlInputStream: InputStream = ByteArrayInputStream(xmlString.bytes)
- var xmlSource: StreamSource = StreamSource(xmlInputStream)
- return this.validate(xmlSource)
- }
-
- /**
- * Return the DOM representation of the Java object, using the JAXB
- * interface. FIXME: narrow input type to JAXB type!
- *
- * @param object to be transformed into DOM. Typically, the object
- * has already got its setters called.
- * @return the DOM Document, or null (if errors occur).
- */
- fun convertJaxbToDom(obj: JAXBElement<Unit>): Document? {
-
- try {
- val jc = JAXBContext.newInstance("tech.libeufin.messages");
-
- /* Make the target document. */
- val dbf = DocumentBuilderFactory.newInstance()
- val db = dbf.newDocumentBuilder();
- val document = db.newDocument();
-
- /* Marshalling the object into the document. */
- val m = jc.createMarshaller()
- m.marshal(obj, document); // document absorbed XML!
- return document;
-
- } catch (e: JAXBException) {
- e.printStackTrace()
- } catch (e: ParserConfigurationException) {
- e.printStackTrace()
- }
-
- return null;
- }
-
- /**
- * Extract String from DOM.
- *
- * @param document the DOM to extract the string from.
- * @return the final String, or null if errors occur.
- */
- fun getStringFromDocument(document: Document): String? {
-
- try {
- /* Make Transformer. */
- val tf = TransformerFactory.newInstance();
- val t = tf.newTransformer();
-
- t.setOutputProperty(OutputKeys.INDENT, "no");
-
- /* Make string writer. */
- val sw = StringWriter();
-
- /* Extract string. */
- t.transform(DOMSource(document), StreamResult(sw))
- return sw.toString()
-
- } catch (e: TransformerConfigurationException) {
- e.printStackTrace()
- } catch (e: TransformerException) {
- e.printStackTrace()
- }
- return null;
- }
-
- /**
- * Extract String from JAXB.
- *
- * @param object the JAXB instance
- * @return String representation of @a object, or null if errors occur
- */
- fun <T>getStringFromJaxb(obj: JAXBElement<T>): String? {
- try {
- val jc = JAXBContext.newInstance("tech.libeufin.messages")
- val sw = StringWriter();
-
- /* Getting the string. */
- val m = jc.createMarshaller();
- m.marshal(obj, sw);
- m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
- return sw.toString();
-
- } catch (e: JAXBException) {
- e.printStackTrace();
- return null;
- }
- }
-};
-\ No newline at end of file
diff --git a/src/main/kotlin/tech/libeufin/XMLTransform.kt b/src/main/kotlin/tech/libeufin/XMLTransform.kt
@@ -0,0 +1,189 @@
+package tech.libeufin;
+
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+import java.io.*;
+import javax.xml.XMLConstants;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBElement;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.*;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.validation.*; // has SchemaFactory()
+import javax.xml.parsers.DocumentBuilderFactory;
+import org.w3c.dom.Document;
+
+/**
+ * This class takes care of importing XSDs and validate
+ * XMLs against those.
+ */
+
+class XMLTransform {
+
+ /**
+ * Bundle of all the XSDs loaded in memory, from disk.
+ */
+ private val bundle = {
+ val classLoader = ClassLoader.getSystemClassLoader()
+ val ebicsHevPath = classLoader.getResourceAsStream("ebics_hev.xsd")
+ val schemas = arrayOf(StreamSource(ebicsHevPath)
+ // other StreamSources for other schemas here ..
+ )
+
+ try {
+ val sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
+ sf.newSchema(schemas)
+ } catch (e: SAXException) {
+ e.printStackTrace()
+ null
+ }
+ }()
+ private val validator = bundle?.newValidator()
+
+ /**
+ * Parse string into XML DOM.
+ * @param xmlString the string to parse.
+ * @return the DOM representing @a xmlString
+ */
+ fun parseStringIntoDom(xmlString: String): Document? {
+
+ val factory = DocumentBuilderFactory.newInstance()
+ factory.isNamespaceAware = true
+
+ try {
+ val xmlInputStream = ByteArrayInputStream(xmlString.toByteArray())
+ val builder = factory.newDocumentBuilder()
+ val document = builder.parse(InputSource(xmlInputStream));
+
+ return document;
+
+ } catch (e: ParserConfigurationException) {
+ e.printStackTrace()
+ } catch (e: SAXException) {
+ e.printStackTrace()
+ } catch (e: IOException) {
+ e.printStackTrace()
+ }
+ return null;
+ }
+
+ /**
+ *
+ * @param xmlDoc the XML document to validate
+ * @return true when validation passes, false otherwise
+ */
+ private fun validate(xmlDoc: StreamSource): Boolean {
+ try {
+ validator?.validate(xmlDoc)
+ } catch (e: SAXException) {
+ e.printStackTrace()
+ return false;
+ } catch (e: IOException) {
+ 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.
+ */
+ fun validateFromString(xmlString: String): Boolean {
+ val xmlInputStream: InputStream = ByteArrayInputStream(xmlString.toByteArray())
+ var xmlSource = StreamSource(xmlInputStream)
+ return this.validate(xmlSource)
+ }
+
+ /**
+ * Return the DOM representation of the Java object, using the JAXB
+ * interface. FIXME: narrow input type to JAXB type!
+ *
+ * @param object to be transformed into DOM. Typically, the object
+ * has already got its setters called.
+ * @return the DOM Document, or null (if errors occur).
+ */
+ fun convertJaxbToDom(obj: JAXBElement<Unit>): Document? {
+
+ try {
+ val jc = JAXBContext.newInstance("tech.libeufin.messages")
+
+ /* Make the target document. */
+ val dbf = DocumentBuilderFactory.newInstance()
+ val db = dbf.newDocumentBuilder()
+ val document = db.newDocument()
+
+ /* Marshalling the object into the document. */
+ val m = jc.createMarshaller()
+ m.marshal(obj, document) // document absorbed the XML!
+ return document
+
+ } catch (e: JAXBException) {
+ e.printStackTrace()
+ } catch (e: ParserConfigurationException) {
+ e.printStackTrace()
+ }
+ return null;
+ }
+
+ /**
+ * Extract String from DOM.
+ *
+ * @param document the DOM to extract the string from.
+ * @return the final String, or null if errors occur.
+ */
+ fun getStringFromDocument(document: Document): String? {
+
+ try {
+ /* Make Transformer. */
+ val tf = TransformerFactory.newInstance();
+ val t = tf.newTransformer()
+
+ t.setOutputProperty(OutputKeys.INDENT, "no")
+
+ /* Make string writer. */
+ val sw = StringWriter()
+
+ /* Extract string. */
+ t.transform(DOMSource(document), StreamResult(sw))
+ return sw.toString()
+
+ } catch (e: TransformerConfigurationException) {
+ e.printStackTrace()
+ } catch (e: TransformerException) {
+ e.printStackTrace()
+ }
+ return null;
+ }
+
+ /**
+ * Extract String from JAXB.
+ *
+ * @param object the JAXB instance
+ * @return String representation of @a object, or null if errors occur
+ */
+ fun <T>getStringFromJaxb(obj: JAXBElement<T>): String? {
+ val sw = StringWriter()
+
+ try {
+ val jc = JAXBContext.newInstance("tech.libeufin.messages")
+ /* Getting the string. */
+ val m = jc.createMarshaller()
+ m.marshal(obj, sw)
+ m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true)
+
+ } catch (e: JAXBException) {
+ e.printStackTrace()
+ return null
+ }
+
+ return sw.toString()
+ }
+}
+\ No newline at end of file
diff --git a/src/main/kotlin/tech/libeufin/messages/HEVResponse.kt b/src/main/kotlin/tech/libeufin/messages/HEVResponse.kt
@@ -13,21 +13,20 @@ class HEVResponse(
reportText: String
) : this(returnCode, reportText, null)
- val value: HEVResponseDataType = {
+ private val value: HEVResponseDataType = {
val srt = SystemReturnCodeType()
srt.setReturnCode(returnCode);
srt.setReportText(reportText);
val value = HEVResponseDataType();
value.setSystemReturnCode(srt);
- if (null != protocolAndVersion) {
- protocolAndVersion.forEach {
- val entry = HEVResponseDataType.VersionNumber()
- entry.setProtocolVersion(it.protocol)
- entry.setValue(it.version)
- value.getVersionNumber().add(entry)
- }
+ protocolAndVersion?.forEach {
+ val entry = HEVResponseDataType.VersionNumber()
+ entry.setProtocolVersion(it.protocol)
+ entry.setValue(it.version)
+ value.getVersionNumber().add(entry)
}
+
value
}()
diff --git a/src/main/kotlin/tech/libeufin/messages/ProtocolAndVersion.kt b/src/main/kotlin/tech/libeufin/messages/ProtocolAndVersion.kt
@@ -1,7 +1,6 @@
package tech.libeufin.messages
class ProtocolAndVersion(protocol: String, version: String) {
-
val protocol = protocol
val version = version
}
\ No newline at end of file