--- a/src/main/scala/Main.scala Sun Aug 28 10:36:01 2011 -0400
+++ b/src/main/scala/Main.scala Sun Aug 28 14:10:01 2011 -0400
@@ -18,7 +18,6 @@
import org.w3.readwriteweb.util._
-// holds some Unfiltered plans
class ReadWriteWeb(base:File) {
val logger:Logger = LoggerFactory.getLogger(this.getClass)
@@ -94,6 +93,32 @@
fos.close()
Ok
}
+ case PostQuery(query) => {
+ import Query.{QueryTypeSelect => SELECT,
+ QueryTypeAsk => ASK,
+ QueryTypeConstruct => CONSTRUCT,
+ QueryTypeDescribe => DESCRIBE}
+ val model:Model = loadModel(fileOnDisk)
+ val qe:QueryExecution = QueryExecutionFactory.create(query, model)
+ query.getQueryType match {
+ case SELECT => {
+ val resultSet:ResultSet = qe.execSelect()
+ Ok ~> ResponseResultSet(resultSet)
+ }
+ case ASK => {
+ val result:Boolean = qe.execAsk()
+ Ok ~> ResponseResultSet(result)
+ }
+ case CONSTRUCT => {
+ val result:Model = qe.execConstruct()
+ Ok ~> ResponseModel(model, baseURI)
+ }
+ case DESCRIBE => {
+ val result:Model = qe.execDescribe()
+ Ok ~> ResponseModel(model, baseURI)
+ }
+ }
+ }
}
}
@@ -111,8 +136,6 @@
// regular Java main
def main(args: Array[String]) {
- // can provide the port as first argument
- // default to 2719
val (port, directory) = args.toList match {
case port :: directory :: Nil => (port.toInt, directory)
case _ => sys.error("wrong arguments")
--- a/src/main/scala/Post.scala Sun Aug 28 10:36:01 2011 -0400
+++ b/src/main/scala/Post.scala Sun Aug 28 14:10:01 2011 -0400
@@ -15,6 +15,7 @@
sealed trait Post
case class PostUpdate(update:UpdateRequest) extends Post
case class PostRDF(model:Model) extends Post
+case class PostQuery(query:Query) extends Post
object Post {
@@ -30,10 +31,16 @@
val update:UpdateRequest = UpdateFactory.create(s, baseURI)
PostUpdate(update)
} catch {
- case qpe:QueryParseException => {
- val model = modelFromString(s, baseURI)
- PostRDF(model)
- }
+ case qpe:QueryParseException =>
+ try {
+ val model = modelFromString(s, baseURI)
+ PostRDF(model)
+ } catch {
+ case je:JenaException => {
+ val query = QueryFactory.create(s)
+ PostQuery(query)
+ }
+ }
}
}
--- a/src/main/scala/util.scala Sun Aug 28 10:36:01 2011 -0400
+++ b/src/main/scala/util.scala Sun Aug 28 14:10:01 2011 -0400
@@ -32,7 +32,18 @@
def stream(os:OutputStream):Unit = model.write(os, lang, 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, lang:String = "RDF/XML-ABBREV"):Model = {
val m = ModelFactory.createDefaultModel()
m.read(is, base, lang)
--- a/src/test/scala/Test.scala Sun Aug 28 10:36:01 2011 -0400
+++ b/src/test/scala/Test.scala Sun Aug 28 14:10:01 2011 -0400
@@ -116,5 +116,18 @@
model must beIsomorphicWith (expectedFinalModel)
}
}
+
+ val queryFoafName =
+"""
+PREFIX foaf: <http://xmlns.com/foaf/0.1/>
+SELECT ?name WHERE { [] foaf:name ?name }
+"""
+
+ "POSTing a SPARQL query { [] foaf:name ?name } to Joe's URI" should {
+ "return Joe's name" in {
+ val resultSet = Http(joe.post(queryFoafName) >- { body => ResultSetFactory.fromXML(body) } )
+ resultSet.next().getLiteral("name").getString must_== "Joe Lambda"
+ }
+ }
}