cpsweb.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Licensed Materials - Property of IBM
  2. //
  3. // IBM Cognos Products: cpscrn
  4. //
  5. // (C) Copyright IBM Corp. 2005, 2014
  6. //
  7. // US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. //
  9. //
  10. (function() {
  11. // define namespace
  12. if (!window.com_ibm_cognos_cps){
  13. com_ibm_cognos_cps = {}
  14. }
  15. if (com_ibm_cognos_cps.ApplicationContext){
  16. // Stop if the ApplicationContext is alread defined.
  17. return;
  18. }
  19. // Helper object used to create the xmlHttpRequest based on the current browser
  20. var CPSConfig = {
  21. browser: 'unknown',
  22. browserVersion: 'unknown',
  23. OS: 'unknown',
  24. xmlHttpDefault: null,
  25. initialize: function()
  26. {
  27. this.browser = this.lookup(this.browsers).toLowerCase() || 'unknown';
  28. this.browserVersion = this.parseVersion(navigator.userAgent) || this.parseVersion(navigator.appVersion) || 'unknown';
  29. this.OS = this.lookup(this.systems) || 'unknown';
  30. this.xmlHttpDefault = this.findXMLHttpActiveXVersion();
  31. },
  32. lookup: function(data)
  33. {
  34. var i, l = data.length;
  35. for (i = 0; i < l; i++)
  36. {
  37. this.versionKey = data[i].partialKey || data[i].identity;
  38. var agent = data[i].agent;
  39. if (agent)
  40. {
  41. if (agent.indexOf(data[i].key) != -1)
  42. return data[i].identity;
  43. }
  44. else if (data[i].prop)
  45. return data[i].identity;
  46. }
  47. },
  48. parseVersion: function(s)
  49. {
  50. var index = s.indexOf(this.versionKey);
  51. if (index == -1)
  52. return;
  53. return parseFloat(s.substring(index + this.versionKey.length + 1).replace(/[^\d\.\-\+]/g, '_')); //sanitize before parse to avoid XSS vulnerability!
  54. },
  55. findXMLHttpActiveXVersion: function()
  56. {
  57. if (window.ActiveXObject)
  58. {
  59. var i, l = this.xmlHttpVersions.length;
  60. for (i = 0; i < l; i++)
  61. {
  62. try
  63. {
  64. // Try and create the ActiveXObject for Internet Explorer, if it doesn't work, try again.
  65. var xmlhttp = new ActiveXObject(this.xmlHttpVersions[i]);
  66. if (xmlhttp)
  67. return this.xmlHttpVersions[i];
  68. }
  69. catch (e)
  70. {
  71. // this ActiveX is not there, continue with next one in list
  72. }
  73. }
  74. }
  75. return null;
  76. },
  77. browsers: [
  78. { agent: navigator.userAgent, key: 'MSIE', identity: 'Explorer', partialKey: 'MSIE' },
  79. { agent: navigator.userAgent, key: 'Firefox', identity: 'Firefox' },
  80. { agent: navigator.userAgent, key: 'Gecko', identity: 'Mozilla', partialKey: 'rv' },
  81. { agent: navigator.userAgent, key: 'Mozilla', identity: 'Netscape', partialKey: 'Mozilla' },
  82. { agent: navigator.userAgent, key: 'Netscape', identity: 'Netscape' },
  83. { prop: window.opera, identity: 'Opera' },
  84. { agent: navigator.vendor, key: 'Apple', identity: 'Safari' }
  85. ],
  86. systems: [
  87. { agent: navigator.platform, key: 'Win', identity: 'Windows' },
  88. { agent: navigator.platform, key: 'Mac', identity: 'Mac' },
  89. { agent: navigator.platform, key: 'Linux', identity: 'Linux' }
  90. ],
  91. xmlHttpVersions: [
  92. 'Msxml2.XMLHTTP.6.0',
  93. 'Msxml2.XMLHTTP.3.0',
  94. 'Msxml2.XMLHTTP',
  95. 'Microsoft.XMLHTTP'
  96. ]
  97. }
  98. CPSConfig.initialize();
  99. /*
  100. Helper DOM utils.
  101. */
  102. com_ibm_cognos_cps._F_DOM =
  103. {
  104. selectNodes: function(node, xpath)
  105. {
  106. if (document.all)
  107. {
  108. var arr = new Array();
  109. var nodes = node.selectNodes(xpath);
  110. var i, l = nodes.length;
  111. for (i=0; i<l; i++)
  112. {
  113. arr.push(nodes.item(i));
  114. }
  115. return arr;
  116. }
  117. else
  118. {
  119. var doc = (node.ownerDocument) ? node.ownerDocument : node;
  120. var results = doc.evaluate(xpath, node, doc.createNSResolver(doc.documentElement), XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
  121. if (results)
  122. {
  123. var arr = new Array();
  124. var node = results.iterateNext();
  125. while (node != null)
  126. {
  127. arr.push(node);
  128. node = results.iterateNext();
  129. }
  130. return arr;
  131. }
  132. return null;
  133. }
  134. },
  135. selectSingleNode: function (node, xpath)
  136. {
  137. if (document.all)
  138. {
  139. return node.selectSingleNode(xpath);
  140. }
  141. else
  142. {
  143. var doc = (node.ownerDocument) ? node.ownerDocument : node;
  144. var results = doc.evaluate(xpath, node, doc.createNSResolver(doc.documentElement), XPathResult.FIRST_ORDERED_NODE_TYPE, null);
  145. if (results && results.singleNodeValue)
  146. return results.singleNodeValue;
  147. return null;
  148. }
  149. },
  150. getAttribute: function(node, name)
  151. {
  152. return node.getAttribute(name);
  153. },
  154. text: function(node)
  155. {
  156. function deepScanText(node)
  157. {
  158. var v = '';
  159. var n = node.firstChild;
  160. while (n != null)
  161. {
  162. if (n.nodeType == 3)
  163. v += n.nodeValue;
  164. else if (n.nodeType == 4)
  165. v += n.data;
  166. else if (n.nodeType == 1)
  167. v += deepScanText(n);
  168. n = n.nextSibling;
  169. }
  170. return v;
  171. }
  172. if (node == null || node === undefined)
  173. return '';
  174. if (document.all)
  175. return node.text;
  176. else if (node.nodeValue)
  177. return node.nodeValue;
  178. return deepScanText(node);
  179. },
  180. parseXml: function(xmlString)
  181. {
  182. var xmlDoc = null;
  183. if (window.ActiveXObject){
  184. xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  185. xmlDoc.async="false";
  186. xmlDoc.loadXML(xmlString);
  187. }
  188. if (xmlDoc == null){
  189. var parser = new DOMParser();
  190. xmlDoc = parser.parseFromString(xmlString,"text/xml");
  191. }
  192. return xmlDoc;
  193. }
  194. }
  195. /*
  196. This class provides the APS that enables our content to work in enterprise portals.
  197. getWebContentResource: return an absolute url that will be used to retrieve a webcontent resource
  198. getGatewayResource: return an absolute url that will be used to retrieve a gateway resource
  199. getProxiedResource: return a url that can be used to do ajax request without running into cross domain issues
  200. getXHR: creates an xmlHttpRequest object.
  201. */
  202. com_ibm_cognos_cps.ApplicationContext = function(spec){
  203. this.getProperty = function(name){
  204. return spec[name];
  205. }
  206. }
  207. com_ibm_cognos_cps.ApplicationContext.prototype = {
  208. getWebContentResource: function(resource) {
  209. return this._getAbsoluteUrl(this.getProperty('webcontent'), resource);
  210. },
  211. getGatewayResource: function(resource) {
  212. return this._getAbsoluteUrl(this.getProperty('gateway'), resource);
  213. },
  214. _getAbsoluteUrl: function(base, url){
  215. if(url.indexOf(base) !== 0){
  216. var index = base.indexOf('://');
  217. if (index !== -1){
  218. index = base.indexOf('/', index + 3);
  219. if (index !== -1){
  220. return base.substring(0, index) + (url.charAt(0) === '/' ? '' : '/' ) + url;
  221. }
  222. }
  223. }
  224. return url;
  225. },
  226. getProxiedResource:function(url){
  227. var proxyTemplate = this.getProperty('proxyTemplate');
  228. // no proxying in CC
  229. if (proxyTemplate && !('cognos' == this.getProperty('portalAgent'))){
  230. return this._cpsresourceurl(this.getProperty('proxyTemplate'), url);
  231. }
  232. return url;
  233. },
  234. getXHR: function(){
  235. // plumtree xhr (knows how to handle timeouts ..)
  236. if (window.CustomPTHttpRequest !== undefined)
  237. return new CustomPTHttpRequest();
  238. if (CPSConfig.xmlHttpDefault != null)
  239. return new ActiveXObject(CPSConfig.xmlHttpDefault);
  240. // Well if there is no ActiveXObject available it must be firefox, opera, or something else
  241. if (typeof XMLHttpRequest != 'undefined')
  242. {
  243. return new XMLHttpRequest();
  244. }
  245. throw 'No XMLHttpRequest object is available';
  246. },
  247. _cpsresourceurl: function(resourceTemplate, target){
  248. var begin = resourceTemplate.indexOf('_cpsresourceurl');
  249. if (begin != -1){
  250. var endMarker='cpsendmarker_';
  251. var end = resourceTemplate.indexOf(endMarker, begin);
  252. if (end != -1){
  253. var marker = resourceTemplate.substring(begin, end + endMarker.length);
  254. var decodeCount = 0;
  255. var decodedMarker = marker;
  256. while(decodedMarker != '_cpsresourceurl:cpsendmarker_' && decodeCount ++ < 10){
  257. decodedMarker = decodeURIComponent(decodedMarker);
  258. }
  259. if (decodeCount < 10){
  260. var encodedTarget = target;
  261. for (i =0; i < decodeCount ; i++){
  262. encodedTarget = encodeURIComponent(encodedTarget);
  263. }
  264. target = resourceTemplate.replace(marker, encodedTarget);
  265. }
  266. }
  267. }
  268. // Plumtree portletIds.. so we can synchronize passports.
  269. if (window.cognosPortletIds){
  270. if(target.indexOf('?') != -1)
  271. target += '&';
  272. else
  273. target += '?';
  274. target += 'cognosPortletIds='+encodeURIComponent(cognosPortletIds.join('_'));
  275. }
  276. return target;
  277. }
  278. }
  279. })();