You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pluto-scm@portals.apache.org by ms...@apache.org on 2014/12/12 06:41:13 UTC

[7/8] portals-pluto git commit: Worked on adding support for 'application/x-www-form-urlencoded' forms.

Worked on adding support for 'application/x-www-form-urlencoded' forms.


Project: http://git-wip-us.apache.org/repos/asf/portals-pluto/repo
Commit: http://git-wip-us.apache.org/repos/asf/portals-pluto/commit/3a1a3d3b
Tree: http://git-wip-us.apache.org/repos/asf/portals-pluto/tree/3a1a3d3b
Diff: http://git-wip-us.apache.org/repos/asf/portals-pluto/diff/3a1a3d3b

Branch: refs/heads/PortletHub
Commit: 3a1a3d3be7f89ac4116effc6318a776e47de1f36
Parents: 015eb70
Author: Scott Nicklous <ms...@apache.org>
Authored: Thu Dec 11 16:55:54 2014 +0100
Committer: Scott Nicklous <ms...@apache.org>
Committed: Thu Dec 11 16:55:54 2014 +0100

----------------------------------------------------------------------
 .../src/main/webapp/WEB-INF/jsp/view-csp.jsp    |  2 +-
 pluto-portal/src/main/webapp/portlet.js         | 12 ++++
 pluto-portal/src/main/webapp/portletHubImpl.js  | 72 ++++++++++++++++++--
 3 files changed, 80 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/3a1a3d3b/PortletHubDemo/src/main/webapp/WEB-INF/jsp/view-csp.jsp
----------------------------------------------------------------------
diff --git a/PortletHubDemo/src/main/webapp/WEB-INF/jsp/view-csp.jsp b/PortletHubDemo/src/main/webapp/WEB-INF/jsp/view-csp.jsp
index f3ffe52..11c0475 100644
--- a/PortletHubDemo/src/main/webapp/WEB-INF/jsp/view-csp.jsp
+++ b/PortletHubDemo/src/main/webapp/WEB-INF/jsp/view-csp.jsp
@@ -52,7 +52,7 @@ limitations under the License.
   </tr></table>
 </form>
 <hr/>
-<FORM id='<portlet:namespace/>-setParams' method='POST' enctype='multipart/form-data'>
+<FORM id='<portlet:namespace/>-setParams' method='POST' enctype='application/x-www-form-urlencoded'>
    <table><tr><td align='left'>
    Select active foreground colors:
    </td><td>

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/3a1a3d3b/pluto-portal/src/main/webapp/portlet.js
----------------------------------------------------------------------
diff --git a/pluto-portal/src/main/webapp/portlet.js b/pluto-portal/src/main/webapp/portlet.js
index 7f1ebe6..13b41d7 100644
--- a/pluto-portal/src/main/webapp/portlet.js
+++ b/pluto-portal/src/main/webapp/portlet.js
@@ -1635,6 +1635,18 @@ var portlet = portlet || {};
                             + " is of type " + type);
                   }
                }
+               
+               // if we're dealing with a form, verify method and enctype
+               
+               if (el) {
+            	   if (el.method.toUpperCase() !== 'POST' && el.method.toUpperCase() !== 'GET') {
+                       throwIllegalArgumentException("Invalid form method " + el.method + ". Allowed methods are GET & POST ");
+            	   }
+            	   // allow the default, which is 'application/x-www-form-urlencoded' encoded, and also multipart/form-data
+            	   if (el.enctype && el.enctype !== 'application\/x-www-form-urlencoded' && el.enctype !== 'multipart\/form-data') {
+                       throwIllegalArgumentException("Invalid form enctype " + el.enctype + ". Allowed: 'application\/x-www-form-urlencoded' & 'multipart\/form-data'  ");
+            	   }
+              }
          
                // everything ok, so do the action
          

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/3a1a3d3b/pluto-portal/src/main/webapp/portletHubImpl.js
----------------------------------------------------------------------
diff --git a/pluto-portal/src/main/webapp/portletHubImpl.js b/pluto-portal/src/main/webapp/portletHubImpl.js
index a3df75c..ef3fbb3 100644
--- a/pluto-portal/src/main/webapp/portletHubImpl.js
+++ b/pluto-portal/src/main/webapp/portletHubImpl.js
@@ -701,6 +701,46 @@ var portlet = portlet || {};
       });
 
    },
+   
+   /**
+    * function to extract data from form and encode it as an 'application/x-www-form-urlencoded' string
+    * 
+    * @param   {HTMLFormElement}  form    Form to be submitted
+    * @private 
+    */
+   encodeFormAsString = function (form) {
+      var fstr = "", parm, parms = [], ii, jj, el, name, val, tag, type;
+      for (ii = 0; ii < form.elements.length; ii++) {
+         el = form.elements[ii];
+         name = el.name;
+         val = el.value;
+         tag = el.nodeName.toUpperCase();
+         if (tag === 'INPUT') {
+            type = el.type.toUpperCase();
+         } else {
+            type = "";
+         }
+         if (name && !el.disabled) {
+            if ((tag === 'SELECT') && (el.multiple === true)) {
+               // multiple select boxes need to be handled differently
+               for (jj = 0; jj < el.options.length; jj++) {
+                  if (el.options[jj].checked) {
+                     val = el.options[jj].value;
+                     parm = encodeURIComponent(name) + '=' + encodeURIComponent(val);
+                     parms.push(parm);
+                  }
+               }
+            } else {
+               if (((type !== 'CHECKBOX') && (type !== 'RADIO')) || (el.checked === true)) {
+                  parm = encodeURIComponent(name) + '=' + encodeURIComponent(val);
+                  parms.push(parm);
+               }
+            }
+         }
+      }
+      fstr = parms.join('&');
+      return fstr;
+   },
 
       
    /**
@@ -724,7 +764,7 @@ var portlet = portlet || {};
          // get the ajax action URL. The Pluto impl creates the URL in JS
          // therefore no error handling 
          getUrl("ACTION", pid, parms).then(function (url) {
-            var xhr, upids, fd;
+            var xhr, upids, fd, method = 'POST', enctype, fstr;
 
             console.log("ajax action URL: " + url);
             
@@ -743,12 +783,34 @@ var portlet = portlet || {};
                   }
                }
             };
-            xhr.open("POST", url, true);
             if (element) {
-               fd = new FormData(element);
-               console.log("ajax action: POST using FormData object: " + fd);
-               xhr.send(fd);
+               method = element.method;      // may be GET or POST
+               enctype = element.enctype;
+               if (enctype === 'multipart\/form-data') {
+                  // multipart/form-data is sent using FormData 
+                  fd = new FormData(element);
+                  console.log("ajax action: POST using FormData object: " + fd);
+                  xhr.open(method, url, true);
+                  xhr.send(fd);
+               } else {
+                  // has to be 'application\/x-www-form-urlencoded', as the hub does not support text/plain
+                  fstr = encodeFormAsString(element);
+                  if (method.toUpperCase() === 'GET') {
+                     if (url.indexOf('?') >= 0) {
+                        url += '&' + fstr;
+                     } else {
+                        url += '?' + fstr;
+                     }
+                     xhr.open(method, url, true);
+                     xhr.send();
+                  } else {
+                     // has to be post, since we only support GET & POST
+                     xhr.open(method, url, true);
+                     xhr.send(fstr);
+                  }
+               }
             } else {
+               xhr.open(method, url, true);
                console.log("ajax action: POST using URL with parameters");
                xhr.send();
             }