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/16 08:37:59 UTC
svn commit: r1326506 - in /incubator/jena/Experimental/jena-client/trunk: ./
src/main/java/org/apache/jena/client/http/
Author: sallen
Date: Mon Apr 16 06:37:58 2012
New Revision: 1326506
URL: http://svn.apache.org/viewvc?rev=1326506&view=rev
Log:
jena-client: Extracted update DATA streaming to its own object.
Added:
incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateMode.java
Modified:
incubator/jena/Experimental/jena-client/trunk/ (props changed)
incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/QuadIteratorEntity.java
incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/TripleIteratorEntity.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
Propchange: incubator/jena/Experimental/jena-client/trunk/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Mon Apr 16 06:37:58 2012
@@ -0,0 +1,4 @@
+.classpath
+.project
+.settings
+target
Modified: incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/QuadIteratorEntity.java
URL: http://svn.apache.org/viewvc/incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/QuadIteratorEntity.java?rev=1326506&r1=1326505&r2=1326506&view=diff
==============================================================================
--- incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/QuadIteratorEntity.java (original)
+++ incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/QuadIteratorEntity.java Mon Apr 16 06:37:58 2012
@@ -1,30 +1,24 @@
package org.apache.jena.client.http;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.Writer;
-import java.util.Iterator;
-
-import org.apache.http.entity.AbstractHttpEntity;
-import org.openjena.atlas.io.IO;
-import org.openjena.atlas.iterator.PeekIterator;
-import org.openjena.atlas.lib.NotImplemented;
-import org.openjena.riot.out.NodeFormatter;
-import org.openjena.riot.out.NodeFormatterNT;
+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.iterator.Iter ;
+import org.openjena.atlas.lib.NotImplemented ;
-import com.hp.hpl.jena.graph.Node;
-import com.hp.hpl.jena.sparql.core.Quad;
+import com.hp.hpl.jena.sparql.core.Quad ;
class QuadIteratorEntity extends AbstractHttpEntity
{
- private final boolean insert;
+ private final UpdateMode mode;
private final Iterator<Quad> it;
- private final NodeFormatter nodeFmt = new NodeFormatterNT() ;
- public QuadIteratorEntity(boolean insert, Iterator<Quad> it)
+ public QuadIteratorEntity(UpdateMode mode, Iterator<Quad> it)
{
- this.insert = insert;
+ this.mode = mode;
this.it = it;
}
@@ -49,7 +43,7 @@ class QuadIteratorEntity extends Abstrac
@Override
public InputStream getContent() throws IOException, IllegalStateException
{
- throw new NotImplemented("TripleIteratorEntity is only meant to be used as a Request entity.");
+ throw new NotImplemented("QuadIteratorEntity is only meant to be used as a Request entity.");
}
@Override
@@ -62,81 +56,8 @@ class QuadIteratorEntity extends Abstrac
if (!it.hasNext()) return;
- // TODO Move this to UpdateWriter
- // TODO Would be cool to use a Trig writer here
- Writer out = IO.asBufferedUTF8(outstream) ;
- try
- {
- String instruction = insert ? "INSERT" : "DELETE";
- out.write(String.format("%1$s DATA {\n", instruction));
-
- PeekIterator<Quad> pit = new PeekIterator<Quad>(it);
- while (pit.hasNext())
- {
- Quad quad = pit.next();
- Node graphName = quad.isDefaultGraph() ? null : quad.getGraph();
-
- if (null != graphName)
- {
- out.write(String.format(" graph <%1$s> {\n", graphName.getURI()));
- }
-
- // This is a little optimization to not repeat the graph block if consecutive quads have the same graph
- while (true)
- {
- out.write(null != graphName ? " " : " ");
- write(out, quad);
-
- if (!pit.hasNext()) break;
-
- Quad nextQuad = pit.peek();
- Node nextGraphName = nextQuad.isDefaultGraph() ? null : nextQuad.getGraph();
-
- if (!nodesAreEqual(graphName, nextGraphName)) break;
-
- quad = pit.next();
-
- }
-
- if (null != graphName)
- {
- out.write("\n }");
- }
- }
-
- out.write("\n}");
- }
- finally
- {
- if (null != out)
- {
- out.flush();
- }
- }
-
+ UpdateStreamer us = new UpdateStreamer(mode, outstream);
+ us.open();
+ Iter.sendToSink(it, us); // us.close() is called by sendToSink
}
-
- private boolean nodesAreEqual(Node a, Node b)
- {
- if (null == a && null == b) return true;
- if (null != a && null == b) return false;
- return a.equals(b);
- }
-
- // really just writing the triple part of the quad
- private void write(Writer out, Quad quad) throws IOException
- {
- Node s = quad.getSubject() ;
- Node p = quad.getPredicate() ;
- Node o = quad.getObject() ;
-
- nodeFmt.format(out, s) ;
- out.write(" ") ;
- nodeFmt.format(out, p) ;
- out.write(" ") ;
- nodeFmt.format(out, o) ;
- out.write(" .\n") ;
-
- }
-
}
Modified: incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/TripleIteratorEntity.java
URL: http://svn.apache.org/viewvc/incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/TripleIteratorEntity.java?rev=1326506&r1=1326505&r2=1326506&view=diff
==============================================================================
--- incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/TripleIteratorEntity.java (original)
+++ incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/TripleIteratorEntity.java Mon Apr 16 06:37:58 2012
@@ -1,32 +1,26 @@
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;
+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.graph.Node ;
+import com.hp.hpl.jena.graph.Triple ;
class TripleIteratorEntity extends AbstractHttpEntity
{
+ private final UpdateMode mode;
private final Node graphName;
- private final boolean insert;
private final Iterator<Triple> it;
- public TripleIteratorEntity(Node graphName, boolean insert, Iterator<Triple> it)
+ public TripleIteratorEntity(UpdateMode mode, Node graphName, Iterator<Triple> it)
{
- if (Quad.isDefaultGraph(graphName))
- {
- graphName = null;
- }
+ this.mode = mode;
this.graphName = graphName;
- this.insert = insert;
this.it = it;
}
@@ -64,25 +58,12 @@ class TripleIteratorEntity extends Abstr
if (!it.hasNext()) return;
- // TODO Move this to UpdateWriter
- 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)
+ UpdateStreamer us = new UpdateStreamer(mode, outstream);
+ us.open();
+ do
{
- write(outstream, "\n}");
- }
- write(outstream, "\n}");
- }
-
- private void write(OutputStream out, String s) throws IOException
- {
- out.write(s.getBytes("UTF-8"));
+ us.add(graphName, it.next());
+ } while (it.hasNext());
+ us.close(); // Not in a finally block, because it doesn't make sense to try to finish writing if something went wrong
}
}
Added: incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateMode.java
URL: http://svn.apache.org/viewvc/incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateMode.java?rev=1326506&view=auto
==============================================================================
--- incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateMode.java (added)
+++ incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateMode.java Mon Apr 16 06:37:58 2012
@@ -0,0 +1,28 @@
+/**
+ * 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;
+
+/**
+ * The mode an update stream is in.
+ */
+public enum UpdateMode
+{
+ INSERT,
+ DELETE,
+}
Modified: 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=1326506&r1=1326505&r2=1326506&view=diff
==============================================================================
--- incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateRemote.java (original)
+++ incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateRemote.java Mon Apr 16 06:37:58 2012
@@ -18,29 +18,31 @@
package org.apache.jena.client.http;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.util.Iterator;
+import java.io.ByteArrayOutputStream ;
+import java.io.IOException ;
+import java.io.InputStream ;
+import java.util.Iterator ;
-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.impl.client.DefaultHttpClient;
-import org.apache.http.protocol.HTTP;
-import org.openjena.atlas.io.IndentedWriter;
-import org.openjena.riot.WebContent;
+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.impl.client.DefaultHttpClient ;
+import org.apache.http.protocol.HTTP ;
+import org.openjena.atlas.io.IndentedWriter ;
+import org.openjena.riot.Lang ;
+import org.openjena.riot.WebContent ;
-import com.hp.hpl.jena.graph.Node;
-import com.hp.hpl.jena.graph.Triple;
-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;
+import com.hp.hpl.jena.graph.Node ;
+import com.hp.hpl.jena.graph.Triple ;
+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
{
@@ -77,26 +79,28 @@ public class UpdateRemote
public void add(Node graphName, Iterator<Triple> triples)
{
- TripleIteratorEntity reqEntity = new TripleIteratorEntity(graphName, true, triples) ;
- execute(reqEntity) ;
+ execute(new TripleIteratorEntity(UpdateMode.INSERT, graphName, triples)) ;
}
public void add(Iterator<Quad> quads)
{
- QuadIteratorEntity reqEntity = new QuadIteratorEntity(true, quads) ;
- execute(reqEntity) ;
+ execute(new QuadIteratorEntity(UpdateMode.INSERT, quads)) ;
+ }
+
+ public void add(InputStream in, Lang lang, String baseUri)
+ {
+ // TODO fix this
+ //execute(new InputStreamEntity(in, -1));
}
public void delete(Node graphName, Iterator<Triple> triples)
{
- TripleIteratorEntity reqEntity = new TripleIteratorEntity(graphName, false, triples) ;
- execute(reqEntity) ;
+ execute(new TripleIteratorEntity(UpdateMode.DELETE, graphName, triples)) ;
}
public void delete(Iterator<Quad> quads)
{
- QuadIteratorEntity reqEntity = new QuadIteratorEntity(false, quads) ;
- execute(reqEntity) ;
+ execute(new QuadIteratorEntity(UpdateMode.DELETE, quads)) ;
}
protected void execute(AbstractHttpEntity reqEntity)
Modified: 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=1326506&r1=1326505&r2=1326506&view=diff
==============================================================================
--- incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateStreamer.java (original)
+++ incubator/jena/Experimental/jena-client/trunk/src/main/java/org/apache/jena/client/http/UpdateStreamer.java Mon Apr 16 06:37:58 2012
@@ -1,50 +1,125 @@
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;
-
+import java.io.IOException ;
+import java.io.OutputStream ;
+import java.io.Writer ;
+
+import org.openjena.atlas.io.IO ;
+import org.openjena.atlas.lib.Closeable ;
+import org.openjena.atlas.lib.Sink ;
+import org.openjena.riot.out.NodeFormatter ;
+import org.openjena.riot.out.NodeFormatterNT ;
+
+import com.hp.hpl.jena.graph.Node ;
+import com.hp.hpl.jena.graph.Triple ;
+import com.hp.hpl.jena.sparql.core.Quad ;
+import com.hp.hpl.jena.update.UpdateException ;
+// TODO maybe move this to UpdateWriter
public class UpdateStreamer implements Sink<Quad>, Closeable
{
+ private final Writer out;
+ private final UpdateMode mode;
+ private final NodeFormatter nodeFmt = new NodeFormatterNT() ;
+ private boolean closed = false;
- private OutputStream out;
+ private Node currentGraph;
- public void open(String serviceUri)
+ public UpdateStreamer(UpdateMode mode, OutputStream outstream)
{
- HttpRequest request = new BasicHttpRequest("POST", serviceUri, HttpVersion.HTTP_1_1); //new HttpPost(serviceUri);
- //HttpRequest request = new HttpPost(serviceUri);
-
-
+ if (outstream == null)
+ {
+ throw new IllegalArgumentException("Output stream may not be null") ;
+ }
- HttpEntity foo;
+ this.mode = mode;
+ this.out = IO.asBufferedUTF8(outstream) ;
}
-
- @Override
- public void send(Quad item)
+
+ public void open()
+ {
+ try
+ {
+ out.write(mode.toString());
+ out.write(" DATA {\n");
+ }
+ catch (IOException e)
+ {
+ throw new UpdateException("IOException while writing to the update OutputStream", e);
+ }
+ }
+
+ public void add(Quad quad)
+ {
+ // TODO quad.asTriple() mutates Quad, as it lazily caches the Triple object... probably not the worst
+ // thing to have happen, but we're throwing the triple object away rather quickly, so lets avoid that.
+ add(quad.getGraph(), new Triple(quad.getSubject(), quad.getPredicate(), quad.getObject()));
+ }
+
+ public void add(Node graphName, Triple triple)
{
- // TODO
- byte[] b = null;
+ if (Quad.isDefaultGraph(graphName))
+ {
+ graphName = null;
+ }
+
try
{
- out.write(b);
+ if (!nodesAreEqual(currentGraph, graphName))
+ {
+ if (null != currentGraph)
+ {
+ out.write("\n }");
+ }
+
+ if (null != graphName)
+ {
+ out.write(" graph <");
+ out.write(graphName.getURI());
+ out.write("> {\n");
+ }
+ }
+
+ // indent
+ out.write(null != graphName ? " " : " ");
+ write(triple);
+
+ currentGraph = graphName;
}
catch (IOException e)
{
- throw new UpdateException("IOException while writing update", e);
+ throw new UpdateException("IOException while writing to the update OutputStream", e);
}
}
+
+ protected static boolean nodesAreEqual(Node a, Node b)
+ {
+ if (null == a && null == b) return true;
+ if (null != a && null == b) return false;
+ return a.equals(b);
+ }
+
+ private void write(Triple triple) throws IOException
+ {
+ Node s = triple.getSubject() ;
+ Node p = triple.getPredicate() ;
+ Node o = triple.getObject() ;
+
+ nodeFmt.format(out, s) ;
+ out.write(" ") ;
+ nodeFmt.format(out, p) ;
+ out.write(" ") ;
+ nodeFmt.format(out, o) ;
+ out.write(" .\n") ;
+
+ }
+
+
+ @Override
+ public void send(Quad item)
+ {
+ add(item);
+ }
@Override
public void flush()
@@ -63,19 +138,25 @@ public class UpdateStreamer implements S
@Override
public void close()
{
- if (null != out)
+ if (!closed)
{
try
{
- out.close();
+ if (null != currentGraph)
+ {
+ out.write("\n }");
+ }
+
+ out.write("\n}");
}
catch (IOException e)
{
throw new UpdateException("IOException while closing update stream", e);
}
+
+ // Since we didn't create the OutputStream, we'll just flush it
+ flush();
+ closed = true;
}
-
- // TODO get HTTP response
-
}
}