mirror of
https://github.com/corda/corda.git
synced 2025-01-11 23:43:03 +00:00
Refactor APIServer to use the same whitelist controlled protocol gateway as the scheduler.
This commit is contained in:
parent
119813a36d
commit
0542f7c509
@ -9,10 +9,7 @@ import com.r3corda.core.protocols.ProtocolLogic
|
|||||||
import com.r3corda.core.serialization.SerializedBytes
|
import com.r3corda.core.serialization.SerializedBytes
|
||||||
import com.r3corda.node.api.*
|
import com.r3corda.node.api.*
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
import java.util.*
|
|
||||||
import javax.ws.rs.core.Response
|
import javax.ws.rs.core.Response
|
||||||
import kotlin.reflect.KParameter
|
|
||||||
import kotlin.reflect.jvm.javaType
|
|
||||||
|
|
||||||
class APIServerImpl(val node: AbstractNode) : APIServer {
|
class APIServerImpl(val node: AbstractNode) : APIServer {
|
||||||
|
|
||||||
@ -72,34 +69,11 @@ class APIServerImpl(val node: AbstractNode) : APIServer {
|
|||||||
if (type is ProtocolClassRef) {
|
if (type is ProtocolClassRef) {
|
||||||
val clazz = Class.forName(type.className)
|
val clazz = Class.forName(type.className)
|
||||||
if (ProtocolLogic::class.java.isAssignableFrom(clazz)) {
|
if (ProtocolLogic::class.java.isAssignableFrom(clazz)) {
|
||||||
// TODO for security, check annotated as exposed on API? Or have PublicProtocolLogic... etc
|
@Suppress("UNCHECKED_CAST")
|
||||||
nextConstructor@ for (constructor in clazz.kotlin.constructors) {
|
val logic = clazz as Class<ProtocolLogic<ProtocolLogic<*>>>
|
||||||
val params = HashMap<KParameter, Any?>()
|
val protocolLogicRef = node.services.protocolLogicRefFactory.createKotlin(logic, args)
|
||||||
for (parameter in constructor.parameters) {
|
val protocolInstance = node.services.protocolLogicRefFactory.toProtocolLogic(protocolLogicRef)
|
||||||
if (parameter.isOptional && !args.containsKey(parameter.name)) {
|
return node.services.startProtocol(clazz.name, protocolInstance)
|
||||||
// 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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
throw UnsupportedOperationException("Could not find matching protocol and constructor for: $type $args")
|
throw UnsupportedOperationException("Could not find matching protocol and constructor for: $type $args")
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user