1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.portlets.custom;
18
19 import java.io.IOException;
20 import java.util.List;
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.Set;
24 import java.util.HashSet;
25
26 import javax.portlet.PortletConfig;
27 import javax.portlet.PortletContext;
28 import javax.portlet.ActionRequest;
29 import javax.portlet.ActionResponse;
30 import javax.portlet.PortletException;
31 import javax.portlet.PortletMode;
32 import javax.portlet.RenderRequest;
33 import javax.portlet.RenderResponse;
34 import javax.portlet.WindowState;
35
36 import org.springframework.util.StringUtils;
37
38 import org.apache.portals.bridges.velocity.GenericVelocityPortlet;
39 import org.apache.velocity.context.Context;
40
41 import org.apache.jetspeed.CommonPortletServices;
42 import org.apache.jetspeed.PortalReservedParameters;
43 import org.apache.jetspeed.om.page.Page;
44 import org.apache.jetspeed.om.page.Fragment;
45 import org.apache.jetspeed.om.common.SecurityConstraints;
46 import org.apache.jetspeed.om.common.SecurityConstraint;
47 import org.apache.jetspeed.page.PageManager;
48
49 /***
50 * Common Custom Config Mode Portlet
51 *
52 * @author <a href="mailto:woonsan@apache.org">Woonsan Ko</a>
53 * @version $Id: $
54 */
55 public class CustomConfigModePortlet extends GenericVelocityPortlet
56 {
57 private static final PortletMode CONFIG_MODE = new PortletMode("config");
58 private static final String DELIMITERS = "[],; ";
59
60 private PageManager pageManager;
61 private String configPage;
62
63 public void init(PortletConfig config) throws PortletException
64 {
65 super.init(config);
66
67 this.configPage = config.getInitParameter("ConfigPage");
68
69 PortletContext context = getPortletContext();
70 this.pageManager = (PageManager) context.getAttribute(CommonPortletServices.CPS_PAGE_MANAGER_COMPONENT);
71
72 if (this.pageManager == null)
73 {
74 throw new PortletException("Could not get instance of pageManager component");
75 }
76 }
77
78 protected void doDispatch(RenderRequest request, RenderResponse response) throws PortletException, IOException
79 {
80 if ( !request.getWindowState().equals(WindowState.MINIMIZED))
81 {
82 PortletMode curMode = request.getPortletMode();
83
84 if (CONFIG_MODE.equals(curMode))
85 {
86 List securityContraintRefList = null;
87
88 try
89 {
90 securityContraintRefList = this.pageManager.getPageSecurity().getSecurityConstraintsDefs();
91 }
92 catch (Exception e)
93 {
94 throw new PortletException("Cannot find page security constraint definitions.", e);
95 }
96
97 if (securityContraintRefList != null)
98 {
99 request.setAttribute("securityContraintRefList", securityContraintRefList);
100 }
101
102 request.setAttribute(PARAM_EDIT_PAGE, this.configPage);
103 doEdit(request, response);
104 }
105 else
106 {
107 super.doDispatch(request, response);
108 }
109 }
110 }
111
112 public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException
113 {
114 String action = request.getParameter("action");
115
116 if ("addConstraint".equals(action))
117 {
118 addSecurityConstraint(request, response);
119 }
120 else if ("removeConstraint".equals(action))
121 {
122 removeSecurityConstraint(request, response);
123 }
124 else if ("updateConstraintRefs".equals(action))
125 {
126 updateSecurityConstraintRefs(request, response);
127 }
128 }
129
130 private void addSecurityConstraint(ActionRequest request, ActionResponse response) throws PortletException, IOException
131 {
132 try
133 {
134 String path = request.getParameter("path");
135 String fragmentId = request.getParameter("fragment");
136 String type = request.getParameter("type");
137 String roles = request.getParameter("roles");
138 String groups = request.getParameter("groups");
139 String users = request.getParameter("users");
140 String permissions = request.getParameter("permissions");
141
142 Page page = this.pageManager.getPage(path);
143 Fragment fragment = page.getFragmentById(fragmentId);
144
145 if (fragment == null)
146 {
147 throw new IllegalStateException("Cannot find fragment: " + fragmentId);
148 }
149
150 SecurityConstraints constraints = fragment.getSecurityConstraints();
151
152 if (constraints == null)
153 {
154 constraints = fragment.newSecurityConstraints();
155 }
156
157 SecurityConstraint constraint = fragment.newSecurityConstraint();
158 Set roleSet = convertToSet(roles, DELIMITERS);
159 Set groupSet = convertToSet(groups, DELIMITERS);
160 Set userSet = convertToSet(users, DELIMITERS);
161
162 if (!roleSet.isEmpty())
163 {
164 constraint.setRoles(new ArrayList(roleSet));
165 }
166 if (!groupSet.isEmpty())
167 {
168 constraint.setGroups(new ArrayList(groupSet));
169 }
170 if (!userSet.isEmpty())
171 {
172 constraint.setUsers(new ArrayList(userSet));
173 }
174
175 Set permissionSet = convertToSet(permissions, DELIMITERS);
176
177 constraint.setPermissions(new ArrayList(permissionSet));
178 List constraintList = constraints.getSecurityConstraints();
179
180 if (constraintList == null)
181 {
182 constraintList = new ArrayList();
183 }
184
185 constraintList.add(constraint);
186
187 constraints.setSecurityConstraints(constraintList);
188 fragment.setSecurityConstraints(constraints);
189 this.pageManager.updatePage(page);
190 }
191 catch (Exception e)
192 {
193 throw new PortletException("Failed to add security constraint.", e);
194 }
195 }
196
197 private void removeSecurityConstraint(ActionRequest request, ActionResponse response) throws PortletException, IOException
198 {
199 try
200 {
201 String path = request.getParameter("path");
202 String fragmentId = request.getParameter("fragment");
203 String roles = request.getParameter("roles");
204 String groups = request.getParameter("groups");
205 String users = request.getParameter("users");
206 String permissions = request.getParameter("permissions");
207
208 Page page = this.pageManager.getPage(path);
209 Fragment fragment = page.getFragmentById(fragmentId);
210
211 if (fragment == null)
212 {
213 throw new IllegalStateException("Cannot find fragment: " + fragmentId);
214 }
215
216 SecurityConstraints constraints = fragment.getSecurityConstraints();
217
218 List constraintList = null;
219
220 if (constraints != null)
221 {
222 constraintList = constraints.getSecurityConstraints();
223
224 if (constraintList != null)
225 {
226 for (Iterator it = constraintList.iterator(); it.hasNext(); )
227 {
228 SecurityConstraint constraint = (SecurityConstraint) it.next();
229
230 Set removeRoleSet = convertToSet(roles, DELIMITERS);
231 Set removeGroupSet = convertToSet(groups, DELIMITERS);
232 Set removeUserSet = convertToSet(users, DELIMITERS);
233
234 List roleList = constraint.getRoles();
235 List groupList = constraint.getGroups();
236 List userList = constraint.getUsers();
237
238 if (equalsSetAndList(removeRoleSet, roleList) &&
239 equalsSetAndList(removeGroupSet, groupList) &&
240 equalsSetAndList(removeUserSet, userList))
241 {
242 it.remove();
243 break;
244 }
245 }
246 }
247 }
248
249 if (constraints != null && constraintList != null)
250 {
251 constraints.setSecurityConstraints(constraintList);
252 }
253
254 fragment.setSecurityConstraints(constraints.isEmpty() ? null : constraints);
255 this.pageManager.updatePage(page);
256 }
257 catch (Exception e)
258 {
259 throw new PortletException("Failed to remove security constraint.", e);
260 }
261 }
262
263 private void updateSecurityConstraintRefs(ActionRequest request, ActionResponse response) throws PortletException, IOException
264 {
265 try
266 {
267 String path = request.getParameter("path");
268 String fragmentId = request.getParameter("fragment");
269 String [] securityConstraintRefs = request.getParameterValues("securityConstraintRef");
270
271 Page page = this.pageManager.getPage(path);
272 Fragment fragment = page.getFragmentById(fragmentId);
273
274 if (fragment == null)
275 {
276 throw new IllegalStateException("Cannot find fragment: " + fragmentId);
277 }
278
279 SecurityConstraints constraints = fragment.getSecurityConstraints();
280
281 if (constraints == null)
282 {
283 constraints = fragment.newSecurityConstraints();
284 }
285
286 Set constraintRefSet = new HashSet();
287
288 if (securityConstraintRefs != null)
289 {
290 for (int i = 0; i < securityConstraintRefs.length; i++)
291 {
292 if (!"".equals(securityConstraintRefs[i]))
293 {
294 constraintRefSet.add(securityConstraintRefs[i]);
295 }
296 }
297 }
298
299 constraints.setSecurityConstraintsRefs(constraintRefSet.isEmpty() ? null : new ArrayList(constraintRefSet));
300 fragment.setSecurityConstraints(constraints.isEmpty() ? null : constraints);
301 this.pageManager.updatePage(page);
302 }
303 catch (Exception e)
304 {
305 throw new PortletException("Failed to remove security constraint.", e);
306 }
307 }
308
309 private Set convertToSet(String s, String delimiters)
310 {
311 Set set = new HashSet();
312
313 String [] tokens = StringUtils.tokenizeToStringArray(s, delimiters, true, true);
314
315 if (tokens != null)
316 {
317 for (int i = 0; i < tokens.length; i++)
318 {
319 set.add(tokens[i]);
320 }
321 }
322
323 return set;
324 }
325
326 private boolean equalsSetAndList(Set set, List list)
327 {
328 if (set == null)
329 {
330 return (list == null || list.isEmpty());
331 }
332 else if (list == null)
333 {
334 return set.isEmpty();
335 }
336 else
337 {
338 return set.equals(new HashSet(list));
339 }
340 }
341
342 }