001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.aggregator.impl;
018:
019: import java.io.IOException;
020: import java.util.Iterator;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.apache.jetspeed.PortalReservedParameters;
025: import org.apache.jetspeed.aggregator.FailedToRenderFragmentException;
026: import org.apache.jetspeed.aggregator.PageAggregator;
027: import org.apache.jetspeed.aggregator.PortletRenderer;
028: import org.apache.jetspeed.container.state.NavigationalState;
029: import org.apache.jetspeed.exception.JetspeedException;
030: import org.apache.jetspeed.om.page.ContentFragment;
031: import org.apache.jetspeed.om.page.ContentPage;
032: import org.apache.jetspeed.request.RequestContext;
033: import org.apache.pluto.om.window.PortletWindow;
034:
035: /**
036: * ContentPageAggregator builds the content required to render a page of portlets.
037: *
038: * @author <a href="mailto:raphael@apache.org">Rapha�l Luta </a>
039: * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
040: * @version $Id: PageAggregatorImpl.java 516448 2007-03-09 16:25:47Z ate $
041: */
042: public class PageAggregatorImpl implements PageAggregator {
043: private final static Log log = LogFactory
044: .getLog(PageAggregatorImpl.class);
045: private PortletRenderer renderer;
046:
047: public PageAggregatorImpl(PortletRenderer renderer) {
048: this .renderer = renderer;
049: }
050:
051: /**
052: * Builds the portlet set defined in the context into a portlet tree.
053: *
054: * @return Unique Portlet Entity ID
055: */
056: public void build(RequestContext context) throws JetspeedException,
057: IOException {
058: ContentPage page = context.getPage();
059: if (null == page) {
060: throw new JetspeedException(
061: "Failed to find PSML Pin ContentPageAggregator.build");
062: }
063: ContentFragment root = page.getRootContentFragment();
064: if (root == null) {
065: throw new JetspeedException(
066: "No root ContentFragment found in ContentPage");
067: }
068: // handle maximized state
069: NavigationalState nav = context.getPortalURL()
070: .getNavigationalState();
071: PortletWindow window = nav.getMaximizedWindow();
072: if (null != window) {
073: renderMaximizedWindow(context, page, root, window);
074: } else {
075: aggregateAndRender(root, context, page);
076: }
077: context.getResponse().getWriter().write(
078: root.getRenderedContent());
079: if (null != window) {
080: context
081: .getRequest()
082: .removeAttribute(
083: PortalReservedParameters.MAXIMIZED_FRAGMENT_ATTRIBUTE);
084: context
085: .getRequest()
086: .removeAttribute(
087: PortalReservedParameters.MAXIMIZED_LAYOUT_ATTRIBUTE);
088: }
089: }
090:
091: /**
092: * <p>
093: * renderMaximizedWindow
094: * </p>
095: *
096: * @param context
097: * @param page
098: * @param layoutContentFragment
099: * @param defaultPortletDecorator
100: * @param dispatcher
101: * @param window
102: * @throws FailedToRenderContentFragmentException
103: */
104: protected void renderMaximizedWindow(RequestContext context,
105: ContentPage page, ContentFragment layoutContentFragment,
106: PortletWindow window)
107: throws FailedToRenderFragmentException {
108: ContentFragment maxedContentFragment = page
109: .getContentFragmentById(window.getId().toString());
110: if (maxedContentFragment != null) {
111: context
112: .getRequest()
113: .setAttribute(
114: PortalReservedParameters.MAXIMIZED_FRAGMENT_ATTRIBUTE,
115: maxedContentFragment);
116: context.getRequest().setAttribute(
117: PortalReservedParameters.FRAGMENT_ATTRIBUTE,
118: maxedContentFragment);
119: context
120: .getRequest()
121: .setAttribute(
122: PortalReservedParameters.MAXIMIZED_LAYOUT_ATTRIBUTE,
123: page.getRootContentFragment());
124:
125: try {
126: renderer.renderNow(maxedContentFragment, context);
127: renderer.renderNow(layoutContentFragment, context);
128:
129: } catch (Exception e) {
130: log.error(e.getMessage(), e);
131: maxedContentFragment
132: .overrideRenderedContent("Sorry, but we were unable access the requested portlet. Send the following message to your portal admin: "
133: + e.getMessage());
134: }
135: } else {
136: String message = "Maximized fragment not found.";
137: log.error(message);
138: if (maxedContentFragment != null)
139: maxedContentFragment
140: .overrideRenderedContent("Sorry, but we were unable access the requested portlet. Send the following message to your portal admin: "
141: + message);
142: }
143: }
144:
145: protected void aggregateAndRender(ContentFragment f,
146: RequestContext context, ContentPage page)
147: throws FailedToRenderFragmentException {
148: if (f.getContentFragments() != null
149: && f.getContentFragments().size() > 0) {
150: Iterator children = f.getContentFragments().iterator();
151: while (children.hasNext()) {
152: ContentFragment child = (ContentFragment) children
153: .next();
154: if (!"hidden".equals(f.getState())) {
155: aggregateAndRender(child, context, page);
156: }
157: }
158: }
159: renderer.renderNow(f, context);
160: }
161: }
|