From 2628a10c510d021792e9a5c5d48a881d7bfa5d8e Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Thu, 2 Mar 2017 17:58:55 +0100 Subject: [PATCH] Refactor the JSON support out into a separate module, :corda:jackson, as Jackson/JSON configuration is generally useful outside the context of web servers. --- build.gradle | 2 +- client/jackson/build.gradle | 14 ++++++++++++++ .../kotlin/net/corda/jackson/JacksonSupport.kt | 9 +++++---- .../kotlin/net/corda/jackson/JacksonSupportTest.kt | 8 +++----- node/webserver/build.gradle | 1 + .../corda/webserver/servlets/ObjectMapperConfig.kt | 4 ++-- .../kotlin/net/corda/simulation/IRSSimulation.kt | 6 ++---- settings.gradle | 2 +- .../kotlin/net/corda/testing/http/HttpUtils.kt | 9 ++++++--- 9 files changed, 35 insertions(+), 20 deletions(-) create mode 100644 client/jackson/build.gradle rename node/webserver/src/main/kotlin/net/corda/webserver/utilities/JsonSupport.kt => client/jackson/src/main/kotlin/net/corda/jackson/JacksonSupport.kt (98%) rename node/webserver/src/test/kotlin/net/corda/webserver/JsonSupportTest.kt => client/jackson/src/test/kotlin/net/corda/jackson/JacksonSupportTest.kt (81%) diff --git a/build.gradle b/build.gradle index 2ce3a0f6ca..9a7da07b34 100644 --- a/build.gradle +++ b/build.gradle @@ -179,7 +179,7 @@ bintrayConfig { projectUrl = 'https://github.com/corda/corda' gpgSign = true gpgPassphrase = System.getenv('CORDA_BINTRAY_GPG_PASSPHRASE') - publications = ['client', 'core', 'corda', 'corda-webserver', 'finance', 'node', 'node-schemas', 'test-utils'] + publications = ['client', 'core', 'corda', 'corda-webserver', 'finance', 'node', 'node-schemas', 'test-utils', 'jackson'] license { name = 'Apache-2.0' url = 'https://www.apache.org/licenses/LICENSE-2.0' diff --git a/client/jackson/build.gradle b/client/jackson/build.gradle new file mode 100644 index 0000000000..0cc36659b1 --- /dev/null +++ b/client/jackson/build.gradle @@ -0,0 +1,14 @@ +apply plugin: 'java' +apply plugin: 'kotlin' +apply plugin: 'net.corda.plugins.publish-utils' + +repositories { + mavenCentral() +} + +dependencies { + compile project(':core') + compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + compile ("com.fasterxml.jackson.module:jackson-module-kotlin:${jackson_version}") + testCompile "junit:junit:$junit_version" +} diff --git a/node/webserver/src/main/kotlin/net/corda/webserver/utilities/JsonSupport.kt b/client/jackson/src/main/kotlin/net/corda/jackson/JacksonSupport.kt similarity index 98% rename from node/webserver/src/main/kotlin/net/corda/webserver/utilities/JsonSupport.kt rename to client/jackson/src/main/kotlin/net/corda/jackson/JacksonSupport.kt index a81a57d9a8..7976a18d75 100644 --- a/node/webserver/src/main/kotlin/net/corda/webserver/utilities/JsonSupport.kt +++ b/client/jackson/src/main/kotlin/net/corda/jackson/JacksonSupport.kt @@ -1,4 +1,4 @@ -package net.corda.webserver.utilities +package net.corda.jackson import com.fasterxml.jackson.core.JsonGenerator import com.fasterxml.jackson.core.JsonParseException @@ -13,10 +13,10 @@ import net.corda.core.contracts.BusinessCalendar import net.corda.core.crypto.* import net.corda.core.messaging.CordaRPCOps import net.corda.core.node.NodeInfo +import net.corda.core.node.services.IdentityService import net.corda.core.serialization.deserialize import net.corda.core.serialization.serialize import net.i2p.crypto.eddsa.EdDSAPublicKey -import net.corda.core.node.services.IdentityService import java.math.BigDecimal import java.time.LocalDate import java.time.LocalDateTime @@ -25,9 +25,9 @@ import java.time.LocalDateTime * Utilities and serialisers for working with JSON representations of basic types. This adds Jackson support for * the java.time API, some core types, and Kotlin data classes. * - * TODO: This does not belong in node. It should be moved to the client module or a dedicated webserver module. + * Note that Jackson can also be used to serialise/deserialise other formats such as Yaml and XML. */ -object JsonSupport { +object JacksonSupport { interface PartyObjectMapper { fun partyFromName(partyName: String): Party? fun partyFromKey(owningKey: CompositeKey): Party? @@ -249,3 +249,4 @@ object JsonSupport { } } } + diff --git a/node/webserver/src/test/kotlin/net/corda/webserver/JsonSupportTest.kt b/client/jackson/src/test/kotlin/net/corda/jackson/JacksonSupportTest.kt similarity index 81% rename from node/webserver/src/test/kotlin/net/corda/webserver/JsonSupportTest.kt rename to client/jackson/src/test/kotlin/net/corda/jackson/JacksonSupportTest.kt index 634d10957f..8ca943f7be 100644 --- a/node/webserver/src/test/kotlin/net/corda/webserver/JsonSupportTest.kt +++ b/client/jackson/src/test/kotlin/net/corda/jackson/JacksonSupportTest.kt @@ -1,20 +1,18 @@ -package net.corda.webserver +package net.corda.jackson import com.pholser.junit.quickcheck.From import com.pholser.junit.quickcheck.Property import com.pholser.junit.quickcheck.runner.JUnitQuickcheck import net.corda.core.testing.PublicKeyGenerator -import net.corda.webserver.utilities.JsonSupport import net.i2p.crypto.eddsa.EdDSAPublicKey import org.junit.runner.RunWith import java.security.PublicKey import kotlin.test.assertEquals @RunWith(JUnitQuickcheck::class) -class JsonSupportTest { - +class JacksonSupportTest { companion object { - val mapper = JsonSupport.createNonRpcMapper() + val mapper = JacksonSupport.createNonRpcMapper() } @Property diff --git a/node/webserver/build.gradle b/node/webserver/build.gradle index 96efca338a..3debf1db88 100644 --- a/node/webserver/build.gradle +++ b/node/webserver/build.gradle @@ -44,6 +44,7 @@ sourceSets { dependencies { compile project(':core') compile project(':node') // TODO: Break this dependency + compile project(':client:jackson') // Web stuff: for HTTP[S] servlets compile "org.eclipse.jetty:jetty-servlet:${jetty_version}" diff --git a/node/webserver/src/main/kotlin/net/corda/webserver/servlets/ObjectMapperConfig.kt b/node/webserver/src/main/kotlin/net/corda/webserver/servlets/ObjectMapperConfig.kt index b0304db015..56965cf877 100644 --- a/node/webserver/src/main/kotlin/net/corda/webserver/servlets/ObjectMapperConfig.kt +++ b/node/webserver/src/main/kotlin/net/corda/webserver/servlets/ObjectMapperConfig.kt @@ -2,7 +2,7 @@ package net.corda.webserver.servlets import com.fasterxml.jackson.databind.ObjectMapper import net.corda.core.messaging.CordaRPCOps -import net.corda.webserver.utilities.JsonSupport +import net.corda.jackson.JacksonSupport import javax.ws.rs.ext.ContextResolver import javax.ws.rs.ext.Provider @@ -12,6 +12,6 @@ import javax.ws.rs.ext.Provider */ @Provider class ObjectMapperConfig(rpc: CordaRPCOps) : ContextResolver { - val defaultObjectMapper = JsonSupport.createDefaultMapper(rpc) + val defaultObjectMapper = JacksonSupport.createDefaultMapper(rpc) override fun getContext(type: Class<*>) = defaultObjectMapper } diff --git a/samples/irs-demo/src/main/kotlin/net/corda/simulation/IRSSimulation.kt b/samples/irs-demo/src/main/kotlin/net/corda/simulation/IRSSimulation.kt index a25207ecd8..89feb5f1e2 100644 --- a/samples/irs-demo/src/main/kotlin/net/corda/simulation/IRSSimulation.kt +++ b/samples/irs-demo/src/main/kotlin/net/corda/simulation/IRSSimulation.kt @@ -18,13 +18,11 @@ import net.corda.flows.TwoPartyDealFlow.Acceptor import net.corda.flows.TwoPartyDealFlow.AutoOffer import net.corda.flows.TwoPartyDealFlow.Instigator import net.corda.irs.contract.InterestRateSwap +import net.corda.jackson.JacksonSupport import net.corda.node.utilities.databaseTransaction import net.corda.testing.initiateSingleShotFlow import net.corda.testing.node.InMemoryMessagingNetwork import net.corda.testing.node.MockIdentityService -import net.i2p.crypto.eddsa.KeyPairGenerator -import java.security.SecureRandom -import net.corda.webserver.utilities.JsonSupport import java.time.LocalDate import java.util.* @@ -33,7 +31,7 @@ import java.util.* * A simulation in which banks execute interest rate swaps with each other, including the fixing events. */ class IRSSimulation(networkSendManuallyPumped: Boolean, runAsync: Boolean, latencyInjector: InMemoryMessagingNetwork.LatencyCalculator?) : Simulation(networkSendManuallyPumped, runAsync, latencyInjector) { - val om = JsonSupport.createInMemoryMapper(MockIdentityService(network.identities)) + val om = JacksonSupport.createInMemoryMapper(MockIdentityService(network.identities)) init { currentDateAndTime = LocalDate.of(2016, 3, 8).atStartOfDay() diff --git a/settings.gradle b/settings.gradle index 6384398d10..d177b9b923 100644 --- a/settings.gradle +++ b/settings.gradle @@ -9,6 +9,7 @@ include 'node' include 'node:capsule' include 'node:webserver' include 'client' +include 'client:jackson' include 'experimental' include 'experimental:sandbox' include 'test-utils' @@ -22,4 +23,3 @@ include 'samples:network-visualiser' include 'samples:simm-valuation-demo' include 'samples:raft-notary-demo' include 'samples:bank-of-corda-demo' - diff --git a/test-utils/src/main/kotlin/net/corda/testing/http/HttpUtils.kt b/test-utils/src/main/kotlin/net/corda/testing/http/HttpUtils.kt index 24ea03f3dd..53f640029e 100644 --- a/test-utils/src/main/kotlin/net/corda/testing/http/HttpUtils.kt +++ b/test-utils/src/main/kotlin/net/corda/testing/http/HttpUtils.kt @@ -3,8 +3,11 @@ package net.corda.testing.http import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.KotlinModule import net.corda.core.utilities.loggerFor -import net.corda.webserver.utilities.JsonSupport -import okhttp3.* +import net.corda.jackson.JacksonSupport +import okhttp3.MediaType +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.RequestBody import java.net.URL import java.util.concurrent.TimeUnit @@ -19,7 +22,7 @@ object HttpUtils { .readTimeout(60, TimeUnit.SECONDS).build() } val defaultMapper: ObjectMapper by lazy { - ObjectMapper().registerModule(JsonSupport.javaTimeModule).registerModule(KotlinModule()) + ObjectMapper().registerModule(JacksonSupport.javaTimeModule).registerModule(KotlinModule()) } fun putJson(url: URL, data: String) : Boolean {