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.lang.StringUtils;
23  import org.apache.commons.lang.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: VelocityDirectScreen.java 1709648 2015-10-20 17:08:10Z tv $
39   */
40  public class VelocityDirectScreen
41      extends VelocityScreen
42  {
43      /** The prefix for lookup up screen pages */
44      private String prefix = getPrefix() + "/";
45  
46      /**
47       * This builds the Velocity template.
48       *
49       * @param pipelineData Turbine information.
50       * @return the content of the screen
51       * @exception Exception, a generic exception.
52       */
53      @Override
54      public String buildTemplate(PipelineData pipelineData)
55          throws Exception
56      {
57          RunData data = getRunData(pipelineData);
58          Context context = velocity.getContext(pipelineData);
59  
60          String screenTemplate = data.getTemplateInfo().getScreenTemplate();
61          String templateName
62              = templateService.getScreenTemplateName(screenTemplate);
63  
64          // The Template Service could not find the Screen
65          if (StringUtils.isEmpty(templateName))
66          {
67              log.error("Screen " + screenTemplate + " not found!");
68              throw new Exception("Could not find screen for " + screenTemplate);
69          }
70  
71          try
72          {
73              velocity.handleRequest(context,
74                                            prefix + templateName,
75                                            data.getResponse().getOutputStream());
76  
77          }
78          catch (Exception e)
79          {
80              // If there is an error, build a $processingException and
81              // attempt to call the error.vm template in the screens
82              // directory.
83              context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString());
84              context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e));
85  
86              templateName = conf.getString(TurbineConstants.TEMPLATE_ERROR_KEY,
87                             TurbineConstants.TEMPLATE_ERROR_VM);
88  
89              velocity.handleRequest(context,
90                      prefix + templateName,
91                      data.getResponse().getOutputStream());
92          }
93  
94          return null;
95      }
96  }