Can we make it easier to release resources?

View: New views
8 Messages — Rating Filter:   Alert me  

Can we make it easier to release resources?

by Bob Lee :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Right now, we have to do something like this:

HttpResponse r = client.execute(...)
try {
  ...
} finally {
  HttpEntity e = r.getEntity();
  if (e != null) {
    // Soon to be renamed finish()
    e.consumeContent();
  }
}

It might be nice if we could do something like this:

HttpResponse r = client.execute(...)
try {
  ...
} finally {
   response.finish();
}

Or even something like this:

public interface ResponseHandler<T> {
  T handle(HttpResponse response) throws IOException;
}

String result = client.execute(myHandler);

Then you could have a few reusable response handlers, like a JSON handler,
and XML handler, etc.

Bob

Re: Can we make it easier to release resources?

by olegk :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Bob Lee wrote:

> Right now, we have to do something like this:
>
> HttpResponse r = client.execute(...)
> try {
>   ...
> } finally {
>   HttpEntity e = r.getEntity();
>   if (e != null) {
>     // Soon to be renamed finish()
>     e.consumeContent();
>   }
> }
>
> It might be nice if we could do something like this:
>
> HttpResponse r = client.execute(...)
> try {
>   ...
> } finally {
>    response.finish();
> }
>
> Or even something like this:
>
> public interface ResponseHandler<T> {
>   T handle(HttpResponse response) throws IOException;
> }
>
> String result = client.execute(myHandler);
>
> Then you could have a few reusable response handlers, like a JSON handler,
> and XML handler, etc.
>
> Bob
>

Hi Bob

I _really_, _really_ like this idea. However, what shall HttpClient do
if it gets status code other than 200? Throw an exception? Ignore the
status code?

Oleg


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...


Re: Can we make it easier to release resources?

by Bob Lee :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, Jun 20, 2008 at 4:16 PM, Oleg Kalnichevski <olegk@...> wrote:

> I _really_, _really_ like this idea. However, what shall HttpClient do if
> it gets status code other than 200? Throw an exception? Ignore the status
> code?
>

We could pass a response to the handler in all the same cases where we
return a response today. If the user wants to add some handling of non-200
codes, they could decorate their handler. Now, I suppose I'm overlapping w/
the response interceptor support. I'll take a closer look at the API.

Bob

Re: Can we make it easier to release resources?

by olegk :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Bob Lee wrote:

> On Fri, Jun 20, 2008 at 4:16 PM, Oleg Kalnichevski <olegk@...> wrote:
>
>> I _really_, _really_ like this idea. However, what shall HttpClient do if
>> it gets status code other than 200? Throw an exception? Ignore the status
>> code?
>>
>
> We could pass a response to the handler in all the same cases where we
> return a response today. If the user wants to add some handling of non-200
> codes, they could decorate their handler. Now, I suppose I'm overlapping w/
> the response interceptor support. I'll take a closer look at the API.
>
> Bob
>

Coming to think of it, It seems perfectly okay to leave this decision up
to the individual handlers. Simple handlers might just thrown an
exception on all status codes but 200. More complex ones such as SOAP
handler could handle 500 status codes if the message contains an SOAP fault.

Want to put together a patch? ;-)

Oleg



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...


Re: Can we make it easier to release resources?

by Bob Lee :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, Jun 20, 2008 at 4:39 PM, Oleg Kalnichevski <olegk@...> wrote:

> Coming to think of it, It seems perfectly okay to leave this decision up to
> the individual handlers. Simple handlers might just thrown an exception on
> all status codes but 200. More complex ones such as SOAP handler could
> handle 500 status codes if the message contains an SOAP fault.
>
> Want to put together a patch? ;-)


Sure. Want me to just add methods to HttpClient? We could build it as a
utility method instead...

Bob

Re: Can we make it easier to release resources?

by olegk :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Bob Lee wrote:

> On Fri, Jun 20, 2008 at 4:39 PM, Oleg Kalnichevski <olegk@...> wrote:
>
>> Coming to think of it, It seems perfectly okay to leave this decision up to
>> the individual handlers. Simple handlers might just thrown an exception on
>> all status codes but 200. More complex ones such as SOAP handler could
>> handle 500 status codes if the message contains an SOAP fault.
>>
>> Want to put together a patch? ;-)
>
>
> Sure. Want me to just add methods to HttpClient? We could build it as a
> utility method instead...
>
> Bob
>

I think it is perfectly fine to add a new execute method to the
HttpClient interface that takes ResponseHandler as an additional parameter.

Oleg

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...


Response handler API; was Re: Can we make it easier to release resources?

by olegk :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, 2008-06-20 at 11:50 -0700, Bob Lee wrote:

...

> Or even something like this:
>
> public interface ResponseHandler<T> {
>   T handle(HttpResponse response) throws IOException;
> }
>
> String result = client.execute(myHandler);
>
> Then you could have a few reusable response handlers, like a JSON handler,
> and XML handler, etc.
>
> Bob

Folks,

I added support for response handlers as suggested by Bob. Here's the
example of the new API

http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientWithResponseHandler.java
http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/BasicResponseHandler.java

I think it turned out pretty neat.

Bob, please review.

Oleg


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...


Re: Response handler API; was Re: Can we make it easier to release resources?

by Sam Berlin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This looks really cool!  Makes it much easier to use HttpClient for
everyday usage.

Sam

On Fri, Jun 27, 2008 at 7:43 PM, Oleg Kalnichevski <olegk@...> wrote:

> On Fri, 2008-06-20 at 11:50 -0700, Bob Lee wrote:
>
> ...
>
>> Or even something like this:
>>
>> public interface ResponseHandler<T> {
>>   T handle(HttpResponse response) throws IOException;
>> }
>>
>> String result = client.execute(myHandler);
>>
>> Then you could have a few reusable response handlers, like a JSON handler,
>> and XML handler, etc.
>>
>> Bob
>
> Folks,
>
> I added support for response handlers as suggested by Bob. Here's the
> example of the new API
>
> http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/examples/org/apache/http/examples/client/ClientWithResponseHandler.java
> http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/BasicResponseHandler.java
>
> I think it turned out pretty neat.
>
> Bob, please review.
>
> Oleg
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@...
> For additional commands, e-mail: dev-help@...
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...

LightInTheBox - Buy quality products at wholesale price