+ first servlet implementation for a Sparql endpoint
authorAlexandre Bertails <bertails@w3.org>
Fri, 11 Jun 2010 18:57:29 -0400
changeset 199 f0b45bdd59f7
parent 198 c86f39305813
child 200 a2dfdb589032
child 202 3e73146b970d
+ first servlet implementation for a Sparql endpoint
~ update sbt to 0.7.4 and update dependencies
project/build.properties
project/build/RDB2RDF.scala
src/main/scala/Servlet.scala
src/main/webapp/WEB-INF/web.xml
--- a/project/build.properties	Tue Jun 08 17:01:53 2010 -0400
+++ b/project/build.properties	Fri Jun 11 18:57:29 2010 -0400
@@ -4,7 +4,7 @@
 project.organization=w3c
 scala.version=2.7.5
 project.version=1.0
-sbt.version=0.6.5
-def.scala.version=2.7.5
-build.scala.versions=2.8.0.Beta1-RC2
+sbt.version=0.7.4
+def.scala.version=2.8.0.RC2
+build.scala.versions=2.8.0.RC2 
 project.initialize=false
--- a/project/build/RDB2RDF.scala	Tue Jun 08 17:01:53 2010 -0400
+++ b/project/build/RDB2RDF.scala	Fri Jun 11 18:57:29 2010 -0400
@@ -1,11 +1,14 @@
 import sbt._
 
-class RDB2RDF(info: ProjectInfo) extends DefaultProject(info) {
+class RDB2RDF(info: ProjectInfo) extends DefaultWebProject(info) {
 
   override def compileOptions = super.compileOptions ++ Seq(Unchecked, Deprecation, ExplainTypes)
 
   val scalatools = "scala-tools" at "http://scala-tools.org/repo-snapshots"
 
-  val scalatest = "org.scalatest" % "scalatest" % "1.0.1-for-scala-2.8.0.Beta1-RC1-with-test-interfaces-0.2-SNAPSHOT" % "test->default"
+  val scalatest = "org.scalatest" % "scalatest" % "1.2-for-scala-2.8.0.RC2-SNAPSHOT" % "test"
+
+  val jetty6 = "org.mortbay.jetty" % "jetty" % "6.1.14" % "test"
+  val servlet = "javax.servlet" % "servlet-api" % "2.5" % "provided" 
 
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/Servlet.scala	Fri Jun 11 18:57:29 2010 -0400
@@ -0,0 +1,93 @@
+package org.w3.calendar
+
+import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse} 
+import scala.xml.XML
+
+import java.net.URI
+import w3c.sw.sql.{Sql,DatabaseDesc,Relation,RelationDesc,Attribute,Value,Datatype,ForeignKey,Name}
+import w3c.sw.sparql.Sparql
+import w3c.sw.sparql2sql.{SparqlToSql,StemURI}
+
+object Config {
+
+  // print the classpath, should be just logged
+  val urls = this.getClass.getClassLoader.asInstanceOf[java.net.URLClassLoader].getURLs
+  println("found in classpath:")
+  for(url <- urls) println("\t"+url.getFile)
+
+  private lazy val properties = {
+    val prop = new java.util.Properties
+    prop.load(this.getClass.getResourceAsStream("/calendar.properties"))
+    prop
+  }
+
+  def getProperty(key:String):Option[String] = 
+    try {
+      Some(properties.getProperty(key))
+    } catch {
+      case _ => None
+    }
+
+  val DDLParser = Sql()
+
+  val dbDdl = """
+CREATE TABLE Employee (empid INT, PRIMARY KEY (empid), lastName STRING, birthday DATE, manager INT, FOREIGN KEY (manager) REFERENCES Employee(empid));
+CREATE TABLE Tasks (taskid INT, PRIMARY KEY (taskid), name STRING, lead INT, FOREIGN KEY (lead) REFERENCES Employee(empid));
+CREATE TABLE TaskAssignments (id INT PRIMARY KEY, PRIMARY KEY (id), task INT, FOREIGN KEY (task) REFERENCES Tasks(taskid), employee INT, FOREIGN KEY (employee) REFERENCES Employee(empid));
+"""
+  val db:DatabaseDesc = DDLParser.parseAll(DDLParser.ddl, dbDdl).get
+
+}
+
+class SparqlEndpoint extends HttpServlet {
+
+  val encoding = "utf-8"
+
+  override def doGet(request:HttpServletRequest, response:HttpServletResponse) {
+
+    val r = request.getParameter("query")
+
+    println(r)
+
+    r match {
+      case null | "" => processIndex(request, response)
+      case query     => processSparql(request, response, query)
+    }
+
+  }
+
+  def processSparql(request:HttpServletRequest, response:HttpServletResponse, query:String) {
+
+    val DDLParser = Sql()
+
+    response.setContentType("text/plain; charset='" + encoding + "'")
+
+    response.getWriter.write("yo")
+
+  }
+
+  def processIndex(request:HttpServletRequest, response:HttpServletResponse) {
+
+    val index =
+      <html xmlns="http://www.w3.org/1999/xhtml">
+        <head><title>RDB2RDF Sparql endpoint</title></head>
+	<body>
+	  <h1>RDB2RDF Sparql endpoint</h1>
+	  <form action="sparql">
+            <p><textarea rows="10" cols="80" name="query" id="query">your query here</textarea>
+	    <input type="submit" /></p>
+	  </form>
+	  <hr />
+	  <address>
+	    <a href="http://www.w3.org/People/Eric/">Eric Prud'hommeaux</a>, <a href="http://www.w3.org/People/Bertails/">Alexandre Bertails</a>, Jun 2010
+	  </address>
+	</body>
+      </html>
+
+    response.setContentType("application/xml; charset='" + encoding + "'")
+
+    XML.write(response.getWriter, index, encoding, false, null) 
+
+  }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/webapp/WEB-INF/web.xml	Fri Jun 11 18:57:29 2010 -0400
@@ -0,0 +1,10 @@
+<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
+  <servlet>
+    <servlet-name>sparqlendpoint</servlet-name>
+    <servlet-class>org.w3.calendar.SparqlEndpoint</servlet-class>
+  </servlet>
+  <servlet-mapping>
+    <servlet-name>sparqlendpoint</servlet-name>
+    <url-pattern>/sparql/*</url-pattern>
+  </servlet-mapping>
+</web-app>