##### Coverage Report - org.apache.turbine.modules.screens.VelocityScreen

Classes in this File | Line Coverage | Branch Coverage | Complexity
VelocityScreen |
43% | 23/53
33% | 4/12
| 2.25;2,25

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