Close http result on exceptions and when Odata Result closed

This commit is contained in:
Vlad Kozyr 2020-05-07 13:33:15 +03:00 committed by mibo
parent 917ec01043
commit c0eeb34f3f
3 changed files with 47 additions and 23 deletions

1
.gitignore vendored
View File

@ -5,6 +5,7 @@
.idea
target
bin
*.iml
*.bak
classes
.DS_Store

View File

@ -18,6 +18,26 @@
*/
package org.apache.olingo.client.core.communication.request;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DecompressingHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.olingo.client.api.ODataClient;
import org.apache.olingo.client.api.communication.header.ODataHeaders;
import org.apache.olingo.client.api.communication.request.ODataRequest;
import org.apache.olingo.client.api.communication.response.ODataResponse;
import org.apache.olingo.client.api.http.HttpClientException;
import org.apache.olingo.commons.api.ex.ODataRuntimeException;
import org.apache.olingo.commons.api.format.ContentType;
import org.apache.olingo.commons.api.http.HttpHeader;
import org.apache.olingo.commons.api.http.HttpMethod;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
@ -26,26 +46,6 @@ import java.net.URI;
import java.nio.charset.Charset;
import java.util.Collection;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DecompressingHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.olingo.client.api.ODataClient;
import org.apache.olingo.client.api.communication.header.ODataHeaders;
import org.apache.olingo.client.api.communication.request.ODataRequest;
import org.apache.olingo.client.api.communication.request.ODataStreamer;
import org.apache.olingo.client.api.communication.response.ODataResponse;
import org.apache.olingo.client.api.http.HttpClientException;
import org.apache.olingo.commons.api.ex.ODataRuntimeException;
import org.apache.olingo.commons.api.http.HttpHeader;
import org.apache.olingo.commons.api.format.ContentType;
import org.apache.olingo.commons.api.http.HttpMethod;
/**
* Abstract representation of an OData request. Get instance by using factories.
*
@ -112,7 +112,7 @@ public abstract class AbstractODataRequest extends AbstractRequest implements OD
public URI getURI() {
return uri;
}
@Override
public HttpUriRequest getHttpRequest() {
return request;
@ -315,6 +315,7 @@ public abstract class AbstractODataRequest extends AbstractRequest implements OD
try {
checkResponse(odataClient, response, getAccept());
} catch (ODataRuntimeException e) {
closeHttpResponse(response);
odataClient.getConfiguration().getHttpClientFactory().close(httpClient);
throw e;
}
@ -322,7 +323,17 @@ public abstract class AbstractODataRequest extends AbstractRequest implements OD
return response;
}
/**
private void closeHttpResponse(HttpResponse response) {
if (response instanceof CloseableHttpResponse) {
try {
((CloseableHttpResponse) response).close();
} catch (IOException e) {
LOG.warn("Unable to close response: {}", response, e);
}
}
}
/**
* Gets an empty response that can be initialized by a stream.
* <br/>
* This method has to be used to build response items about a batch request.

View File

@ -32,6 +32,7 @@ import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.olingo.client.api.ODataClient;
import org.apache.olingo.client.api.communication.request.batch.ODataBatchLineIterator;
@ -103,7 +104,7 @@ public abstract class AbstractODataResponse implements ODataResponse {
* Batch info (if to be batched).
*/
protected ODataBatchController batchInfo = null;
private byte[] inputContent = null;
public AbstractODataResponse(
@ -244,6 +245,7 @@ public abstract class AbstractODataResponse implements ODataResponse {
@Override
public void close() {
closeHttpResponse();
odataClient.getConfiguration().getHttpClientFactory().close(httpClient);
if (batchInfo != null) {
@ -251,6 +253,16 @@ public abstract class AbstractODataResponse implements ODataResponse {
}
}
protected void closeHttpResponse() {
if(res != null && res instanceof CloseableHttpResponse) {
try {
((CloseableHttpResponse) res).close();
} catch (IOException e) {
LOG.debug("Unable to close response: {}", res, e);
}
}
}
@Override
public InputStream getRawResponse() {