commit 9677d8671fd8fcc07f64721223ba22627350ad7c
parent 6d5d4960e19d0a765786dfee86b82258d3358902
Author: Marcello Stanisci <stanisci.m@gmail.com>
Date: Thu, 26 Sep 2019 16:27:24 +0200
respond JSON error object
Diffstat:
8 files changed, 127 insertions(+), 51 deletions(-)
diff --git a/build.gradle b/build.gradle
@@ -15,6 +15,8 @@ repositories {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
+ implementation "io.ktor:ktor-gson:1.1.5"
+ // compile group: 'io.ktor', name: 'ktor-gson', version: '0.9.0'
compile "org.jetbrains.exposed:exposed:0.17.3"
compile "io.ktor:ktor-server-netty:1.2.4"
compile "ch.qos.logback:logback-classic:1.2.3"
diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt
@@ -19,18 +19,22 @@
package tech.libeufin
+import io.ktor.gson.*
import io.ktor.application.*
+import io.ktor.features.CallLogging
import io.ktor.http.*
+import io.ktor.request.receive
import io.ktor.request.receiveText
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 javax.xml.bind.JAXBElement
+import io.ktor.features.*
+import io.netty.handler.codec.http.HttpContent
+import java.text.*
enum class Foo {BAR, BAZ}
@@ -40,13 +44,53 @@ fun main() {
var logger = getLogger()
val server = embeddedServer(Netty, port = 5000) {
+
+ install(CallLogging)
+ install(ContentNegotiation){
+ gson {
+ setDateFormat(DateFormat.LONG)
+ setPrettyPrinting()
+ }
+
+ }
+
routing {
get("/") {
logger.debug("GET: not implemented")
call.respondText("Hello LibEuFin!", ContentType.Text.Plain)
return@get
}
- post("/") {
+
+ post("/admin/customers") {
+
+ // parse JSON
+ try {
+ val body = call.receive<Customer>()
+ logger.info(body.toString())
+
+ } catch (e: Exception) {
+ e.printStackTrace()
+ // return error, FIXME: distinguish between server and client error!
+ call.respond(
+ HttpStatusCode.BadRequest,
+ SandboxError(e.message.toString())
+ )
+ return@post
+ }
+
+
+
+ // create table entries: customer + user + partner + system.
+
+ // return response
+ }
+
+ get("/admin/customers/:id") {
+
+ // query DB and return JSON object.
+ }
+
+ post("/ebicsweb") {
val body: String = call.receiveText()
logger.debug("Body: $body")
diff --git a/src/main/kotlin/tech/libeufin/DB.kt b/src/main/kotlin/tech/libeufin/DB.kt
@@ -4,7 +4,7 @@ import org.jetbrains.exposed.dao.IntIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
-const val CUSTOMER_ID_MAX_LENGTH = 20
+const val CUSTOMER_NAME_MAX_LENGTH = 20
const val SUBSCRIBER_ID_MAX_LENGTH = 10
const val PUBLIC_KEY_MAX_LENGTH = 256 // FIXME review this value!
const val PRIV_KEY_MAX_LENGTH = 512 // FIXME review this value!
@@ -67,9 +67,8 @@ enum class KeyStates {
* its customers.
*/
object Customer: IntIdTable() {
- val customerId: Column<String> = varchar(
- "customerId",
- CUSTOMER_ID_MAX_LENGTH).primaryKey()
+ // Customer ID is the default 'id' field provided by the constructor.
+ val name = varchar("name", CUSTOMER_NAME_MAX_LENGTH)
val ebicsUserId = reference("ebicsUserId", EbicsUsers)
}
diff --git a/src/main/kotlin/tech/libeufin/HEVResponse.kt b/src/main/kotlin/tech/libeufin/HEVResponse.kt
@@ -0,0 +1,40 @@
+package tech.libeufin
+
+import tech.libeufin.messages.HEVResponseDataType
+import tech.libeufin.messages.ObjectFactory
+import tech.libeufin.messages.SystemReturnCodeType
+import javax.xml.bind.JAXBElement
+
+
+class HEVResponse(
+ returnCode: String,
+ reportText: String,
+ protocolAndVersion: Array<ProtocolAndVersion>?) {
+
+ constructor(
+ returnCode: String,
+ reportText: String
+ ) : this(returnCode, reportText, null)
+
+ private val value: HEVResponseDataType = {
+ val srt = SystemReturnCodeType()
+ srt.setReturnCode(returnCode);
+ srt.setReportText(reportText);
+ val value = HEVResponseDataType();
+ value.setSystemReturnCode(srt);
+
+ protocolAndVersion?.forEach {
+ val entry = HEVResponseDataType.VersionNumber()
+ entry.setProtocolVersion(it.protocol)
+ entry.setValue(it.version)
+ value.getVersionNumber().add(entry)
+ }
+
+ value
+ }()
+
+ fun makeHEVResponse(): JAXBElement<HEVResponseDataType> {
+ val of = ObjectFactory()
+ return of.createEbicsHEVResponse(value)
+ }
+}
diff --git a/src/main/kotlin/tech/libeufin/JSON.kt b/src/main/kotlin/tech/libeufin/JSON.kt
@@ -0,0 +1,27 @@
+package tech.libeufin
+
+/**
+ * Error message.
+ */
+data class SandboxError (
+ val message: String
+)
+
+
+/**
+ * Request for POST /admin/customers
+ */
+data class Customer (
+ val name: String
+)
+
+/**
+ * Response for GET /admin/customers/:id
+ */
+data class CustomerInfo (
+ val customerEbicsInfo: CustomerEbicsInfo
+)
+
+data class CustomerEbicsInfo (
+ val userId: Int
+)
+\ No newline at end of file
diff --git a/src/main/kotlin/tech/libeufin/ProtocolAndVersion.kt b/src/main/kotlin/tech/libeufin/ProtocolAndVersion.kt
@@ -0,0 +1,6 @@
+package tech.libeufin
+
+class ProtocolAndVersion(protocol: String, version: String) {
+ val protocol = protocol
+ val version = version
+}
+\ 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
@@ -1,37 +0,0 @@
-package tech.libeufin.messages
-
-import javax.xml.bind.JAXBElement
-
-
-class HEVResponse(
- returnCode: String,
- reportText: String,
- protocolAndVersion: Array<ProtocolAndVersion>?) {
-
- constructor(
- returnCode: String,
- reportText: String
- ) : this(returnCode, reportText, null)
-
- private val value: HEVResponseDataType = {
- val srt = SystemReturnCodeType()
- srt.setReturnCode(returnCode);
- srt.setReportText(reportText);
- val value = HEVResponseDataType();
- value.setSystemReturnCode(srt);
-
- protocolAndVersion?.forEach {
- val entry = HEVResponseDataType.VersionNumber()
- entry.setProtocolVersion(it.protocol)
- entry.setValue(it.version)
- value.getVersionNumber().add(entry)
- }
-
- value
- }()
-
- fun makeHEVResponse(): JAXBElement<HEVResponseDataType> {
- val of = ObjectFactory()
- return of.createEbicsHEVResponse(value)
- }
-}
diff --git a/src/main/kotlin/tech/libeufin/messages/ProtocolAndVersion.kt b/src/main/kotlin/tech/libeufin/messages/ProtocolAndVersion.kt
@@ -1,6 +0,0 @@
-package tech.libeufin.messages
-
-class ProtocolAndVersion(protocol: String, version: String) {
- val protocol = protocol
- val version = version
-}
-\ No newline at end of file