source: MILK/trunk/milk_server/milk_server/config/milkMiddleware.py @ 5303

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/MILK/trunk/milk_server/milk_server/config/milkMiddleware.py@5303
Revision 5303, 8.9 KB checked in by cbyrom, 11 years ago (diff)

Retrieve the 'orderByFieldList' data from the discovery ws + add new
drop down to set the order by direction and implement the logic
for using this.

Line 
1'''
2Class defining the config params for the milk server
3'''
4from paste.deploy import CONFIG
5from ndg.common.src.models.myconfig import myConfig
6from ndg.common.src.lib.atomvalidator import AtomValidator
7from ndg.common.src.clients.http.vocabserverclient import VocabServerClient
8from ndg.common.src.clients.ws.discovery.discoveryserviceclient import DiscoveryServiceClient
9import milk_server.lib.helpers as h
10
11class NDGConfigError(Exception): 
12    '''Errors related to reading from ndg config file'''
13     
14class milkMiddleware:
15   
16    def __init__(self, app, g, app_conf):
17       
18        #this is the next application in the wsgi stack
19        self.app=app
20       
21        #set up the ndg configuration file
22        cf= CONFIG['app_conf']['configfile']
23        cf=myConfig(cf)
24     
25        self.globals=g
26        self.globals.htdocs=cf.get('DEFAULT','htdocs',None)
27        self.globals.localLink=cf.get('layout','localLink',None)
28        self.globals.ndgLink=cf.get('layout','ndgLink','http://ndg.nerc.ac.uk')
29        self.globals.ndgImage=cf.get('layout','ndgImage',None)
30        self.globals.ndgAlt=cf.get('layout','ndgAlt','Visit NDG')
31        self.globals.stfcLink=cf.get('layout','stfcLink')
32        self.globals.stfcImage=cf.get('layout','stfcImage')
33        self.globals.helpIcon=cf.get('layout','helpIcon')
34        self.globals.navPlus=cf.get('layout','navPlus')
35        self.globals.loadingIcon=cf.get('layout','loadingIcon')
36        self.globals.navMinus=cf.get('layout','navMinus')
37        self.globals.LeftAlt=cf.get('layout','HdrLeftAlt')
38        self.globals.LeftLogo=cf.get('layout','HdrLeftLogo')
39        self.globals.icons_xml=cf.get('layout','Xicon')
40        self.globals.icons_plot=cf.get('layout','plot')
41        self.globals.icons_prn=cf.get('layout','printer')
42        self.globals.icons_A=cf.get('NDG_A_SERVICE','icon')
43        self.globals.icons_B=cf.get('NDG_B_SERVICE','icon')
44        self.globals.icons_D=cf.get('DISCOVERY','icon')
45        self.globals.icons_R=cf.get('RELATED','icon')
46        self.globals.icons_key=cf.get('layout','key')
47        self.globals.feedIcon = cf.get('layout','feedIcon')
48       
49        self.globals.debugModeOn = \
50            self.__getTrueFalseValue(cf.get('DEFAULT','debug')) 
51       
52        self.globals.atomEditorEnabled = \
53            self.__getTrueFalseValue(cf.get('MILK_SERVER','editorOn')) 
54
55        self.globals.browseEnabled = \
56            self.__getTrueFalseValue(cf.get('MILK_SERVER','browseOn')) 
57
58        self.globals.discoveryEnabled = \
59            self.__getTrueFalseValue(cf.get('MILK_SERVER','discoveryOn')) 
60
61        self.globals.wmcClientURL = cf.get('WMC_CLIENT','url')
62       
63        self.globals.disclaimer=cf.get('default','disclaimer')
64       
65        self.globals.discoveryURL=cf.get('SEARCH','discoveryURL')
66       
67        self.globals.server=cf.get('DEFAULT','server','')
68
69        self.globals.proxyServer = cf.get('DEFAULT','proxyServer')
70
71        self.globals.mailServer = cf.get('DEFAULT','mailServer')
72        self.globals.tbRecipient = cf.get('DEFAULT', 'tbrecipient')
73        self.globals.metadataMaintainer = cf.get('DEFAULT','metadataMaintainer')
74               
75        # for standalone discovery
76        self.globals.standalone = cf.config.getboolean('DISCOVERY','standalone')
77
78        # NB, without this set, the default host on ndg.badc.rl.ac.uk will be
79        # used by discoveryServiceClient
80        self.globals.discoveryServiceURL = cf.get('DISCOVERY', 'discoveryServiceURL')
81
82        searchClient = DiscoveryServiceClient(HostAndPort = self.globals.discoveryServiceURL)
83        # Look up values to use in the 'order by' list
84        orderByVals = searchClient.getList('orderByFieldList')
85        self.globals.orderByList = orderByVals
86               
87        self.globals.pwFile = cf.get('NDG_EXIST','passwordFile')
88        self.globals.localEXist = cf.get('NDG_EXIST','local')
89        # set up the validator for atoms - to allow re-use across the app
90        self.globals.validator = AtomValidator(None,
91                                               localMILKServer = self.globals.server, 
92                                               dbConfigFile = self.globals.pwFile, 
93                                               dbHostName = self.globals.localEXist,
94                                               newLineChar="<br/>",
95                                               isDebug = self.globals.debugModeOn,
96                                               loadAllCollections = False)
97
98        # initialise client to vocab server - to do vocab term searches
99        self.globals.vocabServerClient = VocabServerClient(proxyServer = self.globals.proxyServer)
100
101        # initialise collection to store re-usable connections to the eXist DB
102        self.globals.eXistDBCons = {}
103       
104        # Security Related
105
106        # Single Sign On settings - check for mode of operation:
107        # 1) act as a client to a separate Single Sign On Service
108        # or
109        # 2) Single Sign On service is integrated into THIS service
110        securityEnabled = not self.globals.standalone
111        isSSOClient = cf.config.has_section('NDG_SECURITY.ssoClient') and \
112            securityEnabled
113           
114        isSSOService = cf.config.has_section('NDG_SECURITY.ssoService') and \
115            securityEnabled
116           
117        if isSSOClient and isSSOService:
118            raise NDGConfigError(\
119                "NDG_SECURITY.ssoClient and NDG_SECURITY.ssoService " + \
120                "sections are present in the NDG Config file: " + \
121                "only one or the other may be set")
122 
123        if isSSOClient:
124            try:
125                from \
126            ndg.security.client.ssoclient.ssoclient.config.ssoClientMiddleware\
127                    import SSOMiddleware
128            except ImportError, e:
129                # If standalone flag is not present security must be enabled
130                raise NDGConfigError(\
131                    '%s: importing Single Sign On Client SSOMiddleware: %s' % \
132                        (__name__, e))
133                       
134               
135            self.app = SSOMiddleware(app, cf.config, g, app_conf,
136                                     defSection='NDG_SECURITY.ssoClient')
137           
138            self.globals.sslServer = g.ndg.security.common.sso.cfg.sslServer
139            self.globals.wayfuri=g.ndg.security.common.sso.cfg.wayfuri
140            self.globals.logout=g.ndg.security.common.sso.cfg.logoutURI
141           
142        elif isSSOService:
143            try:
144                from ndg.security.server.sso.sso.config.ssoServiceMiddleware \
145                    import SSOMiddleware
146            except ImportError, e:
147                # If standalone flag is not present security must be enabled
148                raise NDGConfigError(\
149                    '%s: importing Single Sign On Service SSOMiddleware: %s' %\
150                        (__name__, e))
151                       
152               
153            self.app = SSOMiddleware(app, g, app_conf,
154                                     defSection='NDG_SECURITY.ssoService',
155                                     wssSection='NDG_SECURITY.wssecurity')
156           
157            self.globals.sslServer=g.ndg.security.server.sso.cfg.sslServer
158            self.globals.wayfuri=g.ndg.security.server.sso.cfg.wayfuri
159            self.globals.logout=g.ndg.security.server.sso.cfg.logoutURI
160            self.globals.getCredentials=g.ndg.security.server.sso.cfg.getCredentials
161
162        # Policy Enforcement Point initialisation
163        if securityEnabled:
164            try:
165                from ndg.security.common.authz.pep import PEP
166            except ImportError, e:
167                # If standalone flag is not present security must be enabled
168                raise NDGConfigError('%s: expecting standalone config ' % \
169                        __name__ + 
170                        'flag set to False for Policy Enforcement Point ' + \
171                        'import: %s' % e)
172           
173            self.globals.pep = PEP(cfg=cf.config, 
174                                   cfgSection='NDG_SECURITY.gatekeeper')
175           
176        self.config=cf
177       
178        self.__setUpHelpText(g)
179
180    def __getTrueFalseValue(self, val):
181        '''
182        Check for config values that could be true/false in a consistent manner
183        @param val: value read from config object
184        @return True, if val is some form of 'true'
185        '''
186        if val and val.lower() == 'true':
187            return True
188        return False
189
190    def __setUpHelpText(self, g):
191       
192        cf = CONFIG['app_conf']['helpConfigfile']
193        cf = myConfig(cf)
194        # NB, the cf.config class lowercases everything.
195        for (section, val) in cf.config.items('ATOM_EDITOR'):
196            setattr(g, section, val)
197           
198        # extend the granulite example link - to add a valid location
199        g.example_granulite = g.example_granulite.replace('HREF', \
200            h.url_for('example'))
201
202
203    def __call__(self,environ,start_response):
204       
205        environ['ndgConfig']=self.config
206        return self.app(environ,start_response)
207   
Note: See TracBrowser for help on using the repository browser.