Hi!

I currently store uploaded files in the db.

I then retrieve those images with via an Image page.

My problem now is that the client does not cache these images.

Is there an easy way to fix my erreneous solution !?

        public void prepareResponse(Response r) {
         r.setHeader("Cache-Control", "max-age=3600");
        }

I have included the entire Image.java below ...

Thanks in advance,
Gunnar

================================================================

package utskicket.pages;

import java.io.IOException;
import java.io.InputStream;

import org.apache.tapestry5.StreamResponse;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Response;
import org.slf4j.Logger;

import utskicket.base.BasePage;
import utskicket.model.dao.UploadedFileDAO;
import utskicket.model.entity.UploadedFile;


public class Image extends BasePage {

    private Long groupId;
    private Long imageId;

    @Inject
    private UploadedFileDAO fileDAO;

    @Inject
    private Logger log;

    public class UploadedFileStreamResponse implements StreamResponse {
        private final UploadedFile file;

        public UploadedFileStreamResponse(UploadedFile file) {
            this.file = file;
        }

        public String getContentType() {
                return file.getContentType();
        }

        public InputStream getStream() throws IOException {
            return file.getInputStream();
        }

        // TODO: Find out how to get images cached by browser's. This does
not work!
        public void prepareResponse(Response r) {
        r.setHeader("Cache-Control", "max-age=3600");
        }
    }

    //
------------------------------------------------------------------------------
    // -- Tapestry event methods - Tapestry event methods - Tapestry event
methods --
    //
------------------------------------------------------------------------------

    StreamResponse onActivate(Long groupId, Long imageId) {
        this.groupId = groupId;
        this.imageId = imageId;

        // UploadedFile file = cache.uploadedFileLookup(imageId);
        UploadedFile file = fileDAO.findById(imageId);

        if (null == file) {
            log.warn("Failed to load image #" + imageId);
            return null;
        } else {
            log.debug("Loaded file #" + imageId + " with a size of " +
file.getContent().length + " bytes");
            return new UploadedFileStreamResponse(file);
        }
    }

    Object[] onPassivate() {
        return new Object [] {groupId, imageId};
    }
}

Reply via email to