I'm new to restlets and would like some feedback from the community on some
experimentation. Instead of if/else'ing through the list of variant types and
calling the appropriate logic, I'd like reslets to do that for me.
The example "MediaType" below is similar to the restlet version, except that
each enumeration overrides a call back, for example, the text/html type calls
back to handleTextHTML().
TEXT_HTML("text/html", "HTML document") {
@Override
public Representation callBack(VariantHandler arg0) {
return arg0.handleTextHTML();
}
},
The application developer then extends a resource from BaseResource, and
implements the methods they'd like to handle. (like the AWT MouseEvent
adaptors of old) The examples are not complete, I only implmented 4 media
types. The BaseResource gets the media type, converts to the appropriate
extended MediaType, and the invokes the callback.
@Override
public Representation represent(Variant variant) throws ResourceException {
String mediaType = variant.getMediaType().getName();
return MediaType.value(mediaType).callBack(this);
}
So to handle HTML, the developer just does this:
@Override
public Representation handleTextHTML() {
// here's where we respond to HTML clients.
}
http://restlets.s3.amazonaws.com/VariantHandler.java
http://restlets.s3.amazonaws.com/BaseResource.java
http://restlets.s3.amazonaws.com/MediaType.java
------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=1022069