You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by do...@apache.org on 2008/03/26 16:41:45 UTC

svn commit: r641352 - in /incubator/shindig/trunk: config/syndicator.js features/core.io/io.js features/opensocial-0.7/batchrequest.js features/opensocial-0.7/feature.xml

Author: doll
Date: Wed Mar 26 08:41:42 2008
New Revision: 641352

URL: http://svn.apache.org/viewvc?rev=641352&view=rev
Log:
Fix for SHINDIG-148

The opensocial feature now makes non-proxied requests to the shindig server for data. Added some new gadgets.io utility methods to make this happen. 

Now opensocial data urls must be relative (or at least same domain). 


Modified:
    incubator/shindig/trunk/config/syndicator.js
    incubator/shindig/trunk/features/core.io/io.js
    incubator/shindig/trunk/features/opensocial-0.7/batchrequest.js
    incubator/shindig/trunk/features/opensocial-0.7/feature.xml

Modified: incubator/shindig/trunk/config/syndicator.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/config/syndicator.js?rev=641352&r1=641351&r2=641352&view=diff
==============================================================================
--- incubator/shindig/trunk/config/syndicator.js (original)
+++ incubator/shindig/trunk/config/syndicator.js Wed Mar 26 08:41:42 2008
@@ -89,7 +89,7 @@
   },
   "opensocial-0.7" : {
     // Path to fetch opensocial data from
-    // If relative, will be appended to the document.location.host
+    // Must be on the same domain as the gadget rendering server
     "path" : "/gadgets/socialdata",
     "domain" : "shindig",
     "enableCaja" : true,

Modified: incubator/shindig/trunk/features/core.io/io.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/core.io/io.js?rev=641352&r1=641351&r2=641352&view=diff
==============================================================================
--- incubator/shindig/trunk/features/core.io/io.js (original)
+++ incubator/shindig/trunk/features/core.io/io.js Wed Mar 26 08:41:42 2008
@@ -51,6 +51,41 @@
     }
   }
 
+  /**
+   * Checks the xobj for errors, may call the callback with an error response
+   * if the error is fatal.
+   *
+   * @param {Object} xobj The XHR object to check
+   * @param {Function} callback The callback to call if the error is fatal
+   * @return true if the xobj is not ready to be processed
+   */
+  function hadError(xobj, callback) {
+    if (xobj.readyState !== 4) {
+      return true;
+    }
+    if (xobj.status !== 200) {
+      // TODO Need to work on standardizing errors
+      callback({errors : ["Error " + xobj.status]});
+      return true;
+    }
+    return false;
+  }
+
+  /**
+   * Handles non-proxied XHR callback processing.
+   *
+   * @param {String} url
+   * @param {Function} callback
+   * @param {Object} params
+   * @param {Object} xobj
+   */
+  function processNonProxiedResponse(url, callback, params, xobj) {
+    if (hadError(xobj, callback)) {
+      return;
+    }
+    callback(transformResponseData(params, xobj.responseText));
+  }
+
   var UNPARSEABLE_CRUFT = "throw 1; < don't be evil' >";
 
   /**
@@ -62,12 +97,7 @@
    * @param {Object} xobj
    */
   function processResponse(url, callback, params, xobj) {
-    if (xobj.readyState !== 4) {
-      return;
-    }
-    if (xobj.status !== 200) {
-      // TODO Need to work on standardizing errors
-      callback({errors : ["Error " + xobj.status] });
+    if (hadError(xobj, callback)) {
       return;
     }
     var txt = xobj.responseText;
@@ -80,8 +110,13 @@
     // trusted source, and json parsing is slow in IE.
     var data = eval("(" + txt + ")");
     data = data[url];
+
+    callback(transformResponseData(params, data.body));
+  }
+
+  function transformResponseData(params, serverResponse) {
     var resp = {
-     text: data.body,
+     text: serverResponse,
      errors: []
     };
     switch (params.CONTENT_TYPE) {
@@ -119,7 +154,31 @@
         resp.data = resp.text;
         break;
     }
-    callback(resp);
+
+    return resp;
+  }
+
+  /**
+   * Sends an XHR post request
+   *
+   * @param realUrl The url to fetch data from that was requested by the gadget
+   * @param proxyUrl The url to proxy through
+   * @param callback The function to call once the data is fetched
+   * @param postData The data to post to the proxyUrl
+   * @param params The params to use when processing the response
+   * @param processResponseFunction The function that should process the
+   *     response from the sever before calling the callback
+   */
+  function makePostRequest(realUrl, proxyUrl, callback, postData, params,
+      processResponseFunction) {
+    var xhr = makeXhr();
+    xhr.open("POST", proxyUrl, true);
+    if (callback) {
+      xhr.onreadystatechange = gadgets.util.makeClosure(
+          null, processResponseFunction, realUrl, callback, params, xhr);
+    }
+    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
+    xhr.send(postData);
   }
 
   /**
@@ -160,16 +219,8 @@
     makeRequest : function (url, callback, opt_params) {
       // TODO: This method also needs to respect all members of
       // gadgets.io.RequestParameters, and validate them.
-      var xhr = makeXhr();
-      var params = opt_params || {};
 
-      xhr.open("POST", config.jsonProxyUrl, true);
-      if (callback) {
-        xhr.onreadystatechange = gadgets.util.makeClosure(
-            null, processResponse, url, callback, params, xhr);
-      }
-      // We always send a POST request; we just hide the details.
-      xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
+      var params = opt_params || {};
 
       // Check if authorization is requested
       var auth, st;
@@ -191,7 +242,18 @@
         authz : auth || "",
         st : st || ""
       };
-      xhr.send(gadgets.io.encodeValues(postData));
+
+      makePostRequest(url, config.jsonProxyUrl, callback,
+          gadgets.io.encodeValues(postData), params, processResponse);
+    },
+
+    /**
+     * @private
+     */
+    makeNonProxiedRequest : function (relativeUrl, callback, opt_params) {
+      var params = opt_params || {};
+      makePostRequest(relativeUrl, relativeUrl, callback, params.POST_DATA,
+          params, processNonProxiedResponse);
     },
 
     /**

Modified: incubator/shindig/trunk/features/opensocial-0.7/batchrequest.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/opensocial-0.7/batchrequest.js?rev=641352&r1=641351&r2=641352&view=diff
==============================================================================
--- incubator/shindig/trunk/features/opensocial-0.7/batchrequest.js (original)
+++ incubator/shindig/trunk/features/opensocial-0.7/batchrequest.js Wed Mar 26 08:41:42 2008
@@ -42,6 +42,6 @@
     "AUTHORIZATION" : "SIGNED",
     "POST_DATA" : gadgets.io.encodeValues(this.params_)};
 
-  gadgets.io.makeRequest(this.path_, this.callback_,
+  gadgets.io.makeNonProxiedRequest(this.path_, this.callback_,
       makeRequestParams);
 };

Modified: incubator/shindig/trunk/features/opensocial-0.7/feature.xml
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/opensocial-0.7/feature.xml?rev=641352&r1=641351&r2=641352&view=diff
==============================================================================
--- incubator/shindig/trunk/features/opensocial-0.7/feature.xml (original)
+++ incubator/shindig/trunk/features/opensocial-0.7/feature.xml Wed Mar 26 08:41:42 2008
@@ -39,14 +39,7 @@
           var configParams = config["opensocial-0.7"];
 
           ShindigContainer = function() {
-            var path = configParams.path;
-            if (path.indexOf("http") != 0) {
-              var parentUrl = document.location.protocol + "//"
-                  + document.location.host;
-              path = parentUrl + path;
-            }
-
-            JsonContainer.call(this, path,
+            JsonContainer.call(this, configParams.path,
                 configParams.domain, configParams.supportedFields);
           };
           ShindigContainer.inherits(JsonContainer);