NSCAPI::errorReturn NSCAPIProtobuf2Json(const char* object, const char* request_buffer, unsigned int request_buffer_len, char ** response_buffer, unsigned int *response_buffer_len) { std::string request(request_buffer, request_buffer_len), response, obj(object); try { json_spirit::Object root; if (obj == "SettingsResponseMessage") { Plugin::SettingsResponseMessage message; message.ParseFromString(request); root = json_pb::Plugin::SettingsResponseMessage::to_json(message); } else if (obj == "RegistryResponseMessage") { Plugin::RegistryResponseMessage message; message.ParseFromString(request); root = json_pb::Plugin::RegistryResponseMessage::to_json(message); } else if (obj == "QueryResponseMessage") { Plugin::QueryResponseMessage message; message.ParseFromString(request); root = json_pb::Plugin::QueryResponseMessage::to_json(message); } else { LOG_ERROR_STD("Invalid type: " + obj); return NSCAPI::hasFailed; } std::string response = json_spirit::write(root); *response_buffer_len = static_cast<unsigned int>(response.size()); if (response.empty()) *response_buffer = NULL; else { *response_buffer = new char[*response_buffer_len + 10]; memcpy(*response_buffer, response.c_str(), *response_buffer_len); } } catch (const json_spirit::ParseError &e) { LOG_ERROR_STD("Failed to parse JSON: " + e.reason_); return NSCAPI::hasFailed; } return NSCAPI::isSuccess; }
void client::configuration::i_do_query(destination_container &s, destination_container &d, std::string command, const Plugin::QueryRequestMessage &request, Plugin::QueryResponseMessage &response, bool use_header) { try { boost::program_options::variables_map vm; bool custom_command = false; command_type::const_iterator cit = commands.find(command); if (cit != commands.end()) { command = cit->second.command; custom_command = true; // TODO: Build argument vector here! } if (command.substr(0,8) == "forward_" || command.substr(command.size()-8, 8) == "_forward") { BOOST_FOREACH(const Plugin::QueryRequestMessage::Request &p, request.payload()) { if (p.arguments_size() > 0) { BOOST_FOREACH(const std::string &a, p.arguments()) { if (a == "help-pb") { ::Plugin::Registry::ParameterDetails details; ::Plugin::Registry::ParameterDetail *d = details.add_parameter(); d->set_name("*"); d->set_short_description("This command will forward all arguments to remote system"); nscapi::protobuf::functions::set_response_good_wdata(*response.add_payload(), details.SerializeAsString()); return; } } } } if (!handler->query(s, d, request, response)) nscapi::protobuf::functions::set_response_bad(*response.add_payload(), command + " failed"); } else {
int NSCPClient::clp_handler_impl::query(client::configuration::data_type data, const Plugin::QueryRequestMessage &request_message, Plugin::QueryResponseMessage &response_message) { const ::Plugin::Common_Header& request_header = request_message.header(); connection_data con = parse_header(request_header, data); std::string tmp; int ret = send(con, NSCPIPC::Common_MessageTypes_QUERY_REQUEST, request_message.SerializeAsString(), tmp); response_message.ParseFromString(tmp); return ret; }
int CheckMKClient::clp_handler_impl::query(client::configuration::data_type data, const Plugin::QueryRequestMessage &request_message, Plugin::QueryResponseMessage &response_message) { const ::Plugin::Common_Header& request_header = request_message.header(); int ret = NSCAPI::returnUNKNOWN; connection_data con = parse_header(request_header, data); nscapi::protobuf::functions::make_return_header(response_message.mutable_header(), request_header); instance->send(con); return ret; }
void client::configuration::forward_query(const Plugin::QueryRequestMessage &request, Plugin::QueryResponseMessage &response) { Plugin::QueryResponseMessage local_response; std::string target = "default"; if (request.header().has_destination_id()) target = request.header().destination_id(); BOOST_FOREACH(const std::string t, strEx::s::splitEx(target, std::string(","))) { nscapi::settings_objects::object_handler::optional_object op = targets.find_object(t); if (op) { destination_container d(*op); d.apply(t, request.header()); destination_container s; s.apply(request.header().sender_id(), request.header()); ::Plugin::QueryResponseMessage local_response_message; if (!handler->query(s, d, request, local_response_message)) { nscapi::protobuf::functions::set_response_bad(*response.add_payload(), "Failed to submit"); return; } for (int i = 0; i < local_response_message.payload_size(); i++) { response.add_payload()->CopyFrom(local_response.payload(i)); } } } }
void client::configuration::do_query(const Plugin::QueryRequestMessage &request, Plugin::QueryResponseMessage &response) { Plugin::QueryResponseMessage local_response; std::string target = "default"; if (request.header().has_recipient_id()) target = request.header().recipient_id(); else if (request.header().has_destination_id()) target = request.header().destination_id(); BOOST_FOREACH(const std::string t, strEx::s::splitEx(target, std::string(","))) { destination_container d; destination_container s; // If we have a target, apply it object_handler_type::object_instance op = targets.find_object(t); if (op) d.apply(op); else { object_handler_type::object_instance op = targets.find_object("default"); if (op) d.apply(op); } // Next apply the header object d.apply(t, request.header()); s.apply(request.header().sender_id(), request.header()); std::string command = request.header().command(); if (!command.empty()) { // If we have a header command treat the data as a batch i_do_query(s, d, command, request, response, true); } else { // Parse each objects command and execute them for (int i=0;i<request.payload_size();i++) { ::Plugin::QueryRequestMessage local_request_message; const ::Plugin::QueryRequestMessage::Request &local_request = request.payload(i); local_request_message.mutable_header()->CopyFrom(request.header()); local_request_message.add_payload()->CopyFrom(local_request); std::string command = local_request.command(); ::Plugin::QueryResponseMessage local_response_message; i_do_query(s, d, command, local_request_message, local_response_message, false); for (int j=0;j<local_response_message.payload_size();j++) { response.add_payload()->CopyFrom(local_response_message.payload(j)); } } } } }