source: ndgCommon/trunk/ndg/common/src/dal/ndgRetrieve.py @ 4932

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/ndgCommon/trunk/ndg/common/src/dal/ndgRetrieve.py@4932
Revision 4932, 5.1 KB checked in by cbyrom, 11 years ago (diff)

Update ndgRetrieve to use new clients suite + tidy up logic slightly.

Line 
1'''
2 Given an uri (an instance of ndgObject), retrieve it, with security in place.
3 
4 @author: B Lawrence?
5'''
6import cgi,time, logging
7import DocumentRetrieve
8from ndg.common.src.clients.xmldb.eXist.searchclient import SearchClient
9import ndg.common.src.clients.xmldb.eXist.dbconstants as dc
10from ndg.common.src.clients.ws.discoveryserviceclient import DiscoveryServiceClient
11from ndg.common.src.models.ndgObject import ndgObject as no
12
13
14class NDGRetrieveError(Exception):
15    """Exception handling for NDG Retrieve method."""
16    def __init__(self, msg):
17        logging.error(msg)
18        Exception.__init__(self, msg)
19
20
21def ndgRetrieve(uri, config, output='',remote=0, discovery=1):
22   
23    ''' Given an uri (an instance of ndgObject), retrieve it, with security in place.
24            If output is not '' it should be desired output schema!
25            If remote is non zero, then attempt to get the document
26        from a remote exist site via a (secured) restful http call
27            If discovery is non-zero AND the schema is in
28                [DIF,MDIP,or ISO]
29        then use the discoveryserviceclient class to obtain the document rather
30        than direct from an eXist database.'''
31   
32    def getws(config,uri,remote):
33        ''' Get a ws connection to the local exist database '''
34        logging.debug("Getting WS connection to eXist DB")
35        # The logic here is that
36        #    this code is running on server, which supports repositories sr
37        #    this particular object needs the repository for uri.repository
38        #    which had better be the same as sr, and then we use the
39        #    actual exist repository er.
40        #    I'm sure this can be cleaned up further :-)
41        if remote and discovery:
42            raise ValueError, 'Invalid options to ndgRetrieve (remote AND discovery impossible)'
43        if discovery:
44            logging.debug("Using Discovery service")
45            if uri.schema in no.DISCOVERY_SCHEMES:
46                ws = DiscoveryServiceClient()
47            else:
48                raise NDGRetrieveError('Invalid schema (%s) with discovery \
49                    option in ndgRetrieve' %uri.schema)
50        else:
51            logging.debug("Using Browse service")
52            try:
53                r=config.get('NDG_B_SERVICE',uri.repository)
54                sr=config.get('DEFAULT','repository')
55                logging.debug("NDG_B_SERVICE: '%s', default repository: '%s'" %(r,sr))
56            except:
57                raise NDGRetrieveError("Config file not available or incomplete " + \
58                    "- cannot find data for NDG_B_SERVICE, %s" %uri.repository)
59           
60            if r <> sr:
61                if not remote:
62                    return 0,'The uri [%s] is not available on [%s]'%(uri,sr)
63                else:
64                    try:
65                        ps=config.get('DEFAULT','proxyServer')
66                    except:
67                        raise NDGRetrieveError('Config file not available \
68                            or has no [proxyServer] setting')
69                logging.debug("Retrieving data from remote proxy server, '%s'" %ps)
70                ws = DocumentRetrieve.ndgHTTP(r,proxyServer=ps)
71            else:
72                try:
73                    logging.info("Lookup up repository:'%s'" %uri.repository)
74                    er=config.get('NDG_EXIST',uri.repository)
75                    pwf=config.get('NDG_EXIST','passwordFile')
76                except:
77                    raise NDGRetrieveError('Config file not available or has \
78                        no [NDG_EXIST] setting')
79
80                logging.debug("Retrieving data from repository, '%s'" %er)
81                ws = SearchClient(dbHostName = er,
82                                  configFileName = pwf)
83        return 1,ws
84
85   
86    status,ws=getws(config,uri,remote)
87 
88    if not status: return status,ws
89           
90    if uri.schema==no.NDGA0_DOC_TYPE:
91        target = dc.NDG_A_COLLECTION_PATH
92    elif uri.schema == no.NUMSIM_DOC_TYPE:
93        target = dc.NUMSIM_COLLECTION_PATH
94    elif uri.schema == no.ATOM_DOC_TYPE:
95        target = dc.ATOM_COLLECTION_PATH
96    elif uri.schema == no.ATOM_BACKUP_DOC_TYPE:
97        target = dc.BACKUP_COLLECTION_PATH
98    elif uri.schema == no.BROWSE_DIF_DOC_TYPE:
99        target = dc.DIF_COLLECTION_PATH
100    else:
101        target = dc.MOLES_COLLECTION_PATH
102   
103    # strip trainling slashes, if required
104    if target.endswith('/'):
105        target = target[0:-1]
106       
107    #do the actual retrieve:
108    e=None
109    try:
110        time1=time.time()
111       
112        r=ws.getNDGDoc(uri.repository,uri.schema,uri.localID,targetCollection=target)
113        time2=time.time()-time1
114        logging.info('Document retrieve [%s] took [%s]'%(uri,time2))
115    except Exception,e:
116        e=str(e)
117        r=e
118        logging.error('Document retrieve [%s] failed [%s]'%(uri,e))
119   
120
121    # did we get a sensible answer?
122    if isinstance(r,int):
123        return 0,'<p> There are %s identifiers matching your request! </p>'%r
124    if e is not None:
125        return 0,'Document retrieve [%s] failed [%s]'%(uri,cgi.escape(e))
126   
127    # we used to return an xmlHandler instance, but we don't do that any more ...
128    return 1,r
129   
Note: See TracBrowser for help on using the repository browser.