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