23 #include "ControllerHandler.h"
25 ControllerHandler::ControllerHandler() {
30 ControllerHandler::~ControllerHandler() {
34 bool ControllerHandler::handle(
HttpRequest* req,
HttpResponse& res, map<string, string> urlpattMap, map<string, string> mappattMap,
void* dlib,
35 string ext, resFuncMap rstCntMap, map<string, string> mapMap, map<string, string> urlMap,
string pthwofile)
37 Logger logger = Logger::getLogger(
"ControllerHandler");
39 bool isContrl =
false;
40 if((urlpattMap[req->getCntxt_name()+
"*.*"]!=
"" || urlMap[req->getCntxt_name()+ext]!=
""))
44 if(urlpattMap[req->getCntxt_name()+
"*.*"]!=
"")
45 controller = urlpattMap[req->getCntxt_name()+
"*.*"];
47 controller = urlMap[req->getCntxt_name()+ext];
48 claz =
"getReflectionCIFor" + controller;
49 string libName = Constants::INTER_LIB_FILE;
52 cerr << dlerror() << endl;
55 void *mkr = dlsym(dlib, claz.c_str());
58 FunPtr f = (FunPtr)mkr;
63 void *_temp = ref.newInstanceGVP(ctor);
66 logger << (
"Controller " + controller +
" called") << endl;
67 res = thrd->service(*req);
71 if(res.getStatusCode()!=
"")
73 ext = AuthHandler::getFileExtension(req->getUrl());
76 logger <<
"Controller Exception occurred" << endl;
78 logger <<
"Controller call complete" << endl;
81 else if((mappattMap[req->getCntxt_name()+
"*.*"]!=
"" || mapMap[req->getCntxt_name()+ext]!=
""))
83 string file = req->getFile();
84 string fili = file.substr(0,file.find_last_of(
"."));
85 if(mappattMap[req->getCntxt_name()+
"*.*"]!=
"")
87 req->setFile(fili+mappattMap[req->getCntxt_name()+
"*.*"]);
88 logger << (
"URL mapped from * to " + mappattMap[req->getCntxt_name()+
"*.*"]) << endl;
92 req->setFile(fili+mapMap[req->getCntxt_name()+ext]);
93 logger << (
"URL mapped from " + ext +
" to " + mapMap[req->getCntxt_name()+ext]) << endl;
98 resFuncMap::iterator it;
102 vector<string> valss;
103 map<string, string> mapOfValues;
105 for (it=rstCntMap.begin();it!=rstCntMap.end();it++)
112 prsiz = ft.params.size();
113 string pthwofiletemp(pthwofile);
115 string baseUrl(it->first);
117 StringUtil::split(resturlparts, baseUrl,
"/");
120 StringUtil::split(urlparts, pthwofiletemp,
"/");
122 if(urlparts.size()!=resturlparts.size())
134 for (
int var = 0; var < (int)resturlparts.size(); var++)
137 if(resturlparts.at(var).find(
"{")!=string::npos && resturlparts.at(var).find(
"}")!=string::npos
138 && resturlparts.at(var).length()>2)
140 string paramname = resturlparts.at(var);
142 int st = paramname.find(
"{")+1;
143 pref = paramname.substr(0, st-1);
144 int len = paramname.find(
"}") - st;
145 suff = paramname.substr(paramname.find(
"}")+1);
146 paramname = paramname.substr(st, len);
147 string paramvalue = urlparts.at(var);
150 int stpre = paramvalue.find(pref) + pref.length();
151 int len = paramvalue.length() - pref.length() - suff.length();
152 paramvalue = paramvalue.substr(stpre, len);
154 mapOfValues[paramname] = paramvalue;
156 logger << (
"Restcontroller matched url : " + pthwofiletemp +
",param size: " + CastUtil::lexical_cast<
string>(prsiz) +
157 ", against url: " + baseUrl) << endl;
159 else if(urlparts.at(var)!=resturlparts.at(var))
168 string lhs = StringUtil::toUpperCopy(ft.meth);
169 string rhs = StringUtil::toUpperCopy(req->getMethod());
174 logger <<
"Encountered rest controller url/method match" << endl;
181 res.setHTTPResponseStatus(HTTPResponseStatus::InvalidMethod);
186 res.setHTTPResponseStatus(HTTPResponseStatus::NotFound);
199 string libName = Constants::INTER_LIB_FILE;
202 cerr << dlerror() << endl;
205 string clasnam(
"getReflectionCIFor"+rft.clas);
206 void *mkr = dlsym(dlib, clasnam.c_str());
207 logger << mkr << endl;
210 FunPtr f = (FunPtr)mkr;
215 void *_temp = ref.newInstanceGVP(ctor);
217 rstcnt->request = req;
218 rstcnt->response = &res;
221 bool invValue =
false;
222 for (
int var = 0; var < prsiz; var++)
226 string icont = rft.icontentType;
227 string ocont = rft.ocontentType;
230 icont = ContentTypes::CONTENT_TYPE_APPLICATION_JSON;
231 else if(icont!=req->getContent_type())
233 res.setHTTPResponseStatus(HTTPResponseStatus::UnsupportedMedia);
238 ocont = ContentTypes::CONTENT_TYPE_APPLICATION_JSON;
240 req->setContent_type(icont);
241 res.setContent_type(ocont);
244 if(rft.params.at(var).from==
"path")
245 pmvalue = mapOfValues[rft.params.at(var).name];
246 else if(rft.params.at(var).from==
"reqparam")
247 pmvalue = req->getQueryParam(rft.params.at(var).name);
248 else if(rft.params.at(var).from==
"postparam")
249 pmvalue = req->getRequestParam(rft.params.at(var).name);
250 else if(rft.params.at(var).from==
"header")
251 pmvalue = req->getXtraHeader(rft.params.at(var).name);
253 pmvalue = req->getContent();
255 logger << (
"Restcontroller parameter type/value = " + rft.params.at(var).type +
"/" + pmvalue) << endl;
256 logger << (
"Restcontroller content types input/output = " + icont +
"/" + ocont) << endl;
258 if(rft.params.at(var).type==
"int")
260 argus.push_back(rft.params.at(var).type);
261 int* ival =
new int(CastUtil::lexical_cast<int>(pmvalue));
262 valus.push_back(ival);
264 else if(rft.params.at(var).type==
"short")
266 argus.push_back(rft.params.at(var).type);
267 short* ival =
new short(CastUtil::lexical_cast<short>(pmvalue));
268 valus.push_back(ival);
270 else if(rft.params.at(var).type==
"long")
272 argus.push_back(rft.params.at(var).type);
273 long* ival =
new long(CastUtil::lexical_cast<long>(pmvalue));
274 valus.push_back(ival);
276 else if(rft.params.at(var).type==
"double")
278 argus.push_back(rft.params.at(var).type);
279 double* ival =
new double(CastUtil::lexical_cast<double>(pmvalue));
280 valus.push_back(ival);
282 else if(rft.params.at(var).type==
"float")
284 argus.push_back(rft.params.at(var).type);
285 float* ival =
new float(CastUtil::lexical_cast<float>(pmvalue));
286 valus.push_back(ival);
288 else if(rft.params.at(var).type==
"bool")
290 argus.push_back(rft.params.at(var).type);
291 bool* ival =
new bool(CastUtil::lexical_cast<bool>(pmvalue));
292 valus.push_back(ival);
294 else if(rft.params.at(var).type==
"string" || rft.params.at(var).type==
"std::string")
296 argus.push_back(rft.params.at(var).type);
297 string* sval =
new string(pmvalue);
298 valus.push_back(sval);
300 else if(rft.params.at(var).type.find(
"vector<")==0)
302 string stlcnt = rft.params.at(var).type;
303 StringUtil::replaceFirst(stlcnt,
"vector<",
"");
304 StringUtil::replaceFirst(stlcnt,
">",
"");
305 StringUtil::replaceFirst(stlcnt,
" ",
"");
306 logger << (
"Restcontroller param body holds vector of type " + stlcnt) << endl;
307 string typp =
"vector<" + stlcnt +
">";
308 argus.push_back(typp);
309 void* voidPvect = NULL;
310 if(icont==ContentTypes::CONTENT_TYPE_APPLICATION_JSON)
312 voidPvect = JSONSerialize::unSerializeUnknown(pmvalue,
"std::vector<"+stlcnt+
">");
316 voidPvect = XMLSerialize::unSerializeUnknown(pmvalue,
"std::vector<"+stlcnt+
",");
320 res.setHTTPResponseStatus(HTTPResponseStatus::BadRequest);
323 valus.push_back(voidPvect);
327 argus.push_back(rft.params.at(var).type);
328 void* voidPvect = NULL;
329 if(icont==ContentTypes::CONTENT_TYPE_APPLICATION_JSON)
331 voidPvect = JSONSerialize::unSerializeUnknown(pmvalue, rft.params.at(var).type);
335 voidPvect = XMLSerialize::unSerializeUnknown(pmvalue, rft.params.at(var).type);
339 res.setHTTPResponseStatus(HTTPResponseStatus::BadRequest);
342 valus.push_back(voidPvect);
344 }
catch (
const char* ex) {
345 logger <<
"Restcontroller exception occurred" << endl;
346 logger << ex << endl;
348 res.setHTTPResponseStatus(HTTPResponseStatus::BadRequest);
351 logger <<
"Restcontroller exception occurred" << endl;
353 res.setHTTPResponseStatus(HTTPResponseStatus::BadRequest);
358 Method meth = srv.getMethod(rft.name, argus);
359 if(meth.getMethodName()!=
"" && !invValue)
361 ref.invokeMethodUnknownReturn(_temp,meth,valus);
362 logger <<
"Successfully called restcontroller" << endl;
367 res.setHTTPResponseStatus(HTTPResponseStatus::NotFound);
373 logger <<
"Rest Controller Method Not Found" << endl;