ExpiresCacheHandler.java 2.48 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.servlet.http.HttpServletRequest
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  org.apache.felix.scr.annotations.Service
 *  org.osgi.service.component.ComponentContext
 */
package com.adobe.granite.httpcache.impl;

import com.adobe.granite.httpcache.api.CacheHandler;
import com.adobe.granite.httpcache.api.CacheStore;
import com.adobe.granite.httpcache.api.Headers;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.osgi.service.component.ComponentContext;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
@Component
@Service(value={CacheHandler.class})
public class ExpiresCacheHandler
implements CacheHandler {
    private static final long MINUTE_IN_MS = 60000;
    @Reference(policy=ReferencePolicy.DYNAMIC)
    private volatile CacheStore store;

    @Activate
    public void activate(ComponentContext context, Map<String, Object> properties) {
    }

    @Override
    public CacheHandler.TRI_STATE onReceive(HttpServletRequest request) {
        return CacheHandler.TRI_STATE.NEXT_HANDLER;
    }

    @Override
    public CacheHandler.TRI_STATE onDeliver(String key, Headers headers, HttpServletResponse response) {
        long expires = headers.getDateHeader("Expires");
        if (expires < System.currentTimeMillis()) {
            this.store.evict(key);
            return CacheHandler.TRI_STATE.NO;
        }
        return CacheHandler.TRI_STATE.NEXT_HANDLER;
    }

    @Override
    public CacheHandler.TRI_STATE onFetch(int status, Headers headers, HttpServletResponse response) {
        headers.addHeader("Expires", System.currentTimeMillis() + 60000);
        return CacheHandler.TRI_STATE.NEXT_HANDLER;
    }

    protected void bindStore(CacheStore cacheStore) {
        this.store = cacheStore;
    }

    protected void unbindStore(CacheStore cacheStore) {
        if (this.store == cacheStore) {
            this.store = null;
        }
    }
}