Search in sources :

Example 1 with HttpCacheException

use of com.adobe.acs.commons.httpcache.exception.HttpCacheException in project acs-aem-commons by Adobe-Consulting-Services.

the class AbstractHttpCacheFilter method doFilter.

@SuppressWarnings("squid:S3776")
protected void doFilter(ServletRequest request, ServletResponse response, FilterChain chain, HttpCacheEngine cacheEngine, HttpCacheConfig.FilterScope filterScope) throws IOException, ServletException {
    log.trace("In HttpCache filter.");
    final long start = System.currentTimeMillis();
    final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
    SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;
    HttpCacheConfig cacheConfig = null;
    boolean isResponseCacheable = false;
    try {
        // Get the first accepting cache config, or null if no accepting cacheConfigs can be found.
        cacheConfig = cacheEngine.getCacheConfig(slingRequest, filterScope);
        // An accepting cacheConfig must exist and all cache rules must be met.
        if (cacheConfig != null && cacheEngine.isRequestCacheable(slingRequest, cacheConfig)) {
            // Check if cached response available for this request.
            if (cacheEngine.isCacheHit(slingRequest, cacheConfig)) {
                // Deliver the response from cache.
                if (cacheEngine.deliverCacheContent(slingRequest, slingResponse, cacheConfig)) {
                    if (log.isDebugEnabled()) {
                        log.debug("Delivered cached request [ {} ] in {} ms", slingRequest.getResource().getPath(), System.currentTimeMillis() - start);
                    }
                    return;
                }
            } else {
                // Mark the request as cacheable once processed.
                isResponseCacheable = true;
                // Wrap the response
                slingResponse = cacheEngine.wrapResponse(slingRequest, slingResponse, cacheConfig);
            }
        }
    } catch (HttpCacheException e) {
        log.error("HttpCache exception while dealing with request. Passed on the control to filter chain.", e);
    }
    // Pass on the request to filter chain.
    chain.doFilter(request, slingResponse);
    try {
        // If the request has the attribute marked, cache the response.
        if (isResponseCacheable) {
            cacheEngine.cacheResponse(slingRequest, slingResponse, cacheConfig);
        }
        if (log.isTraceEnabled()) {
            log.trace("Delivered un-cached request [ {} ] in {} ms", slingRequest.getResource().getPath(), System.currentTimeMillis() - start);
        }
    } catch (HttpCacheException e) {
        log.error("HttpCache exception while dealing with response. Returned the filter chain response", e);
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) HttpCacheConfig(com.adobe.acs.commons.httpcache.config.HttpCacheConfig) HttpCacheException(com.adobe.acs.commons.httpcache.exception.HttpCacheException) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Aggregations

HttpCacheConfig (com.adobe.acs.commons.httpcache.config.HttpCacheConfig)1 HttpCacheException (com.adobe.acs.commons.httpcache.exception.HttpCacheException)1 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)1