1/*2 * Copyright 2000-2004 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.modules.pages;
1819// Turbine Modules20import org.apache.turbine.modules.pages.DefaultPage;
2122// Turbine Utility Classes23import org.apache.turbine.util.RunData;
24import org.apache.turbine.services.template.TurbineTemplate;
2526// Jetspeed Classes27import org.apache.jetspeed.util.MimeType;
28import org.apache.jetspeed.om.registry.MediaTypeEntry;
29import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
30import org.apache.jetspeed.services.logging.JetspeedLogger;
31import org.apache.jetspeed.services.Registry;
32import org.apache.jetspeed.capability.CapabilityMap;
33import org.apache.jetspeed.services.TemplateLocator;
34import org.apache.jetspeed.services.rundata.JetspeedRunData;
35import org.apache.jetspeed.services.resources.JetspeedResources;
3637/***38 * When building sites using templates, Screens need only be defined39 * for templates which require dynamic (database or object) data.40 *41 * <p>42 *43 * This page can be used on sites where the number of Screens can be44 * much less than the number of templates. The templates can be45 * grouped in directories with common layouts. Screen modules are46 * then expected to be placed in packages corresponding with the47 * templates' directories and follow a specific naming scheme.48 *49 * <p>50 *51 * The template parameter is parsed and and a Screen whose package52 * matches the templates path and shares the same name minus any53 * extension and beginning with a capital letter is searched for. If54 * not found, a Screen in a package matching the template's path with55 * name Default is searched for. If still not found, a Screen with56 * name Default is looked for in packages corresponding to parent57 * directories in the template's path until a match is found.58 *59 * <p>60 *61 * For example if data.getParameters().getString("template") returns62 * /about_us/directions/driving.wm, the search follows63 * about_us.directions.Driving, about_us.directions.Default,64 * about_us.Default, Default, WebMacroSiteScreen (i.e. the default65 * screen set in TurbineResources).66 *67 * <p>68 *69 * Only one Layout module is used, since it is expected that any70 * dynamic content will be placed in navigations and screens. The71 * layout template to be used is found in a similar way to the Screen.72 * For example the following paths will be searched in the layouts73 * subdirectory: /about_us/directions/driving.wm,74 * /about_us/directions/default.wm, /about_us/default.wm, /default.wm,75 * where wm is the value of the template.default.extension property.76 *77 * <p>78 *79 * This approach allows a site with largely static content to be80 * updated and added to regularly by those with little Java81 * experience.82 *83 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>84 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>85 * @author <a href="mailto:paulsp@apache.org">Paul Spencer</a>86 * @version $Id: JetspeedTemplatePage.java,v 1.24 2004/02/23 02:59:52 jford Exp $87 */88publicclassJetspeedTemplatePageextends DefaultPage
89 {
90privatestaticint httpLifetime = JetspeedResources.getInt("http.lifetime", -1);
9192/***93 * Static initialization of the logger for this class94 */95privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedTemplatePage.class.getName());
9697/***98 * Works with TemplateService to set up default templates and99 * corresponding class modules.100 *101 * @param data Turbine information.102 * @exception Exception, a generic exception.103 */104protectedvoid doBuildBeforeAction(RunData data) throws Exception
105 {
106switch (httpLifetime)
107 {
108case -1:
109break;
110case 0:
111 data.getResponse().setHeader("Cache-Control", "no-cache");
112 data.getResponse().setHeader("Pragma", "no-cache");
113 data.getResponse().setDateHeader("Expires", 0);
114 data.getResponse().setDateHeader("Last-Modified", System.currentTimeMillis());
115break;
116default:
117 data.getResponse().setHeader("Cache-Control", "max-age=" + httpLifetime);
118 data.getResponse().setDateHeader("Expires", System.currentTimeMillis() + (httpLifetime * 1000));
119 data.getResponse().setDateHeader("Last-Modified", System.currentTimeMillis());
120break;
121 }
122123//Set the ContentType of the page124CapabilityMap cm = ((JetspeedRunData)data).getCapability();
125MimeType mime = cm.getPreferredType();
126 String characterSet = JetspeedResources.getString(JetspeedResources.CONTENT_ENCODING_KEY,"utf-8");
127 data.setContentType( mime.getContentType());
128if ( mime != null )
129 {
130MediaTypeEntry media = (MediaTypeEntry)Registry.getEntry(Registry.MEDIA_TYPE, mime.getCode());
131if ( media != null && media.getCharacterSet() != null)
132 {
133 characterSet = media.getCharacterSet();
134 }
135 }
136 data.setCharSet( characterSet );
137138if (logger.isDebugEnabled())
139 {
140 logger.debug( "JetspeedTemplatePage: Setting type to: " + cm.getPreferredType().getContentType()
141 + "; charset=" + JetspeedResources.getString(JetspeedResources.CONTENT_ENCODING_KEY,"utf-8")
142 );
143 }
144145 }
146147/***148 * Works with TemplateService to set up default templates and149 * corresponding class modules.150 *151 * @param data Turbine information.152 * @exception Exception, a generic exception.153 */154protectedvoid doBuildAfterAction(RunData data) throws Exception
155 {
156// Either template or screen should be guaranteed by the SessionValidator157// It is occasionally better to specify the screen instead of template158// in cases where multiple Screens map to one template. The template159// is hardcoded into the Screen in this instance. In this case this160// action is skipped.161162if (!data.hasScreen())
163 {
164165// if only a screen but no template is specified, then we need to display166// a legacy ecs screen --> screenTemplate = ECS167if ( data.getTemplateInfo().getScreenTemplate() == null )
168 {
169 String screen = TurbineTemplate.getDefaultScreen();
170 data.setScreenTemplate(screen);
171 }
172173 String ext = TurbineTemplate.getDefaultExtension();
174175 String template = data.getTemplateInfo().getScreenTemplate();
176177//save the initial requested template before mangling it178 ((JetspeedRunData)data).setRequestedTemplate(template);
179180if (template.lastIndexOf('.')<0)
181 {
182 template=template+"."+ext;
183 }
184if ( logger.isDebugEnabled() )
185 {
186 logger.debug("JetspeedTemplatePage: requested template = " + template);
187 }
188189// get real path now - this is a fix to get us thru 1.3a2190// when the TurbineTemplateService can locate resources by NLS and mediatype,191// then it can be removed192193 String locatedScreen = TemplateLocator.locateScreenTemplate(data, template);
194 data.setScreenTemplate( locatedScreen );
195if ( logger.isDebugEnabled() )
196 {
197 logger.debug("JetspeedTemplatePage: calculated template = " + locatedScreen);
198 }
199200 String layout = TemplateLocator.locateLayoutTemplate(data, template);
201 data.setLayoutTemplate(layout);
202if ( logger.isDebugEnabled() )
203 {
204 logger.debug("JetspeedTemplatePage: layoutTemplate is finally " + layout);
205 }
206207 String screen = TurbineTemplate.getScreenName(template);
208if (screen == null)
209 {
210thrownew Exception("Screen could not be determined. \n" +
211"No matches were found by TemplateService and the \n" +
212"services.TurbineTemplateService.default.screen \n" +
213"property was not set.");
214 }
215 data.setScreen(screen);
216 }
217 }
218219 }