this works now. Trickery with null pointer exceptions from Java most likely webid
authorHenry Story <henry.story@bblfish.net>
Sun, 16 Oct 2011 18:24:44 +0200
branchwebid
changeset 75 f46e9a29b75e
parent 74 25c0b03d1091
child 76 5a275bdf7a44
this works now. Trickery with null pointer exceptions from Java most likely
src/main/scala/auth/Authz.scala
--- a/src/main/scala/auth/Authz.scala	Sun Oct 16 17:09:57 2011 +0200
+++ b/src/main/scala/auth/Authz.scala	Sun Oct 16 18:24:44 2011 +0200
@@ -32,6 +32,7 @@
 import java.net.URL
 import org.w3.readwriteweb.{Resource, ResourceManager, WebCache}
 import com.hp.hpl.jena.query.{QueryExecutionFactory, QueryExecution, QuerySolutionMap, QueryFactory}
+import sun.management.resources.agent
 
 
 /**
@@ -129,13 +130,13 @@
     val Control = acl+"Control"
 
     val selectQuery = QueryFactory.create("""
-    		  PREFIX : <http://www.w3.org/ns/auth/acl#>
-    		  SELECT ?res ?agent ?group ?mode
+    		  PREFIX acl: <http://www.w3.org/ns/auth/acl#>
+    		  SELECT ?mode ?group ?agent
     		  WHERE {
-              ?auth :accessTo ?res ;
-                    :mode ?mode .
-    		    OPTIONAL { ?auth :agentClass ?group . }
-    		    OPTIONAL { ?auth :agent ?agent . }
+              ?auth acl:accessTo ?res ;
+                    acl:mode ?mode .
+          OPTIONAL { ?auth acl:agentClass ?group . }
+	        OPTIONAL { ?auth acl:agent ?agent . }
     		  }""")
   }
 
@@ -153,24 +154,32 @@
       } yield {
         val initialBinding = new QuerySolutionMap();
         initialBinding.add("res", model.createResource("file://local"+path))
-        val qe: QueryExecution = QueryExecutionFactory.create(selectQuery, model, initialBinding)
+        val qe: QueryExecution = QueryExecutionFactory.create(selectQuery, model)//, initialBinding)
         val agentsAllowed = try {
-          qe.execSelect().flatMap(qs => {
+          val exec = qe.execSelect()
+          val res = for (qs <- exec) yield {
             val methods = qs.get("mode").toString match {
               case Read => List(GET)
-              case Write => List(PUT,POST)
+              case Write => List(PUT, POST)
               case Control => List(POST)
-              case _ => List(GET, PUT,  POST,  DELETE) //nothing everything is allowed
+              case _ => List(GET, PUT, POST, DELETE) //nothing everything is allowed
             }
-           if (methods.contains(method)) Some(Pair(qs.get("agent").toString,qs.get("group").toString))
-           else None
-          })
+            if (methods.contains(method)) Some(Pair(qs.get("agent"), qs.get("group")))
+            else None
+          }
+          res.flatten.toList
         } finally {
           qe.close()
         }
-        if (agentsAllowed.hasNext) {
+        if (agentsAllowed.size>0) {
           subj() match {
-            case Some(s) => agentsAllowed.exists( p =>  s.getPrincipals(classOf[WebIdPrincipal]).exists(id=> id.webid == p._1) )
+            case Some(s) => agentsAllowed.exists{ 
+              p =>  s.getPrincipals(classOf[WebIdPrincipal]).
+                exists(id=> {
+                val ps = if (p._1 != null) p._1.toString else null;
+                ps == id.webid
+              })
+            }
             case None => false
           }
           //currently we just check for agent match. Group match would require us to have a store