/* IBM Confidential OCO Source Materials IBM Cognos Products: rs (C) Copyright IBM Corp. 2018, 2019 The source code for this program is not published or otherwise divested of its trade secrets, irrespective of what has been deposited with the U.S. Copyright Office. */ define(['bi/authoring/utils/U_Object', 'bi/authoring/utils/rsPromptHandler'], function(U_Object, rsPromptHandler){ /* C_RestRequest.prototype.F_GetStatusText = function() { // Firefox and Ie can throw an exception when accessing this property return this.m_oXHR ? U_JsUtils.F_GetPropertyInTryCatch( this.m_oXHR, "statusText" ) : null; }; C_RestRequest.prototype.F_GetUrl = function() { return this.m_sUrl; }; C_RestRequest.prototype.f_processTracking = function() { G_Debug.F_Print("C_RestRequest.f_processTracking"); var v_sTracking = this.m_oXHR.getResponseHeader('busTracking'); if (v_sTracking) { this.F_PushRequestTrackingForReuse(v_sTracking); } }; C_RestRequest.prototype.F_PushRequestTrackingForReuse = function(v_sTracking) { }; */ function C_rsRestRequest( v_oGlassContext, v_oListener, v_oRequest ) { this.m_oGlassContext = v_oGlassContext; this.m_oListener = v_oListener; this.m_oRequest = v_oRequest; this.m_oPromptCallbackResponse = null; this.M_iResponseDelay = 0; }; function f_fireEvent( v_sEvent ) { if (this.m_oListener && this.m_oListener[v_sEvent]) { // fire events in a timeout because the glass ajax service will catch and discard any errors that occur, doing in a timeout avoids their catch setTimeout( this.m_oListener[v_sEvent].bind( this.m_oListener, this ), 0 ); } }; function f_isSuccessStatus() { var v_iStatus = this.F_GetStatus(); return v_iStatus >= 200 && v_iStatus < 300; }; function f_onRestResponseDontCare() { // Intentionally empty since there is nothing to be done. }; var f_processAsyncResponse; function f_doResponseProcessing() { if (this.F_GetStatus() == 202) { f_processAsyncResponse.bind(this)(); } else { if (this.m_bAborted) { return; } //this.f_processTracking(); NOT SUPPORTED this.F_ProcessResponse(); f_fireEvent.bind(this)("F_Request_OnComplete"); //TODO use promise? } }; function f_initiateResponseProcessing() { if (this.M_iResponseDelay) { setTimeout(f_doResponseProcessing.bind(this), this.M_iResponseDelay); return; } f_doResponseProcessing.bind(this)(); }; function f_onRestResponseOK(v_oResponseData, v_sStatus, v_oXHR ) { this.m_oResponseData = v_oResponseData; this.m_oXHR = v_oXHR; f_initiateResponseProcessing.bind(this)(); }; function f_onRestResponseFailed(v_sStatus, v_oXHR, v_sErrorThrown) { this.m_oXHR = v_oXHR; this.m_oFailed = { status: v_sStatus, error: v_sErrorThrown, response: this.F_GetResponseText() }; this.F_OnFailed(); f_initiateResponseProcessing.bind(this)(); }; function f_processAsyncResponse() { // Determine if prompting var v_bPrompting = this.m_oResponseData && this.m_oResponseData.status == 'prompting'; var v_sLocation = this.m_oXHR.getResponseHeader('Location'); var v_sAffinity = this.m_oXHR.getResponseHeader('X-CA-Affinity'); var v_oRequestHeaders = {}; if (v_sAffinity) { // This ensures request is routed to the right report_service instance. v_oRequestHeaders['X-CA-Affinity'] = v_sAffinity; } var v_oRequest = { url : v_sLocation, headers : v_oRequestHeaders }; if (this.m_bAborted) { v_oRequest.type = 'DELETE'; Application.GlassContext.services.ajax.ajax(v_oRequest) .done(f_onRestResponseDontCare.bind(this)) .fail(f_onRestResponseDontCare.bind(this)); } else { v_oRequest.type = 'GET'; if (v_bPrompting) { // By default, jquery will convert the response based on the content-type. In this case that means // parsing the SOAP response into a document (content-type: text/xml). However, the resulting document // is not compatible with the documents used by RS. Therefore, we tell jquery to leave // the XML response as plain text and we will handle the parsing ourselves. v_oRequest.dataType = "text"; if (!rsPromptHandler.F_DoRestPrompting(this, v_oRequest, this.m_oGlassContext)) { this.m_oFailed = { status: 'popup blocked' }; //F_SetNewErrorRes( "IDS_CCHL_REQUEST_CANCELLED_PROMPTING" ); this.F_ProcessResponse(); f_fireEvent.bind(this)("F_Request_OnComplete"); } } else { this.m_oGlassContext.services.ajax.ajax(v_oRequest) .done(f_onRestResponseOK.bind(this)) .fail(f_onRestResponseFailed.bind(this)); } } }; function f_getPropertyInTryCatch( o, p ) { try { return o[p]; } catch ( e ) { return null; } }; ///////////////////////////// C_rsRestRequest.prototype.F_GetListener = function() { //TODO use promise? return this.m_oListener; }; C_rsRestRequest.prototype.F_SetListener = function(v_oListener) { //TODO use promise? this.m_oListener = v_oListener; }; C_rsRestRequest.prototype.F_SetHeaders = function( v_oRequestHeaders ) { // Override as required return v_oRequestHeaders; }; C_rsRestRequest.prototype.F_Send = function() { this.m_oRequest.headers = this.F_SetHeaders( this.m_oRequest.headers ); this.m_oGlassContext.services.ajax.ajax(this.m_oRequest) .done(f_onRestResponseOK.bind(this)) .fail(f_onRestResponseFailed.bind(this)); }; C_rsRestRequest.prototype.F_Abort = function() { // glass ajax requests are not cancellable, so the best we can do is to stop listening this.m_bAborted = true; f_fireEvent.bind(this)("F_Request_OnAborted"); //TODO use promise? this.F_SetListener(null); }; C_rsRestRequest.prototype.F_GetStatus = function() { // Firefox and IE can throw an exception when accessing this property return this.m_oXHR ? f_getPropertyInTryCatch( this.m_oXHR, "status" ) : null; }; C_rsRestRequest.prototype.F_ProcessResponse = function() { // Intentionally empty since there is nothing to be done. // Classes extending this class are expected to override this method. }; C_rsRestRequest.prototype.F_GetResponseData = function() { return this.m_oResponseData; }; C_rsRestRequest.prototype.F_GetResponseText = function() { return this.m_oXHR ? this.m_oXHR.responseText : null; }; C_rsRestRequest.prototype.F_OnFailed = function() { // Override as required }; C_rsRestRequest.prototype.F_GetFailed = function() { return this.m_oFailed; }; C_rsRestRequest.prototype.F_SetPromptCallbackResponse = function( v_oResponse, v_oAttachments ) { // v_oResponse is an object created by a child window but this code is running // in the context of the parent window. We need to clone v_oResponse because // when the child goes out of scope, v_oResponse may disappear (in IE 11 it does). this.m_oPromptCallbackResponse = v_oResponse ? U_Object.F_Clone(v_oResponse) : null; }; C_rsRestRequest.prototype.F_OnServerPromptingComplete = function() { if (this.m_oPromptCallbackResponse) { this.m_oResponseData = this.m_oPromptCallbackResponse; this.m_oPromptCallbackResponse = null; this.m_oFailed = null; //this.F_SetError(null); this.F_ProcessResponse(); } else { this.m_oFailed = { status: 'cancelled' }; //F_SetNewErrorRes( "IDS_CCHL_REQUEST_CANCELLED_PROMPTING" ); } f_fireEvent.bind(this)("F_Request_OnComplete"); }; return C_rsRestRequest; });