Do the images ever change?  If not, you can use the same kind of
approach Tapestry uses for assets: incorporate a version number (or
SHA1 checksum) into the path, and set the headers that ensure caching
on the client (I'd have to check the Tapestry code for the exact ones
that work).

On Sat, Feb 25, 2012 at 9:25 AM, Gunnar Eketrapp
<gunnar.eketr...@gmail.com> wrote:
> 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};
>    }
> }



-- 
Howard M. Lewis Ship

Creator of Apache Tapestry

The source for Tapestry training, mentoring and support. Contact me to
learn how I can get you up and productive in Tapestry fast!

(971) 678-5210
http://howardlewisship.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to