001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.portlets.custom;
018:
019: import java.io.IOException;
020: import java.util.List;
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.Set;
024: import java.util.HashSet;
025:
026: import javax.portlet.PortletConfig;
027: import javax.portlet.PortletContext;
028: import javax.portlet.ActionRequest;
029: import javax.portlet.ActionResponse;
030: import javax.portlet.PortletException;
031: import javax.portlet.PortletMode;
032: import javax.portlet.RenderRequest;
033: import javax.portlet.RenderResponse;
034: import javax.portlet.WindowState;
035:
036: import org.springframework.util.StringUtils;
037:
038: import org.apache.portals.bridges.velocity.GenericVelocityPortlet;
039: import org.apache.velocity.context.Context;
040:
041: import org.apache.jetspeed.CommonPortletServices;
042: import org.apache.jetspeed.PortalReservedParameters;
043: import org.apache.jetspeed.om.page.Page;
044: import org.apache.jetspeed.om.page.Fragment;
045: import org.apache.jetspeed.om.common.SecurityConstraints;
046: import org.apache.jetspeed.om.common.SecurityConstraint;
047: import org.apache.jetspeed.page.PageManager;
048:
049: /**
050: * Common Custom Config Mode Portlet
051: *
052: * @author <a href="mailto:woonsan@apache.org">Woonsan Ko</a>
053: * @version $Id: $
054: */
055: public class CustomConfigModePortlet extends GenericVelocityPortlet {
056: private static final PortletMode CONFIG_MODE = new PortletMode(
057: "config");
058: private static final String DELIMITERS = "[],; ";
059:
060: private PageManager pageManager;
061: private String configPage;
062:
063: public void init(PortletConfig config) throws PortletException {
064: super .init(config);
065:
066: this .configPage = config.getInitParameter("ConfigPage");
067:
068: PortletContext context = getPortletContext();
069: this .pageManager = (PageManager) context
070: .getAttribute(CommonPortletServices.CPS_PAGE_MANAGER_COMPONENT);
071:
072: if (this .pageManager == null) {
073: throw new PortletException(
074: "Could not get instance of pageManager component");
075: }
076: }
077:
078: protected void doDispatch(RenderRequest request,
079: RenderResponse response) throws PortletException,
080: IOException {
081: if (!request.getWindowState().equals(WindowState.MINIMIZED)) {
082: PortletMode curMode = request.getPortletMode();
083:
084: if (CONFIG_MODE.equals(curMode)) {
085: List securityContraintRefList = null;
086:
087: try {
088: securityContraintRefList = this .pageManager
089: .getPageSecurity()
090: .getSecurityConstraintsDefs();
091: } catch (Exception e) {
092: throw new PortletException(
093: "Cannot find page security constraint definitions.",
094: e);
095: }
096:
097: if (securityContraintRefList != null) {
098: request.setAttribute("securityContraintRefList",
099: securityContraintRefList);
100: }
101:
102: request.setAttribute(PARAM_EDIT_PAGE, this .configPage);
103: doEdit(request, response);
104: } else {
105: super .doDispatch(request, response);
106: }
107: }
108: }
109:
110: public void processAction(ActionRequest request,
111: ActionResponse response) throws PortletException,
112: IOException {
113: String action = request.getParameter("action");
114:
115: if ("addConstraint".equals(action)) {
116: addSecurityConstraint(request, response);
117: } else if ("removeConstraint".equals(action)) {
118: removeSecurityConstraint(request, response);
119: } else if ("updateConstraintRefs".equals(action)) {
120: updateSecurityConstraintRefs(request, response);
121: }
122: }
123:
124: private void addSecurityConstraint(ActionRequest request,
125: ActionResponse response) throws PortletException,
126: IOException {
127: try {
128: String path = request.getParameter("path");
129: String fragmentId = request.getParameter("fragment");
130: String type = request.getParameter("type");
131: String roles = request.getParameter("roles");
132: String groups = request.getParameter("groups");
133: String users = request.getParameter("users");
134: String permissions = request.getParameter("permissions");
135:
136: Page page = this .pageManager.getPage(path);
137: Fragment fragment = page.getFragmentById(fragmentId);
138:
139: if (fragment == null) {
140: throw new IllegalStateException(
141: "Cannot find fragment: " + fragmentId);
142: }
143:
144: SecurityConstraints constraints = fragment
145: .getSecurityConstraints();
146:
147: if (constraints == null) {
148: constraints = fragment.newSecurityConstraints();
149: }
150:
151: SecurityConstraint constraint = fragment
152: .newSecurityConstraint();
153: Set roleSet = convertToSet(roles, DELIMITERS);
154: Set groupSet = convertToSet(groups, DELIMITERS);
155: Set userSet = convertToSet(users, DELIMITERS);
156:
157: if (!roleSet.isEmpty()) {
158: constraint.setRoles(new ArrayList(roleSet));
159: }
160: if (!groupSet.isEmpty()) {
161: constraint.setGroups(new ArrayList(groupSet));
162: }
163: if (!userSet.isEmpty()) {
164: constraint.setUsers(new ArrayList(userSet));
165: }
166:
167: Set permissionSet = convertToSet(permissions, DELIMITERS);
168:
169: constraint.setPermissions(new ArrayList(permissionSet));
170: List constraintList = constraints.getSecurityConstraints();
171:
172: if (constraintList == null) {
173: constraintList = new ArrayList();
174: }
175:
176: constraintList.add(constraint);
177:
178: constraints.setSecurityConstraints(constraintList);
179: fragment.setSecurityConstraints(constraints);
180: this .pageManager.updatePage(page);
181: } catch (Exception e) {
182: throw new PortletException(
183: "Failed to add security constraint.", e);
184: }
185: }
186:
187: private void removeSecurityConstraint(ActionRequest request,
188: ActionResponse response) throws PortletException,
189: IOException {
190: try {
191: String path = request.getParameter("path");
192: String fragmentId = request.getParameter("fragment");
193: String roles = request.getParameter("roles");
194: String groups = request.getParameter("groups");
195: String users = request.getParameter("users");
196: String permissions = request.getParameter("permissions");
197:
198: Page page = this .pageManager.getPage(path);
199: Fragment fragment = page.getFragmentById(fragmentId);
200:
201: if (fragment == null) {
202: throw new IllegalStateException(
203: "Cannot find fragment: " + fragmentId);
204: }
205:
206: SecurityConstraints constraints = fragment
207: .getSecurityConstraints();
208:
209: List constraintList = null;
210:
211: if (constraints != null) {
212: constraintList = constraints.getSecurityConstraints();
213:
214: if (constraintList != null) {
215: for (Iterator it = constraintList.iterator(); it
216: .hasNext();) {
217: SecurityConstraint constraint = (SecurityConstraint) it
218: .next();
219:
220: Set removeRoleSet = convertToSet(roles,
221: DELIMITERS);
222: Set removeGroupSet = convertToSet(groups,
223: DELIMITERS);
224: Set removeUserSet = convertToSet(users,
225: DELIMITERS);
226:
227: List roleList = constraint.getRoles();
228: List groupList = constraint.getGroups();
229: List userList = constraint.getUsers();
230:
231: if (equalsSetAndList(removeRoleSet, roleList)
232: && equalsSetAndList(removeGroupSet,
233: groupList)
234: && equalsSetAndList(removeUserSet,
235: userList)) {
236: it.remove();
237: break;
238: }
239: }
240: }
241: }
242:
243: if (constraints != null && constraintList != null) {
244: constraints.setSecurityConstraints(constraintList);
245: }
246:
247: fragment
248: .setSecurityConstraints(constraints.isEmpty() ? null
249: : constraints);
250: this .pageManager.updatePage(page);
251: } catch (Exception e) {
252: throw new PortletException(
253: "Failed to remove security constraint.", e);
254: }
255: }
256:
257: private void updateSecurityConstraintRefs(ActionRequest request,
258: ActionResponse response) throws PortletException,
259: IOException {
260: try {
261: String path = request.getParameter("path");
262: String fragmentId = request.getParameter("fragment");
263: String[] securityConstraintRefs = request
264: .getParameterValues("securityConstraintRef");
265:
266: Page page = this .pageManager.getPage(path);
267: Fragment fragment = page.getFragmentById(fragmentId);
268:
269: if (fragment == null) {
270: throw new IllegalStateException(
271: "Cannot find fragment: " + fragmentId);
272: }
273:
274: SecurityConstraints constraints = fragment
275: .getSecurityConstraints();
276:
277: if (constraints == null) {
278: constraints = fragment.newSecurityConstraints();
279: }
280:
281: Set constraintRefSet = new HashSet();
282:
283: if (securityConstraintRefs != null) {
284: for (int i = 0; i < securityConstraintRefs.length; i++) {
285: if (!"".equals(securityConstraintRefs[i])) {
286: constraintRefSet.add(securityConstraintRefs[i]);
287: }
288: }
289: }
290:
291: constraints
292: .setSecurityConstraintsRefs(constraintRefSet
293: .isEmpty() ? null : new ArrayList(
294: constraintRefSet));
295: fragment
296: .setSecurityConstraints(constraints.isEmpty() ? null
297: : constraints);
298: this .pageManager.updatePage(page);
299: } catch (Exception e) {
300: throw new PortletException(
301: "Failed to remove security constraint.", e);
302: }
303: }
304:
305: private Set convertToSet(String s, String delimiters) {
306: Set set = new HashSet();
307:
308: String[] tokens = StringUtils.tokenizeToStringArray(s,
309: delimiters, true, true);
310:
311: if (tokens != null) {
312: for (int i = 0; i < tokens.length; i++) {
313: set.add(tokens[i]);
314: }
315: }
316:
317: return set;
318: }
319:
320: private boolean equalsSetAndList(Set set, List list) {
321: if (set == null) {
322: return (list == null || list.isEmpty());
323: } else if (list == null) {
324: return set.isEmpty();
325: } else {
326: return set.equals(new HashSet(list));
327: }
328: }
329:
330: }
|