123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- // Licensed Materials - Property of IBM
- //
- // IBM Cognos Products: ps
- //
- // (C) Copyright IBM Corp. 2013
- //
- // US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
- dojo.provide('ps.util');
- (function() {
- if (typeof ps == "undefined") {
- this.ps = {};
- }
-
- ps._checkStartsWith = function(text, value) {
- return value == text.substr(0, value.length);
- };
- ps._checkContains = function(text, value) {
- return -1 != text.indexOf(value);
- };
- ps._isEmpty = function(text) {
- return (!text || dojo.trim(text).length === 0);
- };
-
- ps.setGateway = function(gateway) {
- ps._gateway = gateway;
- };
- ps.setWebcontent = function(webcontent) {
- ps._webcontent = webcontent;
- };
- ps.xhrGet = function(/*dojo.__XhrArgs*/args) {
- return ps._setupDeferred('GET', dojo.xhrGet(args));
- };
-
- /*
- logon handler
- */
- ps.getLogonHandler = function() {
- return function(error, ioArgs) {
- var dfd = new dojo.Deferred();
-
- //open logon iframe
- if (!ps._logonDialog) {
- ps._logonDialog = new ps.LogonDialog({
- okHandler: function() {
- ps._logonDialog = null;
- dfd.callback(true);
- },
- cancelHandler: function() {
- ps._logonDialog = null;
- dfd.errback(error);
- }
- });
- }
- ps._logonDialog.startup();
- ps._logonDialog.show();
-
- // after a successful logon, re-invoke original request and return its new deferred object
- dfd.addCallback(dojo.hitch(this, function(result) {
- var newdfd;
- if (dojo.isIE) {
- newdfd = dojo.xhr(this._method, this, ("postData" in this) || ("putData" in this));
- } else {
- newdfd = ps.xhrGet(this);
- }
- return newdfd;
- }));
- return dfd; // dojo.Deferred
- };
- };
-
- ps._errorTypeHandlers = [];
- ps.registerErrorTypeHandler = function(type, fn) {
- ps._errorTypeHandlers[type] = fn;
- };
-
- /**
- * @param {Object} method
- * @param {Object} dfd
- * @return {dojo.Deferred}
- */
- ps._setupDeferred = function(method, dfd) {
- // keep a copy of the method for later use
- dfd.ioArgs.args._method = method;
- // setup error handling with the error type handlers
- dfd.addErrback(dojo.hitch(dfd, function(error) {
- error = ps.parseError(error, this.ioArgs);
- if (typeof ps._errorTypeHandlers[error.code] === 'function') {
- ps._errorTypeHandlers[error.code].call(dfd.ioArgs.args, error, dfd.ioArgs);
- return null; // prevent original error from being displayed, it's already handled here.
- }
- // the error is forwarded to the next deferrer in the chain
- return error;
- }));
- return dfd; // dojo.Deferred
- };
-
- /**
- * Parse the error object. If error.responseText is present and looks like an error
- * object in JSON, XML or HTML formats, parse it. If ioArgs was provided, grab its status and responseText.
- * The Error object has these members: "message", "status", "responseText" & "stack".
- * The "code" and "details" members can be available too.
- * @param {Error} error
- * @param {Object} ioArgs (optional)
- * @return {Error}
- */
- ps.parseError = function(error, ioArgs) {
- if (!error.code) {
- if (!error.responseText && ioArgs && ioArgs.xhr) {
- if (!error.status) {
- error.status = ioArgs.xhr.status;
- }
- if (!error.responseText) {
- error.responseText = ioArgs.xhr.responseText;
- }
- error.code = 'http_' + error.status;
- error.message = dojo.string.substitute(PSMSG.ERR.IDS_EDG_HTTP_ERROR, [error.status]);
- }
- var text = dojo.string.trim(error.responseText || '');
- for (var i = 0, len = ps._errorTranslators.length; i < len; i++) {
- var trans = ps._errorTranslators[i];
- if (trans.check(text, trans.text)) {
- var err_fixed = trans.convert(text);
- if (err_fixed) {
- error = dojo.mixin(new Error(), error, err_fixed);
- }
- break;
- }
- }
- }
- return error;
- };
- /**
- * When an error is reported, these handlers have a chance to mangle the
- * error object to something else.
- */
- ps._errorTranslators = [
- {
- check: ps._checkStartsWith,
- text: '{"error":',
- convert: function(text) {
- // looks like json
- var obj = dojo.fromJson(text);
- return (obj.error) ? obj.error : null;
- }
- },
- {
- check: ps._checkContains,
- text: '<ERROR_CODE>SERVER_NOT_AVAILABLE</ERROR_CODE>',
- convert: function(text) {
- // this is an html markup response
- return {
- status: 500,
- code: 'serverNotAvailable',
- message: PSMSG.ERR.IDS_EDG_ERROR_SERVER_NOT_AVAILABLE
- };
- }
- },
- {
- check: ps._checkContains,
- text: '<ERROR_CODE>CAM_PASSPORT_ERROR</ERROR_CODE>',
- convert: function(text) {
- // this is an html markup response
- return {
- status: 403,
- code: 'camAuthUserRecoverable',
- message: dojo.string.substitute(PSMSG.ERR.IDS_EDG_HTTP_ERROR, [403])
- };
- }
- },
- {
- check: ps._checkContains,
- text: 'metricsManagerService',
- convert: function(text) {
- // this is an html markup response
- return {
- status: 500,
- code: 'CMMServiceNotAvailable',
- message: PSMSG.ERR.IDS_EDG_ERROR_CMM_SERVER_NOT_AVAILABLE
- };
- }
- },
- { //No error response returned
- check: ps._isEmpty,
- convert: function(text) {
- return {
- status:0,
- code: 'noResponseReturned',
- message: PSMSG.ERR.IDS_NAV_ERROR_SERVER_NOT_AVAILABLE
- };
- }
- }
- ];
-
- /**
- * Register an error translator. Any function can be use to scan the error text,
- * however two functions are available: ps._checkStartsWith and ps._checkContains.
- * The function signature of the error translator function is:
- * <code>
- * func(text) {
- * // use text to figure the proper error
- * var obj = {status: 500, code: "errorcode": message: "some message"};
- * return obj;
- * }
- * </code>
- * @param {String} checkFunc
- * @param {String} text
- * @param {Object} func
- */
- ps.registerErrorTranslator = function(checkFunc, text, convert) {
- ps._errorTranslators.push({
- check: checkFunc,
- text: text,
- convert: convert
- });
- };
- })();
|