ws_ecl_service.cpp 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959
  1. #include <memory>
  2. #include "daclient.hpp"
  3. #include "environment.hpp"
  4. #include "workunit.hpp"
  5. #include "wujobq.hpp"
  6. #include "fileview.hpp"
  7. #include "ws_ecl_service.hpp"
  8. #include "ws_ecl_wuinfo.hpp"
  9. #include "xsdparser.hpp"
  10. #include "httpclient.hpp"
  11. #include "xpp/XmlPullParser.h"
  12. #define SDS_LOCK_TIMEOUT (5*60*1000) // 5mins, 30s a bit short
  13. #define WSECL_ACCESS "WsEclAccess"
  14. const char *wsEclXsdTypes[] = {
  15. "xsd:string",
  16. "xsd:string",
  17. "xsd:boolean",
  18. "xsd:decimal",
  19. "xsd:float",
  20. "xsd:double",
  21. "xsd:duration",
  22. "xsd:dateTime",
  23. "xsd:time",
  24. "xsd:date",
  25. "xsd:gYearMonth",
  26. "xsd:gYear",
  27. "xsd:gMonthDay",
  28. "xsd:gDay",
  29. "xsd:gMonth",
  30. "xsd:hexBinary",
  31. "xsd:base64Binary",
  32. "xsd:anyURI",
  33. "xsd:QName",
  34. "xsd:NOTATION",
  35. "xsd:normalizedString",
  36. "xsd:token",
  37. "xsd:language",
  38. "xsd:NMTOKEN",
  39. "xsd:NMTOKENS",
  40. "xsd:Name",
  41. "xsd:NCName",
  42. "xsd:ID",
  43. "xsd:IDREF",
  44. "xsd:IDREFS",
  45. "xsd:ENTITY",
  46. "xsd:ENTITIES",
  47. "xsd:integer",
  48. "xsd:nonPositiveInteger",
  49. "xsd:negativeInteger",
  50. "xsd:long",
  51. "xsd:int",
  52. "xsd:short",
  53. "xsd:byte",
  54. "xsd:nonNegativeInteger",
  55. "xsd:unsignedLong",
  56. "xsd:unsignedInt",
  57. "xsd:unsignedShort",
  58. "xsd:unsignedByte",
  59. "xsd:positiveInteger",
  60. "tns:RawDataFile",
  61. "tns:CsvDataFile",
  62. "tns:EspStringArray",
  63. "tns:EspIntArray",
  64. "tns:XmlDataSet"
  65. };
  66. typedef MapStringTo<wsEclType> MapStringToWsEclType;
  67. int strptrcmp(char const ** l, char const ** r) { return strcmp(*l, *r); }
  68. class wsEclTypeTranslator
  69. {
  70. private:
  71. MapStringToWsEclType typemap;
  72. public:
  73. wsEclTypeTranslator();
  74. wsEclType translate(const char *type);
  75. };
  76. wsEclTypeTranslator::wsEclTypeTranslator()
  77. {
  78. typemap.setValue("xsd:string", xsdString);
  79. typemap.setValue("xsd:boolean", xsdBoolean);
  80. typemap.setValue("xsd:decimal", xsdDecimal);
  81. typemap.setValue("xsd:float", xsdFloat);
  82. typemap.setValue("xsd:double", xsdDouble);
  83. typemap.setValue("xsd:duration", xsdDuration);
  84. typemap.setValue("xsd:dateTime", xsdDateTime);
  85. typemap.setValue("xsd:time", xsdTime);
  86. typemap.setValue("xsd:date", xsdDate);
  87. typemap.setValue("xsd:gyearmonth", xsdYearMonth);
  88. typemap.setValue("xsd:gyear", xsdYear);
  89. typemap.setValue("xsd:gmonthday", xsdMonthDay);
  90. typemap.setValue("xsd:gday", xsdDay);
  91. typemap.setValue("xsd:gmonth", xsdMonth);
  92. typemap.setValue("xsd:hexbinary", xsdHexBinary);
  93. typemap.setValue("xsd:base64binary", xsdBase64Binary);
  94. typemap.setValue("xsd:anyuri", xsdAnyURI);
  95. typemap.setValue("xsd:qname", xsdQName);
  96. typemap.setValue("xsd:notation", xsdNOTATION);
  97. typemap.setValue("xsd:normalizedstring", xsdNormalizedString);
  98. typemap.setValue("xsd:token", xsdToken);
  99. typemap.setValue("xsd:language", xsdLanguage);
  100. typemap.setValue("xsd:nmtoken", xsdNMTOKEN);
  101. typemap.setValue("xsd:nmtokens", xsdNMTOKENS);
  102. typemap.setValue("xsd:name", xsdName);
  103. typemap.setValue("xsd:ncname", xsdNCName);
  104. typemap.setValue("xsd:id", xsdID);
  105. typemap.setValue("xsd:idref", xsdIDREF);
  106. typemap.setValue("xsd:idrefs", xsdIDREFS);
  107. typemap.setValue("xsd:entity", xsdENTITY);
  108. typemap.setValue("xsd:entities", xsdENTITIES);
  109. typemap.setValue("xsd:integer", xsdInteger);
  110. typemap.setValue("xsd:nonpositiveinteger", xsdNonPositiveInteger);
  111. typemap.setValue("xsd:negativeinteger", xsdNegativeInteger);
  112. typemap.setValue("xsd:long", xsdLong);
  113. typemap.setValue("xsd:int", xsdInt);
  114. typemap.setValue("xsd:short", xsdShort);
  115. typemap.setValue("xsd:byte", xsdByte);
  116. typemap.setValue("xsd:nonnegativeinteger", xsdNonNegativeInteger);
  117. typemap.setValue("xsd:unsignedlong", xsdUnsignedLong);
  118. typemap.setValue("xsd:unsignedint", xsdUnsignedInt);
  119. typemap.setValue("xsd:unsignedshort", xsdUnsignedShort);
  120. typemap.setValue("xsd:unsignedbyte", xsdUnsignedByte);
  121. typemap.setValue("xsd:positiveinteger", xsdPositiveInteger);
  122. typemap.setValue("tns:rawdatafile", tnsRawDataFile);
  123. typemap.setValue("tns:csvdatafile", tnsCsvDataFile);
  124. typemap.setValue("tns:espstringarray", tnsEspStringArray);
  125. typemap.setValue("tns:espintarray", tnsEspIntArray);
  126. typemap.setValue("tns:xmldataset", tnsXmlDataSet);
  127. }
  128. wsEclType wsEclTypeTranslator::translate(const char *type)
  129. {
  130. if (!type || !*type)
  131. return wsEclTypeUnknown;
  132. StringBuffer value(type);
  133. wsEclType *ret = typemap.getValue(value.toLowerCase().str());
  134. return (ret) ? *ret : wsEclTypeUnknown;
  135. }
  136. const char *wsEclToXsdTypes(wsEclType from);
  137. static wsEclTypeTranslator *translator = NULL;
  138. const char *wsEclToXsdTypes(wsEclType from)
  139. {
  140. if (from < maxWsEclType)
  141. return wsEclXsdTypes[from];
  142. return wsEclXsdTypes[wsEclTypeUnknown];
  143. }
  144. const char *translateXsdType(const char *from)
  145. {
  146. return wsEclToXsdTypes(translator->translate(from));
  147. }
  148. // Interestingly, only single quote needs to HTML escape.
  149. // ", <, >, & don't need escape.
  150. static void escapeSingleQuote(StringBuffer& src, StringBuffer& escaped)
  151. {
  152. for (const char* p = src.str(); *p!=0; p++)
  153. {
  154. if (*p == '\'')
  155. escaped.append("&apos;");
  156. else
  157. escaped.append(*p);
  158. }
  159. }
  160. static void appendServerAddress(StringBuffer &s, IPropertyTree &env, IPropertyTree &server, const char *daliAddress)
  161. {
  162. const char *port = server.queryProp("@port");
  163. if (port && streq(port, "0")) //roxie on demand
  164. return;
  165. const char *netAddress = server.queryProp("@netAddress");
  166. if (!netAddress && server.hasProp("@computer"))
  167. {
  168. VStringBuffer xpath("Hardware/Computer[@name='%s']/@netAddress", server.queryProp("@computer"));
  169. netAddress = env.queryProp(xpath.str());
  170. }
  171. if ((!netAddress || *netAddress=='.') && daliAddress && *daliAddress)
  172. netAddress = daliAddress;
  173. if (!netAddress || !*netAddress)
  174. return;
  175. if (s.length())
  176. s.append('|');
  177. s.append(netAddress).append(':').append(port ? port : "9876");
  178. }
  179. bool CWsEclService::init(const char * name, const char * type, IPropertyTree * cfg, const char * process)
  180. {
  181. StringBuffer xpath;
  182. xpath.appendf("Software/EspProcess[@name='%s']", process);
  183. IPropertyTree *prc = cfg->queryPropTree(xpath.str());
  184. if (!prc)
  185. throw MakeStringException(-1, "ESP Process %s not configured", process);
  186. auth_method.set(prc->queryProp("Authentication/@method"));
  187. portal_URL.set(prc->queryProp("@protalurl"));
  188. StringBuffer daliAddress;
  189. const char *daliServers = prc->queryProp("@daliServers");
  190. if (daliServers)
  191. {
  192. while (*daliServers && !strchr(":;,", *daliServers))
  193. daliAddress.append(*daliServers++);
  194. }
  195. Owned<IEnvironmentFactory> factory = getEnvironmentFactory();
  196. Owned<IConstEnvironment> environment = factory->openEnvironmentByFile();
  197. Owned<IPropertyTree> pRoot = &environment->getPTree();
  198. xpath.clear().appendf("EspService[@name='%s']/VIPS", name);
  199. IPropertyTree *vips = prc->queryPropTree(xpath.str());
  200. Owned<IPropertyTreeIterator> it = pRoot->getElements("Software/RoxieCluster");
  201. ForEach(*it)
  202. {
  203. const char *name = it->query().queryProp("@name");
  204. if (connMap.getValue(name)) //bad config?
  205. continue;
  206. bool loadBalanced = false;
  207. StringBuffer list;
  208. const char *vip = NULL;
  209. if (vips)
  210. vip = vips->queryProp(xpath.clear().appendf("ProcessCluster[@name='%s']/@vip", name).str());
  211. if (vip && *vip)
  212. {
  213. list.append(vip);
  214. loadBalanced = true;
  215. }
  216. else
  217. {
  218. Owned<IPropertyTreeIterator> servers = it->query().getElements("RoxieServerProcess");
  219. ForEach(*servers)
  220. appendServerAddress(list, *pRoot, servers->query(), daliAddress.str());
  221. }
  222. if (list.length())
  223. {
  224. Owned<ISmartSocketFactory> sf = createSmartSocketFactory(list.str(), !loadBalanced);
  225. connMap.setValue(name, sf.get());
  226. }
  227. }
  228. translator = new wsEclTypeTranslator();
  229. return true;
  230. }
  231. CWsEclService::~CWsEclService()
  232. {
  233. if (translator)
  234. delete translator;
  235. }
  236. void CWsEclBinding::getNavigationData(IEspContext &context, IPropertyTree & data)
  237. {
  238. DBGLOG("CScrubbedXmlBinding::getNavigationData");
  239. StringArray wsModules;
  240. StringBuffer mode;
  241. data.addProp("@viewType", "wsecl_tree");
  242. data.addProp("@action", "NavMenuEvent");
  243. data.addProp("@appName", "WsECL 3.0");
  244. ensureNavDynFolder(data, "Targets", "Targets", "root=true", NULL);
  245. }
  246. IPropertyTree * getQueryRegistries()
  247. {
  248. Owned<IRemoteConnection> conn = querySDS().connect("/QuerySets/", myProcessSession(), RTM_LOCK_READ|RTM_CREATE_QUERY, SDS_LOCK_TIMEOUT);
  249. return conn->getRoot();
  250. }
  251. void CWsEclBinding::getRootNavigationFolders(IEspContext &context, IPropertyTree & data)
  252. {
  253. DBGLOG("CScrubbedXmlBinding::getNavigationData");
  254. StringArray wsModules;
  255. StringBuffer mode;
  256. data.addProp("@viewType", "wsecl_tree");
  257. data.addProp("@action", "NavMenuEvent");
  258. data.addProp("@appName", "WsECL 3.0");
  259. Owned<IStringIterator> targets = getTargetClusters(NULL, NULL);
  260. SCMStringBuffer target;
  261. ForEach(*targets)
  262. {
  263. VStringBuffer parms("queryset=%s", targets->str(target).str());
  264. ensureNavDynFolder(data, target.str(), target.str(), parms.str(), NULL);
  265. }
  266. }
  267. void CWsEclBinding::addQueryNavLink(IPropertyTree &data, IPropertyTree *query, const char *setname, const char *qname)
  268. {
  269. if (!query)
  270. return;
  271. if (query->getPropBool("@suspended"))
  272. return;
  273. if (!qname || !*qname)
  274. qname = query->queryProp("@id");
  275. if (!setname || !*setname || !qname || !*qname)
  276. return;
  277. StringBuffer navPath;
  278. navPath.appendf("/WsEcl/tabview/query/%s/%s", setname, qname);
  279. ensureNavLink(data, qname, navPath.str(), qname, "menu2", navPath.str());
  280. }
  281. void CWsEclBinding::getQueryNames(IPropertyTree* settree, const char *id, const char *qname, StringArray& qnames)
  282. {
  283. if (!id || !*id)
  284. return;
  285. VStringBuffer xpath("Query[@id='%s']", id);
  286. IPropertyTree *query = settree->queryPropTree(xpath.str());
  287. if (query->getPropBool("@isLibrary") || query->getPropBool("@suspended"))
  288. return;
  289. if (!qname || !*qname)
  290. qname = query->queryProp("@id");
  291. if (!qname || !*qname)
  292. return;
  293. qnames.append(qname);
  294. }
  295. void CWsEclBinding::getDynNavData(IEspContext &context, IProperties *params, IPropertyTree & data)
  296. {
  297. if (!params)
  298. return;
  299. data.setPropBool("@volatile", true);
  300. if (params->getPropBool("root", false))
  301. {
  302. getRootNavigationFolders(context, data);
  303. }
  304. else if (params->hasProp("queryset"))
  305. {
  306. const char *setname = params->queryProp("queryset");
  307. if (!setname || !*setname)
  308. return;
  309. Owned<IPropertyTree> settree = getQueryRegistry(setname, true);
  310. if (params->hasProp("QueryList"))
  311. {
  312. Owned<IPropertyTreeIterator> iter = settree->getElements("Query");
  313. ForEach(*iter)
  314. {
  315. if (!iter->query().getPropBool("@isLibrary"))
  316. addQueryNavLink(data, &iter->query(), setname);
  317. }
  318. }
  319. else
  320. {
  321. StringArray qnames;
  322. Owned<IPropertyTreeIterator> iter = settree->getElements("Alias");
  323. ForEach(*iter)
  324. {
  325. IPropertyTree &alias = iter->query();
  326. getQueryNames(settree, alias.queryProp("@id"), alias.queryProp("@name"), qnames);
  327. }
  328. if (qnames.ordinality())
  329. {
  330. qnames.sort(strptrcmp);
  331. ForEachItemIn(i,qnames)
  332. {
  333. StringBuffer navPath;
  334. const char *qname = qnames.item(i);
  335. navPath.appendf("/WsEcl/tabview/query/%s/%s", setname, qname);
  336. ensureNavLink(data, qname, navPath.str(), qname, "menu2", navPath.str());
  337. }
  338. }
  339. }
  340. }
  341. }
  342. static inline bool isPathSeparator(char sep)
  343. {
  344. return (sep=='\\')||(sep=='/');
  345. }
  346. static inline const char *skipPathNodes(const char *&s, int skip)
  347. {
  348. if (s) {
  349. while (*s) {
  350. if (isPathSeparator(*s++))
  351. if (!skip--)
  352. return s;
  353. }
  354. }
  355. return NULL;
  356. }
  357. static inline const char *nextPathNode(const char *&s, StringBuffer &node, int skip=0)
  358. {
  359. if (skip)
  360. skipPathNodes(s, skip);
  361. if (s) while (*s) {
  362. if (isPathSeparator(*s))
  363. return s++;
  364. node.append(*s++);
  365. }
  366. return NULL;
  367. }
  368. static inline const char *firstPathNode(const char *&s, StringBuffer &node)
  369. {
  370. if (s && isPathSeparator(*s))
  371. s++;
  372. return nextPathNode(s, node);
  373. }
  374. static void splitPathTailAndExt(const char *s, StringBuffer &path, StringBuffer &tail, StringBuffer *ext)
  375. {
  376. if (s)
  377. {
  378. const char *finger=s;
  379. while (*finger++);
  380. const char *extpos=finger;
  381. const char *tailpos=s;
  382. while (s!=finger && tailpos==s)
  383. {
  384. switch (*finger)
  385. {
  386. case '.':
  387. if (ext && *extpos!='.')
  388. extpos=finger;
  389. break;
  390. case '/':
  391. case '\\':
  392. tailpos=finger;
  393. break;
  394. }
  395. finger--;
  396. }
  397. if (ext && *extpos=='.')
  398. ext->append(extpos+1);
  399. if (tailpos!=s)
  400. path.append(tailpos - s, s);
  401. if (strchr("\\/", *tailpos))
  402. tailpos++;
  403. tail.append(extpos - tailpos, tailpos);
  404. }
  405. }
  406. static void splitLookupInfo(IProperties *parms, const char *&s, StringBuffer &wuid, StringBuffer &qs, StringBuffer &qid)
  407. {
  408. StringBuffer lookup;
  409. nextPathNode(s, lookup);
  410. if (strieq(lookup.str(), "query"))
  411. {
  412. nextPathNode(s, qs);
  413. nextPathNode(s, qid);
  414. }
  415. else if (strieq(lookup.str(), "wuid"))
  416. {
  417. nextPathNode(s, wuid);
  418. qs.append(parms->queryProp("qset"));
  419. qid.append(parms->queryProp("qname"));
  420. }
  421. }
  422. void CWsEclBinding::xsltTransform(const char* xml, unsigned int len, const char* xslFileName, IProperties *params, StringBuffer& ret)
  423. {
  424. Owned<IXslProcessor> proc = getXslProcessor();
  425. Owned<IXslTransform> trans = proc->createXslTransform();
  426. trans->setXmlSource(xml, len);
  427. StringBuffer xslpath(getCFD());
  428. xslpath.append(xslFileName);
  429. trans->loadXslFromFile(xslpath.str());
  430. if (params)
  431. {
  432. Owned<IPropertyIterator> it = params->getIterator();
  433. for (it->first(); it->isValid(); it->next())
  434. {
  435. const char *key = it->getPropKey();
  436. //set parameter in the XSL transform skipping over the @ prefix, if any
  437. const char* paramName = *key == '@' ? key+1 : key;
  438. trans->setParameter(paramName, StringBuffer().append('\'').append(params->queryProp(key)).append('\'').str());
  439. }
  440. }
  441. trans->transform(ret);
  442. }
  443. StringBuffer &CWsEclBinding::generateNamespace(IEspContext &context, CHttpRequest* request, const char *serv, const char *method, StringBuffer &ns)
  444. {
  445. ns.append("urn:hpccsystems:ecl:");
  446. if (method && *method)
  447. ns.appendLower(strlen(method), method);
  448. return ns;
  449. }
  450. #define REQXML_ROOT 0x0001
  451. #define REQXML_SAMPLE_DATA 0x0002
  452. #define REQXML_TRIM 0x0004
  453. #define REQXML_ESCAPEFORMATTERS 0x0008
  454. static void buildReqXml(StringStack& parent, IXmlType* type, StringBuffer& out, const char* tag, IPropertyTree *parmtree, unsigned flags, const char* ns=NULL)
  455. {
  456. assertex(type!=NULL);
  457. if (!parmtree && (flags & REQXML_TRIM) && !(flags & REQXML_ROOT))
  458. return;
  459. const char* typeName = type->queryName();
  460. if (type->isComplexType())
  461. {
  462. if (typeName && std::find(parent.begin(),parent.end(),typeName) != parent.end())
  463. return; // recursive
  464. int startlen = out.length();
  465. out.appendf("<%s", tag);
  466. if (ns)
  467. out.append(' ').append(ns);
  468. int taglen=out.length()+1;
  469. for (size_t i=0; i<type->getAttrCount(); i++)
  470. {
  471. IXmlAttribute* attr = type->queryAttr(i);
  472. if (parmtree)
  473. {
  474. StringBuffer attrpath("@");
  475. const char *attrval = parmtree->queryProp(attrpath.append(attr->queryName()).str());
  476. if (attrval)
  477. out.appendf(" %s='", attr->queryName()).append(attrval);
  478. }
  479. else
  480. {
  481. out.appendf(" %s='", attr->queryName());
  482. attr->getSampleValue(out);
  483. }
  484. out.append('\'');
  485. }
  486. out.append('>');
  487. if (typeName)
  488. parent.push_back(typeName);
  489. int flds = type->getFieldCount();
  490. switch (type->getSubType())
  491. {
  492. case SubType_Complex_SimpleContent:
  493. assertex(flds==0);
  494. if (parmtree)
  495. {
  496. const char *attrval = parmtree->queryProp(NULL);
  497. if (attrval)
  498. out.append(attrval);
  499. }
  500. else if (flags & REQXML_SAMPLE_DATA)
  501. type->queryFieldType(0)->getSampleValue(out,tag);
  502. break;
  503. default:
  504. for (int idx=0; idx<flds; idx++)
  505. {
  506. IPropertyTree *childtree = NULL;
  507. const char *childname = type->queryFieldName(idx);
  508. if (parmtree)
  509. childtree = parmtree->queryPropTree(childname);
  510. buildReqXml(parent,type->queryFieldType(idx), out, childname, childtree, flags & ~REQXML_ROOT);
  511. }
  512. break;
  513. }
  514. if (typeName)
  515. parent.pop_back();
  516. if ((flags & REQXML_TRIM) && !(flags & REQXML_ROOT) && out.length()==taglen)
  517. out.setLength(startlen);
  518. else
  519. out.appendf("</%s>",tag);
  520. }
  521. else if (type->isArray())
  522. {
  523. if (typeName && std::find(parent.begin(),parent.end(),typeName) != parent.end())
  524. return; // recursive
  525. const char* itemName = type->queryFieldName(0);
  526. IXmlType* itemType = type->queryFieldType(0);
  527. if (!itemName || !itemType)
  528. throw MakeStringException(-1,"*** Invalid array definition: tag=%s, itemName=%s", tag, itemName?itemName:"NULL");
  529. if (typeName)
  530. parent.push_back(typeName);
  531. int startlen = out.length();
  532. out.appendf("<%s", tag);
  533. if (ns)
  534. out.append(' ').append(ns);
  535. out.append(">");
  536. int taglen=out.length();
  537. if (parmtree)
  538. {
  539. VStringBuffer countpath("%s/itemcount", itemName);
  540. const char *countstr=parmtree->queryProp(countpath.str());
  541. if (countstr && *countstr)
  542. {
  543. int count = atoi(countstr);
  544. for (int idx=0; idx<count; idx++)
  545. {
  546. StringBuffer itempath;
  547. itempath.append(itemName).append(idx);
  548. IPropertyTree *itemtree = parmtree->queryPropTree(itempath.str());
  549. if (itemtree)
  550. buildReqXml(parent,itemType,out,itemName, itemtree, flags & ~REQXML_ROOT);
  551. }
  552. }
  553. else if (parmtree->hasProp(itemName))
  554. {
  555. Owned<IPropertyTreeIterator> items = parmtree->getElements(itemName);
  556. ForEach(*items)
  557. buildReqXml(parent,itemType,out,itemName, &items->query(), flags & ~REQXML_ROOT);
  558. }
  559. else
  560. {
  561. const char *s = parmtree->queryProp(NULL);
  562. if (s && *s)
  563. {
  564. StringArray items;
  565. items.appendList(s, "\n");
  566. ForEachItemIn(i, items)
  567. appendXMLTag(out, itemName, items.item(i));
  568. }
  569. }
  570. }
  571. else
  572. buildReqXml(parent,itemType,out,itemName, NULL, flags & ~REQXML_ROOT);
  573. if (typeName)
  574. parent.pop_back();
  575. if ((flags & REQXML_TRIM) && !(flags & REQXML_ROOT) && out.length()==taglen)
  576. out.setLength(startlen);
  577. else
  578. out.appendf("</%s>",tag);
  579. }
  580. else // simple type
  581. {
  582. StringBuffer parmval;
  583. if (parmtree)
  584. parmval.append(parmtree->queryProp(NULL));
  585. if (!parmval.length() && (flags & REQXML_SAMPLE_DATA))
  586. type->getSampleValue(parmval, NULL);
  587. if (parmval.length() || !(flags&REQXML_TRIM))
  588. {
  589. if (strieq(typeName, "boolean"))
  590. {
  591. if (!strieq(parmval, "default"))
  592. {
  593. out.appendf("<%s>", tag);
  594. if (parmval.length())
  595. out.append((strieq(parmval.str(),"1")||strieq(parmval.str(),"true")||strieq(parmval.str(), "on")) ? '1' : '0');
  596. out.appendf("</%s>", tag);
  597. }
  598. }
  599. else
  600. {
  601. out.appendf("<%s>", tag);
  602. out.append(parmval);
  603. out.appendf("</%s>", tag);
  604. }
  605. }
  606. }
  607. }
  608. inline void indenter(StringBuffer &s, int count)
  609. {
  610. s.appendN(count*3, ' ');
  611. }
  612. IException *MakeJSONValueException(int code, const char *start, const char *pos, const char *tail, const char *intro="Invalid json format: ")
  613. {
  614. StringBuffer s(intro);
  615. s.append(pos-start, start).append('^').append(pos);
  616. if (tail && *tail)
  617. s.append(" - ").append(tail);
  618. return MakeStringException(code, "%s", s.str());
  619. }
  620. inline StringBuffer &jsonNumericNext(StringBuffer &s, const char *&c, bool &allowDecimal, bool &allowExponent, const char *start)
  621. {
  622. if (isdigit(*c))
  623. s.append(*c++);
  624. else if ('.'==*c)
  625. {
  626. if (!allowDecimal || !allowExponent)
  627. throw MakeJSONValueException(-1, start, c, "Unexpected decimal");
  628. allowDecimal=false;
  629. s.append(*c++);
  630. }
  631. else if ('e'==*c || 'E'==*c)
  632. {
  633. if (!allowExponent)
  634. throw MakeJSONValueException(-1, start, c, "Unexpected exponent");
  635. allowDecimal=false;
  636. allowExponent=false;
  637. s.append(*c++);
  638. if ('-'==*c || '+'==*c)
  639. s.append(*c++);
  640. if (!isdigit(*c))
  641. throw MakeJSONValueException(-1, start, c, "Unexpected token");
  642. }
  643. else
  644. throw MakeJSONValueException(-1, start, c, "Unexpected token");
  645. return s;
  646. }
  647. inline StringBuffer &jsonNumericStart(StringBuffer &s, const char *&c, const char *start)
  648. {
  649. if ('-'==*c)
  650. return jsonNumericStart(s.append(*c++), c, start);
  651. else if ('0'==*c)
  652. {
  653. s.append(*c++);
  654. if (*c && '.'!=*c)
  655. throw MakeJSONValueException(-1, start, c, "Unexpected token");
  656. }
  657. else if (isdigit(*c))
  658. s.append(*c++);
  659. else
  660. throw MakeJSONValueException(-1, start, c, "Unexpected token");
  661. return s;
  662. }
  663. StringBuffer &appendJSONNumericString(StringBuffer &s, const char *value, bool allowDecimal)
  664. {
  665. if (!value || !*value)
  666. return s.append("null");
  667. bool allowExponent = allowDecimal;
  668. const char *pos = value;
  669. jsonNumericStart(s, pos, value);
  670. while (*pos)
  671. jsonNumericNext(s, pos, allowDecimal, allowExponent, value);
  672. return s;
  673. }
  674. inline const char *jsonNewline(unsigned flags){return ((flags & REQXML_ESCAPEFORMATTERS) ? "\\n" : "\n");}
  675. static void buildJsonMsg(StringStack& parent, IXmlType* type, StringBuffer& out, const char* tag, IPropertyTree *parmtree, unsigned flags, int &indent)
  676. {
  677. assertex(type!=NULL);
  678. if (flags & REQXML_ROOT)
  679. {
  680. out.append("{");
  681. out.append(jsonNewline(flags));
  682. indent++;
  683. }
  684. const char* typeName = type->queryName();
  685. if (type->isComplexType())
  686. {
  687. if (typeName && std::find(parent.begin(),parent.end(),typeName) != parent.end())
  688. return; // recursive
  689. int startlen = out.length();
  690. indenter(out, indent++);
  691. if (tag)
  692. out.appendf("\"%s\": {", tag).append(jsonNewline(flags));
  693. else
  694. out.append("{").append(jsonNewline(flags));
  695. int taglen=out.length()+1;
  696. if (typeName)
  697. parent.push_back(typeName);
  698. if (type->getSubType()==SubType_Complex_SimpleContent)
  699. {
  700. if (parmtree)
  701. {
  702. const char *attrval = parmtree->queryProp(NULL);
  703. indenter(out, indent);
  704. out.appendf("\"%s\" ", (attrval) ? attrval : "");
  705. }
  706. else if (flags & REQXML_SAMPLE_DATA)
  707. {
  708. indenter(out, indent);
  709. out.append("\"");
  710. type->queryFieldType(0)->getSampleValue(out,tag);
  711. out.append("\" ");
  712. }
  713. }
  714. else
  715. {
  716. bool first=true;
  717. int flds = type->getFieldCount();
  718. for (int idx=0; idx<flds; idx++)
  719. {
  720. if (first)
  721. first=false;
  722. else
  723. out.append(",").append(jsonNewline(flags));
  724. IPropertyTree *childtree = NULL;
  725. const char *childname = type->queryFieldName(idx);
  726. if (parmtree)
  727. childtree = parmtree->queryPropTree(childname);
  728. buildJsonMsg(parent, type->queryFieldType(idx), out, childname, childtree, flags & ~REQXML_ROOT, indent);
  729. }
  730. out.append(jsonNewline(flags));
  731. }
  732. if (typeName)
  733. parent.pop_back();
  734. indenter(out, indent--);
  735. out.append("}");
  736. }
  737. else if (type->isArray())
  738. {
  739. if (typeName && std::find(parent.begin(),parent.end(),typeName) != parent.end())
  740. return; // recursive
  741. const char* itemName = type->queryFieldName(0);
  742. IXmlType* itemType = type->queryFieldType(0);
  743. if (!itemName || !itemType)
  744. throw MakeStringException(-1,"*** Invalid array definition: tag=%s, itemName=%s", tag, itemName?itemName:"NULL");
  745. if (typeName)
  746. parent.push_back(typeName);
  747. int startlen = out.length();
  748. indenter(out, indent++);
  749. if (tag)
  750. out.appendf("\"%s\": {%s", tag, jsonNewline(flags));
  751. else
  752. out.append("{").append(jsonNewline(flags));
  753. indenter(out, indent++);
  754. out.appendf("\"%s\": [", itemName).append(jsonNewline(flags));
  755. indent++;
  756. int taglen=out.length();
  757. if (parmtree)
  758. {
  759. VStringBuffer countpath("%s/itemcount", itemName);
  760. const char *countstr=parmtree->queryProp(countpath.str());
  761. if (countstr && *countstr)
  762. {
  763. bool first=true;
  764. int count = atoi(countstr);
  765. for (int idx=0; idx<count; idx++)
  766. {
  767. if (first)
  768. first=false;
  769. else
  770. out.append(",").append(jsonNewline(flags));
  771. StringBuffer itempath;
  772. itempath.append(itemName).append(idx);
  773. IPropertyTree *itemtree = parmtree->queryPropTree(itempath.str());
  774. if (itemtree)
  775. buildJsonMsg(parent,itemType,out, NULL, itemtree, flags & ~REQXML_ROOT, indent);
  776. }
  777. out.append(jsonNewline(flags));
  778. }
  779. else
  780. {
  781. Owned<IPropertyTreeIterator> items = parmtree->getElements(itemName);
  782. bool first=true;
  783. ForEach(*items)
  784. {
  785. if (first)
  786. first=false;
  787. else
  788. out.append(",").append(jsonNewline(flags));
  789. buildJsonMsg(parent,itemType,out, NULL, &items->query(), flags & ~REQXML_ROOT, indent);
  790. }
  791. out.append(jsonNewline(flags));
  792. }
  793. }
  794. else
  795. buildJsonMsg(parent, itemType, out, NULL, NULL, flags & ~REQXML_ROOT, indent);
  796. indenter(out, indent--);
  797. out.append("]").append(jsonNewline(flags));
  798. if (typeName)
  799. parent.pop_back();
  800. indenter(out, indent--);
  801. out.append("}");
  802. }
  803. else // simple type
  804. {
  805. const char *parmval = (parmtree) ? parmtree->queryProp(NULL) : NULL;
  806. indenter(out, indent);
  807. out.appendf("\"%s\": ", tag);
  808. if (parmval)
  809. {
  810. const char *tname = type->queryName();
  811. //TBD: HACK
  812. if (!strnicmp(tname, "real", 4) ||
  813. !strnicmp(tname, "dec", 3) ||
  814. !strnicmp(tname, "double", 6) ||
  815. !strnicmp(tname, "float", 5))
  816. appendJSONNumericString(out, parmval, true);
  817. else if (!strnicmp(tname, "int", 3))
  818. appendJSONNumericString(out, parmval, false);
  819. else if (!strnicmp(tname, "bool", 4))
  820. appendJSONValue(out, NULL, (bool)('1'==*parmval || strieq(parmval, "true")));
  821. else
  822. appendJSONValue(out, NULL, parmval);
  823. }
  824. else if (flags & REQXML_SAMPLE_DATA)
  825. {
  826. out.append('\"');
  827. type->getSampleValue(out,NULL);
  828. out.append('\"');
  829. }
  830. else
  831. out.append("null");
  832. }
  833. if (flags & REQXML_ROOT)
  834. out.append(jsonNewline(flags)).append("}");
  835. }
  836. static inline StringBuffer &appendNamespaceSpecificString(StringBuffer &dest, const char *src)
  837. {
  838. if (src)
  839. while(*src){
  840. dest.append((const char)(isspace(*src) ? '_' : tolower(*src)));
  841. src++;
  842. }
  843. return dest;
  844. }
  845. void buildSampleDataset(StringBuffer &xml, IPropertyTree *xsdtree, const char *service, const char *method, const char *resultname)
  846. {
  847. StringBuffer schemaXml;
  848. toXML(xsdtree, schemaXml);
  849. Owned<IXmlSchema> schema = createXmlSchemaFromString(schemaXml);
  850. if (schema.get())
  851. {
  852. IXmlType* type = schema->queryElementType("Dataset");
  853. if (type)
  854. {
  855. StringBuffer ns("xmlns=\"urn:hpccsystems:ecl:");
  856. appendNamespaceSpecificString(ns, method).append(":result:");
  857. appendNamespaceSpecificString(ns, resultname);
  858. ns.append('\"');
  859. StringStack parent;
  860. buildReqXml(parent, type, xml, "Dataset", NULL, REQXML_SAMPLE_DATA, ns.str());
  861. }
  862. }
  863. }
  864. void CWsEclBinding::buildSampleResponseXml(StringBuffer& msg, IEspContext &context, CHttpRequest* request, WsEclWuInfo &wsinfo)
  865. {
  866. StringBuffer element;
  867. element.append(wsinfo.queryname.sget()).append("Response");
  868. StringBuffer xsds;
  869. wsinfo.getSchemas(xsds);
  870. msg.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  871. if (context.queryRequestParameters()->hasProp("display"))
  872. msg.append("<?xml-stylesheet type=\"text/xsl\" href=\"/esp/xslt/xmlformatter.xsl\"?>");
  873. msg.append('<').append(element.str()).append(" xmlns=\"urn:hpccsystems:ecl:");
  874. msg.appendLower(wsinfo.queryname.length(), wsinfo.queryname.sget()).append("\">");
  875. msg.append("<Results><Result>");
  876. Owned<IPropertyTree> xsds_tree;
  877. if (xsds.length())
  878. xsds_tree.setown(createPTreeFromXMLString(xsds.str()));
  879. if (xsds_tree)
  880. {
  881. Owned<IPropertyTreeIterator> result_xsds =xsds_tree->getElements("Result");
  882. ForEach (*result_xsds)
  883. buildSampleDataset(msg, result_xsds->query().queryPropTree("xs:schema"), wsinfo.qsetname.sget(), wsinfo.queryname.sget(), result_xsds->query().queryProp("@name"));
  884. }
  885. msg.append("</Result></Results>");
  886. msg.append("</").append(element.str()).append('>');
  887. }
  888. int CWsEclBinding::getWsEclLinks(IEspContext &context, CHttpRequest* request, CHttpResponse* response, WsEclWuInfo &wsinfo)
  889. {
  890. StringBuffer xml;
  891. xml.append("<links>");
  892. xml.append("<version>3</version>");
  893. xml.append("<path>").append(wsinfo.qsetname.sget()).append("</path>");
  894. xml.append("<query>").append(wsinfo.queryname.sget()).append("</query>");
  895. StringBuffer xsds;
  896. wsinfo.getSchemas(xsds);
  897. Owned<IPropertyTree> xsdtree;
  898. if (xsds.length())
  899. xsdtree.setown(createPTreeFromXMLString(xsds.str()));
  900. if (xsdtree)
  901. {
  902. xml.append("<input_datasets>");
  903. Owned<IPropertyTreeIterator> input_xsds =xsdtree->getElements("Input");
  904. ForEach (*input_xsds)
  905. {
  906. xml.append("<dataset>");
  907. xml.append("<name>").append(input_xsds->query().queryProp("@sname")).append("</name>");
  908. xml.append("</dataset>");
  909. }
  910. xml.append("</input_datasets>");
  911. xml.append("<result_datasets>");
  912. Owned<IPropertyTreeIterator> result_xsds =xsdtree->getElements("Result");
  913. ForEach (*result_xsds)
  914. {
  915. xml.append("<dataset>");
  916. xml.append("<name>").append(result_xsds->query().queryProp("@sname")).append("</name>");
  917. xml.append("</dataset>");
  918. }
  919. xml.append("</result_datasets>");
  920. }
  921. xml.append("</links>");
  922. Owned<IXslProcessor> xslp = getXslProcessor();
  923. Owned<IXslTransform> xform = xslp->createXslTransform();
  924. xform->loadXslFromFile(StringBuffer(getCFD()).append("./xslt/wsecl3_links.xslt").str());
  925. xform->setXmlSource(xml.str(), xml.length());
  926. StringBuffer page;
  927. xform->transform(page);
  928. response->setContent(page);
  929. response->setContentType("text/html; charset=UTF-8");
  930. response->setStatus(HTTP_STATUS_OK);
  931. response->send();
  932. return 0;
  933. }
  934. int CWsEclBinding::getWsEcl2TabView(CHttpRequest* request, CHttpResponse* response, const char *thepath)
  935. {
  936. IEspContext *context = request->queryContext();
  937. IProperties *parms = request->queryParameters();
  938. StringBuffer wuid;
  939. StringBuffer qs;
  940. StringBuffer qid;
  941. splitLookupInfo(request->queryParameters(), thepath, wuid, qs, qid);
  942. WsEclWuInfo wsinfo(wuid.str(), qs.str(), qid.str(), context->queryUserId(), context->queryPassword());
  943. StringBuffer xml;
  944. xml.append("<tabview>");
  945. xml.append("<version>3</version>");
  946. xml.appendf("<wuid>%s</wuid>", wsinfo.wuid.sget());
  947. xml.appendf("<qset>%s</qset>", wsinfo.qsetname.sget());
  948. xml.appendf("<qname>%s</qname>", wsinfo.queryname.sget());
  949. StringBuffer xsds;
  950. wsinfo.getSchemas(xsds);
  951. Owned<IPropertyTree> xsdtree;
  952. if (xsds.length())
  953. xsdtree.setown(createPTreeFromXMLString(xsds.str()));
  954. if (xsdtree)
  955. {
  956. xml.append("<input_datasets>");
  957. Owned<IPropertyTreeIterator> input_xsds =xsdtree->getElements("Input");
  958. ForEach (*input_xsds)
  959. {
  960. xml.append("<dataset>");
  961. xml.append("<name>").append(input_xsds->query().queryProp("@name")).append("</name>");
  962. xml.append("</dataset>");
  963. }
  964. xml.append("</input_datasets>");
  965. xml.append("<result_datasets>");
  966. Owned<IPropertyTreeIterator> result_xsds =xsdtree->getElements("Result");
  967. ForEach (*result_xsds)
  968. {
  969. xml.append("<dataset>");
  970. xml.append("<name>").append(result_xsds->query().queryProp("@name")).append("</name>");
  971. xml.append("</dataset>");
  972. }
  973. xml.append("</result_datasets>");
  974. }
  975. xml.append("</tabview>");
  976. StringBuffer html;
  977. xsltTransform(xml.str(), xml.length(), "./xslt/wsecl3_tabview.xsl", NULL, html);
  978. response->setStatus("200 OK");
  979. response->setContent(html.str());
  980. response->setContentType("text/html");
  981. response->send();
  982. return 0;
  983. }
  984. void CWsEclBinding::appendSchemaNamespaces(IPropertyTree *namespaces, IEspContext &ctx, CHttpRequest* req, const char *service, const char *method)
  985. {
  986. WsEclWuInfo *wsinfo = (WsEclWuInfo *) ctx.getBindingValue();
  987. if (wsinfo)
  988. appendSchemaNamespaces(namespaces, ctx, req, *wsinfo);
  989. }
  990. void CWsEclBinding::appendSchemaNamespaces(IPropertyTree *namespaces, IEspContext &ctx, CHttpRequest* req, WsEclWuInfo &wsinfo)
  991. {
  992. IProperties *parms = ctx.queryRequestParameters();
  993. StringBuffer xsds;
  994. wsinfo.getSchemas(xsds);
  995. Owned<IPropertyTree> xsdtree;
  996. if (xsds.length())
  997. xsdtree.setown(createPTreeFromXMLString(xsds.str()));
  998. if (xsdtree)
  999. {
  1000. Owned<IPropertyTreeIterator> result_xsds =xsdtree->getElements("Result");
  1001. int count=1;
  1002. ForEach (*result_xsds)
  1003. {
  1004. const char *resultname = result_xsds->query().queryProp("@sname");
  1005. StringBuffer urn("urn:hpccsystems:ecl:");
  1006. appendNamespaceSpecificString(urn, wsinfo.queryname.get()).append(":result:");
  1007. appendNamespaceSpecificString(urn, resultname);
  1008. VStringBuffer nsxml("<namespace nsvar=\"ds%d\" ns=\"%s\" import=\"1\" location=\"../result/%s.xsd\"/>", count++, urn.toLowerCase().str(), resultname);
  1009. namespaces->addPropTree("namespace", createPTreeFromXMLString(nsxml.str()));
  1010. }
  1011. }
  1012. }
  1013. StringBuffer &appendEclXsdName(StringBuffer &content, const char *name, bool istype=false)
  1014. {
  1015. if (name)
  1016. {
  1017. if (!strnicmp(name, "xs:", 3))
  1018. content.append("xsd:").append(name+3);
  1019. else
  1020. {
  1021. if (istype && !strchr(name, ':'))
  1022. content.append("tns:");
  1023. content.append(name);
  1024. }
  1025. }
  1026. return content;
  1027. }
  1028. void appendEclXsdStartTag(StringBuffer &content, IPropertyTree *element, int indent, const char *attrstr=NULL, bool forceclose=false)
  1029. {
  1030. //while (indent--)
  1031. // content.append('\t');
  1032. const char *name = element->queryName();
  1033. appendEclXsdName(content.append('<'), name);
  1034. if (strieq(name, "xs:element"))
  1035. {
  1036. const char *elname=element->queryProp("@name");
  1037. if (!elname || !*elname) //ecl bug?
  1038. element->setProp("@name", "__unknown");
  1039. if (!element->hasProp("@minOccurs"))
  1040. content.append(' ').append("minOccurs=\"0\"");
  1041. }
  1042. if (attrstr)
  1043. content.append(' ').append(attrstr);
  1044. Owned<IAttributeIterator> attrs = element->getAttributes();
  1045. ForEach(*attrs)
  1046. {
  1047. const char *attrname=attrs->queryName()+1;
  1048. appendEclXsdName(content.append(' '), attrname);
  1049. appendEclXsdName(content.append("=\""), attrs->queryValue(), !stricmp(attrname, "type")).append('\"');
  1050. }
  1051. if (forceclose || !element->hasChildren())
  1052. content.append('/');
  1053. content.append(">");
  1054. }
  1055. void appendEclXsdComplexType(StringArray &names, StringBuffer &content, IPropertyTree *element, int indent=0)
  1056. {
  1057. StringBuffer name("t_");
  1058. ForEachItemIn(idx, names)
  1059. {
  1060. name.append(names.item(idx));
  1061. }
  1062. content.appendf("<xsd:complexType name=\"%s\">", name.str());
  1063. Owned<IPropertyTreeIterator> children = element->getElements("*");
  1064. ForEach(*children)
  1065. {
  1066. IPropertyTree &child = children->query();
  1067. appendEclXsdStartTag(content, &child, indent);
  1068. if (strieq(child.queryName(), "xs:sequence") || strieq(child.queryName(), "xs:all"))
  1069. {
  1070. Owned<IPropertyTreeIterator> els = child.getElements("xs:element");
  1071. ForEach(*els)
  1072. {
  1073. IPropertyTree &el = els->query();
  1074. StringBuffer typeattr;
  1075. if (!el.hasProp("@type") && el.hasProp("xs:complexType") && el.hasProp("@name"))
  1076. typeattr.appendf("type=\"tns:%s%s\"", name.str(), el.queryProp("@name"));
  1077. appendEclXsdStartTag(content, &el, indent, typeattr.str(), true);
  1078. }
  1079. }
  1080. if (child.hasChildren())
  1081. {
  1082. content.appendf("</");
  1083. appendEclXsdName(content, child.queryName());
  1084. content.append(">");
  1085. }
  1086. }
  1087. content.append("</xsd:complexType>");
  1088. }
  1089. void appendEclXsdNestedComplexTypes(StringArray &names, StringBuffer &content, IPropertyTree *element, int indent=0)
  1090. {
  1091. if (element->hasChildren())
  1092. {
  1093. Owned<IPropertyTreeIterator> children = element->getElements("*");
  1094. ForEach(*children)
  1095. {
  1096. if (element->hasProp("@name"))
  1097. names.append(element->queryProp("@name"));
  1098. appendEclXsdNestedComplexTypes(names, content, &children->query(), indent+1);
  1099. if (element->hasProp("@name"))
  1100. names.pop();
  1101. }
  1102. if (strieq(element->queryName(), "xs:complexType"))
  1103. appendEclXsdComplexType(names, content, element, indent);
  1104. }
  1105. }
  1106. void appendEclXsdSection(StringBuffer &content, IPropertyTree *element, int indent=0)
  1107. {
  1108. appendEclXsdStartTag(content, element, indent);
  1109. if (element->hasChildren())
  1110. {
  1111. Owned<IPropertyTreeIterator> children = element->getElements("*");
  1112. ForEach(*children)
  1113. {
  1114. appendEclXsdSection(content, &children->query(), indent+1);
  1115. }
  1116. appendEclXsdName(content.append("</"), element->queryName()).append('>');
  1117. }
  1118. }
  1119. void appendEclInputXsds(StringBuffer &content, IPropertyTree *xsd, BoolHash &added)
  1120. {
  1121. Owned<IPropertyTreeIterator> it = xsd->getElements("xs:schema/*");
  1122. const char *schema_name=xsd->queryProp("@name");
  1123. if (schema_name && *schema_name)
  1124. {
  1125. ForEach (*it)
  1126. {
  1127. IPropertyTree &item = it->query();
  1128. StringArray names;
  1129. names.append(schema_name);
  1130. appendEclXsdNestedComplexTypes(names, content, &item, 1);
  1131. const char *aname = item.queryProp("@name");
  1132. StringBuffer temp;
  1133. const char *elname = item.getName(temp).str();
  1134. if (!stricmp(aname, "dataset") || !added.getValue(aname))
  1135. {
  1136. StringBuffer temp;
  1137. if (!stricmp(aname, "dataset"))
  1138. {
  1139. #if 0
  1140. content.appendf("<xsd:complexType name=\"%s\"><xsd:sequence>", schema_name);
  1141. IPropertyTreeIterator *children = item.getElements("xs:complexType/xs:sequence/*");
  1142. ForEach(*children)
  1143. {
  1144. IPropertyTree &child = children->query();
  1145. if (child.hasProp("@name") && !stricmp(child.queryProp("@name"), "Row"))
  1146. {
  1147. child.setProp("@minOccurs", "0");
  1148. child.setProp("@maxOccurs", "unbounded");
  1149. }
  1150. //appendEclXsdSection(content, &child, 2);
  1151. //toXML(&child, content);
  1152. }
  1153. content.appendf("</xsd:sequence></xsd:complexType>", aname);
  1154. #endif
  1155. }
  1156. else
  1157. {
  1158. added.setValue(aname, true);
  1159. appendEclXsdSection(content, &item, 1);
  1160. //toXML(&item, content);
  1161. }
  1162. }
  1163. }
  1164. }
  1165. }
  1166. void CWsEclBinding::SOAPSectionToXsd(WsEclWuInfo &wsinfo, const char *parmXml, StringBuffer &schema, bool isRequest, IPropertyTree *xsdtree)
  1167. {
  1168. Owned<IPropertyTree> tree = createPTreeFromXMLString(parmXml, ipt_none, (XmlReaderOptions)(xr_ignoreWhiteSpace|xr_noRoot));
  1169. schema.appendf("<xsd:element name=\"%s%s\">", wsinfo.queryname.sget(), isRequest ? "Request" : "Response");
  1170. schema.append("<xsd:complexType>");
  1171. schema.append("<xsd:all>");
  1172. Owned<IPropertyTreeIterator> parts = tree->getElements("part");
  1173. if (parts)
  1174. {
  1175. ForEach(*parts)
  1176. {
  1177. IPropertyTree &part = parts->query();
  1178. const char *name=part.queryProp("@name");
  1179. const char *ptype=part.queryProp("@type");
  1180. StringBuffer type;
  1181. if (!strnicmp(ptype, "xsd:", 4))
  1182. {
  1183. type.append(translateXsdType(part.queryProp("@type")));
  1184. }
  1185. else
  1186. {
  1187. StringBuffer xpath;
  1188. StringBuffer xname(name);
  1189. xpath.appendf("Input[@name='%s']",xname.toLowerCase().str());
  1190. if (xsdtree->hasProp(xpath.str()))
  1191. type.append("tns:t_").append(xname).append("Dataset");
  1192. else
  1193. type.append(translateXsdType(part.queryProp("@type")));
  1194. }
  1195. schema.appendf("<xsd:element minOccurs=\"0\" maxOccurs=\"1\" name=\"%s\" type=\"%s\"", name, type.str());
  1196. if (strieq(type.str(), "tns:XmlDataSet"))
  1197. {
  1198. schema.append(">"
  1199. "<xsd:annotation><xsd:appinfo>"
  1200. "<form formRows=\"25\" formCols=\"60\"/>"
  1201. "</xsd:appinfo></xsd:annotation>"
  1202. "</xsd:element>");
  1203. }
  1204. else
  1205. schema.append("/>");
  1206. }
  1207. }
  1208. schema.append("</xsd:all>");
  1209. schema.append("</xsd:complexType>");
  1210. schema.append("</xsd:element>");
  1211. }
  1212. int CWsEclBinding::getXsdDefinition(IEspContext &context, CHttpRequest *request, StringBuffer &content, const char *service, const char *method, bool mda)
  1213. {
  1214. WsEclWuInfo *wsinfo = (WsEclWuInfo *) context.getBindingValue();
  1215. if (wsinfo)
  1216. getXsdDefinition(context, request, content, *wsinfo);
  1217. return 0;
  1218. }
  1219. int CWsEclBinding::getXsdDefinition(IEspContext &context, CHttpRequest *request, StringBuffer &content, WsEclWuInfo &wsinfo)
  1220. {
  1221. IProperties *httpparms=request->queryParameters();
  1222. if (wsecl)
  1223. {
  1224. StringBuffer xsds;
  1225. wsinfo.getSchemas(xsds);
  1226. Owned<IPropertyTree> xsdtree;
  1227. if (xsds.length())
  1228. xsdtree.setown(createPTreeFromXMLString(xsds.str()));
  1229. //common types
  1230. content.append(
  1231. "<xsd:complexType name=\"EspStringArray\">"
  1232. "<xsd:sequence>"
  1233. "<xsd:element name=\"Item\" type=\"xsd:string\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>"
  1234. "</xsd:sequence>"
  1235. "</xsd:complexType>"
  1236. "<xsd:complexType name=\"EspIntArray\">"
  1237. "<xsd:sequence>"
  1238. "<xsd:element name=\"Item\" type=\"xsd:int\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>"
  1239. "</xsd:sequence>"
  1240. "</xsd:complexType>"
  1241. "<xsd:simpleType name=\"XmlDataSet\">"
  1242. "<xsd:restriction base=\"xsd:string\"/>"
  1243. "</xsd:simpleType>"
  1244. "<xsd:simpleType name=\"CsvDataFile\">"
  1245. "<xsd:restriction base=\"xsd:string\"/>"
  1246. "</xsd:simpleType>"
  1247. "<xsd:simpleType name=\"RawDataFile\">"
  1248. "<xsd:restriction base=\"xsd:base64Binary\"/>"
  1249. "</xsd:simpleType>");
  1250. if (wsinfo.queryname.length()>0)
  1251. {
  1252. StringBuffer parmXml;
  1253. if (wsinfo.getWsResource("SOAP", parmXml))
  1254. {
  1255. if (xsdtree)
  1256. {
  1257. BoolHash added;
  1258. Owned<IPropertyTreeIterator> input_xsds =xsdtree->getElements("Input");
  1259. ForEach (*input_xsds)
  1260. {
  1261. appendEclInputXsds(content, &input_xsds->query(), added);
  1262. }
  1263. }
  1264. SOAPSectionToXsd(wsinfo, parmXml.str(), content, true, xsdtree);
  1265. }
  1266. content.appendf("<xsd:element name=\"%sResponse\">", wsinfo.queryname.sget());
  1267. content.append("<xsd:complexType>");
  1268. content.append("<xsd:all>");
  1269. content.append("<xsd:element name=\"Exceptions\" type=\"tns:ArrayOfEspException\" minOccurs=\"0\"/>");
  1270. Owned<IPropertyTreeIterator> result_xsds =xsdtree->getElements("Result");
  1271. if (!result_xsds->first())
  1272. {
  1273. content.append("<xsd:element name=\"Results\" type=\"xsd:string\" minOccurs=\"0\"/>");
  1274. }
  1275. else
  1276. {
  1277. content.append(
  1278. "<xsd:element name=\"Results\" minOccurs=\"0\">"
  1279. "<xsd:complexType>"
  1280. "<xsd:all>"
  1281. "<xsd:element name=\"Result\">"
  1282. "<xsd:complexType>"
  1283. "<xsd:all>");
  1284. int count=1;
  1285. ForEach (*result_xsds)
  1286. {
  1287. content.appendf("<xsd:element ref=\"ds%d:Dataset\" minOccurs=\"0\"/>", count++);
  1288. }
  1289. content.append(
  1290. "</xsd:all>"
  1291. "</xsd:complexType>"
  1292. "</xsd:element>"
  1293. "</xsd:all>"
  1294. "</xsd:complexType>"
  1295. "</xsd:element>");
  1296. }
  1297. content.append("</xsd:all>");
  1298. content.append("<xsd:attribute name=\"sequence\" type=\"xsd:int\"/>");
  1299. content.append("</xsd:complexType>");
  1300. content.append("</xsd:element>");
  1301. }
  1302. }
  1303. return 0;
  1304. }
  1305. bool CWsEclBinding::getSchema(StringBuffer& schema, IEspContext &ctx, CHttpRequest* req, WsEclWuInfo &wsinfo)
  1306. {
  1307. Owned<IPropertyTree> namespaces = createPTree();
  1308. appendSchemaNamespaces(namespaces, ctx, req, wsinfo);
  1309. Owned<IPropertyTreeIterator> nsiter = namespaces->getElements("namespace");
  1310. StringBuffer urn("urn:hpccsystems:ecl:");
  1311. urn.appendLower(wsinfo.queryname.length(), wsinfo.queryname.sget());
  1312. schema.appendf("<xsd:schema elementFormDefault=\"qualified\" targetNamespace=\"%s\" ", urn.str());
  1313. schema.appendf(" xmlns:tns=\"%s\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"", urn.str());
  1314. ForEach(*nsiter)
  1315. {
  1316. IPropertyTree &ns = nsiter->query();
  1317. schema.appendf(" xmlns:%s=\"%s\"", ns.queryProp("@nsvar"), ns.queryProp("@ns"));
  1318. }
  1319. schema.append(">\n");
  1320. ForEach(*nsiter)
  1321. {
  1322. IPropertyTree &ns = nsiter->query();
  1323. if (ns.hasProp("@import"))
  1324. schema.appendf("<xsd:import namespace=\"%s\" schemaLocation=\"%s\"/>", ns.queryProp("@ns"), ns.queryProp("@location"));
  1325. }
  1326. schema.append(
  1327. "<xsd:complexType name=\"EspException\">"
  1328. "<xsd:all>"
  1329. "<xsd:element name=\"Code\" type=\"xsd:string\" minOccurs=\"0\"/>"
  1330. "<xsd:element name=\"Audience\" type=\"xsd:string\" minOccurs=\"0\"/>"
  1331. "<xsd:element name=\"Source\" type=\"xsd:string\" minOccurs=\"0\"/>"
  1332. "<xsd:element name=\"Message\" type=\"xsd:string\" minOccurs=\"0\"/>"
  1333. "</xsd:all>"
  1334. "</xsd:complexType>\n"
  1335. "<xsd:complexType name=\"ArrayOfEspException\">"
  1336. "<xsd:sequence>"
  1337. "<xsd:element name=\"Source\" type=\"xsd:string\" minOccurs=\"0\"/>"
  1338. "<xsd:element name=\"Exception\" type=\"tns:EspException\" minOccurs=\"0\" maxOccurs=\"unbounded\"/>"
  1339. "</xsd:sequence>"
  1340. "</xsd:complexType>\n"
  1341. "<xsd:element name=\"Exceptions\" type=\"tns:ArrayOfEspException\"/>\n"
  1342. );
  1343. getXsdDefinition(ctx, req, schema, wsinfo);
  1344. schema.append("<xsd:element name=\"string\" nillable=\"true\" type=\"xsd:string\" />\n");
  1345. schema.append("</xsd:schema>");
  1346. return true;
  1347. }
  1348. int CWsEclBinding::getGenForm(IEspContext &context, CHttpRequest* request, CHttpResponse* response, WsEclWuInfo &wsinfo, bool box)
  1349. {
  1350. IProperties *parms = request->queryParameters();
  1351. StringBuffer page;
  1352. Owned<IXslProcessor> xslp = getXslProcessor();
  1353. StringBuffer v;
  1354. StringBuffer formxml("<FormInfo>");
  1355. appendXMLTag(formxml, "WUID", wsinfo.wuid.sget());
  1356. appendXMLTag(formxml, "QuerySet", wsinfo.qsetname.sget());
  1357. appendXMLTag(formxml, "QueryName", wsinfo.queryname.sget());
  1358. appendXMLTag(formxml, "ClientVersion", v.appendf("%g",context.getClientVersion()).str());
  1359. appendXMLTag(formxml, "RequestElement", v.clear().append(wsinfo.queryname).append("Request").str());
  1360. Owned<IWuWebView> web = createWuWebView(*wsinfo.wu.get(), wsinfo.queryname.get(), getCFD(), true);
  1361. if (web)
  1362. {
  1363. appendXMLTag(formxml, "Help", web->aggregateResources("HELP", v.clear()).str());
  1364. appendXMLTag(formxml, "Info", web->aggregateResources("INFO", v.clear()).str());
  1365. }
  1366. context.addOptions(ESPCTX_ALL_ANNOTATION);
  1367. if (box)
  1368. {
  1369. StringBuffer xmlreq;
  1370. getWsEcl2XmlRequest(xmlreq, context, request, wsinfo, "xml", NULL, 0);
  1371. if (xmlreq.length())
  1372. {
  1373. Owned<IPropertyTree> pretty = createPTreeFromXMLString(xmlreq.str(), ipt_ordered);
  1374. if (pretty)
  1375. {
  1376. toXML(pretty, xmlreq.clear());
  1377. formxml.append("<Request>");
  1378. encodeUtf8XML(xmlreq, formxml);
  1379. formxml.append("</Request>");
  1380. }
  1381. }
  1382. }
  1383. else
  1384. getSchema(formxml, context, request, wsinfo);
  1385. formxml.append("<CustomViews>");
  1386. if (web)
  1387. {
  1388. StringArray views;
  1389. web->getResultViewNames(views);
  1390. ForEachItemIn(i, views)
  1391. appendXMLTag(formxml, "Result", views.item(i));
  1392. }
  1393. formxml.append("</CustomViews>");
  1394. formxml.append("</FormInfo>");
  1395. Owned<IXslTransform> xform = xslp->createXslTransform();
  1396. StringBuffer xslfile(getCFD());
  1397. if (box)
  1398. xslfile.append("./xslt/wsecl3_boxform.xsl");
  1399. else
  1400. xslfile.append("./xslt/wsecl3_form.xsl");
  1401. xform->loadXslFromFile(xslfile.str());
  1402. xform->setXmlSource(formxml.str(), formxml.length()+1);
  1403. // pass params to form (excluding form and __querystring)
  1404. StringBuffer params;
  1405. if (!getUrlParams(context.queryRequestParameters(),params))
  1406. params.appendf("%cver_=%g",(params.length()>0) ? '&' : '?', context.getClientVersion());
  1407. xform->setStringParameter("queryParams", params.str());
  1408. xform->setParameter("formOptionsAccess", "1");
  1409. xform->setParameter("includeSoapTest", "1");
  1410. xform->setParameter("useTextareaForStringArray", "1");
  1411. // set the prop noDefaultValue param
  1412. IProperties* props = context.queryRequestParameters();
  1413. bool formInitialized = false;
  1414. if (props) {
  1415. Owned<IPropertyIterator> it = props->getIterator();
  1416. for (it->first(); it->isValid(); it->next()) {
  1417. const char* key = it->getPropKey();
  1418. if (*key=='.') {
  1419. formInitialized = true;
  1420. break;
  1421. }
  1422. }
  1423. }
  1424. xform->setParameter("noDefaultValue", formInitialized ? "1" : "0");
  1425. xform->transform(page);
  1426. response->setContentType("text/html");
  1427. response->setContent(page.str());
  1428. response->send();
  1429. return 0;
  1430. }
  1431. inline void appendParameterNode(StringBuffer &xpath, StringBuffer &node)
  1432. {
  1433. if (node.length())
  1434. {
  1435. if (isdigit(node.charAt(0)))
  1436. xpath.setLength(xpath.length()-1);
  1437. xpath.append(node);
  1438. node.clear();
  1439. }
  1440. }
  1441. void buildParametersXml(IPropertyTree *parmtree, IProperties *parms)
  1442. {
  1443. Owned<IPropertyIterator> it = parms->getIterator();
  1444. ForEach(*it)
  1445. {
  1446. const char *key = it->getPropKey();
  1447. const char *val = parms->queryProp(key);
  1448. StringBuffer xpath;
  1449. if (key && *key && val && *val)
  1450. {
  1451. bool isidx=false;
  1452. StringBuffer node;
  1453. for (int pos=0; key[pos]!=0; pos++)
  1454. {
  1455. if (key[pos]!='.')
  1456. node.append(key[pos]);
  1457. else
  1458. {
  1459. appendParameterNode(xpath, node);
  1460. xpath.append('/');
  1461. }
  1462. }
  1463. appendParameterNode(xpath, node);
  1464. ensurePTree(parmtree, xpath.str());
  1465. parmtree->setProp(xpath.str(), val);
  1466. }
  1467. }
  1468. StringBuffer xml;
  1469. toXML(parmtree, xml);
  1470. DBGLOG("parmtree: %s", xml.str());
  1471. }
  1472. void appendValidInputBoxContent(StringBuffer &xml, const char *in)
  1473. {
  1474. //more later
  1475. Owned<IPropertyTree> validAndFlat = createPTreeFromXMLString(in, ipt_ordered);
  1476. toXML(validAndFlat, xml, 0, 0);
  1477. }
  1478. void CWsEclBinding::getWsEcl2XmlRequest(StringBuffer& soapmsg, IEspContext &context, CHttpRequest* request, WsEclWuInfo &wsinfo, const char *xmltype, const char *ns, unsigned flags)
  1479. {
  1480. Owned<IPropertyTree> parmtree = createPTree();
  1481. IProperties *parms = context.queryRequestParameters();
  1482. const char *boxInput = parms->queryProp("_boxFormInput");
  1483. if (boxInput)
  1484. {
  1485. appendValidInputBoxContent(soapmsg, boxInput);
  1486. return;
  1487. }
  1488. buildParametersXml(parmtree, parms);
  1489. StringBuffer element;
  1490. element.append(wsinfo.queryname.sget());
  1491. element.append("Request");
  1492. StringBuffer schemaXml;
  1493. getSchema(schemaXml, context, request, wsinfo);
  1494. DBGLOG("request schema: %s", schemaXml.str());
  1495. Owned<IXmlSchema> schema = createXmlSchemaFromString(schemaXml);
  1496. if (schema.get())
  1497. {
  1498. IXmlType* type = schema->queryElementType(element);
  1499. if (type)
  1500. {
  1501. StringStack parent;
  1502. buildReqXml(parent, type, soapmsg, (!stricmp(xmltype, "roxiexml")) ? wsinfo.queryname.sget() : element.str(), parmtree, flags|REQXML_ROOT, ns);
  1503. }
  1504. }
  1505. }
  1506. StringBuffer &appendJSONException(StringBuffer &s, IException *e, const char *objname="Exceptions", const char *arrayName = "Exception")
  1507. {
  1508. if (!e)
  1509. return s;
  1510. if (objname && *objname)
  1511. appendJSONName(s, objname).append('{');
  1512. if (arrayName && *arrayName)
  1513. appendJSONName(s, arrayName).append('[');
  1514. delimitJSON(s);
  1515. s.append('{');
  1516. appendJSONValue(s, "Code", e->errorCode());
  1517. StringBuffer temp;
  1518. appendJSONValue(s, "Message", e->errorMessage(temp).str());
  1519. s.append('}');
  1520. if (arrayName && *arrayName)
  1521. s.append(']');
  1522. if (objname && *objname)
  1523. s.append('}');
  1524. return s;
  1525. }
  1526. StringBuffer &appendJSONExceptions(StringBuffer &s, IMultiException *e, const char *objname="Exceptions", const char *arrayName = "Exception")
  1527. {
  1528. if (!e)
  1529. return s;
  1530. if (objname && *objname)
  1531. appendJSONName(s, objname).append('{');
  1532. if (arrayName && *arrayName)
  1533. appendJSONName(s, arrayName).append('[');
  1534. ForEachItemIn(i, *e)
  1535. appendJSONException(s, &e->item(i), NULL, NULL);
  1536. if (arrayName && *arrayName)
  1537. s.append(']');
  1538. if (objname && *objname)
  1539. s.append('}');
  1540. return s;
  1541. }
  1542. void CWsEclBinding::getWsEclJsonRequest(StringBuffer& jsonmsg, IEspContext &context, CHttpRequest* request, WsEclWuInfo &wsinfo, const char *xmltype, const char *ns, unsigned flags)
  1543. {
  1544. size32_t start = jsonmsg.length();
  1545. try
  1546. {
  1547. Owned<IPropertyTree> parmtree = createPTree();
  1548. IProperties *parms = context.queryRequestParameters();
  1549. buildParametersXml(parmtree, parms);
  1550. StringBuffer element;
  1551. element.append(wsinfo.queryname.sget());
  1552. element.append("Request");
  1553. StringBuffer schemaXml;
  1554. getSchema(schemaXml, context, request, wsinfo);
  1555. DBGLOG("request schema: %s", schemaXml.str());
  1556. Owned<IXmlSchema> schema = createXmlSchemaFromString(schemaXml);
  1557. if (schema.get())
  1558. {
  1559. IXmlType* type = schema->queryElementType(element);
  1560. if (type)
  1561. {
  1562. StringStack parent;
  1563. int indent=0;
  1564. buildJsonMsg(parent, type, jsonmsg, wsinfo.queryname.sget(), parmtree, flags|REQXML_ROOT|REQXML_ESCAPEFORMATTERS, indent);
  1565. }
  1566. }
  1567. }
  1568. catch (IException *e)
  1569. {
  1570. jsonmsg.setLength(start);
  1571. appendJSONException(jsonmsg.append('{'), e);
  1572. jsonmsg.append('}');
  1573. }
  1574. }
  1575. void CWsEclBinding::getWsEclJsonResponse(StringBuffer& jsonmsg, IEspContext &context, CHttpRequest *request, const char *xml, WsEclWuInfo &wsinfo)
  1576. {
  1577. size32_t start = jsonmsg.length();
  1578. try
  1579. {
  1580. Owned<IPropertyTree> parmtree = createPTreeFromXMLString(xml, ipt_none, (XmlReaderOptions)(xr_ignoreWhiteSpace|xr_ignoreNameSpaces));
  1581. StringBuffer element;
  1582. element.append(wsinfo.queryname.sget());
  1583. element.append("Response");
  1584. VStringBuffer xpath("Body/%s/Results/Result/Exception", element.str());
  1585. Owned<IPropertyTreeIterator> exceptions = parmtree->getElements(xpath.str());
  1586. jsonmsg.appendf("{\n \"%s\": {\n \"Results\": {\n", element.str());
  1587. if (exceptions && exceptions->first())
  1588. {
  1589. jsonmsg.append(" \"Exceptions\": {\n \"Exception\": [\n");
  1590. bool first=true;
  1591. ForEach(*exceptions)
  1592. {
  1593. if (first)
  1594. first=false;
  1595. else
  1596. jsonmsg.append(",\n");
  1597. jsonmsg.appendf(" {\n \"Code\": %d,\n \"Message\": \"%s\"\n }", exceptions->query().getPropInt("Code"), exceptions->query().queryProp("Message"));
  1598. }
  1599. jsonmsg.append("\n ]\n }\n");
  1600. }
  1601. xpath.clear().append("Body/*[1]/Results/Result/Dataset");
  1602. Owned<IPropertyTreeIterator> datasets = parmtree->getElements(xpath.str());
  1603. ForEach(*datasets)
  1604. {
  1605. IPropertyTree &ds = datasets->query();
  1606. const char *dsname = ds.queryProp("@name");
  1607. if (dsname && *dsname)
  1608. {
  1609. StringBuffer schemaResult;
  1610. wsinfo.getOutputSchema(schemaResult, dsname);
  1611. if (schemaResult.length())
  1612. {
  1613. Owned<IXmlSchema> schema = createXmlSchemaFromString(schemaResult);
  1614. if (schema.get())
  1615. {
  1616. IXmlType* type = schema->queryElementType("Dataset");
  1617. if (type)
  1618. {
  1619. StringStack parent;
  1620. int indent=4;
  1621. StringBuffer outname(dsname);
  1622. buildJsonMsg(parent, type, jsonmsg, outname.replace(' ', '_').str(), &ds, 0, indent);
  1623. }
  1624. }
  1625. }
  1626. }
  1627. }
  1628. jsonmsg.append(" }\n }\n}");
  1629. }
  1630. catch (IException *e)
  1631. {
  1632. jsonmsg.setLength(start);
  1633. appendJSONException(jsonmsg.append('{'), e);
  1634. jsonmsg.append('}');
  1635. }
  1636. }
  1637. void CWsEclBinding::getSoapMessage(StringBuffer& soapmsg, IEspContext &context, CHttpRequest* request, WsEclWuInfo &wsinfo, unsigned flags)
  1638. {
  1639. soapmsg.append(
  1640. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  1641. "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\""
  1642. " xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\">"
  1643. " <soap:Body>"
  1644. );
  1645. StringBuffer ns;
  1646. ns.append("xmlns=\"urn:hpccsystems:ecl:").appendLower(wsinfo.queryname.length(), wsinfo.queryname.sget()).append('\"');
  1647. getWsEcl2XmlRequest(soapmsg, context, request, wsinfo, "soap", ns.str(), flags);
  1648. soapmsg.append("</soap:Body></soap:Envelope>");
  1649. }
  1650. int CWsEclBinding::getXmlTestForm(IEspContext &context, CHttpRequest* request, CHttpResponse* response, const char *formtype, WsEclWuInfo &wsinfo)
  1651. {
  1652. getXmlTestForm(context, request, response, wsinfo, formtype);
  1653. return 0;
  1654. };
  1655. inline StringBuffer &buildWsEclTargetUrl(StringBuffer &url, WsEclWuInfo &wsinfo, const char *type, const char *params)
  1656. {
  1657. url.append("/WsEcl/").append(type).append('/');
  1658. if (wsinfo.qsetname.length() && wsinfo.queryname.length())
  1659. url.append("query/").append(wsinfo.qsetname.get()).append('/').append(wsinfo.queryname.get());
  1660. else
  1661. url.append("wuid/").append(wsinfo.wuid.sget());
  1662. if (params && *params)
  1663. url.append('?').append(params);
  1664. return url;
  1665. }
  1666. int CWsEclBinding::getXmlTestForm(IEspContext &context, CHttpRequest* request, CHttpResponse* response, WsEclWuInfo &wsinfo, const char *formtype)
  1667. {
  1668. IProperties *parms = context.queryRequestParameters();
  1669. StringBuffer soapmsg, pageName;
  1670. getSoapMessage(soapmsg, context, request, wsinfo, 0);
  1671. StringBuffer params;
  1672. const char* excludes[] = {"soap_builder_",NULL};
  1673. getEspUrlParams(context,params,excludes);
  1674. Owned<IXslProcessor> xslp = getXslProcessor();
  1675. Owned<IXslTransform> xform = xslp->createXslTransform();
  1676. xform->loadXslFromFile(StringBuffer(getCFD()).append("./xslt/wsecl3_xmltest.xsl").str());
  1677. StringBuffer srcxml;
  1678. srcxml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><srcxml><soapbody><![CDATA[");
  1679. srcxml.append(soapmsg.str());
  1680. srcxml.append("]]></soapbody></srcxml>");
  1681. xform->setXmlSource(srcxml.str(), srcxml.length());
  1682. StringBuffer header;
  1683. if (!stricmp(formtype, "roxiexml"))
  1684. {
  1685. header.append("Content-Type: application/xml; charset=UTF-8");
  1686. xform->setStringParameter("showhttp", "true()");
  1687. pageName.append("ROXIE XML Test");
  1688. }
  1689. else if (!stricmp(formtype, "roxiesoap"))
  1690. {
  1691. header.append("Content-Type: text/xml; charset=UTF-8");
  1692. xform->setStringParameter("showhttp", "true()");
  1693. pageName.append("ROXIE SOAP Test");
  1694. }
  1695. else
  1696. {
  1697. header.append("Content-Type: text/xml; charset=UTF-8");
  1698. xform->setStringParameter("showhttp", "true()");
  1699. pageName.append("SOAP Test");
  1700. }
  1701. // params
  1702. xform->setStringParameter("pageName", pageName.str());
  1703. xform->setStringParameter("serviceName", wsinfo.qsetname.sget());
  1704. xform->setStringParameter("methodName", wsinfo.queryname.sget());
  1705. xform->setStringParameter("wuid", wsinfo.wuid.sget());
  1706. xform->setStringParameter("header", header.str());
  1707. ISecUser* user = context.queryUser();
  1708. bool inhouse = user && (user->getStatus()==SecUserStatus_Inhouse);
  1709. xform->setParameter("inhouseUser", inhouse ? "true()" : "false()");
  1710. StringBuffer url;
  1711. xform->setStringParameter("destination", buildWsEclTargetUrl(url, wsinfo, formtype, params.str()).str());
  1712. StringBuffer page;
  1713. xform->transform(page);
  1714. response->setContent(page);
  1715. response->setContentType("text/html; charset=UTF-8");
  1716. response->setStatus(HTTP_STATUS_OK);
  1717. response->send();
  1718. return 0;
  1719. }
  1720. int CWsEclBinding::getJsonTestForm(IEspContext &context, CHttpRequest* request, CHttpResponse* response, WsEclWuInfo &wsinfo, const char *formtype)
  1721. {
  1722. IProperties *parms = context.queryRequestParameters();
  1723. StringBuffer jsonmsg, pageName;
  1724. getWsEclJsonRequest(jsonmsg, context, request, wsinfo, "json", NULL, 0);
  1725. StringBuffer params;
  1726. const char* excludes[] = {"soap_builder_",NULL};
  1727. getEspUrlParams(context,params,excludes);
  1728. StringBuffer header("Content-Type: application/json; charset=UTF-8");
  1729. Owned<IXslProcessor> xslp = getXslProcessor();
  1730. Owned<IXslTransform> xform = xslp->createXslTransform();
  1731. xform->loadXslFromFile(StringBuffer(getCFD()).append("./xslt/wsecl3_jsontest.xsl").str());
  1732. StringBuffer srcxml;
  1733. srcxml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><srcxml><jsonreq><![CDATA[");
  1734. srcxml.append(jsonmsg.str());
  1735. srcxml.append("]]></jsonreq></srcxml>");
  1736. xform->setXmlSource(srcxml.str(), srcxml.length());
  1737. xform->setStringParameter("showhttp", "true()");
  1738. pageName.append("JSON Test");
  1739. // params
  1740. xform->setStringParameter("pageName", pageName.str());
  1741. xform->setStringParameter("serviceName", wsinfo.qsetname.sget());
  1742. xform->setStringParameter("methodName", wsinfo.queryname.sget());
  1743. xform->setStringParameter("wuid", wsinfo.wuid.sget());
  1744. xform->setStringParameter("header", header.str());
  1745. ISecUser* user = context.queryUser();
  1746. bool inhouse = user && (user->getStatus()==SecUserStatus_Inhouse);
  1747. xform->setParameter("inhouseUser", inhouse ? "true()" : "false()");
  1748. StringBuffer url;
  1749. xform->setStringParameter("destination", buildWsEclTargetUrl(url, wsinfo, formtype, params.str()).str());
  1750. StringBuffer page;
  1751. xform->transform(page);
  1752. response->setContent(page);
  1753. response->setContentType("text/html; charset=UTF-8");
  1754. response->setStatus(HTTP_STATUS_OK);
  1755. response->send();
  1756. return 0;
  1757. }
  1758. int CWsEclBinding::onGetSoapBuilder(IEspContext &context, CHttpRequest* request, CHttpResponse* response, WsEclWuInfo &wsinfo)
  1759. {
  1760. return getXmlTestForm(context, request, response, wsinfo, "soap");
  1761. }
  1762. bool checkWsEclFormType(StringBuffer &form, const char *value)
  1763. {
  1764. if (value)
  1765. {
  1766. bool save = (strieq(value, "ecl")||strieq(value, "box"));
  1767. if (save || (strieq(value, "soap")||strieq(value, "json")))
  1768. {
  1769. form.set(value);
  1770. return save;
  1771. }
  1772. }
  1773. form.set("ecl");
  1774. return false;
  1775. }
  1776. void getWsEclFormType(CHttpRequest* request, CHttpResponse* response, StringBuffer &form)
  1777. {
  1778. bool save=false;
  1779. if (strieq(form, "default"))
  1780. {
  1781. CEspCookie *cookie = request->queryCookie("defaultWsEclForm");
  1782. checkWsEclFormType(form, (cookie) ? cookie->getValue() : NULL);
  1783. }
  1784. else if (checkWsEclFormType(form, form.str()))
  1785. {
  1786. CEspCookie *cookie = request->queryCookie("defaultWsEclForm");
  1787. if (!cookie || !strieq(cookie->getValue(), form.str()))
  1788. response->addCookie(new CEspCookie("defaultWsEclForm", form));
  1789. }
  1790. }
  1791. int CWsEclBinding::getWsEcl2Form(CHttpRequest* request, CHttpResponse* response, const char *thepath)
  1792. {
  1793. IEspContext *context = request->queryContext();
  1794. StringBuffer formtype;
  1795. nextPathNode(thepath, formtype);
  1796. StringBuffer wuid;
  1797. StringBuffer qs;
  1798. StringBuffer qid;
  1799. splitLookupInfo(request->queryParameters(), thepath, wuid, qs, qid);
  1800. WsEclWuInfo wsinfo(wuid.str(), qs.str(), qid.str(), context->queryUserId(), context->queryPassword());
  1801. getWsEclFormType(request, response, formtype);
  1802. if (strieq(formtype.str(), "ecl"))
  1803. return getGenForm(*context, request, response, wsinfo, false);
  1804. else if (strieq(formtype.str(), "box"))
  1805. return getGenForm(*context, request, response, wsinfo, true);
  1806. else if (strieq(formtype.str(), "soap"))
  1807. return getXmlTestForm(*context, request, response, "soap", wsinfo);
  1808. else if (strieq(formtype.str(), "json"))
  1809. return getJsonTestForm(*context, request, response, wsinfo, "json");
  1810. return 0;
  1811. }
  1812. void CWsEclBinding::addParameterToWorkunit(IWorkUnit * workunit, IConstWUResult &vardef, IResultSetMetaData &metadef, const char *varname, IPropertyTree *valtree)
  1813. {
  1814. if (!varname || !*varname)
  1815. return;
  1816. Owned<IWUResult> var = workunit->updateVariableByName(varname);
  1817. if (!vardef.isResultScalar())
  1818. {
  1819. StringBuffer ds;
  1820. if (valtree->hasChildren())
  1821. toXML(valtree, ds);
  1822. else
  1823. {
  1824. const char *val = valtree->queryProp(NULL);
  1825. if (val)
  1826. decodeXML(val, ds);
  1827. }
  1828. if (ds.length())
  1829. var->setResultRaw(ds.length(), ds.str(), ResultFormatXml);
  1830. }
  1831. else
  1832. {
  1833. const char *val = valtree->queryProp(NULL);
  1834. if (val && *val)
  1835. {
  1836. switch (metadef.getColumnDisplayType(0))
  1837. {
  1838. case TypeBoolean:
  1839. var->setResultBool(strieq(val, "1") || strieq(val, "true") || strieq(val, "on"));
  1840. break;
  1841. case TypeInteger:
  1842. var->setResultInt(_atoi64(val));
  1843. break;
  1844. case TypeUnsignedInteger:
  1845. var->setResultInt(_atoi64(val));
  1846. break;
  1847. case TypeReal:
  1848. var->setResultReal(atof(val));
  1849. break;
  1850. case TypeSet:
  1851. case TypeDataset:
  1852. case TypeData:
  1853. var->setResultRaw(strlen(val), val, ResultFormatRaw);
  1854. break;
  1855. case TypeUnicode: {
  1856. MemoryBuffer target;
  1857. convertUtf(target, UtfReader::Utf16le, strlen(val), val, UtfReader::Utf8);
  1858. var->setResultUnicode(target.toByteArray(), (target.length()>1) ? target.length()/2 : 0);
  1859. }
  1860. break;
  1861. case TypeString:
  1862. case TypeUnknown:
  1863. default:
  1864. var->setResultString(val, strlen(val));
  1865. break;
  1866. break;
  1867. }
  1868. var->setResultStatus(ResultStatusSupplied);
  1869. }
  1870. }
  1871. }
  1872. int CWsEclBinding::submitWsEclWorkunit(IEspContext & context, WsEclWuInfo &wsinfo, const char *xml, StringBuffer &out, unsigned flags, const char *viewname, const char *xsltname)
  1873. {
  1874. Owned <IWorkUnitFactory> factory = getSecWorkUnitFactory(*context.querySecManager(), *context.queryUser());
  1875. Owned <IWorkUnit> workunit = factory->createWorkUnit(NULL, "wsecl", context.queryUserId());
  1876. IExtendedWUInterface *ext = queryExtendedWU(workunit);
  1877. ext->copyWorkUnit(wsinfo.wu, false);
  1878. workunit->clearExceptions();
  1879. workunit->resetWorkflow();
  1880. workunit->setClusterName(wsinfo.qsetname.sget());
  1881. workunit->setUser(context.queryUserId());
  1882. SCMStringBuffer wuid;
  1883. workunit->getWuid(wuid);
  1884. SCMStringBuffer token;
  1885. createToken(wuid.str(), context.queryUserId(), context.queryPassword(), token);
  1886. workunit->setSecurityToken(token.str());
  1887. workunit->setState(WUStateSubmitted);
  1888. workunit->commit();
  1889. Owned<IPropertyTree> req = createPTreeFromXMLString(xml, ipt_none, (XmlReaderOptions)(xr_ignoreWhiteSpace|xr_ignoreNameSpaces));
  1890. IPropertyTree *start = req.get();
  1891. if (start->hasProp("Envelope"))
  1892. start=start->queryPropTree("Envelope");
  1893. if (start->hasProp("Body"))
  1894. start=start->queryPropTree("Body/*[1]");
  1895. Owned<IResultSetFactory> resultSetFactory(getResultSetFactory(context.queryUserId(), context.queryPassword()));
  1896. Owned<IPropertyTreeIterator> it = start->getElements("*");
  1897. ForEach(*it)
  1898. {
  1899. IPropertyTree &eclparm=it->query();
  1900. const char *varname = eclparm.queryName();
  1901. IConstWUResult *vardef = wsinfo.wu->getVariableByName(varname);
  1902. if (vardef)
  1903. {
  1904. Owned<IResultSetMetaData> metadef = resultSetFactory->createResultSetMeta(vardef);
  1905. if (metadef)
  1906. addParameterToWorkunit(workunit.get(), *vardef, *metadef, varname, &eclparm);
  1907. }
  1908. }
  1909. workunit->schedule();
  1910. workunit.clear();
  1911. runWorkUnit(wuid.str(), wsinfo.qsetname.sget());
  1912. //don't wait indefinately, in case submitted to an inactive queue wait max + 5 mins
  1913. int wutimeout = 300000;
  1914. if (waitForWorkUnitToComplete(wuid.str(), wutimeout))
  1915. {
  1916. Owned<IWuWebView> web = createWuWebView(wuid.str(), wsinfo.queryname.get(), getCFD(), true);
  1917. if (!web)
  1918. {
  1919. DBGLOG("WS-ECL failed to create WuWebView for workunit %s", wuid.str());
  1920. return 0;
  1921. }
  1922. if (viewname)
  1923. web->renderResults(viewname, out);
  1924. else if (xsltname)
  1925. web->applyResultsXSLT(xsltname, out);
  1926. else
  1927. web->expandResults(out, flags);
  1928. }
  1929. else
  1930. {
  1931. DBGLOG("WS-ECL request timed out, WorkUnit %s", wuid.str());
  1932. }
  1933. DBGLOG("WS-ECL Request processed [using Doxie]");
  1934. return true;
  1935. }
  1936. void xppToXmlString(XmlPullParser &xpp, StartTag &stag, StringBuffer &buffer)
  1937. {
  1938. int level = 1; //assumed due to the way gotonextdataset works.
  1939. int type = XmlPullParser::END_TAG;
  1940. const char * content = "";
  1941. const char *tag = NULL;
  1942. EndTag etag;
  1943. tag = stag.getLocalName();
  1944. if (tag && *tag)
  1945. {
  1946. buffer.appendf("<%s", tag);
  1947. for (int idx=0; idx<stag.getLength(); idx++)
  1948. buffer.appendf(" %s=\"%s\"", stag.getRawName(idx), stag.getValue(idx));
  1949. buffer.append(">");
  1950. }
  1951. do
  1952. {
  1953. type = xpp.next();
  1954. switch(type)
  1955. {
  1956. case XmlPullParser::START_TAG:
  1957. {
  1958. xpp.readStartTag(stag);
  1959. ++level;
  1960. tag = stag.getLocalName();
  1961. if (tag && *tag)
  1962. {
  1963. buffer.appendf("<%s", tag);
  1964. for (int idx=0; idx<stag.getLength(); idx++)
  1965. buffer.appendf(" %s=\"%s\"", stag.getRawName(idx), stag.getValue(idx));
  1966. buffer.append(">");
  1967. }
  1968. break;
  1969. }
  1970. case XmlPullParser::END_TAG:
  1971. xpp.readEndTag(etag);
  1972. tag = etag.getLocalName();
  1973. if (tag && *tag)
  1974. buffer.appendf("</%s>", tag);
  1975. --level;
  1976. break;
  1977. case XmlPullParser::CONTENT:
  1978. content = xpp.readContent();
  1979. encodeUtf8XML(content, buffer);
  1980. break;
  1981. case XmlPullParser::END_DOCUMENT:
  1982. level=0;
  1983. break;
  1984. }
  1985. }
  1986. while (level > 0);
  1987. }
  1988. bool xppGotoTag(XmlPullParser &xppx, const char *tagname, StartTag &stag)
  1989. {
  1990. int level = 0;
  1991. int type = XmlPullParser::END_TAG;
  1992. do
  1993. {
  1994. type = xppx.next();
  1995. switch(type)
  1996. {
  1997. case XmlPullParser::START_TAG:
  1998. {
  1999. xppx.readStartTag(stag);
  2000. ++level;
  2001. const char *tag = stag.getLocalName();
  2002. if (tag && strieq(tag, tagname))
  2003. return true;
  2004. break;
  2005. }
  2006. case XmlPullParser::END_TAG:
  2007. --level;
  2008. break;
  2009. case XmlPullParser::END_DOCUMENT:
  2010. level=0;
  2011. break;
  2012. }
  2013. }
  2014. while (level > 0);
  2015. return false;
  2016. }
  2017. void CWsEclBinding::sendRoxieRequest(const char *target, StringBuffer &req, StringBuffer &resp, StringBuffer &status, const char *query)
  2018. {
  2019. ISmartSocketFactory *conn = NULL;
  2020. SocketEndpoint ep;
  2021. try
  2022. {
  2023. Owned<IConstWUClusterInfo> clusterInfo = getTargetClusterInfo(target);
  2024. if (!clusterInfo)
  2025. throw MakeStringException(-1, "target cluster not found");
  2026. SCMStringBuffer process;
  2027. clusterInfo->getRoxieProcess(process);
  2028. ISmartSocketFactory *conn = wsecl->connMap.getValue(process.str());
  2029. if (!conn)
  2030. throw MakeStringException(-1, "process cluster not found: %s", process.str());
  2031. ep = conn->nextEndpoint();
  2032. Owned<IHttpClientContext> httpctx = getHttpClientContext();
  2033. StringBuffer url("http://");
  2034. ep.getIpText(url).append(':').append(ep.port);
  2035. Owned<IHttpClient> httpclient = httpctx->createHttpClient(NULL, url);
  2036. if (0 > httpclient->sendRequest("POST", "text/xml", req, resp, status))
  2037. throw MakeStringException(-1, "Process cluster communication error: %s", process.str());
  2038. }
  2039. catch (IException *e)
  2040. {
  2041. if (conn && !ep.isNull())
  2042. conn->setStatus(ep, false);
  2043. StringBuffer s;
  2044. VStringBuffer uri("urn:hpccsystems:ecl:%s", query);
  2045. resp.set("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  2046. resp.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body>");
  2047. resp.append('<').append(query).append("Response xmlns='").append(uri).append("'>");
  2048. resp.append("<Results><Result><Exception><Source>WsEcl</Source>");
  2049. resp.append("<Code>").append(e->errorCode()).append("</Code>");
  2050. resp.append("<Message>").append(e->errorMessage(s)).append("</Message>");
  2051. resp.append("</Exception></Result></Results>");
  2052. resp.append("</").append(query).append("Response></soap:Body></soap:Envelope>");
  2053. e->Release();
  2054. }
  2055. }
  2056. int CWsEclBinding::onSubmitQueryOutputXML(IEspContext &context, CHttpRequest* request, CHttpResponse* response, WsEclWuInfo &wsinfo, const char *format)
  2057. {
  2058. StringBuffer soapmsg;
  2059. getSoapMessage(soapmsg, context, request, wsinfo, REQXML_TRIM|REQXML_ROOT);
  2060. DBGLOG("submitQuery soap: %s", soapmsg.str());
  2061. const char *thepath = request->queryPath();
  2062. StringBuffer status;
  2063. StringBuffer output;
  2064. SCMStringBuffer clustertype;
  2065. wsinfo.wu->getDebugValue("targetclustertype", clustertype);
  2066. unsigned xmlflags = WWV_ADD_RESPONSE_TAG | WWV_INCL_NAMESPACES | WWV_INCL_GENERATED_NAMESPACES;
  2067. if (context.queryRequestParameters()->hasProp("display"))
  2068. xmlflags |= WWV_USE_DISPLAY_XSLT;
  2069. if (!format || !streq(format, "expanded"))
  2070. xmlflags |= WWV_OMIT_SCHEMAS;
  2071. if (strieq(clustertype.str(), "roxie"))
  2072. {
  2073. StringBuffer roxieresp;
  2074. sendRoxieRequest(wsinfo.qsetname.get(), soapmsg, roxieresp, status, wsinfo.queryname);
  2075. Owned<IWuWebView> web = createWuWebView(*wsinfo.wu, wsinfo.queryname.get(), getCFD(), true);
  2076. if (web.get())
  2077. web->expandResults(roxieresp.str(), output, xmlflags);
  2078. }
  2079. else
  2080. {
  2081. submitWsEclWorkunit(context, wsinfo, soapmsg.str(), output, xmlflags);
  2082. }
  2083. response->setContent(output.str());
  2084. response->setContentType(HTTP_TYPE_APPLICATION_XML);
  2085. response->setStatus("200 OK");
  2086. response->send();
  2087. return 0;
  2088. }
  2089. int CWsEclBinding::onSubmitQueryOutputView(IEspContext &context, CHttpRequest* request, CHttpResponse* response, WsEclWuInfo &wsinfo)
  2090. {
  2091. StringBuffer soapmsg;
  2092. getSoapMessage(soapmsg, context, request, wsinfo, REQXML_TRIM|REQXML_ROOT);
  2093. DBGLOG("submitQuery soap: %s", soapmsg.str());
  2094. const char *thepath = request->queryPath();
  2095. StringBuffer output;
  2096. StringBuffer status;
  2097. StringBuffer html;
  2098. SCMStringBuffer clustertype;
  2099. wsinfo.wu->getDebugValue("targetclustertype", clustertype);
  2100. StringBuffer xsltfile(getCFD());
  2101. xsltfile.append("xslt/wsecl3_result.xslt");
  2102. const char *view = context.queryRequestParameters()->queryProp("view");
  2103. if (strieq(clustertype.str(), "roxie"))
  2104. {
  2105. sendRoxieRequest(wsinfo.qsetname.get(), soapmsg, output, status, wsinfo.queryname);
  2106. Owned<IWuWebView> web = createWuWebView(*wsinfo.wu, wsinfo.queryname.get(), getCFD(), true);
  2107. if (!view)
  2108. web->applyResultsXSLT(xsltfile.str(), output.str(), html);
  2109. else
  2110. web->renderResults(view, output.str(), html);
  2111. }
  2112. else
  2113. {
  2114. submitWsEclWorkunit(context, wsinfo, soapmsg.str(), html, 0, view, xsltfile.str());
  2115. }
  2116. response->setContent(html.str());
  2117. response->setContentType("text/html; charset=utf-8");
  2118. response->setStatus("200 OK");
  2119. response->send();
  2120. return 0;
  2121. }
  2122. int CWsEclBinding::getWsdlMessages(IEspContext &context, CHttpRequest *request, StringBuffer &content, const char *service, const char *method, bool mda)
  2123. {
  2124. WsEclWuInfo *wsinfo = (WsEclWuInfo *) context.getBindingValue();
  2125. if (wsinfo)
  2126. {
  2127. content.appendf("<message name=\"%sSoapIn\">", wsinfo->queryname.sget());
  2128. content.appendf("<part name=\"parameters\" element=\"tns:%sRequest\"/>", wsinfo->queryname.sget());
  2129. content.append("</message>");
  2130. content.appendf("<message name=\"%sSoapOut\">", wsinfo->queryname.sget());
  2131. content.appendf("<part name=\"parameters\" element=\"tns:%sResponse\"/>", wsinfo->queryname.sget());
  2132. content.append("</message>");
  2133. }
  2134. return 0;
  2135. }
  2136. int CWsEclBinding::getWsdlPorts(IEspContext &context, CHttpRequest *request, StringBuffer &content, const char *service, const char *method, bool mda)
  2137. {
  2138. WsEclWuInfo *wsinfo = (WsEclWuInfo *) context.getBindingValue();
  2139. if (wsinfo)
  2140. {
  2141. content.appendf("<portType name=\"%sServiceSoap\">", wsinfo->qsetname.sget());
  2142. content.appendf("<operation name=\"%s\">", wsinfo->queryname.sget());
  2143. content.appendf("<input message=\"tns:%sSoapIn\"/>", wsinfo->queryname.sget());
  2144. content.appendf("<output message=\"tns:%sSoapOut\"/>", wsinfo->queryname.sget());
  2145. content.append("</operation>");
  2146. content.append("</portType>");
  2147. }
  2148. return 0;
  2149. }
  2150. int CWsEclBinding::getWsdlBindings(IEspContext &context, CHttpRequest *request, StringBuffer &content, const char *service, const char *method, bool mda)
  2151. {
  2152. WsEclWuInfo *wsinfo = (WsEclWuInfo *) context.getBindingValue();
  2153. if (wsinfo)
  2154. {
  2155. content.appendf("<binding name=\"%sServiceSoap\" type=\"tns:%sServiceSoap\">", wsinfo->qsetname.sget(), wsinfo->qsetname.sget());
  2156. content.append("<soap:binding transport=\"http://schemas.xmlsoap.org/soap/http\" style=\"document\"/>");
  2157. content.appendf("<operation name=\"%s\">", wsinfo->queryname.sget());
  2158. content.appendf("<soap:operation soapAction=\"/%s/%s?ver_=1.0\" style=\"document\"/>", wsinfo->qsetname.sget(), wsinfo->queryname.sget());
  2159. content.append("<input>");
  2160. content.append("<soap:body use=\"literal\"/>");
  2161. content.append("</input>");
  2162. content.append("<output><soap:body use=\"literal\"/></output>");
  2163. content.append("</operation>");
  2164. content.append("</binding>");
  2165. }
  2166. return 0;
  2167. }
  2168. int CWsEclBinding::onGetWsdl(IEspContext &context, CHttpRequest* request, CHttpResponse* response, WsEclWuInfo &wsinfo)
  2169. {
  2170. context.setBindingValue(&wsinfo);
  2171. EspHttpBinding::onGetWsdl(context, request, response, wsinfo.qsetname.sget(), wsinfo.queryname.sget());
  2172. context.setBindingValue(NULL);
  2173. return 0;
  2174. }
  2175. int CWsEclBinding::onGetXsd(IEspContext &context, CHttpRequest* request, CHttpResponse* response, WsEclWuInfo &wsinfo)
  2176. {
  2177. context.setBindingValue(&wsinfo);
  2178. EspHttpBinding::onGetXsd(context, request, response, wsinfo.qsetname.sget(), wsinfo.queryname.sget());
  2179. context.setBindingValue(NULL);
  2180. return 0;
  2181. }
  2182. int CWsEclBinding::getWsEclDefinition(CHttpRequest* request, CHttpResponse* response, const char *thepath)
  2183. {
  2184. IEspContext *context = request->queryContext();
  2185. IProperties *parms = context->queryRequestParameters();
  2186. StringBuffer wuid;
  2187. StringBuffer qs;
  2188. StringBuffer qid;
  2189. splitLookupInfo(parms, thepath, wuid, qs, qid);
  2190. WsEclWuInfo wsinfo(wuid.str(), qs.str(), qid.str(), context->queryUserId(), context->queryPassword());
  2191. StringBuffer scope; //main, input, result, etc.
  2192. nextPathNode(thepath, scope);
  2193. StringBuffer respath;
  2194. StringBuffer resname;
  2195. StringBuffer restype;
  2196. if (strieq(scope.str(), "resource"))
  2197. {
  2198. StringBuffer ext;
  2199. splitPathTailAndExt(thepath, respath, resname, &ext);
  2200. }
  2201. else
  2202. splitPathTailAndExt(thepath, respath, resname, &restype);
  2203. if (strieq(scope.str(), "resource"))
  2204. {
  2205. StringBuffer blockStr("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  2206. if (request->getParameters()->hasProp("display"))
  2207. blockStr.append("<?xml-stylesheet type=\"text/xsl\" href=\"/esp/xslt/xmlformatter.xsl\"?>");
  2208. wsinfo.getWsResource(respath.str(), blockStr);
  2209. if (blockStr.length())
  2210. {
  2211. response->setStatus("200 OK");
  2212. response->setContent(blockStr.str());
  2213. response->setContentType(HTTP_TYPE_APPLICATION_XML);
  2214. response->send();
  2215. }
  2216. }
  2217. else if (strieq(restype.str(), "wsdl"))
  2218. {
  2219. if (strieq(scope.str(), "main"))
  2220. {
  2221. VStringBuffer dest("/WsEcl/soap/query/%s/%s", qs.str(), qid.str());
  2222. parms->setProp("multiple_resp_schemas", 1);
  2223. parms->setProp("wsdl_destination_path", dest.str());
  2224. return onGetWsdl(*context, request, response, wsinfo);
  2225. }
  2226. }
  2227. else if (strieq(restype.str(), "xsd"))
  2228. {
  2229. if (strieq(scope.str(), "main"))
  2230. {
  2231. parms->setProp("multiple_resp_schemas", 1);
  2232. return onGetXsd(*context, request, response, wsinfo);
  2233. }
  2234. else if (strieq(scope.str(), "input") || strieq(scope.str(), "result"))
  2235. {
  2236. StringBuffer output;
  2237. StringBuffer xsds;
  2238. wsinfo.getSchemas(xsds);
  2239. Owned<IPropertyTree> xsds_tree;
  2240. if (xsds.length())
  2241. xsds_tree.setown(createPTreeFromXMLString(xsds.str()));
  2242. if (xsds_tree)
  2243. {
  2244. StringBuffer xpath;
  2245. Owned<IPropertyTree> selected_xsd;
  2246. StringBuffer urn("urn:hpccsystems:ecl:");
  2247. appendNamespaceSpecificString(urn, wsinfo.queryname.get());
  2248. urn.appendf(":%s:", scope.toLowerCase().str());
  2249. appendNamespaceSpecificString(urn, resname.str());
  2250. if (!stricmp(scope.str(), "input"))
  2251. xpath.appendf("Input[@sname='%s']/xs:schema", resname.str());
  2252. else if (!stricmp(scope.str(), "result"))
  2253. xpath.appendf("Result[@sname='%s']/xs:schema",resname.str());
  2254. if (xpath.length())
  2255. selected_xsd.setown(xsds_tree->getPropTree(xpath.str()));
  2256. if (selected_xsd)
  2257. {
  2258. selected_xsd->setProp("@targetNamespace", urn.str());
  2259. selected_xsd->setProp("@xmlns", urn.str());
  2260. IPropertyTree *dstree = selected_xsd->queryPropTree("xs:element[@name='Dataset']/xs:complexType");
  2261. if (dstree && !dstree->hasProp("xs:attribute[@name='name']"))
  2262. dstree->addPropTree("xs:attribute", createPTreeFromXMLString("<xs:attribute name=\"name\" type=\"xs:string\"/>"));
  2263. Owned<IPropertyTreeIterator> elements = selected_xsd->getElements("xs:element//xs:element");
  2264. ForEach(*elements)
  2265. elements->query().setPropInt("@minOccurs", 0);
  2266. output.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  2267. if (context->queryRequestParameters()->hasProp("display"))
  2268. output.append("<?xml-stylesheet type=\"text/xsl\" href=\"/esp/xslt/xmlformatter.xsl\"?>");
  2269. toXML(selected_xsd, output);
  2270. }
  2271. }
  2272. if (output.length())
  2273. {
  2274. response->setStatus("200 OK");
  2275. response->setContent(output.str());
  2276. response->setContentType(HTTP_TYPE_APPLICATION_XML);
  2277. response->send();
  2278. }
  2279. }
  2280. }
  2281. return 0;
  2282. }
  2283. int CWsEclBinding::getWsEclExample(CHttpRequest* request, CHttpResponse* response, const char *thepath)
  2284. {
  2285. IProperties *parms = request->queryParameters();
  2286. IEspContext *context = request->queryContext();
  2287. StringBuffer exampletype;
  2288. nextPathNode(thepath, exampletype);
  2289. StringBuffer wuid;
  2290. StringBuffer qs;
  2291. StringBuffer qid;
  2292. splitLookupInfo(parms, thepath, wuid, qs, qid);
  2293. WsEclWuInfo wsinfo(wuid.str(), qs.str(), qid.str(), context->queryUserId(), context->queryPassword());
  2294. context->setBindingValue(&wsinfo);
  2295. if (!stricmp(exampletype.str(), "request"))
  2296. return onGetReqSampleXml(*context, request, response, qs.str(), qid.str());
  2297. else if (!stricmp(exampletype.str(), "response"))
  2298. {
  2299. StringBuffer output;
  2300. buildSampleResponseXml(output, *context, request, wsinfo);
  2301. if (output.length())
  2302. {
  2303. response->setStatus("200 OK");
  2304. response->setContent(output.str());
  2305. response->setContentType(HTTP_TYPE_APPLICATION_XML);
  2306. response->send();
  2307. }
  2308. }
  2309. context->setBindingValue(NULL);
  2310. return 0;
  2311. }
  2312. int CWsEclBinding::onGet(CHttpRequest* request, CHttpResponse* response)
  2313. {
  2314. Owned<IMultiException> me = MakeMultiException("WsEcl");
  2315. try
  2316. {
  2317. IEspContext *context = request->queryContext();
  2318. IProperties *parms = request->queryParameters();
  2319. if (!context->validateFeatureAccess(WSECL_ACCESS, SecAccess_Full, false))
  2320. throw MakeStringException(-1, "WsEcl access permission denied.");
  2321. const char *thepath = request->queryPath();
  2322. StringBuffer serviceName;
  2323. firstPathNode(thepath, serviceName);
  2324. if (stricmp(serviceName.str(), "WsEcl"))
  2325. return EspHttpBinding::onGet(request, response);
  2326. StringBuffer methodName;
  2327. nextPathNode(thepath, methodName);
  2328. if (!stricmp(methodName.str(), "tabview"))
  2329. {
  2330. return getWsEcl2TabView(request, response, thepath);
  2331. }
  2332. else if (!stricmp(methodName.str(), "forms"))
  2333. {
  2334. return getWsEcl2Form(request, response, thepath);
  2335. }
  2336. else if (!stricmp(methodName.str(), "submit"))
  2337. {
  2338. StringBuffer wuid;
  2339. StringBuffer qs;
  2340. StringBuffer qid;
  2341. splitLookupInfo(parms, thepath, wuid, qs, qid);
  2342. StringBuffer format;
  2343. nextPathNode(thepath, format);
  2344. WsEclWuInfo wsinfo(wuid.str(), qs.str(), qid.str(), context->queryUserId(), context->queryPassword());
  2345. return onSubmitQueryOutputXML(*context, request, response, wsinfo, format.str());
  2346. }
  2347. else if (!stricmp(methodName.str(), "xslt"))
  2348. {
  2349. StringBuffer wuid;
  2350. StringBuffer qs;
  2351. StringBuffer qid;
  2352. splitLookupInfo(parms, thepath, wuid, qs, qid);
  2353. WsEclWuInfo wsinfo(wuid.str(), qs.str(), qid.str(), context->queryUserId(), context->queryPassword());
  2354. return onSubmitQueryOutputView(*context, request, response, wsinfo);
  2355. }
  2356. else if (!stricmp(methodName.str(), "example"))
  2357. {
  2358. return getWsEclExample(request, response, thepath);
  2359. }
  2360. else if (!stricmp(methodName.str(), "definitions"))
  2361. {
  2362. return getWsEclDefinition(request, response, thepath);
  2363. }
  2364. else if (!stricmp(methodName.str(), "links"))
  2365. {
  2366. StringBuffer wuid;
  2367. StringBuffer qs;
  2368. StringBuffer qid;
  2369. splitLookupInfo(parms, thepath, wuid, qs, qid);
  2370. WsEclWuInfo wsinfo(wuid.str(), qs.str(), qid.str(), context->queryUserId(), context->queryPassword());
  2371. return getWsEclLinks(*context, request, response, wsinfo);
  2372. }
  2373. else if (strieq(methodName.str(), "soap"))
  2374. {
  2375. StringBuffer url;
  2376. url.append("/WsEcl/forms/soap/").append(thepath);
  2377. response->redirect(*request, url);
  2378. return 0;
  2379. }
  2380. }
  2381. catch (IMultiException* mex)
  2382. {
  2383. me->append(*mex);
  2384. mex->Release();
  2385. }
  2386. catch (IException* e)
  2387. {
  2388. me->append(*e);
  2389. }
  2390. catch (...)
  2391. {
  2392. me->append(*MakeStringExceptionDirect(-1, "Unknown Exception"));
  2393. }
  2394. response->handleExceptions(getXslProcessor(), me, "WsEcl", "", StringBuffer(getCFD()).append("./smc_xslt/exceptions.xslt").str());
  2395. return 0;
  2396. }
  2397. void checkForXmlResponseName(StartTag &starttag, StringBuffer &respname, int &soaplevel)
  2398. {
  2399. if (respname.length())
  2400. return;
  2401. switch(soaplevel)
  2402. {
  2403. case 0:
  2404. {
  2405. if (!stricmp(starttag.getLocalName(), "Envelope"))
  2406. soaplevel=1;
  2407. else
  2408. respname.append(starttag.getLocalName());
  2409. break;
  2410. }
  2411. case 1:
  2412. if (!stricmp(starttag.getLocalName(), "Body"))
  2413. soaplevel=2;
  2414. break;
  2415. case 2:
  2416. respname.append(starttag.getLocalName());
  2417. default:
  2418. break;
  2419. }
  2420. int len=respname.length();
  2421. if (len>8 && !stricmp(respname.str()+len-8, "Response"))
  2422. respname.setLength(len-8);
  2423. }
  2424. void createPTreeFromJsonString(const char *json, bool caseInsensitive, StringBuffer &xml, const char *tail);
  2425. void CWsEclBinding::handleJSONPost(CHttpRequest *request, CHttpResponse *response)
  2426. {
  2427. IEspContext *ctx = request->queryContext();
  2428. IProperties *parms = request->queryParameters();
  2429. StringBuffer jsonresp;
  2430. try
  2431. {
  2432. if (!ctx->validateFeatureAccess(WSECL_ACCESS, SecAccess_Full, false))
  2433. throw MakeStringException(-1, "WsEcl access permission denied.");
  2434. const char *thepath = request->queryPath();
  2435. StringBuffer serviceName;
  2436. firstPathNode(thepath, serviceName);
  2437. if (!strieq(serviceName.str(), "WsEcl"))
  2438. EspHttpBinding::handleHttpPost(request, response);
  2439. StringBuffer action;
  2440. nextPathNode(thepath, action);
  2441. StringBuffer lookup;
  2442. nextPathNode(thepath, lookup);
  2443. StringBuffer wuid;
  2444. StringBuffer queryset;
  2445. StringBuffer queryname;
  2446. if (strieq(lookup.str(), "wuid"))
  2447. {
  2448. nextPathNode(thepath, wuid);
  2449. queryset.append(parms->queryProp("qset"));
  2450. queryname.append(parms->queryProp("qname"));
  2451. }
  2452. else if (strieq(lookup.str(), "query"))
  2453. {
  2454. nextPathNode(thepath, queryset);
  2455. nextPathNode(thepath, queryname);
  2456. }
  2457. WsEclWuInfo wsinfo(wuid.str(), queryset.str(), queryname.str(), ctx->queryUserId(), ctx->queryPassword());
  2458. StringBuffer content(request->queryContent());
  2459. StringBuffer status;
  2460. StringBuffer soapfromjson;
  2461. soapfromjson.append(
  2462. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  2463. "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\""
  2464. " xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\">"
  2465. " <soap:Body>"
  2466. );
  2467. createPTreeFromJsonString(content.str(), false, soapfromjson, "Request");
  2468. soapfromjson.append("</soap:Body></soap:Envelope>");
  2469. DBGLOG("soap from json req: %s", soapfromjson.str());
  2470. StringBuffer soapresp;
  2471. SCMStringBuffer clustertype;
  2472. wsinfo.wu->getDebugValue("targetclustertype", clustertype);
  2473. unsigned xmlflags = WWV_ADD_SOAP | WWV_ADD_RESULTS_TAG | WWV_ADD_RESPONSE_TAG | WWV_INCL_NAMESPACES | WWV_INCL_GENERATED_NAMESPACES;
  2474. if (ctx->queryRequestParameters()->hasProp("display"))
  2475. xmlflags |= WWV_USE_DISPLAY_XSLT;
  2476. if (streq(action.str(), "expanded"))
  2477. xmlflags |= WWV_CDATA_SCHEMAS;
  2478. else
  2479. xmlflags |= WWV_OMIT_SCHEMAS;
  2480. if (strieq(clustertype.str(), "roxie"))
  2481. {
  2482. StringBuffer output;
  2483. sendRoxieRequest(wsinfo.qsetname.get(), soapfromjson, output, status, wsinfo.queryname);
  2484. Owned<IWuWebView> web = createWuWebView(*wsinfo.wu, NULL, getCFD(), true);
  2485. if (web.get())
  2486. web->expandResults(output.str(), soapresp, xmlflags);
  2487. }
  2488. else
  2489. {
  2490. submitWsEclWorkunit(*ctx, wsinfo, soapfromjson.str(), soapresp, xmlflags);
  2491. }
  2492. DBGLOG("HandleSoapRequest response: %s", soapresp.str());
  2493. getWsEclJsonResponse(jsonresp, *ctx, request, soapresp.str(), wsinfo);
  2494. }
  2495. catch (IException *e)
  2496. {
  2497. appendJSONException(jsonresp.set("{"), e);
  2498. jsonresp.append('}');
  2499. }
  2500. response->setContent(jsonresp.str());
  2501. response->setContentType("application/json");
  2502. response->setStatus("200 OK");
  2503. response->send();
  2504. }
  2505. void CWsEclBinding::handleHttpPost(CHttpRequest *request, CHttpResponse *response)
  2506. {
  2507. StringBuffer ct;
  2508. request->getContentType(ct);
  2509. if (!strnicmp(ct.str(), "application/json", 16))
  2510. {
  2511. handleJSONPost(request, response);
  2512. }
  2513. else
  2514. {
  2515. EspHttpBinding::handleHttpPost(request, response);
  2516. }
  2517. }
  2518. int CWsEclBinding::HandleSoapRequest(CHttpRequest* request, CHttpResponse* response)
  2519. {
  2520. IEspContext *ctx = request->queryContext();
  2521. IProperties *parms = request->queryParameters();
  2522. const char *thepath = request->queryPath();
  2523. StringBuffer serviceName;
  2524. firstPathNode(thepath, serviceName);
  2525. if (!strieq(serviceName.str(), "WsEcl"))
  2526. return CHttpSoapBinding::HandleSoapRequest(request, response);
  2527. if(ctx->toBeAuthenticated()) //future support WsSecurity tags?
  2528. {
  2529. response->sendBasicChallenge(getChallengeRealm(), false);
  2530. return 0;
  2531. }
  2532. if (!ctx->validateFeatureAccess(WSECL_ACCESS, SecAccess_Full, false))
  2533. throw MakeStringException(-1, "WsEcl access permission denied.");
  2534. StringBuffer action;
  2535. nextPathNode(thepath, action);
  2536. StringBuffer lookup;
  2537. nextPathNode(thepath, lookup);
  2538. StringBuffer wuid;
  2539. StringBuffer queryset;
  2540. StringBuffer queryname;
  2541. if (strieq(lookup.str(), "wuid"))
  2542. {
  2543. nextPathNode(thepath, wuid);
  2544. queryset.append(parms->queryProp("qset"));
  2545. queryname.append(parms->queryProp("qname"));
  2546. }
  2547. else if (strieq(lookup.str(), "query"))
  2548. {
  2549. nextPathNode(thepath, queryset);
  2550. nextPathNode(thepath, queryname);
  2551. }
  2552. WsEclWuInfo wsinfo(wuid.str(), queryset.str(), queryname.str(), ctx->queryUserId(), ctx->queryPassword());
  2553. StringBuffer content(request->queryContent());
  2554. StringBuffer soapresp;
  2555. StringBuffer status;
  2556. SCMStringBuffer clustertype;
  2557. wsinfo.wu->getDebugValue("targetclustertype", clustertype);
  2558. unsigned xmlflags = WWV_ADD_SOAP | WWV_ADD_RESULTS_TAG | WWV_ADD_RESPONSE_TAG | WWV_INCL_NAMESPACES | WWV_INCL_GENERATED_NAMESPACES;
  2559. if (ctx->queryRequestParameters()->hasProp("display"))
  2560. xmlflags |= WWV_USE_DISPLAY_XSLT;
  2561. if (streq(action.str(), "expanded"))
  2562. xmlflags |= WWV_CDATA_SCHEMAS;
  2563. else
  2564. xmlflags |= WWV_OMIT_SCHEMAS;
  2565. if (strieq(clustertype.str(), "roxie"))
  2566. {
  2567. StringBuffer content(request->queryContent());
  2568. StringBuffer output;
  2569. sendRoxieRequest(wsinfo.qsetname.get(), content, output, status, wsinfo.queryname);
  2570. Owned<IWuWebView> web = createWuWebView(*wsinfo.wu, wsinfo.queryname.get(), getCFD(), true);
  2571. if (web.get())
  2572. web->expandResults(output.str(), soapresp, xmlflags);
  2573. }
  2574. else
  2575. submitWsEclWorkunit(*ctx, wsinfo, content.str(), soapresp, xmlflags);
  2576. DBGLOG("HandleSoapRequest response: %s", soapresp.str());
  2577. response->setContent(soapresp.str());
  2578. response->setContentType("text/xml");
  2579. response->setStatus("200 OK");
  2580. response->send();
  2581. return 0;
  2582. }