Exemplo n.º 1
0
/* ****************************************************************************
*
* parseBatchUpdate - 
*/
std::string parseBatchUpdate(ConnectionInfo* ciP, BatchUpdate* burP)
{
  Document document;

  document.Parse(ciP->payload);

  if (document.HasParseError())
  {
    ErrorCode ec;

    alarmMgr.badInput(clientIp, "JSON Parse Error");
    ec.fill(ERROR_STRING_PARSERROR, "Errors found in incoming JSON buffer");
    ciP->httpStatusCode = SccBadRequest;

    return ec.toJson(true);
  }

  if (!document.IsObject())
  {
    ErrorCode ec;

    alarmMgr.badInput(clientIp, "JSON Parse Error");
    ec.fill("BadRequest", "JSON Parse Error");
    ciP->httpStatusCode = SccBadRequest;

    return ec.toJson(true);
  }
  else if (document.ObjectEmpty())
  {
    ErrorCode ec;

    alarmMgr.badInput(clientIp, "Empty JSON payload");
    ec.fill("BadRequest", "empty payload");
    ciP->httpStatusCode = SccBadRequest;

    return ec.toJson(true);
  }
  else if (!document.HasMember("entities"))
  {
    ErrorCode    ec;
    std::string  details = "Invalid JSON payload, mandatory field /entities/ not found";

    alarmMgr.badInput(clientIp, details);
    ec.fill("BadRequest", details);
    ciP->httpStatusCode = SccBadRequest;

    return ec.toJson(true);
  }
  else if (!document.HasMember("actionType"))
  {
    ErrorCode    ec;
    std::string  details = "Invalid JSON payload, mandatory field /actionType/ not found";

    alarmMgr.badInput(clientIp, details);
    ec.fill("BadRequest", details);
    ciP->httpStatusCode = SccBadRequest;

    return ec.toJson(true);
  }

  for (Value::ConstMemberIterator iter = document.MemberBegin(); iter != document.MemberEnd(); ++iter)
  {
    std::string name   = iter->name.GetString();
    std::string type   = jsonParseTypeNames[iter->value.GetType()];

    if (name == "entities")
    {
      std::string r = parseEntityVector(ciP, iter, &burP->entities, true); // param 4: attributes are allowed in payload

      if (r != "OK")
      {
        ErrorCode ec("BadRequest", r);

        alarmMgr.badInput(clientIp, r);
        ciP->httpStatusCode = SccBadRequest;
        return ec.toJson(true);
      }
    }
    else if (name == "actionType")
    {
      burP->updateActionType.set(iter->value.GetString());
    }
    else
    {
      std::string  description = std::string("Unrecognized field in JSON payload: /") + name + "/";
      ErrorCode    ec("BadRequest", description);

      alarmMgr.badInput(clientIp, description);
      ciP->httpStatusCode = SccBadRequest;

      return ec.toJson(true);
    }
  }

  return "OK";
}
Exemplo n.º 2
0
/* ****************************************************************************
*
* parseBatchQuery - 
*/
std::string parseBatchQuery(ConnectionInfo* ciP, BatchQuery* bqrP)
{
  Document document;

  document.Parse(ciP->payload);

  if (document.HasParseError())
  {
    ErrorCode ec;

    alarmMgr.badInput(clientIp, "JSON Parse Error");
    ec.fill(ERROR_STRING_PARSERROR, "Errors found in incoming JSON buffer");
    ciP->httpStatusCode = SccBadRequest;

    return ec.toJson(true);
  }

  if (!document.IsObject())
  {
    ErrorCode ec;

    alarmMgr.badInput(clientIp, "JSON Parse Error");
    ec.fill("BadRequest", "JSON Parse Error");
    ciP->httpStatusCode = SccBadRequest;

    return ec.toJson(true);
  }
  else if (document.ObjectEmpty())
  {
    ErrorCode ec;

    alarmMgr.badInput(clientIp, "Empty JSON payload");
    ec.fill("BadRequest", "empty payload");
    ciP->httpStatusCode = SccBadRequest;

    return ec.toJson(true);
  }
  else if (!document.HasMember("entities") && !document.HasMember("attributes") && !document.HasMember("scopes"))
  {
    ErrorCode ec;

    alarmMgr.badInput(clientIp, "Invalid JSON payload, no relevant fields found");
    ec.fill("BadRequest", "Invalid JSON payload, no relevant fields found");
    ciP->httpStatusCode = SccBadRequest;

    return ec.toJson(true);
  }

  for (Value::ConstMemberIterator iter = document.MemberBegin(); iter != document.MemberEnd(); ++iter)
  {
    std::string name   = iter->name.GetString();
    std::string type   = jsonParseTypeNames[iter->value.GetType()];

    if (name == "entities")
    {
      std::string r = parseEntityVector(ciP, iter, &bqrP->entities, false);  // param 4: attributes are NOT allowed in payload

      if (r != "OK")
      {
        ErrorCode ec("BadRequest", r);

        alarmMgr.badInput(clientIp, r);
        ciP->httpStatusCode = SccBadRequest;
        return ec.toJson(true);
      }
    }
    else if (name == "attributes")
    {
      std::string r = parseAttributeList(ciP, iter, &bqrP->attributeV);

      if (r != "OK")
      {
        ErrorCode ec("BadRequest", r);

        alarmMgr.badInput(clientIp, r);
        ciP->httpStatusCode = SccBadRequest;
        return ec.toJson(true);
      }
    }
    else if (name == "scopes")
    {
      std::string r = parseScopeVector(ciP, iter, &bqrP->scopeV);

      if (r != "OK")
      {
        ErrorCode ec("BadRequest", r);

        alarmMgr.badInput(clientIp, r);
        ciP->httpStatusCode = SccBadRequest;
        return ec.toJson(true);
      }
    }
    else
    {
      std::string  description = std::string("Unrecognizedfield in JSON payload: /") + name + "/";
      ErrorCode    ec("BadRequest", description);

      alarmMgr.badInput(clientIp, description);
      ciP->httpStatusCode = SccBadRequest;

      return ec.toJson(true);
    }
  }

  return "OK";
}