[3296] | 1 | ############################################################################## |
---|
| 2 | # |
---|
| 3 | # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved. |
---|
| 4 | # |
---|
| 5 | # This software is subject to the provisions of the Zope Public License, |
---|
| 6 | # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
---|
| 7 | # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
---|
| 8 | # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
| 9 | # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
---|
| 10 | # FOR A PARTICULAR PURPOSE. |
---|
| 11 | # |
---|
| 12 | ############################################################################## |
---|
| 13 | # This code is a modified version of CMFCore's CachingPolicyManager.py |
---|
| 14 | """Caching tool implementation. |
---|
| 15 | |
---|
| 16 | $Id: CachingPolicyManager.py 40138 2005-11-15 17:47:37Z jens $ |
---|
| 17 | """ |
---|
| 18 | |
---|
| 19 | import os |
---|
| 20 | |
---|
| 21 | from Globals import package_home |
---|
| 22 | from AccessControl import ClassSecurityInfo |
---|
| 23 | from Globals import InitializeClass |
---|
| 24 | from Globals import DTMLFile |
---|
| 25 | |
---|
| 26 | from Products.CMFCore import permissions |
---|
| 27 | from Products.CMFCore.utils import getToolByName |
---|
| 28 | from Products.CMFCore.CachingPolicyManager import CachingPolicyManager as CMFCachingPolicyManager |
---|
| 29 | |
---|
| 30 | from Products.CacheSetup.config import CACHE_TOOL_ID |
---|
| 31 | |
---|
| 32 | class CSCachingPolicyManager( CMFCachingPolicyManager ): |
---|
| 33 | """ |
---|
| 34 | Manage the set of CachingPolicy objects for the site; dispatch |
---|
| 35 | to them from skin methods. |
---|
| 36 | """ |
---|
| 37 | |
---|
| 38 | id = 'caching_policy_manager' |
---|
| 39 | meta_type = 'CacheFu Caching Policy Manager' |
---|
| 40 | |
---|
| 41 | security = ClassSecurityInfo() |
---|
| 42 | |
---|
| 43 | security.declareProtected(permissions.ManagePortal, 'manage_cachingPolicies') |
---|
| 44 | manage_cachingPolicies = DTMLFile('cachingPoliciesDummy', os.path.dirname(package_home(globals()))) |
---|
| 45 | |
---|
| 46 | # A new method that replaces getHTTPCachingHeaders |
---|
| 47 | security.declareProtected( permissions.View, 'getHeadersToAddAndRemove' ) |
---|
| 48 | def getHeadersToAddAndRemove( self, content, view_method, keywords, time=None): |
---|
| 49 | """Return a tuple of HTTP caching headers, (headers_to_add, headers_to_remove). |
---|
| 50 | The first item is a list of headers to add to the response in the form |
---|
| 51 | (header name, header value). The second item is a list of headers that |
---|
| 52 | should be removed (before adding).""" |
---|
| 53 | pcs = getToolByName(self, CACHE_TOOL_ID, None) |
---|
| 54 | if pcs is None or not pcs.getEnabled(): |
---|
| 55 | return () |
---|
| 56 | member = pcs.getMember() |
---|
| 57 | request = content.REQUEST |
---|
| 58 | (rule, header_set) = pcs.getRuleAndHeaderSet(request, content, view_method, member) |
---|
| 59 | if header_set: |
---|
| 60 | expr_context = rule._getExpressionContext(request, content, view_method, member) |
---|
| 61 | return header_set.getHeaders(expr_context) |
---|
| 62 | return () |
---|
| 63 | |
---|
| 64 | # |
---|
| 65 | # 'portal_caching' interface methods |
---|
| 66 | # |
---|
| 67 | security.declareProtected( permissions.View, 'getHTTPCachingHeaders' ) |
---|
| 68 | def getHTTPCachingHeaders( self, content, view_method, keywords, time=None): |
---|
| 69 | """Return a list of HTTP caching headers based on 'content', 'view_method', and 'keywords'.""" |
---|
| 70 | hdrs = self.getHeadersToAddAndRemove(content, view_method, keywords, time) |
---|
| 71 | if hdrs: |
---|
| 72 | return hdrs[0] |
---|
| 73 | return () |
---|
| 74 | |
---|
| 75 | security.declareProtected( permissions.View, 'getModTimeAndETag' ) |
---|
| 76 | def getModTimeAndETag( self, content, view_method, keywords, time=None): |
---|
| 77 | """ Return the modification time and ETag for the content object, |
---|
| 78 | view method, and keywords as the tuple (modification_time, etag, |
---|
| 79 | set_last_modified_header), where modification_time is a DateTime, |
---|
| 80 | or None. |
---|
| 81 | """ |
---|
| 82 | pcs = getToolByName(self, CACHE_TOOL_ID, None) |
---|
| 83 | if pcs is None or not pcs.getEnabled(): |
---|
| 84 | return None |
---|
| 85 | member = pcs.getMember() |
---|
| 86 | request = content.REQUEST |
---|
| 87 | (rule, header_set) = pcs.getRuleAndHeaderSet(request, content, view_method, member) |
---|
| 88 | if header_set: |
---|
| 89 | expr_context = rule._getExpressionContext(request, content, view_method, member) |
---|
| 90 | etag = header_set.getEtagValue(expr_context) |
---|
| 91 | mod_time = header_set.getLastModifiedValue(expr_context) |
---|
| 92 | use_mod_time = header_set.getLastModified() |
---|
| 93 | return (mod_time, etag, use_mod_time) |
---|
| 94 | |
---|
| 95 | security.declareProtected( permissions.View, 'getETag' ) |
---|
| 96 | def getETag( self, content, view_method, keywords, time=None): |
---|
| 97 | """ Return the ETag for the content object; ignores getEnable304s setting""" |
---|
| 98 | pcs = getToolByName(self, CACHE_TOOL_ID, None) |
---|
| 99 | if pcs is None or not pcs.getEnabled(): |
---|
| 100 | return None |
---|
| 101 | member = pcs.getMember() |
---|
| 102 | request = content.REQUEST |
---|
| 103 | (rule, header_set) = pcs.getRuleAndHeaderSet(request, content, view_method, member) |
---|
| 104 | if header_set: |
---|
| 105 | expr_context = rule._getExpressionContext(request, content, view_method, member) |
---|
| 106 | etag = header_set.getEtagValue(expr_context) |
---|
| 107 | return etag |
---|
| 108 | |
---|
| 109 | InitializeClass( CSCachingPolicyManager ) |
---|