Пример #1
0
bool FilterHandler::handle(HttpRequest* req, HttpResponse* res, const string& ext, Reflector& reflector)
{
	string acurl = req->getActUrl();
	RegexUtil::replace(acurl,"[/]+","/");
	if(acurl.find("/"+req->getCntxt_name())!=0)
		acurl = "/" + req->getCntxt_name() + "/" + acurl;
	RegexUtil::replace(acurl,"[/]+","/");

	bool continue_proc_request = true;
	Logger logger = LoggerFactory::getLogger("FilterHandler");
	vector<string> filters;
	if(getFilterForPath(req->getCntxt_name(), acurl, filters, "handle"))
	{
		for (int var = 0; var < (int)filters.size(); ++var)
		{
			string claz = filters.at(var);
			void *_temp = ConfigurationData::getInstance()->ffeadContext.getBean("filter_"+claz, req->getCntxt_name());
			args argus;
			argus.push_back("HttpRequest*");
			argus.push_back("HttpResponse*");
			vals valus;
			const ClassInfo& srv = ConfigurationData::getInstance()->ffeadContext.classInfoMap[req->getCntxt_name()][claz];
			Method meth = srv.getMethod("doHandle", argus);
			if(meth.getMethodName()!="")
			{
				valus.push_back(req);
				valus.push_back(res);
				continue_proc_request = reflector.invokeMethod<bool>(_temp,meth,valus);
				logger << "Handler Filter called" << endl;
			}
			ConfigurationData::getInstance()->ffeadContext.release("filter_"+claz, req->getCntxt_name());
		}
	}
	return continue_proc_request;
}
Пример #2
0
void ServiceTask::handleWebsockClose(WebSocketData* req) {

	Reflector reflector(ConfigurationData::getInstance()->dlib);

	std::string className;
	std::map<std::string, std::map<std::string, std::string> >& websocketMappingMap = ConfigurationData::getInstance()->websocketMappingMap;
	std::map<std::string, std::string> websockcntMap = websocketMappingMap[req->getCntxt_name()];
	std::map<std::string, std::string>::iterator it;
	for (it=websockcntMap.begin();it!=websockcntMap.end();++it) {
		if(ConfigurationData::urlMatchesPath(req->getCntxt_name(), it->first, req->getUrl()))
		{
			className = it->second;
			break;
		}
	}
	if(className!="")
	{
		void *_temp = ConfigurationData::getInstance()->ffeadContext.getBean("websocketclass_"+className, req->getCntxt_name());
		args argus;
		vals valus;
		const ClassInfo& srv = ConfigurationData::getClassInfo(className, req->getCntxt_name());
		Method methc = srv.getMethod("onClose", argus);
		if(methc.getMethodName()!="")
		{
			 //logger << ("WebSocket Controller " + className + " called") << std::endl;
			 reflector.invokeMethodGVP(_temp,methc,valus);
			 logger << "WebSocket Controller onClose" << std::endl;
		}
		else
		{
			logger << "Invalid WebSocket Controller" << std::endl;
		}
	}
}
Пример #3
0
void Class::addMethod(Method meth)
{
	string key = meth.getMethodName();
	for (int var = 0; var < (int)meth.getArgumentTypes().size(); ++var)
	{
		key += (meth.getArgumentTypes().at(var));
	}
	this->methods[key] = meth;
}
Пример #4
0
bool ControllerHandler::handle(HttpRequest* req, HttpResponse& res, map<string, string> urlpattMap, map<string, string> mappattMap, void* dlib,
		string ext, resFuncMap rstCntMap, map<string, string> mapMap, map<string, string> urlMap, string pthwofile)
{
	Logger logger = Logger::getLogger("ControllerHandler");
	string claz;
	bool isContrl = false;
	if((urlpattMap[req->getCntxt_name()+"*.*"]!="" || urlMap[req->getCntxt_name()+ext]!=""))
	{
		//logger << "Controller requested for " << req->getCntxt_name() << " name " << urlMap[req->getCntxt_name()+ext] << endl;
		if(urlpattMap[req->getCntxt_name()+"*.*"]!="")
			claz = "getReflectionCIFor" + urlpattMap[req->getCntxt_name()+"*.*"];
		else
			claz = "getReflectionCIFor" + urlMap[req->getCntxt_name()+ext];
		string libName = Constants::INTER_LIB_FILE;
		if(dlib == NULL)
		{
			cerr << dlerror() << endl;
			exit(-1);
		}
		void *mkr = dlsym(dlib, claz.c_str());
		if(mkr!=NULL)
		{
			FunPtr f =  (FunPtr)mkr;
			ClassInfo srv = f();
			args argus;
			Constructor ctor = srv.getConstructor(argus);
			Reflector ref;
			void *_temp = ref.newInstanceGVP(ctor);
			Controller *thrd = (Controller *)_temp;
			try{
				 logger << "Controller called" << endl;
				 res = thrd->service(*req);
				 logger << res.getStatusCode() << endl;
				 logger << res.getContent_type() << endl;
				 logger << res.getContent_len() << endl;
				 if(res.getStatusCode()!="")
					 isContrl = true;
				 ext = AuthHandler::getFileExtension(req->getUrl());
				 //delete mkr;
			}catch(...){ logger << "Controller exception" << endl;}
			logger << "Controller called\n" << flush;
		}
	}
	else if((mappattMap[req->getCntxt_name()+"*.*"]!="" || mapMap[req->getCntxt_name()+ext]!=""))
	{
		string file = req->getFile();
		string fili = file.substr(0,file.find_last_of("."));
		if(mappattMap[req->getCntxt_name()+"*.*"]!="")
		{
			req->setFile(fili+mappattMap[req->getCntxt_name()+"*.*"]);
			logger << "URL mapped from * to " << mappattMap[req->getCntxt_name()+"*.*"] << endl;
		}
		else
		{
			req->setFile(fili+mapMap[req->getCntxt_name()+ext]);
			logger << "URL mapped from " << ext << " to " << mapMap[req->getCntxt_name()+ext] << endl;
		}
	}
	else
	{
		resFuncMap::iterator it;
		RestFunction rft;
		bool flag = false;
		int prsiz = 0;
		vector<string> valss;
		//logger << pthwofile << endl;
		for (it=rstCntMap.begin();it!=rstCntMap.end();it++)
		{
			valss.clear();
			//logger << it->first << endl;
			//if(pthwofile.find(it->first)!=string::npos)
			{
				RestFunction ft = it->second;
				prsiz = ft.params.size();
				string pthwofiletemp(pthwofile);
				if(ft.baseUrl=="")
				{
					logger << "checking url : " << pthwofiletemp << ",param size: " << prsiz <<
							", against url: " << it->first << endl;
					for (int var = 0; var < prsiz; var++)
					{
						//logger << "loop - " << pthwofiletemp << endl;
						string valsvv(pthwofiletemp.substr(pthwofiletemp.find_last_of("/")+1));
						pthwofiletemp = pthwofiletemp.substr(0, pthwofiletemp.find_last_of("/"));
						valss.push_back(valsvv);
					}
					reverse(valss.begin(),valss.end());
					//logger << "after - " << pthwofiletemp << endl;
					/*if(pthwofiletemp.at(pthwofiletemp.length()-1)=='/')
					{
						pthwofiletemp = pthwofiletemp.substr(0, pthwofiletemp.length()-1);
					}*/
					//logger << "after - " << pthwofiletemp << endl;
					logger << "checking url : " << pthwofiletemp << ",param size: " << prsiz << ",vals: " << valss.size() <<
							", against url: " << it->first << endl;
					if(it->first==pthwofiletemp)
					{
						string lhs = StringUtil::toUpperCopy(ft.meth);
						string rhs = StringUtil::toUpperCopy(req->getMethod());
						logger << lhs << " <> " << rhs << endl;
						if(prsiz==(int)valss.size() && lhs==rhs)
						{
							logger << "got correct url -- restcontroller " << endl;
							rft = ft;
							flag = true;
						}
						else
						{
							res.setStatusCode("404");
							res.setStatusMsg("Not Found");
							//res.setContent_type("text/plain");
							/*if(prsiz==valss.size())
								res.setContent_str("Invalid number of arguments");
							else
								res.setContent_str("Invalid HTTPMethod used");*/
							logger << "Rest Controller Param/Method Error" << endl;
						}
						break;
					}
				}
				else
				{
					string baseUrl(req->getCntxt_name()+ft.baseUrl);
					logger << "checking url : " << pthwofiletemp << ",param size: " << prsiz <<
							", against url: " << baseUrl << endl;
					for (int var = 1; var <= prsiz; var++)
					{
						strVec vemp;
						stringstream ss;
						ss << "{";
						ss << var;
						ss << "}";
						string param;
						ss >> param;
						StringUtil::split(vemp, baseUrl, (param));
						if(vemp.size()==2 && pthwofiletemp.find(vemp.at(0))!=string::npos)
						{
							string temp = pthwofiletemp;
							StringUtil::replaceFirst(temp, vemp.at(0), "");
							if(temp.find("/")!=string::npos)
							{
								pthwofiletemp = temp.substr(temp.find("/"));
								temp = temp.substr(0, temp.find("/"));
							}
							valss.push_back(temp);
							baseUrl = vemp.at(1);
							logger << "variable at " << param << " mapped to " << temp << " from URL" << endl;
							logger << baseUrl << endl;
							logger << pthwofiletemp << endl;
						}
						else
						{
							flag = false;
							break;
						}
					}
					string lhs = StringUtil::toUpperCopy(ft.meth);
					string rhs = StringUtil::toUpperCopy(req->getMethod());
					logger << lhs << " <> " << rhs << endl;
					if(prsiz==(int)valss.size() && lhs==rhs)
					{

						logger << "got correct url -- restcontroller " << endl;
						rft = ft;
						flag = true;
						break;
					}
					else
					{
						res.setStatusCode("404");
						res.setStatusMsg("Not Found");
						//res.setContent_type("text/plain");
						/*if(prsiz==valss.size())
							res.setContent_str("Invalid number of arguments");
						else
							res.setContent_str("Invalid HTTPMethod used");*/
						logger << "Rest Controller Param/Method Error" << endl;
					}
				}
			}
		}
		if(flag)
		{
			//logger << "inside restcontroller logic ..." << endl;
			string libName = Constants::INTER_LIB_FILE;
			if(dlib == NULL)
			{
				cerr << dlerror() << endl;
				exit(-1);
			}
			string clasnam("getReflectionCIFor"+rft.clas);
			void *mkr = dlsym(dlib, clasnam.c_str());
			logger << mkr << endl;
			if(mkr!=NULL)
			{
				FunPtr f =  (FunPtr)mkr;
				ClassInfo srv = f();
				args argus;
				Constructor ctor = srv.getConstructor(argus);
				Reflector ref;
				void *_temp = ref.newInstanceGVP(ctor);
				RestController* rstcnt = (RestController*)_temp;
				rstcnt->request = req;
				rstcnt->response = &res;

				vals valus;
				bool invValue = false;
				for (int var = 0; var < prsiz; var++)
				{
					try
					{
						argus.push_back(rft.params.at(var).type);
						if(rft.params.at(var).type=="int")
						{
							int* ival = new int(CastUtil::lexical_cast<int>(valss.at(var)));
							valus.push_back(ival);
						}
						else if(rft.params.at(var).type=="long")
						{
							long* ival = new long(CastUtil::lexical_cast<long>(valss.at(var)));
							valus.push_back(ival);
						}
						else if(rft.params.at(var).type=="double")
						{
							double* ival = new double(CastUtil::lexical_cast<double>(valss.at(var)));
							valus.push_back(ival);
						}
						else if(rft.params.at(var).type=="float")
						{
							float* ival = new float(CastUtil::lexical_cast<float>(valss.at(var)));
							valus.push_back(ival);
						}
						else if(rft.params.at(var).type=="bool")
						{
							bool* ival = new bool(CastUtil::lexical_cast<bool>(valss.at(var)));
							valus.push_back(ival);
						}
						else if(rft.params.at(var).type=="string")
						{
							string* sval = new string(valss.at(var));
							valus.push_back(sval);
						}
					} catch (...) {
						invValue= true;
						break;
					}
				}

				Method meth = srv.getMethod(rft.name, argus);
				if(meth.getMethodName()!="" && !invValue)
				{
					ref.invokeMethodUnknownReturn(_temp,meth,valus);
					logger << "successfully called restcontroller" << endl;
					//return;
				}
				else
				{
					res.setStatusCode("404");
					res.setStatusMsg("Not Found");
					//res.setContent_type("text/plain");
					/*if(invValue)
						res.setContent_str("Invalid value passed as URL param");
					else
						res.setContent_str("Rest Controller Method Not Found");*/
					logger << "Rest Controller Method Not Found" << endl;
					//return;
				}
			}
		}
	}
	return isContrl;
}
Пример #5
0
void* ComponentHandler::service(void* arg)
{
	int fd = *(int*)arg;
	init();
	string methInfo,retValue;
	_cmp_instance->getServer()->Receive(fd,methInfo,1024);
	methInfo =methInfo.substr(0,methInfo.find_last_of(">")+1);
	_cmp_instance->logger << methInfo << flush;
	try
	{
		XmlParser parser("Parser");
		_cmp_instance->logger << "\nBean call parsed successfully\n" << flush;
		if(methInfo.find("lang=\"c++\"")!=string::npos || methInfo.find("lang='c++'")!=string::npos)
		{
			Document doc = parser.getDocument(methInfo);
			Element message = doc.getRootElement();
			if(message.getTagName()!="service")
			{
				throw new ComponentHandlerException("No service Tag\n",retValue);
			}
			if(message.getAttributes().size()<4)
			{
				throw new ComponentHandlerException("name,beanName,returnType and lang are mandatory attributes\n",retValue);
			}
			else if(message.getAttribute("name")=="")
			{
				throw new ComponentHandlerException("name attribute missing\n",retValue);
			}
			else if(message.getAttribute("returnType")=="")
			{
				throw new ComponentHandlerException("returnType attribute missing\n",retValue);
			}
			else if(message.getAttribute("beanName")=="")
			{
				throw new ComponentHandlerException("beanName attribute missing\n",retValue);
			}
			else if(message.getAttribute("lang")=="")
			{
				throw new ComponentHandlerException("lang attribute missing\n",retValue);
			}
			if(message.getChildElements().size()!=1)
			{
				throw new ComponentHandlerException("message tag should have only one child tag\n",retValue);
			}
			else if(message.getChildElements().at(0).getTagName()!="args")
			{
				throw new ComponentHandlerException("message tag should have an args child tag\n",retValue);
			}
			Serialize ser;
			Reflector reflector;
			args argus;
			vals valus;
			ElementList argts = message.getChildElements().at(0).getChildElements();
			for (unsigned var = 0; var < argts.size();  var++)
			{
				void *value = NULL;
				Element arg = argts.at(var);
				if(arg.getTagName()!="argument")
					throw new ComponentHandlerException("Invalid Tag, only argument tag allowed\n",retValue);
				else if(arg.getAttribute("type")=="")
					throw new ComponentHandlerException("every argument tag should have a type attribute\n",retValue);
				if(arg.getText()=="" && arg.getChildElements().size()==0)
					throw new ComponentHandlerException("argument value missing\n",retValue);
				if(arg.getAttribute("type")!="")
				{
					Element obj = arg.getChildElements().at(0);
					string objxml = obj.render();
					string objClassName = obj.getTagName();
					value = ser.unSerializeUnknown(objxml,arg.getAttribute("type"));
				}
				argus.push_back(arg.getAttribute("type"));
				valus.push_back(value);
			}
			string className = "Component_"+message.getAttribute("beanName");
			_cmp_instance->logger << "\nBean class = " << className << "\n" << flush;
			string returnType = message.getAttribute("returnType");
			string lang = message.getAttribute("lang");
			ClassInfo clas = reflector.getClassInfo(className);
			string methodName = message.getAttribute("name");
			_cmp_instance->logger << "\nBean service = " << methodName << "\n" << flush;
			if(clas.getClassName()=="")
			{
				throw new ComponentHandlerException("bean does not exist or is not regsitered\n",retValue);
			}
			Method meth = clas.getMethod(methodName,argus);
			if(meth.getMethodName()=="")
			{
				throw new ComponentHandlerException("service does not exist for the bean or the bean does not exist or is not regsitered\n\n",retValue);
			}
			else
			{
				_cmp_instance->logger << "\nGot Bean service " << methodName << "\n" << flush;
				args argus;
				Constructor ctor = clas.getConstructor(argus);
				void *_temp = reflector.newInstanceGVP(ctor);
				_cmp_instance->logger << "\nGot Bean " << _temp << "\n" << flush;
				if(returnType=="void" || returnType=="")
				{
					_cmp_instance->logger << "\nVoid return " << "\n" << flush;
					reflector.invokeMethod<void*>(_temp,meth,valus);
					retValue = ("<return:void></return:void>");
				}
				else
				{
					_cmp_instance->logger << "\nReturn type = "<< returnType << "\n" << flush;
					if(returnType=="int")
					{
						int retv = reflector.invokeMethod<int>(_temp,meth,valus);
						retValue = ("<return:int>"+CastUtil::lexical_cast<string>(retv)+"</return:int>");
					}
					else if(returnType=="float")
					{
						float retv = reflector.invokeMethod<float>(_temp,meth,valus);
						retValue = ("<return:float>"+CastUtil::lexical_cast<string>(retv)+"</return:float>");
					}
					else if(returnType=="double")
					{
						double retv = reflector.invokeMethod<double>(_temp,meth,valus);
						retValue = ("<return:double>"+CastUtil::lexical_cast<string>(retv)+"</return:double>");
					}
					else if(returnType=="string")
					{
						string retv = reflector.invokeMethod<string>(_temp,meth,valus);
						retValue = ("<return:string>"+retv+"</return:string>");
					}
					else if(returnType!="")
					{
						void* retobj = reflector.invokeMethodUnknownReturn(_temp,meth,valus);
						string oxml = ser.serializeUnknown(retobj,returnType);
						retValue = ("<return:"+returnType+">"+oxml+"</return:"+returnType+">");
					}
				}

			}
		}
		else
		{
			retValue = "<return:exception>This is a C++ daemon</return:exception>";
		}
		_cmp_instance->logger << "\nSending data = "<< retValue << "\n" << flush;
		if(retValue!="")
			_cmp_instance->getServer()->Send(fd,retValue);
		//close(fd);
	}
	catch(ComponentHandlerException *e)
	{
		_cmp_instance->logger << e->getMessage() << flush;
		_cmp_instance->getServer()->Send(fd,retValue);
		close(fd);
	}
	catch(...)
	{
		_cmp_instance->logger << "exception occurred" << flush;
		retValue = ("<return:exception>XmlParseException occurred</return:exception>");
		_cmp_instance->getServer()->Send(fd,retValue);
		close(fd);
	}
}
Пример #6
0
bool ControllerHandler::handle(HttpRequest* req, HttpResponse& res, ConfigurationData configData,
		string ext, string pthwofile)
{
	map<string, string> urlpattMap = configData.urlpattMap;
	map<string, string> mappattMap = configData.mappattMap;
	resFuncMap rstCntMap = configData.rstCntMap;
	map<string, string> mapMap = configData.mapMap;
	map<string, string> urlMap = configData.urlMap;

	Logger logger = LoggerFactory::getLogger("ControllerHandler");
	bool isContrl = false;
	if((urlpattMap[req->getCntxt_name()+"*.*"]!="" || urlMap[req->getCntxt_name()+ext]!=""))
	{
		//logger << "Controller requested for " << req->getCntxt_name() << " name " << urlMap[req->getCntxt_name()+ext] << endl;
		string controller;
		if(urlpattMap[req->getCntxt_name()+"*.*"]!="")
			controller = urlpattMap[req->getCntxt_name()+"*.*"];
		else
			controller = urlMap[req->getCntxt_name()+ext];

		void *_temp = configData.ffeadContext->getBean("controller_"+req->getCntxt_name()+controller, req->getCntxt_name());
		Controller* thrd = static_cast<Controller*>(_temp);
		if(thrd!=NULL)
		{
			try{
				 logger << ("Controller " + controller + " called") << endl;
				 res = thrd->service(*req);
				 if(res.getStatusCode()!="")
					 isContrl = true;
				 ext = AuthHandler::getFileExtension(req->getUrl());
				 //delete mkr;
			}catch(...){
				logger << "Controller Exception occurred" << endl;
			}
			logger << "Controller call complete" << endl;
		}
		else
		{
			logger << "Invalid Controller" << endl;
			res.setHTTPResponseStatus(HTTPResponseStatus::InternalServerError);
			isContrl = true;
		}

	}
	else if((mappattMap[req->getCntxt_name()+"*.*"]!="" || mapMap[req->getCntxt_name()+ext]!=""))
	{
		string file = req->getFile();
		string fili = file.substr(0,file.find_last_of("."));
		if(mappattMap[req->getCntxt_name()+"*.*"]!="")
		{
			req->setFile(fili+mappattMap[req->getCntxt_name()+"*.*"]);
			logger << ("URL mapped from * to " + mappattMap[req->getCntxt_name()+"*.*"]) << endl;
		}
		else
		{
			req->setFile(fili+mapMap[req->getCntxt_name()+ext]);
			logger << ("URL mapped from " + ext + " to " + mapMap[req->getCntxt_name()+ext]) << endl;
		}
	}
	else
	{
		resFuncMap::iterator it;
		RestFunction rft;
		bool flag = false;
		int prsiz = 0;
		vector<string> valss;
		map<string, string> mapOfValues;
		//logger << pthwofile << endl;
		for (it=rstCntMap.begin();it!=rstCntMap.end();it++)
		{
			valss.clear();
			//logger << it->first << endl;
			//if(pthwofile.find(it->first)!=string::npos)
			{
				RestFunction ft = it->second;
				prsiz = ft.params.size();
				string pthwofiletemp(pthwofile);

				string baseUrl(it->first);
				strVec resturlparts;
				StringUtil::split(resturlparts, baseUrl, "/");

				strVec urlparts;
				StringUtil::split(urlparts, pthwofiletemp, "/");

				if(urlparts.size()!=resturlparts.size())
				{
					flag = false;
					//break;
				}
				else
				{
					flag = true;
				}
				if(flag)
				{
					bool fflag = true;
					for (int var = 0; var < (int)resturlparts.size(); var++)
					{
						//logger << "resturlparts.at(var) = " << resturlparts.at(var) << endl;
						if(resturlparts.at(var).find("{")!=string::npos && resturlparts.at(var).find("}")!=string::npos
								&& resturlparts.at(var).length()>2)
						{
							string paramname = resturlparts.at(var);
							string pref, suff;
							int st = paramname.find("{")+1;
							pref = paramname.substr(0, st-1);
							int len = paramname.find("}") - st;
							suff = paramname.substr(paramname.find("}")+1);
							paramname = paramname.substr(st, len);
							string paramvalue = urlparts.at(var);
							if(st>1)
							{
								int stpre = paramvalue.find(pref) + pref.length();
								int len = paramvalue.length() - pref.length() - suff.length();
								paramvalue = paramvalue.substr(stpre, len);
							}
							mapOfValues[paramname] = paramvalue;
							//logger << "mapOfValues(" << paramname << ") = "<< paramvalue << endl;
							logger << ("Restcontroller matched url : " + pthwofiletemp + ",param size: " + CastUtil::lexical_cast<string>(prsiz) +
										", against url: " + baseUrl) << endl;
						}
						else if(urlparts.at(var)!=resturlparts.at(var))
						{
							fflag = false;
							break;
						}
					}
					flag = fflag;
				}

				string lhs = StringUtil::toUpperCopy(ft.meth);
				string rhs = StringUtil::toUpperCopy(req->getMethod());
				//if(prsiz==(int)valss.size() && lhs==rhs)
				if(flag && lhs==rhs)
				{

					logger << "Encountered rest controller url/method match" << endl;
					rft = ft;
					flag = true;
					break;
				}
				else if(flag)
				{
					res.setHTTPResponseStatus(HTTPResponseStatus::InvalidMethod);
					return true;
				}
				else
				{
					res.setHTTPResponseStatus(HTTPResponseStatus::NotFound);
					//res.addHeaderValue(HttpResponse::ContentType, ContentTypes::CONTENT_TYPE_TEXT_PLAIN);
					/*if(prsiz==valss.size())
						res.setContent("Invalid number of arguments");
					else
						res.setContent("Invalid HTTPMethod used");*/
					//logger << "Rest Controller Param/Method Error" << endl;
				}
			}
		}
		if(flag)
		{
			//logger << "inside restcontroller logic ..." << endl;
			Reflector ref;
			ClassInfo srv = ref.getClassInfo(rft.clas, req->getCntxt_name());
			void *_temp = configData.ffeadContext->getBean("restcontroller_"+req->getCntxt_name()+rft.clas, req->getCntxt_name());
			RestController* rstcnt = (RestController*)_temp;
			if(rstcnt==NULL)
			{
				logger << "Invalid Rest Controller" << endl;
				res.setHTTPResponseStatus(HTTPResponseStatus::InternalServerError);
				return true;
			}

			rstcnt->request = req;
			rstcnt->response = &res;

			args argus;
			vals valus;
			bool invValue = false;
			vector<ifstream*> allStreams;
			map<string, vector<ifstream*>* > mpvecstreams;
			for (int var = 0; var < prsiz; var++)
			{
				try
				{
					string icont = rft.icontentType;
					string ocont = rft.ocontentType;

					if(icont=="")
						icont = ContentTypes::CONTENT_TYPE_APPLICATION_JSON;
					else if(icont!=req->getHeader(HttpRequest::ContentType) && req->getHeader(HttpRequest::ContentType).find(icont)!=0)
					{
						res.setHTTPResponseStatus(HTTPResponseStatus::UnsupportedMedia);
						return true;
					}

					if(ocont=="")
						ocont = ContentTypes::CONTENT_TYPE_APPLICATION_JSON;

					req->addHeaderValue(HttpRequest::ContentType, icont);
					res.addHeaderValue(HttpResponse::ContentType, ocont);

					string pmvalue;
					if(rft.params.at(var).from=="path")
						pmvalue = mapOfValues[rft.params.at(var).name];
					else if(rft.params.at(var).from=="reqparam")
						pmvalue = req->getQueryParam(rft.params.at(var).name);
					else if(rft.params.at(var).from=="postparam")
						pmvalue = req->getRequestParam(rft.params.at(var).name);
					else if(rft.params.at(var).from=="header")
						pmvalue = req->getHeader(rft.params.at(var).name);
					else if(rft.params.at(var).from=="multipart-content")
					{
						MultipartContent mcont = req->getMultipartContent(rft.params.at(var).name);
						if(mcont.isValid())
						{
							if(mcont.isAFile())
							{
								if(rft.params.at(var).type=="filestream")
								{
									pmvalue = rft.params.at(var).name;
								}
								else if(rft.params.at(var).type!="vector-of-filestream")
								{
									logger << "File can only be mapped to ifstream" << endl;
									res.setHTTPResponseStatus(HTTPResponseStatus::InternalServerError);
									return true;
								}
							}
							else
							{
								pmvalue = mcont.getContent();
							}
						}
						else if(rft.params.at(var).type!="vector-of-filestream")
						{
							logger << "Invalid mapping specified in config, no multipart content found with name " + rft.params.at(var).name << endl;
							res.setHTTPResponseStatus(HTTPResponseStatus::InternalServerError);
							return true;
						}
					}
					else
					{
						if(prsiz>1)
						{
							logger << "Request Body cannot be mapped to more than one argument..." << endl;
							res.setHTTPResponseStatus(HTTPResponseStatus::BadRequest);
							return true;
						}
						pmvalue = req->getContent();
					}

					logger << ("Restcontroller parameter type/value = "  + rft.params.at(var).type + "/" + pmvalue) << endl;
					logger << ("Restcontroller content types input/output = " + icont + "/" + ocont) << endl;

					if(rft.params.at(var).type=="int")
					{
						argus.push_back(rft.params.at(var).type);
						int* ival = new int(CastUtil::lexical_cast<int>(pmvalue));
						valus.push_back(ival);
					}
					else if(rft.params.at(var).type=="short")
					{
						argus.push_back(rft.params.at(var).type);
						short* ival = new short(CastUtil::lexical_cast<short>(pmvalue));
						valus.push_back(ival);
					}
					else if(rft.params.at(var).type=="long")
					{
						argus.push_back(rft.params.at(var).type);
						long* ival = new long(CastUtil::lexical_cast<long>(pmvalue));
						valus.push_back(ival);
					}
					else if(rft.params.at(var).type=="double")
					{
						argus.push_back(rft.params.at(var).type);
						double* ival = new double(CastUtil::lexical_cast<double>(pmvalue));
						valus.push_back(ival);
					}
					else if(rft.params.at(var).type=="float")
					{
						argus.push_back(rft.params.at(var).type);
						float* ival = new float(CastUtil::lexical_cast<float>(pmvalue));
						valus.push_back(ival);
					}
					else if(rft.params.at(var).type=="bool")
					{
						argus.push_back(rft.params.at(var).type);
						bool* ival = new bool(CastUtil::lexical_cast<bool>(pmvalue));
						valus.push_back(ival);
					}
					else if(rft.params.at(var).type=="string" || rft.params.at(var).type=="std::string")
					{
						argus.push_back(rft.params.at(var).type);
						string* sval = new string(pmvalue);
						valus.push_back(sval);
					}
					else if(rft.params.at(var).type=="filestream")
					{
						argus.push_back("ifstream*");
						MultipartContent mcont = req->getMultipartContent(pmvalue);
						if(mcont.isValid() && mcont.isAFile())
						{
							ifstream* ifs = new ifstream;
							ifs->open(mcont.getTempFileName().c_str());
							valus.push_back(ifs);
							allStreams.push_back(ifs);
						}
					}
					else if(rft.params.at(var).type=="vector-of-filestream")
					{
						vector<ifstream*> *vifs = NULL;
						if(mpvecstreams.find(rft.params.at(var).name)==mpvecstreams.end())
						{
							argus.push_back("vector<ifstream*>");
							vifs = new vector<ifstream*>;
							vector<MultipartContent> mcontvec = req->getMultiPartFileList(rft.params.at(var).name);
							for(int mci=0;mci<(int)mcontvec.size();mci++) {
								MultipartContent mcont = mcontvec.at(mci);
								if(mcont.isValid() && mcont.isAFile())
								{
									ifstream* ifs = new ifstream;
									ifs->open(mcont.getTempFileName().c_str());
									vifs->push_back(ifs);
									allStreams.push_back(ifs);
								}
							}
							mpvecstreams[rft.params.at(var).name] = vifs;
						}
						else
						{
							vifs = mpvecstreams[rft.params.at(var).name];
						}
						valus.push_back(vifs);
					}
					else if(rft.params.at(var).type.find("vector-of-")==0 || rft.params.at(var).type.find("list-of-")==0
							|| rft.params.at(var).type.find("deque-of-")==0 || rft.params.at(var).type.find("set-of-")==0
							|| rft.params.at(var).type.find("multiset-of-")==0 || rft.params.at(var).type.find("queue-of-")==0)
					{
						string stlcnt = rft.params.at(var).type;
						string stltype;
						string typp;
						if(rft.params.at(var).type.find("vector-of-")==0)
						{
							StringUtil::replaceFirst(stlcnt,"vector-of-","");
							stltype = "std::vector";
							typp = "vector<" + stlcnt + ">";
						}
						else if(rft.params.at(var).type.find("list-of-")==0)
						{
							StringUtil::replaceFirst(stlcnt,"list-of-","");
							stltype = "std::list";
							typp = "list<" + stlcnt + ">";
						}
						else if(rft.params.at(var).type.find("deque-of-")==0)
						{
							StringUtil::replaceFirst(stlcnt,"deque-of-","");
							stltype = "std::deque";
							typp = "deque<" + stlcnt + ">";
						}
						else if(rft.params.at(var).type.find("set-of-")==0)
						{
							StringUtil::replaceFirst(stlcnt,"set-of-","");
							stltype = "std::set";
							typp = "set<" + stlcnt + ">";
						}
						else if(rft.params.at(var).type.find("multiset-of-")==0)
						{
							StringUtil::replaceFirst(stlcnt,"multiset-of-","");
							stltype = "std::multiset";
							typp = "multiset<" + stlcnt + ">";
						}
						else if(rft.params.at(var).type.find("queue-of-")==0)
						{
							StringUtil::replaceFirst(stlcnt,"queue-of-","");
							stltype = "std::queue";
							typp = "queue<" + stlcnt + ">";
						}
						StringUtil::replaceFirst(stlcnt," ","");
						logger << ("Restcontroller param body holds "+stltype+" of type "  + stlcnt) << endl;

						argus.push_back(typp);
						void* voidPvect = NULL;
						if(icont==ContentTypes::CONTENT_TYPE_APPLICATION_JSON)
						{
							voidPvect = JSONSerialize::unSerializeUnknown(pmvalue, stltype+"<"+stlcnt+">",req->getCntxt_name());
						}
#ifdef INC_XMLSER
						else
						{
							voidPvect = XMLSerialize::unSerializeUnknown(pmvalue, stltype+"<"+stlcnt+",",req->getCntxt_name());
						}
#endif
						if(voidPvect==NULL)
						{
							res.setHTTPResponseStatus(HTTPResponseStatus::BadRequest);
							return true;
						}
						valus.push_back(voidPvect);
					}
					else
					{
						argus.push_back(rft.params.at(var).type);
						void* voidPvect = NULL;
						if(icont==ContentTypes::CONTENT_TYPE_APPLICATION_JSON)
						{
							voidPvect = JSONSerialize::unSerializeUnknown(pmvalue, rft.params.at(var).type,req->getCntxt_name());
						}
#ifdef INC_XMLSER
						else
						{
							voidPvect = XMLSerialize::unSerializeUnknown(pmvalue, rft.params.at(var).type,req->getCntxt_name());
						}
#endif
						if(voidPvect==NULL)
						{
							res.setHTTPResponseStatus(HTTPResponseStatus::BadRequest);
							return true;
						}
						valus.push_back(voidPvect);
					}
				} catch (const char* ex) {
					logger << "Restcontroller exception occurred" << endl;
					logger << ex << endl;
					invValue= true;
					res.setHTTPResponseStatus(HTTPResponseStatus::BadRequest);
					return true;
				} catch (...) {
					logger << "Restcontroller exception occurred" << endl;
					invValue= true;
					res.setHTTPResponseStatus(HTTPResponseStatus::BadRequest);
					return true;
				}
			}

			Method meth = srv.getMethod(rft.name, argus);
			if(meth.getMethodName()!="" && !invValue)
			{
				ref.invokeMethodUnknownReturn(_temp,meth,valus);
				logger << "Successfully called restcontroller" << endl;
				//return;

				for(int i=0;i<(int)allStreams.size();++i) {
					if(allStreams.at(i)!=NULL) {
						if(allStreams.at(i)->is_open()) {
							allStreams.at(i)->close();
							allStreams.at(i)->clear();
						}
						delete allStreams.at(i);
					}
				}

				map<string, vector<ifstream*>* >::iterator it;
				for(it=mpvecstreams.begin();it!=mpvecstreams.end();++it) {
					delete it->second;
				}
			}
			else
			{
				res.setHTTPResponseStatus(HTTPResponseStatus::NotFound);
				//res.addHeaderValue(HttpResponse::ContentType, ContentTypes::CONTENT_TYPE_TEXT_PLAIN);
				logger << "Rest Controller Method Not Found" << endl;
				//return;
			}
		}
	}
	return isContrl;
}
Пример #7
0
void* MethodInvoc::service(void* arg)
{
	int fd = *(int*)arg;
	init();
	string methInfo,retValue;
	_methinv_instance->server.Receive(fd,methInfo,1024);
	methInfo =methInfo.substr(0,methInfo.find_last_of(">")+1);
	try
	{
		XmlParser parser("Parser");
		if(methInfo.find("lang=\"c++\"")!=string::npos || methInfo.find("lang='c++'")!=string::npos)
		{
			Document doc = parser.getDocument(methInfo);
			Element message = doc.getRootElement();
			if(message.getTagName()!="method")
			{
				throw MethodInvokerException("No method Tag\n",retValue);
			}
			if(message.getAttributes().size()<3)
			{
				throw MethodInvokerException("name,class and lang are mandatory attributes\n",retValue);
			}
			else if(message.getAttribute("name")=="")
			{
				throw MethodInvokerException("name attribute missing\n",retValue);
			}
			else if(message.getAttribute("className")=="")
			{
				throw MethodInvokerException("class attribute missing\n",retValue);
			}
			else if(message.getAttribute("lang")=="")
			{
				throw MethodInvokerException("lang attribute missing\n",retValue);
			}
			if(message.getChildElements().size()!=1)
			{
				throw MethodInvokerException("message tag should have only one child tag\n",retValue);
			}
			else if(message.getChildElements().at(0).getTagName()!="args")
			{
				throw MethodInvokerException("message tag should have an args child tag\n",retValue);
			}
			XMLSerialize ser;
			Reflector reflector;
			args argus;
			vals valus;
			ElementList argts = message.getChildElements().at(0).getChildElements();
			for (unsigned var = 0; var < argts.size();  var++)
			{
				void *value = NULL;
				Element arg = argts.at(var);
				if(arg.getTagName()!="argument" || arg.getAttribute("type")=="")
					throw MethodInvokerException("every argument tag should have a name and type attribute\n",retValue);
				if(arg.getText()=="" && arg.getChildElements().size()==0)
					throw MethodInvokerException("argument value missing\n",retValue);
				if(arg.getAttribute("type")=="int")
				{
					int *vt = new int;
					*vt = CastUtil::lexical_cast<int>(arg.getText());
					value = vt;
				}
				else if(arg.getAttribute("type")=="float")
				{
					float *vt = new float;
					*vt = CastUtil::lexical_cast<float>(arg.getText());
					value = vt;
				}
				else if(arg.getAttribute("type")=="double")
				{
					double *vt = new double;
					*vt = CastUtil::lexical_cast<double>(arg.getText());
					value = vt;
				}
				else if(arg.getAttribute("type")=="string")
				{
					string *vt = new string;
					*vt = CastUtil::lexical_cast<string>(arg.getText());
					value = vt;
				}
				else if(arg.getAttribute("type")!="")
				{
					Element obj = arg.getChildElements().at(0);
					string objxml = obj.render();
					string objClassName = obj.getTagName();
					value = ser.unSerializeUnknown(objxml,arg.getAttribute("type"));
				}
				argus.push_back(arg.getAttribute("type"));
				valus.push_back(value);
			}
			string className = message.getAttribute("className");
			string returnType = message.getAttribute("returnType");
			string lang = message.getAttribute("lang");
			ClassInfo clas = reflector.getClassInfo(className);
			string methodName = message.getAttribute("name");;
			if(clas.getClassName()=="")
			{
				throw MethodInvokerException("class does not exist or is not in the library path\n",retValue);
			}
			Method meth = clas.getMethod(methodName,argus);
			if(meth.getMethodName()=="")
			{
				throw MethodInvokerException("method does not exist for the class or the class does not exist in the library path\n",retValue);
			}
			else
			{
				args argus;
				Constructor ctor = clas.getConstructor(argus);
				void *_temp = reflector.newInstanceGVP(ctor);
				if(returnType=="void" || returnType=="")
				{
					reflector.invokeMethod<void*>(_temp,meth,valus);
					retValue = ("<return:void></return:void>");
				}
				else
				{
					if(returnType=="int")
					{
						int retv = reflector.invokeMethod<int>(_temp,meth,valus);
						retValue = ("<return:int>"+CastUtil::lexical_cast<string>(retv)+"</return:int>");
					}
					else if(returnType=="float")
					{
						float retv = reflector.invokeMethod<float>(_temp,meth,valus);
						retValue = ("<return:float>"+CastUtil::lexical_cast<string>(retv)+"</return:float>");
					}
					else if(returnType=="double")
					{
						double retv = reflector.invokeMethod<double>(_temp,meth,valus);
						retValue = ("<return:double>"+CastUtil::lexical_cast<string>(retv)+"</return:double>");
					}
					else if(returnType=="string")
					{
						string retv = reflector.invokeMethod<string>(_temp,meth,valus);
						retValue = ("<return:string>"+retv+"</return:string>");
					}
					else if(returnType!="")
					{
						void* retobj = reflector.invokeMethodUnknownReturn(_temp,meth,valus);
						string oxml = ser.serializeUnknown(retobj,returnType);
						retValue = ("<return:"+returnType+">"+oxml+"</return:"+returnType+">");
					}
				}

			}
		}
		else
		{
			retValue = "<return:exception>This is a C++ daemon</return:exception>";
		}
		if(retValue!="")
			_methinv_instance->server.Send(fd,retValue);
		close(fd);
	}
	catch(MethodInvokerException *e)
	{
		_methinv_instance->server.Send(fd,retValue);
		close(fd);
	}
	catch(...)
	{
		retValue = ("<return:exception>XmlParseException occurred</return:exception>");
		_methinv_instance->server.Send(fd,retValue);
		close(fd);
	}
	return NULL;
}