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 * VelocityCachedScreen is Turbine 2.3.3 VelocityDirectScreen (same package)
031 * with methods added for {@link PipelineData}.
032 * It is is a screen class which buffers its output
033 * before flushing the output stream. It is used in Jetspeed-1 portal.
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 * @version $Id$
038 */
039public class VelocityCachedScreen
040    extends VelocityScreen
041{
042    /**
043     * This builds the Velocity template.
044     *
045     * @param pipelineData Turbine information.
046     * @return A ConcreteElement.
047     * @throws Exception a generic exception.
048     */
049    @Override
050    public String buildTemplate(PipelineData pipelineData)
051        throws Exception
052    {
053        RunData data = pipelineData.getRunData();
054        Context context = velocity.getContext(pipelineData);
055
056        String screenTemplate = data.getTemplateInfo().getScreenTemplate();
057        String templateName
058            = templateService.getScreenTemplateName(screenTemplate);
059
060        // The Template Service could not find the Screen
061        if (StringUtils.isEmpty(templateName))
062        {
063            log.error("Screen " + screenTemplate + " not found!");
064            throw new Exception("Could not find screen for " + screenTemplate);
065        }
066
067        try
068        {
069            velocity.handleRequest(context, prefix + templateName, data.getOut());
070        }
071        catch (Exception e)
072        {
073            // If there is an error, build a $processingException and
074            // attempt to call the error.vm template in the screens
075            // directory.
076            context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString());
077            context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e));
078
079            velocity.handleRequest(context, prefix + templateError, data.getOut());
080        }
081
082        return null;
083    }
084}
085
086