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 2019/06/08 05:20:54 UTC

[portals-pluto] branch master updated (d7663ae -> c57c560b)

This is an automated email from the ASF dual-hosted git repository.

msnicklous pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/portals-pluto.git.


    from d7663ae  minor functional and cosmetic updates to the hub demo portlet and the v3 demo portlet
     new f4ef178  Fixed problem in PageState where certain types of data were not being handled correctly. removed extraneous white space from generated json object to save space.
     new c57c560b functional and cosmetic updates to the annotated demo portlet

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 demo/v3-annotated-demo-portlet/pom.xml             |   4 +
 .../pluto/demo/v3annotated/HelloWorldRender.java   |   4 +-
 .../portals/pluto/demo/v3annotated/NameBean.java   |   3 +-
 .../org/apache/pluto/driver/util/PageState.java    | 285 +++++++++++----------
 .../webapp/WEB-INF/themes/pluto-default-theme.jsp  |   5 +-
 5 files changed, 164 insertions(+), 137 deletions(-)


[portals-pluto] 02/02: functional and cosmetic updates to the annotated demo portlet

Posted by ms...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

msnicklous pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/portals-pluto.git

commit c57c560b260242aa03fd19bb096911ebbe9c484f
Author: Scott Nicklous <ms...@apache.org>
AuthorDate: Sat Jun 8 07:20:39 2019 +0200

    functional and cosmetic updates to the annotated demo portlet
---
 demo/v3-annotated-demo-portlet/pom.xml                                | 4 ++++
 .../org/apache/portals/pluto/demo/v3annotated/HelloWorldRender.java   | 4 +++-
 .../main/java/org/apache/portals/pluto/demo/v3annotated/NameBean.java | 3 +--
 3 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/demo/v3-annotated-demo-portlet/pom.xml b/demo/v3-annotated-demo-portlet/pom.xml
index e1c6c1e..91a66e8 100644
--- a/demo/v3-annotated-demo-portlet/pom.xml
+++ b/demo/v3-annotated-demo-portlet/pom.xml
@@ -44,6 +44,10 @@
 			<artifactId>tomcat-servlet-api</artifactId>
 			<scope>provided</scope>
 		</dependency>
+      <dependency>
+         <groupId>org.apache.commons</groupId>
+         <artifactId>commons-lang3</artifactId>
+      </dependency>
 		<dependency>
 			<groupId>org.slf4j</groupId>
 			<artifactId>slf4j-api</artifactId>
diff --git a/demo/v3-annotated-demo-portlet/src/main/java/org/apache/portals/pluto/demo/v3annotated/HelloWorldRender.java b/demo/v3-annotated-demo-portlet/src/main/java/org/apache/portals/pluto/demo/v3annotated/HelloWorldRender.java
index 4e70e3e..2a8cb97 100644
--- a/demo/v3-annotated-demo-portlet/src/main/java/org/apache/portals/pluto/demo/v3annotated/HelloWorldRender.java
+++ b/demo/v3-annotated-demo-portlet/src/main/java/org/apache/portals/pluto/demo/v3annotated/HelloWorldRender.java
@@ -24,6 +24,8 @@ import javax.portlet.RenderRequest;
 import javax.portlet.RenderResponse;
 import javax.portlet.annotations.RenderMethod;
 
+import org.apache.commons.lang3.StringEscapeUtils;
+
 /**
  * Simple hello world bean portlet
  */
@@ -72,7 +74,7 @@ public class HelloWorldRender {
       txt.append("<h3>Hello \n");
       // Get the name from the bean. If it hasn't been set, just greet the world.
       if (nameBean.getName() != null) {
-         txt.append(nameBean.getName());
+         txt.append(StringEscapeUtils.escapeHtml4(nameBean.getName()));
       } else {
          txt.append("World\n");
       }
diff --git a/demo/v3-annotated-demo-portlet/src/main/java/org/apache/portals/pluto/demo/v3annotated/NameBean.java b/demo/v3-annotated-demo-portlet/src/main/java/org/apache/portals/pluto/demo/v3annotated/NameBean.java
index 36fb4a7..73177d6 100644
--- a/demo/v3-annotated-demo-portlet/src/main/java/org/apache/portals/pluto/demo/v3annotated/NameBean.java
+++ b/demo/v3-annotated-demo-portlet/src/main/java/org/apache/portals/pluto/demo/v3annotated/NameBean.java
@@ -87,8 +87,7 @@ public class NameBean implements PortletSerializable {
       txt.append("   Enter your name:");
       txt.append("   </td><td>");
       txt.append("   <input name='").append(PARAM_NAME);
-      txt.append("' type='text' value='").append((name == null) ? "" : name);
-      txt.append("' size='50' maxlength='50'>");
+      txt.append("' type='text' value='' size='50' maxlength='50'>");
       txt.append("   <input type='hidden' name='").append(ActionRequest.ACTION_NAME);
       txt.append("' value='setName'>");
       txt.append("   </td></tr><tr><td>");


[portals-pluto] 01/02: Fixed problem in PageState where certain types of data were not being handled correctly. removed extraneous white space from generated json object to save space.

Posted by ms...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

msnicklous pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/portals-pluto.git

commit f4ef1781f96f814046d9b87846852da672804bef
Author: Scott Nicklous <ms...@apache.org>
AuthorDate: Sat Jun 8 06:39:37 2019 +0200

    Fixed problem in PageState where certain types of data were not being
    handled correctly. removed extraneous white space from generated json
    object to save space.
---
 .../org/apache/pluto/driver/util/PageState.java    | 285 +++++++++++----------
 .../webapp/WEB-INF/themes/pluto-default-theme.jsp  |   5 +-
 2 files changed, 156 insertions(+), 134 deletions(-)

diff --git a/pluto-portal-driver/src/main/java/org/apache/pluto/driver/util/PageState.java b/pluto-portal-driver/src/main/java/org/apache/pluto/driver/util/PageState.java
index d29023d..2dc077a 100644
--- a/pluto-portal-driver/src/main/java/org/apache/pluto/driver/util/PageState.java
+++ b/pluto-portal-driver/src/main/java/org/apache/pluto/driver/util/PageState.java
@@ -16,7 +16,6 @@
  *  under the License.
  */
 
-
 package org.apache.pluto.driver.util;
 
 import java.util.Collection;
@@ -41,40 +40,41 @@ import org.apache.pluto.driver.services.portal.PublicRenderParameterMapper;
 import org.apache.pluto.driver.url.PortalURL;
 import org.apache.pluto.driver.url.PortalURLParameter;
 import org.apache.pluto.driver.url.PortalURLPublicParameter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.security.web.csrf.CsrfToken;
 
 /**
  * @author Scott Nicklous
  * @author Neil Griffin
  * 
- * This is a utility class that collects all data needed by the portlet
- * hub in one location. The data is formatted appropriately for transmission to 
- * the client.
+ *         This is a utility class that collects all data needed by the portlet hub in one location. The data is
+ *         formatted appropriately for transmission to the client.
  *
  */
 public class PageState {
+   private final Logger                     LOGGER  = LoggerFactory.getLogger(PageState.class);
 
-   private CsrfToken               csrfToken;
-   private DriverConfiguration     drvrConfig;
-   private PageConfig              pageConfig;
-   private PortalRequestContext    portalRC;
-   private PortalURL               portalUrl;
-   private ServletContext          servletContext;
-   private Map<String, RenderData> renderDataMap;
+   private CsrfToken                        csrfToken;
+   private DriverConfiguration              drvrConfig;
+   private PageConfig                       pageConfig;
+   private PortalRequestContext             portalRC;
+   private PortalURL                        portalUrl;
+   private ServletContext                   servletContext;
+   private Map<String, RenderData>          renderDataMap;
 
    /**
     * Constructor. Access the classes containing the necessary data.
-    *  
+    * 
     * @param request
     */
    public PageState(HttpServletRequest request) {
       portalRC = PortalRequestContext.getContext(request);
-      portalUrl = portalRC.getRequestedPortalURL(); 
-      drvrConfig = (DriverConfiguration) portalRC.getServletContext()
-            .getAttribute(AttributeKeys.DRIVER_CONFIG);
+      portalUrl = portalRC.getRequestedPortalURL();
+      drvrConfig = (DriverConfiguration) portalRC.getServletContext().getAttribute(AttributeKeys.DRIVER_CONFIG);
       servletContext = portalRC.getServletContext();
       pageConfig = portalUrl.getPageConfig(servletContext);
-      csrfToken = (CsrfToken)request.getAttribute(CsrfToken.class.getName());
+      csrfToken = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
    }
 
    public PageState(HttpServletRequest request, Map<String, RenderData> renderDataMap) {
@@ -106,14 +106,14 @@ public class PageState {
    public Collection<PortalURLParameter> getParameters() {
       return portalUrl.getParameters();
    }
-   
+
    /**
     * Returns the public render parameters set on the current URL.
     * 
     * @return
     */
    public Map<String, String[]> getPublicParameters() {
-      Map<String,String[]> tmp = new HashMap<String, String[]>();
+      Map<String, String[]> tmp = new HashMap<String, String[]>();
       PublicRenderParameterMapper prpMapper = portalUrl.getPublicRenderParameterMapper();
       if (prpMapper != null) {
          for (int ind : prpMapper.getActiveIndexes()) {
@@ -124,7 +124,7 @@ public class PageState {
       }
       return tmp;
    }
-   
+
    /**
     * Returns a collection containing the portlet IDs for the portlets on the page.
     * 
@@ -133,15 +133,16 @@ public class PageState {
    public Collection<String> getPortletIds() {
       return pageConfig.getPortletIds();
    }
-   
+
    /**
     * Returns a Pluto namespace string for the input portlet ID.
-    * @param   portletId
-    * @return  namespace string for the portlet ID
+    * 
+    * @param portletId
+    * @return namespace string for the portlet ID
     */
    public String getNameSpace(String portletId) {
       StringBuffer ns = new StringBuffer("Pluto_");
-      for (int ii=0; ii < portletId.length(); ii++) {
+      for (int ii = 0; ii < portletId.length(); ii++) {
          if (Character.isJavaIdentifierPart(portletId.charAt(ii))) {
             ns.append(portletId.charAt(ii));
          } else {
@@ -151,11 +152,12 @@ public class PageState {
       ns.append("_");
       return ns.toString();
    }
-   
+
    /**
     * Returns the public render parameter names for the portlet ID as a set.
-    * @param   portletId  
-    * @return  Set of string public render parameter names
+    * 
+    * @param portletId
+    * @return Set of string public render parameter names
     */
    public Set<String> getPRPNames(String portletId) {
       Set<String> prpnames = new HashSet<String>();
@@ -168,19 +170,22 @@ public class PageState {
             String prp = prps.nextElement();
             prpnames.add(prp);
          }
-      } catch (Exception e) {}
+      } catch (Exception e) {
+      }
       return prpnames;
    }
-   
+
    /**
-    * Returns the public render parameter names for the portlet ID as a 
-    * delimited string that can be used in a JSON array.
-    * @param   portletId  
-    * @return  Delimited string of public render parameter names
+    * Returns the public render parameter names for the portlet ID as a delimited string that can be used in a JSON
+    * array.
+    * 
+    * @param portletId
+    * @return Delimited string of public render parameter names
     */
    public String getPRPNamesAsString(String portletId) {
       PortletConfig pc = null;
-      StringBuffer prpstring = new StringBuffer();;
+      StringBuffer prpstring = new StringBuffer();
+      ;
 
       String sep = "";
       try {
@@ -189,18 +194,19 @@ public class PageState {
          while (prps.hasMoreElements()) {
             String prp = prps.nextElement();
             prpstring.append(sep + "\"" + prp + "\"");
-            sep = ", ";
+            sep = ",";
          }
-      } catch (Exception e) {}
+      } catch (Exception e) {
+      }
       return prpstring.toString();
    }
-   
-   
+
    /**
-    * Returns the supported portlet mode names for the portlet ID as a 
-    * delimited string that can be used in a JSON array.
-    * @param   portletId  
-    * @return  Delimited string of portlet mode names
+    * Returns the supported portlet mode names for the portlet ID as a delimited string that can be used in a JSON
+    * array.
+    * 
+    * @param portletId
+    * @return Delimited string of portlet mode names
     */
    public String getPortletModesAsString(String portletId) {
       StringBuffer pmstring = new StringBuffer();
@@ -209,18 +215,19 @@ public class PageState {
          String sep = "";
          for (PortletMode pm : allowedPMs) {
             pmstring.append(sep + "\"" + pm.toString() + "\"");
-            sep = ", ";
+            sep = ",";
          }
-      } catch (Exception e) {}
+      } catch (Exception e) {
+      }
       return pmstring.toString();
    }
-   
-   
+
    /**
-    * Returns the supported window state names for the portlet ID as a 
-    * delimited string that can be used in a JSON array.
-    * @param   portletId  
-    * @return  Delimited string of window state names
+    * Returns the supported window state names for the portlet ID as a delimited string that can be used in a JSON
+    * array.
+    * 
+    * @param portletId
+    * @return Delimited string of window state names
     */
    public String getWindowStatesAsString(String portletId) {
       StringBuffer wsstring = new StringBuffer();
@@ -229,12 +236,13 @@ public class PageState {
          String sep = "";
          for (WindowState ws : allowedWSs) {
             wsstring.append(sep + "\"" + ws.toString() + "\"");
-            sep = ", ";
+            sep = ",";
          }
-      } catch (Exception e) {}
+      } catch (Exception e) {
+      }
       return wsstring.toString();
    }
-   
+
    /**
     * Returns the current portlet mode for the portlet
     * 
@@ -244,7 +252,7 @@ public class PageState {
    public String getPortletMode(String portletId) {
       return portalUrl.getPortletMode(portletId).toString();
    }
-   
+
    /**
     * Returns the current window state for the portlet
     * 
@@ -254,151 +262,162 @@ public class PageState {
    public String getWindowState(String portletId) {
       return portalUrl.getWindowState(portletId).toString();
    }
-   
+
    /**
-    * Returns a relative base base as needed by the portlet hub to generate
-    * URLs. Must be encoded by the caller.
+    * Returns a relative base base as needed by the portlet hub to generate URLs. Must be encoded by the caller.
     * 
     * @return
     */
    public String getUrlBase() {
       StringBuffer ub = new StringBuffer();
-      ub.append(portalUrl.getServletPath().startsWith("/")?"":"/")
-        .append(portalUrl.getServletPath())
-        .append(portalUrl.getRenderPath());
+      ub.append(portalUrl.getServletPath().startsWith("/") ? "" : "/").append(portalUrl.getServletPath()).append(portalUrl.getRenderPath());
       return ub.toString();
    }
-   
+
    /**
-    * Returns the page state needed by the portlet hub as a JSON string suitable
-    * for transport to the client.
+    * Returns the page state needed by the portlet hub as a JSON string suitable for transport to the client.
     * 
-    * @return  JSON string representing the current page state
+    * @return JSON string representing the current page state
     */
    public String toJSONString() {
       StringBuffer json = new StringBuffer(1024);
-      json.append("{\"portlets\" : {\n");
-      
+      json.append("{\"portlets\":{");
+
       Collection<PortalURLParameter> pups = getParameters();
-      Map<String, String[]> pubparms = getPublicParameters();
       PublicRenderParameterMapper mapper = portalUrl.getPublicRenderParameterMapper();
-      int numGroups = mapper.getNumberOfGroups();
-      
+
       boolean sep = false;
       for (String pid : getPortletIds()) {
          if (sep) {
-            json.append(",\n");
+            json.append(",");
          }
          sep = true;
-         json.append("   \"" + getNameSpace(pid) + "\" : {\n");
-         json.append("      \"state\" : {\n");
-         json.append("         \"parameters\" : {\n");
+         json.append("\"" + getNameSpace(pid) + "\":{");
+         json.append("\"state\":{");
+         json.append("\"parameters\":{");
 
          // Add the portlet parameters
-         String c1 = "            \"";
-         for (PortalURLParameter pup : pups){
-            if (pup.getWindowId().equals(pid)){
-               json.append(c1  + pup.getName() + "\" : [");
+         String c1 = "\"";
+         for (PortalURLParameter pup : pups) {
+            if (pup.getWindowId().equals(pid)) {
+               json.append(c1 + pup.getName() + "\":[");
                String c2 = "";
                for (String val : pup.getValues()) {
-                  json.append(c2 + " \"" + val + "\"");
+                  json.append(c2 + "\"" + val + "\"");
                   c2 = ",";
                }
                json.append("]");
-               c1 = ",\n            \"";
+               c1 = ",\"";
             }
          }
-         
+
          // Add the public render parameter values for this portlet
-         
+
          for (PortalURLPublicParameter prp : mapper.getPRPsForWindow(pid, true)) {
-            json.append(c1 + prp.getName() + "\" : [");
+            json.append(c1 + prp.getName() + "\":[");
             String c2 = "";
             for (String val : prp.getValues()) {
                json.append(c2 + " \"" + val + "\"");
                c2 = ",";
             }
             json.append("]");
-            c1 = ",\n            \"";
+            c1 = ",\"";
          }
-         
-//          Set<String> prpnames = getPRPNames(pid);
-//          for (String prp : pubparms.keySet()) {
-//             if (prpnames.contains(prp)) {
-//                json.append(c1 + prp + "\" : [");
-//                String c2 = "";
-//                for (String val : (String[])pubparms.get(prp)) {
-//                   json.append(c2 + " \"" + val + "\"");
-//                   c2 = ",";
-//                }
-//                json.append("]");
-//                c1 = ",\n            \"";
-//             }
-//          }
-
-         json.append("         }, \n");
-         json.append("         \"portletMode\" : \"" + getPortletMode(pid) + "\", \n");
-         json.append("         \"windowState\" : \"" + getWindowState(pid) + "\"\n");
-         json.append("      },\n");
-//          json.append("      \"pubParms\" : [" + getPRPNamesAsString(pid) + "],\n");
-         
-         json.append("      \"pubParms\" : {");
+
+         json.append("}, ");
+         json.append("\"portletMode\":\"" + getPortletMode(pid) + "\",");
+         json.append("\"windowState\":\"" + getWindowState(pid) + "\"");
+         json.append("},");
+         json.append("\"pubParms\":{");
+
          String c2 = "";
          for (PortalURLPublicParameter prp : mapper.getPRPsForWindow(pid, false)) {
             int group = mapper.getIndex(prp);
             json.append(c2);
             json.append("\"");
             json.append(prp.getName());
-            json.append("\" : \"");
+            json.append("\":\"");
             json.append(String.valueOf(group));
             json.append("\"");
-            c2 = ", ";
+            c2 = ",";
          }
-         json.append("},\n");
-         
-         json.append("      \"allowedPM\" : [" + getPortletModesAsString(pid) + "],\n");
-         json.append("      \"allowedWS\" : [" + getWindowStatesAsString(pid) + "],\n");
-         json.append("      \"renderData\" : {\n");
+         json.append("},");
+
+         json.append("\"allowedPM\":[" + getPortletModesAsString(pid) + "],");
+         json.append("\"allowedWS\":[" + getWindowStatesAsString(pid) + "],");
+         json.append("\"renderData\":{");
 
          RenderData renderData = null;
          if (renderDataMap != null) {
             renderData = renderDataMap.get(pid);
          }
          if (renderData == null) {
-            json.append("         \"content\" : null,\n");
-            json.append("         \"mimeType\" : \"text/plain\"\n");
-         }
-         else {
-            json.append("         \"content\" : \"");
+            json.append("\"content\":null,");
+            json.append("\"mimeType\":\"text/plain\"");
+         } else {
+            json.append("\"content\":\"");
             String content = StringEscapeUtils.escapeJson(renderData.getContent());
             json.append(content);
-            json.append("\",\n");
-            json.append("         \"mimeType\" : \"");
+            json.append("\",");
+            json.append("\"mimeType\":\"");
             json.append(renderData.getContentType());
-            json.append("\"\n");
+            json.append("\"");
          }
-         json.append("      },\n");
-         json.append("      \"urlpid\" : \"" + pid + "\"\n");
-         json.append("   }");
+         json.append("},");
+         json.append("\"urlpid\":\"" + pid + "\"");
+         json.append("}");
       }
-      
-      json.append("\n},\n\"prpMap\" : {");
+
+      json.append("},\"prpMap\":{");
       String sep1 = "";
-      for (int ii=0; ii < mapper.getNumberOfGroups(); ii++) {
-         json.append(sep1).append("\n   \"").append(String.valueOf(ii)).append("\" : {");
+      for (int ii = 0; ii < mapper.getNumberOfGroups(); ii++) {
+         json.append(sep1).append("\"").append(String.valueOf(ii)).append("\":{");
          sep1 = ",";
          String sep2 = "";
          for (PortalURLPublicParameter prp : mapper.getPublicParameterGroup(ii)) {
-            json.append(sep2).append("\n      \"")
-                .append(getNameSpace(prp.getWindowId()))
-                .append("\" : \"")
-                .append(prp.getName()).append("\"");
-            sep2 = ", ";
+            json.append(sep2).append("\"").append(getNameSpace(prp.getWindowId())).append("\":\"").append(prp.getName()).append("\"");
+            sep2 = ",";
          }
-         json.append("   }");
+         json.append("}");
+      }
+      json.append("}}");
+
+      if (LOGGER.isDebugEnabled()) {
+         LOGGER.debug("Resulting json string:\n   " + json);
       }
-      json.append("\n}}");
 
       return json.toString();
    }
+
+   /**
+    * This method returns the json string encoded in a manner compatible with
+    * the decodeUriComponent JavaScript decode function on the client. Encoding is only
+    * performed if json contains dangerous characters.
+    * 
+    * @return The encoded json string representing the page state.
+    */
+   public String toEncodedJSONString() {
+
+      String json = toJSONString();
+      String ejson = json;
+
+//      boolean mustEscape = json.matches(".*[<>/].*");
+      boolean mustEscape = json.indexOf('<') >= 0;
+      if (mustEscape) {
+         ejson = ejson.replace("<", "%3C");
+//         ejson = ejson.replace(">", "%3E");
+//         ejson = ejson.replace("/", "%2F");
+      }
+
+      if (LOGGER.isDebugEnabled()) {
+         StringBuilder txt = new StringBuilder(128);
+         txt.append("Must escape: ").append(mustEscape);
+         txt.append(", original json contains escapes: ").append(json.matches(".*(%3C|%3E|%2F).*"));
+         txt.append("\nPreprocessed json string:\n   " + json);
+         txt.append("\nEncoded json string:\n   " + ejson);
+         LOGGER.debug(txt.toString());
+      }
+      
+      return ejson;
+   }
 }
diff --git a/pluto-portal/src/main/webapp/WEB-INF/themes/pluto-default-theme.jsp b/pluto-portal/src/main/webapp/WEB-INF/themes/pluto-default-theme.jsp
index 131dd16..a9a972f 100644
--- a/pluto-portal/src/main/webapp/WEB-INF/themes/pluto-default-theme.jsp
+++ b/pluto-portal/src/main/webapp/WEB-INF/themes/pluto-default-theme.jsp
@@ -39,6 +39,7 @@ group (the left column) displays portlets with odd IDs, while the second group
 <html>
 
 <head>
+    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
     <title>Pluto Portal</title>
     <script type="text/javascript">
        /**
@@ -50,7 +51,9 @@ group (the left column) displays portlets with odd IDs, while the second group
        var portlet = portlet || {};
        portlet.impl = portlet.impl || {};
        portlet.impl.getInitData = function () {
-          return <%=ps.toJSONString()%>;
+          var str = '<%=ps.toEncodedJSONString()%>';
+          str = str.replace(/%3C/g, "<");
+          return JSON.parse(str);
        }
 	   portlet.impl.getUrlBase = function () {
 		   return '<%=response.encodeURL(ps.getUrlBase())%>';