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  
25  import org.apache.ecs.ConcreteElement;
26  import org.apache.ecs.StringElement;
27  
28  import org.apache.velocity.context.Context;
29  
30  import org.apache.turbine.Turbine;
31  import org.apache.turbine.TurbineConstants;
32  import org.apache.turbine.services.template.TurbineTemplate;
33  import org.apache.turbine.services.velocity.TurbineVelocity;
34  import org.apache.turbine.util.RunData;
35  
36  /***
37   * Base Velocity Screen.  The buildTemplate() assumes the template
38   * parameter has been set in the RunData object.  This provides the
39   * ability to execute several templates from one Screen.
40   *
41   * <p>
42   *
43   * If you need more specific behavior in your application, extend this
44   * class and override the doBuildTemplate() method.
45   *
46   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
47   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
48   * @version $Id: VelocityScreen.java 534527 2007-05-02 16:10:59Z tv $
49   */
50  public class VelocityScreen
51      extends TemplateScreen
52  {
53      /*** The prefix for lookup up screen pages */
54      private String prefix = TurbineConstants.SCREEN_PREFIX + "/";
55  
56      /***
57       * Velocity Screens extending this class should overide this
58       * method to perform any particular business logic and add
59       * information to the context.
60       *
61       * @param data Turbine information.
62       * @param context Context for web pages.
63       * @exception Exception, a generic exception.
64       */
65      protected void doBuildTemplate(RunData data,
66                                     Context context)
67              throws Exception
68      {
69      }
70  
71      /***
72       * Needs to be implemented to make TemplateScreen like us.  The
73       * actual method that you should override is the one with the
74       * context in the parameter list.
75       *
76       * @param data Turbine information.
77       * @exception Exception, a generic exception.
78       */
79      protected void doBuildTemplate(RunData data)
80              throws Exception
81      {
82          doBuildTemplate(data, TurbineVelocity.getContext(data));
83      }
84  
85      /***
86       * This builds the Velocity template.
87       *
88       * @param data Turbine information.
89       * @return A ConcreteElement.
90       * @exception Exception, a generic exception.
91       */
92      public ConcreteElement buildTemplate(RunData data)
93          throws Exception
94      {
95          String screenData = null;
96  
97          Context context = TurbineVelocity.getContext(data);
98  
99          String screenTemplate = data.getTemplateInfo().getScreenTemplate();
100         String templateName
101             = TurbineTemplate.getScreenTemplateName(screenTemplate);
102 
103         // The Template Service could not find the Screen
104         if (StringUtils.isEmpty(templateName))
105         {
106             log.error("Screen " + screenTemplate + " not found!");
107             throw new Exception("Could not find screen for " + screenTemplate);
108         }
109 
110         try
111         {
112             // if a layout has been defined return the results, otherwise
113             // send the results directly to the output stream.
114             if (getLayout(data) == null)
115             {
116                 TurbineVelocity.handleRequest(context,
117                         prefix + templateName,
118                         data.getResponse().getOutputStream());
119             }
120             else
121             {
122                 screenData = TurbineVelocity
123                         .handleRequest(context, prefix + templateName);
124             }
125         }
126         catch (Exception e)
127         {
128             // If there is an error, build a $processingException and
129             // attempt to call the error.vm template in the screens
130             // directory.
131             context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString());
132             context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e));
133 
134             templateName = Turbine.getConfiguration()
135                 .getString(TurbineConstants.TEMPLATE_ERROR_KEY,
136                            TurbineConstants.TEMPLATE_ERROR_VM);
137 
138             screenData = TurbineVelocity.handleRequest(
139                 context, prefix + templateName);
140         }
141 
142         // package the response in an ECS element
143         StringElement output = new StringElement();
144         output.setFilterState(false);
145 
146         if (screenData != null)
147         {
148             output.addElement(screenData);
149         }
150         return output;
151     }
152 
153     /***
154      * Return the Context needed by Velocity.
155      *
156      * @param data Turbine information.
157      * @return A Context.
158      *
159      * @deprecated Use TurbineVelocity.getContext(data)
160      */
161     public static Context getContext(RunData data)
162     {
163         return TurbineVelocity.getContext(data);
164     }
165 }