source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/models/ndgRetrieve.py @ 2852

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/models/ndgRetrieve.py@2852
Revision 2852, 5.1 KB checked in by lawrence, 13 years ago (diff)

Revising the query string to support clear distinction
between format (raw,xml,html) and outputSchema (original, DIF etc).

Line 
1import DocumentRetrieve, ndgSearch, ndgObject, xmlHandler2
2import cgi,time
3from Utilities import myConfig
4from paste.deploy import CONFIG
5
6
7def ndgRetrieve(uri,config,logger=0,output=''):
8   
9    ''' Given an ndgObject, uri, retrieve it, with security in place '''
10   
11   
12    def getws(config,uri,):
13        ''' Get a ws connection to the local exist database '''
14        # The logic here is that
15        #    this code is running on server, which supports repositories sr
16        #    this particular object needs the repository for uri.repository
17        #    which had better be the same as sr, and then we use the
18        #    actual exist repository er.
19        #    I'm sure this can be cleaned up further :-)
20        r=config.get('NDG_B_SERVICE',uri.repository)
21        sr=config.get('DEFAULT','repository')
22        if r<> sr:
23            return 0,'The uri [%s] is not available on [%s]'%(uri,server)
24        er=config.get('NDG_EXIST',uri.repository)
25       
26        #Changes by Dom:
27        #pwf=config.get('DEFAULT','passwordFile') 
28        #passwordFile is now in development.ini, moved from ndgDiscovery.config
29        pwf=cf= CONFIG['app_conf']['passwordFile']
30        ws=DocumentRetrieve.DocumentRetrieve(er,pwfile=pwf)
31        return 1,ws
32   
33    server=config.get('DEFAULT','server')
34    if uri.schema in ['DIF','MDIP']:
35        r=server
36        ws=ndgSearch.ndgSearch()
37    elif uri.schema[0:5]=='NDG-B' or uri.schema=='NumSim':
38        status,ws=getws(config,uri)
39        if not status: return status,ws
40    elif uri.schema=='NDG-A0':
41        status,ws=getws(config,uri)
42        if not status: return status,ws
43    #try:
44    if uri.schema=='NDG-A0':
45        target='/db/ndg_A_metadata'
46    elif uri.schema=='NumSim':
47        target='/db/NumSim'
48    else:
49        target='/db/ndg_B_metadata'
50   
51    print uri.uri, 'Output format [%s]'%output
52     
53    if output=='': output=uri.schema
54   
55   
56 
57    #do the actual retrieve:
58    e=None
59    try:
60        time1=time.time()
61        r=ws.get(uri.repository,uri.schema,uri.localID,format=output,targetCollection=target)
62        time2=time.time()-time1
63        logString='Document retrieve [%s] took [%s]'%(uri,time2)
64    except Exception,e:
65        e=str(e)
66        r=e
67        logString='Document retrieve [%s] failed [%s]'%(uri,e)
68   
69    if logger: logger.info(logString)
70
71    # did we get a sensible answer?
72    if isinstance(r,int):
73        return 0,'<p> There are %s identifiers matching your request! </p>'%r
74    if e is not None:
75        return 0,'Document retrieve[%s] failed [%s]'%(uri,cgi.escape(e))
76   
77    try:
78        x=xmlHandler2.xmlHandler(r,string=1)
79        return 1,x
80    except Exception,e:
81        logString='Document parse for [%s] failed [%s]'%(uri,e)
82        if logger: logger.info(logString)
83        return 0,'''<p>Unable to parse documment [%s]<br/>
84                    Reason was [%s]</p>'''%(uri,cgi.escape(str(e)))
85   
86   
87import unittest
88
89class TestCase(unittest.TestCase):
90   
91    def setup(self):
92        self.c=myConfig('../config/ndgDiscovery.config')
93   
94    def testSOAP(self):
95        ''' test link to SOAP web service for DIF'''
96
97        self.setup()
98        doc='neodc.nerc.ac.uk__DIF__NEODC_NEXTMAP'
99        uri=ndgObject.ndgObject(doc)
100        status,xml=ndgRetrieve(uri,self.c)
101        if not status: raise ValueError(xml)
102       
103    def testOriSOAP(self):
104        ''' test link to SOAP web service for DIF (original version)'''
105
106        self.setup()
107        doc='neodc.nerc.ac.uk__DIF__NEODC_NEXTMAP'
108        uri=ndgObject.ndgObject(doc)
109        status,xml=ndgRetrieve(uri,self.c,output='original')
110        if not status: raise ValueError(xml)
111
112    def testEXIST(self):
113        ''' test link to local exist via xml rpc.
114                users will need to ensure that the document is actually
115                in your exist database! '''
116       
117        self.setup()
118        doc='badc.nerc.ac.uk__NDG-B0__activity_activity_cwvc'
119        doc='neodc.nerc.ac.uk__NDG-B1__NEODC_NCAVEO_LCM2000'
120        uri=ndgObject.ndgObject(doc)
121        status,xml=ndgRetrieve(uri,self.c)
122        if not status: raise ValueError(xml)
123       
124    def testNoSOAP(self):
125        ''' Test that we get a sensible error if a DIF doesn't exist '''
126       
127        self.setup()
128        doc='badc.nerc.ac.uk__DIF__blah'
129        uri=ndgObject.ndgObject(doc)
130        status,xml=ndgRetrieve(uri,self.c)
131        if status: raise ValueError(' DIF [%s] should not exist [%s]'%(doc,xml))
132       
133    def testNoB(self):
134        ''' Test that we get a sensible error if a NDG-B doc doesn't exist '''
135       
136        self.setup()
137        doc='badc.nerc.ac.uk__NDG-B0__blah'
138        uri=ndgObject.ndgObject(doc)
139        status,xml=ndgRetrieve(uri,self.c)
140        if status: raise ValueError(' NDG-B0 [%s] should not exist [%s]'%(doc,xml) )
141       
142       
143    def testXMLdif(self):
144        ''' Make sure we can encode and decode at least one DIF properly '''
145        self.setup()
146        doc='neodc.nerc.ac.uk__DIF__NEODC_NEXTMAP'
147        doc='badc.nerc.ac.uk__DIF__dataent_11738019833217179'
148        uri=ndgObject.ndgObject(doc)
149        status,xml=ndgRetrieve(uri,self.c)
150        if status: s=str(xml)
151
152       
153       
154if __name__=="__main__":
155
156    unittest.main()
Note: See TracBrowser for help on using the repository browser.