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

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

Simplify structure of discovery controller, using the results template
to render searches even when no results are found. Add new error
dict to avoid confusing with the error dict used by the formencode
input checker. Add additional input checks and improve error handling.
Add dropdown list to select order by values + add new constants
module to store associated data and error keys + tidy up DiscoveryState?
object to make clearer and logic more consistent + fix paging controls
for results data.

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