[ 
https://issues.apache.org/jira/browse/HTTPCLIENT-2206?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17498484#comment-17498484
 ] 

Robert Wang commented on HTTPCLIENT-2206:
-----------------------------------------

Thanks for the quick response. This is the part of the code I'm actually 
finding problematic:

 
{code:java}
        final int status = response.getCode();
        if (status >= HttpStatus.SC_REDIRECTION) {
            throw new HttpResponseException(status, response.getReasonPhrase());
        } {code}
I think it should dispose the response / content before throwing. Because the 
user is likely to have this code:

 

 
{code:java}
executor.execute(Request.Get(uri)).saveContent(file);{code}
which the user will not have access to a {{Response}} local variable to put a 
try-finally block around it, since it's written in a fluent way.

 

 

I think the solution can be wrapping {{#saveContent}} into a 
{{#handleResponse}} call, like {{{}#returnContent{}}}:

 
{code:java}
    public Content returnContent() throws IOException {
        return handleResponse(new ContentResponseHandler());
    } {code}
And the finally block of {{{}#returnResponse{}}}:

 

 
{code:java}
        } finally {
            this.consumed = true;
            this.response.close();
        } {code}
can be replaced with a single call to {{#dispose()}} as both 
{{ClassicHttpResponse}} and {{InputStream}} are {{{}Closeable{}}}, which 
promises {{#close()}} to be idempotent.

 

> fluent: Response#saveContent(File) does not dispose response upon throwing
> --------------------------------------------------------------------------
>
>                 Key: HTTPCLIENT-2206
>                 URL: https://issues.apache.org/jira/browse/HTTPCLIENT-2206
>             Project: HttpComponents HttpClient
>          Issue Type: Bug
>          Components: Fluent HC
>            Reporter: Robert Wang
>            Priority: Minor
>             Fix For: 4.5.14, 5.1.4, 5.2-alpha2
>
>
>  
> When throwing HttpResponseException, it does not dispose the underlying 
> entity. This can cause conn pool to be stuck, imagine a normal retry loop 
> using the same HttpClient. Perhaps it should be wrapped inside 
> #handleResponse.
> {code:java}
>     public void saveContent(final File file) throws IOException {
>         assertNotConsumed();
>         final int status = response.getCode();
>         if (status >= HttpStatus.SC_REDIRECTION) {
>             throw new HttpResponseException(status, 
> response.getReasonPhrase());
>         }
>         try (FileOutputStream out = new FileOutputStream(file)) {
>             final HttpEntity entity = this.response.getEntity();
>             if (entity != null) {
>                 entity.writeTo(out);
>             }
>         } finally {
>             this.consumed = true;
>         }
>     }
> {code}
> An alternative may be calling response.close(), which its behavior is 
> documented here. I'm not sure how to check the "if supported" part though.
>  
> {code:java}
>     /**
>      * Returns a content stream of the entity.
> ...omitted...
>      * <p>
>      * If this entity belongs to an incoming HTTP message, calling
>      * {@link InputStream#close()} on the returned {@code InputStream} will
>      * try to consume the complete entity content to keep the connection
>      * alive. In cases where this is undesired, e.g. when only a small part
>      * of the content is relevant and consuming the complete entity content
>      * would be too inefficient, <i>only</i> the HTTP message from which
>      * this entity was obtained should be closed (if supported).
>      * </p>
> ...omitted...
>      */
>     InputStream getContent() throws IOException, 
> UnsupportedOperationException; {code}
>  
>  
>  



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

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

Reply via email to