1 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 ; 42 43 import javax.servlet.http.HttpServletRequest ; 44 45 53 public abstract class A_CmsTemplate implements I_CmsTemplate { 54 55 70 public boolean isCacheable(CmsObject cms, String templateFile, String elementName, Hashtable parameters, String templateSelector) { 71 try { 72 return cms.getRequestContext().currentProject().isOnlineProject(); 73 } catch (Exception e) { 74 return false; 75 } 76 } 77 78 97 public boolean isProxyPrivateCacheable(CmsObject cms, String templateFile, String elementName, Hashtable parameters, String templateSelector) { 98 boolean result; 99 try { 100 result = isProxyPublicCacheable(cms, templateFile, elementName, parameters, templateSelector) 101 && cms.getRequestContext().currentUser().isGuestUser(); 102 } catch (Exception e) { 103 result = false; 104 } 105 return result; 106 } 107 108 127 public boolean isProxyPublicCacheable(CmsObject cms, String templateFile, String elementName, Hashtable parameters, String templateSelector) { 128 CmsRequestContext reqContext = cms.getRequestContext(); 129 String key = (String )getKey(cms, templateFile, parameters, templateSelector); 130 String projId = "" + reqContext.currentProject().getId() + ":"; 131 String uri = reqContext.getUri(); 132 String 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 162 public boolean isExportable(CmsObject cms, String templateFile, String elementName, Hashtable parameters, String templateSelector) { 163 HttpServletRequest httpReq = CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getOriginalRequest(); 164 String 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 e) { 174 result = false; 175 } 176 return result; 177 } 178 179 203 public boolean isStreamable(CmsObject cms, String templateFile, String elementName, Hashtable parameters, String templateSelector) { 204 return false; 205 } 206 207 220 public CmsCacheDirectives collectCacheDirectives(CmsObject cms, String templateFile, String elementName, Hashtable parameters, String 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 236 protected void debugPrint(String 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 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 259 protected void throwException(String errorMessage) throws CmsException { 260 throwException(errorMessage, CmsLegacyException.C_UNKNOWN_EXCEPTION); 261 } 262 263 271 protected void throwException(String 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 286 protected void throwException(String errorMessage, Exception 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 |