Wicket in Action

A comprehensive guide for Java developers building Wicket-based web applications

Wicket 1.5 – Mounting resources

In the previous article I described how to mount pages at nice looking urls. Today I’ll show you how to mount resource.

Recently there were several people asking in the mailing lists how to serve images loaded from a database so this will be the functionality of the demo application for this article.

To mount a resource you should do:
MyApp.java

public void init() {
  super.init();
  mountResource("/mount/path", new SomeResourceReference());
  // #mountResource() is a shortcut for:
  // mount(new ResourceMapper("/mount/path", new com.example.SomeResourceReference()));
}

With this setup requests to http://host/mount/path will be handled by com.example.SomeResourceReference. The extraction of the request parameters is the same as with pages. A request to http://host/mount/path/indexed?queryNamed=queryValue will have one indexed and one named parameters. They can be read with Attributes.getParameters().get(0) and .get(“queryNamed”) respectfully. Named indexed parameters also work the same way as with pages.

Now since we know how to mount resources let’s see how to create links to them.

MyApp.java

public void init() {
  super.init();
  mountResource("/images/${name}", new ImageResourceReference());
  mountPage("imagesPage", ImageResourcesPage.class);
}

Here we mounted the resource reference that will find the images and a page which will make use of them.

The important thing when creating the resource reference is to give it unique ResourceReference.Key and provide implementation of #getResource() method:
ImageResourceReference.java

public ImageResourceReference() {
    // this creates a Key with scope 'ImageResourceReference.class' 
    // and a name 'imagesDemo'
    super(ImageResourceReference.class, "imagesDemo");
}
 
@Override
public IResource getResource() {
    return new ImageResource();
}

The key is needed by ResourceMapper to be able to recognize the resource reference when asked to create urls to it.
ImageResource is an implementation of IResource which will serve the images.

ImageResource.java:

 private static class ImageResource extends DynamicImageResource {
 
     @Override
     protected byte[] getImageData(Attributes attributes) {
 
         PageParameters parameters = attributes.getParameters();
         StringValue name = parameters.get("name");
 
         byte[] imageBytes = null;
 
         if (name.isEmpty() == false) {
             imageBytes = getImageAsBytes(name.toString());
         }
         return imageBytes;
     }
 
     private byte[] getImageAsBytes(String label) {...}
 
     @Override
     public boolean equals(Object that) {
         return that instanceof ImageResource;
     }
}

Since we need to serve images we take advantage of Wicket’s DynamicImageResource which cares about HTTP headers and stuff and leave us only to load the image’s bytes. The parameter Attributes gives us access to the current request, response and the extracted request parameters. The override of #equals() is the second requirement needed by ResourceMapper to fully recognize the mapped resource. For our need any ImageResource instance will do the job.

To create urls to images you should use the following code:
ImageResourcesPage.java

 ResourceReference imagesResourceReference = new ImageResourceReference();
 PageParameters imageParameters = new PageParameters();
 
 String imageName = "anyName.jpg";
 imageParameters.set("name", imageName);
 CharSequence urlForImage = getRequestCycle().urlFor(imagesResourceReference, imageParameters);
 ExternalLink link = new ExternalLink("link", urlForImage.toString());

The link will produce urls like /images/anyName.jpg.

With this the task is done.

The full source of the demo application can be found in my Github account.

With this book, Wicket will become the greatest territory the Dutch have settled since Manhattan.

Nathan Hamblen
Senior Software Engineer, Teachscape Inc.

This is the complete and authoritative guide to Wicket, written and reviewed by the core members of the Apache Wicket team. If there's anything you want to know about Wicket, you are sure to find it in this book.

Jonathan Locke
Founder and Architect of Apache Wicket, Foreword Wicket in Action

Without question, Wicket in Action... is the be-all and end-all when it comes to Wicket.

Geertjan Wielenga, Wicket Netbeans Plugin Author

The tutorial and conversational tone of the writing makes the book very approachable.

Nick Heudecker
System Mobile

Loved the sample application—it tied everything together.

Phil Hanna
Senior Software Developer, SAS Institute

The essential guide for learning and using Wicket.

Erik van Oosten
Lead programmer and Project Manager, JTeam

Finally, the Web Framework of web frameworks, Apache Wicket, now has a bible of its own.

Per Ejeklint
Senior Software Architect, Heimore group

Wicket is an innovative evolution of the MVC programming with simple roots, but without a primer such as this, it can be more challenging than it needs to be.

Brian Topping
Founder, Bill2 Inc.

Wicket In Action glues the areas of web development with Apache Wicket together and gives a great overview of Apache Wicket...it will make a great compendium.

Nino Martinez Wael
Java Specialist, Jayway Denmark