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

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

Last bug fixes (well, fixes to handle content problems)
for ticket:853

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,format=''):
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=='DIF':
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    if format=='': format=uri.schema
51   
52    print uri.uri
53 
54    #do the actual search:
55    e=None
56    try:
57        time1=time.time()
58        r=ws.get(uri.repository,uri.schema,uri.localID,format=format,targetCollection=target)
59        time2=time.time()-time1
60        logString='Document retrieve [%s] took [%s]'%(uri,time2)
61    except Exception,e:
62        e=str(e)
63        r=e
64        logString='Document retrieve [%s] failed [%s]'%(uri,e)
65   
66    if logger: logger.info(logString)
67
68    # did we get a sensible answer?
69    if isinstance(r,int):
70        return 0,'<p> There are %s identifiers matching your request! </p>'%r
71    if e is not None:
72        return 0,'Document retrieve[%s] failed [%s]'%(uri,cgi.escape(e))
73   
74    try:
75    x=xmlHandler2.xmlHandler(r,string=1)
76    return 1,x
77    except Exception,e:
78        logString='Document parse for [%s] failed [%s]'%(uri,e)
79        if logger: logger.info(logString)
80        return 0,'''<p>Unable to parse documment [%s]<br/>
81                    Reason was [%s]</p>'''%(uri,cgi.escape(str(e)))
82   
83   
84import unittest
85
86class TestCase(unittest.TestCase):
87   
88    def setup(self):
89        self.c=myConfig('../config/ndgDiscovery.config')
90   
91    def testSOAP(self):
92        ''' test link to SOAP web service for DIF'''
93
94        self.setup()
95        doc='neodc.nerc.ac.uk__DIF__NEODC_NEXTMAP'
96        uri=ndgObject.ndgObject(doc)
97        status,xml=ndgRetrieve(uri,self.c)
98        if not status: raise ValueError(xml)
99       
100    def testOriSOAP(self):
101        ''' test link to SOAP web service for DIF (original version)'''
102
103        self.setup()
104        doc='neodc.nerc.ac.uk__DIF__NEODC_NEXTMAP'
105        uri=ndgObject.ndgObject(doc)
106        status,xml=ndgRetrieve(uri,self.c,format='original')
107        if not status: raise ValueError(xml)
108
109    def testEXIST(self):
110        ''' test link to local exist via xml rpc.
111                users will need to ensure that the document is actually
112                in your exist database! '''
113       
114        self.setup()
115        doc='badc.nerc.ac.uk__NDG-B0__activity_activity_cwvc'
116        doc='neodc.nerc.ac.uk__NDG-B1__NEODC_NCAVEO_LCM2000'
117        uri=ndgObject.ndgObject(doc)
118        status,xml=ndgRetrieve(uri,self.c)
119        if not status: raise ValueError(xml)
120       
121    def testNoSOAP(self):
122        ''' Test that we get a sensible error if a DIF doesn't exist '''
123       
124        self.setup()
125        doc='badc.nerc.ac.uk__DIF__blah'
126        uri=ndgObject.ndgObject(doc)
127        status,xml=ndgRetrieve(uri,self.c)
128        if status: raise ValueError(' DIF [%s] should not exist [%s]'%(doc,xml))
129       
130    def testNoB(self):
131        ''' Test that we get a sensible error if a NDG-B doc doesn't exist '''
132       
133        self.setup()
134        doc='badc.nerc.ac.uk__NDG-B0__blah'
135        uri=ndgObject.ndgObject(doc)
136        status,xml=ndgRetrieve(uri,self.c)
137        if status: raise ValueError(' NDG-B0 [%s] should not exist [%s]'%(doc,xml) )
138       
139       
140    def testXMLdif(self):
141        ''' Make sure we can encode and decode at least one DIF properly '''
142        self.setup()
143        doc='neodc.nerc.ac.uk__DIF__NEODC_NEXTMAP'
144        doc='badc.nerc.ac.uk__DIF__dataent_11738019833217179'
145        uri=ndgObject.ndgObject(doc)
146        status,xml=ndgRetrieve(uri,self.c)
147        if status: s=str(xml)
148
149       
150       
151if __name__=="__main__":
152
153    unittest.main()
Note: See TracBrowser for help on using the repository browser.