From: Sapan Bhatia <sapanb@cs.princeton.edu>
Date: Mon, 26 Oct 2009 14:12:19 +0000 (+0000)
Subject: Continuation - adding request and rule contexts to rspecs currently being processed.
X-Git-Tag: sfa-0.9-6~129
X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=c451060afdf4ef60cdfea767ba6f00d6ffce66f8;p=sfa.git

Continuation - adding request and rule contexts to rspecs currently being processed.
---

diff --git a/sfatables/processors/restrict_to_nodes.xsl b/sfatables/processors/restrict_to_nodes.xsl
index 492e383d..e9b821fe 100644
--- a/sfatables/processors/restrict_to_nodes.xsl
+++ b/sfatables/processors/restrict_to_nodes.xsl
@@ -23,4 +23,3 @@
     </xsl:template>
 
 </xsl:stylesheet>
-et>
diff --git a/sfatables/runtime.py b/sfatables/runtime.py
index bdace139..1aac7731 100644
--- a/sfatables/runtime.py
+++ b/sfatables/runtime.py
@@ -27,7 +27,29 @@ class SFATablesRules:
         self.active_context = request_context
         return
 
-    def add_request_context_to_rspec(self,doc):
+    def create_xml_node(self, name, context_dict):
+        node = libxml2.newNode(name)
+        for k in context_dict.keys():
+            if (type(context_dict[k])==dict):
+                childNode = self.create_xml_node(k, context_dict[k])
+                node.addChild(childNode)
+            else:
+                node.addContent(context_dict[k])
+        return node
+                
+    def add_request_context_to_rspec(self, doc):
+        p = doc.xpathNewContext()
+        context = p.xpathEval("//rspec")
+        if (not context):
+            raise Exception('Request is not an rspec')
+        else:
+            # Add the request context
+            requestNode = self.create_xml_node('request-context',self.active_context)
+            context[0].addChild(requestNode)
+        p.xpathFreeContext()
+        return doc
+
+    def add_rule_context_to_rspec(self,arguments, doc):
         p = doc.xpathNewContext()
         context = p.xpathEval("//rspec")
         if (not context):
@@ -50,8 +72,6 @@ class SFATablesRules:
 
         return doc
 
-
-
     def apply(self, rspec):
         doc = libxml2.parseDoc(rspec)
         doc = self.add_request_context_to_rspec(doc)
@@ -68,6 +88,7 @@ class SFATablesRules:
 
 def main():
     incoming = SFATablesRules('INCOMING')
+    incoming.set_context({'sfa':{'user':{'hrn':'plc.princeton.sapanb'}}})
 
     outgoing = SFATablesRules('OUTGOING')
     print "%d rules loaded for INCOMING chain"%len(incoming.sorted_rule_list)
diff --git a/sfatables/test.sfarspec b/sfatables/test.sfarspec
index 9714e236..6c981808 100644
--- a/sfatables/test.sfarspec
+++ b/sfatables/test.sfarspec
@@ -1,10 +1,6 @@
 <!-- run "runtime.py sfarspec" to apply current rule set to this file -->
 
 <rspec>
-    <request-context>
-        <sfa><user><hrn>plc.princeton.sapanb</hrn></user></sfa>
-    </request-context>
-
     <rule-context>
         <argument>
         <name>blacklist</name>
diff --git a/sfatables/xmlrule.py b/sfatables/xmlrule.py
index b700b60d..a72a480e 100644
--- a/sfatables/xmlrule.py
+++ b/sfatables/xmlrule.py
@@ -3,7 +3,6 @@ import libxslt
 from sfatables.globals import *
 
 class XMLRule:
-    
     def apply_processor(self, type, doc, output_xpath_filter=None):
         processor = self.processors[type]
 
@@ -11,6 +10,7 @@ class XMLRule:
         filepath = os.path.join(sfatables_config, 'processors', processor)
         # XXX
 
+        print filepath
         styledoc = libxml2.parseFile(filepath)
         style = libxslt.parseStylesheetDoc(styledoc)
         result = style.applyStylesheet(doc, None)