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/11/19 17:42:28 UTC

[12/15] portals-pluto git commit: Added PageState utility class to pull together the state information for all portlets on the page.

Added PageState utility class to pull together the state information for all portlets on the page.


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

Branch: refs/heads/PortletHub
Commit: 9b23599fb8d137b3a0b712670f8c07b2c614fcdb
Parents: 59cd72e
Author: Scott Nicklous <ms...@apache.org>
Authored: Wed Nov 19 11:11:59 2014 +0100
Committer: Scott Nicklous <ms...@apache.org>
Committed: Wed Nov 19 11:11:59 2014 +0100

----------------------------------------------------------------------
 .../org/apache/pluto/driver/util/PageState.java | 300 +++++++++++++++++++
 .../apache/pluto/driver/util/package-info.java  |  25 ++
 .../WEB-INF/themes/pluto-default-theme.jsp      |  79 ++---
 3 files changed, 344 insertions(+), 60 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/9b23599f/pluto-portal-driver-impl/src/main/java/org/apache/pluto/driver/util/PageState.java
----------------------------------------------------------------------
diff --git a/pluto-portal-driver-impl/src/main/java/org/apache/pluto/driver/util/PageState.java b/pluto-portal-driver-impl/src/main/java/org/apache/pluto/driver/util/PageState.java
new file mode 100644
index 0000000..6403de7
--- /dev/null
+++ b/pluto-portal-driver-impl/src/main/java/org/apache/pluto/driver/util/PageState.java
@@ -0,0 +1,300 @@
+/*  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+
+package org.apache.pluto.driver.util;
+
+import static java.util.logging.Level.*;
+
+import java.util.Collection;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.logging.Logger;
+
+import javax.portlet.PortletConfig;
+import javax.portlet.PortletMode;
+import javax.portlet.PortletURL;
+import javax.portlet.WindowState;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.pluto.driver.AttributeKeys;
+import org.apache.pluto.driver.config.DriverConfiguration;
+import org.apache.pluto.driver.core.PortalRequestContext;
+import org.apache.pluto.driver.services.portal.PageConfig;
+import org.apache.pluto.driver.url.PortalURL;
+import org.apache.pluto.driver.url.PortalURLParameter;
+
+/**
+ * @author Scott Nicklous
+ * 
+ * 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 static final String LOG_CLASS = PageState.class.getName();
+   private final Logger        LOGGER    = Logger.getLogger(LOG_CLASS);
+
+   private DriverConfiguration   drvrConfig;
+   private PageConfig            pageConfig;
+   private PortalRequestContext  portalRC;
+   private PortalURL             portalUrl;
+   private ServletContext        servletContext;
+   
+   /**
+    * 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);
+      servletContext = portalRC.getServletContext();
+      pageConfig = portalUrl.getPageConfig(servletContext);
+   }
+   
+   /**
+    * Returns the portal URL parameters that are set on the current URL.
+    * 
+    * @return
+    */
+   public Collection<PortalURLParameter> getParameters() {
+      return portalUrl.getParameters();
+   }
+   
+   /**
+    * Returns the public render parameters set on the current URL.
+    * 
+    * @return
+    */
+   public Map<String, String[]> getPublicParameters() {
+      return portalUrl.getPublicParameters();
+   }
+   
+   /**
+    * Returns a collection containing the portlet IDs for the portlets on the page.
+    * 
+    * @return
+    */
+   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
+    */
+   public String getNameSpace(String portletId) {
+      StringBuffer ns = new StringBuffer("Pluto_");
+      for (int ii=0; ii < portletId.length(); ii++) {
+         if (Character.isJavaIdentifierPart(portletId.charAt(ii))) {
+            ns.append(portletId.charAt(ii));
+         } else {
+            ns.append("_");
+         }
+      }
+      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
+    */
+   public Set<String> getPRPNames(String portletId) {
+      Set<String> prpnames = new HashSet<String>();
+      PortletConfig pc = null;
+
+      try {
+         pc = drvrConfig.getPortletConfig(portletId);
+         Enumeration<String> prps = pc.getPublicRenderParameterNames();
+         while (prps.hasMoreElements()) {
+            String prp = prps.nextElement();
+            prpnames.add(prp);
+         }
+      } 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
+    */
+   public String getPRPNamesAsString(String portletId) {
+      PortletConfig pc = null;
+      StringBuffer prpstring = new StringBuffer();;
+
+      String sep = "";
+      try {
+         pc = drvrConfig.getPortletConfig(portletId);
+         Enumeration<String> prps = pc.getPublicRenderParameterNames();
+         while (prps.hasMoreElements()) {
+            String prp = prps.nextElement();
+            prpstring.append(sep + "'" + prp + "'");
+            sep = ", ";
+         }
+      } 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
+    */
+   public String getPortletModesAsString(String portletId) {
+      StringBuffer pmstring = new StringBuffer();
+      try {
+         Set<PortletMode> allowedPMs = drvrConfig.getSupportedPortletModes(portletId);
+         String sep = "";
+         for (PortletMode pm : allowedPMs) {
+            pmstring.append(sep + "'" + pm.toString() + "'");
+            sep = ", ";
+         }
+      } 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
+    */
+   public String getWindowStatesAsString(String portletId) {
+      StringBuffer wsstring = new StringBuffer();
+      try {
+         Set<WindowState> allowedWSs = drvrConfig.getSupportedWindowStates(portletId, "text/html");
+         String sep = "";
+         for (WindowState ws : allowedWSs) {
+            wsstring.append(sep + "'" + ws.toString() + "'");
+            sep = ", ";
+         }
+      } catch (Exception e) {}
+      return wsstring.toString();
+   }
+   
+   /**
+    * Returns the current portlet mode for the portlet
+    * 
+    * @param portletId
+    * @return
+    */
+   public String getPortletMode(String portletId) {
+      return portalUrl.getPortletMode(portletId).toString();
+   }
+   
+   /**
+    * Returns the current window state for the portlet
+    * 
+    * @param portletId
+    * @return
+    */
+   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.
+    * 
+    * @return
+    */
+   public String getUrlBase() {
+      StringBuffer ub = new StringBuffer();
+      ub.append(portalUrl.getServletPath().startsWith("/")?"":"/")
+        .append(portalUrl.getServletPath())
+        .append(portalUrl.getRenderPath());
+      return ub.toString();
+   }
+   
+   public String toJSONString() {
+      StringBuffer json = new StringBuffer(1024);
+      json.append("{");
+      
+      Collection<PortalURLParameter> pups = getParameters();
+      Map<String, String[]> pubparms = getPublicParameters();
+      
+      for (String pid : getPortletIds()) {
+         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 c2 = "";
+               for (String val : pup.getValues()) {
+                  json.append(c2 + " '" + val + "'");
+                  c2 = ",";
+               }
+               json.append("]");
+               c1 = ",\n            '";
+            }
+         }
+         
+         // Add the public render parameter values for this portlet
+         
+         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("         }, ");
+         json.append("         'portletMode' : '" + getPortletMode(pid) + "', ");
+         json.append("         'windowState' : '" + getWindowState(pid) + "'");
+         json.append("      },");
+         json.append("      'pubParms' : [" + getPRPNamesAsString(pid) + "],");
+         json.append("      'allowedPM' : [" + getPortletModesAsString(pid) + "],");
+         json.append("      'allowedWS' : [" + getWindowStatesAsString(pid) + "],");
+         json.append("      'renderData' : {");
+         json.append("         'renderData' : null,");
+         json.append("         'mimeType' : \"text/plain\"");
+         json.append("      },");
+         json.append("      'urlpid' : '" + pid + "'");
+         json.append("   },");
+      }
+      
+      json.append("}");
+      return json.toString();
+   }
+}

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/9b23599f/pluto-portal-driver-impl/src/main/java/org/apache/pluto/driver/util/package-info.java
----------------------------------------------------------------------
diff --git a/pluto-portal-driver-impl/src/main/java/org/apache/pluto/driver/util/package-info.java b/pluto-portal-driver-impl/src/main/java/org/apache/pluto/driver/util/package-info.java
new file mode 100644
index 0000000..faa0d9b
--- /dev/null
+++ b/pluto-portal-driver-impl/src/main/java/org/apache/pluto/driver/util/package-info.java
@@ -0,0 +1,25 @@
+/*  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+/**
+ * @author Scott Nicklous
+ *
+ * Package containing utility classes used by the impl, primarily for
+ * providing the necessary server-side support for the client-side portlet hub.
+ */
+package org.apache.pluto.driver.util;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/9b23599f/pluto-portal/src/main/webapp/WEB-INF/themes/pluto-default-theme.jsp
----------------------------------------------------------------------
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 25870f8..6972a29 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
@@ -22,6 +22,7 @@ limitations under the License.
 <%@ taglib uri="http://portals.apache.org/pluto" prefix="pluto" %>
 <%@ page import="java.util.*,javax.portlet.*,org.apache.pluto.driver.url.*" %>
 <%@ page import="org.apache.pluto.driver.config.*,org.apache.pluto.driver.*" %>
+<%@ page import="org.apache.pluto.driver.util.*" %>
 <%@ page import="org.apache.pluto.container.*,javax.servlet.jsp.*" %>
 <% pageContext.setAttribute("now", new java.util.Date()); %>
 
@@ -50,63 +51,27 @@ group (the left column) displays portlets with odd IDs, while the second group
        portlet.impl.getInitData = function () {
           return {       
              <%
-                PortalRequestContext prc = PortalRequestContext.getContext(request);
-                PortalURL pu = prc.getRequestedPortalURL(); 
-                Collection<PortalURLParameter> pups = pu.getParameters();
-                Map<String, String[]> pubparms = pu.getPublicParameters();
-                DriverConfiguration dc = (DriverConfiguration) prc.getServletContext()
-                      .getAttribute(AttributeKeys.DRIVER_CONFIG);
+                PageState ps = new PageState(request);
+                Collection<String> poids = ps.getPortletIds();
+             
+                Collection<PortalURLParameter> pups = ps.getParameters();
+                Map<String, String[]> pubparms = ps.getPublicParameters();
              %>
              <c:forEach var="pid" items="${currentPage.portletIds}">
                 <%
-                   StringBuffer ns = new StringBuffer("Pluto_");
                    String pid = (String)pageContext.getAttribute("pid");
-                   for (int ii=0; ii < pid.length(); ii++) {
-                      if (Character.isJavaIdentifierPart(pid.charAt(ii))) {
-                         ns.append(pid.charAt(ii));
-                      } else {
-                         ns.append("_");
-                      }
-                   }
-                   ns.append("_");
-
-                   PortletConfig pc = null;
-                   StringBuffer prpstring = new StringBuffer();
-                   Set<String> prpnames = new HashSet<String>();
-                   String sep = "";
-                   try {
-                      pc = dc.getPortletConfig(pid);
-                      Enumeration<String> prps = pc.getPublicRenderParameterNames();
-                      while (prps.hasMoreElements()) {
-                         String prp = prps.nextElement();
-                         prpnames.add(prp);
-                         prpstring.append(sep + "'" + prp + "'");
-                         sep = ", ";
-                      }
-                   } catch (Exception e) {}
                    
-                   StringBuffer pmstring = new StringBuffer();
-                   try {
-                      Set<PortletMode> allowedPMs = dc.getSupportedPortletModes(pid);
-                      sep = "";
-                      for (PortletMode pm : allowedPMs) {
-                         pmstring.append(sep + "'" + pm.toString() + "'");
-                         sep = ", ";
-                      }
-                   } catch (Exception e) {}
+                   String ns = ps.getNameSpace(pid);
+
+                   String prpstring = ps.getPRPNamesAsString(pid);
+                   Set<String> prpnames = ps.getPRPNames(pid);
+                   
+                   String pmstring = ps.getPortletModesAsString(pid);
                    
-                   StringBuffer wsstring = new StringBuffer();
-                   try {
-                      Set<WindowState> allowedWSs = dc.getSupportedWindowStates(pid, "text/html");
-                      sep = "";
-                      for (WindowState ws : allowedWSs) {
-                         wsstring.append(sep + "'" + ws.toString() + "'");
-                         sep = ", ";
-                      }
-                   } catch (Exception e) {}
+                   String wsstring = ps.getWindowStatesAsString(pid);
                    
-                   String pm = pu.getPortletMode(pid).toString();
-                   String ws = pu.getWindowState(pid).toString();
+                   String pm = ps.getPortletMode(pid);
+                   String ws = ps.getWindowState(pid);
                 %>
                 '<%=ns%>' : {
                    'state' : {
@@ -145,9 +110,9 @@ group (the left column) displays portlets with odd IDs, while the second group
                       'portletMode' : '<%=pm%>', 
                       'windowState' : '<%=ws%>'
                    },
-                   'pubParms' : [<%=prpstring.toString()%>],
-                   'allowedPM' : [<%=pmstring.toString()%>],
-                   'allowedWS' : [<%=wsstring.toString()%>],
+                   'pubParms' : [<%=prpstring%>],
+                   'allowedPM' : [<%=pmstring%>],
+                   'allowedWS' : [<%=wsstring%>],
                    'renderData' : {
                       'renderData' : null,
                       'mimeType' : "text/plain"
@@ -158,13 +123,7 @@ group (the left column) displays portlets with odd IDs, while the second group
           }
        }
        <%
-          StringBuffer ub = new StringBuffer();
-          //ub.append(request.getScheme()).append("://").append(request.getServerName());
-          //ub.append(request.getServerPort());
-          ub.append(pu.getServletPath().startsWith("/")?"":"/")
-            .append(pu.getServletPath())
-            .append(pu.getRenderPath());
-          String urlBase = response.encodeURL(ub.toString());
+          String urlBase = response.encodeURL(ps.getUrlBase());
        %>
        portlet.impl.getUrlBase = function () {
           return '<%=urlBase%>';