~ some refactoring
authorAlexandre Bertails <bertails@gmail.com>
Sat, 15 Oct 2011 16:47:37 -0400
changeset 69 3e84e72b6e82
parent 67 353368a48cf5
child 70 1a34982ea440
child 81 fed900642d29
~ some refactoring
src/main/scala/Post.scala
src/main/scala/plan.scala
src/main/scala/util/package.scala
src/test/scala/util/utiltest.scala
--- a/src/main/scala/Post.scala	Sat Oct 15 10:58:04 2011 -0400
+++ b/src/main/scala/Post.scala	Sat Oct 15 16:47:37 2011 -0400
@@ -3,6 +3,7 @@
 import org.w3.readwriteweb.util.modelFromString
 
 import java.io.{InputStream, StringReader}
+import java.net.URL
 import scala.io.Source
 import org.slf4j.{Logger, LoggerFactory}
 import com.hp.hpl.jena.rdf.model._
@@ -30,17 +31,17 @@
 
   def parse(
       is: InputStream,
-      baseURI: String,
+      base: URL,
       contentType: String): Post = {
     assert(supportContentTypes contains contentType)
     val source = Source.fromInputStream(is, "UTF-8")
     val s = source.getLines.mkString("\n")
-    parse(s, baseURI, contentType)
+    parse(s, base, contentType)
   }
   
   def parse(
       s: String,
-      baseURI: String,
+      base: URL,
       contentType: String): Post = {
     assert(supportContentTypes contains contentType)
     
@@ -48,14 +49,14 @@
     
     def postUpdate =
       try {
-        val update: UpdateRequest = UpdateFactory.create(s, baseURI)
+        val update: UpdateRequest = UpdateFactory.create(s, base.toString)
         PostUpdate(update).success
       } catch {
         case qpe: QueryParseException => qpe.fail
       }
       
     def postRDF(lang: Lang) =
-      modelFromString(s, baseURI, lang) flatMap { model => PostRDF(model).success }
+      modelFromString(s, base, lang) flatMap { model => PostRDF(model).success }
     
     def postQuery =
       try {
--- a/src/main/scala/plan.scala	Sat Oct 15 10:58:04 2011 -0400
+++ b/src/main/scala/plan.scala	Sat Oct 15 16:47:37 2011 -0400
@@ -54,8 +54,8 @@
    */
   val plan = unfiltered.filter.Planify {
     case req @ Path(path) if path startsWith rm.basePath => {
-      val baseURI = req.underlying.getRequestURL.toString
-      val r: Resource = rm.resource(new URL(baseURI))
+      val Authoritative(baseURI, _) = req
+      val r: Resource = rm.resource(baseURI)
       req match {
         case GET(_) & Accept(accepts) if isHTML(accepts) => {
           val source = Source.fromFile("src/main/resources/skin.html")("UTF-8")
--- a/src/main/scala/util/package.scala	Sat Oct 15 10:58:04 2011 -0400
+++ b/src/main/scala/util/package.scala	Sat Oct 15 16:47:37 2011 -0400
@@ -1,46 +1,21 @@
 package org.w3.readwriteweb
 
 import java.io._
+import java.net.URL
 import com.hp.hpl.jena.rdf.model._
-import com.hp.hpl.jena.query._
-import unfiltered.response._
+
 import scalaz._
 import Scalaz._
 
 package object util {
   
-  val defaultLang = "RDF/XML-ABBREV"
-
-  class MSAuthorVia(value: String) extends ResponseHeader("MS-Author-Via", List(value))
-  
-  object ViaSPARQL extends MSAuthorVia("SPARQL")
-  
-  object ResponseModel {
-    def apply(model: Model, base: String, lang: Lang): ResponseStreamer =
-      new ResponseStreamer {
-        def stream(os: OutputStream): Unit =
-          model.getWriter(lang.jenaLang).write(model, os, base)
-      }
-  }
-
-  object ResponseResultSet {
-    def apply(rs: ResultSet): ResponseStreamer =
-      new ResponseStreamer {
-        def stream(os: OutputStream): Unit = ResultSetFormatter.outputAsXML(os, rs) 
-      }
-    def apply(result: Boolean): ResponseStreamer =
-      new ResponseStreamer {
-        def stream(os: OutputStream):Unit = ResultSetFormatter.outputAsXML(os, result) 
-      }
-  }
-
   def modelFromInputStream(
       is: InputStream,
-      base: String,
+      base: URL,
       lang: Lang): Validation[Throwable, Model] =
     try {
       val m = ModelFactory.createDefaultModel()
-      m.getReader(lang.jenaLang).read(m, is, base)
+      m.getReader(lang.jenaLang).read(m, is, base.toString)
       m.success
     } catch {
       case t => t.fail
@@ -48,12 +23,12 @@
   
   def modelFromString(
       s: String,
-      base: String,
+      base: URL,
       lang: Lang): Validation[Throwable, Model] =
     try {
       val reader = new StringReader(s)
       val m = ModelFactory.createDefaultModel()
-      m.getReader(lang.jenaLang).read(m, reader, base)
+      m.getReader(lang.jenaLang).read(m, reader, base.toString)
       m.success
     } catch {
       case t => t.fail
--- a/src/test/scala/util/utiltest.scala	Sat Oct 15 10:58:04 2011 -0400
+++ b/src/test/scala/util/utiltest.scala	Sat Oct 15 16:47:37 2011 -0400
@@ -1,5 +1,6 @@
 package org.w3.readwriteweb
 
+import java.net.URL
 import javax.servlet._
 import javax.servlet.http._
 import unfiltered.request._
@@ -27,7 +28,7 @@
 
 package object utiltest {
   
-  def baseURI(req: Request): String = "%s%s" format (req.host, req.path)
+  def baseURI(req: Request): URL = new URL("%s%s" format (req.host, req.path))
   
   def beIsomorphicWith(that: Model): Matcher[Model] =
     new Matcher[Model] {
@@ -39,7 +40,7 @@
   
   class RequestW(req: Request) {
 
-    def as_model(base: String, lang: Lang): Handler[Model] =
+    def as_model(base: URL, lang: Lang): Handler[Model] =
       req >> { is => modelFromInputStream(is, base, lang).toOption.get }
 
     def post(body: String, lang: Lang): Request =