001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.portlet;
016    
017    import java.io.IOException;
018    import java.io.PrintWriter;
019    import java.util.Date;
020    
021    import org.apache.tapestry.IMarkupWriter;
022    import org.apache.tapestry.IPage;
023    import org.apache.tapestry.IRequestCycle;
024    import org.apache.tapestry.Tapestry;
025    import org.apache.tapestry.TapestryUtils;
026    import org.apache.tapestry.asset.AssetFactory;
027    import org.apache.tapestry.markup.MarkupWriterSource;
028    import org.apache.tapestry.util.ContentType;
029    import org.apache.tapestry.util.PageRenderSupportImpl;
030    import org.apache.tapestry.web.WebResponse;
031    
032    /**
033     * The guts of rendering a page as a portlet response; used by
034     * {@link org.apache.tapestry.portlet.RenderService} and
035     * {@link org.apache.tapestry.portlet.PortletHomeService}.
036     * 
037     * @author Howard M. Lewis Ship
038     * @since 4.0
039     */
040    public class PortletRendererImpl implements PortletRenderer
041    {
042        private WebResponse _response;
043    
044        private MarkupWriterSource _markupWriterSource;
045    
046        private AssetFactory _assetFactory;
047    
048        private String _applicationId;
049    
050        public void renderPage(IRequestCycle cycle, String pageName) throws IOException
051        {
052            cycle.activate(pageName);
053    
054            IPage page = cycle.getPage();
055    
056            ContentType contentType = page.getResponseContentType();
057    
058            PrintWriter printWriter = _response.getPrintWriter(contentType);
059    
060            IMarkupWriter writer = _markupWriterSource.newMarkupWriter(printWriter, contentType);
061    
062            String namespace = _response.getNamespace();
063    
064            PageRenderSupportImpl support = new PageRenderSupportImpl(_assetFactory, namespace, null);
065    
066            TapestryUtils.storePageRenderSupport(cycle, support);
067    
068            IMarkupWriter nested = writer.getNestedWriter();
069    
070            cycle.renderPage(nested);
071    
072            String id = "Tapestry Portlet " + _applicationId + " " + namespace;
073    
074            writer.comment("BEGIN " + id);
075            writer.comment("Page: " + page.getPageName());
076            writer.comment("Generated: " + new Date());
077            writer.comment("Framework version: " + Tapestry.VERSION);
078    
079            support.writeBodyScript(writer, cycle);
080    
081            nested.close();
082    
083            support.writeInitializationScript(writer);
084    
085            writer.comment("END " + id);
086    
087            writer.close();
088    
089            // TODO: Trap errors and do some error reporting here?
090        }
091    
092        public void setMarkupWriterSource(MarkupWriterSource markupWriterSource)
093        {
094            _markupWriterSource = markupWriterSource;
095        }
096    
097        public void setResponse(WebResponse response)
098        {
099            _response = response;
100        }
101    
102        public void setAssetFactory(AssetFactory assetFactory)
103        {
104            _assetFactory = assetFactory;
105        }
106    
107        public void setApplicationId(String applicationId)
108        {
109            _applicationId = applicationId;
110        }
111    }