View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.layout.impl;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.Comparator;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Locale;
26  import java.util.Map;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.jetspeed.JetspeedActions;
31  import org.apache.jetspeed.ajax.AjaxAction;
32  import org.apache.jetspeed.ajax.AjaxBuilder;
33  import org.apache.jetspeed.components.portletregistry.PortletRegistry;
34  import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
35  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
36  import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
37  import org.apache.jetspeed.page.PageManager;
38  import org.apache.jetspeed.request.RequestContext;
39  import org.apache.jetspeed.search.ParsedObject;
40  import org.apache.jetspeed.search.SearchEngine;
41  import org.apache.jetspeed.security.SecurityAccessController;
42  import org.apache.pluto.om.common.Parameter;
43  
44  /***
45   * Get Portlets retrieves the portlet list available to the current subject
46   *
47   * AJAX Parameters: 
48   *    filter = (optional)filter to lookup portlets using fulltext search
49   *    
50   * @author <a>David Gurney</a>
51   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
52   * @version $Id: $
53   */
54  public class GetPortletsAction 
55      extends BasePortletAction 
56      implements AjaxAction, AjaxBuilder, Constants, Comparator
57  {
58      protected static final Log log = LogFactory.getLog(GetPortletsAction.class);
59      private PortletRegistry registry = null;
60      private SearchEngine searchEngine = null;
61      private SecurityAccessController securityAccessController;
62      
63      public final static String PORTLET_ICON = "portlet-icon";
64      
65      public GetPortletsAction(String template, String errorTemplate)
66      {
67          this(template, errorTemplate, null, null, null, null, null);
68      }
69      
70      public GetPortletsAction(String template, 
71                               String errorTemplate,
72                               PageManager pageManager,
73                               PortletRegistry registry,
74                               SearchEngine searchEngine,
75                               SecurityAccessController securityAccessController,
76                               PortletActionSecurityBehavior securityBehavior)
77      {
78          super(template, errorTemplate, pageManager, securityBehavior);
79          this.registry = registry;
80          this.searchEngine = searchEngine;
81          this.securityAccessController = securityAccessController;
82      }
83  
84      public boolean run(RequestContext requestContext, Map resultMap)
85      {
86          boolean success = true;
87          String status = "success";
88          try
89          {
90              resultMap.put(ACTION, "getportlets");
91              if (false == checkAccess(requestContext, JetspeedActions.VIEW))
92              {
93  //                if (!createNewPageOnEdit(requestContext))
94  //                {
95                      success = false;
96                      resultMap.put(REASON, "Insufficient access to edit page");
97                      return success;
98  //                }
99  //                status = "refresh";
100             }            
101             String filter = getActionParameter(requestContext, FILTER);                                    
102             List portlets = retrievePortlets(requestContext, filter);            
103             resultMap.put(STATUS, status);
104             resultMap.put(PORTLETS, portlets);
105         } 
106         catch (Exception e)
107         {
108             // Log the exception
109             log.error("exception while getting portlet info", e);
110 
111             // Return a failure indicator
112             success = false;
113         }
114 
115         return success;
116 	}
117     
118     public List retrievePortlets(RequestContext requestContext, String filter)
119     {
120         Iterator portlets = null;
121         List list = new ArrayList();
122         Locale locale = requestContext.getLocale();
123         
124         if (filter == null)
125             portlets = registry.getAllPortletDefinitions().iterator();
126         else
127             portlets = searchEngine.search(filter).getResults().iterator();
128         
129         while (portlets.hasNext())
130         {
131             PortletDefinitionComposite portlet = null;
132             if (filter == null)
133                 portlet = (PortletDefinitionComposite)portlets.next();
134             else
135                 portlet = this.getPortletFromParsedObject((ParsedObject)portlets.next());
136             
137             if (portlet == null)
138                 continue;
139             
140             // Do not display Jetspeed Layout Applications
141             MutablePortletApplication pa = (MutablePortletApplication)portlet.getPortletApplicationDefinition();
142             if (pa.isLayoutApplication())
143                 continue;
144                  
145             // SECURITY filtering
146             String uniqueName = pa.getName() + "::" + portlet.getName();
147             if (securityAccessController.checkPortletAccess(portlet, JetspeedActions.MASK_VIEW))
148             {
149                 Parameter param = portlet.getInitParameterSet().get(PORTLET_ICON);
150                 String image;
151                 if (param != null)
152                 {
153                     //String relativeImagePath = param.getValue();
154                     //String context = muta.getWebApplicationDefinition().getContextRoot();
155                     // Have to use a supported icon in jetspeed, otherwise image can be out of skew
156                     image = "images/portlets/" + param.getValue();
157                 }
158                 else
159                 {                                        
160                     image = "images/portlets/applications-internet.png";
161                 }                
162                 list.add(new PortletInfo(uniqueName, portlet.getDisplayNameText(locale), portlet.getDescriptionText(locale), image));
163             }
164         }            
165         Collections.sort(list, this);
166         return list;
167     }
168     
169     protected PortletDefinitionComposite getPortletFromParsedObject(ParsedObject po)
170     {
171         boolean found = false;
172         String name = "";
173         Map fields = po.getFields();
174         if(fields != null)
175         {
176             Object id = fields.get("ID");
177     
178             if(id != null)
179             {
180                 if(id instanceof Collection)
181                 {
182                     Collection coll = (Collection)id;
183                     name = (String) coll.iterator().next();
184                 }
185                 else
186                 {
187                     name = (String)id;
188                 }
189             }
190             
191             if(po.getType().equals("portlet"))
192             {
193                 Object pa = fields.get("portlet_application");
194                 String paName = "";
195                 if(pa != null)
196                 {
197                     if(id instanceof Collection)
198                     {
199                         Collection coll = (Collection)pa;
200                         paName = (String) coll.iterator().next();
201                     }
202                     else
203                     {
204                         paName = (String)pa;
205                     }
206                 }
207                 name = paName + "::" + name;
208                 found = true;
209             }
210         }
211         if (found == false)
212             return null;
213         
214         return registry.getPortletDefinitionByUniqueName(name);
215     }
216     
217     public int compare(Object obj1, Object obj2)
218     {
219         PortletInfo portlet1 = (PortletInfo)obj1;
220         PortletInfo portlet2 = (PortletInfo)obj2;
221         String name1 = portlet1.getName();
222         String name2 = portlet2.getName();
223         name1 = (name1 == null) ? "unknown" : name1;
224         name2 = (name2 == null) ? "unknown" : name2;
225         return name1.compareTo(name2);
226     }
227 }