{
String uri = request.getUri();
final HttpRequestBase httpMethod = createHttpMethod(uri, request.getHttpMethod());
loadHttpMethod(request, httpMethod);
final HttpResponse res = httpClient.execute(httpMethod);
BaseClientResponse response = new BaseClientResponse(new BaseClientResponseStreamFactory()
{
InputStream stream;
public InputStream getInputStream() throws IOException
{
if (stream == null)
{
HttpEntity entity = res.getEntity();
if (entity == null) return null;
stream = new SelfExpandingBufferredInputStream(entity.getContent());
}
return stream;
}
public void performReleaseConnection()
{
// 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
{
if (stream != null)
{
stream.close();
}
else
{
InputStream is = getInputStream();
if (is != null)
{
is.close();
}
}
}
catch (Exception ignore)
{
}
}
}, this);
response.setStatus(res.getStatusLine().getStatusCode());
response.setHeaders(extractHeaders(res));
response.setProviderFactory(request.getProviderFactory());
return response;
}