You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2019/11/15 12:41:28 UTC

[jena] branch master updated: JENA-1776: Set Content-Length when sending a graph or dataset

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 2d5b86d  JENA-1776: Set Content-Length when sending a graph or dataset
     new d15288a  Merge pull request #628 from afs/jena1776-rdfconnection
2d5b86d is described below

commit 2d5b86d5e91116ea3ba40d20e42295703c0c6d3e
Author: Andy Seaborne <an...@apache.org>
AuthorDate: Sun Nov 10 17:17:55 2019 +0000

    JENA-1776: Set Content-Length when sending a graph or dataset
---
 .../jena/rdfconnection/RDFConnectionFactory.java   |  1 -
 .../jena/rdfconnection/RDFConnectionRemote.java    | 45 +++++++++++++++++++++-
 2 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConnectionFactory.java b/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConnectionFactory.java
index 776817c..b249c29 100644
--- a/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConnectionFactory.java
+++ b/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConnectionFactory.java
@@ -107,7 +107,6 @@ public class RDFConnectionFactory {
             .build();
     }
 
-
     /**
      * Connect to a local (same JVM) dataset.
      * The default isolation is {@code NONE}. 
diff --git a/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConnectionRemote.java b/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConnectionRemote.java
index 4bd57d5..fa76242 100644
--- a/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConnectionRemote.java
+++ b/jena-rdfconnection/src/main/java/org/apache/jena/rdfconnection/RDFConnectionRemote.java
@@ -18,16 +18,19 @@
 
 package org.apache.jena.rdfconnection;
 
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.util.Objects;
 import java.util.function.Supplier;
 
 import org.apache.http.HttpEntity;
 import org.apache.http.client.HttpClient;
+import org.apache.http.entity.ByteArrayEntity;
 import org.apache.http.entity.ContentType;
 import org.apache.http.entity.EntityTemplate;
 import org.apache.http.entity.FileEntity;
 import org.apache.http.protocol.HttpContext;
+import org.apache.jena.atlas.io.IO;
 import org.apache.jena.atlas.lib.InternalErrorException;
 import org.apache.jena.atlas.web.HttpException;
 import org.apache.jena.atlas.web.TypedInputStream;
@@ -492,8 +495,33 @@ public class RDFConnectionRemote implements RDFConnection {
         return graphToHttpEntity(graph, outputTriples);
     }
 
-    /** Create an HttpEntity for the graph */
+    /** Create an HttpEntity for the graph. */
     protected HttpEntity graphToHttpEntity(Graph graph, RDFFormat syntax) {
+        // Length - leaves connection reusable. 
+        return graphToHttpEntityWithLength(graph, syntax);
+    }
+    
+    /** 
+     * Create an HttpEntity for the graph. The HTTP entity will have the length but this
+     * requires serialising the graph at the point when this function is called.  
+     */
+    private HttpEntity graphToHttpEntityWithLength(Graph graph, RDFFormat syntax) {
+        String ct = syntax.getLang().getContentType().getContentType();
+        ByteArrayOutputStream out = new ByteArrayOutputStream(128*1024);
+        RDFDataMgr.write(out, graph, syntax);
+        IO.close(out);
+        ByteArrayEntity entity = new ByteArrayEntity(out.toByteArray());
+        entity.setContentType(ct);
+        return entity;
+    }
+
+    /**
+     * Create an HttpEntity for the graph. The bytes for the graph are written
+     * directly the HTTP stream but the length of the entity will be -1 (unknown).
+     * This does not work over cached connections which need to know when
+     * a request body is finished.
+     */
+    private HttpEntity graphToHttpEntityStream(Graph graph, RDFFormat syntax) {
         EntityTemplate entity = new EntityTemplate((out)->RDFDataMgr.write(out, graph, syntax));
         String ct = syntax.getLang().getContentType().getContentType();
         entity.setContentType(ct);
@@ -507,6 +535,21 @@ public class RDFConnectionRemote implements RDFConnection {
 
     /** Create an HttpEntity for the dataset */
     protected HttpEntity datasetToHttpEntity(DatasetGraph dataset, RDFFormat syntax) {
+        // Length - leaves connection reusable. 
+        return datasetToHttpEntityWithLength(dataset, syntax);
+    }
+        
+    private HttpEntity datasetToHttpEntityWithLength(DatasetGraph dataset, RDFFormat syntax) {
+        String ct = syntax.getLang().getContentType().getContentType();
+        ByteArrayOutputStream out = new ByteArrayOutputStream(128*1024);
+        RDFDataMgr.write(out, dataset, syntax);
+        IO.close(out);
+        ByteArrayEntity entity = new ByteArrayEntity(out.toByteArray());
+        entity.setContentType(ct);
+        return entity;
+    }
+
+    private HttpEntity datasetToHttpEntityStream(DatasetGraph dataset, RDFFormat syntax) {
         EntityTemplate entity = new EntityTemplate((out)->RDFDataMgr.write(out, dataset, syntax));
         String ct = syntax.getLang().getContentType().getContentType();
         entity.setContentType(ct);