001package org.apache.turbine.modules.layouts; 002 003 004/* 005 * Licensed to the Apache Software Foundation (ASF) under one 006 * or more contributor license agreements. See the NOTICE file 007 * distributed with this work for additional information 008 * regarding copyright ownership. The ASF licenses this file 009 * to you under the Apache License, Version 2.0 (the 010 * "License"); you may not use this file except in compliance 011 * with the License. You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, 016 * software distributed under the License is distributed on an 017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 018 * KIND, either express or implied. See the License for the 019 * specific language governing permissions and limitations 020 * under the License. 021 */ 022 023 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026import org.apache.turbine.TurbineConstants; 027import org.apache.turbine.annotation.TurbineService; 028import org.apache.turbine.modules.Layout; 029import org.apache.turbine.pipeline.PipelineData; 030import org.apache.turbine.services.velocity.VelocityService; 031import org.apache.turbine.util.RunData; 032import org.apache.turbine.util.template.TemplateNavigation; 033import org.apache.turbine.util.template.TemplateScreen; 034import org.apache.velocity.context.Context; 035 036/** 037 * This Layout module allows Velocity templates 038 * to be used as layouts. It will stream directly the output of 039 * the layout and navigation templates to the output writer without 040 * using a screen. Use this if you have a large page to output 041 * and won't buffer it in the memory. 042 * 043 * @author <a href="mailto:raphael@apache.org">Raphaël Luta</a> 044 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a> 045 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a> 046 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> 047 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a> 048 * @version $Id: VelocityDirectLayout.java 1706239 2015-10-01 13:18:35Z tv $ 049 */ 050public class VelocityDirectLayout 051 extends Layout 052{ 053 /** Logging */ 054 private static Log log = LogFactory.getLog(VelocityDirectLayout.class); 055 056 /** The prefix for lookup up layout pages */ 057 private String prefix = Layout.PREFIX + "/"; 058 059 /** Injected service instance */ 060 @TurbineService 061 private VelocityService velocityService; 062 063 /** 064 * Method called by LayoutLoader. 065 * 066 * 067 * @param pipelineData PipelineData 068 * @throws Exception generic exception 069 */ 070 @Override 071 public void doBuild(PipelineData pipelineData) 072 throws Exception 073 { 074 RunData data = getRunData(pipelineData); 075 // Get the context needed by Velocity. 076 Context context = velocityService.getContext(pipelineData); 077 078 // variable for the screen in the layout template 079 context.put(TurbineConstants.SCREEN_PLACEHOLDER, 080 new TemplateScreen(data)); 081 082 // variable to reference the navigation screen in the layout template 083 context.put(TurbineConstants.NAVIGATION_PLACEHOLDER, 084 new TemplateNavigation(data)); 085 086 // Grab the layout template set in the VelocityPage. 087 // If null, then use the default layout template 088 // (done by the TemplateInfo object) 089 String templateName = data.getTemplateInfo().getLayoutTemplate(); 090 091 // Set the locale and content type 092 data.getResponse().setLocale(data.getLocale()); 093 data.getResponse().setContentType(data.getContentType()); 094 095 log.debug("Now trying to render layout " + templateName); 096 097 // Finally, generate the layout template and send it to the browser 098 velocityService.handleRequest(context, 099 prefix + templateName, data.getResponse().getOutputStream()); 100 } 101 102 103}