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.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              // Run the Decoration valve to get actions
89              decorationValve.invoke(requestContext, null);
90              
91              //String filter = getActionParameter(requestContext, FILTER);            
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() );  // requestContext.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             {   // build page representation with single layout
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             // Log the exception
191             log.error("exception while getting page", e);
192 
193             // Return a failure indicator
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     	{   // get layout fragment sizes
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     				// logic below is copied from org.apache.jetspeed.portlets.MultiColumnPortlet
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     					//log.info( "GetPageAction settings sizes for " + frag.getId() + " to " + sizesParamVal);
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     						//log.info( "GetPageAction defaulting sizes for " + frag.getId() + " to " + sizesParamVal);
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         {   // get portlet icon and locale specific portlet display name
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 }