View Javadoc

1   package org.apache.turbine.modules.screens;
2   
3   
4   /*
5    * Copyright 2001-2004 The Apache Software Foundation.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License")
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  
21  import org.apache.commons.lang.StringUtils;
22  
23  import org.apache.commons.lang.exception.ExceptionUtils;
24  
25  import org.apache.ecs.ConcreteElement;
26  import org.apache.ecs.StringElement;
27  
28  import org.apache.turbine.Turbine;
29  import org.apache.turbine.TurbineConstants;
30  
31  import org.apache.turbine.pipeline.PipelineData;
32  import org.apache.turbine.services.template.TurbineTemplate;
33  
34  import org.apache.turbine.services.velocity.TurbineVelocity;
35  
36  import org.apache.turbine.util.RunData;
37  
38  import org.apache.velocity.context.Context;
39  
40  /***
41   * Base Velocity Screen.  The buildTemplate() assumes the template
42   * parameter has been set in the RunData object.  This provides the
43   * ability to execute several templates from one Screen.
44   *
45   * <p>
46   *
47   * If you need more specific behavior in your application, extend this
48   * class and override the doBuildTemplate() method.
49   *
50   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
51   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
52   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
53   * @version $Id: VelocityScreen.java 222043 2004-12-06 17:47:33Z painter $
54   */
55  public class VelocityScreen
56      extends TemplateScreen
57  {
58      /*** The prefix for lookup up screen pages */
59      private String prefix = TurbineConstants.SCREEN_PREFIX + "/";
60  
61      /***
62       * Velocity Screens extending this class should overide this
63       * method to perform any particular business logic and add
64       * information to the context.
65       *
66       * @deprecated Use PipelineData version instead.
67       * @param data Turbine information.
68       * @param context Context for web pages.
69       * @exception Exception, a generic exception.
70       */
71      protected void doBuildTemplate(RunData data,
72                                     Context context)
73              throws Exception
74      {
75      }
76  
77      /***
78       * Velocity Screens extending this class should overide this
79       * method to perform any particular business logic and add
80       * information to the context.
81       *
82       * @param pipelineData Turbine information.
83       * @param context Context for web pages.
84       * @exception Exception, a generic exception.
85       */
86      protected void doBuildTemplate(PipelineData pipelineData,
87                                     Context context)
88              throws Exception
89      {
90      }
91  
92      
93      /***
94       * Needs to be implemented to make TemplateScreen like us.  The
95       * actual method that you should override is the one with the
96       * context in the parameter list.
97       *
98       * @deprecated Use PipelineData version instead.
99       * @param data Turbine information.
100      * @exception Exception, a generic exception.
101      */
102     protected void doBuildTemplate(RunData data)
103             throws Exception
104     {
105         doBuildTemplate(data, TurbineVelocity.getContext(data));
106     }
107 
108     /***
109      * Needs to be implemented to make TemplateScreen like us.  The
110      * actual method that you should override is the one with the
111      * context in the parameter list.
112      *
113      * @param data Turbine information.
114      * @exception Exception, a generic exception.
115      */
116     protected void doBuildTemplate(PipelineData pipelineData)
117             throws Exception
118     {
119         doBuildTemplate(pipelineData, TurbineVelocity.getContext(pipelineData));
120     }
121 
122     
123     
124     /***
125      * This builds the Velocity template.
126      *
127      * @deprecated Use PipelineData version instead.
128      * @param data Turbine information.
129      * @return A ConcreteElement.
130      * @exception Exception, a generic exception.
131      */
132     public ConcreteElement buildTemplate(RunData data)
133         throws Exception
134     {
135         String screenData = null;
136         
137         Context context = TurbineVelocity.getContext(data);
138 
139         String screenTemplate = data.getTemplateInfo().getScreenTemplate();
140         String templateName
141             = TurbineTemplate.getScreenTemplateName(screenTemplate);
142         
143         // The Template Service could not find the Screen
144         if (StringUtils.isEmpty(templateName))
145         {
146             log.error("Screen " + screenTemplate + " not found!");
147             throw new Exception("Could not find screen for " + screenTemplate);
148         }
149         
150         try
151         {
152             // if a layout has been defined return the results, otherwise
153             // send the results directly to the output stream.
154             if (getLayout(data) == null)
155             {
156                 TurbineVelocity.handleRequest(context,
157                         prefix + templateName,
158                         data.getResponse().getOutputStream());
159             }
160             else
161             {
162                 screenData = TurbineVelocity
163                         .handleRequest(context, prefix + templateName);
164             }
165         }
166         catch (Exception e)
167         {
168             // If there is an error, build a $processingException and
169             // attempt to call the error.vm template in the screens
170             // directory.
171             context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString());
172             context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e));
173             
174             templateName = Turbine.getConfiguration()
175                 .getString(TurbineConstants.TEMPLATE_ERROR_KEY,
176                            TurbineConstants.TEMPLATE_ERROR_VM);
177             
178             screenData = TurbineVelocity.handleRequest(
179                 context, prefix + templateName);
180         }
181         
182         // package the response in an ECS element
183         StringElement output = new StringElement();
184         output.setFilterState(false);
185 
186         if (screenData != null)
187         {
188             output.addElement(screenData);
189         }
190         return output;
191     }
192 
193     /***
194      * This builds the Velocity template.
195      *
196      * @param data Turbine information.
197      * @return A ConcreteElement.
198      * @exception Exception, a generic exception.
199      */
200     public ConcreteElement buildTemplate(PipelineData pipelineData)
201         throws Exception
202     {
203         RunData data = (RunData) getRunData(pipelineData);
204         String screenData = null;
205         
206         Context context = TurbineVelocity.getContext(pipelineData);
207 
208         String screenTemplate = data.getTemplateInfo().getScreenTemplate();
209         String templateName
210             = TurbineTemplate.getScreenTemplateName(screenTemplate);
211         
212         // The Template Service could not find the Screen
213         if (StringUtils.isEmpty(templateName))
214         {
215             log.error("Screen " + screenTemplate + " not found!");
216             throw new Exception("Could not find screen for " + screenTemplate);
217         }
218         
219         try
220         {
221             // if a layout has been defined return the results, otherwise
222             // send the results directly to the output stream.
223             if (getLayout(pipelineData) == null)
224             {
225                 TurbineVelocity.handleRequest(context,
226                         prefix + templateName,
227                         data.getResponse().getOutputStream());
228             }
229             else
230             {
231                 screenData = TurbineVelocity
232                         .handleRequest(context, prefix + templateName);
233             }
234         }
235         catch (Exception e)
236         {
237             // If there is an error, build a $processingException and
238             // attempt to call the error.vm template in the screens
239             // directory.
240             context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString());
241             context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e));
242             
243             templateName = Turbine.getConfiguration()
244                 .getString(TurbineConstants.TEMPLATE_ERROR_KEY,
245                            TurbineConstants.TEMPLATE_ERROR_VM);
246             
247             screenData = TurbineVelocity.handleRequest(
248                 context, prefix + templateName);
249         }
250         
251         // package the response in an ECS element
252         StringElement output = new StringElement();
253         output.setFilterState(false);
254 
255         if (screenData != null)
256         {
257             output.addElement(screenData);
258         }
259         return output;
260     }
261     
262     
263     /***
264      * Return the Context needed by Velocity.
265      *
266      * @param data Turbine information.
267      * @return A Context.
268      *
269      * @deprecated Use TurbineVelocity.getContext(data)
270      */
271     public static Context getContext(RunData data)
272     {
273         return TurbineVelocity.getContext(data);
274     }
275     
276     /***
277      * Return the Context needed by Velocity.
278      *
279      * @param data Turbine information.
280      * @return A Context.
281      *
282      * @deprecated Use TurbineVelocity.getContext(pipelineData)
283      */
284     public static Context getContext(PipelineData pipelineData)
285     {
286         return TurbineVelocity.getContext(pipelineData);
287     }
288 
289 }