jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sal...@apache.org
Subject svn commit: r1351810 - in /jena/Experimental/jena-client: pom.xml src/main/java/org/apache/jena/client/http/HttpConnection.java src/main/java/org/apache/jena/client/http/HttpUpdater.java
Date Tue, 19 Jun 2012 19:28:06 GMT
Author: sallen
Date: Tue Jun 19 19:28:06 2012
New Revision: 1351810

URL: http://svn.apache.org/viewvc?rev=1351810&view=rev
Log:
jena-client - 1) Bumped version to 0.0.2-SNAPSHOT.  2) Added ability to choose between "application/sparql-update"
and "application/x-www-form-urlencoded" encoding for HttpUpdater.

Modified:
    jena/Experimental/jena-client/pom.xml
    jena/Experimental/jena-client/src/main/java/org/apache/jena/client/http/HttpConnection.java
    jena/Experimental/jena-client/src/main/java/org/apache/jena/client/http/HttpUpdater.java

Modified: jena/Experimental/jena-client/pom.xml
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-client/pom.xml?rev=1351810&r1=1351809&r2=1351810&view=diff
==============================================================================
--- jena/Experimental/jena-client/pom.xml (original)
+++ jena/Experimental/jena-client/pom.xml Tue Jun 19 19:28:06 2012
@@ -20,7 +20,7 @@
   <modelVersion>4.0.0</modelVersion>
   <groupId>org.apache.jena</groupId>
   <artifactId>jena-client</artifactId>
-  <version>0.0.1-SNAPSHOT</version>
+  <version>0.0.2-SNAPSHOT</version>
   <name>JenaClient</name>
   <description>A Jena library for interacting with SPARQL endpoints.</description>
   

Modified: jena/Experimental/jena-client/src/main/java/org/apache/jena/client/http/HttpConnection.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-client/src/main/java/org/apache/jena/client/http/HttpConnection.java?rev=1351810&r1=1351809&r2=1351810&view=diff
==============================================================================
--- jena/Experimental/jena-client/src/main/java/org/apache/jena/client/http/HttpConnection.java
(original)
+++ jena/Experimental/jena-client/src/main/java/org/apache/jena/client/http/HttpConnection.java
Tue Jun 19 19:28:06 2012
@@ -17,6 +17,8 @@ public class HttpConnection extends Conn
     private String queryEndpoint;
     private String updateEndpoint;
     
+    private boolean useContentTypeSPARQLUpdate = true;
+    
     HttpConnection(HttpRepository repo, String queryEndpoint, String updateEndpoint)
     {
         if (null == repo)
@@ -29,6 +31,18 @@ public class HttpConnection extends Conn
         this.updateEndpoint = updateEndpoint;
     }
     
+    /**
+     * Whether or not to use <code>"application/sparql-update"</code> as the
content type for SPARQL Update.  Defaults to true.
+     * If set to false then <code>"application/x-www-form-urlencoded"</code>
is used instead (however, be warned that urlencoded
+     * is not streaming and will buffer the entire update into memory.
+     * 
+     * @param useContentTypeSPARQLUpdate true if <code>"application/sparql-update"</code>
is desired; true is the default
+     */
+    public void useContentTypeSPARQLUpdate(boolean useContentTypeSPARQLUpdate)
+    {
+        this.useContentTypeSPARQLUpdate = useContentTypeSPARQLUpdate;
+    }
+    
     @Override
     public boolean hasBulkUpdater()
     {
@@ -38,7 +52,7 @@ public class HttpConnection extends Conn
     @Override
     protected Updater doCreateBulkUpdater()
     {
-        HttpUpdater updater = new HttpUpdater(updateEndpoint);
+        HttpUpdater updater = new HttpUpdater(updateEndpoint, useContentTypeSPARQLUpdate);
         updater.open();
         return updater;
     }

Modified: jena/Experimental/jena-client/src/main/java/org/apache/jena/client/http/HttpUpdater.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-client/src/main/java/org/apache/jena/client/http/HttpUpdater.java?rev=1351810&r1=1351809&r2=1351810&view=diff
==============================================================================
--- jena/Experimental/jena-client/src/main/java/org/apache/jena/client/http/HttpUpdater.java
(original)
+++ jena/Experimental/jena-client/src/main/java/org/apache/jena/client/http/HttpUpdater.java
Tue Jun 19 19:28:06 2012
@@ -1,8 +1,10 @@
 package org.apache.jena.client.http;
 
-import java.io.FilterOutputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.UnsupportedEncodingException;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -24,24 +26,33 @@ public class HttpUpdater extends Updater
 {
     private final String updateEndpoint;
     private final SerializationContext sCxt;
+    private final boolean useContentTypeSPARQLUpdate;
     
     private HttpURLConnection conn;
     private OutputStream out;
     private UpdateWriter writer;
     private boolean opened = false;
     
+    private ByteArrayOutputStream baos;
+    
     public HttpUpdater(String updateEndpoint)
     {
-        this(updateEndpoint, null);
+        this(updateEndpoint, true);
+    }
+    
+    public HttpUpdater(String updateEndpoint, boolean useContentTypeSPARQLUpdate)
+    {
+        this(updateEndpoint, useContentTypeSPARQLUpdate, null);
     }
     
-    public HttpUpdater(String updateEndpoint, SerializationContext sCxt)
+    public HttpUpdater(String updateEndpoint, boolean useContentTypeSPARQLUpdate, SerializationContext
sCxt)
     {
         if (null == updateEndpoint) {
             throw new IllegalArgumentException("updateEndpoint must be specified if performing
update operations");
         }
         
         this.updateEndpoint = updateEndpoint;
+        this.useContentTypeSPARQLUpdate = useContentTypeSPARQLUpdate;
         this.sCxt = sCxt;
     }
     
@@ -59,20 +70,30 @@ public class HttpUpdater extends Updater
             conn = (HttpURLConnection)urlConn;
             conn.setDoOutput(true);
             conn.setRequestMethod("POST");
-            //conn.setRequestProperty("Content-Type", WebContent.contentTypeSPARQLUpdate);
-            conn.setRequestProperty("Content-Type", WebContent.contentTypeForm);
             // TODO Potential optimization: allow gzip when we know we're dealing with Fuseki
(also update Fuseki to allow this!)
             //conn.setRequestProperty("Content-Encoding", "gzip");
             conn.setChunkedStreamingMode(4096);
+            
+            String contentType = useContentTypeSPARQLUpdate ? WebContent.contentTypeSPARQLUpdate
: WebContent.contentTypeForm;
+            conn.setRequestProperty("Content-Type", contentType);
+            
             conn.connect();
             
             out = conn.getOutputStream();
             //out = new GZIPOutputStream(conn.getOutputStream());
             
-            //IndentedWriter iw = new IndentedWriter(out);
-            out.write("update=".getBytes("UTF-8"));
-            IndentedWriter iw = new IndentedWriter(new URLEncoderOutputStream(out));
-            
+            IndentedWriter iw;
+            if (useContentTypeSPARQLUpdate)
+            {
+                iw = new IndentedWriter(out);
+            }
+            else
+            {
+                out.write("update=".getBytes("UTF-8"));
+                //iw = new IndentedWriter(new URLEncoderOutputStream(out));
+                baos = new ByteArrayOutputStream();
+                iw = new IndentedWriter(baos);
+            }
             
             writer = new UpdateWriter(iw, sCxt);
             writer.open();
@@ -124,6 +145,28 @@ public class HttpUpdater extends Updater
         if (opened)
         {
             writer.close();
+            
+            if (!useContentTypeSPARQLUpdate)
+            {
+                OutputStreamWriter o = new OutputStreamWriter(out);
+                try
+                {
+                    String encoded = URLEncoder.encode(baos.toString(), "UTF-8");
+                    baos = null;
+                    o.write(encoded);
+                    o.close();
+                }
+                catch (UnsupportedEncodingException e)
+                {
+                    // Should never happen...
+                    throw new UpdateException(e);
+                }
+                catch (IOException e)
+                {
+                    throw new UpdateException(e);
+                }
+            }
+            
             try
             {
                 out.close();
@@ -153,19 +196,19 @@ public class HttpUpdater extends Updater
     }
     
     
-    // TODO this is not efficient.  And is it even correct?  Will it properly escape UTF
codepoints over a byte in size?
-    private class URLEncoderOutputStream extends FilterOutputStream
-    {
-        public URLEncoderOutputStream(OutputStream out)
-        {
-            super(out);
-        }
-        
-        @Override
-        public void write(int b) throws IOException
-        {
-            String enc = URLEncoder.encode(new String(new byte[] { (byte)b }), "UTF-8");
-            out.write(enc.getBytes("UTF-8"));
-        }
-    }
+//    // TODO this is not efficient.  And is it even correct?  Will it properly escape UTF
codepoints over a byte in size?
+//    private class URLEncoderOutputStream extends FilterOutputStream
+//    {
+//        public URLEncoderOutputStream(OutputStream out)
+//        {
+//            super(out);
+//        }
+//        
+//        @Override
+//        public void write(int b) throws IOException
+//        {
+//            String enc = URLEncoder.encode(new String(new byte[] { (byte)b }), "UTF-8");
+//            out.write(enc.getBytes("UTF-8"));
+//        }
+//    }
 }



Mime
View raw message