XmlHttpObject.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Viewer
  5. *| (C) Copyright IBM Corp. 2001, 2012
  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 XmlHttpObject() {
  13. this.m_formFields = new CDictionary();
  14. this.xmlHttp = XmlHttpObject.createRequestObject();
  15. this.m_requestIndicator = null;
  16. this.m_httpCallbacks = {};
  17. this.m_asynch = true;
  18. this.m_headers = null;
  19. }
  20. XmlHttpObject.prototype.setHeaders = function(headers) {
  21. this.m_headers = headers;
  22. };
  23. XmlHttpObject.prototype.getHeaders = function() {
  24. return this.m_headers;
  25. };
  26. XmlHttpObject.prototype.newRequest = function() {
  27. var request = new XmlHttpObject();
  28. request.init(this.m_action, this.m_gateway, this.m_url, this.m_asynch);
  29. // if the request was sent using the dispatcher queue,
  30. // then it needs to be notified of the new request object
  31. this.executeHttpCallback("newRequest");
  32. return request;
  33. };
  34. XmlHttpObject.prototype.abortHttpRequest = function() {
  35. if(this.xmlHttp != null) {
  36. this.xmlHttp.abort();
  37. this.xmlHttp = null;
  38. this.executeHttpCallback("cancel");
  39. this.m_httpCallbacks = {};
  40. }
  41. };
  42. XmlHttpObject.prototype.cancel = function() {
  43. this.abortHttpRequest();
  44. };
  45. /**
  46. * Executes a callback. Returns false if the callback wasn't found
  47. * @param {Object} callback
  48. */
  49. XmlHttpObject.prototype.executeHttpCallback = function(callback) {
  50. if (this.m_httpCallbacks && this.m_httpCallbacks[callback]) {
  51. var callbackArguments = this.concatResponseArguments(this.m_httpCallbacks.customArguments);
  52. var callbackFunc = GUtil.generateCallback(this.m_httpCallbacks[callback].method, callbackArguments, this.m_httpCallbacks[callback].object);
  53. callbackFunc();
  54. return true;
  55. }
  56. return false;
  57. };
  58. /**
  59. * Will add callbacks to the existing callback and will replace any conflicting callbacks
  60. * @param {Object} callbacks
  61. */
  62. XmlHttpObject.prototype.setCallbacks = function(callbacks) {
  63. if (!this.m_httpCallbacks) {
  64. this.m_httpCallbacks = {};
  65. }
  66. for (callback in callbacks) {
  67. this.m_httpCallbacks[callback] = callbacks[callback];
  68. }
  69. };
  70. XmlHttpObject.prototype.getCallbacks = function() {
  71. return this.m_httpCallbacks;
  72. };
  73. XmlHttpObject.createRequestObject = function() {
  74. var request = null;
  75. if (window.XMLHttpRequest) {
  76. request = new XMLHttpRequest(); // Firefox, Safari, ...
  77. } else if (window.ActiveXObject) {
  78. request = new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
  79. } else {
  80. //throw error. exit.
  81. }
  82. return request;
  83. };
  84. XmlHttpObject.prototype.waitForXmlHttpResponse = function() {
  85. //explicit so there's no confusion
  86. var request = this.xmlHttp;
  87. if (request && request.readyState === 4) {
  88. if(request.status === 200) {
  89. this.httpSuccess();
  90. } else {
  91. this.httpError();
  92. }
  93. } else {
  94. //wait...
  95. }
  96. };
  97. XmlHttpObject.prototype.init = function(action, gateway, url, asynch) {
  98. this.m_action = action;
  99. this.m_gateway = gateway;
  100. this.m_url = url;
  101. this.m_asynch = asynch;
  102. };
  103. XmlHttpObject.prototype.httpSuccess = function() {
  104. this.executeHttpCallback("postHttpRequest");
  105. // let the dispatcherQueue know the request is done
  106. this.executeHttpCallback("entryComplete");
  107. this.executeHttpCallback("complete");
  108. this.m_httpCallbacks = null;
  109. };
  110. XmlHttpObject.prototype.httpError = function() {
  111. // let the dispastcher queue know a fault happened
  112. this.executeHttpCallback("entryFault");
  113. this.executeHttpCallback("fault");
  114. this.m_httpCallbacks = null;
  115. };
  116. XmlHttpObject.prototype.forceSynchronous = function() {
  117. this.m_asynch = false;
  118. };
  119. XmlHttpObject.prototype.sendRequest = function() {
  120. this.sendHtmlRequest(this.m_action, this.m_gateway, this.m_url, this.m_asynch);
  121. };
  122. XmlHttpObject.prototype.sendHtmlRequest = function(action, gateway, url, async) {
  123. var request = this.xmlHttp;
  124. if (request) {
  125. request.open(action, gateway, async);
  126. //should be after the "open" for best results...
  127. if (async) {
  128. request.onreadystatechange = GUtil.generateCallback(this.waitForXmlHttpResponse, [], this);
  129. } else {
  130. request.onreadystatechange = GUtil.generateCallback(this.waitForXmlHttpResponse, [], this); // IE needs this.
  131. if (!isIE()) {
  132. request.onload = GUtil.generateCallback(this.httpSuccess, [], this);
  133. request.onerror = GUtil.generateCallback(this.httpError, [], this);
  134. }
  135. }
  136. request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  137. if (this.m_headers) {
  138. for (header in this.m_headers) {
  139. request.setRequestHeader(header, this.m_headers[header]);
  140. }
  141. }
  142. this.executeHttpCallback("preHttpRequest");
  143. var requestBody = this.convertFormFieldsToUrl();
  144. if( url ){
  145. requestBody += url;
  146. }
  147. request.send( requestBody );
  148. }
  149. };
  150. XmlHttpObject.prototype.getResponseXml = function() {
  151. return ( this.xmlHttp ) ? this.xmlHttp.responseXML : null;
  152. };
  153. XmlHttpObject.prototype.getResponseText = function() {
  154. return (this.xmlHttp) ? this.xmlHttp.responseText : "";
  155. };
  156. XmlHttpObject.prototype.getResponseHeader = function(item) {
  157. return (this.xmlHttp ) ? this.xmlHttp.getResponseHeader(item) : null;
  158. };
  159. XmlHttpObject.prototype.getStatus = function() {
  160. return this.xmlHttp.status;
  161. };
  162. XmlHttpObject.prototype.addFormField = function(name, value) {
  163. this.m_formFields.add(name, value);
  164. };
  165. XmlHttpObject.prototype.getFormFields = function() {
  166. return this.m_formFields;
  167. };
  168. XmlHttpObject.prototype.getFormField = function(formField) {
  169. return this.m_formFields.get(formField);
  170. };
  171. XmlHttpObject.prototype.clearFormFields = function() {
  172. this.m_formFields = new CDictionary();
  173. };
  174. XmlHttpObject.prototype.convertFormFieldsToUrl = function() {
  175. var url = "";
  176. var formFieldNames = this.m_formFields.keys();
  177. for (var index = 0; index < formFieldNames.length; index++)
  178. {
  179. if (index > 0) {
  180. url += "&";
  181. }
  182. url += encodeURIComponent(formFieldNames[index]) + "=" + encodeURIComponent(this.m_formFields.get(formFieldNames[index]));
  183. }
  184. return url;
  185. };
  186. XmlHttpObject.prototype.concatResponseArguments = function(customArguments) {
  187. var responseArguments = [this];
  188. if(customArguments) {
  189. responseArguments = responseArguments.concat(customArguments);
  190. }
  191. return responseArguments;
  192. };