jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sal...@apache.org
Subject svn commit: r1340329 - in /jena/Experimental/jena-client/src/main/java/org/apache/jena/client: ParameterizerBase.java QueryParameterizerBase.java QueryStatementBase.java UpdateStatementBase.java http/SparqlHttpUpdater.java
Date Sat, 19 May 2012 01:14:40 GMT
Author: sallen
Date: Sat May 19 01:14:40 2012
New Revision: 1340329

URL: http://svn.apache.org/viewvc?rev=1340329&view=rev
Log:
jena-client Fixed SPARQL Update HTTP Protocol.

Added:
    jena/Experimental/jena-client/src/main/java/org/apache/jena/client/ParameterizerBase.java
Removed:
    jena/Experimental/jena-client/src/main/java/org/apache/jena/client/QueryParameterizerBase.java
Modified:
    jena/Experimental/jena-client/src/main/java/org/apache/jena/client/QueryStatementBase.java
    jena/Experimental/jena-client/src/main/java/org/apache/jena/client/UpdateStatementBase.java
    jena/Experimental/jena-client/src/main/java/org/apache/jena/client/http/SparqlHttpUpdater.java

Added: jena/Experimental/jena-client/src/main/java/org/apache/jena/client/ParameterizerBase.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-client/src/main/java/org/apache/jena/client/ParameterizerBase.java?rev=1340329&view=auto
==============================================================================
--- jena/Experimental/jena-client/src/main/java/org/apache/jena/client/ParameterizerBase.java
(added)
+++ jena/Experimental/jena-client/src/main/java/org/apache/jena/client/ParameterizerBase.java
Sat May 19 01:14:40 2012
@@ -0,0 +1,189 @@
+package org.apache.jena.client;
+
+import java.net.URL;
+import java.util.Calendar;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+import org.apache.jena.iri.IRI;
+import org.openjena.atlas.lib.NotImplemented;
+
+import com.hp.hpl.jena.datatypes.RDFDatatype;
+import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
+import com.hp.hpl.jena.datatypes.xsd.XSDDateTime;
+import com.hp.hpl.jena.graph.Node;
+import com.hp.hpl.jena.graph.impl.LiteralLabel;
+import com.hp.hpl.jena.graph.impl.LiteralLabelFactory;
+import com.hp.hpl.jena.query.Query;
+import com.hp.hpl.jena.rdf.model.Literal;
+import com.hp.hpl.jena.rdf.model.RDFNode;
+import com.hp.hpl.jena.sparql.serializer.SerializationContext;
+import com.hp.hpl.jena.sparql.util.FmtUtils;
+import com.hp.hpl.jena.sparql.util.NodeFactory;
+
+/**
+ * 
+ */
+// TODO we're currently taking the approach of just creating Nodes.  We don't prevent users
from shooting themselves in the foot by creating invalid nodes...
+abstract class ParameterizerBase implements Parameterizer
+{
+    protected final Map<String,Node> params = new HashMap<String,Node>();
+//    protected String[] rawStrings = new String[]{};
+    
+    // TODO think about getting rid of the regex and use a true SPARQL parser, so we can
understand quotes and comments
+    protected String buildFromString(String queryString, SerializationContext context)
+    {
+        if (null == context) context = new SerializationContext();
+        
+//        // Do the raw strings first
+//        Object[] escapedStrings = new Object[rawStrings.length];
+//        for (int i=0; i<rawStrings.length; i++)
+//        {
+//            escapedStrings[i] = FmtUtils.stringEsc(rawStrings[i]);
+//        }
+//        // TODO check the sparql injection implications of this
+//        String toReturn = String.format(queryString, (Object[])rawStrings);
+        
+        // Then do the regular parameters second
+        String toReturn = queryString;
+        for (Map.Entry<String, Node> entry : params.entrySet())
+        {
+            // TODO Grabbed this from Rob's ParameterizedSparqlString class, doesn't limit
itself to only variables (which is bad)...
+            Pattern p = Pattern.compile("([?$]" + entry.getKey() + ")([^\\w]|$)");
+            toReturn = p.matcher(toReturn).replaceAll(FmtUtils.stringForNode(entry.getValue(),
context) + "$2");
+        }
+        
+        return toReturn;
+    }
+    
+    protected Query buildFromQuery(Query query)
+    {
+        throw new NotImplemented("Building from a Query object is not implemented yet!");
+    }
+
+    @Override
+    public void clearParam(String var)
+    {
+        params.remove(var);
+    }
+    
+    @Override
+    public void clearParams()
+    {
+        params.clear();
+    }
+
+    @Override
+    public void setParam(String var, Node node)
+    {
+        if (var == null) throw new IllegalArgumentException("var cannot be null");
+        if (var.startsWith("?") || var.startsWith("$")) var = var.substring(1);
+        if (node != null)
+        {
+            params.put(var, node);
+        }
+        else
+        {
+            clearParam(var);
+        }
+    }
+
+    @Override
+    public void setParam(String var, RDFNode node)
+    {
+        setParam(var, node.asNode());
+    }
+
+    @Override
+    public void setIri(String var, String iri)
+    {
+        setParam(var, Node.createURI(iri));
+    }
+
+    @Override
+    public void setIri(String var, IRI iri)
+    {
+        setIri(var, iri.toString());
+    }
+
+    @Override
+    public void setIri(String var, URL url)
+    {
+        setIri(var, url.toString());
+    }
+
+    @Override
+    public void setLiteral(String var, Literal lit)
+    {
+        setParam(var, lit);
+    }
+
+    @Override
+    public void setLiteral(String var, String value)
+    {
+        setParam(var, Node.createLiteral(value));
+    }
+
+    @Override
+    public void setLiteral(String var, String value, String lang)
+    {
+        setParam(var, NodeFactory.createLiteralNode(value, lang, null));
+    }
+
+    @Override
+    public void setLiteral(String var, String value, RDFDatatype datatype)
+    {
+        setParam(var, Node.createLiteral(value, datatype));
+    }
+
+    @Override
+    public void setLiteral(String var, boolean value)
+    {
+        setParam(var, Node.createLiteral(Boolean.toString(value), XSDDatatype.XSDboolean));
+    }
+
+    @Override
+    public void setLiteral(String var, int i)
+    {
+        setParam(var, Node.createLiteral(Integer.toString(i), XSDDatatype.XSDinteger));
+    }
+
+    @Override
+    public void setLiteral(String var, long l)
+    {
+        setParam(var, Node.createLiteral(Long.toString(l), XSDDatatype.XSDlong));
+    }
+
+    @Override
+    public void setLiteral(String var, float f)
+    {
+        setParam(var, Node.createLiteral(Float.toString(f), XSDDatatype.XSDfloat));
+    }
+
+    @Override
+    public void setLiteral(String var, double d)
+    {
+        setParam(var, Node.createLiteral(Double.toString(d), XSDDatatype.XSDdouble));
+    }
+
+    @Override
+    public void setLiteral(String var, Calendar dt)
+    {
+        Object value = new XSDDateTime(dt);
+        LiteralLabel ll = LiteralLabelFactory.create(value, "", XSDDatatype.XSDdateTime);
+        setParam(var, Node.createLiteral(ll));
+    }
+    
+//    @Override
+//    public void clearRawStrings()
+//    {
+//        rawStrings = new String[]{};
+//    }
+//
+//    @Override
+//    public void setRawStrings(String... args)
+//    {
+//        rawStrings = args;
+//    }
+}

Modified: jena/Experimental/jena-client/src/main/java/org/apache/jena/client/QueryStatementBase.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-client/src/main/java/org/apache/jena/client/QueryStatementBase.java?rev=1340329&r1=1340328&r2=1340329&view=diff
==============================================================================
--- jena/Experimental/jena-client/src/main/java/org/apache/jena/client/QueryStatementBase.java
(original)
+++ jena/Experimental/jena-client/src/main/java/org/apache/jena/client/QueryStatementBase.java
Sat May 19 01:14:40 2012
@@ -11,7 +11,7 @@ import com.hp.hpl.jena.rdf.model.Stateme
 import com.hp.hpl.jena.rdf.model.impl.StmtIteratorImpl;
 import com.hp.hpl.jena.sparql.util.ModelUtils;
 
-public abstract class QueryStatementBase extends QueryParameterizerBase implements QueryStatement
+public abstract class QueryStatementBase extends ParameterizerBase implements QueryStatement
 {
     private final Connection connection;
     private long timeout1;

Modified: jena/Experimental/jena-client/src/main/java/org/apache/jena/client/UpdateStatementBase.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-client/src/main/java/org/apache/jena/client/UpdateStatementBase.java?rev=1340329&r1=1340328&r2=1340329&view=diff
==============================================================================
--- jena/Experimental/jena-client/src/main/java/org/apache/jena/client/UpdateStatementBase.java
(original)
+++ jena/Experimental/jena-client/src/main/java/org/apache/jena/client/UpdateStatementBase.java
Sat May 19 01:14:40 2012
@@ -2,7 +2,7 @@ package org.apache.jena.client;
 
 import java.util.concurrent.TimeUnit;
 
-public abstract class UpdateStatementBase extends QueryParameterizerBase implements UpdateStatement
+public abstract class UpdateStatementBase extends ParameterizerBase implements UpdateStatement
 {
     protected final Connection connection;
     private long timeout1;

Modified: jena/Experimental/jena-client/src/main/java/org/apache/jena/client/http/SparqlHttpUpdater.java
URL: http://svn.apache.org/viewvc/jena/Experimental/jena-client/src/main/java/org/apache/jena/client/http/SparqlHttpUpdater.java?rev=1340329&r1=1340328&r2=1340329&view=diff
==============================================================================
--- jena/Experimental/jena-client/src/main/java/org/apache/jena/client/http/SparqlHttpUpdater.java
(original)
+++ jena/Experimental/jena-client/src/main/java/org/apache/jena/client/http/SparqlHttpUpdater.java
Sat May 19 01:14:40 2012
@@ -10,6 +10,7 @@ import java.util.Iterator;
 
 import org.apache.jena.client.UpdaterBase;
 import org.openjena.atlas.io.IndentedWriter;
+import org.openjena.riot.WebContent;
 
 import com.hp.hpl.jena.sparql.core.Quad;
 import com.hp.hpl.jena.sparql.modify.request.UpdateWriter;
@@ -51,13 +52,21 @@ public class SparqlHttpUpdater extends U
             }
             conn = (HttpURLConnection)urlConn;
             conn.setDoOutput(true);
-            
-            // TODO Need to enable chunking mode to support streaming.  Otherwise it is all
buffered up in memory.
-            //conn.setChunkedStreamingMode(0);
+            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);
+            conn.connect();
             
             out = conn.getOutputStream();
+            //out = new GZIPOutputStream(conn.getOutputStream());
+            
+            IndentedWriter iw = new IndentedWriter(out);
+            //iw.print("update=");
             
-            writer = new UpdateWriter(new IndentedWriter(out), sCxt);
+            writer = new UpdateWriter(iw, sCxt);
             writer.open();
             
             opened = true;
@@ -114,6 +123,7 @@ public class SparqlHttpUpdater extends U
                 // Get the response
                 int responseCode = conn.getResponseCode();
                 String responseMessage = conn.getResponseMessage();
+                conn.disconnect();
                 
                 if (responseCode == 204) // No content
                 {



Mime
View raw message