Beispiel #1
0
int DOTCONFDocument::checkConfig(const std::list<DOTCONFDocumentNode*>::iterator & from)
{
    int ret = 0;

    DOTCONFDocumentNode * tagNode = NULL;
    int vi = 0;
    for(std::list<DOTCONFDocumentNode*>::iterator i = from; i != nodeTree.end(); ++i){
        tagNode = *i;
        if(!tagNode->closed){
            error(tagNode->lineNum, tagNode->fileName, "unclosed tag %s", tagNode->name);
            ret = -1;
            break;
        }
        vi = 0;
        while( vi < tagNode->valuesCount ){

            if(strstr(tagNode->values[vi], "${") && strchr(tagNode->values[vi], '}') ){
                ret = macroSubstitute(tagNode, vi );
                mempool->free();
                if(ret == -1){
                    break;
                }
            }
            ++vi;
        }
        if(ret == -1){
            break;
        }
    }

    return ret;
}
/* ****************************************************************************
*
* templateNotify - 
*
* This function performs the necessary substitutions according to the template of
* subscription to form the desired notification and send it to the endpoint specified
* in the subscription.
* 
* 
*/
static bool templateNotify
(
  const SubscriptionId&            subscriptionId,
  const ContextElement&            ce,
  const ngsiv2::HttpInfo&          httpInfo,
  const std::string&               tenant,
  const std::string&               xauthToken,
  const std::string&               fiwareCorrelator,
  RenderFormat                     renderFormat,
  const std::vector<std::string>&  attrsOrder
)
{
  Verb                                verb = httpInfo.verb;
  std::string                         method;
  std::string                         url;
  std::string                         payload;
  std::string                         mimeType;
  std::map<std::string, std::string>  qs;
  std::map<std::string, std::string>  headers;


  //
  // 1. Verb/Method
  //
  if (verb == NOVERB)
  {
    // Default verb/method is POST
    verb = POST;
  }
  method = verbName(verb);


  //
  // 2. URL
  //
  macroSubstitute(&url, httpInfo.url, ce);


  //
  // 3. Payload
  //
  if (httpInfo.payload == "")
  {
    NotifyContextRequest   ncr;
    ContextElementResponse cer;
    
    cer.contextElement = ce;
    ncr.subscriptionId = subscriptionId;
    ncr.contextElementResponseVector.push_back(&cer);
    payload  = ncr.toJson(renderFormat, attrsOrder);
    mimeType = "application/json";
  }
  else
  {
    macroSubstitute(&payload, httpInfo.payload, ce);
    char* pload  = curl_unescape(payload.c_str(), payload.length());
    payload      = pload;
    renderFormat = NGSI_V2_CUSTOM;
    mimeType     = "text/plain";  // May be overridden by 'Content-Type' in 'headers'
    free(pload);
  }


  //
  // 4. URI Params (Query Strings)
  //
  for (std::map<std::string, std::string>::const_iterator it = httpInfo.qs.begin(); it != httpInfo.qs.end(); ++it)
  {
    std::string key   = it->first;
    std::string value = it->second;

    macroSubstitute(&key,   it->first, ce);
    macroSubstitute(&value, it->second, ce);
    if ((value == "") || (key == ""))
    {
      // To avoid e.g '?a=&b=&c='
      continue;
    }
    qs[key] = value;
  }


  //
  // 5. HTTP Headers
  //
  for (std::map<std::string, std::string>::const_iterator it = httpInfo.headers.begin(); it != httpInfo.headers.end(); ++it)
  {
    std::string key   = it->first;
    std::string value = it->second;

    macroSubstitute(&key,   it->first, ce);
    macroSubstitute(&value, it->second, ce);

    if (key == "")
    {
      // To avoid empty header name
      continue;
    }

    headers[key] = value;
  }


  //
  // 6. Split URI in protocol, host, port and path
  //
  std::string  protocol;
  std::string  host;
  int          port;
  std::string  uriPath;

  if (!parseUrl(url, host, port, uriPath, protocol))
  {
    LM_E(("Runtime Error (not sending NotifyContextRequest: malformed URL: '%s')", httpInfo.url.c_str()));
    return false;
  }


  //
  // 7. Add URI params from template to uriPath
  //
  std::string  uri = uriPath;

  if (qs.size() != 0)
  {
    uri += "?";

    int ix = 0;
    for (std::map<std::string, std::string>::iterator it = qs.begin(); it != qs.end(); ++it)
    {
      if (ix != 0)
      {
        uri += "&";
      }

      uri += it->first + "=" + it->second;
      ++ix;
    }
  }


  //
  // 8. Send the request
  //
  //    NOTE: the HTTP headers are sent to httpRequestSend via parameter 'extraHeaders'
  //
  std::string  out;
  int          r;

  r = httpRequestSend(host,
                      port,
                      protocol,
                      method,
                      tenant,
                      ce.entityId.servicePath,
                      xauthToken,
                      uri,
                      mimeType,
                      payload,
                      fiwareCorrelator,
                      renderFormatToString(renderFormat),
                      true,                // Use Rush if CLI '--rush' allows it
                      true,                // wait for response
                      &out,
                      headers,
                      "application/json",  // Accept Format
                      -1);                 // Timeout in milliseconds, depends on CLI '-httpTimeout'

  if (r == 0)
  {
    statisticsUpdate(NotifyContextSent, JSON);
    alarmMgr.notificationErrorReset(url);
    return true;
  }

  return false;
}