view restService/redirector.py @ 11:fad73212354b

wsgi addedx
author dwinter
date Mon, 05 Nov 2012 09:29:21 +0100
parents 5f5447b3a082
children 0287aed01f2b
line wrap: on
line source

'''
redirects depending on a configuration file an index.meta purl to an viewer
Created on 01.11.2012

@author: dwinter
'''

import web
import managePurls.manageIndexMetaPURLs as manageIndexMetaPURLs
import logging

class redirector:
    
    viewers={} # hash mit allen viewer name --> urls
    purlHandler=None
    def __init__(self):
        #read config file for the viewers
        confFile= file("/usr/local/metadataServices/purlService/restService/viewer.config")
        
        self.purlHandler = manageIndexMetaPURLs.IndexMetaPURLManager()
        
        for line in confFile.readlines():
            splitted=line.split(",")
            
            list=[]
            if splitted[1]=="":
                list.append(None) # index.meta werden von dieser einstellung nicht interpretiert
            else:
                list.append(splitted[1])
                
            if len(splitted)>1: # url fur image viewer
                if splitted[2]=="":
                    list.append(None) # index.meta werden von dieser einstellung nicht interpretiert
                else:
                    list.append(splitted[2])
            else:
                list.append(None) # null wenn keiner konfiguriert wird. TODO: handle this
            
            
            self.viewers[splitted[0]]=list
                
    def GET(self,path):
        
        splitted=path.split("/")
        if len(splitted)!=2: #pfrad sollte zwei anteile habe "flavour/purl"
            raise web.notfound("not found")
        
        purl = splitted[1] 
        flavour = splitted[0]
        
        if flavour not in self.viewers.keys():
            raise web.notfound("no viewer for %s"%flavour)
        
        formats = self.viewers[flavour]
        
        viewerWithIndexMetaFormatString =  formats[0];
        viewerWithImagePathFormatString = formats[1];
        
      
        # checke ob es einen Image path gibt
        path,validity = self.purlHandler.getImagePathValidity(purl)
        if path is not None and path!="":
            return self.handlePath(path,validity,viewerWithImagePathFormatString)
        
        
        path,validity = self.purlHandler.getPathValidity(purl)
        
        if path is not None and path !="":
            return self.handlePath(path,validity,viewerWithIndexMetaFormatString)
        
    
    
    #handle path 
    def handlePath(self,path,validity,viewerFormatString):
        
        if viewerFormatString is None or viewerFormatString=="": 
            raise web.internalerror("no viewer configure for indexMeta for this flavour")
        
        if path is None:
            raise web.notfound("Cannnot find a URL to this path")
        
        if validity is manageIndexMetaPURLs.PERM_NON_VALID:
            raise web.notfound("PURL NON VALID ANYMORE!")
         
        if validity is manageIndexMetaPURLs.TEMP_NON_VALID:
            return web.notfound("PURL currently not VALID try later!")
        
    
        viewerUrl = viewerFormatString%path
        
        print viewerUrl
        raise web.redirect(viewerUrl,"302 found")
        
if __name__ == '__main__':
    pass