You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Debbie <db...@yahoo.com> on 2010/03/06 14:59:14 UTC

compressing multipart request from custom client

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


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: compressing multipart request from custom client

Posted by Oleg Kalnichevski <ol...@apache.org>.
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 <ol...@apache.org> wrote:
> 
>> From: Oleg Kalnichevski <ol...@apache.org>
>> Subject: Re: compressing multipart request from custom client
>> To: "HttpClient User Discussion" <ht...@hc.apache.org>
>> 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: httpclient-users-unsubscribe@hc.apache.org
>> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: compressing multipart request from custom client

Posted by Deb <db...@yahoo.com>.
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

--- On Sun, 3/7/10, Oleg Kalnichevski <ol...@apache.org> wrote:

> From: Oleg Kalnichevski <ol...@apache.org>
> Subject: Re: compressing multipart request from custom client
> To: "HttpClient User Discussion" <ht...@hc.apache.org>
> 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: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: compressing multipart request from custom client

Posted by Oleg Kalnichevski <ol...@apache.org>.
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: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org