You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2019/08/15 05:43:43 UTC

[camel] 05/06: CAMEL-13852: refactoring remove duplicated code

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 6d3ccf5641af3ee27f08b6b1d8a824656c9f2342
Author: Marc Giger <gi...@gmx.ch>
AuthorDate: Wed Aug 14 19:58:55 2019 +0200

    CAMEL-13852: refactoring remove duplicated code
---
 .../component/olingo4/api/impl/Olingo4AppImpl.java | 75 ++++++++++++----------
 1 file changed, 40 insertions(+), 35 deletions(-)

diff --git a/components/camel-olingo4/camel-olingo4-api/src/main/java/org/apache/camel/component/olingo4/api/impl/Olingo4AppImpl.java b/components/camel-olingo4/camel-olingo4-api/src/main/java/org/apache/camel/component/olingo4/api/impl/Olingo4AppImpl.java
index e1b8c8b..bf0b2ed 100644
--- a/components/camel-olingo4/camel-olingo4-api/src/main/java/org/apache/camel/component/olingo4/api/impl/Olingo4AppImpl.java
+++ b/components/camel-olingo4/camel-olingo4-api/src/main/java/org/apache/camel/component/olingo4/api/impl/Olingo4AppImpl.java
@@ -512,59 +512,33 @@ public final class Olingo4AppImpl implements Olingo4App {
     }
 
     private AbstractHttpEntity writeContent(final Edm edm, final UriInfo uriInfo, final Object content) throws ODataException {
-        InputStream requestStream = null;
-        AbstractHttpEntity httpEntity = null;
+        AbstractHttpEntity httpEntity;
+
         if (uriInfo.getKind() == UriInfoKind.resource) {
             // any resource entity
             List<UriResource> listResource = uriInfo.getUriResourceParts();
             UriResourceKind lastResourceKind = listResource.get(listResource.size() - 1).getKind();
             switch (lastResourceKind) {
             case action:
-                if (content == null) {
-                    requestStream = new ByteArrayInputStream(new byte[0]);
-                } else if (content instanceof ClientEntity) {
-                    requestStream = odataWriter.writeEntity((ClientEntity)content, getResourceContentType(uriInfo));
-                } else if (content instanceof String) {
-                    httpEntity = new StringEntity((String) content, org.apache.http.entity.ContentType.APPLICATION_JSON);
-                    httpEntity.setChunked(false);
-                    return httpEntity;
+                if (content == null) { // actions may have no input
+                    httpEntity = new ByteArrayEntity(new byte[0]);
                 } else {
-                    throw new ODataException("Unsupported content type: " + content);
+                    httpEntity = writeContent(uriInfo, content);
                 }
                 break;
             case entitySet:
-                if (content instanceof ClientEntity) {
-                    requestStream = odataWriter.writeEntity((ClientEntity)content, getResourceContentType(uriInfo));
-                } else if (content instanceof String) {
-                    httpEntity = new StringEntity((String) content, org.apache.http.entity.ContentType.APPLICATION_JSON);
-                    httpEntity.setChunked(false);
-                    return httpEntity;
-                } else {
-                    throw new ODataException("Unsupported content type: " + content);
-                }
+                httpEntity = writeContent(uriInfo, content);
                 break;
             default:
                 throw new ODataException("Unsupported resource type: " + lastResourceKind);
             }
-            try {
-                httpEntity = new ByteArrayEntity(IOUtils.toByteArray(requestStream));
-            } catch (IOException e) {
-                throw new ODataException("Error during converting input stream to byte array", e);
-            }
-            httpEntity.setChunked(false);
-
         } else if (uriInfo.getKind() == UriInfoKind.batch) {
             final String boundary = BOUNDARY_PREFIX + UUID.randomUUID();
             final String contentHeader = BATCH_CONTENT_TYPE + BOUNDARY_PARAMETER + boundary;
-            final List<Olingo4BatchRequest> batchParts = (List<Olingo4BatchRequest>)content;
+            final List<Olingo4BatchRequest> batchParts = (List<Olingo4BatchRequest>) content;
 
-            requestStream = serializeBatchRequest(edm, batchParts, BOUNDARY_DOUBLE_DASH + boundary);
-            try {
-                httpEntity = new ByteArrayEntity(IOUtils.toByteArray(requestStream));
-            } catch (IOException e) {
-                throw new ODataException("Error during converting input stream to byte array", e);
-            }
-            httpEntity.setChunked(false);
+            final InputStream requestStream = serializeBatchRequest(edm, batchParts, BOUNDARY_DOUBLE_DASH + boundary);
+            httpEntity = writeContent(requestStream);
             httpEntity.setContentType(contentHeader);
         } else {
             throw new ODataException("Unsupported resource type: " + uriInfo.getKind().name());
@@ -573,6 +547,37 @@ public final class Olingo4AppImpl implements Olingo4App {
         return httpEntity;
     }
 
+    private AbstractHttpEntity writeContent(UriInfo uriInfo, Object content) throws ODataException {
+        AbstractHttpEntity httpEntity;
+
+        if (content instanceof ClientEntity) {
+            final InputStream requestStream = odataWriter.writeEntity((ClientEntity) content, getResourceContentType(uriInfo));
+            httpEntity = writeContent(requestStream);
+        } else if (content instanceof String) {
+            httpEntity = new StringEntity((String) content, org.apache.http.entity.ContentType.APPLICATION_JSON);
+        } else {
+            throw new ODataException("Unsupported content type: " + content);
+        }
+
+        httpEntity.setChunked(false);
+
+        return httpEntity;
+    }
+
+    private AbstractHttpEntity writeContent(InputStream inputStream) throws ODataException {
+        AbstractHttpEntity httpEntity;
+        
+        try {
+            httpEntity = new ByteArrayEntity(IOUtils.toByteArray(inputStream));
+        } catch (IOException e) {
+            throw new ODataException("Error during converting input stream to byte array", e);
+        }
+
+        httpEntity.setChunked(false);
+
+        return httpEntity;
+    }
+
     private InputStream serializeBatchRequest(final Edm edm, final List<Olingo4BatchRequest> batchParts, String boundary) throws ODataException {
         final ByteArrayOutputStream batchRequestHeaderOutputStream = new ByteArrayOutputStream();