示例#1
0
/* Like 'search_file' */
onion_connection_status OnionServer::getSettingKinectWrapped(
		Onion::Request &req, Onion::Response &res)
{
	const char* path = onion_request_get_fullpath( req.c_handler() );
#ifdef VERBOSE
	printf("Request of %s.\n",path);
#endif
	std::string filename("./html/");
	filename.append(path);

	std::ifstream file(filename.c_str());
	std::string line;

	if( file.is_open()){

		/* Create header with mime type and charset information for several file extensions.
		 * This is just a workaround. There should be an automatic mechanicm
		 * in libonion. */
		int periodPos = filename.find_last_of('.');
		std::string extension = filename.substr(periodPos+1);
		std::string key("Content-Type");
		std::string defaultType("text/html; charset: utf-8");

		std::string mime = m_mimedict.get( extension , defaultType ) ;
		res.setHeader(key,mime);
		onion_response_write_headers(res.c_handler());// missing in cpp bindings?
		//res.writeHeaders();//this was added by me...

		try{
			/*
			res.write("json_kinect = ", 14);
			const char* kinect = m_settingKinect.getConfig(true);
			size_t len = strlen( kinect );
			res.write(kinect, (int) len );
			res.write(";\n", 2 );
			*/

			while (std::getline(file, line)) {
				res.write( line.c_str(), line.size() );
				res.write("\n", 1 );
			}
		}//catch ( const boost::iobase::failure &ex )
		catch ( const std::exception & ex ){
			std::cerr << "Can not read " << filename << std::endl;
			res.write( ErrReadFailed.c_str(), ErrReadFailed.size());
		}
	}else{
		res.write( ErrNotFound.c_str(), ErrNotFound.size());
	}

	return OCS_PROCESSED;
}
/* ****************************************************************************
*
* parseContextAttributeCompoundValueStandAlone - 
*/
std::string parseContextAttributeCompoundValueStandAlone
(
  Document&            document,
  ContextAttribute*    caP,
  orion::ValueType     valueType
)
{
  caP->compoundValueP            = new orion::CompoundValueNode();
  caP->compoundValueP->name      = "TOP";
  caP->compoundValueP->container = caP->compoundValueP;
  caP->compoundValueP->valueType = caP->valueType;  // Convert to other type?
  caP->compoundValueP->path      = "/";
  caP->compoundValueP->rootP     = caP->compoundValueP;
  caP->compoundValueP->level     = 0;
  caP->compoundValueP->siblingNo = 0;

  orion::CompoundValueNode*   parent  = caP->compoundValueP;


  //
  // Children of the node
  //
  if (caP->valueType == orion::ValueTypeVector)
  {
    int counter  = 0;

    for (Value::ConstValueIterator iter = document.Begin(); iter != document.End(); ++iter)
    {
      std::string                nodeType  = jsonParseTypeNames[iter->GetType()];
      orion::CompoundValueNode*  cvnP      = new orion::CompoundValueNode();
      char                       itemNo[4];

      snprintf(itemNo, sizeof(itemNo), "%03d", counter);

      cvnP->valueType  = stringToCompoundType(nodeType);
      cvnP->container  = parent;
      cvnP->rootP      = parent->rootP;
      cvnP->level      = parent->level + 1;
      cvnP->siblingNo  = counter;
      cvnP->path       = parent->path + "[" + itemNo + "]";


      if (nodeType == "String")
      {
        cvnP->stringValue = iter->GetString();
      }
      else if (nodeType == "Number")
      {
        cvnP->numberValue = iter->GetDouble();
      }
      else if ((nodeType == "True") || (nodeType == "False"))
      {
        cvnP->boolValue   = (nodeType == "True")? true : false;
      }
      else if (nodeType == "Null")
      {
        cvnP->valueType = orion::ValueTypeNone;
      }
      else if (nodeType == "Object")
      {
        cvnP->path += "/";
        cvnP->valueType = orion::ValueTypeObject;
      }
      else if (nodeType == "Array")
      {
        cvnP->path += "/";
        cvnP->valueType = orion::ValueTypeVector;
      }

      parent->childV.push_back(cvnP);

      //
      // Start recursive calls if Object or Array
      //
      if ((nodeType == "Object") || (nodeType == "Array"))
      {
        parseContextAttributeCompoundValue(iter, caP, cvnP);
      }
      else if (!caP->typeGiven)
      {
        caP->type = defaultType(caP->valueType);
      }

      ++counter;
    }
  }
  else if (caP->valueType == orion::ValueTypeObject)
  {
    int counter  = 0;

    for (Value::ConstMemberIterator iter = document.MemberBegin(); iter != document.MemberEnd(); ++iter)
    {
      std::string                nodeType = jsonParseTypeNames[iter->value.GetType()];
      orion::CompoundValueNode*  cvnP     = new orion::CompoundValueNode();

      cvnP->name       = iter->name.GetString();
      cvnP->valueType  = stringToCompoundType(nodeType);
      cvnP->container  = parent;
      cvnP->rootP      = parent->rootP;
      cvnP->level      = parent->level + 1;
      cvnP->siblingNo  = counter;
      cvnP->path       = parent->path + cvnP->name;

      if (nodeType == "String")
      {
        cvnP->stringValue = iter->value.GetString();
      }
      else if (nodeType == "Number")
      {
        cvnP->numberValue = iter->value.GetDouble();
      }
      else if ((nodeType == "True") || (nodeType == "False"))
      {
        cvnP->boolValue   = (nodeType == "True")? true : false;
      }
      else if (nodeType == "Null")
      {
        cvnP->valueType = orion::ValueTypeNone;
      }
      else if (nodeType == "Object")
      {
        cvnP->path += "/";
        cvnP->valueType = orion::ValueTypeObject;
      }
      else if (nodeType == "Array")
      {
        cvnP->path += "/";
        cvnP->valueType = orion::ValueTypeVector;
      }

      parent->childV.push_back(cvnP);
        
      //
      // Start recursive calls if Object or Array
      //
      if ((nodeType == "Object") || (nodeType == "Array"))
      {
        parseContextAttributeCompoundValue(iter, caP, cvnP);
      }
      else if (!caP->typeGiven)
      {
        caP->type = defaultType(caP->valueType);
      }

      ++counter;
    }
  }

  return "OK";
}
示例#3
0
/* ****************************************************************************
*
* parseMetadataObject - 
*/
static std::string parseMetadataObject(const Value& start, Metadata* mdP)
{
  bool  compoundVector = false;

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

    if (name == "type")
    {
      if (type != "String")
      {
        alarmMgr.badInput(clientIp, "ContextAttribute::Metadata::type must be a String");
        return "invalid JSON type for attribute metadata type";
      }

      mdP->type      = iter->value.GetString();
      mdP->typeGiven = true;
    }
    else if (name == "value")
    {
      if (type == "String")
      {
        mdP->stringValue   = iter->value.GetString();
        mdP->valueType     = orion::ValueTypeString;
      }
      else if (type == "Number")
      {
        mdP->valueType     = orion::ValueTypeNumber;
        mdP->numberValue   = iter->value.GetDouble();
      }
      else if (type == "True")
      {
        mdP->valueType     = orion::ValueTypeBoolean;
        mdP->boolValue     = true;
      }
      else if (type == "False")
      {
        mdP->valueType     = orion::ValueTypeBoolean;
        mdP->boolValue     = false;
      }
      else if (type == "Null")
      {
        mdP->valueType     = orion::ValueTypeNone;
      }
      else if ((type == "Array") || (type == "Object"))
      {
        compoundVector = (type == "Array")? true : false;
        mdP->valueType = orion::ValueTypeObject;  // Used both for Array and Object ...
        std::string r  = parseMetadataCompoundValue(iter, mdP, NULL);

        if (r != "OK")
        {
          alarmMgr.badInput(clientIp, "json parse error in Metadata compound value");
          return "json parse error in Metadata compound value";
        }
      }
      else
      {
        std::string details = std::string("ContextAttribute::Metadata::type is '") + type + "'";
        alarmMgr.badInput(clientIp, details);
        return "invalid JSON type for attribute metadata value";
      }
    }
    else
    {
      alarmMgr.badInput(clientIp, "invalid JSON field for attribute metadata");
      return "invalid JSON field for attribute metadata";
    }
  }

  // Is it a date?
  if (mdP->type == DATE_TYPE)
  {
    mdP->numberValue =  parse8601Time(mdP->stringValue);

    if (mdP->numberValue == -1)
    {
      alarmMgr.badInput(clientIp, "date has invalid format");
      return "date has invalid format";
    }

    // Probably reseting stringValue is not needed, but let's do it for cleanliness
    mdP->stringValue = "";
    mdP->valueType   = orion::ValueTypeNumber;
  }

  if (!mdP->typeGiven)
  {
    mdP->type = (compoundVector)? defaultType(orion::ValueTypeVector) : defaultType(mdP->valueType);
  }

  return "OK";
}