Search in sources :

Example 1 with HttpCacheConfig

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

the class HttpCacheEngineImpl method bindHttpCacheConfig.

// -------------------<OSGi specific methods>---------------//
/**
 * Binds cache config. Cache config could come and go at run time.
 *
 * @param cacheConfig
 * @param configs
 */
protected void bindHttpCacheConfig(final HttpCacheConfig cacheConfig, final Map<String, Object> configs) {
    // Validate cache config object
    if (!cacheConfig.isValid()) {
        log.info("Http cache config rejected as the request uri is absent.");
        return;
    }
    // Check if the same object is already there in the map.
    if (cacheConfigs.contains(cacheConfig)) {
        log.trace("Http cache config object already exists in the cacheConfigs map and hence ignored.");
        return;
    }
    // Sort cacheConfigs by order
    final CopyOnWriteArrayList<HttpCacheConfig> tmp = new CopyOnWriteArrayList<HttpCacheConfig>(this.cacheConfigs);
    tmp.add(cacheConfig);
    Collections.sort(tmp, new HttpCacheConfigComparator());
    this.cacheConfigs = tmp;
    this.cacheConfigConfigs.put(cacheConfig, configs);
    log.debug("Total number of cache configs added: {}", cacheConfigs.size());
}
Also used : HttpCacheConfigComparator(com.adobe.acs.commons.httpcache.config.impl.HttpCacheConfigComparator) HttpCacheConfig(com.adobe.acs.commons.httpcache.config.HttpCacheConfig) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 2 with HttpCacheConfig

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

the class InvalidateByCacheConfigVisitorTest method getInvalidateByCacheConfigVisitor.

private InvalidateByCacheConfigVisitor getInvalidateByCacheConfigVisitor(long delta, boolean knows) throws Exception {
    final DynamicClassLoaderManager dclm = mock(DynamicClassLoaderManager.class);
    final CacheKey cacheKey = mockCacheKey();
    final HttpCacheConfig cacheConfig;
    if (knows) {
        cacheConfig = mockCacheStore(cacheKey);
    } else {
        cacheConfig = mockCacheStore();
    }
    final InvalidateByCacheConfigVisitor visitor = new InvalidateByCacheConfigVisitor(11, delta, cacheConfig, dclm);
    final InvalidateByCacheConfigVisitor spy = spy(visitor);
    when(spy, "getCacheKey", any(Node.class)).thenReturn(mockCacheKey());
    return spy;
}
Also used : HttpCacheConfig(com.adobe.acs.commons.httpcache.config.HttpCacheConfig) DynamicClassLoaderManager(org.apache.sling.commons.classloader.DynamicClassLoaderManager) CacheKey(com.adobe.acs.commons.httpcache.keys.CacheKey)

Example 3 with HttpCacheConfig

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

the class InvalidateByCacheConfigVisitorTest method mockCacheStore.

private HttpCacheConfig mockCacheStore() throws HttpCacheKeyCreationException {
    final HttpCacheConfig config = mock(HttpCacheConfig.class);
    when(config.knows(any(CacheKey.class))).thenReturn(false);
    return config;
}
Also used : HttpCacheConfig(com.adobe.acs.commons.httpcache.config.HttpCacheConfig) CacheKey(com.adobe.acs.commons.httpcache.keys.CacheKey)

Example 4 with HttpCacheConfig

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

the class InvalidateByCacheConfigVisitorTest method mockCacheStore.

private HttpCacheConfig mockCacheStore(final CacheKey key) throws HttpCacheKeyCreationException {
    final HttpCacheConfig config = mock(HttpCacheConfig.class);
    when(config.knows(any(CacheKey.class))).thenReturn(true);
    return config;
}
Also used : HttpCacheConfig(com.adobe.acs.commons.httpcache.config.HttpCacheConfig) CacheKey(com.adobe.acs.commons.httpcache.keys.CacheKey)

Example 5 with HttpCacheConfig

use of com.adobe.acs.commons.httpcache.config.HttpCacheConfig 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)6 CacheKey (com.adobe.acs.commons.httpcache.keys.CacheKey)3 HttpCacheConfigComparator (com.adobe.acs.commons.httpcache.config.impl.HttpCacheConfigComparator)1 HttpCacheException (com.adobe.acs.commons.httpcache.exception.HttpCacheException)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)1 CompositeType (javax.management.openmbean.CompositeType)1 TabularDataSupport (javax.management.openmbean.TabularDataSupport)1 TabularType (javax.management.openmbean.TabularType)1 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)1 DynamicClassLoaderManager (org.apache.sling.commons.classloader.DynamicClassLoaderManager)1