[IMP] document_webdav: removed DAV and refactore code
[odoo/odoo.git] / addons / document_webdav / webdav_server.py
1 # -*- encoding: utf-8 -*-
2
3 #
4 # Copyright P. Christeas <p_christ@hol.gr> 2008,2009
5 #
6 #
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsability of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # garantees and support are strongly adviced to contract a Free Software
12 # Service Company
13 #
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27 ###############################################################################
28
29
30 import netsvc
31 import tools
32 from dav_fs import tinydav_handler
33 from tools.config import config
34 from DAV.WebDAVServer import DAVRequestHandler
35 from service.websrv_lib import HTTPDir,FixSendError
36 from DAV.propfind import PROPFIND
37
38 import xml.dom.minidom
39 domimpl = xml.dom.minidom.getDOMImplementation()
40 import urlparse
41 import urllib
42 from DAV import utils
43
44 super_mk_prop_response = PROPFIND.mk_prop_response
45 def mk_prop_response(self,uri,good_props,bad_props,doc):        
46     """ make a new <prop> result element 
47
48     We differ between the good props and the bad ones for
49     each generating an extra <propstat>-Node (for each error
50     one, that means).
51     
52     """      
53     re=doc.createElement("D:response")
54     # append namespaces to response
55     nsnum=0
56     for nsname in self.namespaces:
57         re.setAttribute("xmlns:ns"+str(nsnum),nsname)
58         nsnum=nsnum+1
59     
60     # write href information
61     uparts=urlparse.urlparse(uri)
62     fileloc=uparts[2]
63     href=doc.createElement("D:href")
64     huri=doc.createTextNode(uparts[0]+'://'+'/'.join(uparts[1:2]) + urllib.quote(fileloc))
65     href.appendChild(huri)
66     re.appendChild(href)
67
68     # write good properties
69     ps=doc.createElement("D:propstat")
70     if good_props:
71         re.appendChild(ps)
72
73     gp=doc.createElement("D:prop")
74     for ns in good_props.keys():
75         ns_prefix="ns"+str(self.namespaces.index(ns))+":"
76         for p,v in good_props[ns].items():            
77             if not v:
78                 pass
79             pe=doc.createElement(ns_prefix+str(p))
80             if hasattr(v, '__class__') and v.__class__.__name__ == 'Element':
81                 pe.appendChild(v)
82             else:
83                 if p=="resourcetype":
84                     if v==1:
85                         ve=doc.createElement("D:collection")
86                         pe.appendChild(ve)
87                 else:
88                     ve=doc.createTextNode(tools.ustr(v))
89                     pe.appendChild(ve)
90
91             gp.appendChild(pe)
92     
93     ps.appendChild(gp)
94     s=doc.createElement("D:status")
95     t=doc.createTextNode("HTTP/1.1 200 OK")
96     s.appendChild(t)
97     ps.appendChild(s)
98     re.appendChild(ps)
99
100     # now write the errors!
101     if len(bad_props.items()):
102
103         # write a propstat for each error code
104         for ecode in bad_props.keys():
105             ps=doc.createElement("D:propstat")
106             re.appendChild(ps)
107             bp=doc.createElement("D:prop")
108             ps.appendChild(bp)
109
110             for ns in bad_props[ecode].keys():
111                 ns_prefix="ns"+str(self.namespaces.index(ns))+":"
112             
113             for p in bad_props[ecode][ns]:
114                 pe=doc.createElement(ns_prefix+str(p))
115                 bp.appendChild(pe)
116             
117             s=doc.createElement("D:status")
118             t=doc.createTextNode(utils.gen_estring(ecode))
119             s.appendChild(t)
120             ps.appendChild(s)
121             re.appendChild(ps)
122
123     # return the new response element
124     return re
125     
126
127 PROPFIND.mk_prop_response = mk_prop_response
128
129 def OpenDAVConfig(**kw):
130     class OpenDAV:
131         def __init__(self, **kw):
132             self.__dict__.update(**kw)
133
134     class Config:
135         DAV = OpenDAV(**kw)
136
137     return Config()
138
139 class DAVHandler(FixSendError,DAVRequestHandler):
140     verbose = False
141     
142     def get_userinfo(self,user,pw):
143         print "get_userinfo"
144         return False
145     def _log(self, message):
146         netsvc.Logger().notifyChannel("webdav",netsvc.LOG_DEBUG,message)
147     
148     def handle(self):
149         self._init_buffer()
150
151     def finish(self):
152         pass
153
154     def get_db_from_path(self, uri):
155         if uri or uri == '/':
156             dbs = self.IFACE_CLASS.db_list()
157             res = len(dbs) and dbs[0] or False
158         else:
159             res =  self.IFACE_CLASS.get_db(uri)        
160         return res
161
162     def setup(self):
163         davpath = '/'+config.get_misc('webdav','vdir','webdav')
164         self.baseuri = "http://%s:%d/"% (self.server.server_name,self.server.server_port)
165         self.IFACE_CLASS  = tinydav_handler(self, self.verbose)
166         
167     
168     def log_message(self, format, *args):
169         netsvc.Logger().notifyChannel('webdav',netsvc.LOG_DEBUG_RPC,format % args)
170
171     def log_error(self, format, *args):
172         netsvc.Logger().notifyChannel('xmlrpc',netsvc.LOG_WARNING,format % args)
173
174
175 try:
176     from service.http_server import reg_http_service,OpenERPAuthProvider   
177
178     if (config.get_misc('webdav','enable',True)):
179         directory = '/'+config.get_misc('webdav','vdir','webdav') 
180         handler = DAVHandler
181         verbose = config.get_misc('webdav','verbose',True)
182         handler.debug = config.get_misc('webdav','debug',True)
183         _dc = { 'verbose' : verbose,
184                 'directory' : directory,
185                 'lockemulation' : False,
186                     
187                 }
188
189         conf = OpenDAVConfig(**_dc)
190         handler._config = conf
191         reg_http_service(HTTPDir(directory,DAVHandler,OpenERPAuthProvider()))
192         netsvc.Logger().notifyChannel('webdav',netsvc.LOG_INFO,"WebDAV service registered at path: %s/ "% directory)
193 except Exception, e:
194     logger = netsvc.Logger()
195     logger.notifyChannel('webdav', netsvc.LOG_ERROR, 'Cannot launch webdav: %s' % e)
196
197 #eof
198
199
200