merging with Alex Bertails changeset "vocabulary change: use Lang and Content-Type more consistently" and adapted some of my code too webid
authorHenry Story <henry.story@bblfish.net>
Sun, 16 Oct 2011 19:54:10 +0200
branchwebid
changeset 76 5a275bdf7a44
parent 75 f46e9a29b75e (current diff)
parent 61 938b40aaea88 (diff)
child 77 03ea40f31194
merging with Alex Bertails changeset "vocabulary change: use Lang and Content-Type more consistently" and adapted some of my code too
src/main/scala/WebCache.scala
src/main/scala/plan.scala
src/main/scala/rdfLanguage.scala
src/main/scala/util/package.scala
--- a/src/main/scala/WebCache.scala	Sun Oct 16 18:24:44 2011 +0200
+++ b/src/main/scala/WebCache.scala	Sun Oct 16 19:54:10 2011 +0200
@@ -26,9 +26,7 @@
 import com.hp.hpl.jena.rdf.model.Model
 import java.net.URL
 import org.apache.http.MethodNotSupportedException
-import org.w3.readwriteweb.RDFEncoding._
 import org.w3.readwriteweb.util._
-import org.w3.readwriteweb.{RDFEncoding, RDFXML}
 import scalaz._
 import Scalaz._
 
@@ -61,7 +59,7 @@
       val handler: Handler[Validation[Throwable, Model]] = request.>+>[Validation[Throwable, Model]](res =>  {
         res >:> { headers =>
           val encoding = headers("Content-Type").headOption match {
-            case Some(mime) => RDFEncoding(mime)
+            case Some(mime) => Lang.apply(mime)
             case None => RDFXML  // it would be better to try to do a bit of guessing in this case by looking at content
           }
           val loc = headers("Content-Location").headOption match {
--- a/src/main/scala/plan.scala	Sun Oct 16 18:24:44 2011 +0200
+++ b/src/main/scala/plan.scala	Sun Oct 16 19:54:10 2011 +0200
@@ -66,11 +66,11 @@
         case GET(_) | HEAD(_) =>
           for {
             model <- r.get() failMap { x => NotFound }
-            encoding = RDFEncoding(req)
+            lang = Lang.fromRequest(req)
           } yield {
             req match {
-              case GET(_) => Ok ~> ViaSPARQL ~> ContentType(encoding.toContentType) ~> ResponseModel(model, baseURI, encoding)
-              case HEAD(_) => Ok ~> ViaSPARQL ~> ContentType(encoding.toContentType)
+              case GET(_) => Ok ~> ViaSPARQL ~> ContentType(lang.contentType) ~> ResponseModel(model, baseURI, lang)
+              case HEAD(_) => Ok ~> ViaSPARQL ~> ContentType(lang.contentType)
             }
           }
         case PUT(_) =>
@@ -104,7 +104,7 @@
             }
             case PostQuery(query) => {
               logger.info("SPARQL Query:\n" + query.toString())
-              lazy val encoding = RDFEncoding(req)
+              lazy val lang = Lang.fromRequest(req)
               for {
                 model <- r.get() failMap { t => NotFound }
               } yield {
@@ -116,11 +116,11 @@
                     Ok ~> ContentType("application/sparql-results+xml") ~> ResponseResultSet(qe.execAsk())
                   case CONSTRUCT => {
                     val result: Model = qe.execConstruct()
-                    Ok ~> ContentType(encoding.toContentType) ~> ResponseModel(model, baseURI, encoding)
+                    Ok ~> ContentType(lang.contentType) ~> ResponseModel(model, baseURI, lang)
                   }
                   case DESCRIBE => {
                     val result: Model = qe.execDescribe()
-                    Ok ~> ContentType(encoding.toContentType) ~> ResponseModel(model, baseURI, encoding)
+                    Ok ~> ContentType(lang.contentType) ~> ResponseModel(model, baseURI, lang)
                   }
                 }
               }
--- a/src/main/scala/rdfLanguage.scala	Sun Oct 16 18:24:44 2011 +0200
+++ b/src/main/scala/rdfLanguage.scala	Sun Oct 16 19:54:10 2011 +0200
@@ -2,36 +2,41 @@
 
 import unfiltered.request._
 
-sealed trait RDFEncoding {
-  def toContentType:String
-}
-
-case object RDFXML extends RDFEncoding {
-  def toContentType = "application/rdf+xml"
-}
-
-case object TURTLE extends RDFEncoding {
-  def toContentType = "text/turtle"
-}
-
-object RDFEncoding {
+sealed trait Lang {
   
-  def apply(contentType:String):RDFEncoding =
-    contentType match {
-      case "text/turtle" => TURTLE
-      case "application/rdf+xml" => RDFXML
-      case _ => RDFXML
-    }
-
-  def jena(encoding: RDFEncoding) = encoding match {
-     case RDFXML => "RDF/XML-ABBREV"
-     case TURTLE => "TURTLE"
-     case _      => "RDF/XML-ABBREV" //don't like this default
-   }
-
-  def apply(req:HttpRequest[_]):RDFEncoding = {
-    val contentType = Accept(req).headOption
-    contentType map { RDFEncoding(_) } getOrElse RDFXML
+  def contentType = this match {
+    case RDFXML => "application/rdf+xml"
+    case TURTLE => "text/turtle"
+    case N3 => "text/n3"
+  }
+  
+  def jenaLang = this match {
+    case RDFXML => "RDF/XML-ABBREV"
+    case TURTLE => "TURTLE"
+    case N3 => "N3"
   }
   
 }
+
+object Lang {
+  
+  val default = RDFXML
+  
+  def apply: PartialFunction[String, Lang] = {
+    case "text/n3" => N3
+    case "text/turtle" => TURTLE
+    case "application/rdf+xml" => RDFXML
+  }
+
+  def fromRequest(req: HttpRequest[_]): Lang = {
+    val contentType = Accept(req).headOption
+    contentType map { Lang.apply } getOrElse RDFXML
+  }
+
+}
+
+case object RDFXML extends Lang
+
+case object TURTLE extends Lang
+
+case object N3 extends Lang
--- a/src/main/scala/util/package.scala	Sun Oct 16 18:24:44 2011 +0200
+++ b/src/main/scala/util/package.scala	Sun Oct 16 19:54:10 2011 +0200
@@ -16,13 +16,10 @@
   object ViaSPARQL extends MSAuthorVia("SPARQL")
   
   object ResponseModel {
-    def apply(model: Model, base: String, encoding: RDFEncoding): ResponseStreamer =
+    def apply(model: Model, base: String, lang: Lang): ResponseStreamer =
       new ResponseStreamer {
         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)
-          }
+          model.getWriter(lang.jenaLang).write(model, os, base)
       }
   }
 
@@ -37,10 +34,10 @@
       }
   }
 
-  //Passing strings into mathod arguments, especially as these differ completely between rdf stacks is not so good
+  //Passing strings into method arguments, especially as these differ completely between rdf stacks is not so good
   //passing objects is better
-  def modelFromInputStream(is:InputStream, base: String,  lang: RDFEncoding): Validation[Throwable, Model]=
-      modelFromInputStream(is, base, RDFEncoding.jena(lang))
+  def modelFromInputStream(is:InputStream, base: String,  lang: Lang): Validation[Throwable, Model]=
+      modelFromInputStream(is, base, lang.jenaLang)
 
   def modelFromInputStream(
       is: InputStream,