~ vocabulary change: use Lang and Content-Type more consistently
authorAlexandre Bertails <bertails@gmail.com>
Thu, 13 Oct 2011 19:59:40 -0400
changeset 61 938b40aaea88
parent 51 e34ee305ffe3
child 65 68e9d1a12e27
child 76 5a275bdf7a44
~ vocabulary change: use Lang and Content-Type more consistently
src/main/scala/plan.scala
src/main/scala/rdfLanguage.scala
src/main/scala/util/package.scala
--- a/src/main/scala/plan.scala	Tue Oct 11 22:15:09 2011 -0400
+++ b/src/main/scala/plan.scala	Thu Oct 13 19:59:40 2011 -0400
@@ -65,11 +65,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(_) =>
@@ -103,7 +103,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 {
@@ -115,11 +115,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	Tue Oct 11 22:15:09 2011 -0400
+++ b/src/main/scala/rdfLanguage.scala	Thu Oct 13 19:59:40 2011 -0400
@@ -2,30 +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 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	Tue Oct 11 22:15:09 2011 -0400
+++ b/src/main/scala/util/package.scala	Thu Oct 13 19:59:40 2011 -0400
@@ -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)
       }
   }