A project of The Apache Software Foundation
Navigation

How to serve files at custom URLs

Learn how to mount resources at clean, predictable URLs instead of using auto-generated paths

Just like pages also resources can be mounted to a specific path. Class WebApplication provides method mountResource which is almost identical to mountPage seen in paragraph 10.6.1:

@Override
public void init() {
  super.init();
  //resource mounted to path /foo/bar
  ResourceReference resourceReference = new ResourceReference("rssProducer"){
     RSSReaderResource rssResource = new RSSReaderResource();
     @Override
     public IResource getResource() {
    return rssResource;
  }};
  mountResource("/foo/bar", resourceReference);
}

With the configuration above (taken from project CustomResourceMounting) every request to /foo/bar will be served by the custom resource built in the previous paragraph.

Parameter placeholders are supported as well:

@Override
public void init() {
  super.init();
  //resource mounted to path /foo with a required indexed parameter
  ResourceReference resourceReference = new ResourceReference("rssProducer"){
     RSSReaderResource rssResource = new RSSReaderResource();
     @Override
     public IResource getResource() {
    return rssResource;
  }};
  mountResource("/bar/${baz}", resourceReference);
}