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.aggregator.impl;
18  
19  import java.io.IOException;
20  import java.util.Iterator;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.jetspeed.PortalReservedParameters;
25  import org.apache.jetspeed.aggregator.FailedToRenderFragmentException;
26  import org.apache.jetspeed.aggregator.PageAggregator;
27  import org.apache.jetspeed.aggregator.PortletRenderer;
28  import org.apache.jetspeed.container.state.NavigationalState;
29  import org.apache.jetspeed.exception.JetspeedException;
30  import org.apache.jetspeed.om.page.ContentFragment;
31  import org.apache.jetspeed.om.page.ContentPage;
32  import org.apache.jetspeed.request.RequestContext;
33  import org.apache.pluto.om.window.PortletWindow;
34  
35  /***
36   * ContentPageAggregator builds the content required to render a page of portlets.
37   * 
38   * @author <a href="mailto:raphael@apache.org">Rapha?l Luta </a>
39   * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
40   * @version $Id: PageAggregatorImpl.java 516448 2007-03-09 16:25:47Z ate $
41   */
42  public class PageAggregatorImpl implements PageAggregator
43  {
44      private final static Log log = LogFactory.getLog(PageAggregatorImpl.class);
45      private PortletRenderer renderer;
46  
47      public PageAggregatorImpl( PortletRenderer renderer)
48      {
49          this.renderer = renderer;
50      }
51  
52      /***
53       * Builds the portlet set defined in the context into a portlet tree.
54       * 
55       * @return Unique Portlet Entity ID
56       */
57      public void build( RequestContext context ) throws JetspeedException, IOException
58      {
59          ContentPage page = context.getPage();
60          if (null == page)
61          {
62              throw new JetspeedException("Failed to find PSML Pin ContentPageAggregator.build");
63          }
64          ContentFragment root = page.getRootContentFragment();
65          if (root == null)
66          {
67              throw new JetspeedException("No root ContentFragment found in ContentPage");
68          }
69          // handle maximized state
70          NavigationalState nav = context.getPortalURL().getNavigationalState();
71          PortletWindow window = nav.getMaximizedWindow();
72          if (null != window)
73          {
74              renderMaximizedWindow(context, page, root, window);
75          }
76          else
77          {
78              aggregateAndRender(root, context, page);
79          }        
80          context.getResponse().getWriter().write(root.getRenderedContent());
81          if (null != window)
82          {
83              context.getRequest().removeAttribute(PortalReservedParameters.MAXIMIZED_FRAGMENT_ATTRIBUTE);
84              context.getRequest().removeAttribute(PortalReservedParameters.MAXIMIZED_LAYOUT_ATTRIBUTE);
85          }
86      }
87  
88      /***
89       * <p>
90       * renderMaximizedWindow
91       * </p>
92       * 
93       * @param context
94       * @param page
95       * @param layoutContentFragment
96       * @param defaultPortletDecorator
97       * @param dispatcher
98       * @param window
99       * @throws FailedToRenderContentFragmentException
100      */
101     protected void renderMaximizedWindow( RequestContext context, ContentPage page, ContentFragment layoutContentFragment,
102             PortletWindow window ) throws FailedToRenderFragmentException
103     {
104         ContentFragment maxedContentFragment = page.getContentFragmentById(window.getId().toString());
105         if (maxedContentFragment != null)
106         {
107             context.getRequest().setAttribute(PortalReservedParameters.MAXIMIZED_FRAGMENT_ATTRIBUTE, maxedContentFragment);
108             context.getRequest().setAttribute(PortalReservedParameters.FRAGMENT_ATTRIBUTE, maxedContentFragment);
109             context.getRequest().setAttribute(PortalReservedParameters.MAXIMIZED_LAYOUT_ATTRIBUTE, page.getRootContentFragment());
110 
111             try
112             {
113                 renderer.renderNow(maxedContentFragment, context);
114                 renderer.renderNow(layoutContentFragment, context);              
115                 
116             }
117             catch (Exception e)
118             {
119                 log.error(e.getMessage(), e);
120                 maxedContentFragment.overrideRenderedContent("Sorry, but we were unable access the requested portlet.  Send the following message to your portal admin:  "+  e.getMessage());
121             }
122         }
123         else
124         {
125             String message = "Maximized fragment not found."; 
126             log.error(message);
127             if (maxedContentFragment != null)
128                 maxedContentFragment.overrideRenderedContent("Sorry, but we were unable access the requested portlet.  Send the following message to your portal admin:  "+  message);            
129         }
130     }
131 
132     protected void aggregateAndRender( ContentFragment f, RequestContext context, ContentPage page )
133             throws FailedToRenderFragmentException
134     {
135         if (f.getContentFragments() != null && f.getContentFragments().size() > 0)
136         {
137             Iterator children = f.getContentFragments().iterator();
138             while (children.hasNext())
139             {
140                 ContentFragment child = (ContentFragment) children.next();
141                 if (!"hidden".equals(f.getState()))
142                 {
143                     aggregateAndRender(child, context, page);
144                 }
145             }
146         }
147         renderer.renderNow(f, context);
148     }
149 }