Deb wrote:
Hi Oleg,

Thanks for you quick response and code example -- it is most appreciated!  
Unfortunately, the multipart entity does not implement the getContent method, 
so it can't be used (as it currently stands) to obtain the data to be 
compressed.

Deb


So, what is the problem? Just use #writeTo()

---
static class GzipCompressingEntity extends HttpEntityWrapper {

    private static final String GZIP_CODEC = "gzip";

    public GzipCompressingEntity(final HttpEntity entity) {
        super(entity);
    }

    public Header getContentEncoding() {
        return new BasicHeader(HTTP.CONTENT_ENCODING, GZIP_CODEC);
    }

    public long getContentLength() {
        return -1;
    }

    public boolean isChunked() {
        // force content chunking
        return true;
    }

    public void writeTo(final OutputStream outstream) throws IOException {
        if (outstream == null) {
throw new IllegalArgumentException("Output stream may not be null");
        }
        GZIPOutputStream gzip = new GZIPOutputStream(outstream);
        wrappedEntity.writeTo(outstream);
        gzip.finish();
        gzip.flush();
    }

}
---

Oleg


--- On Sun, 3/7/10, Oleg Kalnichevski <[email protected]> wrote:

From: Oleg Kalnichevski <[email protected]>
Subject: Re: compressing multipart request from custom client
To: "HttpClient User Discussion" <[email protected]>
Date: Sunday, March 7, 2010, 8:25 AM
Debbie wrote:
I have a multipart request that I would like to gzip
compress via a custom HttpClient (so it can be decompressed
by apache via MOD deflate on the server side).  I tried
to just compress the files themselves but the apache server
seems to want the entire body of the request compressed in
order to decompress.
I’ve read a post by Oleg in 2008 that states this is
easy to do and yet I’m not sure how to make this work
using the RequestInterceptor going from client to server.
Any pointers would be very appreciated. Thanks!
Deb

Try this:

---
public static void main(String[] args) throws Exception {

    HttpHost targetHost = new
HttpHost("www.sometarget.com", 80, "http");
    DefaultHttpClient httpclient = new
DefaultHttpClient();
    BasicHttpContext localcontext = new
BasicHttpContext();

    HttpPost httppost = new HttpPost("/");
    MultipartEntity reqentity = new
MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    reqentity.addPart("stuff", new
StringBody("some stuff"));
    httppost.setEntity(new
GzipCompressingEntity(reqentity));
    HttpResponse response =
httpclient.execute(targetHost, httppost, localcontext);

System.out.println(response.getStatusLine());

    HttpEntity entity = response.getEntity();
    if (entity != null) {
        entity.consumeContent();
    }
}

static class GzipCompressingEntity extends
HttpEntityWrapper {

    private static final String GZIP_CODEC =
"gzip";

    public GzipCompressingEntity(final HttpEntity
entity) {
        super(entity);
    }

    public Header getContentEncoding() {
        return new
BasicHeader(HTTP.CONTENT_ENCODING, GZIP_CODEC);
    }

    public long getContentLength() {
        return -1;
    }

    public boolean isChunked() {
        // force content chunking
        return true;
    }

    public void writeTo(final OutputStream
outstream) throws IOException {
        if (outstream == null) {
            throw new
IllegalArgumentException("Output stream may not be null");
        }
        GZIPOutputStream gzip = new
GZIPOutputStream(outstream);
        InputStream in =
wrappedEntity.getContent();
        byte[] tmp = new byte[2048];
        int l;
        while ((l = in.read(tmp)) !=
-1) {
            gzip.write(tmp,
0, l);
        }
        gzip.close();
    }

}

---

Hope this helps

Oleg

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to