-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathSoapHandler.cpp
More file actions
164 lines (156 loc) · 5.56 KB
/
SoapHandler.cpp
File metadata and controls
164 lines (156 loc) · 5.56 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
/*
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.
*/
/*
* SoapHandler.cpp
*
* Created on: Jun 17, 2012
* Author: Sumeet
*/
#include "SoapHandler.h"
void SoapHandler::handle(HttpRequest* req, HttpResponse* res, void* dlib, std::string ws_name)
{
//Logger logger = LoggerFactory::getLogger("SoapHandler");
std::string wsUrl = "http://" + ConfigurationData::getInstance()->coreServerProperties.ip_address + "/";
wsUrl += req->getCurl();
//logger << ("WsUrl is " + wsUrl) << std::endl;
std::string xmlcnttype = CommonUtils::getMimeType(".xml");
std::string meth,env;
Element* soapenv = NULL;
//logger.info("request => "+req->getContent());
Element* soapbody = NULL;
try
{
SimpleXmlParser parser("Validator");
Document doc;
parser.parse(req->getContent(), doc);
soapenv = &(doc.getRootElement());
soapbody = soapenv->getElementByNameIgnoreCase("body");
if(soapbody == NULL) {
throw std::runtime_error("SOAP Body not found in request");
}
//logger << soapbody->getTagName() << "----\n" << std::flush;
Element* method = (Element*)&(soapbody->getChildElements().at(0));
//logger << method.getTagName() << "----\n" << std::flush;
meth = method->getTagName();
std::string methodname = std::string(req->getCntxt_name()) + meth + ws_name;
//logger << methodname << "----\n" << std::flush;
void *mkr = dlsym(dlib, methodname.c_str());
if(mkr!=NULL)
{
typedef std::string (*WsPtr) (Element*);
WsPtr f = (WsPtr)mkr;
std::string outpt = f(method);
typedef std::map<std::string,std::string> AttributeList;
AttributeList attl = soapbody->getAttributes();
AttributeList::iterator it;
std::string bod = "<" + soapbody->getTagNameSpc();
for(it=attl.begin();it!=attl.end();it++)
{
bod.append(" " + it->first + "=\"" + it->second + "\" ");
}
bod.append(">"+outpt + "</" + soapbody->getTagNameSpc()+">");
attl = soapenv->getAttributes();
env = "<" + soapenv->getTagNameSpc();
for(it=attl.begin();it!=attl.end();it++)
{
env.append(" " + it->first + "=\"" + it->second + "\" ");
}
env.append(">"+bod + "</" + soapenv->getTagNameSpc()+">");
}
else
{
AttributeList attl = soapbody->getAttributes();
AttributeList::iterator it;
std::string bod = "<" + soapbody->getTagNameSpc();
for(it=attl.begin();it!=attl.end();it++)
{
bod.append(" " + it->first + "=\"" + it->second + "\" ");
}
bod.append("><soap-fault><faultcode>soap:Server</faultcode><faultstring>Operation not supported</faultstring><faultactor/><detail>No such method error</detail><soap-fault></" + soapbody->getTagNameSpc()+">");
attl = soapenv->getAttributes();
env = "<" + soapenv->getTagNameSpc();
for(it=attl.begin();it!=attl.end();it++)
{
env.append(" " + it->first + "=\"" + it->second + "\" ");
}
env.append(">"+bod + "</" + soapenv->getTagNameSpc()+">");
}
//logger << "\n----------------------------------------------------------------------------\n" << std::flush;
//logger << env << "\n----------------------------------------------------------------------------\n" << std::flush;
}
catch(const Exception& e)
{
std::string bod = "", btag = "";
AttributeList attl;
AttributeList::iterator it;
if(soapbody!=NULL)
{
attl = soapbody->getAttributes();
btag = soapbody->getTagNameSpc();
bod = "<" + soapbody->getTagNameSpc();
for(it=attl.begin();it!=attl.end();it++)
{
bod.append(" " + it->first + "=\"" + it->second + "\" ");
}
}
else
{
btag = soapenv->getNameSpc() + ":body";
bod = "<" + btag;
}
bod.append("><soap-fault><faultcode>soap:Server</faultcode><faultstring>"+e.getMessage()+"</faultstring><detail></detail><soap-fault></" + btag+">");
attl = soapenv->getAttributes();
env = "<" + soapenv->getTagNameSpc();
for(it=attl.begin();it!=attl.end();it++)
{
env.append(" " + it->first + "=\"" + it->second + "\" ");
}
env.append(">"+bod + "</" + soapenv->getTagNameSpc()+">");
//logger << ("Soap fault - " + e.getMessage()) << std::flush;
}
catch(const std::exception& faultc)
{
std::string fault(faultc.what());
std::string bod = "", btag = "";
AttributeList attl;
AttributeList::iterator it;
if(soapbody!=NULL)
{
attl = soapbody->getAttributes();
btag = soapbody->getTagNameSpc();
bod = "<" + soapbody->getTagNameSpc();
for(it=attl.begin();it!=attl.end();it++)
{
bod.append(" " + it->first + "=\"" + it->second + "\" ");
}
}
else
{
btag = soapenv->getNameSpc() + ":body";
bod = "<" + btag;
}
bod.append("><soap-fault><faultcode>soap:Server</faultcode><faultstring>"+fault+"</faultstring><detail></detail><soap-fault></" + btag+">");
attl = soapenv->getAttributes();
env = "<" + soapenv->getTagNameSpc();
for(it=attl.begin();it!=attl.end();it++)
{
env.append(" " + it->first + "=\"" + it->second + "\" ");
}
env.append(">"+bod + "</" + soapenv->getTagNameSpc()+">");
//logger << ("Soap fault - " + fault) << std::flush;
}
res->setHTTPResponseStatus(HTTPResponseStatus::Ok);
res->addHeader(HttpResponse::ContentType, xmlcnttype);
res->setContent(env);
res->setDone(true);
}