source: TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi/insecure.py @ 772

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi/insecure.py@772
Revision 772, 2.3 KB checked in by lawrence, 14 years ago (diff)

This version calls documents from exist, loads and transforms them
using xalan.
To-do:

(1) results are not yet acceptable,
(2) need to get and parse the security metadata
(3) need to connect to Phil's security code.

Line 
1#
2#This module provides python wrappers to Kevin's java code
3# I'm aiming to avoid having any java dependencies in the main browseCGI code
4# as I figure these command line activities at some point ought to be removed.
5# They certainly should be regarded as potentially insecure, especially the
6# exist call ...
7# BNL, April 2006
8#
9
10import os
11import os.path
12import tempfile
13
14def jarLoc(jar):
15        #don't want the jar in the cgi directory ... not quite sure why, but
16        #it seems wrong ... having made that decision, we have to bugger
17        #around with relative paths depending on whether this is test code
18        #(outside the cgi environment) or in the cgi environment
19        cwd=os.getcwd()
20        return os.path.normpath(os.path.join(cwd,jar))
21
22def osCommand(cmd):
23        stdin,stdout,stderr=os.popen3(cmd)
24        error=stderr.readlines()
25        if error !=[]:
26                return 1,error
27        else:
28                s=''
29                for i in stdout.readlines():s+=i
30                return 0,s
31
32def insecureGetDoc(uri,jar='./returnmoles.jar'):
33        ''' Use Kevin's code to get the document from exist '''
34        #we assume the URI is of the form repository/localid
35        #we don't use os.path.split, because this is os indpendent.
36        try:
37                repPath,localID=uri.split('/')
38        except:
39                return 'Invalid URI'
40        else:
41                returnMolesJar=jarLoc(jar)
42                cmd='java -jar %s %s %s xmldb:exist://glue.badc.rl.ac.uk:8080/exist/xmlrpc'%(
43                returnMolesJar,repPath,localID)
44                status,result=osCommand(cmd)
45                return result
46       
47def doXSLT (document,jar='./xalan-j_2_7_0/xalan.jar',xsltMethod=None):
48        ''' Handle a possibly external transformation to produce an html page '''
49        #use temporary files for the moment, ugly, but it will work.
50        # consider using pyana or some other way of doing this later ...
51        #try:
52        #return 'done'
53        fn=tempfile.mktemp('.ndg-xml')
54        f=file(fn,'wb')
55        f.write(document)
56        f.close()
57        on=tempfile.mktemp('.ndg-xml')
58        xalanjar=jarLoc(jar)
59        xslt=jarLoc('./xslt/stubb_2_browse.xsl')
60        cmd='java -cp %s org.apache.xalan.xslt.Process -IN %s -XSL %s -OUT %s'%(xalanjar,fn,xslt,on)
61        status,result=osCommand(cmd)
62        if status:
63                return result
64        else:
65                f=file(on,'r')
66                result=''
67                for i in f.readlines(): result+=i
68                f.close()
69                r=os.remove(fn)
70                r=os.remove(on)
71                return result
72       
73if __name__=="__main__":
74        d= insecureGetDoc('badc.nerc.ac.uk/dataent13',jar='../returnmoles.jar')
75        #print d
76        dd=doXSLT(d,jar='../xalan-j_2_7_0/xalan.jar')
77        print dd
Note: See TracBrowser for help on using the repository browser.