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.HashMap;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.jetspeed.JetspeedActions;
27 import org.apache.jetspeed.PortalReservedParameters;
28 import org.apache.jetspeed.ajax.AjaxAction;
29 import org.apache.jetspeed.ajax.AjaxBuilder;
30 import org.apache.jetspeed.components.portletregistry.PortletRegistry;
31 import org.apache.jetspeed.decoration.DecorationValve;
32 import org.apache.jetspeed.decoration.Theme;
33 import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
34 import org.apache.jetspeed.om.page.Fragment;
35 import org.apache.jetspeed.om.page.Page;
36 import org.apache.jetspeed.page.PageManager;
37 import org.apache.jetspeed.portalsite.PortalSiteRequestContext;
38 import org.apache.jetspeed.profiler.impl.ProfilerValveImpl;
39 import org.apache.jetspeed.request.RequestContext;
40 import org.apache.pluto.om.common.Parameter;
41 import org.apache.pluto.om.common.ParameterSet;
42 import org.apache.pluto.om.portlet.PortletDefinition;
43
44 /***
45 * Get Page retrieves a page from the Page Manager store and PSML format
46 *
47 * AJAX Parameters:
48 * page = the path and name of the page ("/_user/ronaldino/goals.psml")
49 *
50 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
51 * @version $Id: $
52 */
53 public class GetPageAction
54 extends BaseGetResourceAction
55 implements AjaxAction, AjaxBuilder, Constants
56 {
57 protected Log log = LogFactory.getLog(GetPageAction.class);
58
59 private PortletRegistry registry;
60 private DecorationValve decorationValve;
61
62 public GetPageAction(String template,
63 String errorTemplate,
64 PageManager pageManager,
65 PortletActionSecurityBehavior securityBehavior,
66 PortletRegistry registry,
67 DecorationValve decorationValve)
68 {
69 super(template, errorTemplate, pageManager, securityBehavior);
70 this.registry = registry;
71 this.decorationValve = decorationValve;
72 }
73
74 public boolean run(RequestContext requestContext, Map resultMap)
75 {
76 boolean success = true;
77 String status = "success";
78 try
79 {
80 resultMap.put(ACTION, "getpage");
81 if (false == checkAccess(requestContext, JetspeedActions.VIEW))
82 {
83 resultMap.put(REASON, "Insufficient access to view page");
84 success = false;
85 return success;
86 }
87
88
89 decorationValve.invoke(requestContext, null);
90
91
92 Page page = requestContext.getPage();
93 String pageName = getActionParameter(requestContext, PAGE);
94 if (pageName != null)
95 {
96 page = retrievePage(requestContext, pageName);
97 }
98 resultMap.put(STATUS, status);
99 resultMap.put(PAGE, page);
100
101 Theme theme = (Theme)requestContext.getAttribute(PortalReservedParameters.PAGE_THEME_ATTRIBUTE);
102 String pageDecoratorName = null;
103 if ( theme != null )
104 {
105 pageDecoratorName = theme.getPageLayoutDecoration().getName();
106 }
107 else
108 {
109 pageDecoratorName = page.getDefaultDecorator( LAYOUT );
110 }
111 if ( pageDecoratorName != null )
112 resultMap.put( DEFAULT_LAYOUT, pageDecoratorName );
113
114 PortalSiteRequestContext siteRequestContext = (PortalSiteRequestContext)requestContext.getAttribute(ProfilerValveImpl.PORTAL_SITE_REQUEST_CONTEXT_ATTR_KEY);
115 if (siteRequestContext == null)
116 {
117 success = false;
118 resultMap.put(REASON, "Missing portal site request context from ProfilerValve");
119 return success;
120 }
121
122 resultMap.put(PROFILED_PATH, siteRequestContext.getPage().getPath() );
123 putSecurityInformation(resultMap, page);
124 String fragments = getActionParameter(requestContext, FRAGMENTS);
125 if (fragments == null)
126 {
127 resultMap.put(FRAGMENTS, "true");
128 }
129 else
130 {
131 if (fragments.equalsIgnoreCase("true"))
132 {
133 resultMap.put(FRAGMENTS, "true");
134 }
135 else
136 {
137 resultMap.put(FRAGMENTS, "false");
138 return success;
139 }
140 }
141
142 Map fragSizes = new HashMap();
143 Map portletIcons = new HashMap();
144
145 String singleLayoutId = getActionParameter(requestContext, LAYOUTID);
146 if ( singleLayoutId != null )
147 {
148 Fragment currentLayoutFragment = page.getFragmentById( singleLayoutId );
149 if ( currentLayoutFragment == null )
150 {
151 throw new Exception("layout id not found: " + singleLayoutId );
152 }
153 Fragment currentPortletFragment = null;
154
155 String singlePortletId = getActionParameter(requestContext, PORTLETENTITY);
156 if ( singlePortletId != null )
157 {
158 Iterator layoutChildIter = currentLayoutFragment.getFragments().iterator();
159 while ( layoutChildIter.hasNext() )
160 {
161 Fragment childFrag = (Fragment)layoutChildIter.next();
162 if ( childFrag != null )
163 {
164 if ( singlePortletId.equals( childFrag.getId() ) )
165 {
166 currentPortletFragment = childFrag;
167 break;
168 }
169 }
170 }
171 if ( currentPortletFragment == null )
172 {
173 throw new Exception("portlet id " + singlePortletId + " not found in layout " + singleLayoutId );
174 }
175 resultMap.put( "portletsingleId", currentPortletFragment.getId() );
176 }
177
178 retrieveFragmentSpecialProperties( requestContext, currentLayoutFragment, fragSizes, portletIcons );
179 resultMap.put( "layoutsingle", currentLayoutFragment );
180 }
181 else
182 {
183 retrieveFragmentSpecialProperties( requestContext, page.getRootFragment(), fragSizes, portletIcons );
184 }
185 resultMap.put( SIZES, fragSizes );
186 resultMap.put( "portletIcons", portletIcons );
187 }
188 catch (Exception e)
189 {
190
191 log.error("exception while getting page", e);
192
193
194 success = false;
195 }
196
197 return success;
198 }
199
200 protected Page retrievePage(RequestContext requestContext, String pageName)
201 throws Exception
202 {
203 if (pageName == null)
204 {
205 pageName = "/";
206 }
207 Page page = pageManager.getPage(pageName);
208 return page;
209 }
210
211
212 protected void retrieveFragmentSpecialProperties( RequestContext requestContext, Fragment frag, Map fragSizes, Map portletIcons )
213 {
214 if ( frag == null )
215 {
216 return;
217 }
218 if ( fragSizes != null && "layout".equals( frag.getType() ) )
219 {
220 String sizesVal = frag.getProperty( "sizes" );
221 if ( sizesVal == null || sizesVal.length() == 0 )
222 {
223 String layoutName = frag.getName();
224 if ( layoutName != null && layoutName.length() > 0 )
225 {
226
227 PortletDefinition portletDef = registry.getPortletDefinitionByUniqueName( layoutName );
228
229 ParameterSet paramSet = portletDef.getInitParameterSet();
230 Parameter sizesParam = paramSet.get( "sizes" );
231 String sizesParamVal = ( sizesParam == null ) ? null : sizesParam.getValue();
232 if ( sizesParamVal != null && sizesParamVal.length() > 0 )
233 {
234 fragSizes.put( frag.getId(), sizesParamVal );
235
236 }
237 else
238 {
239 Parameter colsParam = paramSet.get( "columns" );
240 String colsParamVal = ( colsParam == null ) ? null : colsParam.getValue();
241 if ( colsParamVal != null && colsParamVal.length() > 0 )
242 {
243 int cols = 0;
244 try
245 {
246 cols = Integer.parseInt( colsParamVal );
247 }
248 catch ( NumberFormatException ex )
249 {
250 }
251 if ( cols < 1 )
252 {
253 cols = 2;
254 }
255 switch (cols)
256 {
257 case 1: sizesParamVal = "100%"; break;
258 case 2: sizesParamVal = "50%,50%"; break;
259 case 3: sizesParamVal = "34%,33%,33%"; break;
260 default: sizesParamVal = "50%,50%"; break;
261 }
262 fragSizes.put( frag.getId(), sizesParamVal );
263
264 }
265 }
266 }
267 }
268 List childFragments = frag.getFragments();
269 if ( childFragments != null )
270 {
271 Iterator childFragIter = childFragments.iterator();
272 while ( childFragIter.hasNext() )
273 {
274 Fragment childFrag = (Fragment)childFragIter.next();
275 retrieveFragmentSpecialProperties( requestContext, childFrag, fragSizes, portletIcons );
276 }
277 }
278 }
279 else if ( portletIcons != null && "portlet".equals( frag.getType() ) )
280 {
281 String portletName = frag.getName();
282 if ( portletName != null && portletName.length() > 0 )
283 {
284 PortletDefinition portletDef = registry.getPortletDefinitionByUniqueName( portletName );
285
286 if ( portletDef != null && portletIcons != null )
287 {
288 ParameterSet paramSet = portletDef.getInitParameterSet();
289 Parameter iconParam = paramSet.get( "portlet-icon" );
290 String iconParamVal = ( iconParam == null ) ? null : iconParam.getValue();
291 if ( iconParamVal != null && iconParamVal.length() > 0 )
292 {
293 portletIcons.put( frag.getId(), iconParamVal );
294 }
295 }
296 else if ( portletDef == null )
297 {
298 log.error( "GetPageAction could not obtain PortletDefinition for portlet " + portletName );
299 }
300 }
301 }
302 }
303
304 }