3
|
1 '''
|
|
2 redirects depending on a configuration file an index.meta purl to an viewer
|
|
3 Created on 01.11.2012
|
|
4
|
|
5 @author: dwinter
|
|
6 '''
|
|
7
|
|
8 import web
|
9
|
9 import managePurls.manageIndexMetaPURLs as manageIndexMetaPURLs
|
3
|
10 import logging
|
15
|
11 import urllib
|
18
|
12 import urllib2
|
3
|
13
|
|
14 class redirector:
|
|
15
|
|
16 viewers={} # hash mit allen viewer name --> urls
|
|
17 purlHandler=None
|
|
18 def __init__(self):
|
|
19 #read config file for the viewers
|
18
|
20 confFile= file("/usr/local/metadataServices/purlService/restService/viewer.config")
|
|
21 #confFile= file("/Users/dwinter/Documents/Projekte/MetaDataManagement/purlService/restService/viewer.config")
|
3
|
22 self.purlHandler = manageIndexMetaPURLs.IndexMetaPURLManager()
|
|
23
|
|
24 for line in confFile.readlines():
|
|
25 splitted=line.split(",")
|
|
26
|
|
27 list=[]
|
|
28 if splitted[1]=="":
|
|
29 list.append(None) # index.meta werden von dieser einstellung nicht interpretiert
|
|
30 else:
|
|
31 list.append(splitted[1])
|
|
32
|
|
33 if len(splitted)>1: # url fur image viewer
|
|
34 if splitted[2]=="":
|
|
35 list.append(None) # index.meta werden von dieser einstellung nicht interpretiert
|
|
36 else:
|
|
37 list.append(splitted[2])
|
|
38 else:
|
|
39 list.append(None) # null wenn keiner konfiguriert wird. TODO: handle this
|
|
40
|
|
41
|
|
42 self.viewers[splitted[0]]=list
|
|
43
|
|
44 def GET(self,path):
|
|
45
|
15
|
46
|
|
47 params = web.input()
|
18
|
48
|
|
49 for key in params.keys():
|
|
50 if key=="query" and params[key]=="()": #leere frage wegwerfen
|
|
51 del params[key]
|
|
52
|
|
53
|
|
54
|
|
55
|
3
|
56 splitted=path.split("/")
|
|
57 if len(splitted)!=2: #pfrad sollte zwei anteile habe "flavour/purl"
|
|
58 raise web.notfound("not found")
|
|
59
|
|
60 purl = splitted[1]
|
|
61 flavour = splitted[0]
|
|
62
|
|
63 if flavour not in self.viewers.keys():
|
|
64 raise web.notfound("no viewer for %s"%flavour)
|
|
65
|
|
66 formats = self.viewers[flavour]
|
|
67
|
12
|
68 viewerWithIndexMetaFormatString = formats[0].rstrip().lstrip().replace("\n",'')
|
|
69 viewerWithImagePathFormatString = formats[1].lstrip().lstrip().replace("\n",'')
|
3
|
70
|
|
71
|
|
72 # checke ob es einen Image path gibt
|
|
73 path,validity = self.purlHandler.getImagePathValidity(purl)
|
|
74 if path is not None and path!="":
|
15
|
75 return self.handlePath(path,validity,viewerWithImagePathFormatString,params)
|
3
|
76
|
|
77
|
|
78 path,validity = self.purlHandler.getPathValidity(purl)
|
|
79
|
|
80 if path is not None and path !="":
|
15
|
81 return self.handlePath(path,validity,viewerWithIndexMetaFormatString,params)
|
3
|
82
|
|
83
|
|
84
|
|
85 #handle path
|
15
|
86 def handlePath(self,path,validity,viewerFormatString,params):
|
3
|
87
|
|
88 if viewerFormatString is None or viewerFormatString=="":
|
|
89 raise web.internalerror("no viewer configure for indexMeta for this flavour")
|
|
90
|
|
91 if path is None:
|
|
92 raise web.notfound("Cannnot find a URL to this path")
|
|
93
|
|
94 if validity is manageIndexMetaPURLs.PERM_NON_VALID:
|
|
95 raise web.notfound("PURL NON VALID ANYMORE!")
|
|
96
|
|
97 if validity is manageIndexMetaPURLs.TEMP_NON_VALID:
|
|
98 return web.notfound("PURL currently not VALID try later!")
|
|
99
|
15
|
100
|
|
101
|
18
|
102
|
15
|
103 if len(params.keys())>0:
|
18
|
104 ret=[key+"="+params[key] for key in params.keys()]
|
|
105 viewerUrl = viewerFormatString%path+'&'+u'&'.join(ret)
|
|
106
|
|
107
|
15
|
108 else:
|
|
109 viewerUrl = viewerFormatString%path
|
18
|
110
|
3
|
111 raise web.redirect(viewerUrl,"302 found")
|
|
112
|
|
113 if __name__ == '__main__':
|
11
|
114 pass
|