View Javadoc

1   /*
2   * Licensed to the Apache Software Foundation (ASF) under one or more
3   * contributor license agreements.  See the NOTICE file distributed with
4   * this work for additional information regarding copyright ownership.
5   * The ASF licenses this file to You under the Apache License, Version 2.0
6   * (the "License"); you may not use this file except in compliance with
7   * the License.  You may obtain a copy of the License at
8   *
9   *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17  package org.apache.jetspeed.portlets.security.constraints;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  import java.util.ArrayList;
22  import java.util.Iterator;
23  
24  import javax.portlet.PortletConfig;
25  import javax.portlet.PortletException;
26  import javax.portlet.RenderRequest;
27  import javax.portlet.RenderResponse;
28  import javax.portlet.PortletSession;
29  
30  import org.apache.jetspeed.CommonPortletServices;
31  import org.apache.jetspeed.om.page.PageSecurity;
32  import org.apache.jetspeed.page.PageManager;
33  import org.apache.jetspeed.security.RoleManager;
34  import org.apache.jetspeed.security.GroupManager;
35  import org.springframework.beans.factory.InitializingBean;
36  import org.springframework.web.portlet.ModelAndView;
37  import org.springframework.web.portlet.context.PortletConfigAware;
38  import org.springframework.web.portlet.mvc.AbstractController;
39  
40  public class ConstraintsViewController extends AbstractController implements InitializingBean, PortletConfigAware
41  {	
42      private static final String     ROLES_CACHE_SESSION_ATTRIBUTE_NAME = "j2Roles";
43      private static final String     GROUPS_CACHE_SESSION_ATTRIBUTE_NAME = "j2Groups";
44  
45      private PortletConfig portletConfig ;
46      protected PageManager pageManager;
47  
48      protected RoleManager rm = null;
49      protected GroupManager gm = null;
50      
51      public void afterPropertiesSet() throws Exception 
52      {
53  //        System.out.println("+++ after property set");
54          //throw new IllegalArgumentException("A DOMTreeService is required");
55      }
56      
57  	public ModelAndView handleRenderRequestInternal(RenderRequest request, RenderResponse response) throws Exception
58  	{
59  		Map model = new HashMap();
60  		model.put( "messages", portletConfig.getResourceBundle( request.getLocale() ) );
61  		model.put( "greeting", "Hello");
62          boolean constraintsEnabled = pageManager.getConstraintsEnabled();
63          model.put("constraintsEnabled", new Boolean(constraintsEnabled));
64          PageSecurity constraints = pageManager.getPageSecurity();        
65          model.put("defs", constraints.getSecurityConstraintsDefs());
66          model.put("globals", constraints.getGlobalSecurityConstraintsRefs());
67  
68          PortletSession session = request.getPortletSession();
69          ArrayList roles = (ArrayList)session.getAttribute(ROLES_CACHE_SESSION_ATTRIBUTE_NAME, PortletSession.PORTLET_SCOPE);
70          if ( roles == null )
71          {
72              try
73              {
74                  Iterator rolesIter = rm.getRoles("");
75                  roles = new ArrayList();
76                  if ( rolesIter != null )
77                  {
78                      while( rolesIter.hasNext() )
79                      {
80                          roles.add( rolesIter.next() );
81                      }
82                  }
83                  session.setAttribute(ROLES_CACHE_SESSION_ATTRIBUTE_NAME, roles, PortletSession.PORTLET_SCOPE);
84  //                System.out.println( "roles: " + roles.toString() );
85              }
86              catch(Exception e)
87              {
88                  logger.error( "Could not get list of roles from RoleManager.", e);
89              }
90          }
91          model.put("roles", roles);
92  
93          ArrayList groups = (ArrayList)session.getAttribute(GROUPS_CACHE_SESSION_ATTRIBUTE_NAME, PortletSession.PORTLET_SCOPE);
94          if ( groups == null )
95          {
96              try
97              {
98                  Iterator groupsIter = gm.getGroups("");
99                  groups = new ArrayList();
100                 if ( groupsIter != null )
101                 {
102                     while( groupsIter.hasNext() )
103                     {
104                         groups.add( groupsIter.next() );
105                     }
106                 }
107                 session.setAttribute(GROUPS_CACHE_SESSION_ATTRIBUTE_NAME, groups, PortletSession.PORTLET_SCOPE);
108                 System.out.println( "groups: " + groups.toString() );
109             }
110             catch(Exception e)
111             {
112                 logger.error( "Could not get list of groups from GroupManager.", e);
113             }
114         }
115         model.put("groups", groups);
116         
117         model.put("renderRequest", request);
118 
119         return new ModelAndView("constraintsView", "model", model);
120 	}	
121 	       
122     public void setPortletConfig(PortletConfig portletConfig)
123     {   
124 //        System.out.println("+++ in set portlet config");
125         this.portletConfig = portletConfig;
126         pageManager = (PageManager) getPortletContext().getAttribute(CommonPortletServices.CPS_PAGE_MANAGER_COMPONENT);        
127         if (null == pageManager) 
128         { 
129             PortletException pe = new PortletException("Failed to find the Page Manager on portlet initialization");
130             throw new RuntimeException(pe); 
131         }
132 
133         rm = (RoleManager) getPortletContext().getAttribute(CommonPortletServices.CPS_ROLE_MANAGER_COMPONENT);
134         if (rm == null)
135             throw new RuntimeException("Could not get instance of portal role manager component");
136 
137         gm = (GroupManager) getPortletContext().getAttribute(CommonPortletServices.CPS_GROUP_MANAGER_COMPONENT);
138         if (gm == null)
139             throw new RuntimeException("Could not get instance of portal group manager component");
140 
141 //        System.out.println("--- out set portlet config:" + pageManager);
142     }
143 
144 
145 }