View Javadoc
1   package org.apache.turbine.modules.screens;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.lang3.StringUtils;
23  import org.apache.commons.lang3.exception.ExceptionUtils;
24  import org.apache.turbine.TurbineConstants;
25  import org.apache.turbine.pipeline.PipelineData;
26  import org.apache.turbine.util.RunData;
27  import org.apache.velocity.context.Context;
28  
29  /**
30   * VelocityCachedScreen is Turbine 2.3.3 VelocityDirectScreen (same package)
31   * with methods added for {@link PipelineData}.
32   * It is is a screen class which buffers its output
33   * before flushing the output stream. It is used in Jetspeed-1 portal.
34   * <p>
35   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
36   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
37   * @version $Id$
38   */
39  public class VelocityCachedScreen
40      extends VelocityScreen
41  {
42      /**
43       * This builds the Velocity template.
44       *
45       * @param pipelineData Turbine information.
46       * @return A ConcreteElement.
47       * @throws Exception a generic exception.
48       */
49      @Override
50      public String buildTemplate(PipelineData pipelineData)
51          throws Exception
52      {
53          RunData data = pipelineData.getRunData();
54          Context context = velocity.getContext(pipelineData);
55  
56          String screenTemplate = data.getTemplateInfo().getScreenTemplate();
57          String templateName
58              = templateService.getScreenTemplateName(screenTemplate);
59  
60          // The Template Service could not find the Screen
61          if (StringUtils.isEmpty(templateName))
62          {
63              log.error("Screen " + screenTemplate + " not found!");
64              throw new Exception("Could not find screen for " + screenTemplate);
65          }
66  
67          try
68          {
69              velocity.handleRequest(context, prefix + templateName, data.getOut());
70          }
71          catch (Exception e)
72          {
73              // If there is an error, build a $processingException and
74              // attempt to call the error.vm template in the screens
75              // directory.
76              context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString());
77              context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e));
78  
79              velocity.handleRequest(context, prefix + templateError, data.getOut());
80          }
81  
82          return null;
83      }
84  }
85  
86