I know I can set the ETAG and LastModified properties on Representation/RepresentationInfo. But I have a simple resource implemented like this :
public class AccountServerResource extends ServerResource implements AccountResource {
private static Logger log = Logger.getLogger(AccountServerResource.class.getName());
@Override
public Account retrieve() {
User user = getClientInfo().getUser();
AccountDAO dao = new AccountDAO();
Account ret = dao.getAccountByEmail(user.getEmail());
log.info("retrieved " + ret);
// getResponse().getEntity() == null at this point !!!
// ---> cannot do this : getResponse().getEntity().setModificationDate(ret.getLastModified());
return ret;
}
}
The representation is not yet attached to the response at this time. When/how do I set the ETAG/LastModified tags ?
What is the recommended practice here ?
---UPDATE---
I tried this approach without luck :
@Override
public Account retrieve() {
User user = getClientInfo().getUser();
AccountDAO dao = new AccountDAO(user.getNamespace());
AccountDAO dao = new AccountDAO();
Account ret = dao.getAccountByEmail(user.getEmail());
log.info("retrieved " + ret);
setOnSent(new StrongEtagCallback<Account>(ret));
return ret;
}
And implementation of the StrongEtagCallback like this :
public class StrongEtagCallback<T extends DomainResource> implements Uniform {
private static SimpleDateFormat df = new SimpleDateFormat("ddMMyyyyHHmmssSSS");
private DomainResource d;
public StrongEtagCallback(T domainResource) {
d = domainResource;
}
@Override
public void handle(Request request, Response response) {
String eTag = d.getClass().getSimpleName() + "-" + d.getId() + "-" + df.format(d.getLastModified());
response.getEntity().setTag(new Tag(eTag, false));
}
}
Where all my entities implement DomainResource which require them to have an ID and LastModified date.
But it does NOT work. I really expected this to work, it is very elegant !
The StrongEtagCallback is being called though, the ETAG set server-side on the entity. My Wireshark nor my GWT client sees a E-TAG header on the response of the response. Diving deeper now.
In researching this issue myself, I noticed a parallel thread started by koma on the Restlet discussion board, in which an alternative and preferable solution was provided by Tim Peierls, namely to override Resource.toRepresentation().
As koma pointed out in that thread, overriding ServerResource.handle()
caused conditions matching to fail (I'm not sure why?), so that approach is problematic.
Example override code provided by Tim Peierls:
@Override public Representation toRepresentation(Object source, Variant target) {
Representation rep = super.toRepresentation(source, target);
if (source instanceof HasLastModified) {
HasLastModified hlm = (HasLastModified) source;
rep.setModificationDate(hlm.getLastModified());
}
if (source instanceof HasEtag) {
HasEtag he = (HasEtag) source;
rep.setTag(he.gettag());
}
return rep;
}