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

Classes in this File | Line Coverage | Branch Coverage | Complexity
VelocityScreen |
73% | 17/23
75% | 3/4
| 2.3333333333333335;2,333

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.configuration.Configuration;
23 |  |
     import org.apache.commons.lang.StringUtils;
24 |  |
     import org.apache.commons.lang.exception.ExceptionUtils;
25 |  |
     import org.apache.turbine.TurbineConstants;
26 |  |
     import org.apache.turbine.annotation.TurbineConfiguration;
27 |  |
     import org.apache.turbine.annotation.TurbineService;
28 |  |
     import org.apache.turbine.pipeline.PipelineData;
29 |  |
     import org.apache.turbine.services.template.TemplateService;
30 |  |
     import org.apache.turbine.services.velocity.VelocityService;
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 PipelineData 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 1709648 2015-10-20 17:08:10Z tv $
48 |  |
      */
49 | 11 |
     public class VelocityScreen
50 |  |
         extends TemplateScreen
51 |  |
     {
52 |  |
         /** The prefix for lookup up screen pages */
53 | 11 |
         private final String prefix = getPrefix() + "/";
54 |  |
55 |  |
         /** Injected service instance */
56 |  |
         @TurbineService
57 |  |
         protected VelocityService velocity;
58 |  |
59 |  |
         /** Injected service instance */
60 |  |
         @TurbineService
61 |  |
         protected TemplateService templateService;
62 |  |
63 |  |
         /** Injected configuration instance */
64 |  |
         @TurbineConfiguration
65 |  |
         protected Configuration conf;
66 |  |
67 |  |
         /**
68 |  |
          * Velocity Screens extending this class should override this
69 |  |
          * method to perform any particular business logic and add
70 |  |
          * information to the context.
71 |  |
          *
72 |  |
          * @param pipelineData Turbine information.
73 |  |
          * @param context Context for web pages.
74 |  |
          * @exception Exception, a generic exception.
75 |  |
          */
76 |  |
         protected void doBuildTemplate(PipelineData pipelineData,
77 |  |
                                        Context context)
78 |  |
                 throws Exception
79 |  |
         {
80 |  |
             // empty
81 | 3 |
         }
82 |  |
83 |  |
         /**
84 |  |
          * Needs to be implemented to make TemplateScreen like us.  The
85 |  |
          * actual method that you should override is the one with the
86 |  |
          * context in the parameter list.
87 |  |
          *
88 |  |
          * @param pipelineData Turbine information.
89 |  |
          * @exception Exception, a generic exception.
90 |  |
          */
91 |  |
         @Override
92 |  |
         protected void doBuildTemplate(PipelineData pipelineData)
93 |  |
                 throws Exception
94 |  |
         {
95 | 4 |
             doBuildTemplate(pipelineData, velocity.getContext(pipelineData));
96 | 4 |
         }
97 |  |
98 |  |
         /**
99 |  |
          * This builds the Velocity template.
100 |  |
          *
101 |  |
          * @param pipelineData Turbine information.
102 |  |
          * @return the content of the screen
103 |  |
          * @exception Exception, a generic exception.
104 |  |
          */
105 |  |
         @Override
106 |  |
         public String buildTemplate(PipelineData pipelineData)
107 |  |
             throws Exception
108 |  |
         {
109 | 4 |
             RunData data = getRunData(pipelineData);
110 | 4 |
             String screenData = null;
111 |  |
112 | 4 |
             Context context = velocity.getContext(pipelineData);
113 |  |
114 | 4 |
             String screenTemplate = data.getTemplateInfo().getScreenTemplate();
115 | 4 |
             String templateName
116 |  |
                 = templateService.getScreenTemplateName(screenTemplate);
117 |  |
118 |  |
             // The Template Service could not find the Screen
119 | 4 |
             if (StringUtils.isEmpty(templateName))
120 |  |
             {
121 | 2 |
                 log.error("Screen " + screenTemplate + " not found!");
122 | 2 |
                 throw new Exception("Could not find screen for " + screenTemplate);
123 |  |
             }
124 |  |
125 |  |
             try
126 |  |
             {
127 |  |
                 // if a layout has been defined return the results, otherwise
128 |  |
                 // send the results directly to the output stream.
129 | 2 |
                 if (getLayout(pipelineData) == null)
130 |  |
                 {
131 | 0 |
                     velocity.handleRequest(context,
132 |  |
                             prefix + templateName,
133 |  |
                             data.getResponse().getOutputStream());
134 |  |
                 }
135 |  |
                 else
136 |  |
                 {
137 | 2 |
                     screenData =
138 |  |
                         velocity.handleRequest(context, prefix + templateName);
139 |  |
                 }
140 |  |
             }
141 | 0 |
             catch (Exception e)
142 |  |
             {
143 |  |
                 // If there is an error, build a $processingException and
144 |  |
                 // attempt to call the error.vm template in the screens
145 |  |
                 // directory.
146 | 0 |
                 context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString());
147 | 0 |
                 context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e));
148 |  |
149 | 0 |
                 templateName = conf.getString(TurbineConstants.TEMPLATE_ERROR_KEY,
150 |  |
                                TurbineConstants.TEMPLATE_ERROR_VM);
151 |  |
152 | 0 |
                 screenData = velocity.handleRequest(context, prefix + templateName);
153 | 2 |
             }
154 |  |
155 | 2 |
             return screenData;
156 |  |
         }
157 |  |
     }