From d022b0be7b44ca5939897d7b9313bf1e3676a82c Mon Sep 17 00:00:00 2001 From: Matthew Nesbit Date: Wed, 13 Jul 2016 11:44:13 +0100 Subject: [PATCH] Refactor APIServer to use the same whitelist controlled protocol gateway as the scheduler. --- .../r3corda/node/internal/APIServerImpl.kt | 36 +++---------------- 1 file changed, 5 insertions(+), 31 deletions(-) diff --git a/node/src/main/kotlin/com/r3corda/node/internal/APIServerImpl.kt b/node/src/main/kotlin/com/r3corda/node/internal/APIServerImpl.kt index 85cafc6692..578673d2c1 100644 --- a/node/src/main/kotlin/com/r3corda/node/internal/APIServerImpl.kt +++ b/node/src/main/kotlin/com/r3corda/node/internal/APIServerImpl.kt @@ -9,10 +9,7 @@ import com.r3corda.core.protocols.ProtocolLogic import com.r3corda.core.serialization.SerializedBytes import com.r3corda.node.api.* import java.time.LocalDateTime -import java.util.* import javax.ws.rs.core.Response -import kotlin.reflect.KParameter -import kotlin.reflect.jvm.javaType class APIServerImpl(val node: AbstractNode) : APIServer { @@ -72,34 +69,11 @@ class APIServerImpl(val node: AbstractNode) : APIServer { if (type is ProtocolClassRef) { val clazz = Class.forName(type.className) if (ProtocolLogic::class.java.isAssignableFrom(clazz)) { - // TODO for security, check annotated as exposed on API? Or have PublicProtocolLogic... etc - nextConstructor@ for (constructor in clazz.kotlin.constructors) { - val params = HashMap() - for (parameter in constructor.parameters) { - if (parameter.isOptional && !args.containsKey(parameter.name)) { - // OK to be missing - } else if (args.containsKey(parameter.name)) { - val value = args[parameter.name] - if (value is Any) { - // TODO consider supporting more complex test here to support coercing numeric/Kotlin types - if (!(parameter.type.javaType as Class<*>).isAssignableFrom(value.javaClass)) { - // Not null and not assignable - break@nextConstructor - } - } else if (!parameter.type.isMarkedNullable) { - // Null and not nullable - break@nextConstructor - } - params[parameter] = value - } else { - break@nextConstructor - } - } - // If we get here then we matched every parameter - val protocol = constructor.callBy(params) as ProtocolLogic<*> - val future = node.smm.add("api-call", protocol) - return future - } + @Suppress("UNCHECKED_CAST") + val logic = clazz as Class>> + val protocolLogicRef = node.services.protocolLogicRefFactory.createKotlin(logic, args) + val protocolInstance = node.services.protocolLogicRefFactory.toProtocolLogic(protocolLogicRef) + return node.services.startProtocol(clazz.name, protocolInstance) } throw UnsupportedOperationException("Could not find matching protocol and constructor for: $type $args") } else {