ffead.server.doc
ExtHandler.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  * ExtHandler.cpp
18  *
19  * Created on: Jun 17, 2012
20  * Author: Sumeet
21  */
22 
23 #include "ExtHandler.h"
24 
25 ExtHandler::ExtHandler() {
26  // TODO Auto-generated constructor stub
27 
28 }
29 
30 ExtHandler::~ExtHandler() {
31  // TODO Auto-generated destructor stub
32 }
33 
34 string ExtHandler::getContentStr(string url,string locale,string ext)
35 {
36  string all;
37  string fname = url;
38  if (url=="/")
39  {
40  return all;
41  }
42  ifstream myfile;
43  if(locale.find("english")==string::npos && (ext==".html" || ext==".htm"))
44  {
45  string fnj = fname;
46  StringUtil::replaceFirst(fnj,".",("_" + locale+"."));
47  myfile.open(fnj.c_str(),ios::in | ios::binary);
48  if (myfile.is_open())
49  {
50  string line;
51  while(getline(myfile,line)){all.append(line+"\n");}
52  myfile.close();
53  return all;
54  }
55  }
56  ifstream myfile1;
57  myfile1.open(fname.c_str(),ios::in | ios::binary);
58  if (myfile1.is_open())
59  {
60  string line;
61  while(getline(myfile1,line)){all.append(line+"\n");}
62  myfile1.close();
63  }
64  return all;
65 }
66 
67 bool ExtHandler::handle(HttpRequest* req, HttpResponse& res, void* dlib, void* ddlib, string resourcePath,
68  map<string, string> tmplMap, map<string, string> vwMap,string ext, map<string, string> props, map<string, string> ajaxIntfMap)
69 {
70  Logger logger = Logger::getLogger("ExtHandler");
71  bool cntrlit = false;
72  string content, claz;
73  string acurl = req->getActUrl();
74  StringUtil::replaceFirst(acurl,"//","/");
75  if(acurl.length()>1)
76  acurl = acurl.substr(1);
77  if(acurl.find(req->getCntxt_name())!=0)
78  acurl = req->getCntxt_name() + "/" + acurl;
79 
80  if(ajaxIntfMap[acurl]!="" && req->getMethod()=="POST" && req->getRequestParam("method")!="")
81  {
82  cntrlit = true;
83  AfcUtil::execute(*req, &res, ajaxIntfMap[acurl]);
84  }
85  else if(ext==".dcp")
86  {
87  string libName = Constants::INTER_LIB_FILE;
88  if(ddlib != NULL)
89  {
90  cntrlit = true;
91  int s = req->getUrl().find_last_of("/")+1;
92  int en = req->getUrl().find_last_of(".");
93  string meth,file;
94  file = req->getUrl().substr(s,en-s);
95  meth = "_" + file + "emittHTML";
96 
97  void *mkr = dlsym(ddlib, meth.c_str());
98  if(mkr!=NULL)
99  {
100  //logger << endl << "inside dcp " << meth << endl;
101  DCPPtr f = (DCPPtr)mkr;
102  content = f();
103  //string patf;
104  //patf = req->getCntxt_root() + "/dcp_" + file + ".html";
105  //content = getContentStr(patf,lprops[req->getDefaultLocale()],ext);
106  //delete mkr;
107  }
108  ext = ".html";
109  if(ext!="" && content.length()==0)
110  {
111  res.setHTTPResponseStatus(HTTPResponseStatus::NotFound);
112  //res.setContent_len("0");
113  }
114  else
115  {
116  res.setHTTPResponseStatus(HTTPResponseStatus::Ok);
117  res.setContent_type(props[ext]);
118  res.setContent_str(content);
119  //res.setContent_len(CastUtil::lexical_cast<string>(content.length()));
120  }
121  }
122  }
123  else if(ext==".view" && vwMap[req->getCntxt_name()+req->getFile()]!="")
124  {
125  cntrlit = true;
126  string libName = Constants::INTER_LIB_FILE;
127  if(dlib == NULL)
128  {
129  cerr << dlerror() << endl;
130  exit(-1);
131  }
132  claz = "getReflectionCIFor" + vwMap[req->getCntxt_name()+req->getFile()];
133  void *mkr = dlsym(dlib, claz.c_str());
134  if(mkr!=NULL)
135  {
136  FunPtr f = (FunPtr)mkr;
137  ClassInfo srv = f();
138  args argus;
139  Reflector ref;
140  Constructor ctor = srv.getConstructor(argus);
141  void *_temp = ref.newInstanceGVP(ctor);
142  DynamicView *thrd = (DynamicView *)_temp;
143  Document doc = thrd->getDocument();
144  View view;
145  string t = view.generateDocument(doc);
146  content = t;
147  }
148  ext = ".html";
149  if(ext!="" && (content.length()==0))
150  {
151  res.setHTTPResponseStatus(HTTPResponseStatus::NotFound);
152  //res.setContent_len("0");
153  }
154  else
155  {
156  res.setHTTPResponseStatus(HTTPResponseStatus::Ok);
157  res.setContent_type(props[ext]);
158  res.setContent_str(content);
159  //res.setContent_len(CastUtil::lexical_cast<string>(content.length()));
160  }
161  }
162  else if(ext==".tpe" && tmplMap[req->getCntxt_name()+req->getFile()]!="")
163  {
164  TemplateEngine te;
165  ext = ".html";
166  if(ddlib != NULL)
167  {
168  if(dlib == NULL)
169  {
170  cerr << dlerror() << endl;
171  exit(-1);
172  }
173  cntrlit = true;
174  claz = "getReflectionCIFor" + tmplMap[req->getCntxt_name()+req->getFile()];
175  void *mkr = dlsym(dlib, claz.c_str());
176  if(mkr!=NULL)
177  {
178  FunPtr f = (FunPtr)mkr;
179  ClassInfo srv = f();
180  args argus;
181  Constructor ctor = srv.getConstructor(argus);
182  Reflector ref;
183  void *_temp = ref.newInstanceGVP(ctor);
184  TemplateHandler *thrd = (TemplateHandler *)_temp;
185  Context cnt = thrd->getContext();
186 
187  logger << "Done with Template Context fetch" << endl;
188  map<string, void*> args;
189  Context::iterator it;
190  for (it=cnt.begin();it!=cnt.end();it++) {
191  string key = it->first;
192  Object o = it->second;
193  logger << ("Template key=" + key + " Value = ") << o.getVoidPointer()<< endl;
194  args[key] = o.getVoidPointer();
195  }
196 
197  int s = req->getUrl().find_last_of("/")+1;
198  int en = req->getUrl().find_last_of(".");
199  string meth,file;
200  file = req->getUrl().substr(s,en-s);
201  meth = "_" + file + "emittTemplateHTML";
202 
203  mkr = dlsym(ddlib, meth.c_str());
204  if(mkr!=NULL)
205  {
206  //logger << endl << "inside dcp " << meth << endl;
207  TemplatePtr f = (TemplatePtr)mkr;
208  content = f(args);
209  //string patf;
210  //patf = req->getCntxt_root() + "/dcp_" + file + ".html";
211  //content = getContentStr(patf,lprops[req->getDefaultLocale()],ext);
212  //delete mkr;
213  }
214  }
215  if(ext!="" && (content.length()==0))
216  {
217  res.setHTTPResponseStatus(HTTPResponseStatus::NotFound);
218  //res.setContent_len("0");
219  }
220  else
221  {
222  res.setHTTPResponseStatus(HTTPResponseStatus::Ok);
223  res.setContent_type(props[ext]);
224  res.setContent_str(content);
225  //res.setContent_len(CastUtil::lexical_cast<string>(content.length()));
226  }
227  }
228  }
229  else if(ext==".wsdl")
230  {
231  cntrlit = true;
232  content = getContentStr(resourcePath+req->getFile(),"english",ext);
233  if((content.length()==0))
234  {
235  res.setHTTPResponseStatus(HTTPResponseStatus::NotFound);
236  //res.setContent_len("0");
237  }
238  else
239  {
240  res.setHTTPResponseStatus(HTTPResponseStatus::Ok);
241  res.setContent_type(props[ext]);
242  res.setContent_str(content);
243  //res.setContent_len(CastUtil::lexical_cast<string>(content.length()));
244  }
245  }
246  return cntrlit;
247 }