KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > template > CmsRootTemplate


1 /*
2 * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/template/CmsRootTemplate.java,v $
3 * Date : $Date: 2005/06/21 15:49:58 $
4 * Version: $Revision: 1.4 $
5 *
6 * This library is part of OpenCms -
7 * the Open Source Content Mananagement System
8 *
9 * Copyright (C) 2001 The OpenCms Group
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * For further information about OpenCms, please see the
22 * OpenCms Website: http://www.opencms.org
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */

28
29
30 package com.opencms.template;
31
32 import org.opencms.file.CmsFile;
33 import org.opencms.file.CmsObject;
34 import org.opencms.main.CmsException;
35 import org.opencms.main.CmsLog;
36
37 import com.opencms.core.I_CmsResponse;
38 import com.opencms.legacy.CmsLegacyException;
39 import com.opencms.legacy.CmsXmlTemplateLoader;
40
41 import java.util.Hashtable JavaDoc;
42
43 /**
44  * Represents an "empty" page or screen that should be filled with
45  * the content of a master template.<p>
46  *
47  * @author Alexander Lucas
48  * @version $Revision: 1.4 $ $Date: 2005/06/21 15:49:58 $
49  *
50  * @deprecated Will not be supported past the OpenCms 6 release.
51  */

52 public class CmsRootTemplate {
53
54     /**
55      * Gets the processed content of the requested master template by calling
56      * the given template class.
57      * <P>
58      * If the result is cacheable, the complete output will be stored
59      * in the template cache for later re-use.
60      *
61      * @param cms CmsObject object for accessing system resources.
62      * @param templateClass Instance of the template class to be called.
63      * @param masterTemplate CmsFile object of the master template file.
64      * @param cache templateCache to be used.
65      * @param parameters Hashtable with all template class parameters.
66      *
67      * @return Byte array containing the results of the master template.
68      *
69      * @throws CmsException if something goes wrong
70      */

71     public byte[] getMasterTemplate(CmsObject cms, I_CmsTemplate templateClass, CmsFile masterTemplate, com.opencms.template.I_CmsTemplateCache cache, Hashtable JavaDoc parameters) throws CmsException {
72         byte[] result;
73
74         String JavaDoc masterTemplateUri = cms.getSitePath(masterTemplate);
75         // Collect cache directives from subtemplates
76
CmsCacheDirectives cd = templateClass.collectCacheDirectives(cms, masterTemplateUri, com.opencms.core.I_CmsConstants.C_ROOT_TEMPLATE_NAME, parameters, null);
77
78         /*System.err.println("******************************************************************");
79         System.err.println("* Cache directives Summary");
80         System.err.println("* File : " + cms.readPath(masterTemplate));
81         System.err.println("* Class : " + templateClass.getClass());
82         System.err.println("* internal cacheable : " + (cd.isInternalCacheable()?"true":"false"));
83         System.err.println("* proxy public cacheable : " + (cd.isProxyPublicCacheable()?"true":"false"));
84         System.err.println("* proxy private cacheable : " + (cd.isProxyPrivateCacheable()?"true":"false"));
85         System.err.println("* exportable cacheable : " + (cd.isExportable()?"true":"false"));
86         System.err.println("* streamable : " + (cd.isStreamable()?"true":"false"));
87         System.err.println("******************************************************************");
88         */

89
90         Object JavaDoc cacheKey = templateClass.getKey(cms, masterTemplateUri, parameters, null);
91         boolean cacheable = cd.isInternalCacheable();
92
93         I_CmsResponse resp = CmsXmlTemplateLoader.getResponse(cms.getRequestContext());
94
95         // was there already a cache-control header set?
96
if (!resp.containsHeader("Cache-Control")) {
97
98             // only if the resource is cacheable and if the current project is online,
99
// then the browser may cache the resource
100
if (cd.isProxyPublicCacheable() || cd.isProxyPrivateCacheable()) {
101
102                 // set max-age to 5 minutes. In this time a proxy may cache this content.
103
resp.setHeader("Cache-Control", "max-age=300");
104                 if (cd.isProxyPrivateCacheable()) {
105                     resp.addHeader("Cache-Control", "private");
106                 }
107
108
109            } else {
110                 // set the http-header to pragma no-cache.
111
//HTTP 1.1
112
resp.setHeader("Cache-Control", "no-cache");
113                 //HTTP 1.0
114
resp.setHeader("Pragma", "no-cache");
115             }
116         }
117
118         if (cacheable && cache.has(cacheKey) && !templateClass.shouldReload(cms, masterTemplateUri, com.opencms.core.I_CmsConstants.C_ROOT_TEMPLATE_NAME, parameters, null)) {
119             result = cache.get(cacheKey);
120         } else {
121             try {
122                 result = templateClass.getContent(cms, masterTemplateUri, com.opencms.core.I_CmsConstants.C_ROOT_TEMPLATE_NAME, parameters);
123             } catch (CmsException e) {
124                 cache.clearCache(cacheKey);
125                 if (CmsLog.getLog(this).isWarnEnabled() && (e instanceof CmsLegacyException) && (((CmsLegacyException)e).getType() == CmsLegacyException.C_NO_USER)) {
126                     
127                     CmsLog.getLog(this).warn("Could not get contents of master template " + masterTemplate.getName(), e);
128                 }
129                 throw e;
130             }
131             if (cacheable) {
132                 cache.put(cacheKey, result);
133             }
134         }
135         return result;
136     }
137 }
138
Popular Tags