You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by sa...@apache.org on 2013/06/05 23:22:21 UTC

svn commit: r1490037 - 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/HttpQueryStatement.java src/main/java/org/apache/jena/client/http/HttpUpdater.java

Author: sallen
Date: Wed Jun  5 21:22:20 2013
New Revision: 1490037

URL: http://svn.apache.org/r1490037
Log:
Allow setting of the Content Types for HttpConnection

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/HttpQueryStatement.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=1490037&r1=1490036&r2=1490037&view=diff
==============================================================================
--- jena/Experimental/jena-client/pom.xml (original)
+++ jena/Experimental/jena-client/pom.xml Wed Jun  5 21:22:20 2013
@@ -20,7 +20,7 @@
   <modelVersion>4.0.0</modelVersion>
   <groupId>org.apache.jena</groupId>
   <artifactId>jena-client</artifactId>
-  <version>0.0.10-SNAPSHOT</version>
+  <version>0.0.11-SNAPSHOT</version>
   <name>Apache Jena - Client (SPARQL 1.1 Client API)</name>
   <description>Client is a library that provides an API for querying and updating local or remote SPARQL 1.1 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=1490037&r1=1490036&r2=1490037&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 Wed Jun  5 21:22:20 2013
@@ -4,6 +4,7 @@ import org.apache.jena.client.Connection
 import org.apache.jena.client.QueryStatement;
 import org.apache.jena.client.UpdateStatement;
 import org.apache.jena.client.Updater;
+import org.apache.jena.riot.WebContent ;
 
 import com.hp.hpl.jena.query.Query;
 import com.hp.hpl.jena.query.ReadWrite;
@@ -16,6 +17,12 @@ public class HttpConnection extends Conn
     
     private boolean useContentTypeSPARQLUpdate = true;
     
+    private String selectContentType = null;
+    private String modelContentType = null;
+    private String askContentType = null;
+    
+    private String updateContentType = null;
+    
     HttpConnection(HttpRepository repo, String queryEndpoint, String updateEndpoint)
     {
         super(repo);
@@ -32,7 +39,37 @@ public class HttpConnection extends Conn
      */
     public void useContentTypeSPARQLUpdate(boolean useContentTypeSPARQLUpdate)
     {
-        this.useContentTypeSPARQLUpdate = useContentTypeSPARQLUpdate;
+        this.updateContentType = useContentTypeSPARQLUpdate ? WebContent.contentTypeSPARQLUpdate : WebContent.contentTypeForm;
+    }
+    
+    /**
+     * Sets the Content Type for SELECT queries provided that the format is supported.
+     * Setting this to <code>null</code> will use Jena's default (currently "application/sparql-results+xml")
+     *  
+     * @param contentType the Content Type to use for SELECT queries
+     */
+    public void setSelectContentType(String contentType) {
+        this.selectContentType = contentType;
+    }
+    
+    /**
+     * Sets the Content Type for CONSTRUCT/DESCRIBE queries provided that the format is supported.
+     * Setting this to <code>null</code> will use Jena's default (currently "application/rdf+xml")
+     *  
+     * @param contentType the Content Type to use for CONSTRUCT/DESCRIBE queries
+     */
+    public void setModelContentType(String contentType) {
+        this.modelContentType = contentType;
+    }
+    
+    /**
+     * Sets the Content Type for ASK queries provided that the format is supported.
+     * Setting this to <code>null</code> will use Jena's default (currently "application/sparql-results+xml")
+     *  
+     * @param contentType the Content Type to use for ASK queries
+     */
+    public void setAskContentType(String contentType) {
+        this.askContentType = contentType;
     }
     
     @Override
@@ -44,7 +81,7 @@ public class HttpConnection extends Conn
     @Override
     protected Updater doCreateStreamingUpdater()
     {
-        HttpUpdater updater = new HttpUpdater(updateEndpoint, useContentTypeSPARQLUpdate);
+        HttpUpdater updater = new HttpUpdater(updateEndpoint, updateContentType);
         updater.open();
         return updater;
     }
@@ -52,13 +89,13 @@ public class HttpConnection extends Conn
     @Override
     public QueryStatement doCreateQueryStatement(String queryString)
     {
-        return new HttpQueryStatement(queryString, queryEndpoint, this);
+        return new HttpQueryStatement(queryString, queryEndpoint, this, selectContentType, modelContentType, askContentType);
     }
 
     @Override
     public QueryStatement doCreateQueryStatement(Query query)
     {
-        return new HttpQueryStatement(query, queryEndpoint, this);
+        return new HttpQueryStatement(query, queryEndpoint, this, selectContentType, modelContentType, askContentType);
     }
     
     @Override

Modified: jena/Experimental/jena-client/src/main/java/org/apache/jena/client/http/HttpQueryStatement.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-client/src/main/java/org/apache/jena/client/http/HttpQueryStatement.java?rev=1490037&r1=1490036&r2=1490037&view=diff
==============================================================================
--- jena/Experimental/jena-client/src/main/java/org/apache/jena/client/http/HttpQueryStatement.java (original)
+++ jena/Experimental/jena-client/src/main/java/org/apache/jena/client/http/HttpQueryStatement.java Wed Jun  5 21:22:20 2013
@@ -9,17 +9,26 @@ import com.hp.hpl.jena.sparql.engine.htt
 class HttpQueryStatement extends QueryStatementBase<QueryEngineHTTP> implements QueryStatement
 {
     private final String queryEndpoint;
+    private final String selectContentType;
+    private final String modelContentType;
+    private final String askContentType;
     
-    HttpQueryStatement(String queryString, String queryEndpoint, HttpConnection connection)
+    HttpQueryStatement(String queryString, String queryEndpoint, HttpConnection connection, String selectContentType, String modelContentType, String askContentType)
     {
         super(queryString, connection);
         this.queryEndpoint = queryEndpoint;
+        this.selectContentType = selectContentType;
+        this.modelContentType = modelContentType;
+        this.askContentType = askContentType;
     }
     
-    HttpQueryStatement(Query query, String queryEndpoint, HttpConnection connection)
+    HttpQueryStatement(Query query, String queryEndpoint, HttpConnection connection, String selectContentType, String modelContentType, String askContentType)
     {
         super(query, connection);
         this.queryEndpoint = queryEndpoint;
+        this.selectContentType = selectContentType;
+        this.modelContentType = modelContentType;
+        this.askContentType = askContentType;
     }
     
     protected QueryEngineHTTP createQueryExecution()
@@ -27,6 +36,10 @@ class HttpQueryStatement extends QuerySt
         QueryEngineHTTP qe = (null != queryString) ?
             new QueryEngineHTTP(queryEndpoint, queryString) :
             new QueryEngineHTTP(queryEndpoint, queryObject);
+        
+        if (null != selectContentType) qe.setSelectContentType(selectContentType);
+        if (null != modelContentType) qe.setModelContentType(modelContentType);
+        if (null != askContentType) qe.setAskContentType(askContentType);
         return qe;
     }
 }

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=1490037&r1=1490036&r2=1490037&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 Wed Jun  5 21:22:20 2013
@@ -28,7 +28,8 @@ public class HttpUpdater extends Updater
 {
     private final String updateEndpoint;
     private final SerializationContext sCxt;
-    private final boolean useContentTypeSPARQLUpdate;
+    private final String contentType;
+    private final boolean streamingWriter;
     
     private HttpURLConnection conn;
     private OutputStream out;
@@ -39,22 +40,28 @@ public class HttpUpdater extends Updater
     
     public HttpUpdater(String updateEndpoint)
     {
-        this(updateEndpoint, true);
+        this(updateEndpoint, null);
     }
     
-    public HttpUpdater(String updateEndpoint, boolean useContentTypeSPARQLUpdate)
+    public HttpUpdater(String updateEndpoint, String contentType)
     {
-        this(updateEndpoint, useContentTypeSPARQLUpdate, null);
+        this(updateEndpoint, contentType, null);
     }
     
-    public HttpUpdater(String updateEndpoint, boolean useContentTypeSPARQLUpdate, SerializationContext sCxt)
+    public HttpUpdater(String updateEndpoint, String contentType, SerializationContext sCxt)
     {
-        if (null == updateEndpoint) {
-            throw new IllegalArgumentException("updateEndpoint must be specified if performing update operations");
+        if (null == updateEndpoint)
+        {
+            throw new NullPointerException("updateEndpoint");
+        }
+        if (null == contentType)
+        {
+            contentType = WebContent.contentTypeSPARQLUpdate;
         }
         
         this.updateEndpoint = updateEndpoint;
-        this.useContentTypeSPARQLUpdate = useContentTypeSPARQLUpdate;
+        this.contentType = contentType;
+        this.streamingWriter = WebContent.contentTypeSPARQLUpdate.equals(contentType);
         this.sCxt = sCxt;
     }
     
@@ -76,7 +83,6 @@ public class HttpUpdater extends Updater
             //conn.setRequestProperty("Content-Encoding", "gzip");
             conn.setChunkedStreamingMode(4096);
             
-            String contentType = useContentTypeSPARQLUpdate ? WebContent.contentTypeSPARQLUpdate : WebContent.contentTypeForm;
             conn.setRequestProperty("Content-Type", contentType);
             
             conn.connect();
@@ -85,7 +91,7 @@ public class HttpUpdater extends Updater
             //out = new GZIPOutputStream(conn.getOutputStream());
             
             IndentedWriter iw;
-            if (useContentTypeSPARQLUpdate)
+            if (streamingWriter)
             {
                 iw = new IndentedWriter(out);
             }
@@ -148,7 +154,7 @@ public class HttpUpdater extends Updater
         {
             writer.close();
             
-            if (!useContentTypeSPARQLUpdate)
+            if (!streamingWriter)
             {
                 OutputStreamWriter o = new OutputStreamWriter(out);
                 try