    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.lang.StringUtils;
    023import org.apache.commons.lang.exception.ExceptionUtils;
    024import org.apache.turbine.TurbineConstants;
    025import org.apache.turbine.pipeline.PipelineData;
    026import org.apache.turbine.util.RunData;
    027import org.apache.velocity.context.Context;
    028
    029/**
    030 * VelocityDirectScreen is a screen class which returns its output
    031 * directly to the output stream. It can be used if buffering an
    032 * output screen isn't possible or the result doesn't fit in the
    033 * memory.
    034 * <p>
    035 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
    036 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
    037 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
    038 * @version $Id: VelocityDirectScreen.java 1773378 2016-12-09 13:19:59Z tv $
    039 */
    040public class VelocityDirectScreen
    041    extends VelocityScreen
    042{
    043    /** The prefix for lookup up screen pages */
    044    private String prefix = getPrefix() + "/";
    045
    046    /**
    047     * This builds the Velocity template.
    048     *
    049     * @param pipelineData Turbine information.
    050     * @return the content of the screen
    051     * @throws Exception a generic exception.
    052     */
    053    @Override
    054    public String buildTemplate(PipelineData pipelineData)
    055        throws Exception
    056    {
    057        RunData data = getRunData(pipelineData);
    058        Context context = velocity.getContext(pipelineData);
    059
    060        String screenTemplate = data.getTemplateInfo().getScreenTemplate();
    061        String templateName
    062            = templateService.getScreenTemplateName(screenTemplate);
    063
    064        // The Template Service could not find the Screen
    065        if (StringUtils.isEmpty(templateName))
    066        {
    067            log.error("Screen " + screenTemplate + " not found!");
    068            throw new Exception("Could not find screen for " + screenTemplate);
    069        }
    070
    071        try
    072        {
    073            velocity.handleRequest(context,
    074                                          prefix + templateName,
    075                                          data.getResponse().getOutputStream());
    076
    077        }
    078        catch (Exception e)
    079        {
    080            // If there is an error, build a $processingException and
    081            // attempt to call the error.vm template in the screens
    082            // directory.
    083            context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString());
    084            context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e));
    085
    086            velocity.handleRequest(context, prefix + templateError,
    087                    data.getResponse().getOutputStream());
    088        }
    089
    090        return null;
    091    }
    092}