I was looking into Restlets to allow me to more quickly write some web
services. Sadly I didn't find that to be the case. Figured I'd explain
why and what I quickly built instead to scratch my itch. Admittedly I
only spent half a day play with Restlets, so my impressions have to be
taken with a grain of salt and might very well be wrong.
I have the need to expose a few fairly simple APIs via a web service.
RESTish APIs are by far my preferred method for doing this, mostly
because they are so easily testable via a browser, easily load balanced
and integrate in various languages easily.
I built a quick image transcoding / scaling Restlet yesterday. Although
it was fairly straight forward, the framework didn't really make things
EASIER for me. I found myself writing a lot of code to validate the
parameters I expected were included in the request, pulling them out,
making sure they were the correct format etc.. then handling those
cases and returning appropriate error messaging and status codes. The
actual WORK for the service was one method, which honestly I should
refactor into it's own library.
I guess the point is that in the vast majority of well designed systems,
the actual logic to a RESTlet should be exceedingly simple. IE, if you
are providing a view into a database, or doing some unit of work, that
code likely lives in a separate library entirely. The framework
(Restlet or otherwise) should be doing the heavy lifting of doing
parameter parsing and validation, since that's really all we are doing,
putting a thin web service veneer on functionality we have already built.
Sadly, I didn't find that the Restlet framework was really making my
life any easier.
Instead I wrote a thin layer over Jetty that DID satisfy that need.
I'll explain it here only because I think it might be worthy of discussion.
I defined a Parameter interface which when handed a Request will extract
named value(s) from it. From this I've derived a bunch of different
Parameter types (Path, Query, Body, Derived). Each parameter can
specify whether it is a required parameter, as well as a regular
expression which the parameter MUST match if present. RestStop's (my
base end point) expose a List of the Parameter's they expect. The
framework takes care of taking an incoming request, parsing the
parameters and displaying appropriate error messaging in case of
failure. The framework guarantees that when handle() is called on
RestStop all required parameters are present and pass format validation.
This GREATLY simplified my code.
Additionally, I created greatly simplified Request and Response objects
which only exposed the incoming parameter values and outgoing template
values. (or optionally an output stream to write to) The primary
purpose there being that unit testing of RestStops is dead simple. You
simply fill a hash with your test input parameters, call handle() and
validate that the output hash is as expected.
I hooked up FreeMarker for simple templating.
I don't claim that this setup is near as powerful or flexible as
Restlets, but it does lead to vastly faster (and I believe safer and
more correct) service implementations.
If you are interested, I'd be happy to share the code. There isn't much
there but it does what it does well.
-Nic