-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathExtHandler.cpp
More file actions
275 lines (264 loc) · 9.1 KB
/
ExtHandler.cpp
File metadata and controls
275 lines (264 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
Copyright 2009-2020, Sumeet Chhetri
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
* ExtHandler.cpp
*
* Created on: Jun 17, 2012
* Author: Sumeet
*/
#include "ExtHandler.h"
bool ExtHandler::handle(HttpRequest* req, HttpResponse* res, void* dlib, void* ddlib, const std::string& ext, Reflector& reflector)
{
std::map<std::string, std::string, std::less<> >* tmplMap = &(ConfigurationData::getInstance()->templateMappingMap.find(req->getCntxt_name())->second);
//Logger logger = LoggerFactory::getLogger("ExtHandler");
bool cntrlit = false;
std::string content, claz;
#ifdef INC_TPE
if(tmplMap->find(req->getCurl())!=tmplMap->end())
{
if(ddlib != NULL)
{
bool done = false;
std::string tpefilename = tmplMap->find(req->getCurl())->second.substr(tmplMap->find(req->getCurl())->second.find(";")+1);
std::string tpeclasname = tmplMap->find(req->getCurl())->second.substr(0, tmplMap->find(req->getCurl())->second.find(";"));
cntrlit = true;
void *_temp = ConfigurationData::getInstance()->ffeadContext.getBean(tpeclasname, req->getCntxt_name());
if(_temp!=NULL)
{
try {
args argus;
vals valus;
ClassInfo* srv = ConfigurationData::getClassInfo(tpeclasname, req->getCntxt_name());
argus.push_back("HttpRequest*");
argus.push_back("Context*");
const Method& meth = srv->getMethod("getContext", argus);
if(meth.getMethodName()!="")
{
Context cnt;
valus.push_back(req);
valus.push_back(&cnt);
reflector.invokeMethodGVP(_temp,meth,valus);
std::string fname = CommonUtils::getTpeFnName(tpefilename, "");
void* mkr = dlsym(ddlib, fname.c_str());
if(mkr!=NULL)
{
TemplatePtr f = (TemplatePtr)mkr;
f(&cnt, res->getContent());
done = true;
}
else
{
//logger << ("No template found for " + fname) << std::endl;
res->setHTTPResponseStatus(HTTPResponseStatus::InternalServerError);
}
}
else
{
//logger << "Invalid Template handler, no method getContext found..." << std::endl;
res->setHTTPResponseStatus(HTTPResponseStatus::InternalServerError);
}
} catch(const std::exception& e) {
//logger << "Template exception occurred" << std::endl;
res->setHTTPResponseStatus(HTTPResponseStatus::InternalServerError);
}
ConfigurationData::getInstance()->ffeadContext.release(_temp, tpeclasname, req->getCntxt_name());
}
if(done)
{
res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
res->addHeader(HttpResponse::ContentType, ContentTypes::CONTENT_TYPE_TEXT_HTML);
}
}
if(cntrlit && res->getStatusCode()!="404") {
res->setDone(true);
}
return cntrlit;
}
#endif
std::map<std::string, std::string, std::less<> >* dcpMap = &(ConfigurationData::getInstance()->dcpMappingMap.find(req->getCntxt_name())->second);
std::map<std::string, std::string, std::less<> >* vwMap = &(ConfigurationData::getInstance()->viewMappingMap.find(req->getCntxt_name())->second);
std::map<std::string, std::string, std::less<> >* ajaxIntfMap = &(ConfigurationData::getInstance()->ajaxInterfaceMap.find(req->getCntxt_name())->second);
std::map<std::string, std::string, std::less<> >* fviewMap = &(ConfigurationData::getInstance()->fviewMappingMap.find(req->getCntxt_name())->second);
std::map<std::string, Element, std::less<> >* formMap = &(ConfigurationData::getInstance()->fviewFormMap.find(req->getCntxt_name())->second);
if(ajaxIntfMap->find(req->getCurl())!=ajaxIntfMap->end() && req->getMethod()=="POST" && req->getRequestParam("method")!="")
{
cntrlit = true;
std::string& claz = ajaxIntfMap->find(req->getCurl())->second;
//logger << "Inside Ajax Interface Execute" << std::endl;
strVec vemp;
std::string methName = req->getRequestParam("method");
if(methName=="")
{
res->setHTTPResponseStatus(HTTPResponseStatus::InternalServerError);
res->setDone(true);
return true;
}
else
{
std::string temp = req->getRequestParam("paramsize");
int paramSize = 0;
if(temp!="")
{
try {
paramSize = CastUtil::toInt(temp.c_str());
} catch(const std::exception& e) {
res->setHTTPResponseStatus(HTTPResponseStatus::InternalServerError);
paramSize = -1;
}
}
if(paramSize>=0)
{
//logger << "Reading Ajax params" << std::endl;
for(int i=0;i<paramSize;i++)
{
std::stringstream s;
std::string ss;
s << (i+1);
s >> ss;
ss = "param_" + ss;
//logger << ss << std::flush;
std::string tem = req->getRequestParam(ss);
vemp.push_back(tem);
}
std::string libName = INTER_LIB_FILE;
std::string funcName;
std::string metn,re;
StringUtil::replaceAll(claz, "::", "_");
metn = std::string(req->getCntxt_name()) + "invokeAjaxMethodFor"+claz+methName;
void *mkr = dlsym(dlib, metn.c_str());
if(mkr!=NULL)
{
typedef std::string (*Funptr2) (strVec);
Funptr2 f2 = (Funptr2)mkr;
//logger << ("Calling method " + metn) << std::endl;
try {
re = f2(vemp);
//logger << "Completed method call" << std::endl;
res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
res->addHeader(HttpResponse::ContentType, (re.find("{")==0 || re.find("[")==0)?"application/json":"text/plain");
res->setContent(re);
} catch(const std::exception& e) {
//logger << "AjaxInterface exception occurred" << std::endl;
res->setHTTPResponseStatus(HTTPResponseStatus::InternalServerError);
}
}
else
{
res->setHTTPResponseStatus(HTTPResponseStatus::InternalServerError);
}
}
else
{
res->setHTTPResponseStatus(HTTPResponseStatus::InternalServerError);
}
}
}
else if(ext==".form" && formMap->find(req->getFile())!=formMap->end())
{
try {
cntrlit = FormHandler::handle(req, res, reflector, &((*formMap)[req->getFile()]));
//logger << ("Request handled by FormHandler") << std::endl;
} catch(const std::exception& e) {
//logger << "FormHandler exception occurred" << std::endl;
res->setHTTPResponseStatus(HTTPResponseStatus::InternalServerError);
}
}
else if(ext==".fview" && fviewMap->find(req->getFile())!=fviewMap->end())
{
cntrlit = FviewHandler::handle(req, res);
//logger << ("Request handled by FviewHandler") << std::endl;
}
#ifdef INC_DCP
else if(dcpMap->find(req->getCurl())!=dcpMap->end())
{
std::string libName = INTER_LIB_FILE;
if(ddlib != NULL)
{
cntrlit = true;
std::string meth;
std::string file = dcpMap->find(req->getCurl())->second;
meth = "_" + file + "emittHTML";
void *mkr = dlsym(ddlib, meth.c_str());
if(mkr!=NULL)
{
DCPPtr f = (DCPPtr)mkr;
try {
content = f();
if(content.length()>0)
{
res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
res->addHeader(HttpResponse::ContentType, ContentTypes::CONTENT_TYPE_TEXT_HTML);
res->setContent(content);
}
} catch(const std::exception& e) {
//logger << "DCP exception occurred" << std::endl;
res->setHTTPResponseStatus(HTTPResponseStatus::InternalServerError);
}
}
else
{
//logger << ("No dcp found for " + meth) << std::endl;
}
}
}
#endif
#ifdef INC_DVIEW
else if(ext==".view" && vwMap->find(req->getCurl())!=vwMap->end())
{
void *_temp = ConfigurationData::getInstance()->ffeadContext.getBean(vwMap->find(req->getCurl())->second, req->getCntxt_name());
if(_temp!=NULL)
{
cntrlit = true;
try {
args argus;
argus.push_back("Document*");
vals valus;
ClassInfo* srv = ConfigurationData::getClassInfo(vwMap->find(req->getCurl())->second, req->getCntxt_name());
const Method& meth = srv->getMethod("getDocument", argus);
if(meth.getMethodName()!="")
{
Document doc;
valus.push_back(&doc);
reflector.invokeMethodGVP(_temp,meth,valus);
View view;
std::string t = view.generateDocument(doc);
content = t;
}
else
{
//logger << "Invalid Dynamic View handler, no method getDocument found..." << std::endl;
res->setHTTPResponseStatus(HTTPResponseStatus::InternalServerError);
}
} catch(const std::exception& e) {
//logger << "Dview exception occurred" << std::endl;
res->setHTTPResponseStatus(HTTPResponseStatus::InternalServerError);
}
ConfigurationData::getInstance()->ffeadContext.release(_temp, vwMap->find(req->getCurl())->second, req->getCntxt_name());
}
else
{
//logger << "Invalid Dynamic View handler" << std::endl;
}
if(content.length()>0)
{
res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
res->addHeader(HttpResponse::ContentType, ContentTypes::CONTENT_TYPE_TEXT_HTML);
res->setContent(content);
}
}
#endif
if(cntrlit && res->getStatusCode()!="404") {
res->setDone(true);
}
return cntrlit;
}