View Javadoc
1   package org.apache.turbine.modules.layouts;
2   
3   
4   /*
5    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *   http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.turbine.TurbineConstants;
28  import org.apache.turbine.annotation.TurbineLoader;
29  import org.apache.turbine.annotation.TurbineService;
30  import org.apache.turbine.modules.Layout;
31  import org.apache.turbine.modules.Screen;
32  import org.apache.turbine.modules.ScreenLoader;
33  import org.apache.turbine.pipeline.PipelineData;
34  import org.apache.turbine.services.velocity.VelocityService;
35  import org.apache.turbine.util.RunData;
36  import org.apache.turbine.util.template.TemplateNavigation;
37  import org.apache.velocity.context.Context;
38  
39  /**
40   * This Layout module allows Velocity templates to be used as layouts.
41   * Since dynamic content is supposed to be primarily located in
42   * screens and navigations there should be relatively few reasons to
43   * subclass this Layout.
44   *
45   * To get the same functionality as with VelocityECSLayout, you can use two
46   * supplied VelocityMacros, TurbineHtmlHead and TurbineHtmlBodyAttributes
47   * in your templates. These are used to put HtmlPageAttributes into a page
48   * before rendering.
49   *
50   * Use these macros should be used in the Layout template like this:
51   *
52   * ... set things like style sheets, scripts here.
53   * <html>
54   * #TurbineHtmlHead()
55   * <body #TurbineHtmlBodyAttributes() >
56   *  .... your body information
57   * </body>
58   * </html>
59   *
60   * As the layout template is rendered _after_ the screen template, you
61   * can of course, add information to the $page tool in your screen template.
62   * This will be added correctly to the <head>...</head> and
63   * <body> tags.
64   *
65   * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
66   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
67   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
68   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
69   * @version $Id: VelocityOnlyLayout.java 1709648 2015-10-20 17:08:10Z tv $
70   */
71  public class VelocityOnlyLayout
72      extends Layout
73  {
74      /** Logging */
75      private static Log log = LogFactory.getLog(VelocityOnlyLayout.class);
76  
77      /** The prefix for lookup up layout pages */
78      private final String prefix = Layout.PREFIX + "/";
79  
80      /** Injected service instance */
81      @TurbineService
82      private VelocityService velocityService;
83  
84      /** Injected loader instance */
85      @TurbineLoader( Screen.class )
86      private ScreenLoader screenLoader;
87  
88      /**
89       * Build the layout.  Also sets the ContentType and Locale headers
90       * of the HttpServletResponse object.
91       *
92       *
93       * @param pipelineData PipelineData
94       * @throws Exception generic exception
95       */
96      @Override
97      public void doBuild(PipelineData pipelineData)
98          throws Exception
99      {
100         RunData data = getRunData(pipelineData);
101         // Get the context needed by Velocity.
102         Context context = velocityService.getContext(pipelineData);
103 
104         String screenName = data.getScreen();
105 
106         log.debug("Loading Screen " + screenName);
107 
108         // First, generate the screen and put it in the context so
109         // we can grab it the layout template.
110         String results = screenLoader.eval(pipelineData, screenName);
111         String returnValue = StringUtils.defaultIfEmpty(results, StringUtils.EMPTY);
112 
113         // variable for the screen in the layout template
114         context.put(TurbineConstants.SCREEN_PLACEHOLDER, returnValue);
115 
116         // variable to reference the navigation screen in the layout template
117         context.put(TurbineConstants.NAVIGATION_PLACEHOLDER,
118                     new TemplateNavigation(data));
119 
120         // Grab the layout template set in the VelocityPage.
121         // If null, then use the default layout template
122         // (done by the TemplateInfo object)
123         String templateName = data.getTemplateInfo().getLayoutTemplate();
124 
125         // Set the locale and content type
126         data.getResponse().setLocale(data.getLocale());
127         data.getResponse().setContentType(data.getContentType());
128 
129         log.debug("Now trying to render layout " + templateName);
130 
131         // Finally, generate the layout template and send it to the browser
132         velocityService.handleRequest(context,
133                 prefix + templateName, data.getResponse().getOutputStream());
134     }
135 }