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.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.turbine.TurbineConstants;
27  import org.apache.turbine.annotation.TurbineService;
28  import org.apache.turbine.modules.Layout;
29  import org.apache.turbine.pipeline.PipelineData;
30  import org.apache.turbine.services.velocity.VelocityService;
31  import org.apache.turbine.util.RunData;
32  import org.apache.turbine.util.template.TemplateNavigation;
33  import org.apache.turbine.util.template.TemplateScreen;
34  import org.apache.velocity.context.Context;
35  
36  /**
37   * This Layout module allows Velocity templates
38   * to be used as layouts. It will stream directly the output of
39   * the layout and navigation templates to the output writer without
40   * using a screen. Use this if you have a large page to output
41   * and won't buffer it in the memory.
42   *
43   * @author <a href="mailto:raphael@apache.org">Raphaƫl Luta</a>
44   * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
45   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
46   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
47   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
48   * @version $Id: VelocityDirectLayout.java 1706239 2015-10-01 13:18:35Z tv $
49   */
50  public class VelocityDirectLayout
51      extends Layout
52  {
53      /** Logging */
54      private static Log log = LogFactory.getLog(VelocityDirectLayout.class);
55  
56      /** The prefix for lookup up layout pages */
57      private String prefix = Layout.PREFIX + "/";
58  
59      /** Injected service instance */
60      @TurbineService
61      private VelocityService velocityService;
62  
63      /**
64       * Method called by LayoutLoader.
65       *
66       *
67       * @param pipelineData PipelineData
68       * @throws Exception generic exception
69       */
70      @Override
71      public void doBuild(PipelineData pipelineData)
72          throws Exception
73      {
74          RunData data = getRunData(pipelineData);
75          // Get the context needed by Velocity.
76          Context context = velocityService.getContext(pipelineData);
77  
78          // variable for the screen in the layout template
79          context.put(TurbineConstants.SCREEN_PLACEHOLDER,
80                      new TemplateScreen(data));
81  
82          // variable to reference the navigation screen in the layout template
83          context.put(TurbineConstants.NAVIGATION_PLACEHOLDER,
84                      new TemplateNavigation(data));
85  
86          // Grab the layout template set in the VelocityPage.
87          // If null, then use the default layout template
88          // (done by the TemplateInfo object)
89          String templateName = data.getTemplateInfo().getLayoutTemplate();
90  
91          // Set the locale and content type
92          data.getResponse().setLocale(data.getLocale());
93          data.getResponse().setContentType(data.getContentType());
94  
95          log.debug("Now trying to render layout " + templateName);
96  
97          // Finally, generate the layout template and send it to the browser
98          velocityService.handleRequest(context,
99                  prefix + templateName, data.getResponse().getOutputStream());
100     }
101 
102 
103 }