+ POST RDF chunk
authorAlexandre Bertails <bertails@gmail.com>
Sat, 27 Aug 2011 20:10:20 -0400
changeset 10 6874dd1a9497
parent 9 9a148d184ac2
child 11 e323200c6ecc
+ POST RDF chunk
src/main/scala/Main.scala
src/test/scala/Test.scala
--- a/src/main/scala/Main.scala	Sat Aug 27 19:24:52 2011 -0400
+++ b/src/main/scala/Main.scala	Sat Aug 27 20:10:20 2011 -0400
@@ -77,18 +77,23 @@
         }
         case POST(_) => {
           /* http://openjena.org/ARQ/javadoc/com/hp/hpl/jena/update/UpdateFactory.html */
-          val update:UpdateRequest = UpdateFactory.read(Body.stream(req))
-          val (fos, model) = foo()
-          UpdateAction.execute(update, model)
-          model.write(fos)
-          fos.close()
-          // give back the modified model
-          Ok ~> new ResponseStreamer {
-            def stream(os:OutputStream):Unit = {
-              val lang = "RDF/XML-ABBREV"
-              model.write(os, lang)
+          Post.parse(Body.stream(req), baseURI) match {
+            case PostUpdate(update) => {
+              val (fos, model) = foo()
+              UpdateAction.execute(update, model)
+              model.write(fos)
+              fos.close()
+              Ok
+            }
+            case PostRDF(diffModel) => {
+              val (fos, model) = foo()
+              model.add(diffModel)
+              model.write(fos)
+              fos.close()
+              Ok
             }
           }
+          
         }
       }
     }
@@ -96,6 +101,33 @@
 
 }
 
+sealed trait Post
+case class PostUpdate(update:UpdateRequest) extends Post
+case class PostRDF(model:Model) extends Post
+
+object Post {
+  
+  def parse(is:InputStream, baseURI:String):Post = {
+    val source = Source.fromInputStream(is, "UTF-8")
+    val s = source.getLines.mkString("\n")
+    parse(s, baseURI)
+  }
+  
+  def parse(s:String, baseURI:String):Post = {
+    val reader = new StringReader(s)
+    try {
+      val update:UpdateRequest = UpdateFactory.create(s, baseURI)
+      PostUpdate(update)      
+    } catch {
+      case qpe:QueryParseException => {
+        val model = modelFromString(s, baseURI)
+        PostRDF(model)
+      }
+    }
+  }
+  
+}
+
 object ReadWriteWebMain {
 
   val logger:Logger = LoggerFactory.getLogger(this.getClass)
--- a/src/test/scala/Test.scala	Sat Aug 27 19:24:52 2011 -0400
+++ b/src/test/scala/Test.scala	Sat Aug 27 20:10:20 2011 -0400
@@ -18,6 +18,7 @@
 
   val base = new File("src/main/resources")
   val joe = host / "2007/wiki/people/JoeLambda"
+  val baseURI = "%s%s" format (joe.host, joe.path)
   val joeOnDisk = new File(base, "2007/wiki/people/JoeLambda")
   
   doBeforeSpec {
@@ -40,7 +41,7 @@
 </rdf:RDF>
 """
   
-  val initialModel = modelFromString(joeRDF, joe.path)
+  val initialModel = modelFromString(joeRDF, baseURI)
 
 //        <foaf:openid rdf:resource="/2007/wiki/people/JoeLambda" />
 //    <foaf:img rdf:resource="/2007/wiki/people/JoeLambda/images/me.jpg" />
@@ -59,7 +60,7 @@
     "now exist and be isomorphic with the original document" in {
       val (statusCode, via, model) = Http(joe >++ { req => (req.get_statusCode,
                                                             req.get_header("MS-Author-Via"),
-                                                            req as_model(joe.path))
+                                                            req as_model(baseURI))
                                                   } )
       statusCode must_== 200
       via must_== "SPARQL"
@@ -79,10 +80,41 @@
       httpCode must_== 200
     }
     "produce a graph with one more triple than the original one" in {
-      val model = Http(joe as_model(joe.path))
+      val model = Http(joe as_model(baseURI))
       model.size must_== (initialModel.size + 1)
     }
   }
   
+  val diffRDF =
+"""
+<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> 
+  <foaf:Person rdf:about="#JL" xmlns:foaf="http://xmlns.com/foaf/0.1/">
+    <foaf:img rdf:resource="/2007/wiki/people/JoeLambda/images/me.jpg" />
+  </foaf:Person>
+</rdf:RDF>
+"""
+
+  val expectedFinalModel = modelFromString(
+"""
+<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> 
+  <foaf:Person rdf:about="#JL" xmlns:foaf="http://xmlns.com/foaf/0.1/">
+    <foaf:name>Joe Lambda</foaf:name>
+    <foaf:homepage rdf:resource="/2007/wiki/people/JoeLambda" />
+    <foaf:openid rdf:resource="/2007/wiki/people/JoeLambda" />
+    <foaf:img rdf:resource="/2007/wiki/people/JoeLambda/images/me.jpg" />
+  </foaf:Person>
+</rdf:RDF>
+""", baseURI)
+
+  "POSTing an RDF documenton Joe's URI" should {
+    "succeed" in {
+      val httpCode:Int = Http(joe.post(diffRDF) get_statusCode)
+      httpCode must_== 200
+    }
+    "append the diff graph" in {
+      val model = Http(joe as_model(baseURI))
+      model must beIsomorphicWith (expectedFinalModel)
+    }
+  }
     
 }