1 # -*- encoding: utf-8 -*-
4 # Copyright P. Christeas <p_christ@hol.gr> 2008,2009
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
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.
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.
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 ###############################################################################
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
38 import xml.dom.minidom
39 domimpl = xml.dom.minidom.getDOMImplementation()
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
48 We differ between the good props and the bad ones for
49 each generating an extra <propstat>-Node (for each error
53 re=doc.createElement("D:response")
54 # append namespaces to response
56 for nsname in self.namespaces:
57 re.setAttribute("xmlns:ns"+str(nsnum),nsname)
60 # write href information
61 uparts=urlparse.urlparse(uri)
63 href=doc.createElement("D:href")
64 huri=doc.createTextNode(uparts[0]+'://'+'/'.join(uparts[1:2]) + urllib.quote(fileloc))
65 href.appendChild(huri)
68 # write good properties
69 ps=doc.createElement("D:propstat")
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():
79 pe=doc.createElement(ns_prefix+str(p))
80 if hasattr(v, '__class__') and v.__class__.__name__ == 'Element':
85 ve=doc.createElement("D:collection")
88 ve=doc.createTextNode(tools.ustr(v))
94 s=doc.createElement("D:status")
95 t=doc.createTextNode("HTTP/1.1 200 OK")
100 # now write the errors!
101 if len(bad_props.items()):
103 # write a propstat for each error code
104 for ecode in bad_props.keys():
105 ps=doc.createElement("D:propstat")
107 bp=doc.createElement("D:prop")
110 for ns in bad_props[ecode].keys():
111 ns_prefix="ns"+str(self.namespaces.index(ns))+":"
113 for p in bad_props[ecode][ns]:
114 pe=doc.createElement(ns_prefix+str(p))
117 s=doc.createElement("D:status")
118 t=doc.createTextNode(utils.gen_estring(ecode))
123 # return the new response element
127 PROPFIND.mk_prop_response = mk_prop_response
129 def OpenDAVConfig(**kw):
131 def __init__(self, **kw):
132 self.__dict__.update(**kw)
139 class DAVHandler(FixSendError,DAVRequestHandler):
142 def get_userinfo(self,user,pw):
145 def _log(self, message):
146 netsvc.Logger().notifyChannel("webdav",netsvc.LOG_DEBUG,message)
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
159 res = self.IFACE_CLASS.get_db(uri)
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)
168 def log_message(self, format, *args):
169 netsvc.Logger().notifyChannel('webdav',netsvc.LOG_DEBUG_RPC,format % args)
171 def log_error(self, format, *args):
172 netsvc.Logger().notifyChannel('xmlrpc',netsvc.LOG_WARNING,format % args)
176 from service.http_server import reg_http_service,OpenERPAuthProvider
178 if (config.get_misc('webdav','enable',True)):
179 directory = '/'+config.get_misc('webdav','vdir','webdav')
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,
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)
194 logger = netsvc.Logger()
195 logger.notifyChannel('webdav', netsvc.LOG_ERROR, 'Cannot launch webdav: %s' % e)