    001package 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
    024import org.apache.commons.lang.StringUtils;
    025import org.apache.commons.logging.Log;
    026import org.apache.commons.logging.LogFactory;
    027import org.apache.turbine.TurbineConstants;
    028import org.apache.turbine.annotation.TurbineLoader;
    029import org.apache.turbine.annotation.TurbineService;
    030import org.apache.turbine.modules.Layout;
    031import org.apache.turbine.modules.Screen;
    032import org.apache.turbine.modules.ScreenLoader;
    033import org.apache.turbine.pipeline.PipelineData;
    034import org.apache.turbine.services.velocity.VelocityService;
    035import org.apache.turbine.util.RunData;
    036import org.apache.turbine.util.template.TemplateNavigation;
    037import org.apache.velocity.context.Context;
    038
    039/**
    040 * This Layout module allows Velocity templates to be used as layouts.
    041 * Since dynamic content is supposed to be primarily located in
    042 * screens and navigations there should be relatively few reasons to
    043 * subclass this Layout.
    044 *
    045 * To get the same functionality as with VelocityECSLayout, you can use two
    046 * supplied VelocityMacros, TurbineHtmlHead and TurbineHtmlBodyAttributes
    047 * in your templates. These are used to put HtmlPageAttributes into a page
    048 * before rendering.
    049 *
    050 * Use these macros should be used in the Layout template like this:
    051 *
    052 * ... set things like style sheets, scripts here.
    053 * &lt;html&gt;
    054 * #TurbineHtmlHead()
    055 * &lt;body #TurbineHtmlBodyAttributes() &gt;
    056 *  .... your body information
    057 * &lt;/body&gt;
    058 * &lt;/html&gt;
    059 *
    060 * As the layout template is rendered _after_ the screen template, you
    061 * can of course, add information to the $page tool in your screen template.
    062 * This will be added correctly to the &lt;head&gt;...&lt;/head&gt; and
    063 * &lt;body&gt; tags.
    064 *
    065 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
    066 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
    067 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
    068 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
    069 * @version $Id: VelocityOnlyLayout.java 1709648 2015-10-20 17:08:10Z tv $
    070 */
    071public class VelocityOnlyLayout
    072    extends Layout
    073{
    074    /** Logging */
    075    private static Log log = LogFactory.getLog(VelocityOnlyLayout.class);
    076
    077    /** The prefix for lookup up layout pages */
    078    private final String prefix = Layout.PREFIX + "/";
    079
    080    /** Injected service instance */
    081    @TurbineService
    082    private VelocityService velocityService;
    083
    084    /** Injected loader instance */
    085    @TurbineLoader( Screen.class )
    086    private ScreenLoader screenLoader;
    087
    088    /**
    089     * Build the layout.  Also sets the ContentType and Locale headers
    090     * of the HttpServletResponse object.
    091     *
    092     *
    093     * @param pipelineData PipelineData
    094     * @throws Exception generic exception
    095     */
    096    @Override
    097    public void doBuild(PipelineData pipelineData)
    098        throws Exception
    099    {
    100        RunData data = getRunData(pipelineData);
    101        // Get the context needed by Velocity.
    102        Context context = velocityService.getContext(pipelineData);
    103
    104        String screenName = data.getScreen();
    105
    106        log.debug("Loading Screen " + screenName);
    107
    108        // First, generate the screen and put it in the context so
    109        // we can grab it the layout template.
    110        String results = screenLoader.eval(pipelineData, screenName);
    111        String returnValue = StringUtils.defaultIfEmpty(results, StringUtils.EMPTY);
    112
    113        // variable for the screen in the layout template
    114        context.put(TurbineConstants.SCREEN_PLACEHOLDER, returnValue);
    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        velocityService.handleRequest(context,
    133                prefix + templateName, data.getResponse().getOutputStream());
    134    }
    135}