I think Andy is talking about a ServerResource subclass with an annotated
method returning a value that will later be converted into a
Representation. In that case there is no existing Representation on which
to call setTag. The way I handle this is to override toRepresentation,
something like this:

    @Override public Representation toRepresentation(Object source, Variant
target) {
        Representation representation = super.toRepresentation(source,
target);
        Tag tag = computeTag(source, target);
        if (tag != null) {
            representation.setTag(tag);
        }
        return representation;
    }

The implementation of computeTag is up to you. I make sure that the types
of the return values for which I want to have ETags have a common supertype
that knows how to compute an ETag.

    Tag computeTag(Object source, Variant target) {
        if (source instanceof ComputesTag) {
            return ((ComputesTag) source).computeTag(target);
        } else {
            return null;
        }
    }

My default implementation of ComputesTag.computeTag works generically
through the use of Guava's new support for hashing.

    // Method of ComputesTag:
    public Tag computeTag(Variant target) {
        HashCode hc = Hashing.md5().newHasher()
            .putObject(this, REP_FUNNEL)
            .putObject(target, VARIANT_FUNNEL)
            .hash();
        return new Tag(hc.toString(), false);
    }

The same technique applies to expirationDate ("Expires"), modificationDate
("Last-Modified"), and locationRef ("Content-Location"). In practice, I
bundle all of these into a common supertype; I don't really have a separate
ComputesTag type.

One thing I have *not* done (yet) is to keep a cache of ETags by request
URI (plus variant info) so I could quickly return 304 Not Modified without
having to build the return value in the first place. The hard part is
ensuring that invalid entries are removed from that cache.

--tim

On Wed, Feb 29, 2012 at 6:30 AM, Thierry Boileau <
[email protected]> wrote:

> Hello Andy,
>
> do you mean that the response will get the entity a little bit later in
> your code?
> Are you trying to set the Tag in the "doInit" method?
>
> Best regards,
> Thierry Boileau
>
>
>
> I'd like to set an ETag response header while processing a request within
>> my ServerResource subclass.  I tried
>>
>> getResponseEntity().setTag(new Tag("foo));
>>
>> but this doesn't work, because there is no entity in the response object
>> yet (getResponseEntity returns null).
>>
>> What's the right way to do this?
>>
>> Thanks.
>> -Andy
>>
>> ------------------------------------------------------
>>
>> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2925566
>>
>
>

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

Reply via email to