I have a self hosted JAX-RS REST service implemented with the JAX-RS Restlet extension.
Now I have to serve static content and I was wondering how to do it with JAX-RS. Note, that I do not know the physical directory structure at compile-time. So, given a URL like
http://bla-bla:8182/static/yaba/daba/doo.png
the file $(ROOT)/yaba/daba/doo.png
has to be returned, where $(ROOT)
is the static content root directory.
Is it possible to do it with pure JAX-RS?
Thanks.
EDIT
Known at compile-time:
Unknown at compile-time:
Just found it.
According to the javax.ws.rs.Path
annotation javadocs one can specify a regex to indicate what is considered to be the template parameter match.
Hence, the following code works:
@Path("static")
public class StaticContentHandler {
...
@GET
@Path("{path:.*}")
public FileRepresentation Get(@PathParam("path") String path) {
...;
}
}
GET http://localhost:8182/static/yaba/daba/doo.png
reaches the Get
method with path
equal to "yaba/daba/doo.png" - just what I was looking for.
Hope it helps anyone.
BTW, FileRepresentation
belongs to Restlet, so a really pure JAX-RS implementation would return something else here.