Close http result on exceptions and when Odata Result closed
diff --git a/.gitignore b/.gitignore
index 81878c2..b54ef11 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,6 +5,7 @@
.idea
target
bin
+*.iml
*.bak
classes
.DS_Store
diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/AbstractODataRequest.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/AbstractODataRequest.java
index 342c4c8..835feb6 100644
--- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/AbstractODataRequest.java
+++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/AbstractODataRequest.java
@@ -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.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 URI getURI() {
return uri;
}
-
+
@Override
public HttpUriRequest getHttpRequest() {
return request;
@@ -315,6 +315,7 @@
try {
checkResponse(odataClient, response, getAccept());
} catch (ODataRuntimeException e) {
+ closeHttpResponse(response);
odataClient.getConfiguration().getHttpClientFactory().close(httpClient);
throw e;
}
@@ -322,7 +323,17 @@
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.
diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/response/AbstractODataResponse.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/response/AbstractODataResponse.java
index 0cf342a..8557f36 100644
--- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/response/AbstractODataResponse.java
+++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/response/AbstractODataResponse.java
@@ -32,6 +32,7 @@
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 @@
* Batch info (if to be batched).
*/
protected ODataBatchController batchInfo = null;
-
+
private byte[] inputContent = null;
public AbstractODataResponse(
@@ -244,6 +245,7 @@
@Override
public void close() {
+ closeHttpResponse();
odataClient.getConfiguration().getHttpClientFactory().close(httpClient);
if (batchInfo != null) {
@@ -251,6 +253,16 @@
}
}
+ 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() {