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.lang3.StringUtils;
023import org.apache.commons.lang3.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$
039 */
040public class VelocityDirectScreen
041    extends VelocityScreen
042{
043    /**
044     * This builds the Velocity template.
045     *
046     * @param pipelineData Turbine information.
047     * @return the content of the screen
048     * @throws Exception a generic exception.
049     */
050    @Override
051    public String buildTemplate(PipelineData pipelineData)
052        throws Exception
053    {
054        RunData data = pipelineData.getRunData();
055        Context context = velocity.getContext(pipelineData);
056
057        String screenTemplate = data.getTemplateInfo().getScreenTemplate();
058        String templateName
059            = templateService.getScreenTemplateName(screenTemplate);
060
061        // The Template Service could not find the Screen
062        if (StringUtils.isEmpty(templateName))
063        {
064            log.error("Screen " + screenTemplate + " not found!");
065            throw new Exception("Could not find screen for " + screenTemplate);
066        }
067
068        try
069        {
070            velocity.handleRequest(context,
071                                          prefix + templateName,
072                                          data.getResponse().getOutputStream());
073
074        }
075        catch (Exception e)
076        {
077            // If there is an error, build a $processingException and
078            // attempt to call the error.vm template in the screens
079            // directory.
080            context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString());
081            context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e));
082
083            velocity.handleRequest(context, prefix + templateError,
084                    data.getResponse().getOutputStream());
085        }
086
087        return null;
088    }
089}