~ do something, not want I want
authorAlexandre Bertails <bertails@gmail.com>
Thu, 25 Aug 2011 23:10:58 -0400
changeset 1 0ee905706f84
parent 0 686e9da37c4b
child 2 2f238399a8a2
~ do something, not want I want
src/main/scala/Main.scala
src/test/scala/Test.scala
--- a/src/main/scala/Main.scala	Thu Aug 25 22:33:54 2011 -0400
+++ b/src/main/scala/Main.scala	Thu Aug 25 23:10:58 2011 -0400
@@ -1,7 +1,5 @@
 package org.w3.readwriteweb
 
-import org.w3.yourapp.util.ResourceManager.fromClasspath
-
 import javax.servlet._
 import javax.servlet.http._
 import unfiltered.request._
@@ -13,7 +11,9 @@
 import scala.io.Source
 
 import org.slf4j.{Logger, LoggerFactory}
+
 import com.hp.hpl.jena.rdf.model._
+import com.hp.hpl.jena.update._
 
 // holds some Unfiltered plans
 class ReadWriteWeb(base:File) {
@@ -29,7 +29,7 @@
       val fis = new FileInputStream(new File(base, path))
       /* http://jena.sourceforge.net/tutorial/RDF_API/index.html#ch-Reading%20RDF */
       val model:Model = ModelFactory.createDefaultModel()
-      model.read(fis, null)
+      model.read(fis, "http://www.w3.org/People/Berners-Lee/")
       req match {
         case GET(_) => {
           Ok ~> new ResponseStreamer {
@@ -41,9 +41,20 @@
             }
           }
         }
-        // case POST(_) => {
-        //   val query = 
-        // }
+        case POST(_) => {
+          val bodyStream = Body.stream(req)
+          /* http://openjena.org/ARQ/javadoc/com/hp/hpl/jena/update/UpdateFactory.html */
+          val update:UpdateRequest = UpdateFactory.read(bodyStream)
+          val graphStore:GraphStore = GraphStoreFactory.create(model)
+          val processor:UpdateProcessor = UpdateExecutionFactory.create(update, graphStore)
+          val result:Model = processor.getGraphStore.toDataset.getDefaultModel
+          Ok ~> new ResponseStreamer {
+            def stream(os:OutputStream):Unit = {
+              val lang = "TURTLE"
+              result.write(os, lang)
+            }
+          }
+        }
       }
     }
   }
--- a/src/test/scala/Test.scala	Thu Aug 25 22:33:54 2011 -0400
+++ b/src/test/scala/Test.scala	Thu Aug 25 23:10:58 2011 -0400
@@ -9,15 +9,40 @@
 
 object ReadWriteWebSpec extends Specification with unfiltered.spec.jetty.Served {
 
+  def post(req:Request, body:String) = (req <<< body).copy(method="POST")
+
   def setup = { _.filter(new ReadWriteWeb(new File("src/main/resources")).read) }
 
-  val get:Request = host / "/People/Berners-Lee/card#i"
+  val timBL = host / "/People/Berners-Lee/card#i"
     
   "GET on TimBL's FOAF profile" should {
     "return something" in {
-      val body:String = Http(get as_str)
+      val body:String = Http(timBL as_str)
       body must not be empty
     }
   }
+
+  val update = host / "/People/Berners-Lee/card#i"
+    
+  val sparqlAdd =
+"""
+PREFIX dc: <http://purl.org/dc/elements/1.1/>
+PREFIX foaf: <http://xmlns.com/foaf/0.1/>
+INSERT DATA
+{ 
+  <http://example/book1> dc:title "A new book" ;
+                         dc:creator "A.N.Other" .
+  <http://www.w3.org/data#W3C>
+      foaf:member <http://www.w3.org/People/Alexandre/> .
+}
+"""
+
+  "SPARQL UPDATE on TimBL's FOAF profile" should {
+    "return something new" in {
+      val body:String = Http(post(timBL, sparqlAdd) as_str)
+      println(body)
+      body must be matching ".*Alexandre.*"
+    }
+  }
   
 }