package page.image;

import java.io.IOException;

import org.wikiwebserver.handler.http.HTTPHandler;
import org.wikiwebserver.handler.http.interfaces.*;
import org.wikiwebserver.util.image.ImageIOHelper;

public class CacheableExample extends Example implements HTTPResponder, CacheableHTTPResponse {

    public String getCacheKey() {
        return String.valueOf(System.currentTimeMillis() / 1000);
    }

    public long getExpireTime() {
        // Cacheable for a second
        return System.currentTimeMillis() + 1000;
    }

    public void init(HTTPHandler conn) throws IOException {
        conn.getResponse().getHeaders().set("Content-Type", "image/png");
    }
    
    public Object respond(HTTPHandler conn) throws IOException {
        
        ImageIOHelper img = new ImageIOHelper(200, 200, "png", conn.getOutputStream());
        
        // Use the parent paint method to draw the example           
        paint(img.createGraphics());
        
        img.writeImageData();
        
        return null;
    }    
}