[Laszlo-checkins] r10845 - in openlaszlo/trunk: lps/components/rpc test/ajax

hqm@openlaszlo.org hqm at openlaszlo.org
Tue Sep 2 07:28:22 PDT 2008


Author: hqm
Date: 2008-09-02 07:28:20 -0700 (Tue, 02 Sep 2008)
New Revision: 10845

Modified:
   openlaszlo/trunk/lps/components/rpc/ajax.lzx
   openlaszlo/trunk/test/ajax/request.lzx
Log:
Change 20080829-hqm-D by hqm at badtzmaru.home on 2008-08-29 16:48:17 EDT
    in /Users/hqm/openlaszlo/trunk
    for http://svn.openlaszlo.org/openlaszlo/trunk

Summary: make ajax compatibility library use dataprovider API

New Features:

Bugs Fixed: LPP-6877

Technical Reviewer: a.bargull
QA Reviewer: pbr
Doc Reviewer: (pending)

Documentation:

Release Notes:

Details:

Use the dataprovider API to make requests. 

A new "parsexml" attribute determines whether the loader tries to parse the response
into an LzDataElement XML object.
    

Tests:

test/ajax/request.lzx



Modified: openlaszlo/trunk/lps/components/rpc/ajax.lzx
===================================================================
--- openlaszlo/trunk/lps/components/rpc/ajax.lzx	2008-09-01 21:27:17 UTC (rev 10844)
+++ openlaszlo/trunk/lps/components/rpc/ajax.lzx	2008-09-02 14:28:20 UTC (rev 10845)
@@ -11,6 +11,18 @@
              onreadystatechange -->
         <attribute name="onreadystatechange" value="null" />
 
+        <!-- Flag to say whether to try to parse the response content as XML. -->
+        <attribute name="parsexml" value="true" />
+
+        <!-- Flag to say whether to try to parse the response content as XML. -->
+        <attribute name="proxied" value="$once{canvas.proxied}" />
+
+        <!-- Timeout in msecs -->
+        <attribute name="timeout" value="60000" />
+
+        <!-- @keywords private -->
+        <attribute name="headers" value="null" />
+
         <!--- String version of data returned from server process -->
         <attribute name="responseText" value="" type="string"/>
 
@@ -21,10 +33,13 @@
         <attribute name="status" value="0" type="number" />
 
         <!--- String message accompanying the status code -->
-        <attribute name="statusText" type="string" />
+        <attribute name="statusText" type="string"  value=""/>
 
         <!--- @keywords private -->
         <attribute name="method" type="string" />
+
+        <!--- @keywords private -->
+        <attribute name="dsLoadDel" value="null" />
         
         <!--- @keywords private -->
         <attribute name="url" type="string" />
@@ -39,37 +54,14 @@
         <attribute name="onerrorDelegate" value="null" />
 
         <!--- @keywords private -->
-        <attribute name="dataset" value="null" />
-
-        <!--- @keywords private -->
-        <method name="init">
-            super.init();
-            
-            if (this.dataset == null) {
-                 this.dataset = new lz.dataset(this);
-            }
-
-            this.dataset.getresponseheaders = true;
-
-            // register a delegate for when the dataset has loaded
-            this.ondataDelegate = new LzDelegate( this , "ondataHandler" , 
-                                                  this.dataset, "ondata" );
-            this.onerrorDelegate = new LzDelegate( this , "onerrorHandler" , 
-                                                  this.dataset, "onerror" );
-        </method>
+        <attribute name="dataRequest" value="null" />
         
         <!--- @keywords private -->
         <method name="destroy" >
-            if (this.ondataDelegate) {
-                this.ondataDelegate.unregisterAll();
-                this.ondataDelegate = null;
+            if (this.onstatusDelegate) {
+                this.onstatusDelegate.unregisterAll();
+                this.onstatusDelegate = null;
             }
-            
-            if (this.onerrorDelegate) {
-                this.onerrorDelegate.unregisterAll();
-                this.onerrorDelegate = null;
-            }
-            
             super.destroy();
         </method>
 
@@ -80,31 +72,30 @@
 
         <!--- Stops the current request -->
         <method name="abort">
-            this.dataset.abort();
+          canvas.httpdataprovider.abortLoadForRequest( this.dataRequest );
         </method>
 
         <!--- Returns complete set of headers (labels and values) as an LzParam -->
         <method name="getAllResponseHeaders">
-            return this.dataset.getAllResponseHeaders();
+            return this.dataRequest.responseheaders;
         </method>
 
         <!--- Returns the string value of a single header label -->
         <method name="getAllResponseHeader" args="hname:String">
             // TODO: [20080825 anba] Possibly deprecate and remove, see TODO below in 'getResponseHeader()'
-            return this.dataset.getResponseHeader(hname);
+            Debug.deprecated(this, 'getResponseHeaders', getResponseHeader);
+            return this.getResponseHeader(hname);
         </method>
-        
+
         <!--- Returns the string value of a single header label -->
         <method name="getResponseHeader" args="hname:String">
-            // TODO: [20080825 anba] added 'getResponseHeader()' because
-            // this is the actual name for the browser XMLHttpRequest. 
-            // I don't know why the original developer used 'getAllResponseHeader()'. 
-            return this.dataset.getResponseHeader(hname);
+            return this.dataRequest.responseheaders[hname];
         </method>
 
         <!--- Assigns destination URL, method, and other optional attributes of a pending request -->
         <method name="open" args="method:String!, url:String!, async:Boolean = true, uname:String = null, password:String = null">
             <![CDATA[
+            this.dataRequest = new LzHTTPDataRequest(this);
             this.responseText = "";
             this.responseXML = null;
             this.url = url;
@@ -112,22 +103,18 @@
             this.async = async;
             if ($debug) {
                 if (!async) {
-                    Debug.write("warning: XMLHttpRequest.open('"+method+"','"+url+"','"+async+"') does not support synchronous mode");
+                    Debug.warn("warning: XMLHttpRequest.open('"+method+"','"+url+"','"+async+"') does not support synchronous mode");
                 }
             }
             this.uname = uname;
             this.password = password;
             if ($debug) {
                 if (uname != null || password != null) {
-                    Debug.write("warning: XMLHttpRequest.open() does not support HTTP authentication");
+                    Debug.warn("warning: XMLHttpRequest.open() does not support HTTP authentication");
                 }
             }
-            if (method.toLowerCase() == "get") {
-                this.dataset.setQueryType("GET");
-            } else if (method.toLowerCase() == "post") {
-                this.dataset.setQueryType("POST");
-            } else if ($debug) {
-                Debug.write("XMLHttpRequest.open: method '"+method+"' not supported, use GET or POST");
+            if ($debug && (method.toLowerCase() != "get") && (method.toLowerCase() != "post") && !$dhtml) {
+                Debug.warn("lz.XMLHttpRequest.open: method '"+method+"' not supported, use GET or POST");
             }
             this.setAttribute('readyState', 1); // open
             if (this.onreadystatechange != null) {
@@ -136,10 +123,31 @@
             ]]>
         </method>
 
-        <method name="ondataHandler" args="d">
+        <!--
+           @access private       
+           Called when  data request status changes.
+           If status is 'success', call setData
+        -->
+        <method name="handleResponse" args="dreq">
+           <![CDATA[
+             if (this.dsLoadDel != null) {
+                 this.dsLoadDel.unregisterFrom(dreq.onstatus);
+             }
+             if (dreq.status == LzDataRequest.SUCCESS) {
+                 this.ondataHandler()
+             } else if (dreq.status == LzDataRequest.ERROR) {
+                 this.onerrorHandler();
+             } else if (dreq.status == LzDataRequest.TIMEOUT) {
+                 this.onerrorHandler();
+             }
+           ]]>
+        </method>
+
+
+        <method name="ondataHandler">
             // Currently raw text response is only available for serverless requests
-            this.responseText = this.dataset.dataRequest['rawdata'];
-            this.responseXML = this.dataset;
+            this.responseText = this.dataRequest['rawdata'];
+            this.responseXML = this.dataRequest.xmldata;
             this.status = 200; // maybe we need something a little more Laszlo-specific?
             this.statusText = "OK";
             this.setAttribute('readyState', 4); // 'receiving' (well, received...)
@@ -148,16 +156,16 @@
             }
         </method>
 
-        <method name="onerrorHandler" args="d">
+        <method name="onerrorHandler">
             <![CDATA[
             // Currently raw text response is only available for serverless requests
             this.responseText = "";
             this.responseXML = null;
             // If the load was proxied, we can actually dig in the headers and get the
             // real response.
-            if (this.dataset.isProxied() && this.dataset.errorstring != null) {
-                //data source error for http://localhost:8080/lps-dev/test/ajax/echo.jsps: HTTP Status code: 404:Not Found
-                var err = this.dataset.errorstring;
+            //data source error for http://localhost:8080/lps-dev/test/ajax/echo.jsps: HTTP Status code: 404:Not Found
+            var err = this.dataRequest.error;
+            if (err) {
                 var marker = 'HTTP Status code: ';
                 var ind = err.indexOf(marker);
                 if (ind >= 0) {
@@ -175,7 +183,7 @@
             if (this.onreadystatechange != null) {
                 this.onreadystatechange(this);
             }
-            ]]>
+           ]]>
         </method>
 
         <!--- Transmits the request, optionally with postable string or DOM object data
@@ -192,29 +200,49 @@
                 if (this.onreadystatechange != null) {
                     this.onreadystatechange(this);
                 }
-                this.dataset.setSrc(this.url);
 
-                // Try to handle content arg, as best we can
-                // given the extremely lame Flash 6/7 HTTP APIs.
-                if (content != null && this.dataset.isProxied()) {
-                    // Raw POST mechanism is only supported in proxied mode
-                    if (typeof(content) == 'string') {
-                        this.dataset.setQueryParam('lzpostbody', content);
-                    } else {
-                        // if it's not a string, and not null, assume it's XML
-                        this.dataset.setQueryParam('lzpostbody', content.serialize());
-                    }
+                var dreq = this.dataRequest;
+                dreq.parsexml = this.parsexml;
+                dreq.method = this.method.toUpperCase();
+                dreq.requestheaders = this.headers;
+                dreq.timeout = this.timeout;
+                // Add in LPS Server proxy javarpc-protocol-specific query args
+
+                if (this.proxied) {
+                    dreq.proxied = true;
+                    dreq.proxyurl = canvas.getProxyURL();
+                } 
+                
+                dreq.src = this.url;
+                dreq.postbody = content;
+                dreq.status  = LzDataRequest.READY;
+                dreq.queuerequests      = true;
+                dreq.getresponseheaders = true;
+                dreq.cacheable       =  false;
+                dreq.clientcacheable =  false;
+                dreq.trimwhitespace  =  false;
+                dreq.nsprefix        =  true;
+                
+                // NB: You must set the onstatus event handler *before* issuing request
+                if (this.dsLoadDel == null) {
+                    this.dsLoadDel = new LzDelegate( this , "handleResponse" , dreq, "onstatus");
+                } else {
+                    this.dsLoadDel.register(dreq, "onstatus");
                 }
-                this.dataset.doRequest();
-            } else if ($debug) {
-                Debug.write("XMLHttpRequest.send: method '"+method+"' not supported, use GET or POST");
+                canvas.httpdataprovider.doRequest( dreq );
+            } else if ($debug && !$dhtml) {
+                // Only DHTML runtime currently support DELET or PUT methods
+                Debug.warn("lz.XMLHttpRequest.send: method '"+method+"' not supported, use GET or POST");
             }
             ]]>
         </method>
 
         <!--- Assigns a label/value pair to the header to be sent with a request -->
         <method name="setRequestHeader" args="key:String, val:String">
-            this.dataset.setHeader(key, val);
+            if (this.headers == null) {
+                this.headers = new LzParam();
+            }
+            this.headers.setValue(key, val);
         </method>
 
         <doc>
@@ -237,11 +265,6 @@
                     
                 </ul>
 
-                This implementation is simply a convenience wrapper
-                for the LzDataset API, which is the preferred way to
-                write runtime-portable code which loads XML data over
-                the network.
-
                 <programlisting>
     &lt;script&gt;
         function loadXMLDoc(url) {

Modified: openlaszlo/trunk/test/ajax/request.lzx
===================================================================
--- openlaszlo/trunk/test/ajax/request.lzx	2008-09-01 21:27:17 UTC (rev 10844)
+++ openlaszlo/trunk/test/ajax/request.lzx	2008-09-02 14:28:20 UTC (rev 10845)
@@ -13,23 +13,26 @@
         // only if request shows "loaded"
         if (request.readyState == 4) {
             // only if "OK"
+            Debug.write("request.responseText:", request.responseText);
             if (request.status == 200) {
                 Debug.write("arg =", request);
                 Debug.write("request.status", request.status);
-                Debug.write("request.responseText:", request.responseText);
                 Debug.write("request.responseXML:", request.responseXML);
                 Debug.write("request.getAllResponseHeaders:", request.getAllResponseHeaders());
             } else {
+
                 Debug.write("There was a problem retrieving the XML data:\n" +
                             request.statusText);
             }
         }
     }
 
+var req;
     
     function loadXMLDoc(url) {
         // branch for native XMLHttpRequest object
-        req = new XMLHttpRequest();
+        req = new lz.XMLHttpRequest();
+        req.parsexml = parsecb.getValue();
         req.onreadystatechange = processReqChange;
         req.open("GET", url, true);
         req.setRequestHeader('X-Test', 'one');
@@ -46,7 +49,12 @@
     <button onclick="loadXMLDoc(in1.getText())">Load Data</button>
 
     <button onclick="loadXMLDoc('badurl')">Test Error Handling, this should fail</button>
+    <checkbox id="parsecb" value="true">parse xml?</checkbox>
 
 
 
 </canvas>
+<!-- * X_LZ_COPYRIGHT_BEGIN ***************************************************
+* Copyright 2001-2008 Laszlo Systems, Inc.  All Rights Reserved.              *
+* Use is subject to license terms.                                            *
+* X_LZ_COPYRIGHT_END ****************************************************** -->



More information about the Laszlo-checkins mailing list