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.groups;
018:
019: import java.io.IOException;
020: import java.security.Principal;
021: import java.sql.Types;
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import javax.portlet.ActionRequest;
027: import javax.portlet.ActionResponse;
028: import javax.portlet.PortletConfig;
029: import javax.portlet.PortletException;
030: import javax.portlet.PortletMode;
031: import javax.portlet.RenderRequest;
032: import javax.portlet.RenderResponse;
033:
034: import org.apache.jetspeed.CommonPortletServices;
035: import org.apache.jetspeed.portlets.security.SecurityResources;
036: import org.apache.jetspeed.portlets.security.SecurityUtil;
037: import org.apache.jetspeed.security.Group;
038: import org.apache.jetspeed.security.GroupManager;
039: import org.apache.jetspeed.security.SecurityException;
040: import org.apache.portals.gems.browser.BrowserIterator;
041: import org.apache.portals.gems.browser.DatabaseBrowserIterator;
042: import org.apache.portals.gems.browser.BrowserPortlet;
043: import org.apache.portals.gems.util.StatusMessage;
044: import org.apache.portals.messaging.PortletMessaging;
045: import org.apache.velocity.context.Context;
046:
047: /**
048: * Group Browser - flat non-hierarchical view
049: *
050: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
051: * @version $Id: GroupBrowser.java 348264 2005-11-22 22:06:45Z taylor $
052: */
053: public class GroupBrowser extends BrowserPortlet {
054: private GroupManager groupManager;
055:
056: public void init(PortletConfig config) throws PortletException {
057: super .init(config);
058: groupManager = (GroupManager) getPortletContext().getAttribute(
059: CommonPortletServices.CPS_GROUP_MANAGER_COMPONENT);
060: if (null == groupManager) {
061: throw new PortletException(
062: "Failed to find the Group Manager on portlet initialization");
063: }
064: }
065:
066: public void getRows(RenderRequest request, String sql,
067: int windowSize) {
068: getRows(request, sql, windowSize, "");
069: }
070:
071: public void getRows(RenderRequest request, String sql,
072: int windowSize, String filter) {
073: List resultSetTitleList = new ArrayList();
074: List resultSetTypeList = new ArrayList();
075:
076: resultSetTypeList.add(String.valueOf(Types.VARCHAR));
077: resultSetTitleList.add("group"); // resource bundle key
078:
079: List list = new ArrayList();
080:
081: try {
082: Iterator groups = groupManager.getGroups(filter);
083:
084: while (groups.hasNext()) {
085: Group group = (Group) groups.next();
086:
087: Principal principal = group.getPrincipal();
088: list.add(principal.getName());
089: }
090: } catch (SecurityException sex) {
091: SecurityUtil.publishErrorMessage(request,
092: SecurityResources.TOPIC_GROUPS, sex.getMessage());
093: }
094:
095: BrowserIterator iterator = new DatabaseBrowserIterator(list,
096: resultSetTitleList, resultSetTypeList, windowSize);
097: setBrowserIterator(request, iterator);
098: iterator.sort("group"); // resource bundle key
099: }
100:
101: public void doView(RenderRequest request, RenderResponse response)
102: throws PortletException, IOException {
103: String selected = (String) PortletMessaging.receive(request,
104: SecurityResources.TOPIC_GROUPS,
105: SecurityResources.MESSAGE_SELECTED);
106: if (selected != null) {
107: Context context = this .getContext(request);
108: context.put("selected", selected);
109: }
110: StatusMessage msg = (StatusMessage) PortletMessaging.consume(
111: request, SecurityResources.TOPIC_GROUPS,
112: SecurityResources.MESSAGE_STATUS);
113: if (msg != null) {
114: this .getContext(request).put("statusMsg", msg);
115: }
116:
117: String filtered = (String) PortletMessaging.receive(request,
118: SecurityResources.TOPIC_GROUPS,
119: SecurityResources.MESSAGE_FILTERED);
120: if (filtered != null) {
121: this .getContext(request).put(FILTERED, "on");
122: }
123: String refresh = (String) PortletMessaging.consume(request,
124: SecurityResources.TOPIC_GROUPS,
125: SecurityResources.MESSAGE_REFRESH);
126: if (refresh != null) {
127: this .clearBrowserIterator(request);
128: }
129:
130: ArrayList errorMessages = (ArrayList) PortletMessaging.consume(
131: request, SecurityResources.TOPIC_GROUPS,
132: SecurityResources.ERROR_MESSAGES);
133: if (errorMessages != null) {
134: this .getContext(request).put(
135: SecurityResources.ERROR_MESSAGES, errorMessages);
136: }
137:
138: super .doView(request, response);
139: }
140:
141: public void processAction(ActionRequest request,
142: ActionResponse response) throws PortletException,
143: IOException {
144: if (request.getPortletMode() == PortletMode.VIEW) {
145: String selected = request.getParameter("group");
146: if (selected != null) {
147: Group group = lookupGroup(request, selected);
148: if (group != null) {
149: PortletMessaging.publish(request,
150: SecurityResources.TOPIC_GROUPS,
151: SecurityResources.MESSAGE_SELECTED,
152: selected);
153: PortletMessaging
154: .publish(request,
155: SecurityResources.TOPIC_GROUPS,
156: SecurityResources.MESSAGE_CHANGED,
157: selected);
158: }
159: }
160: }
161:
162: String filtered = request.getParameter(FILTERED);
163: if (filtered != null) {
164: PortletMessaging.publish(request,
165: SecurityResources.TOPIC_GROUPS,
166: SecurityResources.MESSAGE_FILTERED, "on");
167: } else {
168: PortletMessaging.cancel(request,
169: SecurityResources.TOPIC_GROUPS,
170: SecurityResources.MESSAGE_FILTERED);
171: }
172:
173: super .processAction(request, response);
174:
175: }
176:
177: private Group lookupGroup(ActionRequest actionRequest,
178: String groupName) {
179: try {
180: return groupManager.getGroup(groupName);
181: } catch (SecurityException sex) {
182: SecurityUtil.publishErrorMessage(actionRequest,
183: SecurityResources.TOPIC_GROUPS, sex.getMessage());
184: return null;
185: }
186: }
187:
188: }
|