/* ****************************************************************************
*
* parseAttributeValue - 
*/
std::string parseAttributeValue(ConnectionInfo* ciP, ContextAttribute* caP)
{
  Document    document;
  OrionError  oe;

  document.Parse(ciP->payload);

  if (document.HasParseError())
  {
    alarmMgr.badInput(clientIp, "JSON parse error");
    oe.reasonPhrase = ERROR_STRING_PARSERROR;
    oe.details      = "Errors found in incoming JSON buffer";
    ciP->httpStatusCode = SccBadRequest;;
    return oe.render(ciP, "");
  }


  if (!document.IsObject() && !document.IsArray())
  {
    alarmMgr.badInput(clientIp, "JSON parse error");
    oe.fill(SccBadRequest, "Neither JSON Object nor JSON Array for attribute::value");
    ciP->httpStatusCode = SccBadRequest;;

    return oe.render(ciP, "");
  }

  caP->valueType  = (document.IsObject())? orion::ValueTypeObject : orion::ValueTypeVector;
  parseContextAttributeCompoundValueStandAlone(document, caP, caP->valueType);

  return "OK";
}
/* ****************************************************************************
*
* getSubscription -
*
* GET /v2/subscription/<id>
*
*/
std::string getSubscription
(
  ConnectionInfo*            ciP,
  int                        components,
  std::vector<std::string>&  compV,
  ParseData*                 parseDataP
)
{
  ngsiv2::Subscription sub;
  std::string          idSub = compV[2];
  OrionError           oe;
  std::string          out;
  std::string          err;

  if ((err = idCheck(idSub)) != "OK")
  {
    OrionError oe(SccBadRequest, err);
    return oe.render(ciP, "Invalid subscription ID");
  }

  TIMED_MONGO(mongoGetSubscription(&sub, &oe, idSub, ciP->uriParam, ciP->tenant));

  if (oe.code != SccOk)
  {
    TIMED_RENDER(out = oe.render(ciP, "Invalid subscription ID"));
    return out;
  }

  TIMED_RENDER(out = sub.toJson());
  return out;
}
/* ****************************************************************************
*
* parseAttributeValue - 
*/
std::string parseAttributeValue(ConnectionInfo* ciP, ContextAttribute* caP)
{
  Document   document;
  OrionError oe;

  document.Parse(ciP->payload);

  if (document.HasParseError())
  {
    LM_W(("Bad Input (JSON parse error)"));
    oe.fill(SccBadRequest, "Errors found in incoming JSON buffer");
    ciP->httpStatusCode = SccBadRequest;;
    return oe.render(ciP, "");
  }


  if (!document.IsObject())
  {
    LM_E(("Bad Input (JSON Parse Error)"));
    oe.fill(SccBadRequest, "Error parsing incoming JSON buffer");
    ciP->httpStatusCode = SccBadRequest;;
    return oe.render(ciP, "");
  }


  if (!document.HasMember("value"))
  {
    LM_W(("Bad Input (No attribute value specified"));
    oe.fill(SccBadRequest, "no attribute value specified");
    ciP->httpStatusCode = SccBadRequest;;
    return oe.render(ciP, "");
  }


  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 != "value")
    {
      LM_W(("Bad Input (unexpected JSON field - accepting only 'value'"));
      oe.fill(SccBadRequest, "unexpected JSON field - accepting only /value/");
      ciP->httpStatusCode = SccBadRequest;;
      return oe.render(ciP, "");
    }


    if (type == "String")
    {
      caP->valueType   = orion::ValueTypeString;
      caP->stringValue = iter->value.GetString();
    }
    else if (type == "Number")
    {
      caP->numberValue  = iter->value.GetDouble();
      caP->valueType    = orion::ValueTypeNumber;
    }
    else if (type == "True")
    {
      caP->boolValue    = true;
      caP->valueType    = orion::ValueTypeBoolean;
    }
    else if (type == "False")
    {
      caP->boolValue    = false;
      caP->valueType    = orion::ValueTypeBoolean;
    }
    else if (type == "Array")
    {
      caP->valueType    = orion::ValueTypeVector;

      std::string r = parseContextAttributeCompoundValue(iter, caP, NULL);
      if (r != "OK")
      {
        LM_W(("Bad Input (json error in ContextAttributeObject::Vector"));
        return "json error in ContextAttributeObject::Vector";
      }
    }
    else if (type == "Object")
    {
      caP->valueType    = orion::ValueTypeObject;

      std::string r = parseContextAttributeCompoundValue(iter, caP, NULL);
      if (r != "OK")
      {
        LM_W(("Bad Input (json error in ContextAttributeObject::Object"));
        return "json error in ContextAttributeObject::Object";
      }
    }
  }

  return "OK";
}
Exemple #4
0
std::string deleteEntity
(
  ConnectionInfo*            ciP,
  int                        components,
  std::vector<std::string>&  compV,
  ParseData*                 parseDataP
)
{
  string  answer;
  Entity* eP    = new Entity();

  eP->id = compV[2];

  if (compV.size() == 5)  // Deleting an attribute
  {
    ContextAttribute *ca = new ContextAttribute;
    ca->name = compV[4];
    eP->attributeVector.push_back(ca);
  }
  // Fill in UpdateContextRequest
  parseDataP->upcr.res.fill(eP, "DELETE");

  // Call standard op postUpdateContext
  postUpdateContext(ciP, components, compV, parseDataP);

  // Any error in the response?
  UpdateContextResponse*  upcrsP = &parseDataP->upcrs.res;

  ciP->outFormat = JSON;

  for (unsigned int ix = 0; ix < upcrsP->contextElementResponseVector.size(); ++ix)
  {
    StatusCode      sc  = upcrsP->contextElementResponseVector[ix]->statusCode;
    HttpStatusCode  scc = sc.code;

    if ((scc != SccOk) && (scc != SccNone))
    {
      OrionError oe;

      ciP->httpStatusCode = scc;
      if (scc == SccContextElementNotFound)
      {
        oe.code = scc;
        oe.reasonPhrase ="NotFound";
        oe.details = "The requested entity has not been found. Check type and id";
      }
      else if (scc == SccInvalidParameter)
      {
        oe.code = SccContextElementNotFound;
        oe.reasonPhrase ="NotFound";
        oe.details = "Attribute not found";
        ciP->httpStatusCode = SccContextElementNotFound; // We don't want a 472
      }
      else
      {
        oe.code = scc;
        oe.reasonPhrase = sc.reasonPhrase;
      }
      answer = oe.render(ciP, "");

      eP->release();
      delete eP;
      return answer;
    }
  }

  // Prepare status code
  if ((ciP->httpStatusCode == SccOk) || (ciP->httpStatusCode == SccNone))
  {
    ciP->httpStatusCode = SccNoContent;
  }


  // Cleanup and return result
  eP->release();
  delete eP;

  return "";
}