soapbind.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #pragma warning( disable : 4786 )
  14. //Jlib
  15. #include "jliball.hpp"
  16. //SCM Interfaces
  17. #include "esp.hpp"
  18. //ESP Core
  19. #include "espthread.hpp"
  20. //ESP Bindings
  21. #include "SOAP/Platform/soapbind.hpp"
  22. #include "SOAP/client/soapclient.hpp"
  23. #include "http/platform/httpprot.hpp"
  24. #include "http/platform/httpservice.hpp"
  25. #include "SOAP/Platform/soapservice.hpp"
  26. #define ESP_FACTORY DECL_EXPORT
  27. CSoapBinding::CSoapBinding()
  28. {
  29. }
  30. CSoapBinding::~CSoapBinding()
  31. {
  32. }
  33. //IEspRpcBinding
  34. const char * CSoapBinding::getRpcType()
  35. {
  36. return "soap";
  37. }
  38. const char * CSoapBinding::getTransportType()
  39. {
  40. return "unknown";
  41. }
  42. int CSoapBinding::processRequest(IRpcMessage* rpc_call, IRpcMessage* rpc_response)
  43. {
  44. return 0;
  45. }
  46. CHttpSoapBinding::CHttpSoapBinding():EspHttpBinding(NULL, NULL, NULL)
  47. {
  48. log_level_=hsl_none;
  49. }
  50. CHttpSoapBinding::CHttpSoapBinding(IPropertyTree* cfg, const char *bindname, const char *procname, http_soap_log_level level)
  51. : EspHttpBinding(cfg, bindname, procname)
  52. {
  53. log_level_=level;
  54. }
  55. CHttpSoapBinding::~CHttpSoapBinding()
  56. {
  57. }
  58. const char * CHttpSoapBinding::getTransportType()
  59. {
  60. return "http";
  61. }
  62. static CSoapFault* makeSoapFault(CHttpRequest* request, IMultiException* me, const char *ns)
  63. {
  64. const char* svcName = request->queryServiceName();
  65. if (svcName && *svcName)
  66. {
  67. const char* method = request->queryServiceMethod();
  68. StringBuffer host;
  69. const char* wsdlAddr = request->queryParameters()->queryProp("__wsdl_address");
  70. if (wsdlAddr && *wsdlAddr)
  71. host.append(wsdlAddr);
  72. else
  73. {
  74. host.append(request->queryHost());
  75. if (request->getPort()>0)
  76. host.append(":").append(request->getPort());
  77. }
  78. VStringBuffer ns_ext("xmlns=\"%s\""
  79. " xsi:schemaLocation=\"%s %s/%s/%s?xsd\"",
  80. ns, ns, host.str(), svcName, method ? method : "");
  81. return new CSoapFault(me, ns_ext);
  82. }
  83. return new CSoapFault(me);
  84. }
  85. int CHttpSoapBinding::onSoapRequest(CHttpRequest* request, CHttpResponse* response)
  86. {
  87. Owned<CSoapFault> soapFault;
  88. try
  89. {
  90. return HandleSoapRequest(request,response);
  91. }
  92. catch (IMultiException* mex)
  93. {
  94. StringBuffer ns;
  95. soapFault.setown(makeSoapFault(request,mex, generateNamespace(*request->queryContext(), request, request->queryServiceName(), request->queryServiceMethod(), ns).str()));
  96. //SetHTTPErrorStatus(mex->errorCode(),response);
  97. SetHTTPErrorStatus(500,response);
  98. mex->Release();
  99. }
  100. catch (IException* e)
  101. {
  102. StringBuffer ns;
  103. Owned<IMultiException> mex = MakeMultiException("Esp");
  104. mex->append(*e); // e is owned by mex
  105. soapFault.setown(makeSoapFault(request,mex, generateNamespace(*request->queryContext(), request, request->queryServiceName(), request->queryServiceMethod(), ns).str()));
  106. SetHTTPErrorStatus(500,response);
  107. }
  108. catch (...)
  109. {
  110. soapFault.setown(new CSoapFault(500,"Internal Server Error"));
  111. SetHTTPErrorStatus(500,response);
  112. }
  113. //response->setContentType(soapFault->get_content_type());
  114. response->setContentType(HTTP_TYPE_TEXT_XML_UTF8);
  115. response->setContent(soapFault->get_text());
  116. response->send();
  117. return -1;
  118. }
  119. int CHttpSoapBinding::HandleSoapRequest(CHttpRequest* request, CHttpResponse* response)
  120. {
  121. StringBuffer requeststr;
  122. request->getContent(requeststr);
  123. if(requeststr.length() == 0)
  124. throw MakeStringException(-1, "Content read is empty");
  125. Owned<CSoapService> soapservice;
  126. Owned<CSoapRequest> soaprequest;
  127. Owned<CSoapResponse> soapresponse;
  128. //soapservice.setown(new CSoapService((IEspSoapBinding*)(this)));
  129. soapservice.setown(new CSoapService(this));
  130. soaprequest.setown(new CSoapRequest);
  131. soaprequest->set_text(requeststr.str());
  132. StringBuffer contenttype;
  133. request->getContentType(contenttype);
  134. soaprequest->set_content_type(contenttype.str());
  135. soaprequest->setContext(request->queryContext());
  136. CMimeMultiPart* multipart = request->queryMultiPart();
  137. if(multipart != NULL)
  138. {
  139. soaprequest->setOwnMultiPart(LINK(multipart));
  140. }
  141. soapresponse.setown(new CSoapResponse);
  142. StringBuffer reqPath;
  143. request->getPath(reqPath);
  144. setRequestPath(reqPath.str());
  145. soapservice->processRequest(*soaprequest.get(), *soapresponse.get());
  146. response->setVersion(HTTP_VERSION);
  147. int status = soapresponse->get_status();
  148. if(status == SOAP_OK)
  149. response->setStatus(HTTP_STATUS_OK);
  150. else if(status == SOAP_SERVER_ERROR || status == SOAP_RPC_ERROR || status == SOAP_CONNECTION_ERROR)
  151. {
  152. StringBuffer msg("Internal Server Error");
  153. const char* detail = soapresponse->get_err();
  154. if (detail && *detail)
  155. msg.appendf(" [%s]", detail);
  156. throw MakeStringExceptionDirect(500, msg.str());
  157. }
  158. else if(status == SOAP_CLIENT_ERROR || status == SOAP_REQUEST_TYPE_ERROR)
  159. {
  160. StringBuffer msg("Bad Request");
  161. const char* detail = soapresponse->get_err();
  162. if (detail && *detail)
  163. msg.appendf(" [%s]", detail);
  164. throw MakeStringExceptionDirect(400, msg.str());
  165. }
  166. else if(status == SOAP_AUTHENTICATION_REQUIRED)
  167. response->sendBasicChallenge(m_challenge_realm.str(), false);
  168. else if(status == SOAP_AUTHENTICATION_ERROR)
  169. {
  170. throw MakeStringExceptionDirect(401,"Unauthorized Access");
  171. }
  172. else
  173. response->setStatus(HTTP_STATUS_OK);
  174. response->setContentType(soapresponse->get_content_type());
  175. response->setContent(soapresponse->get_text());
  176. response->send();
  177. return 0;
  178. }
  179. void CSoapRequestBinding::post(const char *proxy, const char* url, IRpcResponseBinding& response, const char *soapaction)
  180. {
  181. CRpcCall rpccall;
  182. CRpcResponse rpcresponse;
  183. rpccall.set_url(url);
  184. rpccall.setProxy(proxy);
  185. serialize(*static_cast<IRpcMessage*>(&rpccall));
  186. CSoapClient soapclient; //to add support for handling cookies soapclient(false);
  187. soapclient.setUsernameToken(getUserId(), getPassword(), getRealm());
  188. int result = soapclient.postRequest(soapaction, rpccall, rpcresponse);
  189. if(result == SOAP_OK)
  190. {
  191. response.setRpcState(RPC_MESSAGE_OK);
  192. response.unserialize(rpcresponse, NULL, NULL);
  193. }
  194. else if(result == SOAP_CONNECTION_ERROR)
  195. {
  196. response.setRpcState(RPC_MESSAGE_CONNECTION_ERROR);
  197. }
  198. else
  199. {
  200. response.setRpcState(RPC_MESSAGE_ERROR);
  201. }
  202. }
  203. void CSoapComplexType::appendContent(IEspContext* ctx, MemoryBuffer& buffer, StringBuffer& mimetype)
  204. {
  205. StringBuffer content;
  206. if (ctx && ctx->getResponseFormat()==ESPSerializationJSON)
  207. {
  208. const char *jsonp = ctx->queryRequestParameters()->queryProp("jsonp");
  209. if (jsonp && *jsonp)
  210. content.append(jsonp).append('(');
  211. content.append('{');
  212. serializeStruct(ctx, content, (const char *)NULL);
  213. content.append('}');
  214. if (jsonp && *jsonp)
  215. content.append(");");
  216. mimetype.set("application/json; charset=UTF-8");
  217. }
  218. else
  219. {
  220. buffer.append(38, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  221. serializeStruct(ctx, content, (const char *)NULL);
  222. mimetype.set("text/xml; charset=UTF-8");
  223. }
  224. buffer.append(content.length(), content.str());
  225. }
  226. inline void open_element(IEspContext *ctx, StringBuffer &xml, const char *name, const char *uri, const char *prefix)
  227. {
  228. if (!name || !*name)
  229. return;
  230. xml.append("<");
  231. if (prefix && *prefix)
  232. xml.append(prefix).append(':');
  233. xml.append(name);
  234. if (uri && *uri)
  235. {
  236. xml.append("xmlns");
  237. if (prefix && *prefix)
  238. xml.append(':').append(prefix);
  239. xml.append("=\"").append(uri).append('\"');
  240. }
  241. }
  242. inline void start_child_attributes(IEspContext *ctx, StringBuffer &xml, const char *name)
  243. {
  244. }
  245. inline void start_child_elements(IEspContext *ctx, StringBuffer &xml, const char *name)
  246. {
  247. if (!name || !*name)
  248. return;
  249. xml.append('>');
  250. }
  251. inline void close_element(IEspContext *ctx, StringBuffer &xml, const char *name, const char *prefix)
  252. {
  253. if (!name || !*name)
  254. return;
  255. xml.append("</");
  256. if (prefix && *prefix)
  257. xml.append(prefix).append(':');
  258. xml.append(name).append('>');
  259. }
  260. void CSoapComplexType::serializeJSONStruct(IEspContext* ctx, StringBuffer& s, const char *name)
  261. {
  262. if (ctx && ctx->getResponseFormat()==ESPSerializationJSON)
  263. {
  264. appendJSONNameOrDelimit(s, name);
  265. s.append("{");
  266. serializeContent(ctx, s);
  267. s.append("}");
  268. return;
  269. }
  270. }
  271. void CSoapComplexType::serializeStruct(IEspContext* ctx, StringBuffer& s, const char *name)
  272. {
  273. const char *tag = (name && *name) ? name : getRootName();
  274. if (ctx && ctx->getResponseFormat()==ESPSerializationJSON)
  275. return serializeJSONStruct(ctx, s, tag);
  276. open_element(ctx, s, tag, getNsURI(), getNsPrefix());
  277. start_child_attributes(ctx, s, name);
  278. serializeAttributes(ctx, s);
  279. start_child_elements(ctx, s, tag);
  280. serializeContent(ctx, s);
  281. close_element(ctx, s, tag, getNsPrefix());
  282. }
  283. void CSoapComplexType::serializeItem(IEspContext* ctx, StringBuffer& s, const char *name)
  284. {
  285. if (ctx && ctx->getResponseFormat()==ESPSerializationJSON)
  286. return serializeJSONStruct(ctx, s, NULL);
  287. serializeStruct(ctx, s, name);
  288. }
  289. void CSoapResponseBinding::handleExceptions(IMultiException *me, const char *serv, const char *meth)
  290. {
  291. if (me->ordinality() > 0)
  292. {
  293. StringBuffer text;
  294. me->errorMessage(text);
  295. text.append('\n');
  296. WARNLOG("Exception(s) in %s::%s - %s", serv, meth, text.str());
  297. IArrayOf<IException>& exceptions = me->getArray();
  298. ForEachItemIn(i, exceptions)
  299. noteException(*LINK(&exceptions.item(i)));
  300. }
  301. }
  302. void SetHTTPErrorStatus(int ErrorCode,CHttpResponse* response)
  303. {
  304. switch(ErrorCode)
  305. {
  306. case 204:
  307. response->setStatus(HTTP_STATUS_NO_CONTENT);
  308. break;
  309. case 301:
  310. response->setStatus(HTTP_STATUS_MOVED_PERMANENTLY);
  311. break;
  312. case 302:
  313. response->setStatus(HTTP_STATUS_REDIRECT);
  314. break;
  315. case 303:
  316. response->setStatus(HTTP_STATUS_REDIRECT_POST);
  317. break;
  318. case 400:
  319. response->setStatus(HTTP_STATUS_BAD_REQUEST);
  320. break;
  321. case 401:
  322. response->setStatus(HTTP_STATUS_UNAUTHORIZED);
  323. break;
  324. case 403:
  325. response->setStatus(HTTP_STATUS_FORBIDDEN);
  326. break;
  327. case 404:
  328. response->setStatus(HTTP_STATUS_NOT_FOUND);
  329. break;
  330. case 405:
  331. response->setStatus(HTTP_STATUS_NOT_ALLOWED);
  332. break;
  333. case 501:
  334. response->setStatus(HTTP_STATUS_NOT_IMPLEMENTED);
  335. break;
  336. default:
  337. response->setStatus(HTTP_STATUS_INTERNAL_SERVER_ERROR);
  338. }
  339. }