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