Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...