View Javadoc

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 at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * 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 and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.modules.pages;
18  
19  // Turbine Modules
20  import org.apache.turbine.modules.pages.DefaultPage;
21  
22  // Turbine Utility Classes
23  import org.apache.turbine.util.RunData;
24  import org.apache.turbine.services.template.TurbineTemplate;
25  
26  // Jetspeed Classes
27  import org.apache.jetspeed.util.MimeType;
28  import org.apache.jetspeed.om.registry.MediaTypeEntry;
29  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
30  import org.apache.jetspeed.services.logging.JetspeedLogger;
31  import org.apache.jetspeed.services.Registry;
32  import org.apache.jetspeed.capability.CapabilityMap;
33  import org.apache.jetspeed.services.TemplateLocator;
34  import org.apache.jetspeed.services.rundata.JetspeedRunData;
35  import org.apache.jetspeed.services.resources.JetspeedResources;
36  
37  /***
38   * When building sites using templates, Screens need only be defined
39   * 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 be
44   * much less than the number of templates.  The templates can be
45   * grouped in directories with common layouts.  Screen modules are
46   * then expected to be placed in packages corresponding with the
47   * templates' directories and follow a specific naming scheme.
48   *
49   * <p>
50   *
51   * The template parameter is parsed and and a Screen whose package
52   * matches the templates path and shares the same name minus any
53   * extension and beginning with a capital letter is searched for.  If
54   * not found, a Screen in a package matching the template's path with
55   * name Default is searched for.  If still not found, a Screen with
56   * name Default is looked for in packages corresponding to parent
57   * directories in the template's path until a match is found.
58   *
59   * <p>
60   *
61   * For example if data.getParameters().getString("template") returns
62   * /about_us/directions/driving.wm, the search follows
63   * about_us.directions.Driving, about_us.directions.Default,
64   * about_us.Default, Default, WebMacroSiteScreen (i.e. the default
65   * screen set in TurbineResources).
66   *
67   * <p>
68   *
69   * Only one Layout module is used, since it is expected that any
70   * dynamic content will be placed in navigations and screens.  The
71   * 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 layouts
73   * 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 be
80   * updated and added to regularly by those with little Java
81   * 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   */
88  public class JetspeedTemplatePage extends DefaultPage
89  {
90      private static int httpLifetime = JetspeedResources.getInt("http.lifetime", -1);
91  
92      /***
93       * Static initialization of the logger for this class
94       */    
95      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedTemplatePage.class.getName());     
96      
97      /***
98       * Works with TemplateService to set up default templates and
99       * corresponding class modules.
100      *
101      * @param data Turbine information.
102      * @exception Exception, a generic exception.
103      */
104     protected void doBuildBeforeAction(RunData data) throws Exception
105     {
106         switch (httpLifetime)
107         {
108             case -1:
109                 break;
110             case 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());
115                 break;
116             default:
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());
120                 break;
121         }
122 
123         //Set the ContentType of the page
124         CapabilityMap cm = ((JetspeedRunData)data).getCapability();
125         MimeType mime = cm.getPreferredType();
126         String characterSet = JetspeedResources.getString(JetspeedResources.CONTENT_ENCODING_KEY,"utf-8");
127         data.setContentType( mime.getContentType());
128         if ( mime != null )
129         {
130             MediaTypeEntry media = (MediaTypeEntry)Registry.getEntry(Registry.MEDIA_TYPE, mime.getCode());
131             if ( media != null && media.getCharacterSet() != null)
132             {
133                 characterSet = media.getCharacterSet();
134             }
135         }
136         data.setCharSet( characterSet );
137 
138         if (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         }
144 
145     }
146 
147     /***
148      * Works with TemplateService to set up default templates and
149      * corresponding class modules.
150      *
151      * @param data Turbine information.
152      * @exception Exception, a generic exception.
153      */
154     protected void doBuildAfterAction(RunData data) throws Exception
155     {
156         // Either template or screen should be guaranteed by the SessionValidator
157         // It is occasionally better to specify the screen instead of template
158         // in cases where multiple Screens map to one template.  The template
159         // is hardcoded into the Screen in this instance.  In this case this
160         // action is skipped.
161 
162         if (!data.hasScreen())
163         {
164 
165             // if only a screen but no template is specified, then we need to display
166             // a legacy ecs screen --> screenTemplate = ECS
167             if ( data.getTemplateInfo().getScreenTemplate() == null )
168             {
169                 String screen = TurbineTemplate.getDefaultScreen();
170                 data.setScreenTemplate(screen);
171             }
172 
173             String ext = TurbineTemplate.getDefaultExtension();
174 
175             String template = data.getTemplateInfo().getScreenTemplate();
176 
177             //save the initial requested template before mangling it
178             ((JetspeedRunData)data).setRequestedTemplate(template);
179 
180             if (template.lastIndexOf('.')<0)
181             {
182                 template=template+"."+ext;
183             }
184             if ( logger.isDebugEnabled() )
185             {
186                 logger.debug("JetspeedTemplatePage: requested template = " + template);
187             }
188 
189             // get real path now - this is a fix to get us thru 1.3a2
190             // when the TurbineTemplateService can locate resources by NLS and mediatype,
191             // then it can be removed
192 
193             String locatedScreen = TemplateLocator.locateScreenTemplate(data, template);
194             data.setScreenTemplate( locatedScreen );
195             if ( logger.isDebugEnabled() )
196             {
197                 logger.debug("JetspeedTemplatePage: calculated template = " + locatedScreen);
198             }
199 
200             String layout = TemplateLocator.locateLayoutTemplate(data, template);
201             data.setLayoutTemplate(layout);
202             if ( logger.isDebugEnabled() )
203             {
204                 logger.debug("JetspeedTemplatePage: layoutTemplate is finally " + layout);
205             }
206 
207             String screen = TurbineTemplate.getScreenName(template);
208             if (screen == null)
209             {
210                 throw new 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     }
218 
219 }