finally
{
cleanUpAfterExecute(httpMethod);
}
ClientResponse response = new ClientResponse(request.getClientConfiguration())
{
InputStream stream;
InputStream hc4Stream;
@Override
protected void setInputStream(InputStream is)
{
stream = is;
}
public InputStream getInputStream()
{
if (stream == null)
{
HttpEntity entity = res.getEntity();
if (entity == null) return null;
try
{
hc4Stream = entity.getContent();
stream = createBufferedStream(hc4Stream);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
return stream;
}
public void releaseConnection() throws IOException
{
// Apache Client 4 is stupid, You have to get the InputStream and close it if there is an entity
// otherwise the connection is never released. There is, of course, no close() method on response
// to make this easier.
try {
// Another stupid thing...TCK is testing a specific exception from stream.close()
// so, we let it propagate up.
if (stream != null)
{
stream.close();
}
else
{
InputStream is = getInputStream();
if (is != null)
{
is.close();
}
}
}
finally {
// just in case the input stream was entirely replaced and not wrapped, we need
// to close the apache client input stream.
if (hc4Stream != null)
{
try {
hc4Stream.close();
}
catch (IOException ignored) {
}
}
else
{
try
{
HttpEntity entity = res.getEntity();
if (entity != null) entity.getContent().close();
}
catch (IOException ignored)
{
}
}
}
}
};
response.setProperties(request.getMutableProperties());
response.setStatus(res.getStatusLine().getStatusCode());
response.setHeaders(extractHeaders(res));
response.setClientConfiguration(request.getClientConfiguration());
return response;
}