~ implemented the switch mode, not yet mapped in the binary
authorAlexandre Bertails <bertails@w3.org>
Mon, 29 Aug 2011 17:26:37 -0400
changeset 27 c380c98da12f
parent 26 c81f55f40e4c
child 28 2baaeecfbe43
~ implemented the switch mode, not yet mapped in the binary
src/main/scala/Main.scala
src/main/scala/Resource.scala
src/main/scala/util.scala
src/test/scala/Test.scala
--- a/src/main/scala/Main.scala	Mon Aug 29 16:36:00 2011 -0400
+++ b/src/main/scala/Main.scala	Mon Aug 29 17:26:37 2011 -0400
@@ -30,59 +30,76 @@
       val baseURI = req.underlying.getRequestURL.toString
       val r:Resource = rm.resource(new URL(baseURI))
       req match {
-        case GET(_) | HEAD(_) => {
-          val model:Model = r.get()
-          val encoding = RDFEncoding(req)
-          req match {
-            case GET(_) => Ok ~> ViaSPARQL ~> ResponseModel(model, baseURI, encoding)
-            case HEAD(_) => Ok ~> ViaSPARQL
+        case GET(_) | HEAD(_) =>
+          try {
+            val model:Model = r.get()
+            val encoding = RDFEncoding(req)
+            req match {
+              case GET(_) => Ok ~> ViaSPARQL ~> ResponseModel(model, baseURI, encoding)
+              case HEAD(_) => Ok ~> ViaSPARQL
+            }
+          } catch {
+            case fnfe:FileNotFoundException => NotFound
+            case t:Throwable => {
+              req match {
+                case GET(_) => InternalServerError ~> ViaSPARQL
+                case HEAD(_) => InternalServerError ~> ViaSPARQL ~> ResponseString(t.getStackTraceString)
+              }
+            }
           }
-        }
-        case PUT(_) => {
-          val bodyModel = modelFromInputStream(Body.stream(req), baseURI)
-          r.save(bodyModel)
-          Created
-        }
-        case POST(_) => {
-          /* http://openjena.org/ARQ/javadoc/com/hp/hpl/jena/update/UpdateFactory.html */
-          Post.parse(Body.stream(req), baseURI) match {
-            case PostUnknown => BadRequest ~> ResponseString("You MUST provide valid content for either: SPARQL UPDATE, SPARQL Query, RDF/XML, TURTLE")
-            case PostUpdate(update) => {
-              val model = r.get()
-              UpdateAction.execute(update, model)
-              r.save(model)
-              Ok
-            }
-            case PostRDF(diffModel) => {
-              val model = r.get()
-              model.add(diffModel)
-              r.save(model)
-              Ok
-            }
-            case PostQuery(query) => {
-              lazy val encoding = RDFEncoding(req)
-              val model:Model = r.get()
-              val qe:QueryExecution = QueryExecutionFactory.create(query, model)
-              query.getQueryType match {
-                case SELECT =>
-                  Ok ~> ResponseResultSet(qe.execSelect())
-                case ASK =>
-                  Ok ~> ResponseResultSet(qe.execAsk())
-                case CONSTRUCT => {
-                  val result:Model = qe.execConstruct()
-                  Ok ~> ResponseModel(model, baseURI, encoding)
-                }
-                case DESCRIBE => {
-                  val result:Model = qe.execDescribe()
-                  Ok ~> ResponseModel(model, baseURI, encoding)
+        case PUT(_) =>
+          try {
+            val bodyModel = modelFromInputStream(Body.stream(req), baseURI)
+            r.save(bodyModel)
+            Created
+          } catch {
+            case t:Throwable => InternalServerError ~> ResponseString(t.getStackTraceString)
+          }
+        case POST(_) =>
+          try {
+            Post.parse(Body.stream(req), baseURI) match {
+              case PostUnknown =>
+                BadRequest ~> ResponseString("You MUST provide valid content for either: SPARQL UPDATE, SPARQL Query, RDF/XML, TURTLE")
+              case PostUpdate(update) => {
+                val model = r.get()
+                UpdateAction.execute(update, model)
+                r.save(model)
+                Ok
+              }
+              case PostRDF(diffModel) => {
+                val model = r.get()
+                model.add(diffModel)
+                r.save(model)
+                Ok
+              }
+              case PostQuery(query) => {
+                lazy val encoding = RDFEncoding(req)
+                val model:Model = r.get()
+                val qe:QueryExecution = QueryExecutionFactory.create(query, model)
+                query.getQueryType match {
+                  case SELECT =>
+                    Ok ~> ResponseResultSet(qe.execSelect())
+                  case ASK =>
+                    Ok ~> ResponseResultSet(qe.execAsk())
+                  case CONSTRUCT => {
+                    val result:Model = qe.execConstruct()
+                    Ok ~> ResponseModel(model, baseURI, encoding)
+                  }
+                  case DESCRIBE => {
+                    val result:Model = qe.execDescribe()
+                    Ok ~> ResponseModel(model, baseURI, encoding)
+                  }
                 }
               }
             }
+          } catch {
+            case fnfe:FileNotFoundException => NotFound
+            case t:Throwable => InternalServerError ~> ResponseString(t.getStackTraceString)
           }
-        }
         case _ => MethodNotAllowed ~> Allow("GET", "PUT", "POST")
       }
     }
+
   }
 
 }
@@ -109,7 +126,7 @@
       System.exit(2)
     }
 
-    val filesystem = new Filesystem(baseDirectory, baseURL)
+    val filesystem = new Filesystem(baseDirectory, baseURL)(ResourcesDontExistByDefault)
     
     val app = new ReadWriteWeb(filesystem)
 
--- a/src/main/scala/Resource.scala	Mon Aug 29 16:36:00 2011 -0400
+++ b/src/main/scala/Resource.scala	Mon Aug 29 17:26:37 2011 -0400
@@ -19,7 +19,7 @@
   def save(model:Model):Unit
 }
 
-class Filesystem(baseDirectory:File, basePath:String, val lang:String = "RDF/XML-ABBREV") extends ResourceManager {
+class Filesystem(baseDirectory:File, basePath:String, val lang:String = "RDF/XML-ABBREV")(implicit mode:RWWMode) extends ResourceManager {
   
   val logger:Logger = LoggerFactory.getLogger(this.getClass)
   
@@ -44,11 +44,16 @@
         try {
           m.read(fis, url.toString)
         } catch {
-          case je:JenaException => logger.error("File %s was either empty or corrupted: considered as empty graph" format fileOnDisk.getAbsolutePath)
+          case je:JenaException => sys.error("File %s was either empty or corrupted: considered as empty graph" format fileOnDisk.getAbsolutePath)
         }
         fis.close()
+        m
+      } else {
+        mode match {
+          case AllResourcesAlreadyExist => m
+          case ResourcesDontExistByDefault => throw new FileNotFoundException
       }
-      m
+      }
     }
     
     def save(model:Model):Unit = {
--- a/src/main/scala/util.scala	Mon Aug 29 16:36:00 2011 -0400
+++ b/src/main/scala/util.scala	Mon Aug 29 17:26:37 2011 -0400
@@ -19,6 +19,10 @@
 import unfiltered.response._
 import unfiltered.jetty._
 
+sealed trait RWWMode
+case object AllResourcesAlreadyExist extends RWWMode
+case object ResourcesDontExistByDefault extends RWWMode
+
 sealed trait RDFEncoding
 case object RDFXML extends RDFEncoding
 case object TURTLE extends RDFEncoding
--- a/src/test/scala/Test.scala	Mon Aug 29 16:36:00 2011 -0400
+++ b/src/test/scala/Test.scala	Mon Aug 29 17:26:37 2011 -0400
@@ -36,7 +36,7 @@
 //    if (joeOnDisk.exists) joeOnDisk.delete()
   }
   
-  val filesystem = new Filesystem(base, "/2007/wiki")
+  val filesystem = new Filesystem(base, "/2007/wiki")(ResourcesDontExistByDefault)
 
   def setup = { _.filter(new ReadWriteWeb(filesystem).read) }
     
@@ -141,15 +141,10 @@
   }
 
   "POSTing an RDF document to a resource that does not exist" should {
-    val uri = host / "2007/wiki/somewhereelse"
-    "succeed" in {
-      val httpCode:Int = Http(uri.post(finalRDF) get_statusCode)
-      // TODO is it a 201?
-      httpCode must_== 200
-    }
-    "create the graph" in {
-      val model = Http(uri as_model(baseURI(uri)))
-      model must beIsomorphicWith (modelFromString(finalRDF, baseURI(uri)))
+    val doesnotexist = host / "2007/wiki/doesnotexist"
+    "return a 404" in {
+      val httpCode:Int = Http.when( _ => true)(doesnotexist get_statusCode)
+      httpCode must_== 404
     }
   }