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.layout.impl;
018:
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.Collections;
022: import java.util.Comparator;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Locale;
026: import java.util.Map;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.jetspeed.JetspeedActions;
031: import org.apache.jetspeed.ajax.AjaxAction;
032: import org.apache.jetspeed.ajax.AjaxBuilder;
033: import org.apache.jetspeed.components.portletregistry.PortletRegistry;
034: import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
035: import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
036: import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
037: import org.apache.jetspeed.page.PageManager;
038: import org.apache.jetspeed.request.RequestContext;
039: import org.apache.jetspeed.search.ParsedObject;
040: import org.apache.jetspeed.search.SearchEngine;
041: import org.apache.jetspeed.security.SecurityAccessController;
042: import org.apache.pluto.om.common.Parameter;
043:
044: /**
045: * Get Portlets retrieves the portlet list available to the current subject
046: *
047: * AJAX Parameters:
048: * filter = (optional)filter to lookup portlets using fulltext search
049: *
050: * @author <a>David Gurney</a>
051: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
052: * @version $Id: $
053: */
054: public class GetPortletsAction extends BasePortletAction implements
055: AjaxAction, AjaxBuilder, Constants, Comparator {
056: protected static final Log log = LogFactory
057: .getLog(GetPortletsAction.class);
058: private PortletRegistry registry = null;
059: private SearchEngine searchEngine = null;
060: private SecurityAccessController securityAccessController;
061:
062: public final static String PORTLET_ICON = "portlet-icon";
063:
064: public GetPortletsAction(String template, String errorTemplate) {
065: this (template, errorTemplate, null, null, null, null, null);
066: }
067:
068: public GetPortletsAction(String template, String errorTemplate,
069: PageManager pageManager, PortletRegistry registry,
070: SearchEngine searchEngine,
071: SecurityAccessController securityAccessController,
072: PortletActionSecurityBehavior securityBehavior) {
073: super (template, errorTemplate, pageManager, securityBehavior);
074: this .registry = registry;
075: this .searchEngine = searchEngine;
076: this .securityAccessController = securityAccessController;
077: }
078:
079: public boolean run(RequestContext requestContext, Map resultMap) {
080: boolean success = true;
081: String status = "success";
082: try {
083: resultMap.put(ACTION, "getportlets");
084: if (false == checkAccess(requestContext,
085: JetspeedActions.VIEW)) {
086: // if (!createNewPageOnEdit(requestContext))
087: // {
088: success = false;
089: resultMap.put(REASON,
090: "Insufficient access to edit page");
091: return success;
092: // }
093: // status = "refresh";
094: }
095: String filter = getActionParameter(requestContext, FILTER);
096: List portlets = retrievePortlets(requestContext, filter);
097: resultMap.put(STATUS, status);
098: resultMap.put(PORTLETS, portlets);
099: } catch (Exception e) {
100: // Log the exception
101: log.error("exception while getting portlet info", e);
102:
103: // Return a failure indicator
104: success = false;
105: }
106:
107: return success;
108: }
109:
110: public List retrievePortlets(RequestContext requestContext,
111: String filter) {
112: Iterator portlets = null;
113: List list = new ArrayList();
114: Locale locale = requestContext.getLocale();
115:
116: if (filter == null)
117: portlets = registry.getAllPortletDefinitions().iterator();
118: else
119: portlets = searchEngine.search(filter).getResults()
120: .iterator();
121:
122: while (portlets.hasNext()) {
123: PortletDefinitionComposite portlet = null;
124: if (filter == null)
125: portlet = (PortletDefinitionComposite) portlets.next();
126: else
127: portlet = this
128: .getPortletFromParsedObject((ParsedObject) portlets
129: .next());
130:
131: if (portlet == null)
132: continue;
133:
134: // Do not display Jetspeed Layout Applications
135: MutablePortletApplication pa = (MutablePortletApplication) portlet
136: .getPortletApplicationDefinition();
137: if (pa.isLayoutApplication())
138: continue;
139:
140: // SECURITY filtering
141: String uniqueName = pa.getName() + "::" + portlet.getName();
142: if (securityAccessController.checkPortletAccess(portlet,
143: JetspeedActions.MASK_VIEW)) {
144: Parameter param = portlet.getInitParameterSet().get(
145: PORTLET_ICON);
146: String image;
147: if (param != null) {
148: //String relativeImagePath = param.getValue();
149: //String context = muta.getWebApplicationDefinition().getContextRoot();
150: // Have to use a supported icon in jetspeed, otherwise image can be out of skew
151: image = "images/portlets/" + param.getValue();
152: } else {
153: image = "images/portlets/applications-internet.png";
154: }
155: list.add(new PortletInfo(uniqueName, portlet
156: .getDisplayNameText(locale), portlet
157: .getDescriptionText(locale), image));
158: }
159: }
160: Collections.sort(list, this );
161: return list;
162: }
163:
164: protected PortletDefinitionComposite getPortletFromParsedObject(
165: ParsedObject po) {
166: boolean found = false;
167: String name = "";
168: Map fields = po.getFields();
169: if (fields != null) {
170: Object id = fields.get("ID");
171:
172: if (id != null) {
173: if (id instanceof Collection) {
174: Collection coll = (Collection) id;
175: name = (String) coll.iterator().next();
176: } else {
177: name = (String) id;
178: }
179: }
180:
181: if (po.getType().equals("portlet")) {
182: Object pa = fields.get("portlet_application");
183: String paName = "";
184: if (pa != null) {
185: if (id instanceof Collection) {
186: Collection coll = (Collection) pa;
187: paName = (String) coll.iterator().next();
188: } else {
189: paName = (String) pa;
190: }
191: }
192: name = paName + "::" + name;
193: found = true;
194: }
195: }
196: if (found == false)
197: return null;
198:
199: return registry.getPortletDefinitionByUniqueName(name);
200: }
201:
202: public int compare(Object obj1, Object obj2) {
203: PortletInfo portlet1 = (PortletInfo) obj1;
204: PortletInfo portlet2 = (PortletInfo) obj2;
205: String name1 = portlet1.getName();
206: String name2 = portlet2.getName();
207: name1 = (name1 == null) ? "unknown" : name1;
208: name2 = (name2 == null) ? "unknown" : name2;
209: return name1.compareTo(name2);
210: }
211: }
|