Loading multiple images

I have an application that shows posts by various users, somewhat similar to these forums. The users image/avatar is shown next to the post. A customlayout component is used to display the image using Embedded and FileResource, along with other details.

Now, Vaadin gives each image its own URL even if it is the same image displayed several times on the page. This causes the browser to fetch the same image several times. This is adversely affecting the performance. If a user has posted ten times on the page, his/her image resource should be fetched only once not ten times [this will happen, if the URL is same, then the browser will take care of it with 304/caching]
.

How do I set set/configure the Embedded+FileResource to work in the desired manner? Are they the correct components to use?

Btw, everything is running as portlets under Liferay portal 6.0 and Vaadin 6.6.0.

Thanks for your responses…

I resolved this by implementing my own ApplicationResource. The key was to add equals and hashcode methods to uniquely identify the same URL.

thanks. Do you mind sharing your implementation?

here you go Vin:


package com.gsl.web.ui;

import com.vaadin.Application;
import com.vaadin.terminal.ApplicationResource;
import com.vaadin.terminal.DownloadStream;
import java.io.File;
import java.io.FileInputStream;

/**
 *
 * @author Imran
 */
public class UserIconResource implements ApplicationResource {

    long cacheTime = 1000 * 60 * 60*24;
    int bufferSize = 1024;
    File file;
    Application application;
    String mimeType = "image/jpeg";
    DownloadStream ds;

    public UserIconResource(File file, Application application) throws Exception {
        this.file = file;
        this.application = application;
         application.addResource(this);
    }

    @Override
    public DownloadStream getStream() {
        try {
            ds = new DownloadStream(new FileInputStream(file), mimeType, file.getName());
            ds.setCacheTime(cacheTime);
            return ds;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public Application getApplication() {
        return application;
    }

    @Override
    public String getFilename() {
        return file.getName();
    }

    @Override
    public long getCacheTime() {
        return cacheTime;
    }

    @Override
    public int getBufferSize() {
        return bufferSize;
    }

    @Override
    public String getMIMEType() {
        return mimeType;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof UserIconResource) {           
            UserIconResource _obj = UserIconResource.class.cast(obj);
            return file.getAbsolutePath().equalsIgnoreCase(_obj.file.getAbsolutePath());
        } else {
            return super.equals(obj);
        }
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 89 * hash + (this.file != null ? this.file.hashCode() : 0);
        return hash;
    }
}


thanks Imran. Appreciate you sharing with the community.