This seems rather straightforward in a resource oriented architecture. However, I could not find any examples or documentation for how to do this in RESTlet, hence the post. I implemented as follows.
In my Application class, I have defined routing for this resource:
router.attach("/jobs/{jobid}/report",JobReportResource.class);
In the JobReportResource constructor, when defining the kinds of representations supported by this resource, I add Variant based on the URL of the request.
String strReqPath = request.getResourceRef().getPath();In getRepresentation, I can now generate the appropriate representation based on the media type of the variant.
if (strReqPath.matches(".*\/html$")) {
getVariants().add(new Variant(MediaType.TEXT_HTML));
} else {
getVariants().add(new Variant(MediaType.TEXT_XML));
}
Representation representation = null;
MediaType requestMediaType = variant.getMediaType();
if (MediaType.TEXT_XML.equals(requestMediaType)) {
representation = new DomRepresentation(MediaType.TEXT_XML);
...
} else if (MediaType.TEXT_HTML.equals(requestMediaType)) {
...
//transform report into html, set strReport
...
representation = new StringRepresentation(strReport,MediaType.TEXT_HTML);
}
