You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by mi...@apache.org on 2016/02/07 20:01:30 UTC

[2/2] olingo-odata4 git commit: [OLINGO-832] Pass out stream to json generator

[OLINGO-832] Pass out stream to json generator


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

Branch: refs/heads/OLINGO-832_StreamSerializerPoC
Commit: a6e0fb1a562b7a3801c79fd0d3dcb19a5a323f3c
Parents: 2a80ca3
Author: mibo <mi...@apache.org>
Authored: Sun Feb 7 20:01:15 2016 +0100
Committer: mibo <mi...@apache.org>
Committed: Sun Feb 7 20:01:15 2016 +0100

----------------------------------------------------------------------
 .../serializer/ChannelSerializerResult.java     | 57 +++++++++++++++++---
 1 file changed, 50 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a6e0fb1a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/ChannelSerializerResult.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/ChannelSerializerResult.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/ChannelSerializerResult.java
index a3b6b0c..60b62f0 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/ChannelSerializerResult.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/ChannelSerializerResult.java
@@ -23,13 +23,13 @@ import com.fasterxml.jackson.core.JsonGenerator;
 import org.apache.olingo.commons.api.data.Entity;
 import org.apache.olingo.commons.api.data.EntityIterator;
 import org.apache.olingo.commons.api.edm.EdmEntityType;
+import org.apache.olingo.commons.api.ex.ODataRuntimeException;
 import org.apache.olingo.server.api.ServiceMetadata;
 import org.apache.olingo.server.api.serializer.EntitySerializerOptions;
 import org.apache.olingo.server.api.serializer.SerializerException;
 import org.apache.olingo.server.api.serializer.SerializerResult;
 import org.apache.olingo.server.core.serializer.json.ODataJsonStreamSerializer;
 import org.apache.olingo.server.core.serializer.utils.CircleStreamBuffer;
-import org.apache.olingo.server.core.serializer.utils.ResultHelper;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -41,7 +41,7 @@ import java.nio.channels.WritableByteChannel;
 import java.nio.charset.Charset;
 
 public class ChannelSerializerResult implements SerializerResult {
-  private ReadableByteChannel channel;
+  private StreamChannel channel;
 
   private static class StreamChannel implements ReadableByteChannel {
     private static final Charset DEFAULT = Charset.forName("UTF-8");
@@ -65,6 +65,44 @@ public class ChannelSerializerResult implements SerializerResult {
       this.tail = ByteBuffer.wrap(tail.getBytes(DEFAULT));
     }
 
+    public boolean write(OutputStream out) throws IOException {
+      if(head.hasRemaining()) {
+        out.write(head.array());
+        head.flip();
+        return true;
+      }
+      if (coll.hasNext()) {
+        try {
+          writeEntity(coll.next(), out);
+          if(coll.hasNext()) {
+            out.write(",".getBytes(DEFAULT));
+          }
+          return true;
+        } catch (SerializerException e) {
+        }
+      } else if(tail.hasRemaining()) {
+        out.write(tail.array());
+        tail.flip();
+        return true;
+      }
+      return false;
+    }
+
+
+    private void writeEntity(Entity entity, OutputStream outputStream) throws SerializerException {
+      try {
+        JsonGenerator json = new JsonFactory().createGenerator(outputStream);
+        jsonSerializer.writeEntity(metadata, entityType, entity, null,
+            options == null ? null : options.getExpand(),
+            options == null ? null : options.getSelect(),
+            options != null && options.getWriteOnlyReferences(),
+            json);
+        json.flush();
+      } catch (final IOException e) {
+        throw new ODataRuntimeException("Failed entity serialization");
+      }
+    }
+
     @Override
     public int read(ByteBuffer dest) throws IOException {
       ByteBuffer buffer = getCurrentBuffer();
@@ -161,12 +199,17 @@ public class ChannelSerializerResult implements SerializerResult {
 
   @Override
   public void writeContent(WritableByteChannel writeChannel) {
-    // TODO: mibo: replace with passing 'writeChannel' to json serializer
-    ResultHelper.copy(this.channel, writeChannel);
+    try {
+      boolean contentAvailable = true;
+      while(contentAvailable) {
+        contentAvailable = this.channel.write(Channels.newOutputStream(writeChannel));
+      }
+    } catch (IOException e) {
+      e.printStackTrace();
+    }
   }
 
-
-  private ChannelSerializerResult(ReadableByteChannel channel) {
+  private ChannelSerializerResult(StreamChannel channel) {
     this.channel = channel;
   }
 
@@ -206,7 +249,7 @@ public class ChannelSerializerResult implements SerializerResult {
     }
 
     public SerializerResult build() {
-      ReadableByteChannel input = new StreamChannel(coll, entityType, head, jsonSerializer, metadata, options, tail);
+      StreamChannel input = new StreamChannel(coll, entityType, head, jsonSerializer, metadata, options, tail);
       return new ChannelSerializerResult(input);
     }
   }