1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
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 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
public class GetPortletsAction |
55 |
|
extends BasePortletAction |
56 |
|
implements AjaxAction, AjaxBuilder, Constants, Comparator |
57 |
|
{ |
58 |
0 |
protected static final Log log = LogFactory.getLog(GetPortletsAction.class); |
59 |
0 |
private PortletRegistry registry = null; |
60 |
0 |
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 |
0 |
this(template, errorTemplate, null, class="keyword">null, class="keyword">null, class="keyword">null, class="keyword">null); |
68 |
0 |
} |
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 |
0 |
super(template, errorTemplate, pageManager, securityBehavior); |
79 |
0 |
this.registry = registry; |
80 |
0 |
this.searchEngine = searchEngine; |
81 |
0 |
this.securityAccessController = securityAccessController; |
82 |
0 |
} |
83 |
|
|
84 |
|
public boolean run(RequestContext requestContext, Map resultMap) |
85 |
|
{ |
86 |
0 |
boolean success = true; |
87 |
0 |
String status = "success"; |
88 |
|
try |
89 |
|
{ |
90 |
0 |
resultMap.put(ACTION, "getportlets"); |
91 |
0 |
if (false == checkAccess(requestContext, JetspeedActions.VIEW)) |
92 |
|
{ |
93 |
|
|
94 |
|
|
95 |
0 |
success = false; |
96 |
0 |
resultMap.put(REASON, "Insufficient access to edit page"); |
97 |
0 |
return success; |
98 |
|
|
99 |
|
|
100 |
|
} |
101 |
0 |
String filter = getActionParameter(requestContext, FILTER); |
102 |
0 |
List portlets = retrievePortlets(requestContext, filter); |
103 |
0 |
resultMap.put(STATUS, status); |
104 |
0 |
resultMap.put(PORTLETS, portlets); |
105 |
|
} |
106 |
0 |
catch (Exception e) |
107 |
|
{ |
108 |
|
|
109 |
0 |
log.error("exception while getting portlet info", e); |
110 |
|
|
111 |
|
|
112 |
0 |
success = false; |
113 |
0 |
} |
114 |
|
|
115 |
0 |
return success; |
116 |
|
} |
117 |
|
|
118 |
|
public List retrievePortlets(RequestContext requestContext, String filter) |
119 |
|
{ |
120 |
0 |
Iterator portlets = null; |
121 |
0 |
List list = new ArrayList(); |
122 |
0 |
Locale locale = requestContext.getLocale(); |
123 |
|
|
124 |
0 |
if (filter == null) |
125 |
0 |
portlets = registry.getAllPortletDefinitions().iterator(); |
126 |
|
else |
127 |
0 |
portlets = searchEngine.search(filter).getResults().iterator(); |
128 |
|
|
129 |
0 |
while (portlets.hasNext()) |
130 |
|
{ |
131 |
0 |
PortletDefinitionComposite portlet = null; |
132 |
0 |
if (filter == null) |
133 |
0 |
portlet = (PortletDefinitionComposite)portlets.next(); |
134 |
|
else |
135 |
0 |
portlet = this.getPortletFromParsedObject((ParsedObject)portlets.next()); |
136 |
|
|
137 |
0 |
if (portlet == null) |
138 |
0 |
continue; |
139 |
|
|
140 |
|
|
141 |
0 |
MutablePortletApplication pa = (MutablePortletApplication)portlet.getPortletApplicationDefinition(); |
142 |
0 |
if (pa.isLayoutApplication()) |
143 |
0 |
continue; |
144 |
|
|
145 |
|
|
146 |
0 |
String uniqueName = pa.getName() + "::" + portlet.getName(); |
147 |
0 |
if (securityAccessController.checkPortletAccess(portlet, JetspeedActions.MASK_VIEW)) |
148 |
|
{ |
149 |
0 |
Parameter param = portlet.getInitParameterSet().get(PORTLET_ICON); |
150 |
|
String image; |
151 |
0 |
if (param != null) |
152 |
|
{ |
153 |
|
|
154 |
|
|
155 |
|
|
156 |
0 |
image = "images/portlets/" + param.getValue(); |
157 |
|
} |
158 |
|
else |
159 |
|
{ |
160 |
0 |
image = "images/portlets/applications-internet.png"; |
161 |
|
} |
162 |
0 |
list.add(new PortletInfo(uniqueName, portlet.getDisplayNameText(locale), portlet.getDescriptionText(locale), image)); |
163 |
|
} |
164 |
0 |
} |
165 |
0 |
Collections.sort(list, this); |
166 |
0 |
return list; |
167 |
|
} |
168 |
|
|
169 |
|
protected PortletDefinitionComposite getPortletFromParsedObject(ParsedObject po) |
170 |
|
{ |
171 |
0 |
boolean found = false; |
172 |
0 |
String name = ""; |
173 |
0 |
Map fields = po.getFields(); |
174 |
0 |
if(fields != null) |
175 |
|
{ |
176 |
0 |
Object id = fields.get("ID"); |
177 |
|
|
178 |
0 |
if(id != null) |
179 |
|
{ |
180 |
0 |
if(id instanceof Collection) |
181 |
|
{ |
182 |
0 |
Collection coll = (Collection)id; |
183 |
0 |
name = (String) coll.iterator().next(); |
184 |
0 |
} |
185 |
|
else |
186 |
|
{ |
187 |
0 |
name = (String)id; |
188 |
|
} |
189 |
|
} |
190 |
|
|
191 |
0 |
if(po.getType().equals("portlet")) |
192 |
|
{ |
193 |
0 |
Object pa = fields.get("portlet_application"); |
194 |
0 |
String paName = ""; |
195 |
0 |
if(pa != null) |
196 |
|
{ |
197 |
0 |
if(id instanceof Collection) |
198 |
|
{ |
199 |
0 |
Collection coll = (Collection)pa; |
200 |
0 |
paName = (String) coll.iterator().next(); |
201 |
0 |
} |
202 |
|
else |
203 |
|
{ |
204 |
0 |
paName = (String)pa; |
205 |
|
} |
206 |
|
} |
207 |
0 |
name = paName + "::" + name; |
208 |
0 |
found = true; |
209 |
|
} |
210 |
|
} |
211 |
0 |
if (found == false) |
212 |
0 |
return null; |
213 |
|
|
214 |
0 |
return registry.getPortletDefinitionByUniqueName(name); |
215 |
|
} |
216 |
|
|
217 |
|
public int compare(Object obj1, Object obj2) |
218 |
|
{ |
219 |
0 |
PortletInfo portlet1 = (PortletInfo)obj1; |
220 |
0 |
PortletInfo portlet2 = (PortletInfo)obj2; |
221 |
0 |
String name1 = portlet1.getName(); |
222 |
0 |
String name2 = portlet2.getName(); |
223 |
0 |
name1 = (name1 == null) ? "unknown" : name1; |
224 |
0 |
name2 = (name2 == null) ? "unknown" : name2; |
225 |
0 |
return name1.compareTo(name2); |
226 |
|
} |
227 |
|
} |