KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/template/A_CmsTemplate.java,v $
3 * Date : $Date: 2005/06/27 23:22:20 $
4 * Version: $Revision: 1.5 $
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 package com.opencms.template;
30
31 import org.opencms.file.CmsFile;
32 import org.opencms.file.CmsObject;
33 import org.opencms.file.CmsRequestContext;
34 import org.opencms.file.CmsResource;
35 import org.opencms.main.CmsException;
36 import org.opencms.main.CmsLog;
37
38 import com.opencms.legacy.CmsLegacyException;
39 import com.opencms.legacy.CmsXmlTemplateLoader;
40
41 import java.util.Hashtable JavaDoc;
42
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44
45 /**
46  * Abstract template class. Contains all commonly used methods for handling cache properties.
47  *
48  * @author Alexander Lucas
49  * @version $Revision: 1.5 $ $Date: 2005/06/27 23:22:20 $
50  *
51  * @deprecated Will not be supported past the OpenCms 6 release.
52  */

53 public abstract class A_CmsTemplate implements I_CmsTemplate {
54
55     /**
56      * Indicates if the results of this class are cacheable in the internal caches.
57      * By default all resources in the online project may be stored in the internal
58      * cache.
59      * <P>
60      * Complex classes that are able to include other subtemplates
61      * have to check the cacheability of their subclasses here!
62      *
63      * @param cms CmsObject Object for accessing system resources
64      * @param templateFile Filename of the template file
65      * @param elementName Element name of this template in our parent template.
66      * @param parameters Hashtable with all template class parameters.
67      * @param templateSelector template section that should be processed.
68      * @return <EM>true</EM> if cacheable, <EM>false</EM> otherwise.
69      */

70     public boolean isCacheable(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName, Hashtable JavaDoc parameters, String JavaDoc templateSelector) {
71         try {
72             return cms.getRequestContext().currentProject().isOnlineProject();
73         } catch (Exception JavaDoc e) {
74             return false;
75         }
76     }
77
78     /**
79      * Indicates if the results of this class may be cached by private proxy caches (browsers).
80      * <P>
81      * Default conditions are:
82      * <ul>
83      * <li>Resource is cacheable in the internal cache</li>
84       * <li>Caching key only consists of the URL</li>
85      * </ul>
86      * <P>
87      * Complex classes that are able to include other subtemplates
88      * have to check the cacheability of their subclasses here!
89      *
90      * @param cms CmsObject Object for accessing system resources
91      * @param templateFile Filename of the template file
92      * @param elementName Element name of this template in our parent template.
93      * @param parameters Hashtable with all template class parameters.
94      * @param templateSelector template section that should be processed.
95      * @return <EM>true</EM> if cacheable, <EM>false</EM> otherwise.
96      */

97     public boolean isProxyPrivateCacheable(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName, Hashtable JavaDoc parameters, String JavaDoc templateSelector) {
98         boolean result;
99         try {
100             result = isProxyPublicCacheable(cms, templateFile, elementName, parameters, templateSelector)
101             && cms.getRequestContext().currentUser().isGuestUser();
102         } catch (Exception JavaDoc e) {
103             result = false;
104         }
105         return result;
106     }
107
108     /**
109      * Indicates if the results of this class may be cached by public proxy caches.
110      * <P>
111      * Default conditions are:
112      * <ul>
113      * <li>Resource may be cached by private proxies</li>
114      * <li>Current user is Guest (otherwise privat information may be stored)</li>
115      * </ul>
116      * <P>
117      * Complex classes that are able to include other subtemplates
118      * have to check the cacheability of their subclasses here!
119      *
120      * @param cms CmsObject Object for accessing system resources
121      * @param templateFile Filename of the template file
122      * @param elementName Element name of this template in our parent template.
123      * @param parameters Hashtable with all template class parameters.
124      * @param templateSelector template section that should be processed.
125      * @return <EM>true</EM> if cacheable, <EM>false</EM> otherwise.
126      */

127     public boolean isProxyPublicCacheable(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName, Hashtable JavaDoc parameters, String JavaDoc templateSelector) {
128         CmsRequestContext reqContext = cms.getRequestContext();
129         String JavaDoc key = (String JavaDoc)getKey(cms, templateFile, parameters, templateSelector);
130         String JavaDoc projId = "" + reqContext.currentProject().getId() + ":";
131         String JavaDoc uri = reqContext.getUri();
132         String JavaDoc uri2 = null;
133         if (uri != null && uri.indexOf("?") > 1) {
134             uri2 = uri.substring(0, uri.indexOf("?"));
135         }
136
137         boolean result = (key.equals(uri) || key.equals(uri2) || key.equals(templateFile)
138                            || key.equals(projId + uri) || key.equals(projId + uri2) || key.equals(projId + templateFile));
139         return result;
140     }
141
142     /**
143      * Indicates if the results of this class are "static" and may be exported.
144      * <P>
145      * Default conditions are:
146      * <ul>
147      * <li>Resource may be cached by public proxies</li>
148      * <li>There are no parameters in the URL</li>
149      * <li>The resource's internal flag must not be set</li>
150      * </ul>
151      * <P>
152      * Complex classes that are able to include other subtemplates
153      * have to check the export ability of their subclasses here!
154      *
155      * @param cms CmsObject Object for accessing system resources
156      * @param templateFile Filename of the template file
157      * @param elementName Element name of this template in our parent template.
158      * @param parameters Hashtable with all template class parameters.
159      * @param templateSelector template section that should be processed.
160      * @return <EM>true</EM> if exportable, <EM>false</EM> otherwise.
161      */

162     public boolean isExportable(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName, Hashtable JavaDoc parameters, String JavaDoc templateSelector) {
163         HttpServletRequest JavaDoc httpReq = CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest();
164         String JavaDoc queryString = "";
165         if (httpReq != null) {
166             queryString = httpReq.getQueryString();
167         }
168         boolean result = isProxyPrivateCacheable(cms, templateFile, elementName, parameters, templateSelector)
169             && (queryString == null || "".equals(queryString));
170         try {
171             CmsFile file = cms.readFile(templateFile);
172             result = result && (file.getFlags() & CmsResource.FLAG_INTERNAL) != CmsResource.FLAG_INTERNAL;
173         } catch (Exception JavaDoc e) {
174             result = false;
175         }
176         return result;
177     }
178
179     /**
180      * Indicates if the current template class is able to stream it's results
181      * directly to the response oputput stream.
182      * <P>
183      * Classes must not set this feature, if they might throw special
184      * exception that cause HTTP errors (e.g. 404/Not Found), or if they
185      * might send HTTP redirects.
186      * <p>
187      * If a class sets this feature, it has to check the
188      * isStreaming() property of the RequestContext. If this is set
189      * to <code>true</code> the results must be streamed directly
190      * to the output stream. If it is <code>false</code> the results
191      * must not be streamed.
192      * <P>
193      * Complex classes that are able to include other subtemplates
194      * have to check the streaming ability of their subclasses here!
195      *
196      * @param cms CmsObject Object for accessing system resources
197      * @param templateFile Filename of the template file
198      * @param elementName Element name of this template in our parent template.
199      * @param parameters Hashtable with all template class parameters.
200      * @param templateSelector template section that should be processed.
201      * @return <EM>true</EM> if this class may stream it's results, <EM>false</EM> otherwise.
202      */

203     public boolean isStreamable(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName, Hashtable JavaDoc parameters, String JavaDoc templateSelector) {
204         return false;
205     }
206
207     /**
208      * Collect caching informations from the current template class.
209      * <P>
210      * Complex classes that are able to include other subtemplates
211      * have to check the streaming ability of their subclasses here!
212      *
213      * @param cms CmsObject Object for accessing system resources
214      * @param templateFile Filename of the template file
215      * @param elementName Element name of this template in our parent template.
216      * @param parameters Hashtable with all template class parameters.
217      * @param templateSelector template section that should be processed.
218      * @return <EM>true</EM> if this class may stream it's results, <EM>false</EM> otherwise.
219      */

220     public CmsCacheDirectives collectCacheDirectives(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName, Hashtable JavaDoc parameters, String JavaDoc templateSelector) {
221         boolean isCacheable = isCacheable(cms, templateFile, elementName, parameters, templateSelector);
222         boolean isProxyPublicCacheable = isProxyPublicCacheable(cms, templateFile, elementName, parameters, templateSelector);
223         boolean isProxyPrivateCacheable = isProxyPrivateCacheable(cms, templateFile, elementName, parameters, templateSelector);
224         boolean isExportable = isExportable(cms, templateFile, elementName, parameters, templateSelector);
225         boolean isStreamable = isStreamable(cms, templateFile, elementName, parameters, templateSelector);
226         CmsCacheDirectives result = new CmsCacheDirectives(isCacheable, isProxyPrivateCacheable, isProxyPublicCacheable, isExportable, isStreamable);
227         return result;
228     }
229     
230     /**
231      * This is for debugging out.put generation.<p>
232      *
233      * @param s String to print
234      * @param i type of cachekey
235      */

236     protected void debugPrint(String JavaDoc s, int i) {
237         System.err.print("* " + s);
238         for (int j=0; j<(15-s.length()); j++) {
239             System.err.print(" ");
240         }
241
242         //INT PUB PRV EXP STR
243
System.err.print(" " + ((i & 1) == 1?"X":" ") + " ");
244         System.err.print(" " + ((i & 2) == 2?"X":" ") + " ");
245         System.err.print(" " + ((i & 4) == 4?"X":" ") + " ");
246         System.err.print(" " + ((i & 8) == 8?"X":" ") + " ");
247         System.err.print(" " + ((i & 16) == 16?"X":" ") + " ");
248     }
249
250
251
252     /**
253      * Help method that handles any occuring error by writing
254      * an error message to the OpenCms logfile and throwing a
255      * CmsException of the type "unknown".
256      * @param errorMessage String with the error message to be printed.
257      * @throws CmsException if something goes wrong
258      */

259     protected void throwException(String JavaDoc errorMessage) throws CmsException {
260         throwException(errorMessage, CmsLegacyException.C_UNKNOWN_EXCEPTION);
261     }
262
263     /**
264      * Help method that handles any occuring error by writing
265      * an error message to the OpenCms logfile and throwing a
266      * CmsException of the given type.
267      * @param errorMessage String with the error message to be printed.
268      * @param type Type of the exception to be thrown.
269      * @throws CmsLegacyException if something goes wrong
270      */

271     protected void throwException(String JavaDoc errorMessage, int type) throws CmsLegacyException {
272         if (CmsLog.getLog(this).isErrorEnabled()) {
273             CmsLog.getLog(this).error(errorMessage);
274         }
275         throw new CmsLegacyException(errorMessage, type);
276     }
277
278     /**
279      * Help method that handles any occuring error by writing
280      * an error message to the OpenCms logfile and re-throwing a
281      * caught exception.
282      * @param errorMessage String with the error message to be printed.
283      * @param e Exception to be re-thrown.
284      * @throws CmsException if something goes wrong
285      */

286     protected void throwException(String JavaDoc errorMessage, Exception JavaDoc e) throws CmsException {
287         if (CmsLog.getLog(this).isErrorEnabled()) {
288             CmsLog.getLog(this).error(errorMessage, e);
289         }
290         if (e instanceof CmsException) {
291             throw (CmsException)e;
292         } else {
293             throw new CmsLegacyException(errorMessage, CmsLegacyException.C_UNKNOWN_EXCEPTION, e);
294         }
295     }
296 }
297
Popular Tags