001package org.apache.turbine.modules.layouts;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.apache.turbine.TurbineConstants;
025import org.apache.turbine.annotation.TurbineService;
026import org.apache.turbine.modules.Layout;
027import org.apache.turbine.modules.pages.DefaultPage;
028import org.apache.turbine.pipeline.PipelineData;
029import org.apache.turbine.services.velocity.VelocityService;
030import org.apache.turbine.util.RunData;
031import org.apache.turbine.util.template.TemplateNavigation;
032import org.apache.turbine.util.template.TemplateScreen;
033import org.apache.velocity.context.Context;
034
035/**
036 * This Layout module is Turbine 2.3.3 VelocityDirectLayout (same package)
037 * with methods added for {@link PipelineData}. It is used in Jetspeed-1 portal.
038 *
039 * By using this layout any view write will immediately call the provided print writer {@link RunData#getOut()} and
040 * the HTTP servlet response will be flushed and set the committed flag. This means of course
041 * no change to the HTTP response header will be possible afterwards. By setting the {@link RunData#setAction(String)} in the request
042 * (not only the model, but also) additional response headers could be set, cft. {@link DefaultPage#doBuild(PipelineData)}.
043 *
044 * @author <a href="mailto:raphael@apache.org">Raphaël Luta</a>
045 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
046 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
047 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
048 * @version $Id$
049 */
050public class VelocityCachedLayout
051    extends Layout
052{
053    /** Logging */
054    private static Log log = LogFactory.getLog(VelocityCachedLayout.class);
055
056    /** The prefix for lookup up layout pages */
057    private String prefix = getPrefix() + "/";
058
059    /** Injected service instance */
060    @TurbineService
061    private VelocityService velocityService;
062
063    /**
064     * Method called by LayoutLoader.
065     *
066     *
067     * @param pipelineData PipelineData
068     * @throws Exception generic exception
069     */
070    @Override
071    public void doBuild(PipelineData pipelineData)
072        throws Exception
073    {
074        RunData data = getRunData(pipelineData);
075        // Get the context needed by Velocity.
076        Context context = velocityService.getContext(pipelineData);
077
078        // variable for the screen in the layout template
079        context.put(TurbineConstants.SCREEN_PLACEHOLDER,
080                    new TemplateScreen(data));
081
082        // variable to reference the navigation screen in the layout template
083        context.put(TurbineConstants.NAVIGATION_PLACEHOLDER,
084                    new TemplateNavigation(data));
085
086        // Grab the layout template set in the VelocityPage.
087        // If null, then use the default layout template
088        // (done by the TemplateInfo object)
089        String templateName = data.getTemplateInfo().getLayoutTemplate();
090
091        // Set the locale and content type
092        data.getResponse().setLocale(data.getLocale());
093        data.getResponse().setContentType(data.getContentType());
094
095        log.debug("Now trying to render layout " + templateName);
096
097        // Finally, generate the layout template and send it to the browser
098        velocityService.handleRequest(context,
099                prefix + templateName, data.getOut());
100    }
101}
102