Coverage Report - org.apache.turbine.modules.layouts.VelocityLayout
 
Classes in this File Line Coverage Branch Coverage Complexity
VelocityLayout
100%
15/15
N/A
1
 
 1  
 package org.apache.turbine.modules.layouts;
 2  
 
 3  
 
 4  
 import javax.servlet.http.HttpServletResponse;
 5  
 
 6  
 import org.apache.logging.log4j.LogManager;
 7  
 
 8  
 /*
 9  
  * Licensed to the Apache Software Foundation (ASF) under one
 10  
  * or more contributor license agreements.  See the NOTICE file
 11  
  * distributed with this work for additional information
 12  
  * regarding copyright ownership.  The ASF licenses this file
 13  
  * to you under the Apache License, Version 2.0 (the
 14  
  * "License"); you may not use this file except in compliance
 15  
  * with the License.  You may obtain a copy of the License at
 16  
  *
 17  
  *   http://www.apache.org/licenses/LICENSE-2.0
 18  
  *
 19  
  * Unless required by applicable law or agreed to in writing,
 20  
  * software distributed under the License is distributed on an
 21  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 22  
  * KIND, either express or implied.  See the License for the
 23  
  * specific language governing permissions and limitations
 24  
  * under the License.
 25  
  */
 26  
 
 27  
 
 28  
 import org.apache.logging.log4j.Logger;
 29  
 import org.apache.turbine.Turbine;
 30  
 import org.apache.turbine.annotation.TurbineService;
 31  
 import org.apache.turbine.modules.Layout;
 32  
 import org.apache.turbine.pipeline.PipelineData;
 33  
 import org.apache.turbine.services.velocity.VelocityService;
 34  
 import org.apache.turbine.util.RunData;
 35  
 import org.apache.velocity.context.Context;
 36  
 
 37  
 /**
 38  
  * This Layout module allows Velocity templates
 39  
  * to be used as layouts.
 40  
  *
 41  
  * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
 42  
  */
 43  27
 public abstract class VelocityLayout implements Layout
 44  
 {
 45  
     /** Logging */
 46  27
     protected final Logger log = LogManager.getLogger(this.getClass());
 47  
 
 48  
     /** The prefix for lookup up layout pages */
 49  
     protected static final String prefix = PREFIX + "/";
 50  
 
 51  
     /** Injected service instance */
 52  
     @TurbineService
 53  
     protected VelocityService velocityService;
 54  
 
 55  
     /**
 56  
      * Method called by LayoutLoader.
 57  
      *
 58  
      *
 59  
      * @param pipelineData PipelineData
 60  
      * @throws Exception generic exception
 61  
      */
 62  
     @Override
 63  
     public void doBuild(PipelineData pipelineData)
 64  
         throws Exception
 65  
     {
 66  12
         RunData data = pipelineData.getRunData();
 67  
         // Get the context needed by Velocity.
 68  12
         Context context = velocityService.getContext(pipelineData);
 69  
 
 70  
         // Provide objects to Velocity context
 71  12
         populateContext(pipelineData, context);
 72  
 
 73  
         // Grab the layout template set in the VelocityPage.
 74  
         // If null, then use the default layout template
 75  
         // (done by the TemplateInfo object)
 76  6
         String templateName = data.getTemplateInfo().getLayoutTemplate();
 77  
 
 78  
         // Set the locale and content type
 79  6
         data.getResponse().setLocale(data.getLocale());
 80  6
         data.getResponse().setContentType(data.getContentType());
 81  
 
 82  6
         log.debug("Now trying to render layout {}", templateName);
 83  
 
 84  
         // Finally, generate the layout template and send it to the browser
 85  6
         render(pipelineData, context, templateName);
 86  6
     }
 87  
 
 88  
     /**
 89  
      * Populate Velocity context
 90  
      *
 91  
      * @param pipelineData PipelineData
 92  
      * @param context the Velocity context
 93  
      *
 94  
      * @throws Exception if evaluation fails
 95  
      */
 96  
     protected abstract void populateContext(PipelineData pipelineData, Context context)
 97  
         throws Exception;
 98  
 
 99  
     /**
 100  
      * Render layout
 101  
      *
 102  
      * @param pipelineData PipelineData
 103  
      * @param context the Velocity context
 104  
      * @param templateName relative path to Velocity template
 105  
      *
 106  
      * @throws Exception if rendering fails
 107  
      */
 108  
     protected void render(PipelineData pipelineData, Context context, String templateName)
 109  
         throws Exception
 110  
     {
 111  12
         velocityService.handleRequest(context,
 112  
                 prefix + templateName,
 113  6
                 pipelineData.get(Turbine.class, HttpServletResponse.class)
 114  6
                     .getOutputStream());
 115  6
     }
 116  
 }