[OLINGO-989] Stream handling enhancements

Signed-off-by: Christian Amend <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/3e9e6931
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/3e9e6931
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/3e9e6931

Branch: refs/heads/master
Commit: 3e9e69314b83380da300ed9ae010272b534481f7
Parents: c8d69b3
Author: Kanika Jashrapuria <[email protected]>
Authored: Wed Apr 27 17:25:34 2016 +0530
Committer: Christian Amend <[email protected]>
Committed: Wed Jul 27 14:43:57 2016 +0200

----------------------------------------------------------------------
 .../request/AbstractODataRequest.java           | 23 ++++++-------
 .../request/AsyncRequestWrapperImpl.java        | 35 ++++++++++----------
 .../response/AbstractODataResponse.java         | 28 ++++++++--------
 3 files changed, 44 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/3e9e6931/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/AbstractODataRequest.java
----------------------------------------------------------------------
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 d6274c0..017006b 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
@@ -1,18 +1,18 @@
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
+ * or more contributor license agreements. See the NOTICE file
  * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
+ * regarding copyright ownership. The ASF licenses this file
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
+ * with the License. You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
+ * KIND, either express or implied. See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
@@ -33,6 +33,7 @@ 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.client.utils.HttpClientUtils;
 import org.apache.http.impl.client.DecompressingHttpClient;
 import org.apache.olingo.client.api.ODataClient;
 import org.apache.olingo.client.api.communication.header.ODataHeaders;
@@ -173,9 +174,7 @@ public abstract class AbstractODataRequest extends 
AbstractRequest implements OD
   @Override
   public String getAccept() {
     final String acceptHead = odataHeaders.getHeader(HttpHeader.ACCEPT);
-    return StringUtils.isBlank(acceptHead) ?
-        getDefaultFormat().toContentTypeString() :
-        acceptHead;
+    return StringUtils.isBlank(acceptHead) ? 
getDefaultFormat().toContentTypeString() : acceptHead;
   }
 
   @Override
@@ -196,9 +195,7 @@ public abstract class AbstractODataRequest extends 
AbstractRequest implements OD
   @Override
   public String getContentType() {
     final String contentTypeHead = 
odataHeaders.getHeader(HttpHeader.CONTENT_TYPE);
-    return StringUtils.isBlank(contentTypeHead) ?
-        getDefaultFormat().toContentTypeString() :
-        contentTypeHead;
+    return StringUtils.isBlank(contentTypeHead) ? 
getDefaultFormat().toContentTypeString() : contentTypeHead;
   }
 
   @Override
@@ -257,9 +254,11 @@ public abstract class AbstractODataRequest extends 
AbstractRequest implements OD
       final HttpEntity httpEntity = doExecute().getEntity();
       return httpEntity == null ? null : httpEntity.getContent();
     } catch (IOException e) {
+      HttpClientUtils.closeQuietly(httpClient);
       throw new HttpClientException(e);
     } catch (RuntimeException e) {
       this.request.abort();
+      HttpClientUtils.closeQuietly(httpClient);
       throw new HttpClientException(e);
     }
   }
@@ -330,7 +329,7 @@ public abstract class AbstractODataRequest extends 
AbstractRequest implements OD
       if (ODataResponse.class.isAssignableFrom(clazz)) {
         try {
           final Constructor<?> constructor = clazz.getDeclaredConstructor(
-                  this.getClass(), ODataClient.class, HttpClient.class, 
HttpResponse.class);
+              this.getClass(), ODataClient.class, HttpClient.class, 
HttpResponse.class);
           constructor.setAccessible(true);
           return (V) constructor.newInstance(this, odataClient, httpClient, 
null);
         } catch (Exception e) {

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/3e9e6931/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/AsyncRequestWrapperImpl.java
----------------------------------------------------------------------
diff --git 
a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/AsyncRequestWrapperImpl.java
 
b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/AsyncRequestWrapperImpl.java
index 20a645c..c084e24 100644
--- 
a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/AsyncRequestWrapperImpl.java
+++ 
b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/AsyncRequestWrapperImpl.java
@@ -1,18 +1,18 @@
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
+ * or more contributor license agreements. See the NOTICE file
  * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
+ * regarding copyright ownership. The ASF licenses this file
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
+ * with the License. You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
+ * KIND, either express or implied. See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
@@ -29,6 +29,7 @@ import org.apache.http.HttpResponse;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
 import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.client.utils.HttpClientUtils;
 import org.apache.http.entity.InputStreamEntity;
 import org.apache.http.impl.client.DecompressingHttpClient;
 import org.apache.http.util.EntityUtils;
@@ -47,7 +48,7 @@ import org.apache.olingo.commons.api.http.HttpMethod;
 import org.apache.olingo.commons.api.http.HttpStatusCode;
 
 public class AsyncRequestWrapperImpl<R extends ODataResponse> extends 
AbstractRequest
-        implements AsyncRequestWrapper<R> {
+    implements AsyncRequestWrapper<R> {
 
   protected static final int MAX_RETRY = 5;
 
@@ -94,8 +95,8 @@ public class AsyncRequestWrapperImpl<R extends ODataResponse> 
extends AbstractRe
 
     this.request = 
odataClient.getConfiguration().getHttpUriRequestFactory().create(method, 
this.uri);
 
-    if(request instanceof HttpEntityEnclosingRequestBase) {
-      if(odataRequest instanceof AbstractODataBasicRequest) {
+    if (request instanceof HttpEntityEnclosingRequestBase) {
+      if (odataRequest instanceof AbstractODataBasicRequest) {
         AbstractODataBasicRequest<?> br = (AbstractODataBasicRequest<?>) 
odataRequest;
         HttpEntityEnclosingRequestBase httpRequest = 
((HttpEntityEnclosingRequestBase) request);
         httpRequest.setEntity(new InputStreamEntity(br.getPayload(), -1));
@@ -150,8 +151,7 @@ public class AsyncRequestWrapperImpl<R extends 
ODataResponse> extends AbstractRe
 
     protected boolean preferenceApplied = false;
 
-    public AsyncResponseWrapperImpl() {
-    }
+    public AsyncResponseWrapperImpl() {}
 
     /**
      * Constructor.
@@ -229,8 +229,8 @@ public class AsyncRequestWrapperImpl<R extends 
ODataResponse> extends AbstractRe
 
     @Override
     public AsyncResponseWrapper<ODataDeleteResponse> asyncDelete() {
-      return 
odataClient.getAsyncRequestFactory().<ODataDeleteResponse>getAsyncRequestWrapper(
-              
odataClient.getCUDRequestFactory().getDeleteRequest(location)).execute();
+      return odataClient.getAsyncRequestFactory().<ODataDeleteResponse> 
getAsyncRequestWrapper(
+          
odataClient.getCUDRequestFactory().getDeleteRequest(location)).execute();
     }
 
     @Override
@@ -244,10 +244,11 @@ public class AsyncRequestWrapperImpl<R extends 
ODataResponse> extends AbstractRe
     private R instantiateResponse(final HttpResponse res) {
       R odataResponse;
       try {
-        odataResponse = (R) ((AbstractODataRequest) 
odataRequest).getResponseTemplate().
-                initFromEnclosedPart(res.getEntity().getContent());
+        odataResponse = (R) ((AbstractODataRequest) 
odataRequest).getResponseTemplate().initFromEnclosedPart(res
+            .getEntity().getContent());
 
       } catch (Exception e) {
+        HttpClientUtils.closeQuietly(res);
         LOG.error("Error instantiating odata response", e);
         odataResponse = null;
       }
@@ -261,7 +262,7 @@ public class AsyncRequestWrapperImpl<R extends 
ODataResponse> extends AbstractRe
         this.location = URI.create(headers[0].getValue());
       } else {
         throw new AsyncRequestException(
-                "Invalid async request response. Monitor URL '" + 
headers[0].getValue() + "'");
+            "Invalid async request response. Monitor URL '" + 
headers[0].getValue() + "'");
       }
 
       headers = res.getHeaders(HttpHeader.RETRY_AFTER);
@@ -290,8 +291,8 @@ public class AsyncRequestWrapperImpl<R extends 
ODataResponse> extends AbstractRe
       throw new AsyncRequestException("Invalid async request response. Missing 
monitor URL");
     }
 
-    final HttpUriRequest monitor = 
odataClient.getConfiguration().getHttpUriRequestFactory().
-            create(HttpMethod.GET, location);
+    final HttpUriRequest monitor = 
odataClient.getConfiguration().getHttpUriRequestFactory().create(HttpMethod.GET,
+        location);
 
     return executeHttpRequest(httpClient, monitor);
   }

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/3e9e6931/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/response/AbstractODataResponse.java
----------------------------------------------------------------------
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 58923cd..94460d9 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
@@ -1,18 +1,18 @@
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
+ * or more contributor license agreements. See the NOTICE file
  * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
+ * regarding copyright ownership. The ASF licenses this file
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
+ * with the License. You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
+ * KIND, either express or implied. See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
@@ -34,6 +34,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.utils.HttpClientUtils;
 import org.apache.olingo.client.api.ODataClient;
 import org.apache.olingo.client.api.communication.request.ODataStreamer;
 import 
org.apache.olingo.client.api.communication.request.batch.ODataBatchLineIterator;
@@ -74,7 +75,7 @@ public abstract class AbstractODataResponse implements 
ODataResponse {
    * Response headers.
    */
   protected final Map<String, Collection<String>> headers =
-          new TreeMap<String, 
Collection<String>>(String.CASE_INSENSITIVE_ORDER);
+      new TreeMap<String, Collection<String>>(String.CASE_INSENSITIVE_ORDER);
 
   /**
    * Response code.
@@ -102,7 +103,7 @@ public abstract class AbstractODataResponse implements 
ODataResponse {
   protected ODataBatchController batchInfo = null;
 
   public AbstractODataResponse(
-          final ODataClient odataClient, final HttpClient httpclient, final 
HttpResponse res) {
+      final ODataClient odataClient, final HttpClient httpclient, final 
HttpResponse res) {
 
     this.odataClient = odataClient;
     this.httpClient = httpclient;
@@ -149,13 +150,14 @@ public abstract class AbstractODataResponse implements 
ODataResponse {
     try {
       this.payload = res.getEntity() == null ? null : 
res.getEntity().getContent();
     } catch (final IllegalStateException e) {
+      HttpClientUtils.closeQuietly(res);
       LOG.error("Error retrieving payload", e);
       throw new ODataRuntimeException(e);
     } catch (final IOException e) {
+      HttpClientUtils.closeQuietly(res);
       LOG.error("Error retrieving payload", e);
       throw new ODataRuntimeException(e);
     }
-
     for (Header header : res.getAllHeaders()) {
       final Collection<String> headerValues;
       if (headers.containsKey(header.getName())) {
@@ -177,10 +179,10 @@ public abstract class AbstractODataResponse implements 
ODataResponse {
 
   @Override
   public ODataResponse initFromBatch(
-          final Map.Entry<Integer, String> responseLine,
-          final Map<String, Collection<String>> headers,
-          final ODataBatchLineIterator batchLineIterator,
-          final String boundary) {
+      final Map.Entry<Integer, String> responseLine,
+      final Map<String, Collection<String>> headers,
+      final ODataBatchLineIterator batchLineIterator,
+      final String boundary) {
 
     if (hasBeenInitialized) {
       throw new IllegalStateException("Request already initialized");
@@ -204,7 +206,7 @@ public abstract class AbstractODataResponse implements 
ODataResponse {
       }
 
       final ODataBatchLineIteratorImpl batchLineIterator =
-              new ODataBatchLineIteratorImpl(IOUtils.lineIterator(part, 
Constants.UTF8));
+          new ODataBatchLineIteratorImpl(IOUtils.lineIterator(part, 
Constants.UTF8));
 
       final Map.Entry<Integer, String> partResponseLine = 
ODataBatchUtilities.readResponseLine(batchLineIterator);
       LOG.debug("Retrieved async item response {}", partResponseLine);

Reply via email to