KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > template > cache > CmsUri


1 /*
2 * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/template/cache/CmsUri.java,v $
3 * Date : $Date: 2005/06/27 23:22:30 $
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 package com.opencms.template.cache;
30
31 import org.opencms.file.CmsObject;
32 import org.opencms.main.CmsException;
33 import org.opencms.workplace.editors.CmsDefaultPageEditor;
34
35 import com.opencms.core.I_CmsResponse;
36 import com.opencms.legacy.CmsLegacyException;
37 import com.opencms.legacy.CmsXmlTemplateLoader;
38 import com.opencms.template.CmsCacheDirectives;
39
40 import java.util.Enumeration JavaDoc;
41 import java.util.Hashtable JavaDoc;
42
43 /**
44  * An instance of CmsUri represents an requestable ressource in the OpenCms
45  * element cache area. It points to the starting element and handles the access-
46  * checks to this ressource in a simple way.
47  *
48  * If access is granted for the current user it starts the startingElement to
49  * process the content of this ressource.
50  *
51  * @author Andreas Schouten
52  *
53  * @deprecated Will not be supported past the OpenCms 6 release.
54  */

55 public class CmsUri {
56
57     /**
58      * The Key to the Element used to start the contentgeneration for
59      * this Uri.
60      */

61     private CmsElementDescriptor m_startingElement;
62
63     /**
64      * A Vector with Element Definitions. For normal URI's this Vector contains
65      * only a definition for the body-element.
66      */

67     private CmsElementDefinitionCollection m_elementDefinitions = null;
68
69     /**
70      * Constructor.
71      *
72      * @param startingElement the Element to start the contentgenerating for this uri.
73      * @param readAccessGroup the Group that can read the uri.
74      * @param def a content-definition for the an element (normaly body).
75      */

76     public CmsUri(CmsElementDescriptor startingElement, CmsElementDefinition def,
77         boolean https){
78         this(startingElement, new CmsElementDefinitionCollection());
79         m_elementDefinitions.add(def);
80     }
81
82     /**
83      * Constructor.
84      *
85      * @param startingElement the Element to start the contentgenerating for this uri.
86      * @param readAccessGroup the Group that can read the uri.
87      * @param definitions a vector of definitions for elements.
88      */

89     public CmsUri(CmsElementDescriptor startingElement, CmsElementDefinitionCollection definitions) {
90         m_startingElement = startingElement;
91         m_elementDefinitions = definitions;
92     }
93
94     public byte[] callCanonicalRoot(CmsElementCache elementCache, CmsObject cms, Hashtable JavaDoc parameters) throws CmsException {
95         // check for read access
96
checkReadAccess(cms);
97         // get the startelement. If it is not there it will be created.
98
A_CmsElement elem = elementCache.getElementLocator().get(cms, m_startingElement, parameters);
99
100         if(elem == null){
101             throw new CmsLegacyException("Couldn't create start element for this uri, have a look at the log file for details.");
102         }
103         // put the elementdefinition infos in the parameter
104
try {
105             Enumeration JavaDoc els = m_elementDefinitions.getAllElementNames();
106             while(els.hasMoreElements()){
107                 String JavaDoc currentElement = (String JavaDoc)els.nextElement();
108                 CmsElementDefinition eldef = m_elementDefinitions.get(currentElement);
109                 parameters.put(currentElement +"._TEMPLATE_", eldef.getTemplateName());
110                 parameters.put(currentElement +"._CLASS_", eldef.getClassName());
111                 if(eldef.getTemplateSelector()!= null) {
112                     parameters.put(currentElement +"._TEMPLATESELECTOR_", eldef.getTemplateSelector());
113                 } else {
114                     parameters.put(currentElement +"._TEMPLATESELECTOR_", "default");
115                 }
116             }
117         } catch(NullPointerException JavaDoc exc) {
118             // no body-element found - ignoring
119
}
120
121         // check the proxistuff and set the response header
122
CmsCacheDirectives proxySettings = new CmsCacheDirectives(true);
123         elem.checkProxySettings(cms, proxySettings, parameters);
124
125         // now for the subelements
126
if(m_elementDefinitions != null){
127             Enumeration JavaDoc elementNames = m_elementDefinitions.getAllElementNames();
128             while(elementNames.hasMoreElements()){
129                 String JavaDoc name = (String JavaDoc)elementNames.nextElement();
130                 CmsElementDefinition currentDef = m_elementDefinitions.get(name);
131                 
132                 if (CmsDefaultPageEditor.XML_BODY_ELEMENT.equalsIgnoreCase(name)) {
133                     // need to check for the body template here so that non-XMLTemplate templates
134
// like JSPs know where to find the body defined in the XMLTemplate
135
if (currentDef.getTemplateName() != null) {
136                         cms.getRequestContext().setAttribute(CmsDefaultPageEditor.XML_BODY_ELEMENT, currentDef.getTemplateName());
137                     }
138                 }
139                                 
140                 A_CmsElement currentEle = elementCache.getElementLocator().get(
141                                         cms, new CmsElementDescriptor(currentDef.getClassName(),
142                                         currentDef.getTemplateName()), parameters);
143                 if(currentEle == null){
144                     throw new CmsLegacyException("Couldn't create element '"+name+"', have a look at the log file for details.");
145                 }
146                 currentEle.checkProxySettings(cms, proxySettings, parameters);
147             }
148         }
149
150         I_CmsResponse resp = CmsXmlTemplateLoader.getResponse(cms.getRequestContext());
151         // was there already a cache-control header set?
152
if(!resp.containsHeader("Cache-Control")) {
153             // only if the resource is cacheable and if the current project is online,
154
// then the browser may cache the resource
155
if(proxySettings.isProxyPrivateCacheable()
156                         && cms.getRequestContext().currentProject().getId() == org.opencms.file.CmsProject.ONLINE_PROJECT_ID){
157                 // set max-age to 5 minutes. In this time a proxy may cache this content.
158
resp.setHeader("Cache-Control", "max-age=300");
159                 if(!proxySettings.isProxyPublicCacheable()){
160                     resp.addHeader("Cache-Control", "private");
161                 }
162            }else{
163                 // set the http-header to pragma no-cache.
164
//HTTP 1.1
165
resp.setHeader("Cache-Control", "no-cache");
166                 //HTTP 1.0
167
resp.setHeader("Pragma", "no-cache");
168             }
169         }
170
171
172         return elem.getContent(elementCache, cms, m_elementDefinitions, com.opencms.core.I_CmsConstants.C_ROOT_TEMPLATE_NAME, parameters, null);
173     }
174     /**
175      * checks the read access.
176      * @param cms The cms Object for reading groups.
177      * @throws CmsException if no read access.
178      */

179     public void checkReadAccess(CmsObject cms) throws CmsException{
180         return;
181 // if (m_readAccessGroup == null || "".equals(m_readAccessGroup )){
182
// // everyone can read this
183
// return;
184
// }
185
// CmsGroup currentGroup = cms.getRequestContext().currentGroup();
186
// if (m_readAccessGroup.equals(currentGroup.getName())){
187
// // easy: same group; access granted
188
// return;
189
// }
190
// // maybe it is an Admin
191
// if(currentGroup.getName().equals(I_CmsConstants.C_GROUP_ADMIN)){
192
// // ok Admins can read everything
193
// return;
194
// }
195
// // limited access and not the same group, but maybe parentgroup?
196
// CmsGroup group1 = currentGroup;
197
// CmsGroup group2 = cms.readGroup(m_readAccessGroup);
198
// do{
199
// group1 = cms.getParent(group1.getName());
200
// if(group1 != null && group1.getId() == group2.getId()){
201
// // is parent; access granted
202
// return;
203
// }
204
// }while(group1 != null);
205
//
206
// // maybe an other group of this user has access
207
// Vector allGroups = cms.getGroupsOfUser(cms.getRequestContext().currentUser().getName());
208
// for(int i=0; i<allGroups.size(); i++){
209
// if(m_readAccessGroup.equals(((CmsGroup)allGroups.elementAt(i)).getName())){
210
// return;
211
// }
212
// }
213
// // no way to read this sorry
214
// throw new CmsException(currentGroup.getName()+" has no read access. ",
215
// CmsException.C_ACCESS_DENIED);
216
}
217     
218     public CmsElementDescriptor getElementDescriptor() {
219         return m_startingElement;
220     }
221 }
Popular Tags