ffead.server.doc
FormHandler.cpp
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 /*
17  * FormHandler.cpp
18  *
19  * Created on: Jun 17, 2012
20  * Author: Sumeet
21  */
22 
23 #include "FormHandler.h"
24 
25 FormHandler::FormHandler() {
26  // TODO Auto-generated constructor stub
27 
28 }
29 
30 FormHandler::~FormHandler() {
31  // TODO Auto-generated destructor stub
32 }
33 
34 string FormHandler::handle(HttpRequest* req, HttpResponse& res, map<string, Element> formMap, void* dlib)
35 {
36  Logger logger = Logger::getLogger("FormHandler");
37  Reflector ref;
38  Element ele = formMap[req->getFile()];
39  //logger << ele.getTagName() << endl;
40  //logger << ele.render() << endl;
41  ClassInfo binfo = ref.getClassInfo(ele.getAttribute("bean"));
42  ElementList eles = ele.getChildElements();
43  string json = "{";
44  for (unsigned int apps = 0; apps < eles.size(); apps++)
45  {
46  if(eles.at(apps).getTagName()=="field")
47  {
48  string name = eles.at(apps).getAttribute("name");
49  Field fld = binfo.getField(eles.at(apps).getAttribute("prop"));
50  if(fld.getType()=="string")
51  json += "\""+eles.at(apps).getAttribute("prop")+"\": \"" + req->getQueryParam(name) + "\",";
52  else
53  {
54  if(fld.getType()=="int" || fld.getType()=="long")
55  {
56  if(req->getQueryParam(name)=="")
57  json += "\""+eles.at(apps).getAttribute("prop")+"\": 0,";
58  else
59  json += "\""+eles.at(apps).getAttribute("prop")+"\": " + req->getQueryParam(name) + ",";
60  }
61  else if(fld.getType()=="double" || fld.getType()=="float")
62  {
63  if(req->getQueryParam(name)=="")
64  json += "\""+eles.at(apps).getAttribute("prop")+"\": 0.0,";
65  else
66  json += "\""+eles.at(apps).getAttribute("prop")+"\": " + req->getQueryParam(name) + ",";
67  }
68  else if(fld.getType()=="bool")
69  {
70  if(req->getQueryParam(name)=="")
71  json += "\""+eles.at(apps).getAttribute("prop")+"\": false,";
72  else
73  json += "\""+eles.at(apps).getAttribute("prop")+"\": " + req->getQueryParam(name) + ",";
74  }
75  }
76  }
77  }
78  if(json.find(",")!=string::npos)
79  {
80  json = json.substr(0,json.length()-1);
81  }
82  json += "}";
83  //logger << json << endl;
84  string libName = Constants::INTER_LIB_FILE;
85  if(dlib == NULL)
86  {
87  cerr << dlerror() << endl;
88  exit(-1);
89  }
90  string meth = "toVoidP" + ele.getAttribute("bean");
91  logger << ("Fetching Formcontroller for " + ele.getAttribute("bean")) << endl;
92  void *mkr = dlsym(dlib, meth.c_str());
93  if(mkr!=NULL)
94  {
95  toVoidP f1 = (toVoidP)mkr;
96  void *_beaninst = f1(json);
97  //FunPtr f = (FunPtr)mkr;
98  ClassInfo srv = ref.getClassInfo(ele.getAttribute("controller"));
99  args argus;
100  vals valus;
101  Constructor ctor = srv.getConstructor(argus);
102  void *_temp = ref.newInstanceGVP(ctor);
103  argus.push_back("void*");
104  argus.push_back("HttpResponse*");
105  valus.push_back(_beaninst);
106  valus.push_back(&res);
107  Method meth = srv.getMethod("onSubmit",argus);
108  if(meth.getMethodName()!="")
109  {
110  ref.invokeMethodUnknownReturn(_temp,meth,valus);
111  logger << "Successfully called Formcontroller" << endl;
112  }
113  else
114  {
115  res.setHTTPResponseStatus(HTTPResponseStatus::NotFound);
116  res.setContent_type(ContentTypes::CONTENT_TYPE_TEXT_PLAIN);
117  res.setContent_str("Formcontroller Method Not Found");
118  logger << "Formcontroller Method Not Found" << endl;
119  }
120  }
121  return json;
122 }