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.ecs.ConcreteElement;
25  import org.apache.turbine.Turbine;
26  import org.apache.turbine.TurbineConstants;
27  import org.apache.turbine.pipeline.PipelineData;
28  import org.apache.turbine.services.template.TurbineTemplate;
29  import org.apache.turbine.services.velocity.TurbineVelocity;
30  import org.apache.turbine.util.RunData;
31  import org.apache.velocity.context.Context;
32  
33  /**
34   * VelocityDirectScreen is a screen class which returns its output
35   * directly to the output stream. It can be used if buffering an
36   * output screen isn't possible or the result doesn't fit in the
37   * memory.
38   * <p>
39   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
40   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
41   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
42   * @version $Id: VelocityDirectScreen.java 938645 2010-04-27 20:57:51Z tv $
43   */
44  public class VelocityDirectScreen
45      extends VelocityScreen
46  {
47      /** The prefix for lookup up screen pages */
48      private String prefix = getPrefix() + "/";
49  
50      /**
51       * This builds the Velocity template.
52       *
53       * @deprecated Use PipelineData version instead.
54       * @param data Turbine information.
55       * @return A ConcreteElement.
56       * @exception Exception, a generic exception.
57       */
58      public ConcreteElement buildTemplate(RunData data)
59          throws Exception
60      {
61          Context context = TurbineVelocity.getContext(data);
62  
63          String screenTemplate = data.getTemplateInfo().getScreenTemplate();
64          String templateName
65              = TurbineTemplate.getScreenTemplateName(screenTemplate);
66  
67          // The Template Service could not find the Screen
68          if (StringUtils.isEmpty(templateName))
69          {
70              log.error("Screen " + screenTemplate + " not found!");
71              throw new Exception("Could not find screen for " + screenTemplate);
72          }
73  
74          try
75          {
76              TurbineVelocity.handleRequest(context,
77                                            prefix + templateName,
78                                            data.getResponse().getOutputStream());
79  
80          }
81          catch (Exception e)
82          {
83              // If there is an error, build a $processingException and
84              // attempt to call the error.vm template in the screens
85              // directory.
86              context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString());
87              context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e));
88  
89              templateName = Turbine.getConfiguration()
90                  .getString(TurbineConstants.TEMPLATE_ERROR_KEY,
91                             TurbineConstants.TEMPLATE_ERROR_VM);
92  
93              TurbineVelocity.handleRequest(context,
94                      prefix + templateName,
95                      data.getResponse().getOutputStream());
96          }
97  
98          return null;
99      }
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 }