001    package org.apache.turbine.modules.layouts;
002    
003    
004    /*
005     * Licensed to the Apache Software Foundation (ASF) under one
006     * or more contributor license agreements.  See the NOTICE file
007     * distributed with this work for additional information
008     * regarding copyright ownership.  The ASF licenses this file
009     * to you under the Apache License, Version 2.0 (the
010     * "License"); you may not use this file except in compliance
011     * with the License.  You may obtain a copy of the License at
012     *
013     *   http://www.apache.org/licenses/LICENSE-2.0
014     *
015     * Unless required by applicable law or agreed to in writing,
016     * software distributed under the License is distributed on an
017     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018     * KIND, either express or implied.  See the License for the
019     * specific language governing permissions and limitations
020     * under the License.
021     */
022    
023    
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    import org.apache.turbine.TurbineConstants;
027    import org.apache.turbine.modules.Layout;
028    import org.apache.turbine.pipeline.PipelineData;
029    import org.apache.turbine.services.velocity.TurbineVelocity;
030    import org.apache.turbine.util.RunData;
031    import org.apache.turbine.util.template.TemplateNavigation;
032    import org.apache.turbine.util.template.TemplateScreen;
033    import org.apache.velocity.context.Context;
034    
035    /**
036     * This Layout module allows Velocity templates
037     * to be used as layouts. It will stream directly the output of
038     * the layout and navigation templates to the output writer without
039     * using a screen. Use this if you have a large page to output
040     * and won't buffer it in the memory.
041     *
042     * @author <a href="mailto:raphael@apache.org">Raphaƫl Luta</a>
043     * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
044     * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
045     * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
046     * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
047     * @version $Id: VelocityDirectLayout.java 1071044 2011-02-15 20:47:31Z tv $
048     */
049    public class VelocityDirectLayout
050        extends Layout
051    {
052        /** Logging */
053        private static Log log = LogFactory.getLog(VelocityDirectLayout.class);
054    
055        /** The prefix for lookup up layout pages */
056        private String prefix = Layout.PREFIX + "/";
057    
058        /**
059         * Method called by LayoutLoader.
060         *
061         * @deprecated Use the PipelineData version instead
062         * @param data Turbine information.
063         * @exception Exception a generic exception.
064         */
065        @Deprecated
066        @Override
067        public void doBuild(RunData data)
068            throws Exception
069        {
070            // Get the context needed by Velocity.
071            Context context = TurbineVelocity.getContext(data);
072    
073            // variable for the screen in the layout template
074            context.put(TurbineConstants.SCREEN_PLACEHOLDER,
075                        new TemplateScreen(data));
076    
077            // variable to reference the navigation screen in the layout template
078            context.put(TurbineConstants.NAVIGATION_PLACEHOLDER,
079                        new TemplateNavigation(data));
080    
081            // Grab the layout template set in the VelocityPage.
082            // If null, then use the default layout template
083            // (done by the TemplateInfo object)
084            String templateName = data.getTemplateInfo().getLayoutTemplate();
085    
086            // Set the locale and content type
087            data.getResponse().setLocale(data.getLocale());
088            data.getResponse().setContentType(data.getContentType());
089    
090            log.debug("Now trying to render layout " + templateName);
091    
092            // Finally, generate the layout template and send it to the browser
093            TurbineVelocity.handleRequest(context,
094                    prefix + templateName, data.getResponse().getOutputStream());
095        }
096    
097        /**
098         * Method called by LayoutLoader.
099         *
100         *
101         * @param data PipelineData
102         * @throws Exception generic exception
103         */
104        @Override
105        public void doBuild(PipelineData pipelineData)
106            throws Exception
107        {
108            RunData data = getRunData(pipelineData);
109            // Get the context needed by Velocity.
110            Context context = TurbineVelocity.getContext(pipelineData);
111    
112            // variable for the screen in the layout template
113            context.put(TurbineConstants.SCREEN_PLACEHOLDER,
114                        new TemplateScreen(data));
115    
116            // variable to reference the navigation screen in the layout template
117            context.put(TurbineConstants.NAVIGATION_PLACEHOLDER,
118                        new TemplateNavigation(data));
119    
120            // Grab the layout template set in the VelocityPage.
121            // If null, then use the default layout template
122            // (done by the TemplateInfo object)
123            String templateName = data.getTemplateInfo().getLayoutTemplate();
124    
125            // Set the locale and content type
126            data.getResponse().setLocale(data.getLocale());
127            data.getResponse().setContentType(data.getContentType());
128    
129            log.debug("Now trying to render layout " + templateName);
130    
131            // Finally, generate the layout template and send it to the browser
132            TurbineVelocity.handleRequest(context,
133                    prefix + templateName, data.getResponse().getOutputStream());
134        }
135    
136    
137    }