portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From woon...@apache.org
Subject svn commit: r911486 - in /portals/jetspeed-2/applications/j2-admin/trunk/src/main: java/org/apache/jetspeed/portlets/custom/CustomConfigModePortlet.java webapp/WEB-INF/view/custom/config-mode.vm
Date Thu, 18 Feb 2010 16:49:40 GMT
Author: woonsan
Date: Thu Feb 18 16:49:39 2010
New Revision: 911486

URL: http://svn.apache.org/viewvc?rev=911486&view=rev
Log:
JS2-1107: fixing custom config mode portlet based on new page layout component. Thanks to
Randy for providing the proper apis.

Modified:
    portals/jetspeed-2/applications/j2-admin/trunk/src/main/java/org/apache/jetspeed/portlets/custom/CustomConfigModePortlet.java
    portals/jetspeed-2/applications/j2-admin/trunk/src/main/webapp/WEB-INF/view/custom/config-mode.vm

Modified: portals/jetspeed-2/applications/j2-admin/trunk/src/main/java/org/apache/jetspeed/portlets/custom/CustomConfigModePortlet.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/applications/j2-admin/trunk/src/main/java/org/apache/jetspeed/portlets/custom/CustomConfigModePortlet.java?rev=911486&r1=911485&r2=911486&view=diff
==============================================================================
--- portals/jetspeed-2/applications/j2-admin/trunk/src/main/java/org/apache/jetspeed/portlets/custom/CustomConfigModePortlet.java
(original)
+++ portals/jetspeed-2/applications/j2-admin/trunk/src/main/java/org/apache/jetspeed/portlets/custom/CustomConfigModePortlet.java
Thu Feb 18 16:49:39 2010
@@ -18,6 +18,7 @@
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
@@ -33,14 +34,18 @@
 import javax.portlet.RenderResponse;
 import javax.portlet.WindowState;
 
+import org.apache.commons.lang.ArrayUtils;
+import org.apache.commons.lang.StringUtils;
 import org.apache.jetspeed.CommonPortletServices;
+import org.apache.jetspeed.PortalReservedParameters;
+import org.apache.jetspeed.layout.PageLayoutComponent;
 import org.apache.jetspeed.om.common.SecurityConstraint;
 import org.apache.jetspeed.om.common.SecurityConstraints;
-import org.apache.jetspeed.om.page.BaseFragmentElement;
-import org.apache.jetspeed.om.page.Page;
+import org.apache.jetspeed.om.page.ContentFragment;
+import org.apache.jetspeed.om.page.ContentPage;
 import org.apache.jetspeed.page.PageManager;
+import org.apache.jetspeed.request.RequestContext;
 import org.apache.portals.bridges.velocity.GenericVelocityPortlet;
-import org.springframework.util.StringUtils;
 
 /**
  * Common Custom Config Mode Portlet
@@ -50,34 +55,52 @@
 public class CustomConfigModePortlet extends GenericVelocityPortlet
 {
     private static final PortletMode CONFIG_MODE = new PortletMode("config");
-    private static final String DELIMITERS = "[],; ";
+    private static final String DELIMITERS = "[],; \t\r\n";
     
     private PageManager pageManager;
+    private PageLayoutComponent pageLayoutComponent;
     private String configPage;
     
+    @Override
     public void init(PortletConfig config) throws PortletException
     {
         super.init(config);
         
-        this.configPage = config.getInitParameter("ConfigPage");
+        configPage = config.getInitParameter("ConfigPage");
         
         PortletContext context = getPortletContext();
-        this.pageManager = (PageManager) context.getAttribute(CommonPortletServices.CPS_PAGE_MANAGER_COMPONENT);
         
-        if (this.pageManager == null)
+        pageManager = (PageManager) context.getAttribute(CommonPortletServices.CPS_PAGE_MANAGER_COMPONENT);
+        
+        if (pageManager == null)
         {
             throw new PortletException("Could not get instance of pageManager component");
         }
+        
+        pageLayoutComponent = (PageLayoutComponent) context.getAttribute(CommonPortletServices.CPS_PAGE_LAYOUT_COMPONENT);
+        
+        if (pageLayoutComponent == null)
+        {
+            throw new PortletException("Could not get instance of pageLayoutComponent");
+        }
     }
     
+    @Override
     protected void doDispatch(RenderRequest request, RenderResponse response) throws PortletException,
IOException
     {
-        if ( !request.getWindowState().equals(WindowState.MINIMIZED))
+        if (!request.getWindowState().equals(WindowState.MINIMIZED))
         {
             PortletMode curMode = request.getPortletMode();
             
             if (CONFIG_MODE.equals(curMode))
             {
+                ContentFragment curFragment = (ContentFragment) request.getAttribute(PortalReservedParameters.FRAGMENT_ATTRIBUTE);
+                
+                if (curFragment == null)
+                {
+                    throw new PortletException("Cannot retrieve current fragment from the
request.");
+                }
+                
                 List securityContraintRefList = null;
                 
                 try
@@ -94,7 +117,11 @@
                     request.setAttribute("securityContraintRefList", securityContraintRefList);
                 }
                 
+                request.setAttribute("fragmentId", curFragment.getId());
+                request.setAttribute("securityConstraints", new TransientSecurityConstraints(curFragment.getSecurityConstraints()));
+                
                 request.setAttribute(PARAM_EDIT_PAGE, this.configPage);
+                
                 doEdit(request, response);
             }
             else
@@ -104,6 +131,7 @@
         }
     }
 
+    @Override
     public void processAction(ActionRequest request, ActionResponse response) throws PortletException,
IOException
     {
         String action = request.getParameter("action");
@@ -122,66 +150,82 @@
         }
     }
     
+    @Override
+    protected String getTitle(RenderRequest request)
+    {
+        String title = null;
+        
+        try
+        {
+            ContentFragment curFragment = (ContentFragment) request.getAttribute(PortalReservedParameters.FRAGMENT_ATTRIBUTE);
+            title = curFragment.getPortletContent().getTitle();
+        }
+        catch (Exception ignore)
+        {
+        }
+        
+        if (title != null)
+        {
+            return title;
+        }
+        
+        return super.getTitle(request);
+    }
+    
     private void addSecurityConstraint(ActionRequest request, ActionResponse response) throws
PortletException, IOException
     {
         try
         {
-            String path = request.getParameter("path");
+            RequestContext requestContext = (RequestContext) request.getAttribute(RequestContext.REQUEST_PORTALENV);
+            ContentPage page = requestContext.getPage();
             String fragmentId = request.getParameter("fragment");
-            String type = request.getParameter("type");
-            String roles = request.getParameter("roles");
-            String groups = request.getParameter("groups");
-            String users = request.getParameter("users");
-            String permissions = request.getParameter("permissions");
             
-            Page page = this.pageManager.getPage(path);
-            BaseFragmentElement fragment = page.getFragmentById(fragmentId);
+            ContentFragment fragment = page.getFragmentById(fragmentId);
             
             if (fragment == null)
             {
-                throw new IllegalStateException("Cannot find fragment: " + fragmentId);
+                throw new PortletException("Cannot find fragment: " + fragmentId);
             }
             
-            SecurityConstraints constraints = fragment.getSecurityConstraints();
+            SecurityConstraints constraints = new TransientSecurityConstraints(fragment.getSecurityConstraints());
+            SecurityConstraint constraint = new TransientSecurityConstraint(fragment.newSecurityConstraint());
+            String [] rolesArray = StringUtils.split(request.getParameter("roles"), DELIMITERS);
+            String [] groupsArray = StringUtils.split(request.getParameter("groups"), DELIMITERS);
+            String [] usersArray = StringUtils.split(request.getParameter("users"), DELIMITERS);
             
-            if (constraints == null)
+            if (!ArrayUtils.isEmpty(rolesArray))
             {
-                constraints = fragment.newSecurityConstraints();
+                constraint.setRoles(Arrays.asList(rolesArray));
             }
             
-            SecurityConstraint constraint = fragment.newSecurityConstraint();
-            Set roleSet = convertToSet(roles, DELIMITERS);
-            Set groupSet = convertToSet(groups, DELIMITERS);
-            Set userSet = convertToSet(users, DELIMITERS);
-            
-            if (!roleSet.isEmpty())
+            if (!ArrayUtils.isEmpty(groupsArray))
             {
-                constraint.setRoles(new ArrayList(roleSet));
+                constraint.setGroups(Arrays.asList(groupsArray));
             }
-            if (!groupSet.isEmpty())
+            
+            if (!ArrayUtils.isEmpty(usersArray))
             {
-                constraint.setGroups(new ArrayList(groupSet));
+                constraint.setUsers(Arrays.asList(usersArray));
             }
-            if (!userSet.isEmpty())
+            
+            String [] permissionArray = StringUtils.split(StringUtils.defaultString(request.getParameter("permissions")),
DELIMITERS);
+            
+            if (!ArrayUtils.isEmpty(permissionArray))
             {
-                constraint.setUsers(new ArrayList(userSet));
+                constraint.setPermissions(Arrays.asList(permissionArray));
             }
             
-            Set permissionSet = convertToSet(permissions, DELIMITERS);
-            
-            constraint.setPermissions(new ArrayList(permissionSet));
-            List constraintList = constraints.getSecurityConstraints();
+            List<SecurityConstraint> constraintList = constraints.getSecurityConstraints();
             
             if (constraintList == null)
             {
-                constraintList = new ArrayList();
+                constraintList = new ArrayList<SecurityConstraint>();
             }
             
             constraintList.add(constraint);
-            
             constraints.setSecurityConstraints(constraintList);
-            fragment.setSecurityConstraints(constraints);
-            this.pageManager.updatePage(page);
+            
+            pageLayoutComponent.updateSecurityConstraints(fragment, constraints);
         }
         catch (Exception e)
         {
@@ -193,24 +237,25 @@
     {
         try
         {
-            String path = request.getParameter("path");
+            RequestContext requestContext = (RequestContext) request.getAttribute(RequestContext.REQUEST_PORTALENV);
+            ContentPage page = requestContext.getPage();
             String fragmentId = request.getParameter("fragment");
-            String roles = request.getParameter("roles");
-            String groups = request.getParameter("groups");
-            String users = request.getParameter("users");
-            String permissions = request.getParameter("permissions");
             
-            Page page = this.pageManager.getPage(path);
-            BaseFragmentElement fragment = page.getFragmentById(fragmentId);
+            ContentFragment fragment = page.getFragmentById(fragmentId);
             
             if (fragment == null)
             {
-                throw new IllegalStateException("Cannot find fragment: " + fragmentId);
+                throw new PortletException("Cannot find fragment: " + fragmentId);
             }
+
+            String roles = request.getParameter("roles");
+            String groups = request.getParameter("groups");
+            String users = request.getParameter("users");
+            String permissions = request.getParameter("permissions");
             
-            SecurityConstraints constraints = fragment.getSecurityConstraints();
+            SecurityConstraints constraints = new TransientSecurityConstraints(fragment.getSecurityConstraints());
             
-            List constraintList = null;
+            List<SecurityConstraint> constraintList = null;
             
             if (constraints != null)
             {
@@ -222,17 +267,17 @@
                     {
                         SecurityConstraint constraint = (SecurityConstraint) it.next();
                         
-                        Set removeRoleSet = convertToSet(roles, DELIMITERS);
-                        Set removeGroupSet = convertToSet(groups, DELIMITERS);
-                        Set removeUserSet = convertToSet(users, DELIMITERS);
+                        String [] removeRoleArray = StringUtils.split(roles, DELIMITERS);
+                        String [] removeGroupArray = StringUtils.split(groups, DELIMITERS);
+                        String [] removeUserArray = StringUtils.split(users, DELIMITERS);
                         
-                        List roleList = constraint.getRoles();
-                        List groupList = constraint.getGroups();
-                        List userList = constraint.getUsers();
+                        List<String> roleList = constraint.getRoles();
+                        List<String> groupList = constraint.getGroups();
+                        List<String> userList = constraint.getUsers();
                         
-                        if (equalsSetAndList(removeRoleSet, roleList) &&
-                            equalsSetAndList(removeGroupSet, groupList) &&
-                            equalsSetAndList(removeUserSet, userList))
+                        if (hasEqualItems(removeRoleArray, roleList) &&
+                            hasEqualItems(removeGroupArray, groupList) &&
+                            hasEqualItems(removeUserArray, userList))
                         {
                             it.remove();
                             break;
@@ -246,8 +291,7 @@
                 constraints.setSecurityConstraints(constraintList);
             }
             
-            fragment.setSecurityConstraints(constraints.isEmpty() ? null : constraints);
-            this.pageManager.updatePage(page);
+            pageLayoutComponent.updateSecurityConstraints(fragment, constraints);
         }
         catch (Exception e)
         {
@@ -259,26 +303,22 @@
     {
         try
         {
-            String path = request.getParameter("path");
+            RequestContext requestContext = (RequestContext) request.getAttribute(RequestContext.REQUEST_PORTALENV);
+            ContentPage page = requestContext.getPage();
             String fragmentId = request.getParameter("fragment");
-            String [] securityConstraintRefs = request.getParameterValues("securityConstraintRef");
             
-            Page page = this.pageManager.getPage(path);
-            BaseFragmentElement fragment = page.getFragmentById(fragmentId);
+            ContentFragment fragment = page.getFragmentById(fragmentId);
             
             if (fragment == null)
             {
-                throw new IllegalStateException("Cannot find fragment: " + fragmentId);
+                throw new PortletException("Cannot find fragment: " + fragmentId);
             }
             
-            SecurityConstraints constraints = fragment.getSecurityConstraints();
+            String [] securityConstraintRefs = request.getParameterValues("securityConstraintRef");
             
-            if (constraints == null)
-            {
-                constraints = fragment.newSecurityConstraints();
-            }
+            SecurityConstraints constraints = new TransientSecurityConstraints(fragment.getSecurityConstraints());
             
-            Set constraintRefSet = new HashSet();
+            Set<String> constraintRefSet = new HashSet<String>();
             
             if (securityConstraintRefs != null)
             {
@@ -291,9 +331,9 @@
                 }
             }
             
-            constraints.setSecurityConstraintsRefs(constraintRefSet.isEmpty() ? null : new
ArrayList(constraintRefSet));
-            fragment.setSecurityConstraints(constraints.isEmpty() ? null : constraints);
-            this.pageManager.updatePage(page);
+            constraints.setSecurityConstraintsRefs(constraintRefSet.isEmpty() ? null : new
ArrayList<String>(constraintRefSet));
+            
+            pageLayoutComponent.updateSecurityConstraints(fragment, constraints);
         }
         catch (Exception e)
         {
@@ -301,37 +341,170 @@
         }
     }
     
-    private Set convertToSet(String s, String delimiters)
+    private boolean hasEqualItems(String [] array, List<String> list)
     {
-        Set set = new HashSet();
-        
-        String [] tokens = StringUtils.tokenizeToStringArray(s, delimiters, true, true);
+        if (ArrayUtils.isEmpty(array))
+        {
+            return (list == null || list.isEmpty());
+        }
+        else if (list == null)
+        {
+            return ArrayUtils.isEmpty(array);
+        }
+        else if (ArrayUtils.getLength(array) == list.size())
+        {
+            for (String item : array)
+            {
+                if (!list.contains(item))
+                {
+                    return false;
+                }
+            }
             
-        if (tokens != null)
+            return true;
+        }
+        
+        return false;
+    }
+    
+    private class TransientSecurityConstraints implements SecurityConstraints
+    {
+        private String owner;
+        private List<SecurityConstraint> securityConstraints;
+        private List<String> securityConstraintsRefs;
+        
+        public TransientSecurityConstraints(SecurityConstraints sourceSecurityConstraints)
         {
-            for (int i = 0; i < tokens.length; i++)
+            if (sourceSecurityConstraints != null)
             {
-                set.add(tokens[i]);
+                this.owner = sourceSecurityConstraints.getOwner();
+                
+                List<SecurityConstraint> sourceConstraintList = sourceSecurityConstraints.getSecurityConstraints();
+                
+                if (sourceConstraintList != null)
+                {
+                    this.securityConstraints = new ArrayList<SecurityConstraint>();
+                    
+                    for (SecurityConstraint constraint : sourceConstraintList)
+                    {
+                        this.securityConstraints.add(new TransientSecurityConstraint(constraint));
+                    }
+                }
+                
+                List<String> constraintsRefs = sourceSecurityConstraints.getSecurityConstraintsRefs();
+                
+                if (constraintsRefs != null)
+                {
+                    securityConstraintsRefs = new ArrayList<String>(constraintsRefs);
+                }
             }
         }
         
-        return set;
+        public String getOwner()
+        {
+            return owner;
+        }
+
+        public List getSecurityConstraints()
+        {
+            return securityConstraints;
+        }
+
+        public List getSecurityConstraintsRefs()
+        {
+            return securityConstraintsRefs;
+        }
+
+        public boolean isEmpty()
+        {
+            return (this.securityConstraints == null || this.securityConstraints.isEmpty())
&& (this.securityConstraintsRefs == null || this.securityConstraintsRefs.isEmpty());
+        }
+
+        public void setOwner(String owner)
+        {
+            this.owner = owner;
+        }
+
+        public void setSecurityConstraints(List constraints)
+        {
+            this.securityConstraints = constraints;
+        }
+
+        public void setSecurityConstraintsRefs(List constraintsRefs)
+        {
+            this.securityConstraintsRefs = constraintsRefs;
+        }
     }
     
-    private boolean equalsSetAndList(Set set, List list)
+    private class TransientSecurityConstraint implements SecurityConstraint
     {
-        if (set == null)
+        private List<String> roles;
+        private List<String> groups;
+        private List<String> users;
+        private List<String> permissions;
+        
+        public TransientSecurityConstraint(SecurityConstraint sourceSecurityConstraint)
         {
-            return (list == null || list.isEmpty());
+            if (sourceSecurityConstraint.getRoles() != null)
+            {
+                this.roles = new ArrayList<String>(sourceSecurityConstraint.getRoles());
+            }
+            
+            if (sourceSecurityConstraint.getGroups() != null)
+            {
+                this.groups = new ArrayList<String>(sourceSecurityConstraint.getGroups());
+            }
+            
+            if (sourceSecurityConstraint.getUsers() != null)
+            {
+                this.users = new ArrayList<String>(sourceSecurityConstraint.getUsers());
+            }
+            
+            if (sourceSecurityConstraint.getPermissions() != null)
+            {
+                this.permissions = new ArrayList<String>(sourceSecurityConstraint.getPermissions());
+            }
         }
-        else if (list == null)
+        
+        public List getGroups()
+        {
+            return groups;
+        }
+
+        public List getPermissions()
         {
-            return set.isEmpty();
+            return permissions;
         }
-        else
+
+        public List getRoles()
+        {
+            return roles;
+        }
+
+        public List getUsers()
+        {
+            return users;
+        }
+
+        public void setGroups(List groups)
+        {
+            this.groups = groups;
+        }
+
+        public void setPermissions(List permissions)
+        {
+            this.permissions = permissions;
+        }
+
+        public void setRoles(List roles)
+        {
+            this.roles = roles;
+        }
+
+        public void setUsers(List users)
         {
-            return set.equals(new HashSet(list));
+            this.users = users;
         }
     }
     
-}
\ No newline at end of file
+}

Modified: portals/jetspeed-2/applications/j2-admin/trunk/src/main/webapp/WEB-INF/view/custom/config-mode.vm
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/applications/j2-admin/trunk/src/main/webapp/WEB-INF/view/custom/config-mode.vm?rev=911486&r1=911485&r2=911486&view=diff
==============================================================================
--- portals/jetspeed-2/applications/j2-admin/trunk/src/main/webapp/WEB-INF/view/custom/config-mode.vm
(original)
+++ portals/jetspeed-2/applications/j2-admin/trunk/src/main/webapp/WEB-INF/view/custom/config-mode.vm
Thu Feb 18 16:49:39 2010
@@ -16,14 +16,11 @@
 *#
 
 #set ($MESSAGES = $portletConfig.getResourceBundle($renderRequest.Locale))
-#set($curPage = $renderRequest.getAttribute("org.apache.jetspeed.Page"))
-#set($curFrag = $renderRequest.getAttribute("org.apache.jetspeed.Fragment"))
-#set($constraintList = $curFrag.securityConstraints.securityConstraints)
-#set($constraintsRefList = $curFrag.securityConstraints.securityConstraintsRefs)
+#set($fragmentId = $renderRequest.getAttribute("fragmentId"))
+#set($securityConstraints = $renderRequest.getAttribute("securityConstraints"))
+#set($constraintList = $securityConstraints.securityConstraints)
+#set($constraintsRefList = $securityConstraints.securityConstraintsRefs)
 #set($securityContraintRefList = $renderRequest.getAttribute("securityContraintRefList"))
-
-$renderResponse.setTitle($curFrag.getPortletContent().getTitle())
-
 #set($actionUrl = $renderResponse.createActionURL())     
 
 <br/>
@@ -65,8 +62,7 @@
         <td class="$rowstyle">
           <form method="POST" action="${actionUrl}">
             <input type="hidden" name="action" value="removeConstraint">
-            <input type="hidden" name="path" value="${curPage.path}">
-            <input type="hidden" name="fragment" value="${curFrag.id}">
+            <input type="hidden" name="fragment" value="${fragmentId}">
             <input type="hidden" name="roles" value="$!{constraint.roles}">
             <input type="hidden" name="groups" value="$!{constraint.groups}">
             <input type="hidden" name="users" value="$!{constraint.users}">
@@ -91,8 +87,7 @@
         <td class="portlet-section-alternate" nowrap>$MESSAGES.getString("customconfigmode.label.constraintref")</td>
         <td class="portlet-section-body" nowrap>
           <input type="hidden" name="action" value="updateConstraintRefs">
-          <input type="hidden" name="path" value="${curPage.path}">
-          <input type="hidden" name="fragment" value="${curFrag.id}">
+          <input type="hidden" name="fragment" value="${fragmentId}">
           <select name="securityConstraintRef">
             #if ($constraintsRefList)
               <option value=""
@@ -137,8 +132,7 @@
         <td class="portlet-section-alternate" nowrap>$MESSAGES.getString("customconfigmode.label.roles")</td>
         <td class="portlet-section-body" nowrap>
           <input type="hidden" name="action" value="addConstraint">
-          <input type="hidden" name="path" value="${curPage.path}">
-          <input type="hidden" name="fragment" value="${curFrag.id}">
+          <input type="hidden" name="fragment" value="${fragmentId}">
           <input type="text" name="roles" value="" />
         </td>
       </tr>



---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org


Mime
View raw message