Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6011491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:16:03+00:00 2026-05-23T02:16:03+00:00

I am trying to set the Content-MD5 header on a request created with the

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:16:04+00:00Added an answer on May 23, 2026 at 2:16 am

    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:

    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!
                }
            }
        }
    }
    

    To use this filter:

    Client client = Client.create();
    webResource = client.resource("http://localhost:8080/pathToYourResource");
    
    contentMD5filter = new ContentMD5Filter();
    webResource.addFilter(contentMD5filter);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find a way, using AFNetworking, to set the Content-Type header
Im trying to set up a session and i am getting a header error,
Im trying to set the certificate friendly name during the certificate request/acceptance process. I
I'm working with a custom checkbox, and I'm trying to set the content to
I'm trying to set the default value for a Content Taxonomy field in a
I trying to set up a simple blog with Radiant CMS and have a
I'm trying to set up my site so that content in the main area
I'm trying to set some content in between some div tags on a JSP
I'm trying to set the height of one element to the height of another
I'm trying to set up a div which contains 4 divs. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.