AsynchRequest.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Viewer
  5. *| (C) Copyright IBM Corp. 2001, 2018
  6. *|
  7. *| US Government Users Restricted Rights - Use, duplication or
  8. *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. *|
  10. *+------------------------------------------------------------------------+
  11. */
  12. function AsynchRequest(gateway, webContentRoot) {
  13. // initialize the base class
  14. AsynchRequest.baseConstructor.call(this);
  15. this.m_gateway = gateway;
  16. this.m_webContentRoot = webContentRoot;
  17. this.m_callbacks = {};
  18. this.m_soapFault = null;
  19. this.m_faultDialog = null;
  20. this.m_promptDialog = null;
  21. this.m_logonDialog = null;
  22. }
  23. // set up the base class
  24. AsynchRequest.prototype = new XmlHttpObject();
  25. AsynchRequest.baseConstructor = XmlHttpObject;
  26. AsynchRequest.prototype.parent = XmlHttpObject.prototype;
  27. AsynchRequest.prototype.getTracking = function() {return "";}; // treat as an abstract method
  28. AsynchRequest.prototype.getConversation = function() {return "";}; // treat as an abstract method
  29. AsynchRequest.prototype.getPrimaryAction = function() {return "";}; // treat as an abstract method
  30. AsynchRequest.prototype.getActionState = function() {return "";}; // treat as an abstract method
  31. AsynchRequest.prototype.getAsynchStatus = function() {return "";}; // treat as an abstract method
  32. AsynchRequest.prototype.getResult = function() {return null;}; // treat as an abstract method
  33. AsynchRequest.prototype.getSoapFault = function() { return this.m_soapFault; };
  34. AsynchRequest.prototype.constructFaultEnvelope = function() { return null; }; // treat as an abstract method
  35. AsynchRequest.prototype.getPromptHTMLFragment = function() {return "";};// treat as an abstract method
  36. AsynchRequest.prototype.isRAPWaitTrue = function() {return false;}; // treat as an abstract method
  37. AsynchRequest.prototype.getRAPRequestCache = function() { return null;}; // treat as an abstract method
  38. AsynchRequest.prototype.getMainConversation = function() { return null;}; // treat as an abstract method
  39. AsynchRequest.prototype.getMainTracking = function() { return null;}; // treat as an abstract method
  40. AsynchRequest.prototype.construct = function() {}; // treat as an abstract method
  41. /**
  42. * Executes a callback. Returns false if the callback wasn't found
  43. * @param {Object} callback
  44. */
  45. AsynchRequest.prototype.executeCallback = function(callback) {
  46. if (this.m_callbacks[callback]) {
  47. var callbackArguments = this.concatResponseArguments(this.m_callbacks.customArguments);
  48. var callbackFunc = GUtil.generateCallback(this.m_callbacks[callback].method, callbackArguments, this.m_callbacks[callback].object);
  49. callbackFunc();
  50. return true;
  51. }
  52. return false;
  53. };
  54. AsynchRequest.prototype.setCallbacks = function(callbacks) {
  55. if (!this.m_callbacks) {
  56. this.m_callbacks = {};
  57. }
  58. for (callback in callbacks) {
  59. this.m_callbacks[callback] = callbacks[callback];
  60. }
  61. };
  62. AsynchRequest.prototype.getCallbacks = function() {
  63. return this.m_callbacks;
  64. };
  65. AsynchRequest.prototype.newRequest = function() {
  66. var asynchRequest = this.construct();
  67. // copy any headers over
  68. asynchRequest.setHeaders(this.getHeaders());
  69. if (this.getFormFields().exists("b_action")) {
  70. asynchRequest.addFormField("b_action", this.getFormField("b_action"));
  71. }
  72. if (this.getFormFields().exists("cv.catchLogOnFault")) {
  73. asynchRequest.addFormField("cv.catchLogOnFault", this.getFormField("cv.catchLogOnFault"));
  74. }
  75. asynchRequest.setPromptDialog(this.m_promptDialog);
  76. asynchRequest.setFaultDialog(this.m_faultDialog);
  77. asynchRequest.setLogonDialog(this.m_logonDialog);
  78. asynchRequest.m_asynch = this.m_asynch;
  79. // if the request was sent using the dispatcher queue,
  80. // then it needs to be notified of the new request object
  81. if (this.m_callbacks.newRequest) {
  82. var newRequestCallback = GUtil.generateCallback(this.m_callbacks.newRequest.method, [asynchRequest], this.m_callbacks.newRequest.object);
  83. newRequestCallback();
  84. }
  85. return asynchRequest;
  86. };
  87. AsynchRequest.prototype.success = function() {
  88. var asynchStatus = this.getAsynchStatus();
  89. switch(asynchStatus) {
  90. case "stillWorking":
  91. case "working":
  92. this.working();
  93. break;
  94. case "prompting":
  95. this.prompting();
  96. break;
  97. case "fault":
  98. case "complete":
  99. case "conversationComplete":
  100. this.complete();
  101. break;
  102. default:
  103. // the AsynchRequest class is sometimes used for non-asynch type requests.
  104. // if we diddn't get a status back simply call the complete callback
  105. this.complete();
  106. break;
  107. }
  108. };
  109. AsynchRequest.prototype.setFaultDialog = function(faultDialog) {
  110. if(faultDialog instanceof IFaultDialog) {
  111. if(typeof console != "undefined") {
  112. console.log("AsynchRequest.prototype.setFaultDialog is deprecated");
  113. }
  114. this.m_faultDialog = faultDialog;
  115. } else if(faultDialog && typeof console != "undefined") {
  116. console.log("The parameter faultDialog must be an instance of IFaultDialog");
  117. }
  118. };
  119. AsynchRequest.prototype.setPromptDialog = function(promptDialog) {
  120. if(promptDialog instanceof IPromptDialog) {
  121. if(typeof console != "undefined") {
  122. console.log("AsynchRequest.prototype.setPromptDialog is deprecated");
  123. }
  124. this.m_promptDialog = promptDialog;
  125. } else if(promptDialog && typeof console != "undefined") {
  126. console.log("The parameter promptDialog must be an instance of IPromptDialog");
  127. }
  128. };
  129. AsynchRequest.prototype.setLogonDialog = function(logonDialog) {
  130. if(logonDialog instanceof ILogOnDialog) {
  131. if(typeof console != "undefined") {
  132. console.log("AsynchRequest.prototype.setLogonDialog is deprecated");
  133. }
  134. this.m_logonDialog = logonDialog;
  135. } else if(logonDialog && typeof console != "undefined") {
  136. console.log("The parameter logOnDialog must be an instance of ILogOnDialog");
  137. }
  138. };
  139. AsynchRequest.prototype.resubmitRequest = function() {
  140. var asynchRequest = this.newRequest();
  141. asynchRequest.m_formFields = this.m_formFields;
  142. asynchRequest.sendRequest();
  143. return asynchRequest;
  144. };
  145. AsynchRequest.prototype.sendRequest = function() {
  146. var asynchRequest = this;
  147. var callbacks = {
  148. "complete":{"object":asynchRequest,"method":asynchRequest.successHandler},
  149. "fault":{"object":asynchRequest,"method":asynchRequest.errorHandler}
  150. };
  151. this.init("POST", this.m_gateway, "", this.m_asynch);
  152. this.executeCallback("preHttpRequest");
  153. this.parent.setCallbacks.call(this, callbacks);
  154. this.parent.sendRequest.call(this);
  155. };
  156. AsynchRequest.prototype.errorHandler = function() {
  157. this.executeCallback("postHttpRequest");
  158. // let the dispatcher queue know the request is done
  159. this.executeCallback("entryFault");
  160. this.executeCallback("error");
  161. };
  162. AsynchRequest.prototype.successHandler = function() {
  163. this.executeCallback("postHttpRequest");
  164. // hide any currently open dialogs
  165. if(typeof window["AsynchRequestPromptDialog"] != "undefined" && window["AsynchRequestPromptDialog"] != null) {
  166. window["AsynchRequestPromptDialog"].hide();
  167. window["AsynchRequestPromptDialog"] = null;
  168. }
  169. // check to see if dispatch caught the request and returned a login dialog
  170. if(this.getResponseHeader("Content-type").indexOf("text/html") != -1) {
  171. var responseText = this.getResponseText();
  172. if(responseText.indexOf("<ERROR_CODE>CAM_PASSPORT_ERROR</ERROR_CODE>") != -1) {
  173. this.passportTimeout();
  174. } else if ((responseText.indexOf("http-equiv=\"refresh\"") != -1) || (responseText.indexOf("http-equiv='refresh'") != -1)) {
  175. this.passportTimeout();
  176. } else {
  177. // let the dispatcher queue know the request is done
  178. this.executeCallback("entryFault");
  179. if (!this.executeCallback("fault")) {
  180. // unknown/unexpected html response, throw it up in a new window
  181. var htmlWindow = window.open("","",'height=400,width=500');
  182. if(htmlWindow != null) {
  183. htmlWindow.document.write(responseText);
  184. }
  185. }
  186. }
  187. } else {
  188. // verify we didn't receive a fault
  189. this.m_soapFault = this.constructFaultEnvelope();
  190. if(this.m_soapFault != null) {
  191. var camElement = XMLHelper_FindChildByTagName(this.m_soapFault, "CAM", true);
  192. if(camElement != null && XMLHelper_FindChildByTagName(camElement, "promptInfo", true)) {
  193. this.passportTimeout();
  194. } else {
  195. this.fault();
  196. }
  197. } else {
  198. this.success();
  199. }
  200. }
  201. };
  202. AsynchRequest.prototype.cancel = function() {
  203. this.parent.cancel.call(this);
  204. // Create a new basic object to do the cancel
  205. var tracking = this.getFormField("m_tracking");
  206. if (tracking) {
  207. var request = new XmlHttpObject();
  208. request.init("POST", this.m_gateway, "", false);
  209. if (this.getFormField("cv.outputKey")) {
  210. request.addFormField("b_action", "cvx.high");
  211. request.addFormField("cv.outputKey", this.getFormField("cv.outputKey"));
  212. request.setHeaders(this.getHeaders());
  213. }
  214. else {
  215. request.addFormField("b_action", "cognosViewer");
  216. }
  217. request.addFormField("cv.responseFormat", "successfulRequest");
  218. request.addFormField("ui.action", "cancel");
  219. request.addFormField("m_tracking", tracking);
  220. if (this.getFormField("cv.debugDirectory")) {
  221. request.addFormField("cv.debugDirectory", this.getFormField("cv.debugDirectory"));
  222. }
  223. request.sendRequest();
  224. this.executeCallback("cancel");
  225. }
  226. };
  227. AsynchRequest.prototype.working = function() {
  228. this.executeCallback("working");
  229. // ALWAYS handle the working internally
  230. var asynchRequest = this.newRequest();
  231. asynchRequest.addFormField("m_tracking", this.getTracking());
  232. if (this.getFormField("cv.outputKey")) {
  233. asynchRequest.addFormField("cv.outputKey", this.getFormField("cv.outputKey"));
  234. asynchRequest.addFormField("b_action", "cvx.high");
  235. }
  236. if (this.isRAPWaitTrue()) {
  237. // if rapWait is true, we can to send all parameters with current tracking again.
  238. asynchRequest.m_formFields = this.m_formFields;
  239. asynchRequest.addFormField("m_tracking", this.getTracking());
  240. asynchRequest.addFormField("rapWait", "true");
  241. var requestCache = this.getRAPRequestCache();
  242. if (requestCache !== null && typeof requestCache != "undefined" ) {
  243. asynchRequest.addFormField("rapRequestCache", requestCache);
  244. }
  245. var mainConversation = this.getMainConversation();
  246. if (mainConversation) {
  247. asynchRequest.addFormField("mainConversation", mainConversation);
  248. }
  249. var mainTracking = this.getMainTracking();
  250. if (mainTracking) {
  251. asynchRequest.addFormField("mainTracking", mainTracking);
  252. }
  253. } else {
  254. /**
  255. * ****************************************************************************
  256. * ANY CHANGES TO THESE PARAMETERS MUST ALSO BE MADE IN MobileXmlOutput.java
  257. * ****************************************************************************
  258. */
  259. asynchRequest.addFormField("ui.action", "wait");
  260. asynchRequest.addFormField("ui.primaryAction", this.getPrimaryAction());
  261. asynchRequest.addFormField("cv.actionState", this.getActionState());
  262. if (this.getFormField("ui.preserveRapTags")) {
  263. asynchRequest.addFormField("ui.preserveRapTags", this.getFormField("ui.preserveRapTags"));
  264. }
  265. if (this.getFormField("ui.backURL")) {
  266. asynchRequest.addFormField("ui.backURL", this.getFormField("ui.backURL"));
  267. }
  268. if (this.getFormField("errURL")) {
  269. asynchRequest.addFormField("errURL", this.getFormField("errURL"));
  270. }
  271. if (this.getFormField("cv.showFaultPage")) {
  272. asynchRequest.addFormField("cv.showFaultPage", this.getFormField("cv.showFaultPage"));
  273. }
  274. if (this.getFormField("cv.catchLogOnFault")) {
  275. asynchRequest.addFormField("cv.catchLogOnFault", this.getFormField("cv.catchLogOnFault"));
  276. }
  277. }
  278. if (this.getFormField("bux")) {
  279. asynchRequest.addFormField("bux", this.getFormField("bux"));
  280. }
  281. if ( this.getFormField("cv.debugDirectory")) {
  282. asynchRequest.addFormField("cv.debugDirectory", this.getFormField("cv.debugDirectory"));
  283. }
  284. asynchRequest.sendRequest();
  285. };
  286. AsynchRequest.prototype.prompting = function() {
  287. // let the dispatcher queue know the request is done
  288. this.executeCallback("entryComplete");
  289. if(!this.executeCallback("prompting")) {
  290. if(this.m_promptDialog != null){
  291. this.showPromptPage();
  292. } else if(typeof console != "undefined") {
  293. console.log("An unhandled prompt response was returned: %o", this.xmlHttp);
  294. }
  295. }
  296. this.executeCallback("postEntryComplete");
  297. };
  298. AsynchRequest.prototype.promptPageOkCallback = function(promptValues) {
  299. var asynchRequest = this.newRequest();
  300. asynchRequest.addFormField("ui.action", "forward");
  301. asynchRequest.addFormField("m_tracking", this.getTracking());
  302. asynchRequest.addFormField("ui.conversation", this.getConversation());
  303. asynchRequest.addFormField("ui.primaryAction", this.getPrimaryAction());
  304. asynchRequest.addFormField("cv.actionState", this.getActionState());
  305. for(var promptValue in promptValues) {
  306. asynchRequest.addFormField(promptValue, promptValues[promptValue]);
  307. }
  308. asynchRequest.sendRequest();
  309. window["AsynchRequestObject"] = null;
  310. };
  311. AsynchRequest.prototype.promptPageCancelCallback = function() {
  312. window["AsynchRequestPromptDialog"].hide();
  313. this.complete();
  314. };
  315. AsynchRequest.prototype.showPromptPage = function() {
  316. window["AsynchRequestObject"] = this;
  317. window["AsynchRequestPromptDialog"] = this.m_promptDialog;
  318. var cvIdParam = this.m_promptDialog.getViewerId() == null ? "" : "?cv.id=" + this.m_promptDialog.getViewerId();
  319. window["AsynchRequestPromptDialog"].initialize(this.m_webContentRoot + "/rv/showStandalonePrompts.html" + cvIdParam, 400, 400);
  320. window["AsynchRequestPromptDialog"].show();
  321. };
  322. AsynchRequest.prototype.passportTimeout = function() {
  323. // let the dispatcher queue know the request is done
  324. this.executeCallback("entryFault");
  325. if(!this.executeCallback("passportTimeout")) {
  326. if(this.m_logonDialog != null) {
  327. this.m_logonDialog.show(response.getSoapFault());
  328. } else if(typeof console != "undefined") {
  329. console.log("An unhandled passport timeout fault was returned: %o", this.getSoapFault());
  330. }
  331. }
  332. };
  333. AsynchRequest.prototype.fault = function() {
  334. // let the dispatcher queue know the request is done
  335. this.executeCallback("entryFault");
  336. if(!this.executeCallback("fault")) {
  337. if(this.m_faultDialog != null) {
  338. this.m_faultDialog.show(this.getSoapFault());
  339. } else if(typeof console != "undefined") {
  340. console.log("An unhandled soap fault was returned: %o", this.getSoapFault());
  341. }
  342. }
  343. };
  344. AsynchRequest.prototype.complete = function() {
  345. // let the dispatcher queue know the request is done
  346. this.executeCallback("entryComplete");
  347. this.executeCallback("complete");
  348. this.executeCallback( "postEntryComplete" );
  349. };
  350. AsynchRequest.prototype.getSoapFaultCode = function() {
  351. var soapFault = this.constructFaultEnvelope();
  352. if(soapFault) {
  353. var faultCode = XMLHelper_FindChildByTagName(soapFault, "faultcode", true);
  354. if(faultCode != null) {
  355. return XMLHelper_GetText(faultCode);
  356. }
  357. }
  358. return null;
  359. };
  360. AsynchRequest.prototype.getSoapFaultDetailMessageString = function() {
  361. var soapFault = this.constructFaultEnvelope();
  362. if(soapFault) {
  363. var entry = XMLHelper_FindChildByTagName(soapFault, "messageString", true);
  364. if(entry != null) {
  365. return XMLHelper_GetText(entry);
  366. }
  367. }
  368. return null;
  369. };