View Javadoc
1   package org.apache.turbine.modules.layouts;
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.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.turbine.TurbineConstants;
25  import org.apache.turbine.annotation.TurbineService;
26  import org.apache.turbine.modules.Layout;
27  import org.apache.turbine.modules.pages.DefaultPage;
28  import org.apache.turbine.pipeline.PipelineData;
29  import org.apache.turbine.services.velocity.VelocityService;
30  import org.apache.turbine.util.RunData;
31  import org.apache.turbine.util.template.TemplateNavigation;
32  import org.apache.turbine.util.template.TemplateScreen;
33  import org.apache.velocity.context.Context;
34  
35  /**
36   * This Layout module is Turbine 2.3.3 VelocityDirectLayout (same package)
37   * with methods added for {@link PipelineData}. It is used in Jetspeed-1 portal.
38   *
39   * By using this layout any view write will immediately call the provided print writer {@link RunData#getOut()} and
40   * the HTTP servlet response will be flushed and set the committed flag. This means of course
41   * no change to the HTTP response header will be possible afterwards. By setting the {@link RunData#setAction(String)} in the request
42   * (not only the model, but also) additional response headers could be set, cft. {@link DefaultPage#doBuild(PipelineData)}.
43   *
44   * @author <a href="mailto:raphael@apache.org">Raphaƫl Luta</a>
45   * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
46   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
47   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
48   * @version $Id$
49   */
50  public class VelocityCachedLayout
51      extends Layout
52  {
53      /** Logging */
54      private static Log log = LogFactory.getLog(VelocityCachedLayout.class);
55  
56      /** The prefix for lookup up layout pages */
57      private String prefix = getPrefix() + "/";
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.getOut());
100     }
101 }
102