def modifyStreamingResponse(event): """Invoke the modifyResponse() method of a caching operation, if one can be found, for a streaming response (one using response.write()). """ response = event.response if response is None: return request = getRequest() if request is None: return # Mark the response to allow us to avoid attempting a modify operation # again in the normal hook above alsoProvides(request, IStreamedResponse) published = request.get('PUBLISHED', None) rule, operationName, operation = findOperation(request) if rule is None: return response.setHeader(X_CACHE_RULE_HEADER, rule) logger.debug("Published: %s Ruleset: %s Operation: %s", repr(published), rule, operation) if operation is not None: response.setHeader(X_CACHE_OPERATION_HEADER, operationName) operation.modifyResponse(rule, response)
def mutate(self): request = self.request # Abort if this was a streamed request handled by our event handler # below if IStreamedResponse.providedBy(request): return published = request.get('PUBLISHED', None) rule, operationName, operation = findOperation(request) if rule is None: return request.response.setHeader(X_CACHE_RULE_HEADER, rule) logger.debug( 'Published: %s Ruleset: %s Operation: %s', repr(published), rule, operation ) if operation is not None: request.response.setHeader(X_CACHE_OPERATION_HEADER, operationName) operation.modifyResponse(rule, request.response)
def intercept(event): """Invoke the interceptResponse() method of a caching operation, if one can be found. To properly abort request processing, this will raise an exception. The actual response (typically an empty response) is then set via a view on the exception. We set and lock the response status to avoid defaulting to a 404 exception. """ try: request = event.request published = request.get('PUBLISHED', None) rule, operationName, operation = findOperation(request) if rule is None: return request.response.setHeader(X_CACHE_RULE_HEADER, rule) logger.debug( 'Published: %s Ruleset: %s Operation: %s', repr(published), rule, operation ) if operation is not None: responseBody = operation.interceptResponse(rule, request.response) if responseBody is not None: # Only put this in the response if the operation actually # intercepted something request.response.setHeader( X_CACHE_OPERATION_HEADER, operationName ) # Stop any post-processing, including the operation's response # modification if DISABLE_TRANSFORM_REQUEST_KEY not in request.environ: request.environ[DISABLE_TRANSFORM_REQUEST_KEY] = True # The view is liable to have set a response status. Lock it # now so that it doesn't get set to 500 later. status = request.response.getStatus() if status: request.response.setStatus(status, lock=True) raise Intercepted(status, responseBody) except ConflictError: raise except Intercepted: raise except Exception: logging.exception( 'Swallowed exception in plone.caching IPubAfterTraversal event ' 'handler' )
def intercept(event): """Invoke the interceptResponse() method of a caching operation, if one can be found. To properly abort request processing, this will raise an exception. The actual response (typically an empty response) is then set via a view on the exception. We set and lock the response status to avoid defaulting to a 404 exception. """ try: request = event.request published = request.get('PUBLISHED', None) rule, operationName, operation = findOperation(request) if rule is None: return request.response.setHeader(X_CACHE_RULE_HEADER, rule) logger.debug("Published: %s Ruleset: %s Operation: %s", repr(published), rule, operation) if operation is not None: responseBody = operation.interceptResponse(rule, request.response) if responseBody is not None: # Only put this in the response if the operation actually # intercepted something request.response.setHeader(X_CACHE_OPERATION_HEADER, operationName) # Stop any post-processing, including the operation's response # modification if DISABLE_TRANSFORM_REQUEST_KEY not in request.environ: request.environ[DISABLE_TRANSFORM_REQUEST_KEY] = True # The view is liable to have set a response status. Lock it # now so that it doesn't get set to 500 later. status = request.response.getStatus() if status: request.response.setStatus(status, lock=True) raise Intercepted(status, responseBody) except ConflictError: raise except Intercepted: raise except: logging.exception("Swallowed exception in plone.caching IPubAfterTraversal event handler")
def mutate(self): request = self.request # Abort if this was a streamed request handled by our event handler # below if IStreamedResponse.providedBy(request): return published = request.get('PUBLISHED', None) rule, operationName, operation = findOperation(request) if rule is None: return request.response.setHeader(X_CACHE_RULE_HEADER, rule) logger.debug('Published: %s Ruleset: %s Operation: %s', repr(published), rule, operation) if operation is not None: request.response.setHeader(X_CACHE_OPERATION_HEADER, operationName) operation.modifyResponse(rule, request.response)