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 2012/04/12 02:40:58 UTC

svn commit: r1325081 - in /incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http: ./ QuadEntity.java TripleEntity.java UpdateRemote.java UpdateStreamer.java

Author: sallen
Date: Thu Apr 12 00:40:58 2012
New Revision: 1325081

URL: http://svn.apache.org/viewvc?rev=1325081&view=rev
Log:
Added some classes for update support (not finished).

Added:
    incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/
    incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/QuadEntity.java
    incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/TripleEntity.java
    incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateRemote.java
    incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateStreamer.java

Added: incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/QuadEntity.java
URL: http://svn.apache.org/viewvc/incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/QuadEntity.java?rev=1325081&view=auto
==============================================================================
--- incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/QuadEntity.java (added)
+++ incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/QuadEntity.java Thu Apr 12 00:40:58 2012
@@ -0,0 +1,61 @@
+package org.apache.jena.client.http;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Iterator;
+
+import org.apache.http.entity.AbstractHttpEntity;
+import org.openjena.atlas.lib.NotImplemented;
+
+import com.hp.hpl.jena.sparql.core.Quad;
+
+public class QuadEntity extends AbstractHttpEntity
+{
+    private final Iterator<Quad> it;
+    
+    public QuadEntity(Iterator<Quad> it)
+    {
+        this.it = it;
+    }
+
+    @Override
+    public boolean isRepeatable()
+    {
+        return false;
+    }
+
+    @Override
+    public long getContentLength()
+    {
+        return -1;
+    }
+    
+    @Override
+    public boolean isStreaming()
+    {
+        return true;
+    }
+
+    @Override
+    public InputStream getContent() throws IOException, IllegalStateException
+    {
+        throw new NotImplemented();
+    }
+
+    @Override
+    public void writeTo(OutputStream outstream) throws IOException
+    {
+        if (outstream == null)
+        {
+            throw new IllegalArgumentException("Output stream may not be null");
+        }
+        
+        throw new NotImplemented();
+        
+//        outstream.write("INSERT DATA {\n".getBytes("UTF-8"));
+//        RiotWriter.writeTrig(outstream, it);
+//        outstream.write("\n}".getBytes("UTF-8"));
+    }
+
+}

Added: incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/TripleEntity.java
URL: http://svn.apache.org/viewvc/incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/TripleEntity.java?rev=1325081&view=auto
==============================================================================
--- incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/TripleEntity.java (added)
+++ incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/TripleEntity.java Thu Apr 12 00:40:58 2012
@@ -0,0 +1,85 @@
+package org.apache.jena.client.http;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Iterator;
+
+import org.apache.http.entity.AbstractHttpEntity;
+import org.openjena.atlas.lib.NotImplemented;
+import org.openjena.riot.RiotWriter;
+
+import com.hp.hpl.jena.graph.Node;
+import com.hp.hpl.jena.graph.Triple;
+import com.hp.hpl.jena.sparql.core.Quad;
+
+public class TripleEntity extends AbstractHttpEntity
+{
+    private final Node graphName;
+    private final boolean insert;
+    private final Iterator<Triple> it;
+    
+    public TripleEntity(Node graphName, boolean insert, Iterator<Triple> it)
+    {
+        if (Quad.isDefaultGraph(graphName))
+        {
+            graphName = null;
+        }
+        this.graphName = graphName;
+        this.insert = insert;
+        this.it = it;
+    }
+
+    @Override
+    public boolean isRepeatable()
+    {
+        return false;
+    }
+
+    @Override
+    public long getContentLength()
+    {
+        return -1;
+    }
+    
+    @Override
+    public boolean isStreaming()
+    {
+        return true;
+    }
+
+    @Override
+    public InputStream getContent() throws IOException, IllegalStateException
+    {
+        throw new NotImplemented();
+    }
+
+    @Override
+    public void writeTo(OutputStream outstream) throws IOException
+    {
+        if (outstream == null)
+        {
+            throw new IllegalArgumentException("Output stream may not be null");
+        }
+        
+        String instruction = insert ? "INSERT" : "DELETE";
+        write(outstream, String.format("%1$s DATA {\n", instruction));
+        if (null != graphName)
+        {
+            write(outstream, String.format("graph <%1$s> {\n", graphName.getURI()));
+        }
+        
+        RiotWriter.writeTriples(outstream, it);
+        
+        if (null != graphName)
+        {
+            write(outstream, "\n}");
+        }
+        write(outstream, "\n}");
+    }
+
+    private void write(OutputStream out, String s) throws IOException
+    {
+        out.write(s.getBytes("UTF-8"));
+    }
+}

Added: incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateRemote.java
URL: http://svn.apache.org/viewvc/incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateRemote.java?rev=1325081&view=auto
==============================================================================
--- incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateRemote.java (added)
+++ incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateRemote.java Thu Apr 12 00:40:58 2012
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jena.client.http;
+
+import java.io.ByteArrayOutputStream ;
+import java.io.IOException ;
+import java.util.Iterator;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse ;
+import org.apache.http.client.HttpClient ;
+import org.apache.http.client.methods.HttpPost ;
+import org.apache.http.entity.AbstractHttpEntity ;
+import org.apache.http.entity.ByteArrayEntity ;
+import org.apache.http.entity.InputStreamEntity;
+import org.apache.http.impl.client.DefaultHttpClient ;
+import org.apache.http.protocol.HTTP ;
+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.Target ;
+import com.hp.hpl.jena.sparql.modify.request.UpdateDrop ;
+import com.hp.hpl.jena.sparql.modify.request.UpdateWriter ;
+import com.hp.hpl.jena.update.Update ;
+import com.hp.hpl.jena.update.UpdateException ;
+import com.hp.hpl.jena.update.UpdateRequest ;
+
+public class UpdateRemote
+{
+    private final String serviceURL;
+    
+    public UpdateRemote(String serviceURL)
+    {
+        this.serviceURL = serviceURL;
+    }
+    
+    /** Reset the remote repository by execution a "DROP ALL" command on it */
+    public void executeClear()
+    {
+        Update clear = new UpdateDrop(Target.ALL) ;
+        execute(clear) ;
+    }
+    
+    public void execute(Update request)
+    {
+        execute(new UpdateRequest(request)) ;
+    }
+    
+    public void execute(UpdateRequest request)
+    {
+        ByteArrayOutputStream b_out = new ByteArrayOutputStream() ;
+        IndentedWriter out = new IndentedWriter(b_out) ; 
+        UpdateWriter.output(request, out) ;
+        out.flush() ;
+        byte[] bytes = b_out.toByteArray() ;
+        AbstractHttpEntity reqEntity = new ByteArrayEntity(bytes) ;
+        reqEntity.setContentType(WebContent.contentTypeSPARQLUpdate) ;
+        reqEntity.setContentEncoding(HTTP.UTF_8) ;
+        execute(reqEntity);            
+    }
+    
+    
+    public void add(Iterator<Quad> quads)
+    {
+        
+    }
+    
+    
+    public void delete(Iterator<Quad> quads)
+    {
+        
+    }
+    
+    
+    protected void execute(AbstractHttpEntity reqEntity)
+    {
+        HttpPost httpPost = new HttpPost(serviceURL) ;
+        reqEntity.setContentType(WebContent.contentTypeSPARQLUpdate) ;
+        reqEntity.setContentEncoding(HTTP.UTF_8) ;
+        httpPost.setEntity(reqEntity) ;
+        
+        HttpClient httpclient = new DefaultHttpClient() ;
+        
+        try
+        {
+            HttpResponse response = httpclient.execute(httpPost) ;
+            int responseCode = response.getStatusLine().getStatusCode() ;
+            String responseMessage = response.getStatusLine().getReasonPhrase() ;
+            
+            if ( responseCode == 204 ) // No content
+                return ;
+            if ( responseCode == 200 ) // OK
+                // But what was the content?
+                // TODO read body 
+                return ; 
+            throw new UpdateException(responseCode+" "+responseMessage) ;
+        } catch (IOException ex)
+        {
+            throw new UpdateException(ex) ;
+        }
+    }
+    
+}

Added: incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateStreamer.java
URL: http://svn.apache.org/viewvc/incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateStreamer.java?rev=1325081&view=auto
==============================================================================
--- incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateStreamer.java (added)
+++ incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateStreamer.java Thu Apr 12 00:40:58 2012
@@ -0,0 +1,81 @@
+package org.apache.jena.client.http;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpVersion;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.message.BasicHttpRequest;
+import org.openjena.atlas.lib.Closeable;
+import org.openjena.atlas.lib.Sink;
+
+import com.hp.hpl.jena.sparql.ARQException;
+import com.hp.hpl.jena.sparql.core.Quad;
+import com.hp.hpl.jena.update.UpdateException;
+
+
+public class UpdateStreamer implements Sink<Quad>, Closeable
+{
+    
+    private OutputStream out;
+    
+    public void open(String serviceUri)
+    {
+        HttpRequest request = new BasicHttpRequest("POST", serviceUri, HttpVersion.HTTP_1_1); //new HttpPost(serviceUri);
+        //HttpRequest request = new HttpPost(serviceUri);
+        
+        
+        
+        HttpEntity foo;
+    }
+
+    @Override
+    public void send(Quad item)
+    {
+        // TODO
+        byte[] b = null;
+        try
+        {
+            out.write(b);
+        }
+        catch (IOException e)
+        {
+            throw new UpdateException("IOException while writing update", e);
+        }
+    }
+
+    @Override
+    public void flush()
+    {
+        try
+        {
+            out.flush();
+        }
+        catch (IOException e)
+        {
+            throw new UpdateException("IOException while flushing update stream", e);
+        }
+    }
+
+    
+    @Override
+    public void close()
+    {
+        if (null != out)
+        {
+            try
+            {
+                out.close();
+            }
+            catch (IOException e)
+            {
+                throw new UpdateException("IOException while closing update stream", e);
+            }
+        }
+        
+        // TODO get HTTP response
+        
+    }
+}