    001    package 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
    022    import org.apache.commons.lang.StringUtils;
    023    import org.apache.commons.lang.exception.ExceptionUtils;
    024    import org.apache.ecs.ConcreteElement;
    025    import org.apache.turbine.Turbine;
    026    import org.apache.turbine.TurbineConstants;
    027    import org.apache.turbine.pipeline.PipelineData;
    028    import org.apache.turbine.services.template.TurbineTemplate;
    029    import org.apache.turbine.services.velocity.TurbineVelocity;
    030    import org.apache.turbine.util.RunData;
    031    import org.apache.velocity.context.Context;
    032
    033    /**
    034     * VelocityDirectScreen is a screen class which returns its output
    035     * directly to the output stream. It can be used if buffering an
    036     * output screen isn't possible or the result doesn't fit in the
    037     * memory.
    038     * <p>
    039     * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
    040     * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
    041     * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
    042     * @version $Id: VelocityDirectScreen.java 938645 2010-04-27 20:57:51Z tv $
    043     */
    044    public class VelocityDirectScreen
    045        extends VelocityScreen
    046    {
    047        /** The prefix for lookup up screen pages */
    048        private String prefix = getPrefix() + "/";
    049
    050        /**
    051         * This builds the Velocity template.
    052         *
    053         * @deprecated Use PipelineData version instead.
    054         * @param data Turbine information.
    055         * @return A ConcreteElement.
    056         * @exception Exception, a generic exception.
    057         */
    058        public ConcreteElement buildTemplate(RunData data)
    059            throws Exception
    060        {
    061            Context context = TurbineVelocity.getContext(data);
    062
    063            String screenTemplate = data.getTemplateInfo().getScreenTemplate();
    064            String templateName
    065                = TurbineTemplate.getScreenTemplateName(screenTemplate);
    066
    067            // The Template Service could not find the Screen
    068            if (StringUtils.isEmpty(templateName))
    069            {
    070                log.error("Screen " + screenTemplate + " not found!");
    071                throw new Exception("Could not find screen for " + screenTemplate);
    072            }
    073
    074            try
    075            {
    076                TurbineVelocity.handleRequest(context,
    077                                              prefix + templateName,
    078                                              data.getResponse().getOutputStream());
    079
    080            }
    081            catch (Exception e)
    082            {
    083                // If there is an error, build a $processingException and
    084                // attempt to call the error.vm template in the screens
    085                // directory.
    086                context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString());
    087                context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e));
    088
    089                templateName = Turbine.getConfiguration()
    090                    .getString(TurbineConstants.TEMPLATE_ERROR_KEY,
    091                               TurbineConstants.TEMPLATE_ERROR_VM);
    092
    093                TurbineVelocity.handleRequest(context,
    094                        prefix + templateName,
    095                        data.getResponse().getOutputStream());
    096            }
    097
    098            return null;
    099        }
    100
    101        /**
    102         * This builds the Velocity template.
    103         *
    104         * @param data Turbine information.
    105         * @return A ConcreteElement.
    106         * @exception Exception, a generic exception.
    107         */
    108        public ConcreteElement buildTemplate(PipelineData pipelineData)
    109            throws Exception
    110        {
    111            RunData data = getRunData(pipelineData);
    112            Context context = TurbineVelocity.getContext(pipelineData);
    113
    114            String screenTemplate = data.getTemplateInfo().getScreenTemplate();
    115            String templateName
    116                = TurbineTemplate.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                TurbineVelocity.handleRequest(context,
    128                                              prefix + templateName,
    129                                              data.getResponse().getOutputStream());
    130
    131            }
    132            catch (Exception e)
    133            {
    134                // If there is an error, build a $processingException and
    135                // attempt to call the error.vm template in the screens
    136                // directory.
    137                context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString());
    138                context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e));
    139
    140                templateName = Turbine.getConfiguration()
    141                    .getString(TurbineConstants.TEMPLATE_ERROR_KEY,
    142                               TurbineConstants.TEMPLATE_ERROR_VM);
    143
    144                TurbineVelocity.handleRequest(context,
    145                        prefix + templateName,
    146                        data.getResponse().getOutputStream());
    147            }
    148
    149            return null;
    150        }
    151    }