| 1 | /************************************************************************* |
| 2 | * libjson-rpc-cpp |
| 3 | ************************************************************************* |
| 4 | * @file rpcprotocolserverv1.cpp |
| 5 | * @date 10/23/2014 |
| 6 | * @author Peter Spiess-Knafl <dev@spiessknafl.at> |
| 7 | * @license See attached LICENSE.txt |
| 8 | ************************************************************************/ |
| 9 | |
| 10 | #include "rpcprotocolserverv1.h" |
| 11 | #include <jsonrpccpp/common/errors.h> |
| 12 | #include <jsonrpccpp/common/exception.h> |
| 13 | |
| 14 | using namespace jsonrpc; |
| 15 | |
| 16 | RpcProtocolServerV1::RpcProtocolServerV1(IProcedureInvokationHandler &handler) : AbstractProtocolHandler(handler) {} |
| 17 | |
| 18 | void RpcProtocolServerV1::HandleJsonRequest(const Json::Value &req, Json::Value &response) { |
| 19 | if (req.isObject()) { |
| 20 | int error = this->ValidateRequest(req); |
| 21 | if (error == 0) { |
| 22 | try { |
| 23 | this->ProcessRequest(req, response); |
| 24 | } catch (const JsonRpcException &exc) { |
| 25 | this->WrapException(req, exc, response); |
| 26 | } |
| 27 | } else { |
| 28 | this->WrapError(req, error, Errors::GetErrorMessage(error), response); |
| 29 | } |
| 30 | } else { |
| 31 | this->WrapError(Json::nullValue, Errors::ERROR_RPC_INVALID_REQUEST, Errors::GetErrorMessage(Errors::ERROR_RPC_INVALID_REQUEST), response); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | bool RpcProtocolServerV1::ValidateRequestFields(const Json::Value &request) { |
| 36 | if (!(request.isMember(KEY_REQUEST_METHODNAME) && request[KEY_REQUEST_METHODNAME].isString())) |
| 37 | return false; |
| 38 | if (!request.isMember(KEY_REQUEST_ID)) |
| 39 | return false; |
| 40 | if (!request.isMember(KEY_REQUEST_PARAMETERS)) |
| 41 | return false; |
| 42 | if (!(request[KEY_REQUEST_PARAMETERS].isArray() || request[KEY_REQUEST_PARAMETERS].isNull())) |
| 43 | return false; |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | void RpcProtocolServerV1::WrapResult(const Json::Value &request, Json::Value &response, Json::Value &retValue) { |
| 48 | response[KEY_RESPONSE_RESULT] = retValue; |
| 49 | response[KEY_RESPONSE_ERROR] = Json::nullValue; |
| 50 | response[KEY_REQUEST_ID] = request[KEY_REQUEST_ID]; |
| 51 | } |
| 52 | |
| 53 | void RpcProtocolServerV1::WrapError(const Json::Value &request, int code, const std::string &message, Json::Value &result) { |
| 54 | result["error" ]["code" ] = code; |
| 55 | result["error" ]["message" ] = message; |
| 56 | result["result" ] = Json::nullValue; |
| 57 | if (request.isObject() && request.isMember("id" )) { |
| 58 | result["id" ] = request["id" ]; |
| 59 | } else { |
| 60 | result["id" ] = Json::nullValue; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void RpcProtocolServerV1::WrapException(const Json::Value &request, const JsonRpcException &exception, Json::Value &result) { |
| 65 | this->WrapError(request, exception.GetCode(), exception.GetMessage(), result); |
| 66 | result["error" ]["data" ] = exception.GetData(); |
| 67 | } |
| 68 | |
| 69 | procedure_t RpcProtocolServerV1::GetRequestType(const Json::Value &request) { |
| 70 | if (request[KEY_REQUEST_ID] == Json::nullValue) |
| 71 | return RPC_NOTIFICATION; |
| 72 | return RPC_METHOD; |
| 73 | } |
| 74 | |