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   * VelocityDirectScreen is a screen class which returns its output
31   * directly to the output stream. It can be used if buffering an
32   * output screen isn't possible or the result doesn't fit in the
33   * memory.
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   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
38   * @version $Id$
39   */
40  public class VelocityDirectScreen
41      extends VelocityScreen
42  {
43      /**
44       * This builds the Velocity template.
45       *
46       * @param pipelineData Turbine information.
47       * @return the content of the screen
48       * @throws Exception a generic exception.
49       */
50      @Override
51      public String buildTemplate(PipelineData pipelineData)
52          throws Exception
53      {
54          RunData data = pipelineData.getRunData();
55          Context context = velocity.getContext(pipelineData);
56  
57          String screenTemplate = data.getTemplateInfo().getScreenTemplate();
58          String templateName
59              = templateService.getScreenTemplateName(screenTemplate);
60  
61          // The Template Service could not find the Screen
62          if (StringUtils.isEmpty(templateName))
63          {
64              log.error("Screen " + screenTemplate + " not found!");
65              throw new Exception("Could not find screen for " + screenTemplate);
66          }
67  
68          try
69          {
70              velocity.handleRequest(context,
71                                            prefix + templateName,
72                                            data.getResponse().getOutputStream());
73  
74          }
75          catch (Exception e)
76          {
77              // If there is an error, build a $processingException and
78              // attempt to call the error.vm template in the screens
79              // directory.
80              context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString());
81              context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e));
82  
83              velocity.handleRequest(context, prefix + templateError,
84                      data.getResponse().getOutputStream());
85          }
86  
87          return null;
88      }
89  }