+ content negociation
authorAlexandre Bertails <bertails@gmail.com>
Sun, 28 Aug 2011 18:46:03 -0400
changeset 17 a0e1740a7ea8
parent 16 72f48cef617d
child 18 d43e2e3bbbfe
+ content negociation
src/main/scala/Main.scala
src/main/scala/util.scala
src/test/scala/Test.scala
src/test/scala/utiltest.scala
--- a/src/main/scala/Main.scala	Sun Aug 28 15:27:06 2011 -0400
+++ b/src/main/scala/Main.scala	Sun Aug 28 18:46:03 2011 -0400
@@ -64,7 +64,8 @@
       req match {
         case GET(_) => {
           val model:Model = loadModel(fileOnDisk)
-          Ok ~> ViaSPARQL ~> ResponseModel(model, baseURI)
+          val encoding = RDFEncoding(req)
+          Ok ~> ViaSPARQL ~> ResponseModel(model, baseURI, encoding)
         }
         case PUT(_) => {
           val bodyModel = modelFromInputStream(Body.stream(req), baseURI)
@@ -94,6 +95,7 @@
               Ok
             }
             case PostQuery(query) => {
+              lazy val encoding = RDFEncoding(req)
               import Query.{QueryTypeSelect => SELECT,
             		        QueryTypeAsk => ASK,
                             QueryTypeConstruct => CONSTRUCT,
@@ -111,11 +113,11 @@
                 }
                 case CONSTRUCT => {
                   val result:Model = qe.execConstruct()
-                  Ok ~> ResponseModel(model, baseURI)
+                  Ok ~> ResponseModel(model, baseURI, encoding)
                 }
                 case DESCRIBE => {
                   val result:Model = qe.execDescribe()
-                  Ok ~> ResponseModel(model, baseURI)
+                  Ok ~> ResponseModel(model, baseURI, encoding)
                 }
               }
             }
--- a/src/main/scala/util.scala	Sun Aug 28 15:27:06 2011 -0400
+++ b/src/main/scala/util.scala	Sun Aug 28 18:46:03 2011 -0400
@@ -19,6 +19,26 @@
 import unfiltered.response._
 import unfiltered.jetty._
 
+sealed trait RDFEncoding
+case object RDFXML extends RDFEncoding
+case object TURTLE extends RDFEncoding
+
+object RDFEncoding {
+  
+  def apply(contentType:String):RDFEncoding =
+    contentType match {
+      case "text/turtle" => TURTLE
+      case "application/rdf+xml" => RDFXML
+      case _ => RDFXML
+    }
+    
+  def apply(req:HttpRequest[_]):RDFEncoding = {
+    val contentType = RequestContentType(req)
+    contentType map { RDFEncoding(_) } getOrElse RDFXML
+  }
+  
+}
+
 package object util {
   
   val defaultLang = "RDF/XML-ABBREV"
@@ -27,9 +47,13 @@
   object ViaSPARQL extends MSAuthorVia("SPARQL")
   
   object ResponseModel {
-    def apply(model:Model, base:String, lang:String = defaultLang):ResponseStreamer =
+    def apply(model:Model, base:String, encoding:RDFEncoding):ResponseStreamer =
       new ResponseStreamer {
-        def stream(os:OutputStream):Unit = model.write(os, lang, base)
+        def stream(os:OutputStream):Unit =
+          encoding match {
+            case RDFXML => model.getWriter("RDF/XML-ABBREV").write(model, os, base)
+            case TURTLE => model.getWriter("TURTLE").write(model, os, base)
+          }
       }
   }
 
--- a/src/test/scala/Test.scala	Sun Aug 28 15:27:06 2011 -0400
+++ b/src/test/scala/Test.scala	Sun Aug 28 18:46:03 2011 -0400
@@ -86,6 +86,14 @@
       model.size must_== (initialModel.size + 1)
     }
   }
+
+  "a GET on Joe's URI" should {
+    "deliver TURTLE and RDF/XML graphs that are isomorphic to each other" in {
+      val rdfxml = Http(joe as_model(baseURI))
+      val turtle = Http(joe <:< Map("Content-type" -> "text/turtle") as_model(baseURI, lang="TURTLE"))
+      rdfxml must beIsomorphicWith(turtle)
+    }
+  }
   
   val diffRDF =
 """
--- a/src/test/scala/utiltest.scala	Sun Aug 28 15:27:06 2011 -0400
+++ b/src/test/scala/utiltest.scala	Sun Aug 28 18:46:03 2011 -0400
@@ -38,7 +38,7 @@
   class RequestW(req:Request) {
 
     def as_model(base:String, lang:String = "RDF/XML-ABBREV"):Handler[Model] =
-      req >> { is => modelFromInputStream(is, base) }
+      req >> { is => modelFromInputStream(is, base, lang) }
 
     def post(body:String):Request =
       (req <<< body).copy(method="POST")