| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| VelocityScreen |
|
| 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.lang.StringUtils; | |
| 23 | import org.apache.commons.lang.exception.ExceptionUtils; | |
| 24 | import org.apache.turbine.TurbineConstants; | |
| 25 | import org.apache.turbine.annotation.TurbineConfiguration; | |
| 26 | import org.apache.turbine.annotation.TurbineService; | |
| 27 | import org.apache.turbine.pipeline.PipelineData; | |
| 28 | import org.apache.turbine.services.template.TemplateService; | |
| 29 | import org.apache.turbine.services.velocity.VelocityService; | |
| 30 | import org.apache.turbine.util.RunData; | |
| 31 | import org.apache.velocity.context.Context; | |
| 32 | ||
| 33 | /** | |
| 34 | * Base Velocity Screen. The buildTemplate() assumes the template | |
| 35 | * parameter has been set in the PipelineData object. This provides the | |
| 36 | * ability to execute several templates from one Screen. | |
| 37 | * | |
| 38 | * <p> | |
| 39 | * | |
| 40 | * If you need more specific behavior in your application, extend this | |
| 41 | * class and override the doBuildTemplate() method. | |
| 42 | * | |
| 43 | * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a> | |
| 44 | * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> | |
| 45 | * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a> | |
| 46 | * @version $Id: VelocityScreen.java 1773378 2016-12-09 13:19:59Z tv $ | |
| 47 | */ | |
| 48 | 11 | public class VelocityScreen |
| 49 | extends TemplateScreen | |
| 50 | { | |
| 51 | /** The prefix for lookup up screen pages */ | |
| 52 | 11 | private final String prefix = getPrefix() + "/"; |
| 53 | ||
| 54 | /** Injected service instance */ | |
| 55 | @TurbineService | |
| 56 | protected VelocityService velocity; | |
| 57 | ||
| 58 | /** Injected service instance */ | |
| 59 | @TurbineService | |
| 60 | protected TemplateService templateService; | |
| 61 | ||
| 62 | 11 | @TurbineConfiguration( TurbineConstants.TEMPLATE_ERROR_KEY ) |
| 63 | protected String templateError = TurbineConstants.TEMPLATE_ERROR_VM; | |
| 64 | ||
| 65 | /** | |
| 66 | * Velocity Screens extending this class should override this | |
| 67 | * method to perform any particular business logic and add | |
| 68 | * information to the context. | |
| 69 | * | |
| 70 | * @param pipelineData Turbine information. | |
| 71 | * @param context Context for web pages. | |
| 72 | * @throws Exception a generic exception. | |
| 73 | */ | |
| 74 | protected void doBuildTemplate(PipelineData pipelineData, | |
| 75 | Context context) | |
| 76 | throws Exception | |
| 77 | { | |
| 78 | // empty | |
| 79 | 3 | } |
| 80 | ||
| 81 | /** | |
| 82 | * Needs to be implemented to make TemplateScreen like us. The | |
| 83 | * actual method that you should override is the one with the | |
| 84 | * context in the parameter list. | |
| 85 | * | |
| 86 | * @param pipelineData Turbine information. | |
| 87 | * @throws Exception a generic exception. | |
| 88 | */ | |
| 89 | @Override | |
| 90 | protected void doBuildTemplate(PipelineData pipelineData) | |
| 91 | throws Exception | |
| 92 | { | |
| 93 | 4 | doBuildTemplate(pipelineData, velocity.getContext(pipelineData)); |
| 94 | 4 | } |
| 95 | ||
| 96 | /** | |
| 97 | * This builds the Velocity template. | |
| 98 | * | |
| 99 | * @param pipelineData Turbine information. | |
| 100 | * @return the content of the screen | |
| 101 | * @throws Exception a generic exception. | |
| 102 | */ | |
| 103 | @Override | |
| 104 | public String buildTemplate(PipelineData pipelineData) | |
| 105 | throws Exception | |
| 106 | { | |
| 107 | 4 | RunData data = getRunData(pipelineData); |
| 108 | 4 | String screenData = null; |
| 109 | ||
| 110 | 4 | Context context = velocity.getContext(pipelineData); |
| 111 | ||
| 112 | 4 | String screenTemplate = data.getTemplateInfo().getScreenTemplate(); |
| 113 | 4 | String templateName |
| 114 | = templateService.getScreenTemplateName(screenTemplate); | |
| 115 | ||
| 116 | // The Template Service could not find the Screen | |
| 117 | 4 | if (StringUtils.isEmpty(templateName)) |
| 118 | { | |
| 119 | 2 | log.error("Screen " + screenTemplate + " not found!"); |
| 120 | 2 | throw new Exception("Could not find screen for " + screenTemplate); |
| 121 | } | |
| 122 | ||
| 123 | try | |
| 124 | { | |
| 125 | // if a layout has been defined return the results, otherwise | |
| 126 | // send the results directly to the output stream. | |
| 127 | 2 | if (getLayout(pipelineData) == null) |
| 128 | { | |
| 129 | 0 | velocity.handleRequest(context, |
| 130 | prefix + templateName, | |
| 131 | data.getResponse().getOutputStream()); | |
| 132 | } | |
| 133 | else | |
| 134 | { | |
| 135 | 2 | screenData = |
| 136 | velocity.handleRequest(context, prefix + templateName); | |
| 137 | } | |
| 138 | } | |
| 139 | 0 | catch (Exception e) |
| 140 | { | |
| 141 | // If there is an error, build a $processingException and | |
| 142 | // attempt to call the error.vm template in the screens | |
| 143 | // directory. | |
| 144 | 0 | context.put (TurbineConstants.PROCESSING_EXCEPTION_PLACEHOLDER, e.toString()); |
| 145 | 0 | context.put (TurbineConstants.STACK_TRACE_PLACEHOLDER, ExceptionUtils.getStackTrace(e)); |
| 146 | ||
| 147 | 0 | screenData = velocity.handleRequest(context, prefix + templateError); |
| 148 | 2 | } |
| 149 | ||
| 150 | 2 | return screenData; |
| 151 | } | |
| 152 | } |