在ServerResource发送的表示形式上设置ETAG/LastModified [英] Setting ETAG/LastModified on Representation sent by ServerResource
问题描述
我知道我可以在Representation/RepresentationInfo上设置ETAG和LastModified属性.但是我有一个实现这样的简单资源:
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;
}
}
此表示目前尚未附加到响应中.我何时/如何设置ETAG/LastModified标签?
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 ?
---更新---
我没有运气就尝试过这种方法:
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;
}
StrongEtagCallback的实现如下:
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));
}
}
我所有的实体都在其中实现DomainResource,这要求它们具有ID和LastModified日期.
Where all my entities implement DomainResource which require them to have an ID and LastModified date.
但是它不起作用.我真的希望它能正常工作,这非常优雅!
尽管正在调用StrongEtagCallback,但ETAG在实体的服务器端设置了.我的Wireshark或我的GWT客户端都在响应中看到一个E-TAG标头.现在跳得更深.
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().
正如koma在那个线程中指出的那样,覆盖 ServerResource.handle()
会导致条件匹配失败(我不确定为什么吗?),所以这种方法是有问题的.
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.
蒂姆·皮尔斯(Tim Peierls)提供的示例替代代码:
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;
}
这篇关于在ServerResource发送的表示形式上设置ETAG/LastModified的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!