in core/controller/src/main/scala/org/apache/openwhisk/core/controller/WebActions.scala [237:275]
private def resultAsHttp(result: JsValue, transid: TransactionId, rp: WebApiDirectives) = {
Try {
val JsObject(fields) = result
val headers = fields.get("headers").map {
case JsObject(hs) =>
hs.flatMap {
case (k, v) => headersFromJson(k, v)
}.toList
case _ => throw new Throwable("Invalid header")
} getOrElse List.empty
val body = fields.get("body")
val intCode = fields.get(rp.statusCode).map {
// the following throws an exception if the code is not a whole number or a valid code
case JsNumber(c) => c.toIntExact
// parse the string to an Int (not a BigInt) matching JsNumber case match above
// c.toInt could throw an exception if the string isn't an integer
case JsString(c) => c.toInt
case _ => throw new Throwable("Illegal status code")
}
val code: Option[StatusCode] = intCode.map(c => StatusCodes.getForKey(c).getOrElse(StatusCodes.custom(c, "")))
body.collect {
case JsString(str) if str.nonEmpty => interpretHttpResponse(code.getOrElse(OK), headers, str, transid)
case JsString(str) /* str.isEmpty */ => respondWithEmptyEntity(code.getOrElse(NoContent), headers)
case js if js != JsNull => interpretHttpResponseAsJson(code.getOrElse(OK), headers, js, transid)
} getOrElse respondWithEmptyEntity(code.getOrElse(NoContent), headers)
} getOrElse {
// either the result was not a JsObject or there was an exception validating the
// response as an http result (including an invalid status code)
terminate(BadRequest, Messages.invalidMedia(`message/http`))(transid, jsonPrettyPrinter)
}
}