Example #1
0
   virtual bool execute(const HttpRequestContext& requestContext, ///< request context
                        UtlSList& params,                         ///< request param list
                        void* userData,                           ///< user data
                        XmlRpcResponse& response,                 ///< request response
                        ExecutionStatus& status
                        )
      {
         UtlString* dbName = dynamic_cast<UtlString*>(params.at(0));

         if (dbName && !dbName->isNull())
         {
            OsReadLock lock(*ConfigRPC::spDatabaseLock);

            ConfigRPC* db = ConfigRPC::find(*dbName);
            if (db)
            {
               status = db->mCallback->accessAllowed(requestContext, ConfigRPC_Callback::Set);
               if ( XmlRpcMethod::OK == status )
               {
                  // read in the dataset
                  OsConfigDb dataset;
                  OsStatus datasetStatus = db->load(dataset);
                  if ( OS_SUCCESS == datasetStatus )
                  {
                     // get the list of names that the request is asking for
                     UtlContainable* secondParam = params.at(1);
                     if ( secondParam )
                     {
                        UtlHashMap* paramList = dynamic_cast<UtlHashMap*>(secondParam);
                        if (paramList)
                        {
                           /*
                            * Iterate over the requested name/value pairs
                            */
                           UtlHashMapIterator params(*paramList);
                           UtlContainable* nextParam = NULL;
                           size_t paramsSet = 0;

                           while (    XmlRpcMethod::OK == status
                                  && (nextParam = params())
                                  )
                           {
                              UtlString* name = dynamic_cast<UtlString*>(params.key());
                              if ( name )
                              {
                                 UtlString* value = dynamic_cast<UtlString*>(params.value());
                                 if (value)
                                 {
                                    dataset.set(*name, *value);
                                    paramsSet++;
                                 }
                                 else
                                 {
                                    UtlString faultMsg;
                                    faultMsg.append("parameter name '");
                                    faultMsg.append(*name);
                                    faultMsg.append("' value is not a string");
                                    response.setFault(ConfigRPC::invalidType, faultMsg.data());
                                    status = XmlRpcMethod::FAILED;
                                 }
                              }
                              else
                              {
                                 UtlString faultMsg;
                                 faultMsg.append("parameter number ");
                                 char paramIndex[10];
                                 sprintf(paramIndex,"%zu", paramsSet + 1);
                                 faultMsg.append(paramIndex);
                                 faultMsg.append(" name is not a string");
                                 response.setFault(ConfigRPC::invalidType, faultMsg.data());
                                 status = XmlRpcMethod::FAILED;
                              }
                           }

                           if ( XmlRpcMethod::OK == status )
                           {
                              if (OS_SUCCESS == db->store(dataset))
                              {
                                 UtlInt numberSet(paramList->entries());
                                 response.setResponse(&numberSet);
                              }
                              else
                              {
                                 response.setFault( ConfigRPC::storeFailed
                                                   ,"error storing dataset"
                                                   );
                                 status = XmlRpcMethod::FAILED;
                              }
                           }
                        }
                        else
                        {
                           // The second parameter was not a list
                           response.setFault( ConfigRPC::invalidType
                                             ,"second parameter is not a struct"
                                             );
                           status = XmlRpcMethod::FAILED;
                        }
                     }
                     else // no parameter names specified
                     {
                        // No second parameter
                        response.setFault( ConfigRPC::invalidType
                                          ,"no second parameter of name/value pairs"
                                          );
                        status = XmlRpcMethod::FAILED;
                     }
                  }
                  else
                  {
                     UtlString faultMsg("dataset load failed");
                     response.setFault(ConfigRPC::loadFailed, faultMsg);
                     status = XmlRpcMethod::FAILED;
                  }
               }
               else
               {
                  UtlString faultMsg("Access Denied");
                  response.setFault(XmlRpcMethod::FAILED, faultMsg.data());
               }
            }
            else
            {
               UtlString faultMsg;
               faultMsg.append("db lookup failed for '");
               faultMsg.append(*dbName);
               faultMsg.append("'");
               response.setFault( XmlRpcResponse::UnregisteredMethod, faultMsg.data());
               status = XmlRpcMethod::FAILED;
            }
         }
         else
         {
            response.setFault( XmlRpcResponse::EmptyParameterValue
                              ,"'dbname' parameter is missing or invalid type"
                              );
            status = XmlRpcMethod::FAILED;
         }

         return true;
      }