    001package org.apache.turbine.modules.screens;
    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.configuration.Configuration;
    023import org.apache.commons.lang.StringUtils;
    024import org.apache.commons.lang.exception.ExceptionUtils;
    025import org.apache.turbine.TurbineConstants;
    026import org.apache.turbine.annotation.TurbineConfiguration;
    027import org.apache.turbine.annotation.TurbineService;
    028import org.apache.turbine.pipeline.PipelineData;
    029import org.apache.turbine.services.template.TemplateService;
    030import org.apache.turbine.services.velocity.VelocityService;
    031import org.apache.turbine.util.RunData;
    032import org.apache.velocity.context.Context;
    033
    034/**
    035 * Base Velocity Screen.  The buildTemplate() assumes the template
    036 * parameter has been set in the PipelineData object.  This provides the
    037 * ability to execute several templates from one Screen.
    038 *
    039 * <p>
    040 *
    041 * If you need more specific behavior in your application, extend this
    042 * class and override the doBuildTemplate() method.
    043 *
    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: VelocityScreen.java 1709648 2015-10-20 17:08:10Z tv $
    048 */
    049public class VelocityScreen
    050    extends TemplateScreen
    051{
    052    /** The prefix for lookup up screen pages */
    053    private final String prefix = getPrefix() + "/";
    054
    055    /** Injected service instance */
    056    @TurbineService
    057    protected VelocityService velocity;
    058
    059    /** Injected service instance */
    060    @TurbineService
    061    protected TemplateService templateService;
    062
    063    /** Injected configuration instance */
    064    @TurbineConfiguration
    065    protected Configuration conf;
    066
    067    /**
    068     * Velocity Screens extending this class should override this
    069     * method to perform any particular business logic and add
    070     * information to the context.
    071     *
    072     * @param pipelineData Turbine information.
    073     * @param context Context for web pages.
    074     * @exception Exception, a generic exception.
    075     */
    076    protected void doBuildTemplate(PipelineData pipelineData,
    077                                   Context context)
    078            throws Exception
    079    {
    080        // empty
    081    }
    082
    083    /**
    084     * Needs to be implemented to make TemplateScreen like us.  The
    085     * actual method that you should override is the one with the
    086     * context in the parameter list.
    087     *
    088     * @param pipelineData Turbine information.
    089     * @exception Exception, a generic exception.
    090     */
    091    @Override
    092    protected void doBuildTemplate(PipelineData pipelineData)
    093            throws Exception
    094    {
    095        doBuildTemplate(pipelineData, velocity.getContext(pipelineData));
    096    }
    097
    098    /**
    099     * This builds the Velocity template.
    100     *
    101     * @param pipelineData Turbine information.
    102     * @return the content of the screen
    103     * @exception Exception, a generic exception.
    104     */
    105    @Override
    106    public String buildTemplate(PipelineData pipelineData)
    107        throws Exception
    108    {
    109        RunData data = getRunData(pipelineData);
    110        String screenData = null;
    111
    112        Context context = velocity.getContext(pipelineData);
    113
    114        String screenTemplate = data.getTemplateInfo().getScreenTemplate();
    115        String templateName
    116            = templateService.getScreenTemplateName(screenTemplate);
    117
    118        // The Template Service could not find the Screen
    119        if (StringUtils.isEmpty(templateName))
    120        {
    121            log.error("Screen " + screenTemplate + " not found!");
    122            throw new Exception("Could not find screen for " + screenTemplate);
    123        }
    124
    125        try
    126        {
    127            // if a layout has been defined return the results, otherwise
    128            // send the results directly to the output stream.
    129            if (getLayout(pipelineData) == null)
    130            {
    131                velocity.handleRequest(context,
    132                        prefix + templateName,
    133                        data.getResponse().getOutputStream());
    134            }
    135            else
    136            {
    137                screenData =
    138                    velocity.handleRequest(context, prefix + templateName);
    139            }
    140        }
    141        catch (Exception e)
    142        {
    143            // If there is an error, build a $processingException and
    144            // attempt to call the error.vm template in the screens
    145            // directory.
    146            context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString());
    147            context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e));
    148
    149            templateName = conf.getString(TurbineConstants.TEMPLATE_ERROR_KEY,
    150                           TurbineConstants.TEMPLATE_ERROR_VM);
    151
    152            screenData = velocity.handleRequest(context, prefix + templateName);
    153        }
    154
    155        return screenData;
    156    }
    157}