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());
}
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;
}
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;
}
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;
}
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);
}
}
Aggregations