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.groups;
18  
19  import java.io.IOException;
20  import java.security.Principal;
21  import java.sql.Types;
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import javax.portlet.ActionRequest;
27  import javax.portlet.ActionResponse;
28  import javax.portlet.PortletConfig;
29  import javax.portlet.PortletException;
30  import javax.portlet.PortletMode;
31  import javax.portlet.RenderRequest;
32  import javax.portlet.RenderResponse;
33  
34  import org.apache.jetspeed.CommonPortletServices;
35  import org.apache.jetspeed.portlets.security.SecurityResources;
36  import org.apache.jetspeed.portlets.security.SecurityUtil;
37  import org.apache.jetspeed.security.Group;
38  import org.apache.jetspeed.security.GroupManager;
39  import org.apache.jetspeed.security.SecurityException;
40  import org.apache.portals.gems.browser.BrowserIterator;
41  import org.apache.portals.gems.browser.DatabaseBrowserIterator;
42  import org.apache.portals.gems.browser.BrowserPortlet;
43  import org.apache.portals.gems.util.StatusMessage;
44  import org.apache.portals.messaging.PortletMessaging;
45  import org.apache.velocity.context.Context;
46  
47  /***
48   * Group Browser - flat non-hierarchical view
49   * 
50   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
51   * @version $Id: GroupBrowser.java 348264 2005-11-22 22:06:45Z taylor $
52   */
53  public class GroupBrowser extends BrowserPortlet
54  {
55      private GroupManager groupManager;
56      
57      public void init(PortletConfig config)
58      throws PortletException 
59      {
60          super.init(config);
61          groupManager = (GroupManager) 
62              getPortletContext().getAttribute(CommonPortletServices.CPS_GROUP_MANAGER_COMPONENT);
63          if (null == groupManager)
64          {
65              throw new PortletException("Failed to find the Group Manager on portlet initialization");
66          }
67      }
68             
69      public void getRows(RenderRequest request, String sql, int windowSize)
70      {
71          getRows(request, sql, windowSize, "");
72      }
73      
74      public void getRows(RenderRequest request, String sql, int windowSize, String filter)
75      {
76          List resultSetTitleList = new ArrayList();
77          List resultSetTypeList = new ArrayList();
78          
79          resultSetTypeList.add(String.valueOf(Types.VARCHAR));
80          resultSetTitleList.add("group"); // resource bundle key
81  
82          List list = new ArrayList();
83  
84          try
85          {
86              Iterator groups = groupManager.getGroups(filter);
87                          
88              while (groups.hasNext())
89              {
90                  Group group = (Group)groups.next();
91                  
92                  Principal principal = group.getPrincipal();                
93                  list.add(principal.getName());
94              }   
95          }
96          catch (SecurityException sex)
97          {
98              SecurityUtil.publishErrorMessage(request, SecurityResources.TOPIC_GROUPS, sex.getMessage());
99          }                                    
100         
101         BrowserIterator iterator = new DatabaseBrowserIterator(list, resultSetTitleList, resultSetTypeList, windowSize);
102         setBrowserIterator(request, iterator);
103         iterator.sort("group"); // resource bundle key
104     }
105        
106     public void doView(RenderRequest request, RenderResponse response)
107     throws PortletException, IOException
108     {
109         String selected = (String)PortletMessaging.receive(request, SecurityResources.TOPIC_GROUPS, SecurityResources.MESSAGE_SELECTED);
110         if (selected != null)
111         {        
112             Context context = this.getContext(request);
113             context.put("selected", selected);
114         }
115         StatusMessage msg = (StatusMessage)PortletMessaging.consume(request, SecurityResources.TOPIC_GROUPS, SecurityResources.MESSAGE_STATUS);
116         if (msg != null)
117         {
118             this.getContext(request).put("statusMsg", msg);            
119         }
120         
121         String filtered = (String)PortletMessaging.receive(request, SecurityResources.TOPIC_GROUPS, SecurityResources.MESSAGE_FILTERED);
122         if (filtered != null)
123         {
124             this.getContext(request).put(FILTERED, "on");            
125         }        
126         String refresh = (String)PortletMessaging.consume(request, SecurityResources.TOPIC_GROUPS, SecurityResources.MESSAGE_REFRESH); 
127         if (refresh != null)
128         {        
129             this.clearBrowserIterator(request);
130         }                
131         
132         ArrayList errorMessages = (ArrayList)PortletMessaging.consume(request, SecurityResources.TOPIC_GROUPS, SecurityResources.ERROR_MESSAGES);
133         if (errorMessages != null )
134         {
135             this.getContext(request).put(SecurityResources.ERROR_MESSAGES, errorMessages);
136         }
137         
138         super.doView(request, response);
139     }
140 
141     public void processAction(ActionRequest request, ActionResponse response)
142     throws PortletException, IOException
143     {
144         if (request.getPortletMode() == PortletMode.VIEW)
145         {
146             String selected = request.getParameter("group");
147             if (selected != null)
148             {
149                 Group group = lookupGroup(request, selected);
150                 if (group != null)
151                 {
152                     PortletMessaging.publish(request, SecurityResources.TOPIC_GROUPS, SecurityResources.MESSAGE_SELECTED, selected);
153                     PortletMessaging.publish(request, SecurityResources.TOPIC_GROUPS, SecurityResources.MESSAGE_CHANGED, selected);
154                 }
155             }
156         }
157         
158         String filtered = request.getParameter(FILTERED);
159         if (filtered != null)
160         {
161             PortletMessaging.publish(request, SecurityResources.TOPIC_GROUPS, SecurityResources.MESSAGE_FILTERED, "on");            
162         }
163         else
164         {
165             PortletMessaging.cancel(request, SecurityResources.TOPIC_GROUPS, SecurityResources.MESSAGE_FILTERED);
166         }
167         
168         super.processAction(request, response);
169             
170     }
171 
172     private Group lookupGroup(ActionRequest actionRequest, String groupName)
173     {
174         try
175         {
176             return groupManager.getGroup(groupName);
177         }
178         catch (SecurityException sex)
179         {
180             SecurityUtil.publishErrorMessage(actionRequest, SecurityResources.TOPIC_GROUPS, sex.getMessage());
181             return null;
182         }
183     }
184     
185 }