ffead.server.doc
SoapHandler.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  * SoapHandler.cpp
18  *
19  * Created on: Jun 17, 2012
20  * Author: Sumeet
21  */
22 
23 #include "SoapHandler.h"
24 
25 SoapHandler::SoapHandler() {
26  // TODO Auto-generated constructor stub
27 
28 }
29 
30 SoapHandler::~SoapHandler() {
31  // TODO Auto-generated destructor stub
32 }
33 
34 void SoapHandler::handle(HttpRequest* req, HttpResponse& res, void* dlib, string xmlcnttype)
35 {
36  Logger logger = Logger::getLogger("SoapHandler");
37  string meth,ws_name,env;
38  ws_name = req->getFile();
39  Element soapenv;
40  logger.info("request => "+req->getContent());
41  Element soapbody;
42  try
43  {
44  XmlParser parser("Parser");
45  Document doc = parser.getDocument(req->getContent());
46  soapenv = doc.getRootElement();
47  //logger << soapenv.getTagName() << "----\n" << flush;
48 
49  if(soapenv.getChildElements().size()==1
50  && soapenv.getChildElements().at(0).getTagName()=="Body")
51  soapbody = soapenv.getChildElements().at(0);
52  else if(soapenv.getChildElements().size()==2
53  && soapenv.getChildElements().at(1).getTagName()=="Body")
54  soapbody = soapenv.getChildElements().at(1);
55  //logger << soapbody.getTagName() << "----\n" << flush;
56  Element method = soapbody.getChildElements().at(0);
57  //logger << method.getTagName() << "----\n" << flush;
58  meth = method.getTagName();
59  string methodname = meth + ws_name;
60  //ogger << methodname << "----\n" << flush;
61  void *mkr = dlsym(dlib, methodname.c_str());
62  if(mkr!=NULL)
63  {
64  typedef string (*WsPtr) (Element);
65  WsPtr f = (WsPtr)mkr;
66  string outpt = f(method);
67  typedef map<string,string> AttributeList;
68  AttributeList attl = soapbody.getAttributes();
69  AttributeList::iterator it;
70  string bod = "<" + soapbody.getTagNameSpc();
71  for(it=attl.begin();it!=attl.end();it++)
72  {
73  bod.append(" " + it->first + "=\"" + it->second + "\" ");
74  }
75  bod.append(">"+outpt + "</" + soapbody.getTagNameSpc()+">");
76  attl = soapenv.getAttributes();
77  env = "<" + soapenv.getTagNameSpc();
78  for(it=attl.begin();it!=attl.end();it++)
79  {
80  env.append(" " + it->first + "=\"" + it->second + "\" ");
81  }
82  env.append(">"+bod + "</" + soapenv.getTagNameSpc()+">");
83  //delete mkr;
84  }
85  else
86  {
87  typedef map<string,string> AttributeList;
88  AttributeList attl = soapbody.getAttributes();
89  AttributeList::iterator it;
90  string bod = "<" + soapbody.getTagNameSpc();
91  for(it=attl.begin();it!=attl.end();it++)
92  {
93  bod.append(" " + it->first + "=\"" + it->second + "\" ");
94  }
95  bod.append("><soap-fault><faultcode>soap:Server</faultcode><faultstring>Operation not supported</faultstring><faultactor/><detail>No such method error</detail><soap-fault></" + soapbody.getTagNameSpc()+">");
96  attl = soapenv.getAttributes();
97  env = "<" + soapenv.getTagNameSpc();
98  for(it=attl.begin();it!=attl.end();it++)
99  {
100  env.append(" " + it->first + "=\"" + it->second + "\" ");
101  }
102  env.append(">"+bod + "</" + soapenv.getTagNameSpc()+">");
103  }
104  logger << "\n----------------------------------------------------------------------------\n" << flush;
105  logger << env << "\n----------------------------------------------------------------------------\n" << flush;
106  }
107  catch(string &fault)
108  {
109  typedef map<string,string> AttributeList;
110  AttributeList attl = soapbody.getAttributes();
111  AttributeList::iterator it;
112  string bod = "<" + soapbody.getTagNameSpc();
113  for(it=attl.begin();it!=attl.end();it++)
114  {
115  bod.append(" " + it->first + "=\"" + it->second + "\" ");
116  }
117  bod.append("><soap-fault><faultcode>soap:Server</faultcode><faultstring>"+fault+"</faultstring><detail></detail><soap-fault></" + soapbody.getTagNameSpc()+">");
118  attl = soapenv.getAttributes();
119  env = "<" + soapenv.getTagNameSpc();
120  for(it=attl.begin();it!=attl.end();it++)
121  {
122  env.append(" " + it->first + "=\"" + it->second + "\" ");
123  }
124  env.append(">"+bod + "</" + soapenv.getTagNameSpc()+">");
125  logger << ("Soap fault - " + fault) << flush;
126  }
127  catch(Exception *e)
128  {
129  typedef map<string,string> AttributeList;
130  AttributeList attl = soapbody.getAttributes();
131  AttributeList::iterator it;
132  string bod = "<" + soapbody.getTagNameSpc();
133  for(it=attl.begin();it!=attl.end();it++)
134  {
135  bod.append(" " + it->first + "=\"" + it->second + "\" ");
136  }
137  bod.append("><soap-fault><faultcode>soap:Server</faultcode><faultstring>"+e->what()+"</faultstring><detail></detail><soap-fault></" + soapbody.getTagNameSpc()+">");
138  attl = soapenv.getAttributes();
139  env = "<" + soapenv.getTagNameSpc();
140  for(it=attl.begin();it!=attl.end();it++)
141  {
142  env.append(" " + it->first + "=\"" + it->second + "\" ");
143  }
144  env.append(">"+bod + "</" + soapenv.getTagNameSpc()+">");
145  logger << ("Soap fault - " + e->what()) << flush;
146  }
147  catch(...)
148  {
149  typedef map<string,string> AttributeList;
150  AttributeList attl = soapbody.getAttributes();
151  AttributeList::iterator it;
152  string bod = "<" + soapbody.getTagNameSpc();
153  for(it=attl.begin();it!=attl.end();it++)
154  {
155  bod.append(" " + it->first + "=\"" + it->second + "\" ");
156  }
157  bod.append("><soap-fault><faultcode>soap:Server</faultcode><faultstring>Standard Exception</faultstring><detail></detail><soap-fault></" + soapbody.getTagNameSpc()+">");
158  attl = soapenv.getAttributes();
159  env = "<" + soapenv.getTagNameSpc();
160  for(it=attl.begin();it!=attl.end();it++)
161  {
162  env.append(" " + it->first + "=\"" + it->second + "\" ");
163  }
164  env.append(">"+bod + "</" + soapenv.getTagNameSpc()+">");
165  logger << "Soap Standard Exception" << flush;
166  }
167  res.setHTTPResponseStatus(HTTPResponseStatus::Ok);
168  res.setContent_type(xmlcnttype);
169  res.setContent_str(env);
170  //res.setContent_len(CastUtil::lexical_cast<string>(env.length()));
171 }