Jersey的客户端API中的Content-MD5

时间:2011-06-08 13:02:07

标签: java jersey

我正在尝试在使用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);
    }
}

这不起作用,因为摘要是早期读取的(即在整个内容写入之前。

关于如何做到这一点的任何想法?除了设置标题之外,我还需要在之后的另一个过滤器中访问它(以实现安全机制)

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。这是一个非常复杂的方法,因为我必须暂时存储输出流,但我想如果你自己需要它,你可以弄明白:)

过滤器的代码:

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;

import com.sun.jersey.api.client.AbstractClientRequestAdapter;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientRequest;
import com.sun.jersey.api.client.ClientRequestAdapter;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.filter.ClientFilter;

public class ContentMD5Filter extends ClientFilter {

    @Override
    public ClientResponse handle(final ClientRequest cr) throws ClientHandlerException {
        cr.setAdapter(new ContentMD5Adapter(cr.getAdapter()));
        return getNext().handle(cr);
    }

    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 {
            return new ContentMD5OutputStream(request, getAdapter().adapt(request, out));
        }
    }

    private static final class ContentMD5OutputStream extends OutputStream {

        private final ByteArrayOutputStream byteArrayOutputStream;
        private final OutputStream jerseyStream;
        private final ClientRequest request;

        public ContentMD5OutputStream(final ClientRequest request, final OutputStream jerseyStream) {
            this.jerseyStream = jerseyStream;
            this.request = request;

            byteArrayOutputStream = new ByteArrayOutputStream();
        }

        @Override
        public void write(final int b) throws IOException {
            byteArrayOutputStream.write(b);
        }

        @Override
        public void close() throws IOException {
            byteArrayOutputStream.close();
            byte[] byteArray = byteArrayOutputStream.toByteArray();
            setContentMD5Header(byteArray);
            IOUtils.write(byteArray, jerseyStream);
            jerseyStream.close();
        }

        private void setContentMD5Header(final byte[] data) {
            try {
                MessageDigest instance = MessageDigest.getInstance("MD5");
                String contentMD5 = new String(Base64.encodeBase64(instance.digest(data)));
                request.getHeaders().add("Content-MD5", contentMD5);
            } catch (NoSuchAlgorithmException e) {
                // Errorhandling. Should not happen though!
            }
        }
    }
}

要使用此过滤器:

Client client = Client.create();
webResource = client.resource("http://localhost:8080/pathToYourResource");

contentMD5filter = new ContentMD5Filter();
webResource.addFilter(contentMD5filter);