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

Oleg Kalnichevski commented on HTTPCORE-302:
--------------------------------------------

Quietly discarding I/O exceptions would be a rather bad idea, at least without 
logging them. Please note, though, if some extra processing logic is needed in 
a particular application context one can easily replace the default HTTP 
message classes with custom ones by using a custom  HttpResponseFactory / 
HttpRequestFactory implementations.

---
class MyBasicHttpResponse extends BasicHttpResponse {

    public MyBasicHttpResponse(ProtocolVersion ver, int code, String reason) {
        super(ver, code, reason);
    }

    public MyBasicHttpResponse(StatusLine statusLine) {
        super(statusLine);
    }

    @Override
    public void setEntity(HttpEntity entity) {
        EntityUtils.consumeQuietly(getEntity());
        super.setEntity(entity);
    }        

}

class MyHttpResponseFactory implements HttpResponseFactory {
    
    public HttpResponse newHttpResponse(ProtocolVersion ver, int status, 
HttpContext context) {
        return new MyBasicHttpResponse(ver, status, 
                EnglishReasonPhraseCatalog.INSTANCE.getReason(status, 
Locale.US));
    }
    
    public HttpResponse newHttpResponse(StatusLine statusline, HttpContext 
context) {
        return new MyBasicHttpResponse(statusline);
    }
};
---

EntityUtils#updateEntity() should also be just fine. Would you like to 
contribute a patch?

Oleg 
                
> setEntity should consume the old entity first
> ---------------------------------------------
>
>                 Key: HTTPCORE-302
>                 URL: https://issues.apache.org/jira/browse/HTTPCORE-302
>             Project: HttpComponents HttpCore
>          Issue Type: Improvement
>          Components: HttpCore
>    Affects Versions: 4.2.1
>            Reporter: William R. Speirs
>             Fix For: 4.3
>
>
> The setEntity() method of HttpResponse makes it very easy to leak connections 
> if used without consuming the old entity first. I'm proposing the following 
> enhancements:
> 1) add a Javadoc note to the setEntity() method of HttpResponse that says 
> something like: "Implementations might not consume an existing entity. 
> Callers should consume any existing entity before calling this method."
> 2) either add a consumeAndSetEntity() method to BasicHttpResponse which 
> consumes an existing entity before setting the reference, OR change the 
> implementation of setEntity() to consume any existing entity first. (My vote 
> is for the later, although it will change existing behavior, it shouldn't 
> negatively impact any callers.)
> The reason for this improvement request is simply that I've been burned a few 
> times by calling setEntity() without first consuming the existing entity, and 
> I think we could make this more user friendly. I'm happy to submit a patch 
> once we decide what should be done with #2.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: 
https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

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

Reply via email to