libeufin

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

commit 0e157a5c06d5080c7b7b527f76483cbb784cd77d
parent eb13868f603dc2701bd31561f7c2f82b4385d4d7
Author: Marcello Stanisci <stanisci.m@gmail.com>
Date:   Fri,  8 Nov 2019 16:50:30 +0100

Use 'moshi' to enforce JSON fields.

Diffstat:
Mnexus/build.gradle | 4++++
Mnexus/src/main/kotlin/JSON.kt | 4++++
Mnexus/src/main/kotlin/Main.kt | 24++++++++++++------------
3 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/nexus/build.gradle b/nexus/build.gradle @@ -4,6 +4,8 @@ plugins { id 'application' } +apply plugin: 'kotlin-kapt' + sourceCompatibility = '11' targetCompatibility = '11' version = '0.0.1' @@ -33,6 +35,8 @@ dependencies { implementation "javax.activation:activation:1.1" implementation "org.glassfish.jaxb:jaxb-runtime:2.3.1" implementation 'org.apache.santuario:xmlsec:2.1.4' + kapt 'com.squareup.moshi:moshi-kotlin-codegen:1.8.0' + compile 'com.ryanharter.ktor:ktor-moshi:1.0.1' testImplementation group: 'junit', name: 'junit', version: '4.12' } diff --git a/nexus/src/main/kotlin/JSON.kt b/nexus/src/main/kotlin/JSON.kt @@ -1,9 +1,13 @@ package tech.libeufin.nexus +import com.google.gson.annotations.JsonAdapter +import com.squareup.moshi.JsonClass + /** * This object is POSTed by clients _after_ having created * a EBICS subscriber at the sandbox. */ +@JsonClass(generateAdapter = true) data class EbicsSubscriberInfoRequest( val ebicsURL: String, val hostID: String, diff --git a/nexus/src/main/kotlin/Main.kt b/nexus/src/main/kotlin/Main.kt @@ -19,6 +19,8 @@ package tech.libeufin.nexus +import com.ryanharter.ktor.moshi.moshi +import com.squareup.moshi.JsonDataException import io.ktor.application.ApplicationCallPipeline import io.ktor.application.call import io.ktor.application.install @@ -38,7 +40,6 @@ import io.ktor.routing.post import io.ktor.routing.routing import io.ktor.server.engine.embeddedServer import io.ktor.server.netty.Netty -import org.apache.commons.codec.digest.Crypt import org.apache.xml.security.binding.xmldsig.RSAKeyValueType import org.apache.xml.security.binding.xmldsig.SignatureType import org.jetbrains.exposed.sql.transactions.transaction @@ -156,6 +157,10 @@ fun main() { val server = embeddedServer(Netty, port = 5001) { install(ContentNegotiation) { + + moshi { + } + gson { setDateFormat(DateFormat.LONG) setPrettyPrinting() @@ -168,6 +173,11 @@ fun main() { call.respondText("Internal server error.\n", ContentType.Text.Plain, HttpStatusCode.InternalServerError) } + exception<JsonDataException> { cause -> + logger.error("Exception while handling '${call.request.uri}'", cause) + call.respondText("Bad request\n", ContentType.Text.Plain, HttpStatusCode.BadRequest) + } + exception<NotAnIdError> { cause -> logger.error("Exception while handling '${call.request.uri}'", cause) call.respondText("Bad request\n", ContentType.Text.Plain, HttpStatusCode.BadRequest) @@ -253,17 +263,7 @@ fun main() { post("/ebics/subscribers") { - // FIXME: parsed object is not enforced! - val body = try { - call.receive<EbicsSubscriberInfoRequest>() - } catch (e: Exception) { - e.printStackTrace() - call.respond( - HttpStatusCode.BadRequest, - NexusError(e.message.toString()) - ) - return@post - } + val body = call.receive<EbicsSubscriberInfoRequest>() val pairA = CryptoUtil.generateRsaKeyPair(2048) val pairB = CryptoUtil.generateRsaKeyPair(2048)