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

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

Support for viewing xhtml description sections within stub-B

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='',remote=0):
8   
9    ''' Given an ndgObject, uri, retrieve it, with security in place.
10            If logger is non-zero it should be a python log class
11            If output is not '' it should be desired output schema (normally
12        to be used for discovery documents in a variety of outputs or
13        to indicate that an original document is required.
14            If remote is non zero, then attempt to get the document
15        from a remote site via a (secured) restful http call'''
16   
17    def getws(config,uri,remote):
18        ''' Get a ws connection to the local exist database '''
19        # The logic here is that
20        #    this code is running on server, which supports repositories sr
21        #    this particular object needs the repository for uri.repository
22        #    which had better be the same as sr, and then we use the
23        #    actual exist repository er.
24        #    I'm sure this can be cleaned up further :-)
25        r=config.get('NDG_B_SERVICE',uri.repository)
26        sr=config.get('DEFAULT','repository')
27        er=config.get('NDG_EXIST',uri.repository)
28        if r<> sr:
29            if not remote:
30                return 0,'The uri [%s] is not available on [%s]'%(uri,server)
31            else:
32                ps=config.get('DEFAULT','proxyServer')
33                ws=DocumentRetrieve.ndgHTTP(r,proxyServer=ps)
34        else:
35            pwf=config.get('NDG_EXIST','passwordFile')
36            ws=DocumentRetrieve.DocumentRetrieve(er,pwfile=pwf)
37        return 1,ws
38   
39    server=config.get('DEFAULT','server')
40    if uri.schema in ['DIF','MDIP']:
41        r=server
42        status,ws=1,ndgSearch.ndgSearch()
43    elif uri.schema[0:5]=='NDG-B' or uri.schema=='NumSim':
44        status,ws=getws(config,uri,remote)
45    elif uri.schema=='NDG-A0':
46        status,ws=getws(config,uri,remote)
47    else:
48        status=0
49        ws='<p> Invalid scheme in NDG format URI </p>'
50     
51    if not status: return status,ws
52           
53    #try:
54    if uri.schema=='NDG-A0':
55        target='/db/ndg_A_metadata'
56    elif uri.schema=='NumSim':
57        target='/db/NumSim'
58    else:
59        target='/db/ndg_B_metadata'
60     
61    if output=='': output=uri.schema
62   
63    #do the actual retrieve:
64    e=None
65    try:
66        time1=time.time()
67        r=ws.get(uri.repository,uri.schema,uri.localID,format=output,targetCollection=target)
68        time2=time.time()-time1
69        logString='Document retrieve [%s] took [%s]'%(uri,time2)
70    except Exception,e:
71        e=str(e)
72        r=e
73        logString='Document retrieve [%s] failed [%s]'%(uri,e)
74   
75    if logger: logger.info(logString)
76
77    # did we get a sensible answer?
78    if isinstance(r,int):
79        return 0,'<p> There are %s identifiers matching your request! </p>'%r
80    if e is not None:
81        return 0,'Document retrieve[%s] failed [%s]'%(uri,cgi.escape(e))
82   
83    try:
84        x=xmlHandler2.xmlHandler(r,string=1)
85       
86        return 1,x
87    except Exception,e:
88        logString='Document parse for [%s] failed [%s]'%(uri,e)
89        if logger: logger.info(logString)
90        return 0,'''<p>Unable to parse documment [%s]<br/>
91                    Reason was [%s]</p>'''%(uri,cgi.escape(str(e)))
92   
93   
94import unittest
95
96class TestCase(unittest.TestCase):
97   
98    def setup(self):
99        self.c=myConfig('../../ndgDiscovery.config')
100   
101    def testSOAP(self):
102        ''' test link to SOAP web service for DIF'''
103        self.setup()
104        doc='neodc.nerc.ac.uk__DIF__dataent_11707799142713994'
105        uri=ndgObject.ndgObject(doc)
106        status,xml=ndgRetrieve(uri,self.c)
107        if not status: raise ValueError(xml)
108       
109    def testOriSOAP(self):
110        ''' test link to SOAP web service for DIF (original version)'''
111        self.setup()
112        doc='neodc.nerc.ac.uk__DIF__dataent_11707799142713994'
113        uri=ndgObject.ndgObject(doc)
114        status,xml=ndgRetrieve(uri,self.c,output='original')
115        if not status: raise ValueError(xml)
116
117    def testEXIST(self):
118        ''' test link to local exist via xml rpc.
119                users will need to ensure that the document is actually
120                in your exist database! '''
121        self.setup()
122        doc='badc.nerc.ac.uk__NDG-B0__activity_activity_cwvc'
123        doc='neodc.nerc.ac.uk__NDG-B1__dataent_11924879127625221'
124        uri=ndgObject.ndgObject(doc)
125        status,xml=ndgRetrieve(uri,self.c)
126        if not status: raise ValueError(xml)
127       
128    def testNoSOAP(self):
129        ''' Test that we get a sensible error if a DIF doesn't exist '''
130        self.setup()
131        doc='badc.nerc.ac.uk__DIF__blah'
132        uri=ndgObject.ndgObject(doc)
133        status,xml=ndgRetrieve(uri,self.c)
134        if status: raise ValueError(' DIF [%s] should not exist [%s]'%(doc,xml))
135       
136    def testNoB(self):
137        ''' Test that we get a sensible error if a NDG-B doc doesn't exist '''
138        self.setup()
139        doc='badc.nerc.ac.uk__NDG-B0__blah'
140        uri=ndgObject.ndgObject(doc)
141        status,xml=ndgRetrieve(uri,self.c)
142        if status: raise ValueError(' NDG-B0 [%s] should not exist [%s]'%(doc,xml) )
143       
144       
145    def testXMLdif(self):
146        ''' Make sure we can encode and decode at least one DIF properly '''
147        self.setup()
148        doc='neodc.nerc.ac.uk__DIF__NEODC_NEXTMAP'
149        doc='badc.nerc.ac.uk__DIF__dataent_11738019833217179'
150        uri=ndgObject.ndgObject(doc)
151        status,xml=ndgRetrieve(uri,self.c)
152        if status: s=str(xml)
153
154    def testRemoteGet(self):
155        ''' Attempt a remote get '''
156        self.setup()
157        doc='www.npm.ac.uk__NDG-B1__data_entity.363'
158        uri=ndgObject.ndgObject(doc)
159        status,xml=ndgRetrieve(uri,self.c,remote=1)
160        if not status: print xml #ie print error message if there is one!
161        self.assertEqual(status,1)
162       
163    def testNumSim(self):
164        '''Get a NumSim document '''
165        self.setup()
166        doc='badc.nerc.ac.uk__NumSim__HadAM2_CodeBase'
167        doc='badc.nerc.ac.uk__NumSim__HadGEM1_Control_Experiment'
168        uri=ndgObject.ndgObject(doc)
169        status,xml=ndgRetrieve(uri,self.c)
170        self.assertEqual(status,1)
171       
172    def testHTTPget(self):
173        ''' Try a remote get with no frills'''
174        s='http://www.google.co.uk'
175        x=DocumentRetrieve.genericHTTP(proxyServer='http://wwwcache3.rl.ac.uk:8080/')
176        y=x.get(s)
177       
178    def testVocabServer(self):
179        ''' Test the vocabServer '''
180        v=DocumentRetrieve.ndgVocabPOX(proxyServer="http://wwwcache3.rl.ac.uk:8080/")
181        print v.getRelated('rain')
182       
183       
184       
185       
186if __name__=="__main__":
187
188    unittest.main()
Note: See TracBrowser for help on using the repository browser.