package org.netmelody.cieye.server.response;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ResourceBundle;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.netmelody.cieye.server.PictureFetcher;
import org.simpleframework.http.Response;

public final class PictureResponder implements CiEyeResponder {

    private static final Log LOG = LogFactory.getLog(PictureResponder.class);
    private static final ResourceBundle MIME_TYPES = ResourceBundle.getBundle(FileResponder.class.getName());
    
    private final String name;
    private final String extension;
    private final PictureFetcher state;

    public PictureResponder(PictureFetcher state, String name) {
        this.state = state;
        this.name = name;
        this.extension = name.substring(name.lastIndexOf('.') + 1);
        LOG.info(name);
    }

    @Override
    public void writeTo(Response response) throws IOException {
        FileInputStream picture = null;
        OutputStream body = null;
        try {
            body = response.getOutputStream();
            final File file = state.getPictureResource(name);
            picture = new FileInputStream(file);
            response.set("Content-Type", contentTypeOf(extension));
            response.setDate("Last-Modified", file.lastModified());
            IOUtils.copy(picture, body);
        }
        finally {
            IOUtils.closeQuietly(picture);
            IOUtils.closeQuietly(body);
        }
    }

    private static String contentTypeOf(String extension) {
        return MIME_TYPES.getString(extension);
    }
}


@@ -757,21 +757,30 @@
 Fetcher 
-state
+pictureFetcher
 ;%0A%0A    p
@@ -817,21 +817,30 @@
 Fetcher 
-state
+pictureFetcher
 , String
@@ -865,21 +865,39 @@
 his.
-state = state
+pictureFetcher = pictureFetcher
 ;%0A  
@@ -1266,13 +1266,22 @@
 e = 
-state
+pictureFetcher
 .get

rename variable