The best way I know to manipulate the response object without giving up the
simplicity of object->variant conversion is to override the
Resource.toRepresentation method. An example of using this technique can be
found in the (0-rated) first answer to this question:

http://stackoverflow.com/questions/9015511/setting-etag-lastmodified-on-representation-sent-by-serverresource


In your case, you'd want to invent a HasLinks interface, e.g.:

interface HasLinks {
    Iterable<Link> getLinks();
}

and implement it for all convertible object types that have links that you
want to expose. You can use the Link type from the Atom extension of
Restlet or just roll your own. (I did the former, but it was probably
overkill.)

Then have all of your ServerResource implementations (that might ever
return an object that implements HasLinks) extend a common base class that
has a clause like this in its toRepresentation implementation:

    if (source instanceof HasLinks) {
        for (Link link : (HasLinks) source) {
            // Add custom header built from link.
        }
    }

Note that its perfectly OK to use this common base in more places than its
ever used, because of the instanceof check.

--tim


On Mon, Aug 6, 2012 at 1:13 PM, Norm Deane <norm.de...@vanderbilt.edu>wrote:

> One of the recipes in the O'Reily RESTful Web Services Cookbook outlines a
> way to provide a format-independent means to convey resource links using
> HTTP Headers. Something like this...
>
> # Response
> HTTP/1.1 200 OK
> Content-Type: application/xml
> Link: <http://dss.mc.vanderbilt.edu/patients/123>;
> rel="self;type=application/xml"
> Link: <http://dss.mc.vanderbilt.edu/patients/123/problems>;
> rel="related;type=application/xml"
>
> What would be the best way to do this within the Restlet framework? Right
> now my ServerResources don't do any direct manipulation of the Response and
> are pretty lightweight using the ConverterService to handle variant->object
> and object->variant conversions.
>
>
> Thanks,
>
> Norm
>
> ------------------------------------------------------
>
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2996982
>

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2996999

Reply via email to