Jersey 客户端 API 中的 Content-MD5
我正在尝试在使用 Jersey Client API 创建的请求上设置 Content-MD5 标头。我创建了 ClientFilter,它实现了另一个适配器(类似于 GZipFilter 的工作方式)。像这样:
public class ContentMD5Filter extends ClientFilter {
private static final class ContentMD5Adapter extends AbstractClientRequestAdapter {
ContentMD5Adapter(final ClientRequestAdapter cra) {
super(cra);
}
@Override
public OutputStream adapt(final ClientRequest request, final OutputStream out) throws IOException {
try {
MessageDigest instance = MessageDigest.getInstance("MD5");
request.getHeaders().add("Content-MD5", instance);
return new DigestOutputStream(out, instance);
} catch (NoSuchAlgorithmException e) {
throw new WebApplicationException();
}
}
}
@Override
public ClientResponse handle(final ClientRequest cr) throws ClientHandlerException {
cr.setAdapter(new ContentMD5Adapter(cr.getAdapter()));
return getNext().handle(cr);
}
}
这不起作用,因为摘要被提前读取(即在写入整个内容之前)。
关于如何做到这一点有什么想法吗?除了设置标题之外,我还需要在之后在另一个过滤器中访问它(实施安全机制)
I am trying to set the Content-MD5 header on a request created with the Jersey Client API. I have created ClientFilter which implements another adapter (similar to how the GZipFilter works). Like this:
public class ContentMD5Filter extends ClientFilter {
private static final class ContentMD5Adapter extends AbstractClientRequestAdapter {
ContentMD5Adapter(final ClientRequestAdapter cra) {
super(cra);
}
@Override
public OutputStream adapt(final ClientRequest request, final OutputStream out) throws IOException {
try {
MessageDigest instance = MessageDigest.getInstance("MD5");
request.getHeaders().add("Content-MD5", instance);
return new DigestOutputStream(out, instance);
} catch (NoSuchAlgorithmException e) {
throw new WebApplicationException();
}
}
}
@Override
public ClientResponse handle(final ClientRequest cr) throws ClientHandlerException {
cr.setAdapter(new ContentMD5Adapter(cr.getAdapter()));
return getNext().handle(cr);
}
}
This doesn't work, as the digest are read to early (ie. before the whole content is written.
Any ideas on how to do this? As well as setting the header, I need to access it in another filter afterwards (to implement a security mechanism)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了解决方案。这是一种相当复杂的方法,因为我必须临时存储输出流,但我想如果您自己需要的话,您可以弄清楚:)
过滤器的代码:
要使用此过滤器:
I found the solution. It's a quite complex way to do it, since I have to temporarily store the outputstream, but I guess you can figure it out if you need this yourself :)
The code for the filter:
To use this filter: