Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wicket Dynamic Image URL

Short question: I need to turn a dynamic image pulled from a database into a URL without adding a component to the displaying page (such as using a NonCachingImage) using Wicket.

The perfect solution (that I've implemented in other Frameworks) is simply to create a page that takes the image ID as a url parameter and renders the image to the response stream. Unfortunately Wicket's Page class extends MarkupContainer, which revolves around MarkupStreams. MarkupStreams aren't very conducive to rendering byte data directly.

Long question: I'm using Wicket 1.4.0, running in Tomcat 6.0.18. The image is stored in a Postgres database, retrieved via JDBC. The image needs to be rendered by a third party API that only accepts image URLs. I have a model object that contains the byte data, mime type, and a Resource object that can pull the model from the DB and add it to a response stream.

Any ideas?

like image 599
Michael Krauklis Avatar asked Oct 03 '09 01:10

Michael Krauklis


2 Answers

I've only just started to work with Wicket myself, but I would simply mount the resource as a shared resource with its own URL. You just override init() in your Application and register the resource with

getSharedResources().add(resourceKey, dynamicImageResource);

Then, you mount it as a shared resource with

mountSharedResource(path, resourceKey);

For some reason, that I still do not completely grasp, you have to prepend the class name of the application to the resource key you pass to mountSharedResource().


Let's add a fully working example for some bonus votes! First create an empty Wicket template with

mvn archetype:create -DarchetypeGroupId=org.apache.wicket \
    -DarchetypeArtifactId=wicket-archetype-quickstart \
    -DarchetypeVersion=1.4.0 -DgroupId=com.mycompany \
    -DartifactId=myproject

Then, override the init() method in WicketApplication by adding:

@Override
protected void init() {
    final String resourceKey = "DYN_IMG_KEY";
    final String queryParm = "id";

    getSharedResources().add(resourceKey, new Resource() {
        @Override
        public IResourceStream getResourceStream() {
            final String query = getParameters().getString(queryParm);

            // generate an image containing the query argument
            final BufferedImage img = new BufferedImage(100, 100,
                    BufferedImage.TYPE_INT_RGB);
            final Graphics2D g2 = img.createGraphics();
            g2.setColor(Color.WHITE);
            g2.drawString(query, img.getWidth() / 2, img.getHeight() / 2);

            // return the image as a PNG stream
            return new AbstractResourceStreamWriter() {
                public String getContentType() {
                    return "image/png";
                }
                public void write(OutputStream output) {
                    try { ImageIO.write(img, "png", output); }
                    catch (IOException ex) { /* never swallow exceptions! */ }
                }
            };
        }
    });

    mountSharedResource("/resource", Application.class.getName() + "/" +
            resourceKey);
}

The little dynamic PNG resource just writes the query parameter on black background. Of course, you can access your DB or do whatever you like to produce the image data.

Finally, execute mvn jetty:run, and you will be able to access the resource at this URL.

like image 135
janko Avatar answered Oct 11 '22 20:10

janko


I'll add another answer to say Martin Grigorov wrote a really nice blogpost over at wicketinaction.com to detail how to serve up images loaded from a database in Wicket 1.5:

http://wicketinaction.com/2011/07/wicket-1-5-mounting-resources/

This matches exactly with @Michael's question.

like image 20
Tim Avatar answered Oct 11 '22 21:10

Tim