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.security.constraints;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021: import java.util.ArrayList;
022: import java.util.Iterator;
023:
024: import javax.portlet.PortletConfig;
025: import javax.portlet.PortletException;
026: import javax.portlet.RenderRequest;
027: import javax.portlet.RenderResponse;
028: import javax.portlet.PortletSession;
029:
030: import org.apache.jetspeed.CommonPortletServices;
031: import org.apache.jetspeed.om.page.PageSecurity;
032: import org.apache.jetspeed.page.PageManager;
033: import org.apache.jetspeed.security.RoleManager;
034: import org.apache.jetspeed.security.GroupManager;
035: import org.springframework.beans.factory.InitializingBean;
036: import org.springframework.web.portlet.ModelAndView;
037: import org.springframework.web.portlet.context.PortletConfigAware;
038: import org.springframework.web.portlet.mvc.AbstractController;
039:
040: public class ConstraintsViewController extends AbstractController
041: implements InitializingBean, PortletConfigAware {
042: private static final String ROLES_CACHE_SESSION_ATTRIBUTE_NAME = "j2Roles";
043: private static final String GROUPS_CACHE_SESSION_ATTRIBUTE_NAME = "j2Groups";
044:
045: private PortletConfig portletConfig;
046: protected PageManager pageManager;
047:
048: protected RoleManager rm = null;
049: protected GroupManager gm = null;
050:
051: public void afterPropertiesSet() throws Exception {
052: // System.out.println("+++ after property set");
053: //throw new IllegalArgumentException("A DOMTreeService is required");
054: }
055:
056: public ModelAndView handleRenderRequestInternal(
057: RenderRequest request, RenderResponse response)
058: throws Exception {
059: Map model = new HashMap();
060: model.put("messages", portletConfig.getResourceBundle(request
061: .getLocale()));
062: model.put("greeting", "Hello");
063: boolean constraintsEnabled = pageManager
064: .getConstraintsEnabled();
065: model
066: .put("constraintsEnabled", new Boolean(
067: constraintsEnabled));
068: PageSecurity constraints = pageManager.getPageSecurity();
069: model.put("defs", constraints.getSecurityConstraintsDefs());
070: model.put("globals", constraints
071: .getGlobalSecurityConstraintsRefs());
072:
073: PortletSession session = request.getPortletSession();
074: ArrayList roles = (ArrayList) session.getAttribute(
075: ROLES_CACHE_SESSION_ATTRIBUTE_NAME,
076: PortletSession.PORTLET_SCOPE);
077: if (roles == null) {
078: try {
079: Iterator rolesIter = rm.getRoles("");
080: roles = new ArrayList();
081: if (rolesIter != null) {
082: while (rolesIter.hasNext()) {
083: roles.add(rolesIter.next());
084: }
085: }
086: session.setAttribute(
087: ROLES_CACHE_SESSION_ATTRIBUTE_NAME, roles,
088: PortletSession.PORTLET_SCOPE);
089: // System.out.println( "roles: " + roles.toString() );
090: } catch (Exception e) {
091: logger
092: .error(
093: "Could not get list of roles from RoleManager.",
094: e);
095: }
096: }
097: model.put("roles", roles);
098:
099: ArrayList groups = (ArrayList) session.getAttribute(
100: GROUPS_CACHE_SESSION_ATTRIBUTE_NAME,
101: PortletSession.PORTLET_SCOPE);
102: if (groups == null) {
103: try {
104: Iterator groupsIter = gm.getGroups("");
105: groups = new ArrayList();
106: if (groupsIter != null) {
107: while (groupsIter.hasNext()) {
108: groups.add(groupsIter.next());
109: }
110: }
111: session.setAttribute(
112: GROUPS_CACHE_SESSION_ATTRIBUTE_NAME, groups,
113: PortletSession.PORTLET_SCOPE);
114: System.out.println("groups: " + groups.toString());
115: } catch (Exception e) {
116: logger
117: .error(
118: "Could not get list of groups from GroupManager.",
119: e);
120: }
121: }
122: model.put("groups", groups);
123:
124: model.put("renderRequest", request);
125:
126: return new ModelAndView("constraintsView", "model", model);
127: }
128:
129: public void setPortletConfig(PortletConfig portletConfig) {
130: // System.out.println("+++ in set portlet config");
131: this .portletConfig = portletConfig;
132: pageManager = (PageManager) getPortletContext().getAttribute(
133: CommonPortletServices.CPS_PAGE_MANAGER_COMPONENT);
134: if (null == pageManager) {
135: PortletException pe = new PortletException(
136: "Failed to find the Page Manager on portlet initialization");
137: throw new RuntimeException(pe);
138: }
139:
140: rm = (RoleManager) getPortletContext().getAttribute(
141: CommonPortletServices.CPS_ROLE_MANAGER_COMPONENT);
142: if (rm == null)
143: throw new RuntimeException(
144: "Could not get instance of portal role manager component");
145:
146: gm = (GroupManager) getPortletContext().getAttribute(
147: CommonPortletServices.CPS_GROUP_MANAGER_COMPONENT);
148: if (gm == null)
149: throw new RuntimeException(
150: "Could not get instance of portal group manager component");
151:
152: // System.out.println("--- out set portlet config:" + pageManager);
153: }
154:
155: }
|