OW2 Consortium telosys

Rev

Blame | Last modification | View Log | RSS feed

//------------------------------------------------------------------------------
// ScreenServiceXHR_class.js : XHR object wrapper used by the ScreenService class
// Telosys framework
// Author : Laurent GUERIN 
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// CLASS DEFINITION 
//------------------------------------------------------------------------------
function ScreenServiceXHR( sDefaultUrlParam )
{
        //------------------------------------------------------------------------------
        // PRIVATE ATTRIBUTES
        //------------------------------------------------------------------------------        
        var _bTrace              = false ;
        var _sDefaultUrl         = null ;
        var _oXMLHTTP            = null ; // THE "XMLHttpRequest" object
        var _sRequestContent     = null ; // The last request content
        var _oScreenResponse     = null ; // The last response object
        var _fctProcessResponse  = null ; // External function to process the response
        var _sLastUrlInvoked     = null ;
        
        if ( sDefaultUrlParam != null )
        {
                _sDefaultUrl = sDefaultUrlParam ;
        }
        
        //--- Instance init     
    if (window.ActiveXObject) 
        {
        //--- Internet Explorer
        _oXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest)
    {
        //--- Mozilla & Safari
        _oXMLHTTP = new XMLHttpRequest();
    }

        //==============================================================================
        // PRIVATE METHODS 
        //==============================================================================
        //------------------------------------------------------------------------------
        function _trace ( sMsg ) 
        {
                if ( _bTrace ) alert ( "TRACE [ScreenServiceXHR] :\n\n" + sMsg ) ;
        }
        //------------------------------------------------------------------------------
        function _error ( sMsg ) 
        {
                alert ( "ERROR [ScreenServiceXHR] :\n\n" + sMsg ) ;
        }
        //------------------------------------------------------------------------------

        //------------------------------------------------------------------------------
        // AJAX response processing : build the response and call the callBackFunction 
        //------------------------------------------------------------------------------
        function _processAjaxResponse() // Called for each state change (1,2,3 & 4)
        {
                //_trace("_processAjaxResponse() : readyState = " + _oXMLHTTP.readyState );
                //--- If new state is "COMPLETE" ...
                if ( _oXMLHTTP.readyState == 4 )
                {
                        _trace("_processAjaxResponse() : Response received : \n" + _oXMLHTTP.responseText );
                        
                var sTelosysRedirect = _oXMLHTTP.getResponseHeader("Telosys-Redirect");  // v 0.9.9
                // NB : if not found Firefox returns NULL and IE returns ""
                        if ( sTelosysRedirect != null && sTelosysRedirect != "" ) // v 0.9.9
                        {
                                //--- Redirection required (the content received is not an XML response)
                                _trace("_processAjaxResponse() : Telosys Redirection : '" + sTelosysRedirect + "' " );
                                fwkLoadPage( sTelosysRedirect );
                        }
                        else
                        {
                                //--- Build the ScreenResponse object
                                _trace("_processAjaxResponse() : Build the ScreenResponse object ..." );
                                _oScreenResponse = new ScreenResponse(_oXMLHTTP);
                                
                                //--- Keep the last request URL and the response received ( since v 1.1.0 )
                                if ( _oScreenResponse != null )
                                {                                       
                                        ScreenServiceLastCall.init( _sLastUrlInvoked, _oScreenResponse.getResponseText() );
                                }
                                else
                                {
                                        ScreenServiceLastCall.init( _sLastUrlInvoked, "ERROR : Response is null !" );
                                }
                                
                                //--- Run the callback if it exists
                                if ( _fctProcessResponse != null )
                                {
                                        _trace("_processAjaxResponse() : Run callback function ..." );
                                        _fctProcessResponse(_oScreenResponse) ;
                                }
                        }
                }
        }
        //------------------------------------------------------------------------------
        // Send HttpRequest to the server 
        //------------------------------------------------------------------------------
        function _sendRequest ( sHttpMethod, sURL, bAsync, sContent, processResponseFunction) // RET : void 
        {
                if ( sHttpMethod == null )
                {
                        _error ( "_sendRequest() : HTTP Method is null ! " ); 
                        return ;
                }
                if ( sURL == null ) // Use default URL
                {
                        sURL = _sDefaultUrl ;
                }
                if ( sURL == null )  // URL still null ? 
                {
                        _error ( "_sendRequest() : URL is null ! " ); 
                        return ;
                }
                if ( processResponseFunction == null )
                {
                        _error ("_sendRequest() : processResponse function is NULL !");
                        return ;
                }
                
                if ( _oXMLHTTP == null )
        {
                        _error ( "_sendRequest() : XMLHttpRequest object is null !" );
                        return ;
        }

                _sRequestContent = sContent ;
                _fctProcessResponse = processResponseFunction ;
                
            _trace( "_sendRequest("+sHttpMethod+","+sURL+","+bAsync+",..,..) : \nContent :\n" + _sRequestContent ); // Show the request 

                //--- (1) Setup the HTTP Request Method and URL                 
                _sLastUrlInvoked = sURL ;
            _oXMLHTTP.open (sHttpMethod, sURL, bAsync);  // false = SYNCHRONOUS  (true = ASYNCHRONOUS)      
                if ( sContent != null )
                {
                    _oXMLHTTP.setRequestHeader("Content-Type", "text/xml");
                }

                //--- (2) Configure the callback method (if ASYNCHRONOUS) 
            if ( bAsync )
            {
                        _oXMLHTTP.onreadystatechange = _processAjaxResponse ;
                }

                //--- (3) Send the request to the server 
            _oXMLHTTP.send (sContent); // Send the content (via HTTP)
            
            //--- (4) if this request is not ASYNCHRONOUS => call the method to process the response
            if ( ! bAsync )
            {
                        _processAjaxResponse() ;
                }
        }
        
        //==============================================================================
        // PUBLIC / PRIVILEGED  METHODS 
        //==============================================================================
        this.setTrace = function ( p ) 
        {
                _bTrace = p ;
        }

        //------------------------------------------------------------------------------
        // Get the last request content (request body)
        //------------------------------------------------------------------------------
        this.getRequest = function() 
        {
                return _sRequestContent ;
        }
        //------------------------------------------------------------------------------
        // Get the Screen Response object
        //------------------------------------------------------------------------------
        this.getResponse = function() 
        {
                return _oScreenResponse ;
        }

        //------------------------------------------------------------------------------
        // SYNCHRONOUS "GET"
        //------------------------------------------------------------------------------
        this.getSync = function ( sUrl, sContent, processResponseFunction )
        {
                _trace ("getSync(" + sUrl + ", .., ..)" );
                _sendRequest("GET", sUrl, false, sContent, processResponseFunction);
        }
        //------------------------------------------------------------------------------
        // ASYNCHRONOUS "GET"
        //------------------------------------------------------------------------------
        this.getAsync = function ( sUrl, sContent, processResponseFunction )
        {
                _trace ("getAsync(" + sUrl + ", .., ..)" );
                _sendRequest("GET", sUrl, true, sContent, processResponseFunction);
        }
        
        //------------------------------------------------------------------------------
        // SYNCHRONOUS "POST"
        //------------------------------------------------------------------------------
        this.postSync = function ( sUrl, sContent, processResponseFunction )
        {
                _trace ("postSync(" + sUrl + ", .., ..)" );
                _sendRequest("POST", sUrl, false, sContent, processResponseFunction);
        }
        //------------------------------------------------------------------------------
        // ASYNCHRONOUS "POST"
        //------------------------------------------------------------------------------
        this.postAsync = function ( sUrl, sContent, processResponseFunction )
        {
                _trace ("postAsync(" + sUrl + ", .., ..)" );
                _sendRequest("POST", sUrl, true, sContent, processResponseFunction);
        }
        //------------------------------------------------------------------------------
}
//------------------------------------------------------------------------------
//                                  F  I  N
//------------------------------------------------------------------------------

Generated by GNU enscript 1.6.4.