Re: adding interceptors on per request basis (was Re: Unable to see response encoding even through Content-Encoding header is present)

2014-01-10 Thread Oleg Kalnichevski
On Thu, 2014-01-09 at 13:34 -0800, Gaurav Kumar wrote:
 In my use case, I need to know on per request basis what was the encoding
 used by remote server. I was hoping there there was out of the box way to
 know content-encoding but I am okay with disabling decompression. Thanks
 for the suggestion.
 
 

You do not have to disable it. You can still use a custom protocol
interceptor to get the job done.
 
---
CloseableHttpClient httpclient = HttpClients.custom()
.addInterceptorFirst(new HttpResponseInterceptor() {
@Override
public void process(
HttpResponse response,
HttpContext context) throws HttpException,
IOException {
HttpEntity entity = response.getEntity();
if (entity != null  entity.getContentLength() != 0) {
Header ceheader = entity.getContentEncoding();
if (ceheader != null) {
context.setAttribute(mystuff,
ceheader.getValue());
}
}
}
}).build();
HttpClientContext context = HttpClientContext.create();
CloseableHttpResponse response = httpclient.execute(new
HttpGet(/stuff), context);
try {
String originalEncoding = context.getAttribute(mystuff,
String.class);
} finally {
response.close();
}
---

Oleg



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



Re: adding interceptors on per request basis (was Re: Unable to see response encoding even through Content-Encoding header is present)

2014-01-10 Thread Gaurav Kumar
Beautiful! Thanks a lot.


On Fri, Jan 10, 2014 at 2:03 AM, Oleg Kalnichevski ol...@apache.org wrote:

 On Thu, 2014-01-09 at 13:34 -0800, Gaurav Kumar wrote:
  In my use case, I need to know on per request basis what was the encoding
  used by remote server. I was hoping there there was out of the box way to
  know content-encoding but I am okay with disabling decompression. Thanks
  for the suggestion.
 
 

 You do not have to disable it. You can still use a custom protocol
 interceptor to get the job done.

 ---
 CloseableHttpClient httpclient = HttpClients.custom()
 .addInterceptorFirst(new HttpResponseInterceptor() {
 @Override
 public void process(
 HttpResponse response,
 HttpContext context) throws HttpException,
 IOException {
 HttpEntity entity = response.getEntity();
 if (entity != null  entity.getContentLength() != 0) {
 Header ceheader = entity.getContentEncoding();
 if (ceheader != null) {
 context.setAttribute(mystuff,
 ceheader.getValue());
 }
 }
 }
 }).build();
 HttpClientContext context = HttpClientContext.create();
 CloseableHttpResponse response = httpclient.execute(new
 HttpGet(/stuff), context);
 try {
 String originalEncoding = context.getAttribute(mystuff,
 String.class);
 } finally {
 response.close();
 }
 ---

 Oleg



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




Re: adding interceptors on per request basis (was Re: Unable to see response encoding even through Content-Encoding header is present)

2014-01-09 Thread sebb
On 9 January 2014 09:06, Oleg Kalnichevski ol...@apache.org wrote:
 On Wed, 2014-01-08 at 18:29 -0800, Gaurav Kumar wrote:
 Thanks! I am now able to see content-encoding. However, the example I
 followed showed how to add interceptors on global client instance level -
 is there a way I can add interceptor on per-request basis?



 Protocol interceptors are meant for cross-cutting aspects that apply to
 all messages processed by the same protocol processor. They make no
 sense on per request basis.

 If you want full control over content encoding on per request basis then
 you should disable automatic content decompression.

Does the decompression change the content encoding?

If not, I don't see why the header is not kept.

 Oleg


 On Wed, Jan 8, 2014 at 8:11 AM, Oleg Kalnichevski ol...@apache.org wrote:

  On Wed, 2014-01-08 at 07:17 -0800, Gaurav Kumar wrote:
   So what is the best way to figure out what encoding is being used?
  
 
  By using a protocol interceptor.
 
  Oleg
 
   On Jan 8, 2014 2:28 AM, Oleg Kalnichevski ol...@apache.org wrote:
   
On Wed, 2014-01-08 at 00:04 -0800, Gaurav Kumar wrote:
 Using HttpClient 4.3-beta2.

 Have a look at this httpclient header log (headers only)
 http://pastebin.com/kWk6rbJ2

 As you can see, the server is responding with Content-Encoding
  header.

 Now, refer to this Java code - http://pastebin.com/i7nhAksb - it
  simple
 prints the headers and a message if content encoding cannot be found.
   The
 output of this code is here- http://pastebin.com/WtkuBsZb


 My question is - why HttpClient is not able to see content encoding
  even
 though it's  header log  shows it got the header from server?

   
Content-Length, Content-Encoding, Content-MD5 headers are removed by
ResponseContentEncoding as they no longer agree with the properties of
automatically decompressed response entity.
   
Oleg
   
   
   
-
To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org
For additional commands, e-mail: httpclient-users-h...@hc.apache.org
   
 
 
 
  -
  To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org
  For additional commands, e-mail: httpclient-users-h...@hc.apache.org
 
 



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


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



Re: adding interceptors on per request basis (was Re: Unable to see response encoding even through Content-Encoding header is present)

2014-01-09 Thread Oleg Kalnichevski
On Thu, 2014-01-09 at 12:10 +, sebb wrote:
 On 9 January 2014 09:06, Oleg Kalnichevski ol...@apache.org wrote:
  On Wed, 2014-01-08 at 18:29 -0800, Gaurav Kumar wrote:
  Thanks! I am now able to see content-encoding. However, the example I
  followed showed how to add interceptors on global client instance level -
  is there a way I can add interceptor on per-request basis?
 
 
 
  Protocol interceptors are meant for cross-cutting aspects that apply to
  all messages processed by the same protocol processor. They make no
  sense on per request basis.
 
  If you want full control over content encoding on per request basis then
  you should disable automatic content decompression.
 
 Does the decompression change the content encoding?
 
 If not, I don't see why the header is not kept.
 

Naturally it does, from 'gzip' or 'compress' to 'identity'.

Oleg



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



Re: adding interceptors on per request basis (was Re: Unable to see response encoding even through Content-Encoding header is present)

2014-01-09 Thread sebb
On 9 January 2014 12:22, Oleg Kalnichevski ol...@apache.org wrote:
 On Thu, 2014-01-09 at 12:10 +, sebb wrote:
 On 9 January 2014 09:06, Oleg Kalnichevski ol...@apache.org wrote:
  On Wed, 2014-01-08 at 18:29 -0800, Gaurav Kumar wrote:
  Thanks! I am now able to see content-encoding. However, the example I
  followed showed how to add interceptors on global client instance level -
  is there a way I can add interceptor on per-request basis?
 
 
 
  Protocol interceptors are meant for cross-cutting aspects that apply to
  all messages processed by the same protocol processor. They make no
  sense on per request basis.
 
  If you want full control over content encoding on per request basis then
  you should disable automatic content decompression.

 Does the decompression change the content encoding?

 If not, I don't see why the header is not kept.


 Naturally it does, from 'gzip' or 'compress' to 'identity'.

Sorry, was getting confused with Content-Type.

 Oleg



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


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



Re: adding interceptors on per request basis (was Re: Unable to see response encoding even through Content-Encoding header is present)

2014-01-09 Thread Gaurav Kumar
In my use case, I need to know on per request basis what was the encoding
used by remote server. I was hoping there there was out of the box way to
know content-encoding but I am okay with disabling decompression. Thanks
for the suggestion.


On Thu, Jan 9, 2014 at 1:06 AM, Oleg Kalnichevski ol...@apache.org wrote:

 On Wed, 2014-01-08 at 18:29 -0800, Gaurav Kumar wrote:
  Thanks! I am now able to see content-encoding. However, the example I
  followed showed how to add interceptors on global client instance level -
  is there a way I can add interceptor on per-request basis?
 
 

 Protocol interceptors are meant for cross-cutting aspects that apply to
 all messages processed by the same protocol processor. They make no
 sense on per request basis.

 If you want full control over content encoding on per request basis then
 you should disable automatic content decompression.

 Oleg


  On Wed, Jan 8, 2014 at 8:11 AM, Oleg Kalnichevski ol...@apache.org
 wrote:
 
   On Wed, 2014-01-08 at 07:17 -0800, Gaurav Kumar wrote:
So what is the best way to figure out what encoding is being used?
   
  
   By using a protocol interceptor.
  
   Oleg
  
On Jan 8, 2014 2:28 AM, Oleg Kalnichevski ol...@apache.org
 wrote:

 On Wed, 2014-01-08 at 00:04 -0800, Gaurav Kumar wrote:
  Using HttpClient 4.3-beta2.
 
  Have a look at this httpclient header log (headers only)
  http://pastebin.com/kWk6rbJ2
 
  As you can see, the server is responding with Content-Encoding
   header.
 
  Now, refer to this Java code - http://pastebin.com/i7nhAksb - it
   simple
  prints the headers and a message if content encoding cannot be
 found.
The
  output of this code is here- http://pastebin.com/WtkuBsZb
 
 
  My question is - why HttpClient is not able to see content
 encoding
   even
  though it's  header log  shows it got the header from server?
 

 Content-Length, Content-Encoding, Content-MD5 headers are removed
 by
 ResponseContentEncoding as they no longer agree with the
 properties of
 automatically decompressed response entity.

 Oleg




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

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



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